KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opencms > legacy > CmsImportModuledata


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

33
34 package com.opencms.legacy;
35
36 import org.opencms.file.CmsObject;
37 import org.opencms.file.CmsResource;
38 import org.opencms.i18n.CmsEncoder;
39 import org.opencms.i18n.CmsMessageContainer;
40 import org.opencms.importexport.CmsImport;
41 import org.opencms.importexport.CmsImportExportException;
42 import org.opencms.importexport.I_CmsImport;
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.util.CmsUUID;
48 import org.opencms.xml.CmsXmlException;
49
50 import com.opencms.defaults.master.*;
51
52 import java.io.File JavaDoc;
53 import java.io.FileInputStream JavaDoc;
54 import java.io.InputStream JavaDoc;
55 import java.io.Serializable JavaDoc;
56 import java.lang.reflect.Constructor JavaDoc;
57 import java.lang.reflect.InvocationTargetException JavaDoc;
58 import java.text.ParseException JavaDoc;
59 import java.text.SimpleDateFormat JavaDoc;
60 import java.util.Hashtable JavaDoc;
61 import java.util.Iterator JavaDoc;
62 import java.util.List JavaDoc;
63 import java.util.Vector JavaDoc;
64 import java.util.zip.ZipEntry JavaDoc;
65
66 import org.apache.commons.logging.Log;
67
68 import org.dom4j.Document;
69 import org.dom4j.Element;
70
71 /**
72  * Holds the functionaility to import resources from the filesystem
73  * or a zip file into the OpenCms COS.
74  *
75  * @author Alexander Kandzior (a.kandzior@alkacon.com)
76  * @author Michael Emmerich (m.emmerich@alkacon.com)
77  * @author Thomas Weckert (t.weckert@alkacon.com)
78  *
79  * @version $Revision: 1.18 $ $Date: 2006/03/27 14:53:03 $
80  *
81  * @deprecated Will not be supported past the OpenCms 6 release.
82  */

