KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/com/opencms/legacy/CmsExportModuledata.java,v $
3  * Date : $Date: 2006/03/27 14:53:03 $
4  * Version: $Revision: 1.13 $
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  * version 2.1 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * For further information about Alkacon Software, please see the
25  * company website: http://www.alkacon.com
26  *
27  * For further information about OpenCms, please see the
28  * project website: http://www.opencms.org
29  *
30  * You should have received a copy of the GNU Lesser General Public
31  * License along with this library; if not, write to the Free Software
32  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33  */

34
35 package com.opencms.legacy;
36
37 import org.opencms.file.CmsObject;
38 import org.opencms.file.CmsProperty;
39 import org.opencms.file.CmsPropertyDefinition;
40 import org.opencms.file.CmsResource;
41 import org.opencms.file.CmsResourceFilter;
42 import org.opencms.i18n.CmsMessageContainer;
43 import org.opencms.importexport.CmsExport;
44 import org.opencms.importexport.CmsImportExportException;
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.util.CmsDateUtil;
50 import org.opencms.util.CmsXmlSaxWriter;
51
52 import com.opencms.defaults.master.*;
53
54 import java.io.IOException JavaDoc;
55 import java.io.OutputStreamWriter JavaDoc;
56 import java.io.Serializable JavaDoc;
57 import java.lang.reflect.Constructor JavaDoc;
58 import java.lang.reflect.InvocationTargetException JavaDoc;
59 import java.util.Arrays JavaDoc;
60 import java.util.Enumeration JavaDoc;
61 import java.util.HashSet JavaDoc;
62 import java.util.Iterator JavaDoc;
63 import java.util.List JavaDoc;
64 import java.util.Set JavaDoc;
65 import java.util.Vector JavaDoc;
66 import java.util.zip.ZipEntry JavaDoc;
67
68 import org.apache.commons.logging.Log;
69
70 import org.dom4j.Document;
71 import org.dom4j.DocumentHelper;
72 import org.dom4j.Element;
73 import org.dom4j.io.SAXWriter;
74 import org.xml.sax.SAXException JavaDoc;
75
76 /**
77  * Holds the functionaility to export channels and modulemasters from the cms
78  * to the filesystem.
79  *
80  * @author Alexander Kandzior (a.kandzior@alkacon.com)
81  * @author Michael Emmerich (m.emmerich@alkacon.com)
82  *
83  * @version $Revision: 1.13 $ $Date: 2006/03/27 14:53:03 $
84  *
85  * @deprecated Will not be supported past the OpenCms 6 release.
86  */

