KickJava   Java API By Example, From Geeks To Geeks.

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


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

83 public class CmsImportVersion3 extends A_CmsImport {
84
85     /** The version number of this import implementation.<p> */
86     private static final int IMPORT_VERSION = 3;
87
88     /** The log object for this class. */
89     private static final Log LOG = CmsLog.getLog(CmsImportVersion3.class);
90
91     /**
92      * Creates a new CmsImportVerion3 object.<p>
93      */

94     public CmsImportVersion3() {
95
96         m_convertToXmlPage = true;
97     }
98
99     /**
100      * @see org.opencms.importexport.I_CmsImport#getVersion()
101      * @return the version number of this import implementation
102      */

103     public int getVersion() {
104
105         return CmsImportVersion3.IMPORT_VERSION;
106     }
107
108     /**
109      * @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)
110      */

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

160     protected void importUser(
161         String JavaDoc name,
162         String JavaDoc description,
163         String JavaDoc flags,
164         String JavaDoc password,
165         String JavaDoc firstname,
166         String JavaDoc lastname,
167         String JavaDoc email,
168         String JavaDoc address,
169         String JavaDoc type,
170         Map JavaDoc userInfo,
171         List JavaDoc userGroups) throws CmsImportExportException {
172
173         boolean convert = false;
174
175         Map JavaDoc config = OpenCms.getPasswordHandler().getConfiguration();
176         if (config != null && config.containsKey(I_CmsPasswordHandler.CONVERT_DIGEST_ENCODING)) {
177             convert = Boolean.valueOf((String JavaDoc)config.get(I_CmsPasswordHandler.CONVERT_DIGEST_ENCODING)).booleanValue();
178         }
179
180         if (convert) {
181             password = convertDigestEncoding(password);
182         }
183
184         super.importUser(
185             name,
186             description,
187             flags,
188             password,
189             firstname,
190             lastname,
191             email,
192             address,
193             type,
194             userInfo,
195             userGroups);
196     }
197
198     /**
199      * Imports the resources and writes them to the cms.<p>
200      *
201      * @throws CmsImportExportException if something goes wrong
202      */

203     private void importAllResources() throws CmsImportExportException {
204
205         String JavaDoc source, destination, type, uuidresource, userlastmodified, usercreated, flags, timestamp;
206         long datelastmodified, datecreated;
207
208         List JavaDoc fileNodes, acentryNodes;
209         Element currentElement, currentEntry;
210         List JavaDoc properties = null;
211
212         if (m_importingChannelData) {
213             m_cms.getRequestContext().saveSiteRoot();
214             m_cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS);
215         }
216         // get list of unwanted properties
217
List JavaDoc deleteProperties = OpenCms.getImportExportManager().getIgnoredProperties();
218         if (deleteProperties == null) {
219             deleteProperties = new ArrayList JavaDoc();
220         }
221         // get list of immutable resources
222
List JavaDoc immutableResources = OpenCms.getImportExportManager().getImmutableResources();
223         if (immutableResources == null) {
224             immutableResources = Collections.EMPTY_LIST;
225         }
226         if (LOG.isDebugEnabled()) {
227             LOG.debug(Messages.get().getBundle().key(
228                 Messages.LOG_IMPORTEXPORT_IMMUTABLE_RESOURCES_SIZE_1,
229                 Integer.toString(immutableResources.size())));
230         }
231         // get the wanted page type for imported pages
232
m_convertToXmlPage = OpenCms.getImportExportManager().convertToXmlPage();
233
234         try {
235             // get all file-nodes
236
fileNodes = m_docXml.selectNodes("//" + CmsImportExportManager.N_FILE);
237
238             int importSize = fileNodes.size();
239             // walk through all files in manifest
240
for (int i = 0; i < fileNodes.size(); i++) {
241                 m_report.print(org.opencms.report.Messages.get().container(
242                     org.opencms.report.Messages.RPT_SUCCESSION_2,
243                     String.valueOf(i + 1),
244                     String.valueOf(importSize)));
245                 currentElement = (Element)fileNodes.get(i);
246                 // get all information for a file-import
247
// <source>
248
source = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_SOURCE);
249                 // <destintion>
250
destination = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_DESTINATION);
251                 // <type>
252
type = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_TYPE);
253                 // <uuidstructure>
254
//uuidstructure = CmsImport.getChildElementTextValue(
255
// currentElement,
256
// CmsImportExportManager.N_UUIDSTRUCTURE);
257
// <uuidresource>
258
uuidresource = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_UUIDRESOURCE);
259                 // <datelastmodified>
260
timestamp = CmsImport.getChildElementTextValue(
261                     currentElement,
262                     CmsImportExportManager.N_DATELASTMODIFIED);
263                 if (timestamp != null) {
264                     datelastmodified = Long.parseLong(timestamp);
265                 } else {
266                     datelastmodified = System.currentTimeMillis();
267                 }
268                 // <userlastmodified>
269
userlastmodified = CmsImport.getChildElementTextValue(
270                     currentElement,
271                     CmsImportExportManager.N_USERLASTMODIFIED);
272                 // <datecreated>
273
timestamp = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_DATECREATED);
274                 if (timestamp != null) {
275                     datecreated = Long.parseLong(timestamp);
276                 } else {
277                     datecreated = System.currentTimeMillis();
278                 }
279                 // <usercreated>
280
usercreated = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_USERCREATED);
281                 // <flags>
282
flags = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_FLAGS);
283
284                 String JavaDoc translatedName = m_cms.getRequestContext().addSiteRoot(m_importPath + destination);
285                 if (CmsResourceTypeFolder.RESOURCE_TYPE_NAME.equals(type)) {
286                     translatedName += "/";
287                 }
288                 // translate the name during import
289
translatedName = m_cms.getRequestContext().getDirectoryTranslator().translateResource(translatedName);
290                 // check if this resource is immutable
291
boolean resourceNotImmutable = checkImmutable(translatedName, immutableResources);
292                 translatedName = m_cms.getRequestContext().removeSiteRoot(translatedName);
293                 // if the resource is not immutable and not on the exclude list, import it
294
if (resourceNotImmutable) {
295                     // print out the information to the report
296
m_report.print(Messages.get().container(Messages.RPT_IMPORTING_0), I_CmsReport.FORMAT_NOTE);
297                     m_report.print(org.opencms.report.Messages.get().container(
298                         org.opencms.report.Messages.RPT_ARGUMENT_1,
299                         translatedName));
300                     //m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0));
301
// get all properties
302
properties = readPropertiesFromManifest(currentElement, deleteProperties);
303
304                     // import the resource
305
CmsResource res = importResource(
306                         source,
307                         destination,
308                         type,
309                         uuidresource,
310                         datelastmodified,
311                         userlastmodified,
312                         datecreated,
313                         usercreated,
314                         flags,
315                         properties);
316
317                     List JavaDoc aceList = new ArrayList JavaDoc();
318                     if (res != null) {
319
320                         // write all imported access control entries for this file
321
acentryNodes = currentElement.selectNodes("*/" + CmsImportExportManager.N_ACCESSCONTROL_ENTRY);
322                         // collect all access control entries
323
for (int j = 0; j < acentryNodes.size(); j++) {
324                             currentEntry = (Element)acentryNodes.get(j);
325                             // get the data of the access control entry
326
String JavaDoc id = CmsImport.getChildElementTextValue(
327                                 currentEntry,
328                                 CmsImportExportManager.N_ACCESSCONTROL_PRINCIPAL);
329                             String JavaDoc acflags = CmsImport.getChildElementTextValue(
330                                 currentEntry,
331                                 CmsImportExportManager.N_FLAGS);
332                             String JavaDoc allowed = CmsImport.getChildElementTextValue(
333                                 currentEntry,
334                                 CmsImportExportManager.N_ACCESSCONTROL_PERMISSIONSET
335                                     + "/"
336                                     + CmsImportExportManager.N_ACCESSCONTROL_ALLOWEDPERMISSIONS);
337                             String JavaDoc denied = CmsImport.getChildElementTextValue(
338                                 currentEntry,
339                                 CmsImportExportManager.N_ACCESSCONTROL_PERMISSIONSET
340                                     + "/"
341                                     + CmsImportExportManager.N_ACCESSCONTROL_DENIEDPERMISSIONS);
342
343                             // get the correct principal
344
try {
345                                 String JavaDoc principalId = new CmsUUID().toString();
346                                 String JavaDoc principal = id.substring(id.indexOf('.') + 1, id.length());
347
348                                 if (id.startsWith(I_CmsPrincipal.PRINCIPAL_GROUP)) {
349                                     principal = OpenCms.getImportExportManager().translateGroup(principal);
350                                     principalId = m_cms.readGroup(principal).getId().toString();
351                                 } else {
352                                     principal = OpenCms.getImportExportManager().translateUser(principal);
353                                     principalId = m_cms.readUser(principal).getId().toString();
354                                 }
355
356                                 // add the entry to the list
357
aceList.add(getImportAccessControlEntry(res, principalId, allowed, denied, acflags));
358                             } catch (CmsDataAccessException e) {
359                                 // user or group not found, so do not import the ace
360
}
361                         }
362                         importAccessControlEntries(res, aceList);
363
364                     } else {
365                         // resource import failed, since no CmsResource was created
366
m_report.print(Messages.get().container(Messages.RPT_SKIPPING_0), I_CmsReport.FORMAT_NOTE);
367                         m_report.println(org.opencms.report.Messages.get().container(
368                             org.opencms.report.Messages.RPT_ARGUMENT_1,
369                             translatedName));
370                     }
371                 } else {
372                     // skip the file import, just print out the information to the report
373
m_report.print(Messages.get().container(Messages.RPT_SKIPPING_0), I_CmsReport.FORMAT_NOTE);
374                     m_report.println(org.opencms.report.Messages.get().container(
375                         org.opencms.report.Messages.RPT_ARGUMENT_1,
376                         translatedName));
377                 }
378             }
379
380         } catch (Exception JavaDoc e) {
381
382             m_report.println(e);
383
384             CmsMessageContainer message = Messages.get().container(
385                 Messages.ERR_IMPORTEXPORT_ERROR_IMPORTING_RESOURCES_0);
386             if (LOG.isDebugEnabled()) {
387                 LOG.debug(message.key(), e);
388             }
389
390             throw new CmsImportExportException(message, e);
391         } finally {
392             if (m_importingChannelData) {
393                 m_cms.getRequestContext().restoreSiteRoot();
394             }
395         }
396
397     }
398
399     /**
400      * Imports a resource (file or folder) into the cms.<p>
401      *
402      * @param source the path to the source-file
403      * @param destination the path to the destination-file in the cms
404      * @param type the resource-type of the file
405      * @param uuidresource the resource uuid of the resource
406      * @param datelastmodified the last modification date of the resource
407      * @param userlastmodified the user who made the last modifications to the resource
408      * @param datecreated the creation date of the resource
409      * @param usercreated the user who created
410      * @param flags the flags of the resource
411      * @param properties a list with properties for this resource
412      *
413      * @return imported resource
414      */

