KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/importexport/A_CmsImport.java,v $
3  * Date : $Date: 2006/03/27 14:52:54 $
4  * Version: $Revision: 1.84 $
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.CmsUserSettings;
35 import org.opencms.file.CmsGroup;
36 import org.opencms.file.CmsObject;
37 import org.opencms.file.CmsProperty;
38 import org.opencms.file.CmsPropertyDefinition;
39 import org.opencms.file.CmsResource;
40 import org.opencms.file.types.CmsResourceTypePointer;
41 import org.opencms.i18n.CmsMessageContainer;
42 import org.opencms.i18n.I_CmsMessageBundle;
43 import org.opencms.main.CmsException;
44 import org.opencms.main.CmsLog;
45 import org.opencms.main.OpenCms;
46 import org.opencms.report.I_CmsReport;
47 import org.opencms.security.CmsAccessControlEntry;
48 import org.opencms.util.CmsFileUtil;
49 import org.opencms.util.CmsStringUtil;
50 import org.opencms.util.CmsUUID;
51
52 import java.io.ByteArrayInputStream JavaDoc;
53 import java.io.File JavaDoc;
54 import java.io.FileNotFoundException JavaDoc;
55 import java.io.IOException JavaDoc;
56 import java.io.InputStream JavaDoc;
57 import java.io.ObjectInputStream JavaDoc;
58 import java.util.ArrayList JavaDoc;
59 import java.util.HashMap JavaDoc;
60 import java.util.Iterator JavaDoc;
61 import java.util.List JavaDoc;
62 import java.util.Locale JavaDoc;
63 import java.util.Map JavaDoc;
64 import java.util.Stack JavaDoc;
65 import java.util.zip.ZipEntry JavaDoc;
66 import java.util.zip.ZipException JavaDoc;
67 import java.util.zip.ZipFile JavaDoc;
68
69 import org.apache.commons.codec.binary.Base64;
70 import org.apache.commons.logging.Log;
71
72 import org.dom4j.Attribute;
73 import org.dom4j.Document;
74 import org.dom4j.Element;
75
76 /**
77  * Collection of common used methods for implementing OpenCms Import classes.<p>
78  *
79  * This class does not implement a real OpenCms import, real import implmentation should be
80  * inherited form this class.<p>
81  *
82  * @author Michael Emmerich
83  * @author Thomas Weckert
84  *
85  * @version $Revision: 1.84 $
86  *
87  * @since 6.0.0
88  *
89  * @see org.opencms.importexport.I_CmsImport
90  */

