KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > importexport > CmsImportVersion2


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/importexport/CmsImportVersion2.java,v $
3  * Date : $Date: 2006/03/27 14:52:54 $
4  * Version: $Revision: 1.113 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.importexport;
33
34 import org.opencms.db.CmsDbUtil;
35 import org.opencms.file.CmsFile;
36 import org.opencms.file.CmsFolder;
37 import org.opencms.file.CmsObject;
38 import org.opencms.file.CmsProperty;
39 import org.opencms.file.CmsPropertyDefinition;
40 import org.opencms.file.CmsResource;
41 import org.opencms.file.CmsResourceFilter;
42 import org.opencms.file.types.CmsResourceTypeFolder;
43 import org.opencms.file.types.CmsResourceTypePlain;
44 import org.opencms.file.types.CmsResourceTypeXmlPage;
45 import org.opencms.file.types.I_CmsResourceType;
46 import org.opencms.i18n.CmsMessageContainer;
47 import org.opencms.lock.CmsLockException;
48 import org.opencms.main.CmsException;
49 import org.opencms.main.CmsLog;
50 import org.opencms.main.OpenCms;
51 import org.opencms.report.I_CmsReport;
52 import org.opencms.security.CmsRole;
53 import org.opencms.security.I_CmsPasswordHandler;
54 import org.opencms.util.CmsStringUtil;
55 import org.opencms.util.CmsUUID;
56 import org.opencms.xml.CmsXmlException;
57 import org.opencms.xml.CmsXmlUtils;
58 import org.opencms.xml.page.CmsXmlPage;
59
60 import java.io.File JavaDoc;
61 import java.util.ArrayList JavaDoc;
62 import java.util.Collections JavaDoc;
63 import java.util.HashMap JavaDoc;
64 import java.util.Iterator JavaDoc;
65 import java.util.List JavaDoc;
66 import java.util.Map JavaDoc;
67 import java.util.StringTokenizer JavaDoc;
68 import java.util.zip.ZipFile JavaDoc;
69
70 import org.apache.commons.logging.Log;
71
72 import org.dom4j.Document;
73 import org.dom4j.Element;
74 import org.dom4j.Node;
75
76 /**
77  * Implementation of the OpenCms Import Interface ({@link org.opencms.importexport.I_CmsImport}) for
78  * the import version 2.<p>
79  *
80  * This import format was used in OpenCms 5.0.0 - 5.1.2.<p>
81  *
82  * @author Michael Emmerich
83  * @author Thomas Weckert
84  *
85  * @version $Revision: 1.113 $
86  *
87  * @since 6.0.0
88  *
89  * @see org.opencms.importexport.A_CmsImport
90  */