83 public class CmsImportModuledata extends CmsImport implements Serializable JavaDoc {
84     
85     /** Serial version UID required for safe serialization. */
86     private static final long serialVersionUID = 2474902557481830457L;
87     
88     /** The log object for this class. */
89     private static final Log LOG = CmsLog.getLog(CmsImportModuledata.class);
90
91     /**
92      * Constructs a new import object which imports the module data from an OpenCms
93      * export zip file or a folder in the "real" file system.<p>
94      *
95      * @param cms the current cms object
96      * @param importFile the file or folder to import from
97      * @param importPath the path in the cms VFS to import into
98      * @param report a report object to output the progress information to
99      */

100     public CmsImportModuledata(CmsObject cms, String JavaDoc importFile, String JavaDoc importPath, I_CmsReport report) {
101
102         // set member variables
103
m_cms = cms;
104         m_importFile = importFile;
105         m_importPath = importPath;
106         m_report = report;
107         m_importingChannelData = true;
108         m_importImplementations = OpenCms.getImportExportManager().getImportVersionClasses();
109     }
110
111     /**
112      * Gets the available modules in the current system
113      * and imports the data for existing modules.<p>
114      * @throws CmsException in case something goes wrong
115      */

116     public void importModuleMasters() throws CmsException {
117
118         // get list of legacy modules that have a publish class
119
Iterator JavaDoc it = CmsLegacyModuleAction.getLegacyModulePublishClasses().iterator();
120
121         // now get the subIds of each module
122
Hashtable JavaDoc availableModules = new Hashtable JavaDoc();
123         while (it.hasNext()) {
124             String JavaDoc classname = (String JavaDoc)it.next();
125             // get the subId of the module
126
try {
127                 int subId = getContentDefinition(classname, new Class JavaDoc[] {CmsObject.class}, new Object JavaDoc[] {m_cms}).getSubId();
128                 // put the subid and the classname into the hashtable of available modules
129
availableModules.put("" + subId, classname);
130             } catch (Exception JavaDoc e) {
131                 // do nothing
132
}
133
134         }
135         // now get the moduledata for import
136
List JavaDoc masterNodes;
137         Element currentMasterElement;
138         String JavaDoc subid;
139
140         try {
141             // get all master-nodes
142
masterNodes = m_docXml.selectNodes("//" + CmsExportModuledata.C_EXPORT_TAG_MASTER);
143             int length = masterNodes.size();
144
145             // walk through all files in manifest
146
for (int i = 0; i < length; i++) {
147                 currentMasterElement = (Element)masterNodes.get(i);
148                 // get the subid of the modulemaster
149
subid = CmsImport.getChildElementTextValue(
150                     currentMasterElement,
151                     CmsExportModuledata.C_EXPORT_TAG_MASTER_SUBID);
152                 // check if there exists a module with this subid
153
String JavaDoc classname = (String JavaDoc)availableModules.get(subid);
154                 if ((classname != null) && !("".equals(classname.trim()))) {
155                     // import the dataset, the channelrelation and the media
156
m_report.print(org.opencms.report.Messages.get().container(
157                         org.opencms.report.Messages.RPT_SUCCESSION_2,
158                         String.valueOf(i + 1),
159                         String.valueOf(length)), I_CmsReport.FORMAT_NOTE);
160                     importMaster(subid, classname, currentMasterElement);
161                 }
162             }
163         } catch (Exception JavaDoc exc) {
164             throw new CmsLegacyException(CmsLegacyException.C_UNKNOWN_EXCEPTION, exc);
165         }
166     }
167
168     /**
169      * Imports the moduledata and writes them to the cms even if there already exist
170      * conflicting files.<p>
171      *
172      * @throws CmsImportExportException if something goes wrong
173      * @throws CmsXmlException if the manifest of the import could not be unmarshalled
174      */

175     public synchronized void importResources() throws CmsImportExportException, CmsXmlException {
176
177         // initialize the import
178
openImportFile();
179         m_report.println(
180             Messages.get().container(Messages.RPT_IMPORT_VERSION_1, new Integer JavaDoc(m_importVersion)),
181             I_CmsReport.FORMAT_NOTE);
182         try {
183             // first import the channels
184
m_report.println(
185                 Messages.get().container(Messages.RPT_IMPORT_CHANNELS_BEGIN_0),
186                 I_CmsReport.FORMAT_HEADLINE);
187             //importAllResources(null, null, null, null, null);
188
// now find the correct import implementation
189
m_cms.getRequestContext().saveSiteRoot();
190             m_cms.getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS);
191             Iterator JavaDoc i = m_importImplementations.iterator();
192             while (i.hasNext()) {
193                 I_CmsImport imp = (I_CmsImport)i.next();
194                 if (imp.getVersion() == m_importVersion) {
195                     // this is the correct import version, so call it for the import process
196
imp.importResources(
197                         m_cms,
198                         m_importPath,
199                         m_report,
200                         m_importResource,
201                         m_importZip,
202                         m_docXml);
203                     break;
204                 }
205             }
206             m_cms.getRequestContext().restoreSiteRoot();
207             m_report.println(
208                 Messages.get().container(Messages.RPT_IMPORT_CHANNELS_END_0),
209                 I_CmsReport.FORMAT_HEADLINE);
210
211             // now import the moduledata
212
m_report.println(
213                 Messages.get().container(Messages.RPT_IMPORT_MODULE_BEGIN_0),
214                 I_CmsReport.FORMAT_HEADLINE);
215             importModuleMasters();
216             m_report.println(Messages.get().container(Messages.RPT_IMPORT_MODULE_END_0), I_CmsReport.FORMAT_HEADLINE);
217         } catch (CmsXmlException e) {
218
219             throw e;
220         } catch (CmsImportExportException e) {
221
222             throw e;
223         } catch (CmsException e) {
224             m_report.println(e);
225
226             CmsMessageContainer message = Messages.get().container(
227                 Messages.ERR_COS_IMPORTEXPORT_ERROR_IMPORTING_RESOURCES_0);
228             if (LOG.isDebugEnabled()) {
229                 LOG.debug(message.key(), e);
230             }
231
232             throw new CmsImportExportException(message, e);
233         } finally {
234             // close the import file
235
closeImportFile();
236         }
237     }
238
239     /**
240      * Gets the content definition class method constructor.<p>
241      *
242      * @param classname the name of the cd class
243      * @param classes types needed for cd constructor
244      * @param objects objects needed for cd constructor
245      * @return content definition object
246      */