91
92 public abstract class A_CmsImport implements I_CmsImport {
93
94     /** The name of the legacy resource type "page". */
95     public static final String JavaDoc RESOURCE_TYPE_LEGACY_PAGE_NAME = "page";
96
97     /** Debug flag to show debug output. */
98     protected static final int DEBUG = 0;
99
100     /** The id of the legacy resource type "link". */
101     protected static final int RESOURCE_TYPE_LINK_ID = 1024;
102
103     /** The name of the legacy resource type "link". */
104     protected static final String JavaDoc RESOURCE_TYPE_LINK_NAME = "link";
105
106     /** The id of the legacy resource type "newpage". */
107     protected static final int RESOURCE_TYPE_NEWPAGE_ID = 9;
108
109     /** The name of the legacy resource type "newpage". */
110     protected static final String JavaDoc RESOURCE_TYPE_NEWPAGE_NAME = "newpage";
111
112     /** The log object for this class. */
113     private static final Log LOG = CmsLog.getLog(A_CmsImport.class);
114
115     /** The cms context to do the import operations with. */
116     protected CmsObject m_cms;
117
118     /** Flag for conversion to xml pages. */
119     protected boolean m_convertToXmlPage;
120
121     /** The xml manifest-file. */
122     protected Document m_docXml;
123
124     /** Groups to create during import are stored here. */
125     protected Stack JavaDoc m_groupsToCreate;
126
127     /** Indicates if module data is being imported. */
128     protected boolean m_importingChannelData;
129
130     /** The import-path to write resources into the cms. */
131     protected String JavaDoc m_importPath;
132
133     /** The import-resource (folder) to load resources from. */
134     protected File JavaDoc m_importResource;
135
136     /** The import-resource (zip) to load resources from. */
137     protected ZipFile JavaDoc m_importZip;
138
139     /** Storage for all pointer properties which must be converted into links. */
140     protected Map JavaDoc m_linkPropertyStorage;
141
142     /** Storage for all pointers which must be converted into links. */
143     protected Map JavaDoc m_linkStorage;
144
145     /** The object to report the log messages. */
146     protected I_CmsReport m_report;
147
148     /** Messages object with the locale of the current user. */
149     protected I_CmsMessageBundle m_userMessages;
150
151     /**
152      * Converts a given digest to base64 encoding.<p>
153      *
154      * @param value the digest value in the legacy encoding
155      * @return the digest in the new encoding
156      */

157     public String JavaDoc convertDigestEncoding(String JavaDoc value) {
158
159         byte[] data = new byte[value.length() / 2];
160
161         for (int i = 0; i < data.length; i++) {
162             data[i] = (byte)(Integer.parseInt(value.substring(i * 2, i * 2 + 2), 16) - 128);
163         }
164
165         return new String JavaDoc(Base64.encodeBase64(data));
166     }
167
168     /**
169      * Checks if the resources is in the list of immutalbe resources. <p>
170      *
171      * @param translatedName the name of the resource
172      * @param immutableResources the list of the immutable resources
173      * @return true or false
174      */

175     protected boolean checkImmutable(String JavaDoc translatedName, List JavaDoc immutableResources) {
176
177         boolean resourceNotImmutable = true;
178         if (immutableResources.contains(translatedName)) {
179             if (LOG.isDebugEnabled()) {
180                 LOG.debug(Messages.get().getBundle().key(
181                     Messages.LOG_IMPORTEXPORT_RESOURCENAME_IMMUTABLE_1,
182                     translatedName));
183             }
184             // this resource must not be modified by an import if it already exists
185
m_cms.getRequestContext().saveSiteRoot();
186             try {
187                 m_cms.getRequestContext().setSiteRoot("/");
188                 m_cms.readResource(translatedName);
189                 resourceNotImmutable = false;
190                 if (LOG.isDebugEnabled()) {
191                     LOG.debug(Messages.get().getBundle().key(
192                         Messages.LOG_IMPORTEXPORT_IMMUTABLE_FLAG_SET_1,
193                         translatedName));
194                 }
195             } catch (CmsException e) {
196                 // resourceNotImmutable will be true
197
if (LOG.isDebugEnabled()) {
198                     LOG.debug(Messages.get().getBundle().key(
199                         Messages.LOG_IMPORTEXPORT_ERROR_ON_TEST_IMMUTABLE_1,
200                         translatedName), e);
201                 }
202             } finally {
203                 m_cms.getRequestContext().restoreSiteRoot();
204             }
205         }
206         return resourceNotImmutable;
207     }
208
209     /**
210      * Cleans up member variables after the import is finished.<p>
211      *
212      * This is required since there is only one instance for
213      * each import version that is kept in memory and reused.<p>
214      */

215     protected void cleanUp() {
216
217         m_importResource = null;
218         m_importZip = null;
219         m_report = null;
220         m_linkStorage = null;
221         m_linkPropertyStorage = null;
222         m_groupsToCreate = null;
223         m_cms = null;
224     }
225
226     /**
227      * Converts old style pointers to siblings if possible.<p>
228      */

229     protected void convertPointerToSiblings() {
230
231         Iterator JavaDoc keys = m_linkStorage.keySet().iterator();
232         int linksSize = m_linkStorage.size();
233         int i = 0;
234         CmsResource resource = null;
235         String JavaDoc link = null;
236         String JavaDoc key = null;
237
238         try {
239             // loop through all links to convert
240
while (keys.hasNext()) {
241
242                 try {
243                     key = (String JavaDoc)keys.next();
244                     link = (String JavaDoc)m_linkStorage.get(key);
245                     List JavaDoc properties = (List JavaDoc)m_linkPropertyStorage.get(key);
246                     CmsProperty.setAutoCreatePropertyDefinitions(properties, true);
247
248                     m_report.print(org.opencms.report.Messages.get().container(
249                         org.opencms.report.Messages.RPT_SUCCESSION_2,
250                         String.valueOf(++i),
251                         String.valueOf(linksSize)), I_CmsReport.FORMAT_NOTE);
252                     m_report.print(Messages.get().container(Messages.RPT_CONVERT_LINK_0), I_CmsReport.FORMAT_NOTE);
253                     m_report.print(org.opencms.report.Messages.get().container(
254                         org.opencms.report.Messages.RPT_ARGUMENT_1,
255                         key + " "));
256                     m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
257
258                     // check if this is an internal pointer
259
if (link.startsWith("/")) {
260                         // check if the pointer target is existing
261
CmsResource target = m_cms.readResource(link);
262
263                         // create a new sibling as CmsResource
264
resource = new CmsResource(
265                             new CmsUUID(), // structure ID is always a new UUID
266
target.getResourceId(),
267                             key,
268                             target.getTypeId(),
269                             target.isFolder(),
270                             0,
271                             m_cms.getRequestContext().currentProject().getId(), // TODO: pass flags from import
272
CmsResource.STATE_NEW,
273                             target.getDateCreated(),
274                             target.getUserCreated(),
275                             target.getDateLastModified(),
276                             target.getUserLastModified(),
277                             CmsResource.DATE_RELEASED_DEFAULT,
278                             CmsResource.DATE_EXPIRED_DEFAULT,
279                             1,
280                             0);
281
282                         m_cms.importResource(key, resource, null, properties);
283                         m_report.println(org.opencms.report.Messages.get().container(
284                             org.opencms.report.Messages.RPT_OK_0), I_CmsReport.FORMAT_OK);
285
286                         if (LOG.isInfoEnabled()) {
287                             LOG.info(Messages.get().getBundle().key(
288                                 Messages.LOG_CONVERT_LINK_DOTS_OK_3,
289                                 String.valueOf(i),
290                                 String.valueOf(linksSize),
291                                 key));
292                         }
293
294                     } else {
295
296                         m_cms.createResource(key, CmsResourceTypePointer.getStaticTypeId(), link.getBytes(), properties);
297                         m_report.println(org.opencms.report.Messages.get().container(
298                             org.opencms.report.Messages.RPT_OK_0), I_CmsReport.FORMAT_OK);
299
300                         if (LOG.isInfoEnabled()) {
301                             LOG.info(Messages.get().getBundle().key(
302                                 Messages.LOG_CONVERT_LINK_OK_3,
303                                 String.valueOf(i),
304                                 String.valueOf(linksSize),
305                                 key));
306                         }
307
308                     }
309                 } catch (CmsException e) {
310                     m_report.println();
311                     m_report.print(
312                         Messages.get().container(Messages.RPT_CONVERT_LINK_NOTFOUND_1, link),
313                         I_CmsReport.FORMAT_WARNING);
314
315                     if (LOG.isErrorEnabled()) {
316                         LOG.error(Messages.get().getBundle().key(
317                             Messages.ERR_IMPORTEXPORT_LINK_CONVERSION_FAILED_2,
318                             key,
319                             link), e);
320                     }
321                 }
322             }
323         } finally {
324             if (m_linkStorage != null) {
325                 m_linkStorage.clear();
326             }
327             m_linkStorage = null;
328
329             if (m_linkPropertyStorage != null) {
330                 m_linkPropertyStorage.clear();
331             }
332             m_linkPropertyStorage = null;
333         }
334     }
335
336     /**
337      * Returns a byte array containing the content of the file.<p>
338      *
339      * @param filename the name of the file to read
340      * @return a byte array containing the content of the file
341      */

342     protected byte[] getFileBytes(String JavaDoc filename) {
343
344         try {
345             // is this a zip-file?
346
if (m_importZip != null) {
347                 // yes
348
ZipEntry JavaDoc entry = m_importZip.getEntry(filename);
349
350                 // path to file might be relative, too
351
if (entry == null && filename.startsWith("/")) {
352                     entry = m_importZip.getEntry(filename.substring(1));
353                 }
354                 if (entry == null) {
355                     throw new ZipException JavaDoc(Messages.get().getBundle().key(
356                         Messages.LOG_IMPORTEXPORT_FILE_NOT_FOUND_IN_ZIP_1,
357                         filename));
358                 }
359
360                 InputStream JavaDoc stream = m_importZip.getInputStream(entry);
361                 int size = new Long JavaDoc(entry.getSize()).intValue();
362                 return CmsFileUtil.readFully(stream, size);
363             } else {
364                 // no - use directory
365
File JavaDoc file = new File JavaDoc(m_importResource, filename);
366                 return CmsFileUtil.readFile(file);
367             }
368         } catch (FileNotFoundException JavaDoc fnfe) {
369             if (LOG.isErrorEnabled()) {
370                 LOG.error(Messages.get().getBundle().key(Messages.ERR_IMPORTEXPORT_FILE_NOT_FOUND_1, filename), fnfe);
371             }
372             m_report.println(fnfe);
373         } catch (IOException JavaDoc ioe) {
374             if (LOG.isErrorEnabled()) {
375                 LOG.error(Messages.get().getBundle().key(Messages.ERR_IMPORTEXPORT_ERROR_READING_FILE_1, filename), ioe);
376             }
377             m_report.println(ioe);
378         }
379         // this will only be returned in case there was an exception
380
return "".getBytes();
381     }
382
383     /**
384      * Creates a new access control entry and stores it for later write out.
385      *
386      * @param res the resource
387      * @param id the id of the principal
388      * @param allowed the allowed permissions
389      * @param denied the denied permissions
390      * @param flags the flags
391      *
392      * @return the created ACE
393      */

394     protected CmsAccessControlEntry getImportAccessControlEntry(
395         CmsResource res,
396         String JavaDoc id,
397         String JavaDoc allowed,
398         String JavaDoc denied,
399         String JavaDoc flags) {
400
401         return new CmsAccessControlEntry(
402             res.getResourceId(),
403             new CmsUUID(id),
404             Integer.parseInt(allowed),
405             Integer.parseInt(denied),
406             Integer.parseInt(flags));
407     }
408
409     /**
410      * Returns the appropriate locale for the given destination.<p>
411      *
412      * @param destination the destination path (parent must exist)
413      * @param properties the properties to check at first
414      *
415      * @return the locale
416      */

417     protected Locale JavaDoc getLocale(String JavaDoc destination, List JavaDoc properties) {
418
419         String JavaDoc localeName = CmsProperty.get(CmsPropertyDefinition.PROPERTY_LOCALE, properties).getValue();
420
421         if (localeName != null) {
422             // locale was already set on the files properties
423
return (Locale JavaDoc)OpenCms.getLocaleManager().getAvailableLocales(localeName).get(0);
424         }
425         // locale not set in properties, read default locales
426
return (Locale JavaDoc)OpenCms.getLocaleManager().getDefaultLocales(m_cms, CmsResource.getParentFolder(destination)).get(
427             0);
428     }
429
430     /**
431      * Writes alread imported access control entries for a given resource.
432      *
433      * @param resource the resource assigned to the access control entries
434      * @param aceList the access control entries to create
435      */

436     protected void importAccessControlEntries(CmsResource resource, List JavaDoc aceList) {
437
438         if (aceList.size() == 0) {
439             // no ACE in the list
440
return;
441         }
442         try {
443             m_cms.importAccessControlEntries(resource, aceList);
444         } catch (CmsException exc) {
445
446             m_report.println(
447                 Messages.get().container(Messages.RPT_IMPORT_ACL_DATA_FAILED_0),
448                 I_CmsReport.FORMAT_WARNING);
449         }
450     }
451
452     /**
453      * Imports a single group.<p>
454      *
455      * @param name the name of the group
456      * @param description group description
457      * @param flags group flags
458      * @param parentgroupName name of the parent group
459      *
460      * @throws CmsImportExportException if something goes wrong
461      */

462     protected void importGroup(String JavaDoc name, String JavaDoc description, String JavaDoc flags, String JavaDoc parentgroupName)
463     throws CmsImportExportException {
464
465         if (description == null) {
466             description = "";
467         }
468
469         CmsGroup parentGroup = null;
470         try {
471             if (CmsStringUtil.isNotEmpty(parentgroupName)) {
472                 try {
473                     parentGroup = m_cms.readGroup(parentgroupName);
474                 } catch (CmsException exc) {
475                     // parentGroup will be null
476
}
477             }
478
479             if (CmsStringUtil.isNotEmpty(parentgroupName) && (parentGroup == null)) {
480                 // cannot create group, put on stack and try to create later
481
Map JavaDoc groupData = new HashMap JavaDoc();
482                 groupData.put(CmsImportExportManager.N_NAME, name);
483                 groupData.put(CmsImportExportManager.N_DESCRIPTION, description);
484                 groupData.put(CmsImportExportManager.N_FLAGS, flags);
485                 groupData.put(CmsImportExportManager.N_PARENTGROUP, parentgroupName);
486                 m_groupsToCreate.push(groupData);
487             } else {
488                 try {
489                     m_report.print(Messages.get().container(Messages.RPT_IMPORT_GROUP_0), I_CmsReport.FORMAT_NOTE);
490                     m_report.print(org.opencms.report.Messages.get().container(
491                         org.opencms.report.Messages.RPT_ARGUMENT_1,
492                         name));
493                     m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
494                     m_cms.createGroup(name, description, Integer.parseInt(flags), parentgroupName);
495                     m_report.println(
496                         org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
497                         I_CmsReport.FORMAT_OK);
498                 } catch (CmsException exc) {
499
500                     m_report.println(Messages.get().container(Messages.RPT_NOT_CREATED_0), I_CmsReport.FORMAT_OK);
501                 }
502             }
503
504         } catch (Exception JavaDoc e) {
505
506             m_report.println(e);
507
508             CmsMessageContainer message = Messages.get().container(
509                 Messages.ERR_IMPORTEXPORT_ERROR_IMPORTING_GROUP_1,
510                 name);
511             if (LOG.isDebugEnabled()) {
512                 LOG.debug(message.key(), e);
513             }
514             throw new CmsImportExportException(message, e);
515         }
516     }
517
518     /**
519      * Imports the OpenCms groups.<p>
520      *
521      * @throws CmsImportExportException if something goes wrong
522      */

523     protected void importGroups() throws CmsImportExportException {
524
525         List JavaDoc groupNodes;
526         Element currentElement;
527         String JavaDoc name, description, flags, parentgroup;
528         try {
529             // getAll group nodes
530
groupNodes = m_docXml.selectNodes("//" + CmsImportExportManager.N_GROUPDATA);
531             // walk through all groups in manifest
532
for (int i = 0; i < groupNodes.size(); i++) {
533                 currentElement = (Element)groupNodes.get(i);
534                 name = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_NAME);
535                 name = OpenCms.getImportExportManager().translateGroup(name);
536                 description = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_DESCRIPTION);
537                 flags = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_FLAGS);
538                 parentgroup = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_PARENTGROUP);
539                 if ((parentgroup != null) && (parentgroup.length() > 0)) {
540                     parentgroup = OpenCms.getImportExportManager().translateGroup(parentgroup);
541                 }
542                 // import this group
543