91 public class CmsImportVersion2 extends A_CmsImport {
92
93     /** The runtime property name for old webapp names. */
94     private static final String JavaDoc COMPATIBILITY_WEBAPPNAMES = "compatibility.support.webAppNames";
95
96     /** The version number of this import implementation. */
97     private static final int IMPORT_VERSION = 2;
98
99     /** The log object for this class. */
100     private static final Log LOG = CmsLog.getLog(CmsImportVersion2.class);
101
102     /** Web application names for conversion support. */
103     protected List JavaDoc m_webAppNames;
104
105     /** Old webapp URL for import conversion. */
106     protected String JavaDoc m_webappUrl;
107
108     /** folder storage for page file and body conversion. */
109     private List JavaDoc m_folderStorage;
110
111     /** page file storage for page file and body co.version. */
112     private List JavaDoc m_pageStorage;
113
114     /**
115      * Translates directory Strings from OpenCms 4.x structure to new 5.0 structure.<p>
116      *
117      * @param content the filecontent
118      * @param rules the translation rules
119      * @return String the manipulated file content
120      */

121     public static String JavaDoc setDirectories(String JavaDoc content, String JavaDoc[] rules) {
122
123         // get translation rules
124
for (int i = 0; i < rules.length; i++) {
125             String JavaDoc actRule = rules[i];
126             // cut String "/default/vfs/" from rule
127
actRule = CmsStringUtil.substitute(actRule, "/default/vfs", "");
128             // divide rule into search and replace parts and delete regular expressions
129
StringTokenizer JavaDoc ruleT = new StringTokenizer JavaDoc(actRule, "#");
130             ruleT.nextToken();
131             String JavaDoc search = ruleT.nextToken();
132             int pos = search.lastIndexOf("(.*)");
133             if (pos >= 0) {
134                 search = search.substring(0, pos);
135             }
136             String JavaDoc replace = ruleT.nextToken();
137             if (pos >= 0) {
138                 replace = replace.substring(0, replace.lastIndexOf("$1"));
139             }
140             // scan content for paths if the replace String is not present
141
if (content.indexOf(replace) == -1 && content.indexOf(search) != -1) {
142                 // ensure subdirectories of the same name are not replaced
143
search = "([}>\"'\\[]\\s*)" + search;
144                 replace = "$1" + replace;
145                 content = CmsStringUtil.substitutePerl(content, search, replace, "g");
146             }
147         }
148         return content;
149     }
150
151     /**
152      * @see org.opencms.importexport.I_CmsImport#getVersion()
153      * @return the version number of this import implementation
154      */

155     public int getVersion() {
156
157         return CmsImportVersion2.IMPORT_VERSION;
158     }
159
160     /**
161      * @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)
162      */

163     public void importResources(
164         CmsObject cms,
165         String JavaDoc importPath,
166         I_CmsReport report,
167         File importResource,
168         ZipFile JavaDoc importZip,
169         Document docXml) throws CmsImportExportException {
170
171         // initialize the import
172
initialize();
173         m_cms = cms;
174         m_importPath = importPath;
175         m_report = report;
176         m_importResource = importResource;
177         m_importZip = importZip;
178         m_docXml = docXml;
179         m_importingChannelData = false;
180
181         m_folderStorage = new ArrayList JavaDoc();
182         m_pageStorage = new ArrayList JavaDoc();
183         m_linkStorage = new HashMap JavaDoc();
184         m_linkPropertyStorage = new HashMap JavaDoc();
185
186         if (OpenCms.getRunLevel() >= OpenCms.RUNLEVEL_3_SHELL_ACCESS) {
187             if ((OpenCms.getMemoryMonitor() != null) && OpenCms.getMemoryMonitor().enabled()) {
188                 OpenCms.getMemoryMonitor().register(this.getClass().getName() + ".m_folderStorage", m_folderStorage);
189                 OpenCms.getMemoryMonitor().register(this.getClass().getName() + ".m_pageStorage", m_pageStorage);
190                 OpenCms.getMemoryMonitor().register(this.getClass().getName() + ".m_linkStorage", m_linkStorage);
191                 OpenCms.getMemoryMonitor().register(
192                     this.getClass().getName() + ".m_linkPropertyStorage",
193                     m_linkPropertyStorage);
194             }
195         }
196
197         try {
198             // first import the user information
199
if (cms.hasRole(CmsRole.ACCOUNT_MANAGER)) {
200                 importGroups();
201                 importUsers();
202             }
203             // now import the VFS resources
204
importAllResources();
205             convertPointerToSiblings();
206         } finally {
207             cleanUp();
208         }
209     }
210
211     /**
212      * Cleans up member variables after the import is finished.<p>
213      *
214      * This is required since there is only one instance for
215      * each import version that is kept in memory and reused.<p>
216      */

217     protected void cleanUp() {
218
219         m_pageStorage = null;
220         m_folderStorage = null;
221         m_webAppNames = null;
222         m_webappUrl = null;
223         super.cleanUp();
224     }
225
226     /**
227      * Performs all required pre-import steps.<p>
228      *
229      * The content is *NOT* changed in the implementation of this class.<p>
230      *
231      * @param source the source path of the resource
232      * @param destination the destination path of the resource
233      * @param content the content of the resource
234      * @param resType the type of the resource
235      * @return the (prepared) content of the resource
236      */

237     protected byte[] convertContent(String JavaDoc source, String JavaDoc destination, byte[] content, String JavaDoc resType) {
238
239         // if the import is older than version 3, some additional conversions must be made
240
if (getVersion() < 3) {
241             if ("page".equals(resType)) {
242                 if (DEBUG > 0) {
243                     System.err.println("#########################");
244                     System.err.println("["
245                         + this.getClass().getName()
246                         + ".convertContent()]: storing resource "
247                         + source
248                         + '.');
249                 }
250                 // if the imported resource is a page, store its path inside the VFS for later
251
// integration with its body
252
m_pageStorage.add(destination);
253             } else if ("folder".equals(resType)) {
254                 // check if the imported resource is a folder. Folders created in the /system/bodies/ folder
255
if (destination.startsWith(CmsCompatibleCheck.VFS_PATH_BODIES.substring(1))) {
256                     // must be remove since we do not use body files anymore.
257
m_folderStorage.add(destination);
258                 }
259             }
260         }
261
262         return content;
263     }
264
265     /**
266      * Gets the encoding from the &lt;?XML ...&gt; tag if present.<p>
267      *
268      * @param content the file content
269      * @return String the found encoding
270      */

271     protected String JavaDoc getEncoding(String JavaDoc content) {
272
273         String JavaDoc encoding = content;
274         int index = encoding.toLowerCase().indexOf("encoding=\"");
275         // encoding attribute found, get the value
276
if (index != -1) {
277             encoding = encoding.substring(index + 10);
278             index = encoding.indexOf("\"");
279             if (index != -1) {
280                 encoding = encoding.substring(0, index);
281                 return encoding.toUpperCase();
282             }
283         }
284         // no encoding attribute found
285
return "";
286     }
287
288     /**
289      * Imports a single user.<p>
290      * @param name user name
291      * @param description user description
292      * @param flags user flags
293      * @param password user password
294      * @param firstname firstname of the user
295      * @param lastname lastname of the user
296      * @param email user email
297      * @param address user address
298      * @param type user type
299      * @param userInfo user info
300      * @param userGroups user groups
301      *
302      * @throws CmsImportExportException in case something goes wrong
303      */

304     protected void importUser(
305         String JavaDoc name,
306         String JavaDoc description,
307         String JavaDoc flags,
308         String JavaDoc password,
309         String JavaDoc firstname,
310         String JavaDoc lastname,
311         String JavaDoc email,
312         String JavaDoc address,
313         String JavaDoc type,
314         Map JavaDoc userInfo,
315         List JavaDoc userGroups) throws CmsImportExportException {
316
317         boolean convert = false;
318
319         Map JavaDoc config = OpenCms.getPasswordHandler().getConfiguration();
320         if (config != null && config.containsKey(I_CmsPasswordHandler.CONVERT_DIGEST_ENCODING)) {
321             convert = Boolean.valueOf((String JavaDoc)config.get(I_CmsPasswordHandler.CONVERT_DIGEST_ENCODING)).booleanValue();
322         }
323
324         if (convert) {
325             password = convertDigestEncoding(password);
326         }
327
328         super.importUser(
329             name,
330             description,
331             flags,
332             password,
333             firstname,
334             lastname,
335             email,
336             address,
337             type,
338             userInfo,
339             userGroups);
340     }
341
342     /**
343      * Initializes all member variables before the import is started.<p>
344      *
345      * This is required since there is only one instance for
346      * each import version that is kept in memory and reused.<p>
347      */

348     protected void initialize() {
349
350         m_convertToXmlPage = true;
351         m_webAppNames = new ArrayList JavaDoc();
352         super.initialize();
353     }
354
355     /**
356      * Sets the right encoding and returns the result.<p>
357      *
358      * @param content the filecontent
359      * @param encoding the encoding to use
360      * @return modified content
361      */

362     protected String JavaDoc setEncoding(String JavaDoc content, String JavaDoc encoding) {
363
364         if (content.toLowerCase().indexOf("<?xml") == -1) {
365             return content;
366         } else {
367             // XML information present, replace encoding
368
// set the encoding only if it does not exist
369
String JavaDoc xmlTag = content.substring(0, content.indexOf(">") + 1);
370             if (xmlTag.toLowerCase().indexOf("encoding") == -1) {
371                 content = content.substring(content.indexOf(">") + 1);
372                 content = "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>" + content;
373             }
374         }
375         return content;
376     }
377
378     /**
379      * Returns the compatibility web app names.<p>
380      *
381      * @return the compatibility web app names
382      */

383     private List JavaDoc getCompatibilityWebAppNames() {
384
385         List JavaDoc webAppNamesOri = new ArrayList JavaDoc();
386
387         String JavaDoc configuredWebAppNames = (String JavaDoc)OpenCms.getRuntimeProperty(COMPATIBILITY_WEBAPPNAMES);
388         if (configuredWebAppNames != null && configuredWebAppNames.length() != 0) {
389             // split the comma separated list of web app names
390
StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(configuredWebAppNames, ",;");
391             while (tokenizer.hasMoreTokens()) {
392                 webAppNamesOri.add(tokenizer.nextToken());
393             }
394         }
395
396         List JavaDoc webAppNames = new ArrayList JavaDoc();
397         for (int i = 0; i < webAppNamesOri.size(); i++) {
398             // remove possible white space
399
String JavaDoc name = ((String JavaDoc)webAppNamesOri.get(i)).trim();
400             if (CmsStringUtil.isNotEmpty(name)) {
401                 webAppNames.add(name);
402                 if (LOG.isInfoEnabled()) {
403                     LOG.info(Messages.get().getBundle().key(
404                         Messages.INIT_IMPORTEXPORT_OLD_CONTEXT_PATH_2,
405                         Integer.toString((i + 1)),
406                         name));
407                 }
408             }
409         }
410
411         String JavaDoc key = (webAppNames.size() > 0) ? Messages.INIT_IMPORTEXPORT_OLD_CONTEXT_SUPPORT_ENABLED_0
412         : Messages.INIT_IMPORTEXPORT_OLD_CONTEXT_SUPPORT_DISABLED_0;
413         if (LOG.isInfoEnabled()) {
414             LOG.info(Messages.get().getBundle().key(key));
415         }
416
417         // check if list is null
418
if (webAppNames == null) {
419             webAppNames = new ArrayList JavaDoc();
420         }
421
422         // add current context to webapp names list
423
if (!webAppNames.contains(OpenCms.getSystemInfo().getOpenCmsContext())) {
424             webAppNames.add(OpenCms.getSystemInfo().getOpenCmsContext());
425         }
426
427         return webAppNames;
428     }
429
430     /**
431      * Imports the resources and writes them to the cms.<p>
432      *
433      * @throws CmsImportExportException if something goes wrong
434      */

435     private void importAllResources() throws CmsImportExportException {
436
437         List JavaDoc fileNodes = null, acentryNodes = null;
438         Element currentElement = null, currentEntry = null;
439         String JavaDoc source = null, destination = null, resourceTypeName = null, timestamp = null, uuid = null, uuidresource = null;
440         long lastmodified = 0;
441         int resourceTypeId = CmsDbUtil.UNKNOWN_ID;
442         List JavaDoc properties = null;
443         boolean old_overwriteCollidingResources = false;
444
445         if (m_importingChannelData) {
446             m_cms.getRequestContext().saveSiteRoot();
447             m_cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS);
448         }
449         try {
450             m_webAppNames = getCompatibilityWebAppNames();
451         } catch (Exception JavaDoc e) {
452             if (LOG.isDebugEnabled()) {
453                 LOG.debug(Messages.get().getBundle().key(
454                     Messages.LOG_IMPORTEXPORT_ERROR_GETTING_WEBAPP_COMPATIBILITY_NAMES_0), e);
455             }
456             m_report.println(e);
457         }
458         if (m_webAppNames == null) {
459             m_webAppNames = Collections.EMPTY_LIST;
460         }
461
462         // get the old webapp url from the OpenCms properties
463
m_webappUrl = OpenCms.getImportExportManager().getOldWebAppUrl();
464         if (m_webappUrl == null) {
465             // use a default value
466
m_webappUrl = "http://localhost:8080/opencms/opencms";
467         }
468         // cut last "/" from webappUrl if present
469
if (m_webappUrl.endsWith("/")) {
470             m_webappUrl = m_webappUrl.substring(0, m_webappUrl.lastIndexOf("/"));
471         }
472
473         // get list of unwanted properties
474
List JavaDoc deleteProperties = OpenCms.getImportExportManager().getIgnoredProperties();
475
476         // get list of immutable resources
477
List JavaDoc immutableResources = OpenCms.getImportExportManager().getImmutableResources();
478         if (immutableResources == null) {
479             immutableResources = Collections.EMPTY_LIST;
480         }
481         if (LOG.isDebugEnabled()) {
482             LOG.debug(Messages.get().getBundle().key(
483                 Messages.LOG_IMPORTEXPORT_IMMUTABLE_RESOURCES_SIZE_1,
484                 Integer.toString(immutableResources.size())));
485         }
486
487         // save the value of the boolean flag whether colliding resources should be overwritten
488
old_overwriteCollidingResources = OpenCms.getImportExportManager().overwriteCollidingResources();
489
490         // force v1 and v2 imports to overwrite colliding resources, because they dont have resource
491
// UUIDs in their manifest anyway
492
OpenCms.getImportExportManager().setOverwriteCollidingResources(true);
493
494         try {
495             // get all file-nodes
496
fileNodes = m_docXml.selectNodes("//" + CmsImportExportManager.N_FILE);
497             int importSize = fileNodes.size();
498
499             // walk through all files in manifest
500
for (int i = 0; i < importSize; i++) {
501
502                 m_report.print(org.opencms.report.Messages.get().container(
503                     org.opencms.report.Messages.RPT_SUCCESSION_2,
504                     String.valueOf(i + 1),
505                     String.valueOf(importSize)), I_CmsReport.FORMAT_NOTE);
506                 currentElement = (Element)fileNodes.get(i);
507
508                 // get all information for a file-import
509
source = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_SOURCE);
510                 destination = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_DESTINATION);
511
512                 resourceTypeName = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_TYPE);
513                 if (RESOURCE_TYPE_NEWPAGE_NAME.equals(resourceTypeName)) {
514                     resourceTypeId = RESOURCE_TYPE_NEWPAGE_ID;
515                 } else if (RESOURCE_TYPE_LEGACY_PAGE_NAME.equals(resourceTypeName)) {
516                     // resource with a "legacy" resource type are imported using the "plain" resource
517
// type because you cannot import a resource without having the resource type object
518
resourceTypeId = CmsResourceTypePlain.getStaticTypeId();
519                 } else if (RESOURCE_TYPE_LINK_NAME.equals(resourceTypeName)) {
520                     // set resource type of legacy "link" which is converted later
521
resourceTypeId = RESOURCE_TYPE_LINK_ID;
522                 } else {
523                     I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(resourceTypeName);
524                     resourceTypeId = type.getTypeId();
525                 }
526
527                 uuid = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_UUIDSTRUCTURE);
528                 uuidresource = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_UUIDRESOURCE);
529
530                 timestamp = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_LASTMODIFIED);
531                 if (timestamp != null) {
532                     lastmodified = Long.parseLong(timestamp);
533                 } else {
534                     lastmodified = System.currentTimeMillis();
535                 }
536
537                 // if the type is "script" set it to plain
538
if ("script".equals(resourceTypeName)) {
539                     resourceTypeName = CmsResourceTypePlain.getStaticTypeName();
540                 }
541
542                 if (LOG.isDebugEnabled()) {
543                     LOG.debug(Messages.get().getBundle().key(
544                         Messages.LOG_IMPORTEXPORT_ORIGINAL_RESOURCE_NAME_1,
545                         destination));
546                 }
547
548                 String JavaDoc translatedName = m_cms.getRequestContext().addSiteRoot(m_importPath + destination);
549                 if (CmsResourceTypeFolder.RESOURCE_TYPE_NAME.equals(resourceTypeName)) {
550                     // ensure folders end with a "/"
551
if (!CmsResource.isFolder(translatedName)) {
552                         translatedName += "/";
553                     }
554                 }
555
556                 if (LOG.isDebugEnabled()) {
557                     LOG.debug(Messages.get().getBundle().key(
558                         Messages.LOG_IMPORTEXPORT_TRANSLATED_RESOURCE_NAME_1,
559                         translatedName));
560                 }
561
562                 boolean resourceNotImmutable = checkImmutable(translatedName, immutableResources);
563                 translatedName = m_cms.getRequestContext().removeSiteRoot(translatedName);
564                 if (resourceNotImmutable) {
565
566                     // print out the information to the report
567
m_report.print(Messages.get().container(Messages.RPT_IMPORTING_0), I_CmsReport.FORMAT_NOTE);
568                     m_report.print(org.opencms.report.Messages.get().container(
569                         org.opencms.report.Messages.RPT_ARGUMENT_1,
570                         translatedName));
571                     m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
572
573                     // get all properties
574
properties = readPropertiesFromManifest(currentElement, deleteProperties);
575
576                     // import the specified file
577
CmsResource res = importResource(
578                         source,
579                         destination,
580                         uuid,
581                         uuidresource,
582                         resourceTypeId,
583                         resourceTypeName,
584                         lastmodified,
585                         properties);
586
587                     if (res != null) {
588
589                         List JavaDoc aceList = new ArrayList JavaDoc();
590                         // write all imported access control entries for this file
591
acentryNodes = currentElement.selectNodes("*/" + CmsImportExportManager.N_ACCESSCONTROL_ENTRY);
592                         // collect all access control entries
593
for (int j = 0; j < acentryNodes.size(); j++) {
594                             currentEntry = (Element)acentryNodes.get(j);
595                             // get the data of the access control entry
596
String JavaDoc id = CmsImport.getChildElementTextValue(currentEntry, CmsImportExportManager.N_ID);
597                             String JavaDoc acflags = CmsImport.getChildElementTextValue(
598                                 currentEntry,
599                                 CmsImportExportManager.N_FLAGS);
600                             String JavaDoc allowed = CmsImport.getChildElementTextValue(
601                                 currentEntry,
602                                 CmsImportExportManager.N_ACCESSCONTROL_ALLOWEDPERMISSIONS);
603                             String JavaDoc denied = CmsImport.getChildElementTextValue(
604                                 currentEntry,
605                                 CmsImportExportManager.N_ACCESSCONTROL_DENIEDPERMISSIONS);
606
607                             // add the entry to the list
608
aceList.add(getImportAccessControlEntry(res, id, allowed, denied, acflags));
609                         }
610                         importAccessControlEntries(res, aceList);
611                         if (LOG.isInfoEnabled()) {
612                             LOG.info(Messages.get().getBundle().key(
613                                 Messages.LOG_IMPORTING_4,
614                                 new Object JavaDoc[] {
615                                     String.valueOf(i + 1),
616                                     String.valueOf(importSize),
617                                     translatedName,
618                                     destination}));
619                         }
620
621                     } else {
622                         // resource import failed, since no CmsResource was created
623
m_report.print(Messages.get().container(Messages.RPT_SKIPPING_0), I_CmsReport.FORMAT_OK);
624                         m_report.println(org.opencms.report.Messages.get().container(
625                             org.opencms.report.Messages.RPT_ARGUMENT_1,
626                             translatedName));
627                         if (LOG.isInfoEnabled()) {
628                             LOG.info(Messages.get().getBundle().key(
629                                 Messages.LOG_SKIPPING_3,
630                                 String.valueOf(i + 1),
631                                 String.valueOf(importSize),
632                                 translatedName));
633                         }
634                     }
635                 } else {
636                     // skip the file import, just print out the information to the report
637
m_report.print(Messages.get().container(Messages.RPT_SKIPPING_0), I_CmsReport.FORMAT_NOTE);
638                     m_report.println(org.opencms.report.Messages.get().container(
639                         org.opencms.report.Messages.RPT_ARGUMENT_1,
640                         translatedName));
641
642                     if (LOG.isInfoEnabled()) {
643                         LOG.info(Messages.get().getBundle().key(
644                             Messages.LOG_SKIPPING_3,
645                             String.valueOf(i + 1),
646                             String.valueOf(importSize),
647                             translatedName));
648                     }
649                 }
650             }
651
652             // now merge the body and page control files. this only has to be done if the import
653
// version is below version 3
654
if (getVersion() < 3 && m_convertToXmlPage) {
655                 mergePageFiles();
656                 removeFolders();
657             }
658         } catch (Exception JavaDoc e) {
659
660             m_report.println(e);
661
662             CmsMessageContainer message = Messages.get().container(
663                 Messages.ERR_IMPORTEXPORT_ERROR_IMPORTING_RESOURCES_0);
664             if (LOG.isDebugEnabled()) {
665                 LOG.debug(message.key(), e);
666             }
667
668             throw new CmsImportExportException(message, e);
669         } finally {
670             if (m_importingChannelData) {
671                 m_cms.getRequestContext().restoreSiteRoot();
672             }
673
674             // set the flag to overwrite colliding resources back to its original value
675
OpenCms.getImportExportManager().setOverwriteCollidingResources(old_overwriteCollidingResources);
676         }
677     }
678
679     /**
680      * Imports a resource (file or folder) into the cms.<p>
681      *
682      * @param source the path to the source-file
683      * @param destination the path to the destination-file in the cms
684      * @param uuid the structure uuid of the resource
685      * @param uuidresource the resource uuid of the resource
686      * @param resourceTypeId the ID of the file's resource type
687      * @param resourceTypeName the name of the file's resource type
688      * @param lastmodified the timestamp of the file
689      * @param properties a list with properties for this resource
690      *
691      * @return imported resource
692      */