247     protected CmsMasterContent getContentDefinition(String JavaDoc classname, Class JavaDoc[] classes, Object JavaDoc[] objects) {
248
249         CmsMasterContent cd = null;
250         try {
251             Class JavaDoc cdClass = Class.forName(classname);
252             Constructor JavaDoc co = cdClass.getConstructor(classes);
253             cd = (CmsMasterContent)co.newInstance(objects);
254         } catch (InvocationTargetException JavaDoc ite) {
255             if (CmsLog.getLog(this).isWarnEnabled()) {
256                 CmsLog.getLog(this).warn("Invocation target exception", ite);
257             }
258         } catch (NoSuchMethodException JavaDoc nsm) {
259             if (CmsLog.getLog(this).isWarnEnabled()) {
260                 CmsLog.getLog(this).warn("Requested method was not found", nsm);
261             }
262         } catch (InstantiationException JavaDoc ie) {
263             if (CmsLog.getLog(this).isWarnEnabled()) {
264                 CmsLog.getLog(this).warn("The reflected class is abstract", ie);
265             }
266         } catch (Exception JavaDoc e) {
267             if (CmsLog.getLog(this).isWarnEnabled()) {
268                 CmsLog.getLog(this).warn("Other exception", e);
269             }
270         }
271         return cd;
272     }
273
274     /**
275      * Gets the channel relations for the master from the xml file.<p>
276      *
277      * @param masterElement the current element of the xml file
278      * @return vector containing the ids of all channels of the master
279      */

280     protected Vector JavaDoc getMasterChannelRelation(Element masterElement) {
281
282         Vector JavaDoc channelRelations = new Vector JavaDoc();
283         // get the channelnames of the master
284
List JavaDoc channelNodes = masterElement.selectNodes("*/" + CmsExportModuledata.C_EXPORT_TAG_MASTER_CHANNELNAME);
285
286         // walk through all channelrelations
287
for (int j = 0; j < channelNodes.size(); j++) {
288             // get the name of the channel
289
String JavaDoc channelName = ((Element)channelNodes.get(j)).getTextTrim();
290             // try to read the channel and get its channelid
291
if ((channelName != null) && !("".equals(channelName.trim()))) {
292                 channelRelations.addElement(channelName);
293             }
294         }
295         return channelRelations;
296     }
297
298     /**
299      * Gets the dataset for the master from the xml file.<p>
300      *
301      * @param subId the subid of the module
302      * @param masterElement the current element of the xml file
303      * @return the dataset with the imported information
304      * @throws CmsException in case something goes wrong
305      */