544                 importGroup(name, description, flags, parentgroup);
545             }
546
547             // now try to import the groups in the stack
548
while (!m_groupsToCreate.empty()) {
549                 Stack JavaDoc tempStack = m_groupsToCreate;
550                 m_groupsToCreate = new Stack JavaDoc();
551                 while (tempStack.size() > 0) {
552                     Map JavaDoc groupdata = (HashMap JavaDoc)tempStack.pop();
553                     name = (String JavaDoc)groupdata.get(CmsImportExportManager.N_NAME);
554                     description = (String JavaDoc)groupdata.get(CmsImportExportManager.N_DESCRIPTION);
555                     flags = (String JavaDoc)groupdata.get(CmsImportExportManager.N_FLAGS);
556                     parentgroup = (String JavaDoc)groupdata.get(CmsImportExportManager.N_PARENTGROUP);
557                     // try to import the group
558
importGroup(name, description, flags, parentgroup);
559                 }
560             }
561         } catch (CmsImportExportException e) {
562
563             throw e;
564         } catch (Exception JavaDoc e) {
565
566             m_report.println(e);
567
568             CmsMessageContainer message = Messages.get().container(Messages.ERR_IMPORTEXPORT_ERROR_IMPORTING_GROUPS_0);
569             if (LOG.isDebugEnabled()) {
570                 LOG.debug(message.key(), e);
571             }
572
573             throw new CmsImportExportException(message, e);
574         }
575     }
576
577     /**
578      * Imports a single user.<p>
579      * @param name user name
580      * @param description user description
581      * @param flags user flags
582      * @param password user password
583      * @param firstname firstname of the user
584      * @param lastname lastname of the user
585      * @param email user email
586      * @param address user address
587      * @param type user type
588      * @param userInfo user info
589      * @param userGroups user groups
590      *
591      * @throws CmsImportExportException in case something goes wrong
592      */