693     private CmsResource importResource(
694         String JavaDoc source,
695         String JavaDoc destination,
696         String JavaDoc uuid,
697         String JavaDoc uuidresource,
698         int resourceTypeId,
699         String JavaDoc resourceTypeName,
700         long lastmodified,
701         List JavaDoc properties) {
702
703         byte[] content = null;
704         CmsResource res = null;
705         String JavaDoc targetName = null;
706
707         try {
708
709             if (m_importingChannelData) {
710                 // try to read an existing channel to get the channel id
711
String JavaDoc channelId = null;
712                 try {
713                     if ((resourceTypeName.equalsIgnoreCase(CmsResourceTypeFolder.RESOURCE_TYPE_NAME))
714                         && (!destination.endsWith("/"))) {
715                         destination += "/";
716                     }
717                     CmsResource channel = m_cms.readResource("/" + destination);
718
719                     channelId = m_cms.readPropertyObject(
720                         m_cms.getSitePath(channel),
721                         CmsPropertyDefinition.PROPERTY_CHANNELID,
722                         false).getValue();
723
724                 } catch (Exception JavaDoc e) {
725                     // ignore the exception, a new channel id will be generated
726
}
727
728                 if (channelId != null) {
729                     properties.add(new CmsProperty(CmsPropertyDefinition.PROPERTY_CHANNELID, channelId, null));
730                 }
731             }
732
733             // get the file content
734
if (source != null) {
735                 content = getFileBytes(source);
736             }
737
738             content = convertContent(source, destination, content, resourceTypeName);
739
740             // get all required information to create a CmsResource
741
int size = 0;
742             if (content != null) {
743                 size = content.length;
744             }
745             // get the required UUIDs
746
CmsUUID curUser = m_cms.getRequestContext().currentUser().getId();
747             CmsUUID newUuidstructure = new CmsUUID();
748             CmsUUID newUuidresource = new CmsUUID();
749             if (uuid != null) {
750                 newUuidstructure = new CmsUUID(uuid);
751             }
752             if (uuidresource != null) {
753                 newUuidresource = new CmsUUID(uuidresource);
754             }
755
756             // extract the name of the resource form the destination
757
targetName = destination;
758             if (targetName.endsWith("/")) {
759                 targetName = targetName.substring(0, targetName.length() - 1);
760             }
761
762             boolean isFolder = false;
763             try {
764                 isFolder = CmsFolder.isFolderType(resourceTypeId);
765             } catch (Throwable JavaDoc t) {
766                 // the specified resource type ID might be of an unknown resource type.
767
// as another option, check the content length and resource type name
768
// to determine if the resource is a folder or not.
769
isFolder = ((content.length == 0) && CmsResourceTypeFolder.RESOURCE_TYPE_NAME.equalsIgnoreCase(resourceTypeName));
770             }
771
772             // create a new CmsResource
773
CmsResource resource = new CmsResource(
774                 newUuidstructure,
775                 newUuidresource,
776                 targetName,
777                 resourceTypeId,
778                 isFolder,
779                 0,
780                 m_cms.getRequestContext().currentProject().getId(),
781                 CmsResource.STATE_NEW,
782                 lastmodified,
783                 curUser,
784                 lastmodified,
785                 curUser,
786                 CmsResource.DATE_RELEASED_DEFAULT,
787                 CmsResource.DATE_EXPIRED_DEFAULT,
788                 1,
789                 size);
790
791             if (RESOURCE_TYPE_LINK_ID == resourceTypeId) {
792                 // store links for later conversion
793
m_report.print(Messages.get().container(Messages.RPT_STORING_LINK_0), I_CmsReport.FORMAT_NOTE);
794                 m_linkStorage.put(m_importPath + destination, new String JavaDoc(content));
795                 m_linkPropertyStorage.put(m_importPath + destination, properties);
796                 res = resource;
797             } else {
798                 // import this resource in the VFS
799
String JavaDoc resName = m_importPath + destination;
800                 res = m_cms.importResource(resName, resource, content, properties);
801                 try {
802                     m_cms.unlockResource(resName);
803                 } catch (CmsLockException e) {
804                     if (LOG.isDebugEnabled()) {
805                         LOG.debug(Messages.get().getBundle().key(
806                             Messages.LOG_IMPORTEXPORT_UNABLE_TO_UNLOCK_RESOURCE_1,
807                             resName), e);
808                     }
809                 }
810             }
811
812             m_report.println(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
813
814         } catch (CmsException exc) {
815             CmsMessageContainer message = Messages.get().container(
816                 Messages.ERR_IMPORTEXPORT_ERROR_IMPORTING_RESOURCE_1,
817                 targetName);
818             if (LOG.isDebugEnabled()) {
819                 LOG.debug(message.key(), exc);
820             }
821
822             // an error while importing the file
823
m_report.println(exc);
824             try {
825                 // Sleep some time after an error so that the report output has a chance to keep up
826
Thread.sleep(1000);
827             } catch (Exception JavaDoc e) {
828                 //
829
}
830         }
831         return res;
832     }
833
834     /**
835      * Merges a single page.<p>
836      *
837      * @param resourcename the resource name of the page
838      * @throws CmsImportExportException if something goes wrong
839      * @throws CmsXmlException if the page file could not be unmarshalled
840      */

841     private void mergePageFile(String JavaDoc resourcename) throws CmsXmlException, CmsImportExportException {
842
843         try {
844
845             if (LOG.isDebugEnabled()) {
846                 LOG.debug(Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_START_MERGING_1, resourcename));
847             }
848
849             // in OpenCms versions <5 node names have not been case sensitive. thus, nodes are read both in upper
850
// and lower case letters, or have to be tested for equality ignoring upper/lower case...
851

852             // get the header file
853
CmsFile pagefile = m_cms.readFile(resourcename, CmsResourceFilter.ALL);
854             Document contentXml = CmsXmlUtils.unmarshalHelper(pagefile.getContents(), null);
855
856             // get the <masterTemplate> node to check the content. this node contains the name of the template file.
857
String JavaDoc masterTemplateNodeName = "//masterTemplate";
858             Node masterTemplateNode = contentXml.selectSingleNode(masterTemplateNodeName);
859             if (masterTemplateNode == null) {
860                 masterTemplateNode = contentXml.selectSingleNode(masterTemplateNodeName.toLowerCase());
861             }
862             if (masterTemplateNode == null) {
863                 masterTemplateNode = contentXml.selectSingleNode(masterTemplateNodeName.toUpperCase());
864             }
865
866             // there is only one <masterTemplate> allowed
867
String JavaDoc mastertemplate = null;
868             if (masterTemplateNode != null) {
869                 // get the name of the mastertemplate
870
mastertemplate = masterTemplateNode.getText().trim();
871             }
872
873             // get the <ELEMENTDEF> nodes to check the content.
874
// this node contains the information for the body element.
875
String JavaDoc elementDefNodeName = "//ELEMENTDEF";
876             Node bodyNode = contentXml.selectSingleNode(elementDefNodeName);
877             if (bodyNode == null) {
878                 bodyNode = contentXml.selectSingleNode(elementDefNodeName.toLowerCase());
879             }
880
881             // there is only one <ELEMENTDEF> allowed
882
if (bodyNode != null) {
883
884                 String JavaDoc bodyclass = null;
885                 String JavaDoc bodyname = null;
886                 Map JavaDoc bodyparams = null;
887
888                 List JavaDoc nodes = ((Element)bodyNode).elements();
889                 for (int i = 0, n = nodes.size(); i < n; i++) {
890
891                     Node node = (Node)nodes.get(i);
892
893                     if ("CLASS".equalsIgnoreCase(node.getName())) {
894                         bodyclass = node.getText().trim();
895                     } else if ("TEMPLATE".equalsIgnoreCase(node.getName())) {
896                         bodyname = node.getText().trim();
897                         if (!bodyname.startsWith("/")) {
898                             bodyname = CmsResource.getFolderPath(resourcename) + bodyname;
899                         }
900                     } else if ("PARAMETER".equalsIgnoreCase(node.getName())) {
901                         Element paramElement = (Element)node;
902                         if (bodyparams == null) {
903                             bodyparams = new HashMap JavaDoc();
904                         }
905                         bodyparams.put((paramElement.attribute("name")).getText(), paramElement.getTextTrim());
906                     }
907                 }
908
909                 if (mastertemplate == null || bodyname == null) {
910
911                     CmsMessageContainer message = Messages.get().container(
912                         Messages.ERR_IMPORTEXPORT_ERROR_CANNOT_MERGE_PAGE_FILE_3,
913                         resourcename,
914                         mastertemplate,
915                         bodyname);
916                     if (LOG.isDebugEnabled()) {
917                         LOG.debug(message.key());
918                     }
919
920                     throw new CmsImportExportException(message);
921                 }
922
923                 // lock the resource, so that it can be manipulated
924
m_cms.lockResource(resourcename);
925
926                 // get all properties
927
List JavaDoc properties = m_cms.readPropertyObjects(resourcename, false);
928
929                 // now get the content of the bodyfile and insert it into the control file
930
CmsFile bodyfile = m_cms.readFile(bodyname, CmsResourceFilter.IGNORE_EXPIRATION);
931
932                 //get the encoding
933
String JavaDoc encoding = CmsProperty.get(CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, properties).getValue();
934                 if (encoding == null) {
935                     encoding = OpenCms.getSystemInfo().getDefaultEncoding();
936                 }
937
938                 if (m_convertToXmlPage) {
939                     if (LOG.isDebugEnabled()) {
940                         LOG.debug(Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_START_CONVERTING_TO_XML_0));
941                     }
942
943                     CmsXmlPage xmlPage = CmsXmlPageConverter.convertToXmlPage(m_cms, bodyfile.getContents(), getLocale(
944                         resourcename,
945                         properties), encoding);
946
947                     if (LOG.isDebugEnabled()) {
948                         LOG.debug(Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_END_CONVERTING_TO_XML_0));
949                     }
950
951                     if (xmlPage != null) {
952                         pagefile.setContents(xmlPage.marshal());
953
954                         // set the type to xml page
955
pagefile.setType(CmsResourceTypeXmlPage.getStaticTypeId());
956                     }
957                 }
958
959                 // add the template and other required properties
960
CmsProperty newProperty = new CmsProperty(CmsPropertyDefinition.PROPERTY_TEMPLATE, mastertemplate, null);
961                 // property lists must not contain equal properties
962
properties.remove(newProperty);
963                 properties.add(newProperty);
964
965                 // if set, add the bodyclass as property
966
if (CmsStringUtil.isNotEmpty(bodyclass)) {
967                     newProperty = new CmsProperty(CmsPropertyDefinition.PROPERTY_TEMPLATE, mastertemplate, null);
968                     newProperty.setAutoCreatePropertyDefinition(true);
969                     properties.remove(newProperty);
970                     properties.add(newProperty);
971                 }
972                 // if set, add bodyparams as properties
973
if (bodyparams != null) {
974                     for (Iterator JavaDoc p = bodyparams.keySet().iterator(); p.hasNext();) {
975                         String JavaDoc key = (String JavaDoc)p.next();
976                         newProperty = new CmsProperty(key, (String JavaDoc)bodyparams.get(key), null);
977                         newProperty.setAutoCreatePropertyDefinition(true);
978                         properties.remove(newProperty);
979                         properties.add(newProperty);
980                     }
981                 }
982
983                 if (LOG.isDebugEnabled()) {
984                     LOG.debug(Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_START_IMPORTING_XML_PAGE_0));
985                 }
986
987                 // now import the resource
988
m_cms.importResource(resourcename, pagefile, pagefile.getContents(), properties);
989
990                 // finally delete the old body file, it is not needed anymore
991
m_cms.lockResource(bodyname);
992                 m_cms.deleteResource(bodyname, CmsResource.DELETE_PRESERVE_SIBLINGS);
993
994                 if (LOG.isDebugEnabled()) {
995                     LOG.debug(Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_END_IMPORTING_XML_PAGE_0));
996                 }
997
998                 m_report.println(
999                     org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
1000                    I_CmsReport.FORMAT_OK);
1001
1002            } else {
1003
1004                // there are more than one template nodes in this control file
1005
// convert the resource into a plain text file
1006
// lock the resource, so that it can be manipulated
1007
m_cms.lockResource(resourcename);
1008                // set the type to plain
1009
pagefile.setType(CmsResourceTypePlain.getStaticTypeId());
1010                // write all changes
1011
m_cms.writeFile(pagefile);
1012                // done, unlock the resource
1013
m_cms.unlockResource(resourcename);
1014
1015                if (LOG.isDebugEnabled()) {
1016                    LOG.debug(Messages.get().getBundle().key(
1017                        Messages.LOG_IMPORTEXPORT_CANNOT_CONVERT_XML_STRUCTURE_1,
1018                        resourcename));
1019                }
1020
1021                m_report.println(Messages.get().container(Messages.RPT_NOT_CONVERTED_0), I_CmsReport.FORMAT_OK);
1022
1023            }
1024
1025            if (LOG.isDebugEnabled()) {
1026                LOG.debug(Messages.get().getBundle().key(Messages.LOG_IMPORTEXPORT_END_MERGING_1, resourcename));
1027            }
1028        } catch (CmsXmlException e) {
1029
1030            throw e;
1031        } catch (CmsException e) {
1032
1033            m_report.println(e);
1034
1035            CmsMessageContainer message = Messages.get().container(
1036                Messages.ERR_IMPORTEXPORT_ERROR_MERGING_PAGE_FILE_1,
1037                resourcename);
1038            if (LOG.isDebugEnabled()) {
1039                LOG.debug(message.key(), e);
1040            }
1041
1042            throw new CmsImportExportException(message, e);
1043        }
1044
1045    }
1046
1047    /**
1048     * Merges the page control files and their corresponding bodies into a single files.<p>
1049     *
1050     * @throws CmsImportExportException if something goes wrong
1051     * @throws CmsXmlException if the page file could not be unmarshalled
1052     */

