001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.importexport;
029
030import org.opencms.configuration.CmsParameterConfiguration;
031import org.opencms.file.CmsFile;
032import org.opencms.file.CmsObject;
033import org.opencms.file.CmsProperty;
034import org.opencms.file.CmsResource;
035import org.opencms.file.CmsResourceFilter;
036import org.opencms.file.types.CmsResourceTypePlain;
037import org.opencms.file.types.I_CmsResourceType;
038import org.opencms.i18n.CmsMessageContainer;
039import org.opencms.loader.CmsLoaderException;
040import org.opencms.main.CmsException;
041import org.opencms.main.CmsLog;
042import org.opencms.main.OpenCms;
043import org.opencms.relations.CmsRelation;
044import org.opencms.relations.CmsRelationType;
045import org.opencms.relations.I_CmsLinkParseable;
046import org.opencms.report.I_CmsReport;
047import org.opencms.security.CmsAccessControlEntry;
048import org.opencms.security.CmsRole;
049import org.opencms.security.I_CmsPasswordHandler;
050import org.opencms.security.I_CmsPrincipal;
051import org.opencms.util.CmsDateUtil;
052import org.opencms.util.CmsUUID;
053import org.opencms.xml.CmsXmlException;
054import org.opencms.xml.CmsXmlUtils;
055
056import java.io.File;
057import java.io.IOException;
058import java.text.ParseException;
059import java.util.ArrayList;
060import java.util.Collections;
061import java.util.HashMap;
062import java.util.Iterator;
063import java.util.List;
064import java.util.Map;
065import java.util.Map.Entry;
066import java.util.zip.ZipFile;
067
068import org.apache.commons.logging.Log;
069
070import org.dom4j.Document;
071import org.dom4j.Element;
072import org.dom4j.Node;
073
074/**
075 * Implementation of the OpenCms Import Interface ({@link org.opencms.importexport.I_CmsImport}) for
076 * the import version 5.<p>
077 *
078 * This import format is used in OpenCms since 6.3.0.<p>
079 *
080 * @since 6.3.0
081 *
082 * @see org.opencms.importexport.A_CmsImport
083 *
084 * @deprecated this import class is no longer in use and should only be used to import old export files
085 */
086@Deprecated
087public class CmsImportVersion5 extends A_CmsImport {
088
089    /** The version number of this import implementation.<p> */
090    public static final int IMPORT_VERSION = 5;
091
092    /** The log object for this class. */
093    private static final Log LOG = CmsLog.getLog(CmsImportVersion5.class);
094
095    /** Stores all relations defined in the import file to be created after all resources has been imported. */
096    protected Map<String, List<CmsRelation>> m_importedRelations;
097
098    /** Stores all resources of any type that implements the {@link I_CmsLinkParseable} interface. */
099    protected List<CmsResource> m_parseables;
100
101    /** The keep permissions flag. */
102    protected boolean m_keepPermissions;
103
104    /**
105     * Creates a new CmsImportVerion7 object.<p>
106     */
107    public CmsImportVersion5() {
108
109        m_convertToXmlPage = true;
110    }
111
112    /**
113     * @see org.opencms.importexport.I_CmsImport#getVersion()
114     */
115    public int getVersion() {
116
117        return CmsImportVersion5.IMPORT_VERSION;
118    }
119
120    /**
121     * @see org.opencms.importexport.I_CmsImport#importData(CmsObject, I_CmsReport, CmsImportParameters)
122     */
123    public void importData(CmsObject cms, I_CmsReport report, CmsImportParameters params)
124    throws CmsImportExportException, CmsXmlException {
125
126        // initialize the import
127        initialize();
128        m_cms = cms;
129        m_importPath = params.getDestinationPath();
130        m_report = report;
131        m_keepPermissions = params.isKeepPermissions();
132
133        m_linkStorage = new HashMap<String, String>();
134        m_linkPropertyStorage = new HashMap<String, List<CmsProperty>>();
135        m_parseables = new ArrayList<CmsResource>();
136        m_importedRelations = new HashMap<String, List<CmsRelation>>();
137
138        CmsImportHelper helper = new CmsImportHelper(params);
139        try {
140            helper.openFile();
141            m_importResource = helper.getFolder();
142            m_importZip = helper.getZipFile();
143            m_docXml = CmsXmlUtils.unmarshalHelper(helper.getFileBytes(CmsImportExportManager.EXPORT_MANIFEST), null);
144            // first import the user information
145            if (OpenCms.getRoleManager().hasRole(m_cms, CmsRole.ACCOUNT_MANAGER)) {
146                importGroups();
147                importUsers();
148            }
149            // now import the VFS resources
150            readResourcesFromManifest();
151            convertPointerToSiblings();
152            rewriteParseables();
153            importRelations();
154        } catch (IOException ioe) {
155            CmsMessageContainer msg = Messages.get().container(
156                Messages.ERR_IMPORTEXPORT_ERROR_READING_FILE_1,
157                CmsImportExportManager.EXPORT_MANIFEST);
158            if (LOG.isErrorEnabled()) {
159                LOG.error(msg.key(), ioe);
160            }
161            throw new CmsImportExportException(msg, ioe);
162        } finally {
163            helper.closeFile();
164            cleanUp();
165        }
166    }
167
168    /**
169     * @see org.opencms.importexport.I_CmsImport#importResources(org.opencms.file.CmsObject, java.lang.String, org.opencms.report.I_CmsReport, java.io.File, java.util.zip.ZipFile, org.dom4j.Document)
170     *
171     * @deprecated use {@link #importData(CmsObject, I_CmsReport, CmsImportParameters)} instead
172     */
173    @Deprecated
174    public void importResources(
175        CmsObject cms,
176        String importPath,
177        I_CmsReport report,
178        File importResource,
179        ZipFile importZip,
180        Document docXml)
181    throws CmsImportExportException {
182
183        CmsImportParameters params = new CmsImportParameters(
184            importResource != null ? importResource.getAbsolutePath() : importZip.getName(),
185            importPath,
186            false);
187
188        try {
189            importData(cms, report, params);
190        } catch (CmsXmlException e) {
191            throw new CmsImportExportException(e.getMessageContainer(), e);
192        }
193    }
194
195    /**
196     * Convert a given time stamp from a String format to a long value.<p>
197     *
198     * The time stamp is either the string representation of a long value (old export format)
199     * or a user-readable string format.
200     *
201     * @param timestamp time stamp to convert
202     *
203     * @return long value of the time stamp
204     */
205    protected long convertTimestamp(String timestamp) {
206
207        long value = 0;
208        // try to parse the time stamp string
209        // if it successes, its an old style long value
210        try {
211            value = Long.parseLong(timestamp);
212
213        } catch (NumberFormatException e) {
214            // the time stamp was in in a user-readable string format, create the long value form it
215            try {
216                value = CmsDateUtil.parseHeaderDate(timestamp);
217            } catch (ParseException pe) {
218                value = System.currentTimeMillis();
219            }
220        }
221        return value;
222    }
223
224    /**
225     * Imports the relations.<p>
226     */
227    protected void importRelations() {
228
229        if (m_importedRelations.isEmpty()) {
230            return;
231        }
232
233        m_report.println(Messages.get().container(Messages.RPT_START_IMPORT_RELATIONS_0), I_CmsReport.FORMAT_HEADLINE);
234
235        int i = 0;
236        Iterator<Entry<String, List<CmsRelation>>> it = m_importedRelations.entrySet().iterator();
237        while (it.hasNext()) {
238            Entry<String, List<CmsRelation>> entry = it.next();
239            String resourcePath = entry.getKey();
240            List<CmsRelation> relations = entry.getValue();
241
242            m_report.print(
243                org.opencms.report.Messages.get().container(
244                    org.opencms.report.Messages.RPT_SUCCESSION_2,
245                    String.valueOf(i + 1),
246                    String.valueOf(m_importedRelations.size())),
247                I_CmsReport.FORMAT_NOTE);
248
249            m_report.print(
250                Messages.get().container(
251                    Messages.RPT_IMPORTING_RELATIONS_FOR_2,
252                    resourcePath,
253                    Integer.valueOf(relations.size())),
254                I_CmsReport.FORMAT_NOTE);
255            m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
256
257            boolean withErrors = false;
258            Iterator<CmsRelation> itRelations = relations.iterator();
259            while (itRelations.hasNext()) {
260                CmsRelation relation = itRelations.next();
261                try {
262                    // Add the relation to the resource
263                    m_cms.importRelation(
264                        m_cms.getSitePath(relation.getSource(m_cms, CmsResourceFilter.ALL)),
265                        m_cms.getSitePath(relation.getTarget(m_cms, CmsResourceFilter.ALL)),
266                        relation.getType().getName());
267                } catch (CmsException e) {
268                    m_report.addWarning(e);
269                    withErrors = true;
270                    if (LOG.isWarnEnabled()) {
271                        LOG.warn(e.getLocalizedMessage());
272                    }
273                    if (LOG.isDebugEnabled()) {
274                        LOG.debug(e.getLocalizedMessage(), e);
275                    }
276                }
277            }
278            if (!withErrors) {
279                m_report.println(
280                    org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
281                    I_CmsReport.FORMAT_OK);
282            } else {
283                m_report.println(
284                    org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_FAILED_0),
285                    I_CmsReport.FORMAT_ERROR);
286            }
287            i++;
288        }
289
290        m_report.println(Messages.get().container(Messages.RPT_END_IMPORT_RELATIONS_0), I_CmsReport.FORMAT_HEADLINE);
291    }
292
293    /**
294     * Reads all the relations of the resource from the <code>manifest.xml</code> file
295     * and adds them to the according resource.<p>
296     *
297     * @param resource the resource to import the relations for
298     * @param parentElement the current element
299     */
300    protected void importRelations(CmsResource resource, Element parentElement) {
301
302        // Get the nodes for the relations
303        @SuppressWarnings("unchecked")
304        List<Node> relationElements = parentElement.selectNodes(
305            "./" + A_CmsImport.N_RELATIONS + "/" + A_CmsImport.N_RELATION);
306
307        List<CmsRelation> relations = new ArrayList<CmsRelation>();
308        // iterate over the nodes
309        Iterator<Node> itRelations = relationElements.iterator();
310        while (itRelations.hasNext()) {
311            Element relationElement = (Element)itRelations.next();
312            String structureID = getChildElementTextValue(relationElement, A_CmsImport.N_RELATION_ATTRIBUTE_ID);
313            String targetPath = getChildElementTextValue(relationElement, A_CmsImport.N_RELATION_ATTRIBUTE_PATH);
314            String relationType = getChildElementTextValue(relationElement, A_CmsImport.N_RELATION_ATTRIBUTE_TYPE);
315            CmsUUID targetId = new CmsUUID(structureID);
316            CmsRelationType type = CmsRelationType.valueOf(relationType);
317
318            CmsRelation relation = new CmsRelation(
319                resource.getStructureId(),
320                resource.getRootPath(),
321                targetId,
322                targetPath,
323                type);
324
325            relations.add(relation);
326        }
327
328        if (!relations.isEmpty()) {
329            m_importedRelations.put(resource.getRootPath(), relations);
330        }
331    }
332
333    /**
334     * Imports a resource (file or folder) into the cms.<p>
335     *
336     * @param source the path to the source-file
337     * @param destination the path to the destination-file in the cms
338     * @param type the resource type name of the file
339     * @param uuidstructure the structure uuid of the resource
340     * @param uuidresource the resource uuid of the resource
341     * @param datelastmodified the last modification date of the resource
342     * @param userlastmodified the user who made the last modifications to the resource
343     * @param datecreated the creation date of the resource
344     * @param usercreated the user who created
345     * @param datereleased the release date of the resource
346     * @param dateexpired the expire date of the resource
347     * @param flags the flags of the resource
348     * @param properties a list with properties for this resource
349     *
350     * @return imported resource
351     */
352    protected CmsResource importResource(
353        String source,
354        String destination,
355        I_CmsResourceType type,
356        String uuidstructure,
357        String uuidresource,
358        long datelastmodified,
359        String userlastmodified,
360        long datecreated,
361        String usercreated,
362        long datereleased,
363        long dateexpired,
364        String flags,
365        List<CmsProperty> properties) {
366
367        byte[] content = null;
368        CmsResource result = null;
369
370        try {
371
372            // get the file content
373            if (source != null) {
374                content = getFileBytes(source);
375            }
376            int size = 0;
377            if (content != null) {
378                size = content.length;
379            }
380
381            // get UUIDs for the user
382            CmsUUID newUserlastmodified;
383            CmsUUID newUsercreated;
384            // check if user created and user last modified are valid users in this system.
385            // if not, use the current user
386            try {
387                newUserlastmodified = m_cms.readUser(userlastmodified).getId();
388            } catch (CmsException e) {
389                newUserlastmodified = m_cms.getRequestContext().getCurrentUser().getId();
390                // datelastmodified = System.currentTimeMillis();
391            }
392
393            try {
394                newUsercreated = m_cms.readUser(usercreated).getId();
395            } catch (CmsException e) {
396                newUsercreated = m_cms.getRequestContext().getCurrentUser().getId();
397                // datecreated = System.currentTimeMillis();
398            }
399
400            // get UUID for the structure
401            CmsUUID newUuidstructure = null;
402            if (uuidstructure != null) {
403                // create a UUID from the provided string
404                newUuidstructure = new CmsUUID(uuidstructure);
405            } else {
406                // if null generate a new structure id
407                newUuidstructure = new CmsUUID();
408            }
409
410            // get UUIDs for the resource and content
411            CmsUUID newUuidresource = null;
412            if ((uuidresource != null) && (!type.isFolder())) {
413                // create a UUID from the provided string
414                newUuidresource = new CmsUUID(uuidresource);
415            } else {
416                // folders get always a new resource record UUID
417                newUuidresource = new CmsUUID();
418            }
419
420            // create a new CmsResource
421            CmsResource resource = new CmsResource(
422                newUuidstructure,
423                newUuidresource,
424                destination,
425                type.getTypeId(),
426                type.isFolder(),
427                Integer.valueOf(flags).intValue(),
428                m_cms.getRequestContext().getCurrentProject().getUuid(),
429                CmsResource.STATE_NEW,
430                datecreated,
431                newUsercreated,
432                datelastmodified,
433                newUserlastmodified,
434                datereleased,
435                dateexpired,
436                1,
437                size,
438                System.currentTimeMillis(),
439                0);
440
441            // import this resource in the VFS
442            result = m_cms.importResource(destination, resource, content, properties);
443
444            if (result != null) {
445                m_report.println(
446                    org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
447                    I_CmsReport.FORMAT_OK);
448            }
449        } catch (Exception exc) {
450            // an error while importing the file
451            m_report.println(exc);
452            try {
453                // Sleep some time after an error so that the report output has a chance to keep up
454                Thread.sleep(1000);
455            } catch (Exception e) {
456                //
457            }
458        }
459        return result;
460    }
461
462    /**
463     * @see org.opencms.importexport.A_CmsImport#importUser(String, String, String, String, String, String, long, Map, List)
464     */
465    @Override
466    protected void importUser(
467        String name,
468        String flags,
469        String password,
470        String firstname,
471        String lastname,
472        String email,
473        long dateCreated,
474        Map<String, Object> userInfo,
475        List<String> userGroups)
476    throws CmsImportExportException {
477
478        boolean convert = false;
479
480        CmsParameterConfiguration config = OpenCms.getPasswordHandler().getConfiguration();
481        if ((config != null) && config.containsKey(I_CmsPasswordHandler.CONVERT_DIGEST_ENCODING)) {
482            convert = config.getBoolean(I_CmsPasswordHandler.CONVERT_DIGEST_ENCODING, false);
483        }
484
485        if (convert) {
486            password = convertDigestEncoding(password);
487        }
488
489        super.importUser(name, flags, password, firstname, lastname, email, dateCreated, userInfo, userGroups);
490    }
491
492    /**
493     * Reads all file nodes plus their meta-information (properties, ACL)
494     * from the <code>manifest.xml</code> and imports them as Cms resources to the VFS.<p>
495     *
496     * @throws CmsImportExportException if something goes wrong
497     */
498    @SuppressWarnings("unchecked")
499    protected void readResourcesFromManifest() throws CmsImportExportException {
500
501        String source = null, destination = null, uuidstructure = null, uuidresource = null, userlastmodified = null,
502        usercreated = null, flags = null, timestamp = null;
503        long datelastmodified = 0, datecreated = 0, datereleased = 0, dateexpired = 0;
504
505        List<Node> fileNodes = null, acentryNodes = null;
506        Element currentElement = null, currentEntry = null;
507        List<CmsProperty> properties = null;
508
509        // get list of immutable resources
510        List<String> immutableResources = OpenCms.getImportExportManager().getImmutableResources();
511        if (immutableResources == null) {
512            immutableResources = Collections.emptyList();
513        }
514        if (LOG.isDebugEnabled()) {
515            LOG.debug(
516                Messages.get().getBundle().key(
517                    Messages.LOG_IMPORTEXPORT_IMMUTABLE_RESOURCES_SIZE_1,
518                    Integer.toString(immutableResources.size())));
519        }
520        // get list of ignored properties
521        List<String> ignoredProperties = OpenCms.getImportExportManager().getIgnoredProperties();
522        if (ignoredProperties == null) {
523            ignoredProperties = Collections.emptyList();
524        }
525
526        // get the desired page type for imported pages
527        m_convertToXmlPage = OpenCms.getImportExportManager().convertToXmlPage();
528
529        try {
530            // get all file-nodes
531            fileNodes = m_docXml.selectNodes("//" + A_CmsImport.N_FILE);
532            int importSize = fileNodes.size();
533
534            // walk through all files in manifest
535            for (int i = 0; i < fileNodes.size(); i++) {
536                m_report.print(
537                    org.opencms.report.Messages.get().container(
538                        org.opencms.report.Messages.RPT_SUCCESSION_2,
539                        String.valueOf(i + 1),
540                        String.valueOf(importSize)),
541                    I_CmsReport.FORMAT_NOTE);
542                currentElement = (Element)fileNodes.get(i);
543
544                // <source>
545                source = getChildElementTextValue(currentElement, A_CmsImport.N_SOURCE);
546                // <destination>
547                destination = getChildElementTextValue(currentElement, A_CmsImport.N_DESTINATION);
548
549                // <type>
550                String typeName = getChildElementTextValue(currentElement, A_CmsImport.N_TYPE);
551                I_CmsResourceType type;
552                try {
553                    type = OpenCms.getResourceManager().getResourceType(typeName);
554                } catch (CmsLoaderException e) {
555                    int plainId;
556                    try {
557                        plainId = OpenCms.getResourceManager().getResourceType(
558                            CmsResourceTypePlain.getStaticTypeName()).getTypeId();
559                    } catch (CmsLoaderException e1) {
560                        // this should really never happen
561                        plainId = CmsResourceTypePlain.getStaticTypeId();
562                    }
563                    type = OpenCms.getResourceManager().getResourceType(plainId);
564                }
565
566                // <uuidstructure>
567                uuidstructure = getChildElementTextValue(currentElement, A_CmsImport.N_UUIDSTRUCTURE);
568
569                // <uuidresource>
570                if (!type.isFolder()) {
571                    uuidresource = getChildElementTextValue(currentElement, A_CmsImport.N_UUIDRESOURCE);
572                } else {
573                    uuidresource = null;
574                }
575
576                // <datelastmodified>
577                timestamp = getChildElementTextValue(currentElement, A_CmsImport.N_DATELASTMODIFIED);
578                if (timestamp != null) {
579                    datelastmodified = convertTimestamp(timestamp);
580                } else {
581                    datelastmodified = System.currentTimeMillis();
582                }
583
584                // <userlastmodified>
585                userlastmodified = getChildElementTextValue(currentElement, A_CmsImport.N_USERLASTMODIFIED);
586                userlastmodified = OpenCms.getImportExportManager().translateUser(userlastmodified);
587
588                // <datecreated>
589                timestamp = getChildElementTextValue(currentElement, A_CmsImport.N_DATECREATED);
590                if (timestamp != null) {
591                    datecreated = convertTimestamp(timestamp);
592                } else {
593                    datecreated = System.currentTimeMillis();
594                }
595
596                // <usercreated>
597                usercreated = getChildElementTextValue(currentElement, A_CmsImport.N_USERCREATED);
598                usercreated = OpenCms.getImportExportManager().translateUser(usercreated);
599
600                // <datereleased>
601                timestamp = getChildElementTextValue(currentElement, A_CmsImport.N_DATERELEASED);
602                if (timestamp != null) {
603                    datereleased = convertTimestamp(timestamp);
604                } else {
605                    datereleased = CmsResource.DATE_RELEASED_DEFAULT;
606                }
607
608                // <dateexpired>
609                timestamp = getChildElementTextValue(currentElement, A_CmsImport.N_DATEEXPIRED);
610                if (timestamp != null) {
611                    dateexpired = convertTimestamp(timestamp);
612                } else {
613                    dateexpired = CmsResource.DATE_EXPIRED_DEFAULT;
614                }
615
616                // <flags>
617                flags = getChildElementTextValue(currentElement, A_CmsImport.N_FLAGS);
618
619                // apply name translation and import path
620                String translatedName = m_cms.getRequestContext().addSiteRoot(m_importPath + destination);
621                if (type.isFolder()) {
622                    // ensure folders end with a "/"
623                    if (!CmsResource.isFolder(translatedName)) {
624                        translatedName += "/";
625                    }
626                }
627
628                // check if this resource is immutable
629                boolean resourceNotImmutable = checkImmutable(translatedName, immutableResources);
630                translatedName = m_cms.getRequestContext().removeSiteRoot(translatedName);
631                // if the resource is not immutable and not on the exclude list, import it
632                if (resourceNotImmutable) {
633                    // print out the information to the report
634                    m_report.print(Messages.get().container(Messages.RPT_IMPORTING_0), I_CmsReport.FORMAT_NOTE);
635                    m_report.print(
636                        org.opencms.report.Messages.get().container(
637                            org.opencms.report.Messages.RPT_ARGUMENT_1,
638                            translatedName));
639                    m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
640                    // get all properties
641                    properties = readPropertiesFromManifest(currentElement, ignoredProperties);
642
643                    boolean exists = m_cms.existsResource(translatedName, CmsResourceFilter.ALL);
644
645                    // import the resource
646                    CmsResource res = importResource(
647                        source,
648                        translatedName,
649                        type,
650                        uuidstructure,
651                        uuidresource,
652                        datelastmodified,
653                        userlastmodified,
654                        datecreated,
655                        usercreated,
656                        datereleased,
657                        dateexpired,
658                        flags,
659                        properties);
660
661                    if (res != null) {
662                        // only set permissions if the resource did not exists or if the keep permissions flag is not set
663                        if (!exists || !m_keepPermissions) {
664                            // if the resource was imported add the access control entries if available
665                            List<CmsAccessControlEntry> aceList = new ArrayList<CmsAccessControlEntry>();
666
667                            // write all imported access control entries for this file
668                            acentryNodes = currentElement.selectNodes("*/" + A_CmsImport.N_ACCESSCONTROL_ENTRY);
669
670                            // collect all access control entries
671                            for (int j = 0; j < acentryNodes.size(); j++) {
672                                currentEntry = (Element)acentryNodes.get(j);
673
674                                // get the data of the access control entry
675                                String id = getChildElementTextValue(
676                                    currentEntry,
677                                    A_CmsImport.N_ACCESSCONTROL_PRINCIPAL);
678                                String principalId = new CmsUUID().toString();
679                                String principal = id.substring(id.indexOf('.') + 1, id.length());
680
681                                try {
682                                    if (id.startsWith(I_CmsPrincipal.PRINCIPAL_GROUP)) {
683                                        principal = OpenCms.getImportExportManager().translateGroup(principal);
684                                        principalId = m_cms.readGroup(principal).getId().toString();
685                                    } else if (id.startsWith(I_CmsPrincipal.PRINCIPAL_USER)) {
686                                        principal = OpenCms.getImportExportManager().translateUser(principal);
687                                        principalId = m_cms.readUser(principal).getId().toString();
688                                    } else if (id.startsWith(CmsRole.PRINCIPAL_ROLE)) {
689                                        principalId = CmsRole.valueOfRoleName(principal).getId().toString();
690                                    } else if (id.equalsIgnoreCase(CmsAccessControlEntry.PRINCIPAL_ALL_OTHERS_NAME)) {
691                                        principalId = CmsAccessControlEntry.PRINCIPAL_ALL_OTHERS_ID.toString();
692                                    } else if (id.equalsIgnoreCase(
693                                        CmsAccessControlEntry.PRINCIPAL_OVERWRITE_ALL_NAME)) {
694                                        principalId = CmsAccessControlEntry.PRINCIPAL_OVERWRITE_ALL_ID.toString();
695                                    } else {
696                                        if (LOG.isWarnEnabled()) {
697                                            LOG.warn(
698                                                Messages.get().getBundle().key(
699                                                    Messages.LOG_IMPORTEXPORT_ERROR_IMPORTING_ACE_1,
700                                                    id));
701                                        }
702                                    }
703
704                                    String acflags = getChildElementTextValue(currentEntry, A_CmsImport.N_FLAGS);
705
706                                    String allowed = ((Element)currentEntry.selectNodes("./"
707                                        + A_CmsImport.N_ACCESSCONTROL_PERMISSIONSET
708                                        + "/"
709                                        + A_CmsImport.N_ACCESSCONTROL_ALLOWEDPERMISSIONS).get(0)).getTextTrim();
710
711                                    String denied = ((Element)currentEntry.selectNodes("./"
712                                        + A_CmsImport.N_ACCESSCONTROL_PERMISSIONSET
713                                        + "/"
714                                        + A_CmsImport.N_ACCESSCONTROL_DENIEDPERMISSIONS).get(0)).getTextTrim();
715
716                                    // add the entry to the list
717                                    aceList.add(
718                                        getImportAccessControlEntry(res, principalId, allowed, denied, acflags));
719                                } catch (CmsException e) {
720                                    // user or group of ACE might not exist in target system, ignore ACE
721                                    if (LOG.isWarnEnabled()) {
722                                        LOG.warn(
723                                            Messages.get().getBundle().key(
724                                                Messages.LOG_IMPORTEXPORT_ERROR_IMPORTING_ACE_1,
725                                                translatedName),
726                                            e);
727                                    }
728                                    m_report.println(e);
729                                    m_report.addError(e);
730                                }
731                            }
732                            importAccessControlEntries(res, aceList);
733                        }
734
735                        // Add the relations for the resource
736                        importRelations(res, currentElement);
737
738                        if (OpenCms.getResourceManager().getResourceType(
739                            res.getTypeId()) instanceof I_CmsLinkParseable) {
740                            // store for later use
741                            m_parseables.add(res);
742                        }
743
744                        if (LOG.isInfoEnabled()) {
745                            LOG.info(
746                                Messages.get().getBundle().key(
747                                    Messages.LOG_IMPORTING_4,
748                                    new Object[] {
749                                        String.valueOf(i + 1),
750                                        String.valueOf(importSize),
751                                        translatedName,
752                                        destination}));
753                        }
754                    } else {
755                        // resource import failed, since no CmsResource was created
756                        m_report.print(Messages.get().container(Messages.RPT_SKIPPING_0), I_CmsReport.FORMAT_NOTE);
757                        m_report.println(
758                            org.opencms.report.Messages.get().container(
759                                org.opencms.report.Messages.RPT_ARGUMENT_1,
760                                translatedName));
761
762                        if (LOG.isInfoEnabled()) {
763                            LOG.info(
764                                Messages.get().getBundle().key(
765                                    Messages.LOG_SKIPPING_3,
766                                    String.valueOf(i + 1),
767                                    String.valueOf(importSize),
768                                    translatedName));
769                        }
770                    }
771
772                } else {
773                    // skip the file import, just print out the information to the report
774
775                    m_report.print(Messages.get().container(Messages.RPT_SKIPPING_0), I_CmsReport.FORMAT_NOTE);
776                    m_report.println(
777                        org.opencms.report.Messages.get().container(
778                            org.opencms.report.Messages.RPT_ARGUMENT_1,
779                            translatedName));
780
781                    if (LOG.isInfoEnabled()) {
782                        LOG.info(
783                            Messages.get().getBundle().key(
784                                Messages.LOG_SKIPPING_3,
785                                String.valueOf(i + 1),
786                                String.valueOf(importSize),
787                                translatedName));
788                    }
789                }
790            }
791        } catch (Exception e) {
792            m_report.println(e);
793            m_report.addError(e);
794
795            CmsMessageContainer message = Messages.get().container(
796                Messages.ERR_IMPORTEXPORT_ERROR_IMPORTING_RESOURCES_0);
797            if (LOG.isDebugEnabled()) {
798                LOG.debug(message.key(), e);
799            }
800
801            throw new CmsImportExportException(message, e);
802        }
803    }
804
805    /**
806     * Rewrites all parseable files, to assure link check.<p>
807     */
808    protected void rewriteParseables() {
809
810        if (m_parseables.isEmpty()) {
811            return;
812        }
813
814        m_report.println(Messages.get().container(Messages.RPT_START_PARSE_LINKS_0), I_CmsReport.FORMAT_HEADLINE);
815
816        int i = 0;
817        Iterator<CmsResource> it = m_parseables.iterator();
818        while (it.hasNext()) {
819            CmsResource res = it.next();
820
821            m_report.print(
822                org.opencms.report.Messages.get().container(
823                    org.opencms.report.Messages.RPT_SUCCESSION_2,
824                    String.valueOf(i + 1),
825                    String.valueOf(m_parseables.size())),
826                I_CmsReport.FORMAT_NOTE);
827
828            m_report.print(
829                Messages.get().container(Messages.RPT_PARSE_LINKS_FOR_1, m_cms.getSitePath(res)),
830                I_CmsReport.FORMAT_NOTE);
831            m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
832
833            try {
834                // make sure the date last modified is kept...
835                CmsFile file = m_cms.readFile(res);
836                file.setDateLastModified(res.getDateLastModified());
837                m_cms.writeFile(file);
838
839                m_report.println(
840                    org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
841                    I_CmsReport.FORMAT_OK);
842            } catch (Throwable e) {
843                m_report.addWarning(e);
844                m_report.println(
845                    org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_FAILED_0),
846                    I_CmsReport.FORMAT_ERROR);
847                if (LOG.isWarnEnabled()) {
848                    LOG.warn(Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_REWRITING_1, res.getRootPath()));
849                }
850                if (LOG.isDebugEnabled()) {
851                    LOG.debug(e.getLocalizedMessage(), e);
852                }
853            }
854            i++;
855        }
856
857        m_report.println(Messages.get().container(Messages.RPT_END_PARSE_LINKS_0), I_CmsReport.FORMAT_HEADLINE);
858    }
859}