306     protected CmsMasterDataSet getMasterDataSet(int subId, Element masterElement) throws CmsException {
307
308         String JavaDoc datasetfile = null, username = null, groupname = null, accessFlags = null, publicationDate = null, purgeDate = null, creationDate = null, flags = null, feedId = null, feedReference = null, feedFilename = null, title = null, master_id = null;
309
310         CmsMasterDataSet newDataset = new CmsMasterDataSet();
311
312         // get the file with the dataset of the master
313
datasetfile = ((Element)masterElement.selectNodes("./" + CmsExportModuledata.C_EXPORT_TAG_MASTER_DATASET).get(0)).getTextTrim();
314
315         Document datasetXml = CmsImportVersion1.getXmlDocument(getFileInputStream(datasetfile));
316         Element dataset = (Element)datasetXml.getRootElement().selectNodes(
317             "./" + CmsExportModuledata.C_EXPORT_TAG_MASTER_DATASET).get(0);
318
319         // get the information from the dataset and add it to the dataset
320
// first add the subid
321
newDataset.m_subId = subId;
322
323         master_id = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_ID);
324         if (master_id != null) {
325             newDataset.m_masterId = new CmsUUID(master_id);
326         } else {
327             newDataset.m_masterId = CmsUUID.getNullUUID();
328         }
329
330         // get the id of the user or set the owner to the current user
331
username = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_USER);
332         CmsUUID userId = null;
333         try {
334             if ((username != null) && !("".equals(username.trim()))) {
335                 userId = m_cms.readUser(username).getId();
336             }
337         } catch (Exception JavaDoc e) {
338             // userId will be current user
339
userId = m_cms.getRequestContext().currentUser().getId();
340         }
341
342         newDataset.m_userId = userId;
343         // get the id of the group or set the group to the current user
344
groupname = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_GROUP);
345
346         CmsUUID groupId = CmsUUID.getNullUUID();
347         try {
348             if ((groupname != null) && !("".equals(groupname.trim()))) {
349                 groupId = m_cms.readGroup(groupname).getId();
350             }
351         } catch (Exception JavaDoc e) {
352             try {
353                 groupId = m_cms.readGroup(OpenCms.getDefaultUsers().getGroupUsers()).getId();
354             } catch (Exception JavaDoc e2) {
355                 // ignore
356
}
357         }
358
359         newDataset.m_groupId = groupId;
360         // set the accessflags or the default flags
361
accessFlags = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_ACCESSFLAGS);
362         try {
363             newDataset.m_accessFlags = Integer.parseInt(accessFlags);
364         } catch (Exception JavaDoc e) {
365             newDataset.m_accessFlags = com.opencms.core.I_CmsConstants.C_ACCESS_DEFAULT_FLAGS;
366         }
367         // set the publication date
368
publicationDate = CmsImport.getChildElementTextValue(
369             dataset,
370             CmsExportModuledata.C_EXPORT_TAG_MASTER_PUBLICATIONDATE);
371         try {
372             newDataset.m_publicationDate = convertDate(publicationDate);
373         } catch (Exception JavaDoc e) {
374             // ignore
375
}
376         // set the purge date
377
purgeDate = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_PURGEDATE);
378         try {
379             newDataset.m_purgeDate = convertDate(purgeDate);
380         } catch (Exception JavaDoc e) {
381             // ignore
382
}
383         // set the creation date if possible
384
try {
385             creationDate = CmsImport.getChildElementTextValue(
386                 dataset,
387                 CmsExportModuledata.C_EXPORT_TAG_MASTER_CREATEDATE);
388             newDataset.m_dateCreated = convertDate(creationDate);
389         } catch (Exception JavaDoc e) {
390             // ignore
391
}
392         // set the flags
393
flags = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_FLAGS);
394         try {
395             newDataset.m_flags = Integer.parseInt(flags);
396         } catch (Exception JavaDoc e) {
397             // ignore
398
}
399         // set the feedid
400
feedId = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_FEEDID);
401         try {
402             newDataset.m_feedId = Integer.parseInt(feedId);
403         } catch (Exception JavaDoc e) {
404             // ignore
405
}
406         // set the feedreference
407
feedReference = CmsImport.getChildElementTextValue(
408             dataset,
409             CmsExportModuledata.C_EXPORT_TAG_MASTER_FEEDREFERENCE);
410         try {
411             newDataset.m_feedReference = Integer.parseInt(feedReference);
412         } catch (Exception JavaDoc e) {
413             // ignore
414
}
415         // set the feedfilenam
416
feedFilename = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_FEEDFILENAME);
417         newDataset.m_feedFilename = feedFilename;
418         // set the masters title
419
title = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_TITLE);
420         newDataset.m_title = title;
421         // set the values of data_big
422
for (int i = 0; i < newDataset.m_dataBig.length; i++) {
423             String JavaDoc filename = CmsImport.getChildElementTextValue(
424                 dataset,
425                 CmsExportModuledata.C_EXPORT_TAG_MASTER_DATABIG + i);
426             String JavaDoc value = new String JavaDoc();
427             if (filename != null && !"".equals(filename.trim())) {
428                 // get the value from the file
429
value = new String JavaDoc(getFileBytes(filename));
430             }
431             newDataset.m_dataBig[i] = value;
432         }
433         // get the values of data_medium
434
for (int i = 0; i < newDataset.m_dataMedium.length; i++) {
435             String JavaDoc filename = CmsImport.getChildElementTextValue(
436                 dataset,
437                 CmsExportModuledata.C_EXPORT_TAG_MASTER_DATAMEDIUM + i);
438             String JavaDoc value = new String JavaDoc();
439             if (filename != null && !"".equals(filename.trim())) {
440                 // get the value from the file
441
value = new String JavaDoc(getFileBytes(filename));
442             }
443             newDataset.m_dataMedium[i] = value;
444         }
445         // get the values of data_small
446
for (int i = 0; i < newDataset.m_dataSmall.length; i++) {
447             String JavaDoc filename = CmsImport.getChildElementTextValue(
448                 dataset,
449                 CmsExportModuledata.C_EXPORT_TAG_MASTER_DATASMALL + i);
450             String JavaDoc value = new String JavaDoc();
451             if (filename != null && !"".equals(filename.trim())) {
452                 // get the value from the file
453
value = new String JavaDoc(getFileBytes(filename));
454             }
455             newDataset.m_dataSmall[i] = value;
456         }
457         // get the values of data_int
458
for (int i = 0; i < newDataset.m_dataInt.length; i++) {
459             String JavaDoc value = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_DATAINT
460                 + i);
461             try {
462                 newDataset.m_dataInt[i] = new Integer JavaDoc(value).intValue();
463             } catch (Exception JavaDoc e) {
464                 newDataset.m_dataInt[i] = 0;
465             }
466         }
467         // get the values of data_reference
468
for (int i = 0; i < newDataset.m_dataReference.length; i++) {
469             String JavaDoc value = CmsImport.getChildElementTextValue(
470                 dataset,
471                 CmsExportModuledata.C_EXPORT_TAG_MASTER_DATAREFERENCE + i);
472             try {
473                 newDataset.m_dataReference[i] = new Integer JavaDoc(value).intValue();
474             } catch (Exception JavaDoc e) {
475                 newDataset.m_dataReference[i] = 0;
476             }
477         }
478         // get the values of data_date
479
for (int i = 0; i < newDataset.m_dataDate.length; i++) {
480             String JavaDoc value = CmsImport.getChildElementTextValue(dataset, CmsExportModuledata.C_EXPORT_TAG_MASTER_DATADATE
481                 + i);
482             try {
483                 newDataset.m_dataDate[i] = convertDate(value);
484             } catch (Exception JavaDoc e) {
485                 newDataset.m_dataDate[i] = 0;
486             }
487         }
488         return newDataset;
489     }
490
491     /**
492      * Gets the media of the master from the xml file.<p>
493      *
494      * @param masterElement The current element of the xml file
495      * @return vector containing the media (CmsMasterMedia object) of the master
496      * @throws CmsException in case something goes wrong
497      */

