KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/importexport/CmsImportVersion4.java,v $
3  * Date : $Date: 2006/03/27 14:52:53 $
4  * Version: $Revision: 1.87 $
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.file.CmsObject;
35 import org.opencms.file.CmsResource;
36 import org.opencms.file.types.I_CmsResourceType;
37 import org.opencms.i18n.CmsMessageContainer;
38 import org.opencms.main.CmsException;
39 import org.opencms.main.CmsLog;
40 import org.opencms.main.OpenCms;
41 import org.opencms.report.I_CmsReport;
42 import org.opencms.security.CmsRole;
43 import org.opencms.security.I_CmsPasswordHandler;
44 import org.opencms.security.I_CmsPrincipal;
45 import org.opencms.util.CmsDateUtil;
46 import org.opencms.util.CmsUUID;
47
48 import java.io.File JavaDoc;
49 import java.text.ParseException JavaDoc;
50 import java.util.ArrayList JavaDoc;
51 import java.util.Collections JavaDoc;
52 import java.util.HashMap JavaDoc;
53 import java.util.List JavaDoc;
54 import java.util.Map JavaDoc;
55 import java.util.zip.ZipFile JavaDoc;
56
57 import org.apache.commons.logging.Log;
58
59 import org.dom4j.Document;
60 import org.dom4j.Element;
61
62 /**
63  * Implementation of the OpenCms Import Interface ({@link org.opencms.importexport.I_CmsImport}) for
64  * the import version 4.<p>
65  *
66  * This import format is used in OpenCms since 5.1.6.<p>
67  *
68  * @author Michael Emmerich
69  * @author Thomas Weckert
70  *
71  * @version $Revision: 1.87 $
72  *
73  * @since 6.0.0
74  *
75  * @see org.opencms.importexport.A_CmsImport
76  */