1053    private void mergePageFiles() throws CmsXmlException, CmsImportExportException {
1054
1055        try {
1056            // check if the template property exists. If not, create it.
1057
try {
1058                m_cms.readPropertyDefinition(CmsPropertyDefinition.PROPERTY_TEMPLATE);
1059            } catch (CmsException e) {
1060                // the template propertydefintion does not exist. So create it.
1061
m_cms.createPropertyDefinition(CmsPropertyDefinition.PROPERTY_TEMPLATE);
1062            }
1063
1064            // copy all propertydefinitions of the old page to the new page
1065
List JavaDoc definitions = m_cms.readAllPropertyDefinitions();
1066
1067            Iterator JavaDoc j = definitions.iterator();
1068            while (j.hasNext()) {
1069                CmsPropertyDefinition definition = (CmsPropertyDefinition)j.next();
1070                // check if this propertydef already exits
1071
try {
1072                    m_cms.readPropertyDefinition(definition.getName());
1073                } catch (Exception JavaDoc e) {
1074                    m_cms.createPropertyDefinition(definition.getName());
1075                }
1076            }
1077        } catch (CmsException e) {
1078
1079            CmsMessageContainer message = Messages.get().container(
1080                Messages.ERR_IMPORTEXPORT_ERROR_COPYING_PROPERTY_DEFINITIONS_0);
1081            if (LOG.isDebugEnabled()) {
1082                LOG.debug(message.key(), e);
1083            }
1084
1085            throw new CmsImportExportException(message);
1086        }
1087
1088        // iterate through the list of all page controlfiles found during the import process
1089
int size = m_pageStorage.size();
1090        m_report.println(Messages.get().container(Messages.RPT_MERGE_START_0), I_CmsReport.FORMAT_HEADLINE);
1091        Iterator JavaDoc i = m_pageStorage.iterator();
1092        int counter = 1;
1093        while (i.hasNext()) {
1094            String JavaDoc resname = (String JavaDoc)i.next();
1095            // adjust the resourcename if nescessary
1096
if (!resname.startsWith("/")) {
1097                resname = "/" + resname;
1098            }
1099
1100            m_report.print(org.opencms.report.Messages.get().container(
1101                org.opencms.report.Messages.RPT_SUCCESSION_2,
1102                String.valueOf(counter),
1103                String.valueOf(size)), I_CmsReport.FORMAT_NOTE);
1104            m_report.print(Messages.get().container(Messages.RPT_MERGE_0), I_CmsReport.FORMAT_NOTE);
1105            m_report.print(org.opencms.report.Messages.get().container(
1106                org.opencms.report.Messages.RPT_ARGUMENT_1,
1107                resname));
1108
1109            mergePageFile(resname);
1110            if (LOG.isInfoEnabled()) {
1111                LOG.info(Messages.get().getBundle().key(
1112                    Messages.LOG_MERGING_3,
1113                    String.valueOf(counter),
1114                    String.valueOf(size),
1115                    resname));
1116            }
1117
1118            counter++;
1119
1120        }
1121        // free mem
1122
m_pageStorage.clear();
1123
1124    }
1125
1126    /**
1127     * Deletes the folder structure which has been creating while importing the body files..<p>
1128     *
1129     * @throws CmsImportExportException if something goes wrong
1130     */