498     protected Vector JavaDoc getMasterMedia(Element masterElement) throws CmsException {
499
500         Vector JavaDoc masterMedia = new Vector JavaDoc();
501         // get the mediafiles of the master
502
List JavaDoc mediaNodes = masterElement.selectNodes("*/" + CmsExportModuledata.C_EXPORT_TAG_MASTER_MEDIA);
503         // walk through all media
504
for (int j = 0; j < mediaNodes.size(); j++) {
505             // get the name of the file where the mediadata is stored
506
String JavaDoc mediaFilename = ((Element)mediaNodes.get(j)).getTextTrim();
507             // try to get the information of the media
508
if ((mediaFilename != null) && !("".equals(mediaFilename.trim()))) {
509                 CmsMasterMedia newMedia = getMediaData(mediaFilename);
510                 masterMedia.add(newMedia);
511             }
512         }
513         return masterMedia;
514     }
515
516     /**
517      * Imports a single master.<p>
518      *
519      * @param subId the subid of the module
520      * @param classname the name of the module class
521      * @param masterElement the current element of the xml file
522      * @throws CmsException in case something goes wrong
523      */

524     protected void importMaster(String JavaDoc subId, String JavaDoc classname, Element masterElement) throws CmsException {
525
526         CmsMasterDataSet newDataset = new CmsMasterDataSet();
527         Vector JavaDoc channelRelations = new Vector JavaDoc();
528         Vector JavaDoc masterMedia = new Vector JavaDoc();
529
530         m_report.print(org.opencms.importexport.Messages.get().container(
531             org.opencms.importexport.Messages.RPT_IMPORTING_0), I_CmsReport.FORMAT_NOTE);
532
533         // try to get the dataset
534
try {
535             int subIdInt = Integer.parseInt(subId);
536             newDataset = getMasterDataSet(subIdInt, masterElement);
537         } catch (Exception JavaDoc e) {
538             m_report.println(e);
539             throw new CmsLegacyException("Cannot get dataset ", e);
540         }
541
542         m_report.print(Messages.get().container(
543             Messages.RPT_ARGUMENT_2,
544             CmsEncoder.escapeHtml(newDataset.m_title),
545             classname));
546         m_report.print(org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_DOTS_0));
547
548         // try to get the channelrelations
549
try {
550             channelRelations = getMasterChannelRelation(masterElement);
551         } catch (Exception JavaDoc e) {
552             m_report.println(e);
553             throw new CmsLegacyException("Cannot get channelrelations ", e);
554         }
555
556         // try to get the media
557
try {
558             masterMedia = getMasterMedia(masterElement);
559         } catch (Exception JavaDoc e) {
560             m_report.println(e);
561             throw new CmsLegacyException("Cannot get media ", e);
562         }
563
564         // add the channels and media to the dataset
565
newDataset.m_channelToAdd = channelRelations;
566         newDataset.m_mediaToAdd = masterMedia;
567         // create the new content definition
568
CmsMasterContent newMaster = getContentDefinition(classname, new Class JavaDoc[] {
569             CmsObject.class,
570             CmsMasterDataSet.class}, new Object JavaDoc[] {m_cms, newDataset});
571
572         try {
573             CmsUUID userId = newMaster.getOwner();
574             CmsUUID groupId = newMaster.getGroupId();
575             // first insert the new master
576
newMaster.importMaster();
577             // now update the master because user and group might be changed
578
newMaster.chown(m_cms, userId);
579             newMaster.chgrp(m_cms, groupId);
580         } catch (Exception JavaDoc e) {
581             m_report.println(e);
582             throw new CmsLegacyException("Cannot write master ", e);
583         }
584
585         m_report.println(
586             org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
587             I_CmsReport.FORMAT_OK);
588     }
589
590     /**
591      * Coverts a String Date in long.<p>
592      *
593      * @param date String
594      * @return long converted date
595      */