77 public class CmsImportVersion4 extends A_CmsImport {
78
79     /** The version number of this import implementation.<p> */
80     private static final int IMPORT_VERSION = 4;
81
82     /** The log object for this class. */
83     private static final Log LOG = CmsLog.getLog(CmsImportVersion4.class);
84
85     /**
86      * Creates a new CmsImportVerion4 object.<p>
87      */

88     public CmsImportVersion4() {
89
90         m_convertToXmlPage = true;
91     }
92
93     /**
94      * @see org.opencms.importexport.I_CmsImport#getVersion()
95      * @return the version number of this import implementation
96      */

97     public int getVersion() {
98
99         return CmsImportVersion4.IMPORT_VERSION;
100     }
101
102     /**
103      * @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)
104      */

105     public void importResources(
106         CmsObject cms,
107         String JavaDoc importPath,
108         I_CmsReport report,
109         File JavaDoc importResource,
110         ZipFile JavaDoc importZip,
111         Document docXml) throws CmsImportExportException {
112
113         // initialize the import
114
initialize();
115         m_cms = cms;
116         m_importPath = importPath;
117         m_report = report;
118         m_importResource = importResource;
119         m_importZip = importZip;
120         m_docXml = docXml;
121         m_importingChannelData = false;
122         m_linkStorage = new HashMap JavaDoc();
123         m_linkPropertyStorage = new HashMap JavaDoc();
124
125         try {
126             // first import the user information
127
if (cms.hasRole(CmsRole.ACCOUNT_MANAGER)) {
128                 importGroups();
129                 importUsers();
130             }
131             // now import the VFS resources
132
readResourcesFromManifest();
133             convertPointerToSiblings();
134         } finally {
135             cleanUp();
136         }
137     }
138
139     /**
140      * Imports a single user.<p>
141      * @param name user name
142      * @param description user description
143      * @param flags user flags
144      * @param password user password
145      * @param firstname firstname of the user
146      * @param lastname lastname of the user
147      * @param email user email
148      * @param address user address
149      * @param type user type
150      * @param userInfo user info
151      * @param userGroups user groups
152      *
153      * @throws CmsImportExportException in case something goes wrong
154      */

155     protected void importUser(
156         String JavaDoc name,
157         String JavaDoc description,
158         String JavaDoc flags,
159         String JavaDoc password,
160         String JavaDoc firstname,
161         String JavaDoc lastname,
162         String JavaDoc email,
163         String JavaDoc address,
164         String JavaDoc type,
165         Map JavaDoc userInfo,
166         List JavaDoc userGroups) throws CmsImportExportException {
167
168         boolean convert = false;
169
170         Map JavaDoc config = OpenCms.getPasswordHandler().getConfiguration();
171         if (config != null && config.containsKey(I_CmsPasswordHandler.CONVERT_DIGEST_ENCODING)) {
172             convert = Boolean.valueOf((String JavaDoc)config.get(I_CmsPasswordHandler.CONVERT_DIGEST_ENCODING)).booleanValue();
173         }
174
175         if (convert) {
176             password = convertDigestEncoding(password);
177         }
178
179         super.importUser(
180             name,
181             description,
182             flags,
183             password,
184             firstname,
185             lastname,
186             email,
187             address,
188             type,
189             userInfo,
190             userGroups);
191     }
192
193     /**
194      * Convert a given timestamp from a String format to a long value.<p>
195      *
196      * The timestamp is either the string representation of a long value (old export format)
197      * or a user-readable string format.
198      *
199      * @param timestamp timestamp to convert
200      * @return long value of the timestamp
201      */

202     private long convertTimestamp(String JavaDoc timestamp) {
203
204         long value = 0;
205         // try to parse the timestamp string
206
// if it successes, its an old style long value
207
try {
208             value = Long.parseLong(timestamp);
209
210         } catch (NumberFormatException JavaDoc e) {
211             // the timestamp was in in a user-readable string format, create the long value form it
212
try {
213                 value = CmsDateUtil.parseHeaderDate(timestamp);
214             } catch (ParseException JavaDoc pe) {
215                 value = System.currentTimeMillis();
216             }
217         }
218         return value;
219     }
220
221     /**
222      * Imports a resource (file or folder) into the cms.<p>
223      *
224      * @param source the path to the source-file
225      * @param destination the path to the destination-file in the cms
226      * @param type the resource type name of the file
227      * @param uuidresource the resource uuid of the resource
228      * @param datelastmodified the last modification date of the resource
229      * @param userlastmodified the user who made the last modifications to the resource
230      * @param datecreated the creation date of the resource
231      * @param usercreated the user who created
232      * @param datereleased the release date of the resource
233      * @param dateexpired the expire date of the resource
234      * @param flags the flags of the resource
235      * @param properties a list with properties for this resource
236      *
237      * @return imported resource
238      */

239     private CmsResource importResource(
240         String JavaDoc source,
241         String JavaDoc destination,
242         I_CmsResourceType type,
243         String JavaDoc uuidresource,
244         long datelastmodified,
245         String JavaDoc userlastmodified,
246         long datecreated,
247         String JavaDoc usercreated,
248         long datereleased,
249         long dateexpired,
250         String JavaDoc flags,
251         List JavaDoc properties) {
252
253         byte[] content = null;
254         CmsResource result = null;
255
256         try {
257
258             // get the file content
259
if (source != null) {
260                 content = getFileBytes(source);
261             }
262             int size = 0;
263             if (content != null) {
264                 size = content.length;
265             }
266
267             // get UUIDs for the user
268
CmsUUID newUserlastmodified;
269             CmsUUID newUsercreated;
270             // check if user created and user lastmodified are valid users in this system.
271
// if not, use the current user
272
try {
273                 newUserlastmodified = m_cms.readUser(userlastmodified).getId();
274             } catch (CmsException e) {
275                 newUserlastmodified = m_cms.getRequestContext().currentUser().getId();
276                 // datelastmodified = System.currentTimeMillis();
277
}
278
279             try {
280                 newUsercreated = m_cms.readUser(usercreated).getId();
281             } catch (CmsException e) {
282                 newUsercreated = m_cms.getRequestContext().currentUser().getId();
283                 // datecreated = System.currentTimeMillis();
284
}
285
286             // get UUIDs for the resource and content
287
CmsUUID newUuidresource = null;
288             if ((uuidresource != null) && (!type.isFolder())) {
289                 // create a UUID from the provided string
290
newUuidresource = new CmsUUID(uuidresource);
291             } else {
292                 // folders get always a new resource record UUID
293
newUuidresource = new CmsUUID();
294             }
295
296             // create a new CmsResource
297
CmsResource resource = new CmsResource(
298                 new CmsUUID(), // structure ID is always a new UUID
299
newUuidresource,
300                 destination,
301                 type.getTypeId(),
302                 type.isFolder(),
303                 new Integer JavaDoc(flags).intValue(),
304                 m_cms.getRequestContext().currentProject().getId(),
305                 CmsResource.STATE_NEW,
306                 datecreated,
307                 newUsercreated,
308                 datelastmodified,
309                 newUserlastmodified,
310                 datereleased,
311                 dateexpired,
312                 1,
313                 size);
314
315             // import this resource in the VFS
316
result = m_cms.importResource(destination, resource, content, properties);
317
318             if (result != null) {
319                 m_report.println(
320                     org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
321                     I_CmsReport.FORMAT_OK);
322             }
323         } catch (Exception JavaDoc exc) {
324             // an error while importing the file
325
m_report.println(exc);
326             try {
327                 // Sleep some time after an error so that the report output has a chance to keep up
328
Thread.sleep(1000);
329             } catch (Exception JavaDoc e) {
330                 //
331
}
332         }
333         return result;
334     }
335
336     /**
337      * Reads all file nodes plus their meta-information (properties, ACL)
338      * from the <code>manifest.xml</code> and imports them as Cms resources to the VFS.<p>
339      *
340      * @throws CmsImportExportException if something goes wrong
341      */

342     private void readResourcesFromManifest() throws CmsImportExportException {
343
344         String JavaDoc source = null, destination = null, uuidresource = null, userlastmodified = null, usercreated = null, flags = null, timestamp = null;
345         long datelastmodified = 0, datecreated = 0, datereleased = 0, dateexpired = 0;
346
347         List JavaDoc fileNodes = null, acentryNodes = null;
348         Element currentElement = null, currentEntry = null;
349         List JavaDoc properties = null;
350
351         if (m_importingChannelData) {
352             m_cms.getRequestContext().saveSiteRoot();
353             m_cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS);
354         }
355
356         // get list of immutable resources
357
List JavaDoc immutableResources = OpenCms.getImportExportManager().getImmutableResources();
358         if (immutableResources == null) {
359             immutableResources = Collections.EMPTY_LIST;
360         }
361         if (LOG.isDebugEnabled()) {
362             LOG.debug(Messages.get().getBundle().key(
363                 Messages.LOG_IMPORTEXPORT_IMMUTABLE_RESOURCES_SIZE_1,
364                 Integer.toString(immutableResources.size())));
365         }
366         // get list of ignored properties
367
List JavaDoc ignoredProperties = OpenCms.getImportExportManager().getIgnoredProperties();
368         if (ignoredProperties == null) {
369             ignoredProperties = Collections.EMPTY_LIST;
370         }
371
372         // get the desired page type for imported pages
373
m_convertToXmlPage = OpenCms.getImportExportManager().convertToXmlPage();
374
375         try {
376             // get all file-nodes
377
fileNodes = m_docXml.selectNodes("//" + CmsImportExportManager.N_FILE);
378             int importSize = fileNodes.size();
379
380             // walk through all files in manifest
381
for (int i = 0; i < fileNodes.size(); i++) {
382                 m_report.print(org.opencms.report.Messages.get().container(
383                     org.opencms.report.Messages.RPT_SUCCESSION_2,
384                     String.valueOf(i + 1),
385                     String.valueOf(importSize)), I_CmsReport.FORMAT_NOTE);
386                 currentElement = (Element)fileNodes.get(i);
387
388                 // <source>
389
source = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_SOURCE);
390                 // <destination>
391

392                 destination = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_DESTINATION);
393
394                 // <type>
395
String JavaDoc typeName = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_TYPE);
396                 I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(typeName);
397
398                 if (!type.isFolder()) {
399                     // <uuidresource>
400
uuidresource = CmsImport.getChildElementTextValue(
401                         currentElement,
402                         CmsImportExportManager.N_UUIDRESOURCE);
403                 } else {
404                     uuidresource = null;
405                 }
406
407                 // <datelastmodified>
408
timestamp = CmsImport.getChildElementTextValue(
409                     currentElement,
410                     CmsImportExportManager.N_DATELASTMODIFIED);
411                 if (timestamp != null) {
412                     datelastmodified = convertTimestamp(timestamp);
413                 } else {
414                     datelastmodified = System.currentTimeMillis();
415                 }
416
417                 // <userlastmodified>
418
userlastmodified = CmsImport.getChildElementTextValue(
419                     currentElement,
420                     CmsImportExportManager.N_USERLASTMODIFIED);
421                 userlastmodified = OpenCms.getImportExportManager().translateUser(userlastmodified);
422
423                 // <datecreated>
424
timestamp = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_DATECREATED);
425                 if (timestamp != null) {
426                     datecreated = convertTimestamp(timestamp);
427                 } else {
428                     datecreated = System.currentTimeMillis();
429                 }
430
431                 // <usercreated>
432
usercreated = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_USERCREATED);
433                 usercreated = OpenCms.getImportExportManager().translateUser(usercreated);
434
435                 // <datereleased>
436
timestamp = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_DATERELEASED);
437                 if (timestamp != null) {
438                     datereleased = convertTimestamp(timestamp);
439                 } else {
440                     datereleased = CmsResource.DATE_RELEASED_DEFAULT;
441                 }
442
443                 // <dateexpired>
444
timestamp = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_DATEEXPIRED);
445                 if (timestamp != null) {
446                     dateexpired = convertTimestamp(timestamp);
447                 } else {
448                     dateexpired = CmsResource.DATE_EXPIRED_DEFAULT;
449                 }
450
451                 // <flags>
452
flags = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_FLAGS);
453
454                 // apply name translation and import path
455
String JavaDoc translatedName = m_cms.getRequestContext().addSiteRoot(m_importPath + destination);
456                 if (type.isFolder()) {
457                     // ensure folders end with a "/"
458
if (!CmsResource.isFolder(translatedName)) {
459                         translatedName += "/";
460                     }
461                 }
462
463                 // check if this resource is immutable
464
boolean resourceNotImmutable = checkImmutable(translatedName, immutableResources);
465                 translatedName = m_cms.getRequestContext().removeSiteRoot(translatedName);
466                 // if the resource is not immutable and not on the exclude list, import it
467
if (resourceNotImmutable) {
468                     // print out the information to the report
469
m_report.print(Messages.get().container(Messages.RPT_IMPORTING_0), I_CmsReport.FORMAT_NOTE);
470                     m_report.print(org.opencms.report.Messages.get().container(
471                         org.opencms.report.Messages.RPT_ARGUMENT_1,
472                         translatedName));
473                     m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
474                     // get all properties
475
properties = readPropertiesFromManifest(currentElement, ignoredProperties);
476
477                     // import the resource
478
CmsResource res = importResource(
479                         source,
480                         translatedName,
481                         type,
482                         uuidresource,
483                         datelastmodified,
484                         userlastmodified,
485                         datecreated,
486                         usercreated,
487                         datereleased,
488                         dateexpired,
489                         flags,
490                         properties);
491
492                     // if the resource was imported add the access control entrys if available
493
if (res != null) {
494
495                         List JavaDoc aceList = new ArrayList JavaDoc();
496
497                         // write all imported access control entries for this file
498
acentryNodes = currentElement.selectNodes("*/" + CmsImportExportManager.N_ACCESSCONTROL_ENTRY);
499
500                         // collect all access control entries
501
for (int j = 0; j < acentryNodes.size(); j++) {
502                             currentEntry = (Element)acentryNodes.get(j);
503
504                             // get the data of the access control entry
505
String JavaDoc id = CmsImport.getChildElementTextValue(
506                                 currentEntry,
507                                 CmsImportExportManager.N_ACCESSCONTROL_PRINCIPAL);
508                             String JavaDoc principalId = new CmsUUID().toString();
509                             String JavaDoc principal = id.substring(id.indexOf('.') + 1, id.length());
510
511                             try {
512                                 if (id.startsWith(I_CmsPrincipal.PRINCIPAL_GROUP)) {
513                                     principal = OpenCms.getImportExportManager().translateGroup(principal);
514                                     principalId = m_cms.readGroup(principal).getId().toString();
515                                 } else {
516                                     principal = OpenCms.getImportExportManager().translateUser(principal);
517                                     principalId = m_cms.readUser(principal).getId().toString();
518                                 }
519
520                                 String JavaDoc acflags = CmsImport.getChildElementTextValue(
521                                     currentEntry,
522                                     CmsImportExportManager.N_FLAGS);
523
524                                 String JavaDoc allowed = ((Element)currentEntry.selectNodes(
525                                     "./"
526                                         + CmsImportExportManager.N_ACCESSCONTROL_PERMISSIONSET
527                                         + "/"
528                                         + CmsImportExportManager.N_ACCESSCONTROL_ALLOWEDPERMISSIONS).get(0)).getTextTrim();
529
530                                 String JavaDoc denied = ((Element)currentEntry.selectNodes(
531                                     "./"
532                                         + CmsImportExportManager.N_ACCESSCONTROL_PERMISSIONSET
533                                         + "/"
534                                         + CmsImportExportManager.N_ACCESSCONTROL_DENIEDPERMISSIONS).get(0)).getTextTrim();
535
536                                 // add the entry to the list
537
aceList.add(getImportAccessControlEntry(res, principalId, allowed, denied, acflags));
538                             } catch (CmsException e) {
539                                 // user or group of ACE might not exist in target system, ignore ACE
540
if (LOG.isWarnEnabled()) {
541                                     LOG.warn(Messages.get().getBundle().key(
542                                         Messages.LOG_IMPORTEXPORT_ERROR_IMPORTING_ACE_1,
543                                         translatedName), e);
544                                 }
545                                 m_report.println(e);
546                             }
547                         }
548
549                         importAccessControlEntries(res, aceList);
550
551                         if (LOG.isInfoEnabled()) {
552                             LOG.info(Messages.get().getBundle().key(
553                                 Messages.LOG_IMPORTING_4,
554                                 new Object JavaDoc[] {
555                                     String.valueOf(i + 1),
556                                     String.valueOf(importSize),
557                                     translatedName,
558                                     destination}));
559                         }
560                     } else {
561                         // resource import failed, since no CmsResource was created
562
m_report.print(Messages.get().container(Messages.RPT_SKIPPING_0), I_CmsReport.FORMAT_NOTE);
563                         m_report.println(org.opencms.report.Messages.get().container(
564                             org.opencms.report.Messages.RPT_ARGUMENT_1,
565                             translatedName));
566
567                         if (LOG.isInfoEnabled()) {
568                             LOG.info(Messages.get().getBundle().key(
569                                 Messages.LOG_SKIPPING_3,
570                                 String.valueOf(i + 1),
571                                 String.valueOf(importSize),
572                                 translatedName));
573                         }
574                     }
575
576                 } else {
577                     // skip the file import, just print out the information to the report
578

579                     m_report.print(Messages.get().container(Messages.RPT_SKIPPING_0), I_CmsReport.FORMAT_NOTE);
580                     m_report.println(org.opencms.report.Messages.get().container(
581                         org.opencms.report.Messages.RPT_ARGUMENT_1,
582                         translatedName));
583
584                     if (LOG.isInfoEnabled()) {
585                         LOG.info(Messages.get().getBundle().key(
586                             Messages.LOG_SKIPPING_3,
587                             String.valueOf(i + 1),
588                             String.valueOf(importSize),
589                             translatedName));
590                     }
591                 }
592             }
593         } catch (Exception JavaDoc e) {
594
595             m_report.println(e);
596
597             CmsMessageContainer message = Messages.get().container(
598                 Messages.ERR_IMPORTEXPORT_ERROR_IMPORTING_RESOURCES_0);
599             if (LOG.isDebugEnabled()) {
600                 LOG.debug(message.key(), e);
601             }
602
603             throw new CmsImportExportException(message, e);
604         } finally {
605             if (m_importingChannelData) {
606                 m_cms.getRequestContext().restoreSiteRoot();
607             }
608         }
609     }
610 }
Popular Tags