415     private CmsResource importResource(
416         String JavaDoc source,
417         String JavaDoc destination,
418         String JavaDoc type,
419         String JavaDoc uuidresource,
420         long datelastmodified,
421         String JavaDoc userlastmodified,
422         long datecreated,
423         String JavaDoc usercreated,
424         String JavaDoc flags,
425         List JavaDoc properties) {
426
427         byte[] content = null;
428         CmsResource result = null;
429
430         try {
431
432             // get the file content
433
if (source != null) {
434                 content = getFileBytes(source);
435             }
436             int size = 0;
437             if (content != null) {
438                 size = content.length;
439             }
440
441             // get all required information to create a CmsResource
442
I_CmsResourceType resType;
443
444             // get UUIDs for the user
445
CmsUUID newUserlastmodified;
446             CmsUUID newUsercreated;
447             // check if user created and user lastmodified are valid users in this system.
448
// if not, use the current user
449
try {
450                 newUserlastmodified = m_cms.readUser(userlastmodified).getId();
451             } catch (CmsException e) {
452                 newUserlastmodified = m_cms.getRequestContext().currentUser().getId();
453                 // datelastmodified = System.currentTimeMillis();
454
}
455
456             try {
457                 newUsercreated = m_cms.readUser(usercreated).getId();
458             } catch (CmsException e) {
459                 newUsercreated = m_cms.getRequestContext().currentUser().getId();
460                 // datecreated = System.currentTimeMillis();
461
}
462
463             // convert to xml page if wanted
464
if (m_convertToXmlPage && (type.equals(RESOURCE_TYPE_NEWPAGE_NAME))) {
465
466                 if (content != null) {
467
468                     //get the encoding
469
String JavaDoc encoding = null;
470                     encoding = CmsProperty.get(CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, properties).getValue();
471                     if (encoding == null) {
472                         encoding = OpenCms.getSystemInfo().getDefaultEncoding();
473                     }
474
475                     CmsXmlPage xmlPage = CmsXmlPageConverter.convertToXmlPage(m_cms, content, getLocale(
476                         destination,
477                         properties), encoding);
478
479                     content = xmlPage.marshal();
480                 }
481                 resType = OpenCms.getResourceManager().getResourceType(CmsResourceTypeXmlPage.getStaticTypeId());
482             } else if (type.equals(RESOURCE_TYPE_LINK_NAME)) {
483                 resType = OpenCms.getResourceManager().getResourceType(CmsResourceTypePointer.getStaticTypeId());
484             } else if (type.equals(RESOURCE_TYPE_LEGACY_PAGE_NAME)) {
485                 resType = OpenCms.getResourceManager().getResourceType(CmsResourceTypePlain.getStaticTypeId());
486             } else {
487                 resType = OpenCms.getResourceManager().getResourceType(type);
488             }
489
490             // get UUIDs for the resource and content
491
CmsUUID newUuidresource = null;
492             if ((uuidresource != null) && (!resType.isFolder())) {
493                 // create a UUID from the provided string
494
newUuidresource = new CmsUUID(uuidresource);
495             } else {
496                 // folders get always a new resource record UUID
497
newUuidresource = new CmsUUID();
498             }
499
500             // create a new CmsResource
501
CmsResource resource = new CmsResource(
502                 new CmsUUID(), // structure ID is always a new UUID
503
newUuidresource,
504                 destination,
505                 resType.getTypeId(),
506                 resType.isFolder(),
507                 new Integer JavaDoc(flags).intValue(),
508                 m_cms.getRequestContext().currentProject().getId(),
509                 CmsResource.STATE_NEW,
510                 datecreated,
511                 newUsercreated,
512                 datelastmodified,
513                 newUserlastmodified,
514                 CmsResource.DATE_RELEASED_DEFAULT,
515                 CmsResource.DATE_EXPIRED_DEFAULT,
516                 1,
517                 size);
518
519             if (type.equals(RESOURCE_TYPE_LINK_NAME)) {
520                 // store links for later conversion
521
m_report.print(Messages.get().container(Messages.RPT_STORING_LINK_0), I_CmsReport.FORMAT_NOTE);
522                 m_linkStorage.put(m_importPath + destination, new String JavaDoc(content));
523                 m_linkPropertyStorage.put(m_importPath + destination, properties);
524                 result = resource;
525             } else {
526                 // import this resource in the VFS
527
result = m_cms.importResource(destination, resource, content, properties);
528             }
529
530             if (result != null) {
531                 m_report.println(
532                     org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
533                     I_CmsReport.FORMAT_OK);
534             }
535         } catch (Exception JavaDoc exc) {
536             // an error while importing the file
537
m_report.println(exc);
538             try {
539                 // Sleep some time after an error so that the report output has a chance to keep up
540
Thread.sleep(1000);
541             } catch (Exception JavaDoc e) {
542                 //
543
}
544         }
545
546         return result;
547     }
548 }
Popular Tags