596     private long convertDate(String JavaDoc date) {
597
598         java.text.SimpleDateFormat JavaDoc formatterFullTime = new SimpleDateFormat JavaDoc("M/d/yy h:mm a");
599         long adate = 0;
600         try {
601             adate = formatterFullTime.parse(date).getTime();
602         } catch (ParseException JavaDoc e) {
603             java.text.SimpleDateFormat JavaDoc formatterFullTimeDe = new SimpleDateFormat JavaDoc("dd.MM.yyyy HH:mm");
604             try {
605                 adate = formatterFullTimeDe.parse(date).getTime();
606             } catch (ParseException JavaDoc e2) {
607                 // ignore
608
}
609         }
610
611         return adate;
612     }
613
614     /**
615      * Returns a buffered reader for this resource using the importFile as root.<p>
616      *
617      * @param filename the name of the file to read
618      * @return the file reader for this file
619      * @throws CmsException in case something goes wrong
620      */

621     private InputStream JavaDoc getFileInputStream(String JavaDoc filename) throws CmsException {
622
623         try {
624             // is this a zip-file?
625
if (m_importZip != null) {
626                 // yes
627
ZipEntry JavaDoc entry = m_importZip.getEntry(filename);
628                 return m_importZip.getInputStream(entry);
629             } else {
630                 // no - use directory
631
File JavaDoc xmlFile = new File JavaDoc(m_importResource, filename);
632                 return new FileInputStream JavaDoc(xmlFile);
633             }
634         } catch (Exception JavaDoc e) {
635             throw new CmsLegacyException(CmsLegacyException.C_UNKNOWN_EXCEPTION, e);
636         }
637     }
638
639     /**
640      * Gets the information for a single media from the media file.<p>
641      *
642      * @param mediaFilename the name of the xml file that contains the media information
643      * @return the media information from the media file
644      * @throws CmsException in case something goes wrong
645      */