87 public class CmsExportModuledata extends CmsExport implements Serializable JavaDoc {
88
89     /** Serial version UID required for safe serialization. */
90     private static final long serialVersionUID = -8416442909693060452L;
91
92     /** Manifest tag: master. */
93     public static String JavaDoc C_EXPORT_TAG_MASTER = "master";
94
95     /** Manifest tag: access_flags. */
96     public static String JavaDoc C_EXPORT_TAG_MASTER_ACCESSFLAGS = "access_flags";
97
98     /** Manifest tag: channelname. */
99     public static String JavaDoc C_EXPORT_TAG_MASTER_CHANNELNAME = "channelname";
100
101     /** Manifest tag: channelrelations. */
102     public static String JavaDoc C_EXPORT_TAG_MASTER_CHANNELREL = "channelrelations";
103
104     /** Manifest tag: creation_date. */
105     public static String JavaDoc C_EXPORT_TAG_MASTER_CREATEDATE = "create_date";
106
107     /** Manifest tag: data_big_. */
108     public static String JavaDoc C_EXPORT_TAG_MASTER_DATABIG = "data_big_";
109
110     /** Manifest tag: data_date_. */
111     public static String JavaDoc C_EXPORT_TAG_MASTER_DATADATE = "data_date_";
112
113     /** Manifest tag: data_int_. */
114     public static String JavaDoc C_EXPORT_TAG_MASTER_DATAINT = "data_int_";
115
116     /** Manifest tag: data_medium_. */
117     public static String JavaDoc C_EXPORT_TAG_MASTER_DATAMEDIUM = "data_medium_";
118
119     /** Manifest tag: data_reference_. */
120     public static String JavaDoc C_EXPORT_TAG_MASTER_DATAREFERENCE = "data_reference_";
121
122     /** Manifest tag: dataset. */
123     public static String JavaDoc C_EXPORT_TAG_MASTER_DATASET = "dataset";
124
125     /** Manifest tag: data_small_. */
126     public static String JavaDoc C_EXPORT_TAG_MASTER_DATASMALL = "data_small_";
127
128     /** Manifest tag: feed_filename. */
129     public static String JavaDoc C_EXPORT_TAG_MASTER_FEEDFILENAME = "feed_filename";
130
131     /** Manifest tag: feed_id. */
132     public static String JavaDoc C_EXPORT_TAG_MASTER_FEEDID = "feed_id";
133
134     /** Manifest tag: feed_reference. */
135     public static String JavaDoc C_EXPORT_TAG_MASTER_FEEDREFERENCE = "feed_reference";
136
137     /** Manifest tag: flags. */
138     public static String JavaDoc C_EXPORT_TAG_MASTER_FLAGS = "flags";
139
140     /** Manifest tag: group_name. */
141     public static String JavaDoc C_EXPORT_TAG_MASTER_GROUP = "group_name";
142
143     /** Manifest tag: master ID. */
144     public static String JavaDoc C_EXPORT_TAG_MASTER_ID = "master_id";
145
146     /** Manifest tag: media. */
147     public static String JavaDoc C_EXPORT_TAG_MASTER_MEDIA = "media";
148
149     /** Manifest tag: mediaset. */
150     public static String JavaDoc C_EXPORT_TAG_MASTER_MEDIASET = "mediaset";
151
152     /** Manifest tag: publication_date. */
153     public static String JavaDoc C_EXPORT_TAG_MASTER_PUBLICATIONDATE = "publication_date";
154
155     /** Manifest tag: purge_date. */
156     public static String JavaDoc C_EXPORT_TAG_MASTER_PURGEDATE = "purge_date";
157
158     /** Manifest tag: sub_id. */
159     public static String JavaDoc C_EXPORT_TAG_MASTER_SUBID = "sub_id";
160
161     /** Manifest tag: title. */
162     public static String JavaDoc C_EXPORT_TAG_MASTER_TITLE = "title";
163
164     /** Manifest tag: user_name. */
165     public static String JavaDoc C_EXPORT_TAG_MASTER_USER = "user_name";
166
167     /** Manifest tag: masters. */
168     public static String JavaDoc C_EXPORT_TAG_MASTERS = "masters";
169
170     /** Manifest tag: media_content. */
171     public static String JavaDoc C_EXPORT_TAG_MEDIA_CONTENT = "media_content";
172
173     /** Manifest tag: media_description. */
174     public static String JavaDoc C_EXPORT_TAG_MEDIA_DESCRIPTION = "media_description";
175
176     /** Manifest tag: media_height. */
177     public static String JavaDoc C_EXPORT_TAG_MEDIA_HEIGHT = "media_height";
178
179     /** Manifest tag: media_mimetype. */
180     public static String JavaDoc C_EXPORT_TAG_MEDIA_MIMETYPE = "media_mimetype";
181
182     /** Manifest tag: media_name. */
183     public static String JavaDoc C_EXPORT_TAG_MEDIA_NAME = "media_name";
184
185     /** Manifest tag: media_position. */
186     public static String JavaDoc C_EXPORT_TAG_MEDIA_POSITION = "media_position";
187
188     /** Manifest tag: media_size. */
189     public static String JavaDoc C_EXPORT_TAG_MEDIA_SIZE = "media_size";
190
191     /** Manifest tag: media_title. */
192     public static String JavaDoc C_EXPORT_TAG_MEDIA_TITLE = "media_title";
193
194     /** Manifest tag: media_type. */
195     public static String JavaDoc C_EXPORT_TAG_MEDIA_TYPE = "media_type";
196
197     /** Manifest tag: media_width. */
198     public static String JavaDoc C_EXPORT_TAG_MEDIA_WIDTH = "media_width";
199
200     /** The log object for this class. */
201     private static final Log LOG = CmsLog.getLog(CmsExportModuledata.class);
202
203     /** The channelid and the resourceobject of the exported channels. */
204     private Set JavaDoc m_exportedChannelIds;
205
206     /** Holds information about contents that have already been exported. */
207     private Vector JavaDoc m_exportedMasters = new Vector JavaDoc();
208
209     /**
210      * This constructs a new CmsExportModuledata-object which exports the channels and modulemasters.
211      *
212      * @param cms the cms-object to work with
213      * @param exportFile the filename of the zip to export to
214      * @param resourcesToExport the cos folders (channels) to export
215      * @param modulesToExport the modules to export
216      * @param report to write the progress information to
217      *
218      * @throws CmsImportExportException if something goes wrong
219      */

220     public CmsExportModuledata(
221         CmsObject cms,
222         String JavaDoc exportFile,
223         String JavaDoc[] resourcesToExport,
224         String JavaDoc[] modulesToExport,
225         I_CmsReport report)
226     throws CmsImportExportException {
227
228         setCms(cms);
229         setReport(report);
230         setExportFileName(exportFile);
231         setExportedChannelIds(new HashSet JavaDoc());
232
233         // save the site root
234
getCms().getRequestContext().saveSiteRoot();
235
236         // open the export file
237
Element exportNode;
238         try {
239             exportNode = openExportFile();
240
241             // first export the cos folders (ie. channels)
242
getReport().println(
243                 Messages.get().container(Messages.RPT_EXPORT_CHANNELS_BEGIN_0),
244                 I_CmsReport.FORMAT_HEADLINE);
245
246             getCms().getRequestContext().setSiteRoot(CmsResource.VFS_FOLDER_CHANNELS);
247             // export all the resources
248
exportAllResources(exportNode, Arrays.asList(resourcesToExport));
249             getReport().println(
250                 Messages.get().container(Messages.RPT_EXPORT_CHANNELS_END_0),
251                 I_CmsReport.FORMAT_HEADLINE);
252
253             // get the modules to export
254
Vector JavaDoc modules = new Vector JavaDoc();
255             Vector JavaDoc moduleNames = new Vector JavaDoc();
256             for (int i = 0; i < modulesToExport.length; i++) {
257                 String JavaDoc modName = modulesToExport[i];
258                 if (modName != null && !"".equals(modName)) {
259                     moduleNames.addElement(modulesToExport[i]);
260                 }
261             }
262
263             List JavaDoc moduleExportables = CmsLegacyModuleAction.getLegacyModulePublishClasses();
264             // if there was no module selected then select all exportable modules,
265
// else get only the modules from Hashtable that were selected
266
if (moduleNames.size() == 0) {
267                 if (resourcesToExport.length > 0) {
268                     Iterator JavaDoc modElements = moduleExportables.iterator();
269                     while (modElements.hasNext()) {
270                         modules.add(modElements.next());
271                     }
272                 }
273             } else {
274                 modules = moduleNames;
275             }
276
277             // now do the export for all modules with the given channel ids
278
Enumeration JavaDoc enumModules = modules.elements();
279             while (enumModules.hasMoreElements()) {
280                 // get the name of the content definition class
281
String JavaDoc classname = (String JavaDoc)enumModules.nextElement();
282                 exportCos(exportNode, classname, getExportedChannelIds());
283             }
284
285             // close the export file
286
closeExportFile(exportNode);
287
288         } catch (CmsException e) {
289             getReport().println(e);
290
291             CmsMessageContainer message = Messages.get().container(
292                 Messages.ERR_COS_IMPORTEXPORT_ERROR_EXPORTING_TO_FILE_1,
293                 getExportFileName());
294             if (LOG.isDebugEnabled()) {
295                 LOG.debug(message, e);
296             }
297
298             throw new CmsImportExportException(message, e);
299         } catch (SAXException JavaDoc se) {
300             getReport().println(se);
301
302             CmsMessageContainer message = Messages.get().container(
303                 Messages.ERR_COS_IMPORTEXPORT_ERROR_EXPORTING_TO_FILE_1,
304                 getExportFileName());
305             if (LOG.isDebugEnabled()) {
306                 LOG.debug(message, se);
307             }
308
309             throw new CmsImportExportException(message, se);
310         } catch (IOException JavaDoc ioe) {
311             getReport().println(ioe);
312
313             CmsMessageContainer message = Messages.get().container(
314                 Messages.ERR_COS_IMPORTEXPORT_ERROR_EXPORTING_TO_FILE_1,
315                 getExportFileName());
316             if (LOG.isDebugEnabled()) {
317                 LOG.debug(message, ioe);
318             }
319
320             throw new CmsImportExportException(message, ioe);
321         } finally {
322             // restore the site root
323
getCms().getRequestContext().restoreSiteRoot();
324         }
325     }
326
327     /**
328      * @see org.opencms.importexport.CmsExport#addChildResources(java.lang.String)
329      */

330     protected void addChildResources(String JavaDoc folderName) throws CmsImportExportException, IOException JavaDoc, SAXException JavaDoc {
331
332         try {
333             // collect channel id information
334
String JavaDoc channelId = getCms().readFolder(folderName, CmsResourceFilter.IGNORE_EXPIRATION).getResourceId().toString();
335             if (channelId != null) {
336                 getExportedChannelIds().add(channelId);
337             }
338
339             // continue with super implementation
340
super.addChildResources(folderName);
341         } catch (CmsImportExportException e) {
342
343             throw e;
344         } catch (CmsException e) {
345
346             CmsMessageContainer message = Messages.get().container(
347                 Messages.ERR_COS_IMPORTEXPORT_ERROR_ADDING_CHILD_RESOURCES_1,
348                 folderName);
349             if (LOG.isDebugEnabled()) {
350                 LOG.debug(message, e);
351             }
352
353             throw new CmsImportExportException(message, e);
354         }
355     }
356
357     /**
358      * Returns the set of already exported channel Ids.<p>
359      *
360      * @return the set of already exported channel Ids
361      */

362     protected Set JavaDoc getExportedChannelIds() {
363
364         return m_exportedChannelIds;
365     }
366
367     /**
368      * @see org.opencms.importexport.CmsExport#getExportNodeName()
369      */

370     protected String JavaDoc getExportNodeName() {
371
372         return com.opencms.core.I_CmsConstants.C_EXPORT_TAG_MODULEXPORT;
373     }
374
375     /**
376      * @see org.opencms.importexport.CmsExport#getResourceNodeName()
377      */

378     protected String JavaDoc getResourceNodeName() {
379
380         return "channels";
381     }
382
383     /**
384      * @see org.opencms.importexport.CmsExport#isIgnoredProperty(org.opencms.file.CmsProperty)
385      */

386     protected boolean isIgnoredProperty(CmsProperty property) {
387
388         if (property == null) {
389             return true;
390         }
391         // don't export "channel" property
392
return CmsPropertyDefinition.PROPERTY_CHANNELID.equals(property.getName());
393     }
394
395     /**
396      * Sets the set of already exported channel Ids.<p>
397      *
398      * @param exportedChannelIds the set of already exported channel Ids
399      */

400     protected void setExportedChannelIds(Set JavaDoc exportedChannelIds) {
401
402         m_exportedChannelIds = exportedChannelIds;
403     }
404
405     /**
406      * Exports the content definition data,
407      * only content definition data from selected channels will be exported.<p>
408      *
409      * @param parent the export root node
410      * @param classname name of the content definition class
411      * @param exportedChannelIds set of channels that have been exported
412      * @throws CmsException if something goes wrong
413      * @throws SAXException if something goes wrong procesing the manifest.xml
414      */

415     private void exportCos(Element parent, String JavaDoc classname, Set JavaDoc exportedChannelIds) throws CmsException, SAXException JavaDoc {
416
417         // output something to the report for the data
418
getReport().println(
419             Messages.get().container(Messages.RPT_EXPORT_MODULE_BEGIN_1, classname),
420             I_CmsReport.FORMAT_HEADLINE);
421
422         Iterator JavaDoc keys = exportedChannelIds.iterator();
423         // get the subId of the module
424
int subId = getContentDefinition(classname, new Class JavaDoc[] {CmsObject.class}, new Object JavaDoc[] {getCms()}).getSubId();
425         // the number for identifying each master
426
int masterNr = 1;
427
428         Element masters = parent.addElement(C_EXPORT_TAG_MASTERS);
429         getSaxWriter().writeOpen(masters);
430
431         while (keys.hasNext()) {
432             String JavaDoc channelId = (String JavaDoc)keys.next();
433             try {
434                 Vector JavaDoc allDatasets = new Vector JavaDoc();
435                 // execute the static method readAllByChannel of the content definition class
436
allDatasets = (Vector JavaDoc)Class.forName(classname).getMethod(
437                     "readAllByChannel",
438                     new Class JavaDoc[] {CmsObject.class, String JavaDoc.class, Integer JavaDoc.class}).invoke(
439                     null,
440                     new Object JavaDoc[] {getCms(), channelId, new Integer JavaDoc(subId)});
441
442                 for (int i = 0; i < allDatasets.size(); i++) {
443                     CmsMasterDataSet curDataset = (CmsMasterDataSet)allDatasets.elementAt(i);
444                     if (!m_exportedMasters.contains("" + curDataset.m_masterId)) {
445                         exportCosModule(masters, classname, curDataset, masterNr, subId);
446                         m_exportedMasters.add("" + curDataset.m_masterId);
447                         masterNr++;
448                     }
449                 }
450             } catch (InvocationTargetException JavaDoc e) {
451                 getReport().println(e);
452                 if (CmsLog.getLog(this).isErrorEnabled()) {
453                     CmsLog.getLog(this).error("Error exporting COS data for channel id " + channelId, e);
454                 }
455             } catch (NoSuchMethodException JavaDoc e) {
456                 getReport().println(e);
457                 if (CmsLog.getLog(this).isErrorEnabled()) {
458                     CmsLog.getLog(this).error("Error exporting COS data for channel id " + channelId, e);
459                 }
460             } catch (IllegalArgumentException JavaDoc e) {
461                 getReport().println(e);
462                 if (CmsLog.getLog(this).isErrorEnabled()) {
463                     CmsLog.getLog(this).error("Error exporting COS data for channel id " + channelId, e);
464                 }
465             } catch (SecurityException JavaDoc e) {
466                 getReport().println(e);
467                 if (CmsLog.getLog(this).isErrorEnabled()) {
468                     CmsLog.getLog(this).error("Error exporting COS data for channel id " + channelId, e);
469                 }
470             } catch (IllegalAccessException JavaDoc e) {
471                 getReport().println(e);
472                 if (CmsLog.getLog(this).isErrorEnabled()) {
473                     CmsLog.getLog(this).error("Error exporting COS data for channel id " + channelId, e);
474                 }
475             } catch (ClassNotFoundException JavaDoc e) {
476                 getReport().println(e);
477                 if (CmsLog.getLog(this).isErrorEnabled()) {
478                     CmsLog.getLog(this).error("Error exporting COS data for channel id " + channelId, e);
479                 }
480             }
481         }
482
483         getSaxWriter().writeClose(masters);
484         parent.remove(masters);
485
486         getReport().println(
487             Messages.get().container(Messages.RPT_EXPORT_MODULE_END_0, classname),
488             I_CmsReport.FORMAT_HEADLINE);
489     }
490
491     /**
492      * Export a single content definition.<P>
493      *
494      * @param parent the export root node
495      * @param classname name of the content definition class
496      * @param dataset data for the content definition object instance
497      * @param masterNr id of master
498      * @param subId id of content definition
499      *
500      * @throws CmsException if something goes wrong
501      * @throws SAXException if something goes wrong procesing the manifest.xml
502      */

503     private void exportCosModule(Element parent, String JavaDoc classname, CmsMasterDataSet dataset, int masterNr, int subId)
504     throws CmsException, SAXException JavaDoc {
505
506         // output something to the report for the resource
507

508         getReport().print(
509             Messages.get().container(Messages.RPT_EXPORT_WITH_ID_2, dataset.m_title, dataset.m_masterId),
510             I_CmsReport.FORMAT_NOTE);
511
512         // the name of the XML-file where the dataset is stored
513
String JavaDoc dataSetFile = "dataset_" + subId + "_" + masterNr + ".xml";
514         // create new mastercontent for getting channels and media
515
CmsMasterContent content = getContentDefinition(
516             classname,
517             new Class JavaDoc[] {CmsObject.class, CmsMasterDataSet.class},
518             new Object JavaDoc[] {getCms(), dataset});
519         // write these informations to the xml-manifest
520
Element master = parent.addElement(C_EXPORT_TAG_MASTER);
521
522         master.addElement(C_EXPORT_TAG_MASTER_SUBID).addText(Integer.toString(subId));
523         // add the name of the datasetfile and create the datasetfile
524
// with the information from the dataset
525
master.addElement(C_EXPORT_TAG_MASTER_DATASET).addText(dataSetFile);
526         exportCosModuleData(dataset, dataSetFile, masterNr, subId);
527         // add the channel relation of this master
528
Element channelrel = master.addElement(C_EXPORT_TAG_MASTER_CHANNELREL);
529         Vector JavaDoc moduleChannels = content.getChannels();
530         for (int i = 0; i < moduleChannels.size(); i++) {
531             String JavaDoc channelname = (String JavaDoc)moduleChannels.elementAt(i);
532             channelrel.addElement(C_EXPORT_TAG_MASTER_CHANNELNAME).addText(channelname);
533         }
534         // add the mediaset
535
Element mediaset = master.addElement(C_EXPORT_TAG_MASTER_MEDIASET);
536         Vector JavaDoc moduleMedia = content.getMedia();
537         for (int i = 0; i < moduleMedia.size(); i++) {
538             // for each media add the name of the xml-file for the mediadata to the manifest
539
// and create the files for the media information
540
String JavaDoc mediaFile = "media_" + subId + "_" + masterNr + "_" + i + ".xml";
541             mediaset.addElement(C_EXPORT_TAG_MASTER_MEDIA).addText(mediaFile);
542             exportCosModuleMedia((CmsMasterMedia)moduleMedia.elementAt(i), mediaFile, masterNr, subId, i);
543         }
544         // write the XML
545
digestElement(parent, master);
546         getReport().println(
547             org.opencms.report.Messages.get().container(org.opencms.report.Messages.RPT_OK_0),
548             I_CmsReport.FORMAT_OK);
549     }
550
551     /**
552      * Exports a content definition content in a "dataset_xxx.xml" file and a number of
553      * "datayyy_xxx.dat" files.<p>
554      *
555      * @param dataset data for the content definition object instance
556      * @param filename name of the zip file for the module data export
557      * @param masterNr id of master
558      * @param subId id of content definition
559      *
560      * @throws CmsException if something goes wrong
561      */

562     private void exportCosModuleData(CmsMasterDataSet dataset, String JavaDoc filename, int masterNr, int subId)
563     throws CmsException {
564
565         // create a new xml document
566
Document doc = DocumentHelper.createDocument();
567         Element data = doc.addElement(com.opencms.core.I_CmsConstants.C_EXPORT_TAG_MODULEXPORT).addElement(
568             C_EXPORT_TAG_MASTER_DATASET);
569
570         // add the data of the contentdefinition
571
// get the name of the owner
572
String JavaDoc ownerName = "";
573         try {
574             ownerName = getCms().readUser(dataset.m_userId).getName();
575         } catch (CmsException e) {
576             if (CmsLog.getLog(this).isErrorEnabled()) {
577                 CmsLog.getLog(this).error("Unable to read user with id " + dataset.m_userId, e);
578             }
579         }
580         // get the name of the group
581
String JavaDoc groupName = "";
582         try {
583             groupName = getCms().readGroup(dataset.m_groupId).getName();
584         } catch (CmsException e) {
585             if (CmsLog.getLog(this).isErrorEnabled()) {
586                 CmsLog.getLog(this).error("Unable to read group with id " + dataset.m_groupId, e);
587             }
588         }
589
590         data.addElement(C_EXPORT_TAG_MASTER_ID).addText(dataset.m_masterId.toString());
591         data.addElement(C_EXPORT_TAG_MASTER_USER).addText(ownerName);
592         data.addElement(C_EXPORT_TAG_MASTER_GROUP).addText(groupName);
593         data.addElement(C_EXPORT_TAG_MASTER_ACCESSFLAGS).addText(Integer.toString(dataset.m_accessFlags));
594         data.addElement(C_EXPORT_TAG_MASTER_PUBLICATIONDATE).addText(
595             CmsDateUtil.getDateTimeShort(dataset.m_publicationDate));
596         data.addElement(C_EXPORT_TAG_MASTER_PURGEDATE).addText(CmsDateUtil.getDateTimeShort(dataset.m_purgeDate));
597         data.addElement(C_EXPORT_TAG_MASTER_FLAGS).addText(Integer.toString(dataset.m_flags));
598         data.addElement(C_EXPORT_TAG_MASTER_FEEDID).addText(Integer.toString(dataset.m_feedId));
599         data.addElement(C_EXPORT_TAG_MASTER_FEEDREFERENCE).addText(Integer.toString(dataset.m_feedReference));
600         data.addElement(C_EXPORT_TAG_MASTER_FEEDFILENAME).addText(
601             dataset.m_feedFilename != null ? dataset.m_feedFilename : "");
602         data.addElement(C_EXPORT_TAG_MASTER_TITLE).addCDATA(dataset.m_title != null ? dataset.m_title : "");
603
604         // get the values of data_big from the string array
605
for (int i = 0; i < dataset.m_dataBig.length; i++) {
606             String JavaDoc value = dataset.m_dataBig[i];
607             String JavaDoc dataFile = new String JavaDoc();
608             if (value != null && !"".equals(value)) {
609                 // the name of the file where the value of the field is stored
610
dataFile = "databig_" + subId + "_" + masterNr + "_" + i + ".dat";
611                 writeExportFile(dataFile, value.getBytes());
612             }
613             data.addElement(C_EXPORT_TAG_MASTER_DATABIG + i).addText(dataFile);
614         }
615         // get the values of data_medium from the string array
616
for (int i = 0; i < dataset.m_dataMedium.length; i++) {
617             String JavaDoc value = dataset.m_dataMedium[i];
618             String JavaDoc dataFile = new String JavaDoc();
619             if (value != null && !"".equals(value)) {
620                 // the name of the file where the value of the field is stored
621
dataFile = "datamedium_" + subId + "_" + masterNr + "_" + i + ".dat";
622                 writeExportFile(dataFile, value.getBytes());
623             }
624             data.addElement(C_EXPORT_TAG_MASTER_DATAMEDIUM + i).addText(dataFile);
625         }
626         // get the values of data_small from the string array
627
for (int i = 0; i < dataset.m_dataSmall.length; i++) {
628             String JavaDoc value = dataset.m_dataSmall[i];
629             String JavaDoc dataFile = new String JavaDoc();
630             if (value != null && !"".equals(value)) {
631                 // the name of the file where the value of the field is stored
632
dataFile = "datasmall_" + subId + "_" + masterNr + "_" + i + ".dat";
633                 writeExportFile(dataFile, value.getBytes());
634             }
635             data.addElement(C_EXPORT_TAG_MASTER_DATASMALL + i).addText(dataFile);
636         }
637         // get the values of data_int from the int array
638
for (int i = 0; i < dataset.m_dataInt.length; i++) {
639             String JavaDoc value = "" + dataset.m_dataInt[i];
640             data.addElement(C_EXPORT_TAG_MASTER_DATAINT + i).addText(value);
641         }
642         // get the values of data_reference from the int array
643
for (int i = 0; i < dataset.m_dataReference.length; i++) {
644             String JavaDoc value = "" + dataset.m_dataReference[i];
645             data.addElement(C_EXPORT_TAG_MASTER_DATAREFERENCE + i).addText(value);
646         }
647         // get the values of data_reference from the int array
648
for (int i = 0; i < dataset.m_dataDate.length; i++) {
649             String JavaDoc value = CmsDateUtil.getDateTimeShort(dataset.m_dataDate[i]);
650             data.addElement(C_EXPORT_TAG_MASTER_DATADATE + i).addText(value);
651         }
652
653         try {
654             // set up new zip entry
655
ZipEntry JavaDoc entry = new ZipEntry JavaDoc(filename);
656             getExportZipStream().putNextEntry(entry);
657             // generate the SAX XML writer
658
CmsXmlSaxWriter saxHandler = new CmsXmlSaxWriter(
659                 new OutputStreamWriter JavaDoc(getExportZipStream()),
660                 OpenCms.getSystemInfo().getDefaultEncoding());
661             // write the document
662
(new SAXWriter(saxHandler, saxHandler)).write(doc);
663             // close zip entry
664
getExportZipStream().closeEntry();
665         } catch (SAXException JavaDoc e) {
666             getReport().println(e);
667             if (CmsLog.getLog(this).isErrorEnabled()) {
668                 CmsLog.getLog(this).error("Unable to write ZIP dataset file " + filename, e);
669             }
670             throw new CmsLegacyException(CmsLegacyException.C_UNKNOWN_EXCEPTION, e);
671         } catch (IOException JavaDoc e) {
672             getReport().println(e);
673             if (CmsLog.getLog(this).isErrorEnabled()) {
674                 CmsLog.getLog(this).error("Unable to write ZIP dataset file " + filename, e);
675             }
676             throw new CmsLegacyException(CmsLegacyException.C_UNKNOWN_EXCEPTION, e);
677         }
678     }
679
680     /**
681      * Exports a media object, creates a "media_xxx.xml" and a "mediacontent_xxx.dat" data file.<p>
682      *
683      * @param media data for the media object instance
684      * @param filename name of the xml file for the media data export
685      * @param masterNr id of master
686      * @param subId id of content definition
687      * @param mediaId if od media object
688      *
689      * @throws CmsException if something goes wrong
690      */

691     private void exportCosModuleMedia(CmsMasterMedia media, String JavaDoc filename, int masterNr, int subId, int mediaId)
692     throws CmsException {
693
694         // create a new xml document
695
Document doc = DocumentHelper.createDocument();
696         Element data = doc.addElement(com.opencms.core.I_CmsConstants.C_EXPORT_TAG_MODULEXPORT);
697         // add the data element
698
Element em = data.addElement(C_EXPORT_TAG_MASTER_MEDIA);
699         // add the data of the contentdefinition
700
em.addElement(C_EXPORT_TAG_MEDIA_POSITION).addText(Integer.toString(media.getPosition()));
701         em.addElement(C_EXPORT_TAG_MEDIA_WIDTH).addText(Integer.toString(media.getWidth()));
702         em.addElement(C_EXPORT_TAG_MEDIA_HEIGHT).addText(Integer.toString(media.getHeight()));
703         em.addElement(C_EXPORT_TAG_MEDIA_SIZE).addText(Integer.toString(media.getSize()));
704         em.addElement(C_EXPORT_TAG_MEDIA_MIMETYPE).addText(media.getMimetype() != null ? media.getMimetype() : "");
705         em.addElement(C_EXPORT_TAG_MEDIA_TYPE).addText(Integer.toString(media.getType()));
706         em.addElement(C_EXPORT_TAG_MEDIA_TITLE).addCDATA(media.getTitle() != null ? media.getTitle() : "");
707         em.addElement(C_EXPORT_TAG_MEDIA_NAME).addCDATA(media.getName() != null ? media.getName() : "");
708         em.addElement(C_EXPORT_TAG_MEDIA_DESCRIPTION).addCDATA(
709             media.getDescription() != null ? media.getDescription() : "");
710         // now add the name of the file where the media content is stored and write this file
711
String JavaDoc contentFilename = "mediacontent_" + subId + "_" + masterNr + "_" + mediaId + ".dat";
712         em.addElement(C_EXPORT_TAG_MEDIA_CONTENT).addText(contentFilename);
713         writeExportFile(contentFilename, media.getMedia());
714         try {
715             // set up new zip entry
716
ZipEntry JavaDoc entry = new ZipEntry JavaDoc(filename);
717             getExportZipStream().putNextEntry(entry);
718             // generate the SAX XML writer
719
CmsXmlSaxWriter saxHandler = new CmsXmlSaxWriter(
720                 new OutputStreamWriter JavaDoc(getExportZipStream()),
721                 OpenCms.getSystemInfo().getDefaultEncoding());
722             // write the document
723
(new SAXWriter(saxHandler, saxHandler)).write(doc);
724             // close zip entry
725
getExportZipStream().closeEntry();
726         } catch (SAXException JavaDoc e) {
727             getReport().println(e);
728             if (CmsLog.getLog(this).isErrorEnabled()) {
729                 CmsLog.getLog(this).error("Unable to write ZIP media content file " + contentFilename, e);
730             }
731             throw new CmsLegacyException(CmsLegacyException.C_UNKNOWN_EXCEPTION, e);
732         } catch (IOException JavaDoc e) {
733             getReport().println(e);
734             if (CmsLog.getLog(this).isErrorEnabled()) {
735                 CmsLog.getLog(this).error("Unable to write ZIP media content file " + contentFilename, e);
736             }
737             throw new CmsLegacyException(CmsLegacyException.C_UNKNOWN_EXCEPTION, e);
738         }
739     }
740
741     /**
742      * Returns a master content definition object instance created with the reflection API.<p>
743      *
744      * @param classname name of the content definition class
745      * @param classes required for constructor generation
746      * @param objects instances to be used as parameters for class instance generation
747      *
748      * @return a master content definition object instance created with the reflection API
749      */

750     private CmsMasterContent getContentDefinition(String JavaDoc classname, Class JavaDoc[] classes, Object JavaDoc[] objects) {
751
752         CmsMasterContent cd = null;
753         try {
754             Class JavaDoc cdClass = Class.forName(classname);
755             Constructor JavaDoc co = cdClass.getConstructor(classes);
756             cd = (CmsMasterContent)co.newInstance(objects);
757         } catch (InvocationTargetException JavaDoc e) {
758             getReport().println(e);
759             if (CmsLog.getLog(this).isErrorEnabled()) {
760                 CmsLog.getLog(this).error("Error generating instance for class " + classname, e);
761             }
762         } catch (NoSuchMethodException JavaDoc e) {
763             getReport().println(e);
764             if (CmsLog.getLog(this).isErrorEnabled()) {
765                 CmsLog.getLog(this).error("Error generating instance for class " + classname, e);
766             }
767         } catch (InstantiationException JavaDoc e) {
768             getReport().println(e);
769             if (CmsLog.getLog(this).isErrorEnabled()) {
770                 CmsLog.getLog(this).error("Error generating instance for class " + classname, e);
771             }
772         } catch (ClassNotFoundException JavaDoc e) {
773             getReport().println(e);
774             if (CmsLog.getLog(this).isErrorEnabled()) {
775                 CmsLog.getLog(this).error("Error generating instance for class " + classname, e);
776             }
777         } catch (IllegalArgumentException JavaDoc e) {
778             getReport().println(e);
779             if (CmsLog.getLog(this).isErrorEnabled()) {
780                 CmsLog.getLog(this).error("Error generating instance for class " + classname, e);
781             }
782         } catch (IllegalAccessException JavaDoc e) {
783             getReport().println(e);
784             if (CmsLog.getLog(this).isErrorEnabled()) {
785                 CmsLog.getLog(this).error("Error generating instance for class " + classname, e);
786             }
787         }
788         return cd;
789     }
790
791     /**
792      * Writes a binary content to a "*.dat" file in the export zip.<p>
793      *
794      * @param filename name of the file, usually ends with .dat
795      * @param content contents to write to the file
796      */

797     private void writeExportFile(String JavaDoc filename, byte[] content) {
798
799         try {
800             // store the userinfo in zip-file
801
ZipEntry JavaDoc entry = new ZipEntry JavaDoc(filename);
802             getExportZipStream().putNextEntry(entry);
803             getExportZipStream().write(content);
804             getExportZipStream().closeEntry();
805         } catch (IOException JavaDoc ioex) {
806             getReport().println(ioex);
807             if (CmsLog.getLog(this).isErrorEnabled()) {
808                 CmsLog.getLog(this).error("Unable to write ZIP for filename " + filename, ioex);
809             }
810         }
811     }
812 }
Popular Tags