593     protected void importUser(
594         String JavaDoc name,
595         String JavaDoc description,
596         String JavaDoc flags,
597         String JavaDoc password,
598         String JavaDoc firstname,
599         String JavaDoc lastname,
600         String JavaDoc email,
601         String JavaDoc address,
602         String JavaDoc type,
603         Map JavaDoc userInfo,
604         List JavaDoc userGroups) throws CmsImportExportException {
605
606         // create a new user id
607
String JavaDoc id = new CmsUUID().toString();
608         try {
609             try {
610                 m_report.print(Messages.get().container(Messages.RPT_IMPORT_USER_0), I_CmsReport.FORMAT_NOTE);
611                 m_report.print(org.opencms.report.Messages.get().container(
612                     org.opencms.report.Messages.RPT_ARGUMENT_1,
613                     name));
614                 m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
615                 m_cms.importUser(
616                     id,
617                     name,
618                     password,
619                     description,
620                     firstname,
621                     lastname,
622                     email,
623                     address,
624                     Integer.parseInt(flags),
625                     Integer.parseInt(type),
626                     userInfo);
627                 // add user to all groups list
628
for (int i = 0; i < userGroups.size(); i++) {
629                     try {
630                         m_cms.addUserToGroup(name, (String JavaDoc)userGroups.get(i));
631                     } catch (CmsException exc) {
632                         // ignore
633
}
634                 }
635                 m_report.println(
636                     org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
637                     I_CmsReport.FORMAT_OK);
638             } catch (CmsException exc) {
639                 m_report.println(Messages.get().container(Messages.RPT_NOT_CREATED_0), I_CmsReport.FORMAT_OK);
640             }
641         } catch (Exception JavaDoc e) {
642
643             m_report.println(e);
644
645             CmsMessageContainer message = Messages.get().container(
646                 Messages.ERR_IMPORTEXPORT_ERROR_IMPORTING_USER_1,
647                 name);
648             if (LOG.isDebugEnabled()) {
649                 LOG.debug(message.key(), e);
650             }
651             throw new CmsImportExportException(message, e);
652         }
653     }
654
655     /**
656      * Imports the OpenCms users.<p>
657      *
658      * @throws CmsImportExportException if something goes wrong
659      */