1131    private void removeFolders() throws CmsImportExportException {
1132
1133        try {
1134
1135            int size = m_folderStorage.size();
1136
1137            m_report.println(Messages.get().container(Messages.RPT_DELFOLDER_START_0), I_CmsReport.FORMAT_HEADLINE);
1138            // iterate though all collected folders. Iteration must start at the end of the list,
1139
// as folders habe to be deleted in the reverse order.
1140
int counter = 1;
1141            for (int j = (size - 1); j >= 0; j--) {
1142                String JavaDoc resname = (String JavaDoc)m_folderStorage.get(j);
1143                resname = (resname.startsWith("/") ? "" : "/") + resname + (resname.endsWith("/") ? "" : "/");
1144                // now check if the folder is really empty. Only delete empty folders
1145
List JavaDoc files = m_cms.getFilesInFolder(resname, CmsResourceFilter.IGNORE_EXPIRATION);
1146
1147                if (files.size() == 0) {
1148                    List JavaDoc folders = m_cms.getSubFolders(resname, CmsResourceFilter.IGNORE_EXPIRATION);
1149                    if (folders.size() == 0) {
1150                        m_report.print(org.opencms.report.Messages.get().container(
1151                            org.opencms.report.Messages.RPT_SUCCESSION_2,
1152                            String.valueOf(counter),
1153                            String.valueOf(size)), I_CmsReport.FORMAT_NOTE);
1154                        m_report.print(Messages.get().container(Messages.RPT_DELFOLDER_0), I_CmsReport.FORMAT_NOTE);
1155                        m_report.print(org.opencms.report.Messages.get().container(
1156                            org.opencms.report.Messages.RPT_ARGUMENT_1,
1157                            resname), I_CmsReport.FORMAT_DEFAULT);
1158                        m_cms.lockResource(resname);
1159                        m_cms.deleteResource(resname, CmsResource.DELETE_PRESERVE_SIBLINGS);
1160                        m_report.println(org.opencms.report.Messages.get().container(
1161                            org.opencms.report.Messages.RPT_OK_0), I_CmsReport.FORMAT_OK);
1162                        counter++;
1163                    }
1164                }
1165            }
1166        } catch (CmsException e) {
1167
1168            CmsMessageContainer message = Messages.get().container(
1169                Messages.ERR_IMPORTEXPORT_ERROR_REMOVING_FOLDERS_OF_IMPORTED_BODY_FILES_0);
1170            if (LOG.isDebugEnabled()) {
1171                LOG.debug(message.key(), e);
1172            }
1173
1174            throw new CmsImportExportException(message, e);
1175        }
1176    }
1177}
Popular Tags