646     private CmsMasterMedia getMediaData(String JavaDoc mediaFilename) throws CmsException {
647
648         String JavaDoc position = null, width = null, height = null, size = null, mimetype = null, type = null, title = null, name = null, description = null, contentfile = null;
649         CmsMasterMedia newMedia = null;
650         Document mediaXml = null;
651         Element rootElement = null;
652         byte[] mediacontent = null;
653
654         newMedia = new CmsMasterMedia();
655         mediaXml = CmsImportVersion1.getXmlDocument(getFileInputStream(mediaFilename));
656         rootElement = mediaXml.getRootElement();
657
658         position = ((Element)rootElement.selectNodes("./media/" + CmsExportModuledata.C_EXPORT_TAG_MEDIA_POSITION).get(
659             0)).getTextTrim();
660         try {
661             newMedia.setPosition(Integer.parseInt(position));
662         } catch (Exception JavaDoc e) {
663             // ignore
664
}
665
666         width = ((Element)rootElement.selectNodes("./media/" + CmsExportModuledata.C_EXPORT_TAG_MEDIA_WIDTH).get(0)).getTextTrim();
667         try {
668             newMedia.setWidth(Integer.parseInt(width));
669         } catch (Exception JavaDoc e) {
670             // ignore
671
}
672
673         height = ((Element)rootElement.selectNodes("./media/" + CmsExportModuledata.C_EXPORT_TAG_MEDIA_HEIGHT).get(0)).getTextTrim();
674         try {
675             newMedia.setHeight(Integer.parseInt(height));
676         } catch (Exception JavaDoc e) {
677             // ignore
678
}
679
680         size = ((Element)rootElement.selectNodes("./media/" + CmsExportModuledata.C_EXPORT_TAG_MEDIA_SIZE).get(0)).getTextTrim();
681         try {
682             newMedia.setSize(Integer.parseInt(size));
683         } catch (Exception JavaDoc e) {
684             // ignore
685
}
686
687         mimetype = ((Element)rootElement.selectNodes("./media/" + CmsExportModuledata.C_EXPORT_TAG_MEDIA_MIMETYPE).get(
688             0)).getTextTrim();
689         newMedia.setMimetype(mimetype);
690
691         type = ((Element)rootElement.selectNodes("./media/" + CmsExportModuledata.C_EXPORT_TAG_MEDIA_TYPE).get(0)).getTextTrim();
692         try {
693             newMedia.setType(Integer.parseInt(type));
694         } catch (Exception JavaDoc e) {
695             // ignore
696
}
697
698         title = ((Element)rootElement.selectNodes("./media/" + CmsExportModuledata.C_EXPORT_TAG_MEDIA_TITLE).get(0)).getTextTrim();
699         newMedia.setTitle(title);
700
701         name = ((Element)rootElement.selectNodes("./media/" + CmsExportModuledata.C_EXPORT_TAG_MEDIA_NAME).get(0)).getTextTrim();
702         newMedia.setName(name);
703
704         description = ((Element)rootElement.selectNodes("./media/" + CmsExportModuledata.C_EXPORT_TAG_MEDIA_DESCRIPTION).get(
705             0)).getTextTrim();
706         newMedia.setDescription(description);
707
708         contentfile = ((Element)rootElement.selectNodes("./media/" + CmsExportModuledata.C_EXPORT_TAG_MEDIA_CONTENT).get(
709             0)).getTextTrim();
710         try {
711             mediacontent = getFileBytes(contentfile);
712         } catch (Exception JavaDoc e) {
713             m_report.println(e);
714         }
715         newMedia.setMedia(mediacontent);
716
717         return newMedia;
718     }
719 }
720
Popular Tags