660     protected void importUsers() throws CmsImportExportException {
661
662         List JavaDoc userNodes;
663         List JavaDoc groupNodes;
664         List JavaDoc userGroups;
665         Element currentElement, currentGroup;
666         Map JavaDoc userInfo = new HashMap JavaDoc();
667         String JavaDoc name, description, flags, password, firstname, lastname, email, address, type, pwd, infoNode, defaultGroup;
668         // try to get the import resource
669
//getImportResource();
670
try {
671             // getAll user nodes
672
userNodes = m_docXml.selectNodes("//" + CmsImportExportManager.N_USERDATA);
673             // walk threw all groups in manifest
674
for (int i = 0; i < userNodes.size(); i++) {
675                 currentElement = (Element)userNodes.get(i);
676                 name = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_NAME);
677                 name = OpenCms.getImportExportManager().translateUser(name);
678                 // decode passwords using base 64 decoder
679
pwd = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_PASSWORD);
680                 password = new String JavaDoc(Base64.decodeBase64(pwd.trim().getBytes()));
681                 description = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_DESCRIPTION);
682                 flags = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_FLAGS);
683                 firstname = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_FIRSTNAME);
684                 lastname = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_LASTNAME);
685                 email = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_EMAIL);
686                 address = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_TAG_ADDRESS);
687                 type = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_TYPE);
688                 defaultGroup = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_DEFAULTGROUP);
689                 // get the userinfo and put it into the additional info map
690
infoNode = CmsImport.getChildElementTextValue(currentElement, CmsImportExportManager.N_USERINFO);
691                 try {
692                     // read the userinfo from the dat-file
693
byte[] value = getFileBytes(infoNode);
694                     // deserialize the object
695
ByteArrayInputStream JavaDoc bin = new ByteArrayInputStream JavaDoc(value);
696                     ObjectInputStream JavaDoc oin = new ObjectInputStream JavaDoc(bin);
697                     userInfo = (Map JavaDoc)oin.readObject();
698                 } catch (IOException JavaDoc ioex) {
699                     m_report.println(ioex);
700                 }
701
702                 // get the groups of the user and put them into the list
703
groupNodes = currentElement.selectNodes("*/" + CmsImportExportManager.N_GROUPNAME);
704                 userGroups = new ArrayList JavaDoc();
705                 for (int j = 0; j < groupNodes.size(); j++) {
706                     currentGroup = (Element)groupNodes.get(j);
707                     String JavaDoc userInGroup = CmsImport.getChildElementTextValue(currentGroup, CmsImportExportManager.N_NAME);
708                     userInGroup = OpenCms.getImportExportManager().translateGroup(userInGroup);
709                     userGroups.add(userInGroup);
710                 }
711
712                 if (CmsStringUtil.isNotEmpty(defaultGroup)) {
713                     userInfo.put(CmsUserSettings.ADDITIONAL_INFO_DEFAULTGROUP, defaultGroup);
714                 }
715
716                 // import this user
717
importUser(
718                     name,
719                     description,
720                     flags,
721                     password,
722                     firstname,
723                     lastname,
724                     email,
725                     address,
726                     type,
727                     userInfo,
728                     userGroups);
729             }
730         } catch (CmsImportExportException e) {
731
732             throw e;
733         } catch (Exception JavaDoc e) {
734
735             m_report.println(e);
736
737             CmsMessageContainer message = Messages.get().container(Messages.ERR_IMPORTEXPORT_ERROR_IMPORTING_USERS_0);
738             if (LOG.isDebugEnabled()) {
739                 LOG.debug(message.key(), e);
740             }
741
742             throw new CmsImportExportException(message, e);
743         }
744     }
745
746     /**
747      * Initializes all member variables before the import is started.<p>
748      *
749      * This is required since there is only one instance for
750      * each import version that is kept in memory and reused.<p>
751      */

