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