752     protected void initialize() {
753
754         m_groupsToCreate = new Stack JavaDoc();
755     }
756
757     /**
758      * Reads all properties below a specified parent element from the <code>manifest.xml</code>.<p>
759      *
760      * @param parentElement the current file node
761      * @param ignoredPropertyKeys a list of properies to be ignored
762      *
763      * @return a list with all properties
764      */

765     protected List JavaDoc readPropertiesFromManifest(Element parentElement, List JavaDoc ignoredPropertyKeys) {
766
767         // all imported Cms property objects are collected in map first forfaster access
768
Map JavaDoc properties = new HashMap JavaDoc();
769         CmsProperty property = null;
770         List JavaDoc propertyElements = parentElement.selectNodes("./"
771             + CmsImportExportManager.N_PROPERTIES
772             + "/"
773             + CmsImportExportManager.N_PROPERTY);
774         Element propertyElement = null;
775         String JavaDoc key = null, value = null;
776         Attribute attrib = null;
777
778         // iterate over all property elements
779
for (int i = 0, n = propertyElements.size(); i < n; i++) {
780             propertyElement = (Element)propertyElements.get(i);
781             key = CmsImport.getChildElementTextValue(propertyElement, CmsImportExportManager.N_NAME);
782
783             if (key == null || ignoredPropertyKeys.contains(key)) {
784                 // continue if the current property (key) should be ignored or is null
785
continue;
786             }
787
788             // all Cms properties are collected in a map keyed by their property keys
789
property = (CmsProperty)properties.get(key);
790             if (property == null) {
791                 property = new CmsProperty();
792                 property.setName(key);
793                 property.setAutoCreatePropertyDefinition(true);
794                 properties.put(key, property);
795             }
796
797             value = CmsImport.getChildElementTextValue(propertyElement, CmsImportExportManager.N_VALUE);
798             if (value == null) {
799                 value = "";
800             }
801
802             attrib = propertyElement.attribute(CmsImportExportManager.N_PROPERTY_ATTRIB_TYPE);
803             if ((attrib != null) && attrib.getValue().equals(CmsImportExportManager.N_PROPERTY_ATTRIB_TYPE_SHARED)) {
804                 // it is a shared/resource property value
805
property.setResourceValue(value);
806             } else {
807                 // it is an individual/structure value
808
property.setStructureValue(value);
809             }
810         }
811
812         return new ArrayList JavaDoc(properties.values());
813     }
814 }
Popular Tags