KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > managementtool > actions > ExportRepositoryAction


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.applications.managementtool.actions;
25
26 import java.io.File JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.OutputStreamWriter JavaDoc;
29 import java.util.Date JavaDoc;
30 import java.util.List JavaDoc;
31
32 import org.apache.log4j.Logger;
33 import org.exolab.castor.jdo.Database;
34 import org.exolab.castor.mapping.Mapping;
35 import org.exolab.castor.xml.Marshaller;
36 import org.infoglue.cms.applications.common.VisualFormatter;
37 import org.infoglue.cms.applications.common.actions.InfoGlueAbstractAction;
38 import org.infoglue.cms.controllers.kernel.impl.simple.CastorDatabaseService;
39 import org.infoglue.cms.controllers.kernel.impl.simple.ContentController;
40 import org.infoglue.cms.controllers.kernel.impl.simple.ContentTypeDefinitionController;
41 import org.infoglue.cms.controllers.kernel.impl.simple.RepositoryController;
42 import org.infoglue.cms.controllers.kernel.impl.simple.SiteNodeController;
43 import org.infoglue.cms.entities.content.Content;
44 import org.infoglue.cms.entities.content.impl.simple.ContentImpl;
45 import org.infoglue.cms.entities.management.Repository;
46 import org.infoglue.cms.entities.management.impl.simple.InfoGlueExportImpl;
47 import org.infoglue.cms.entities.structure.SiteNode;
48 import org.infoglue.cms.entities.structure.impl.simple.SiteNodeImpl;
49 import org.infoglue.cms.util.CmsPropertyHandler;
50
51
52 /**
53  * This class handles Exporting of a repository to an XML-file.
54  *
55  * @author mattias
56  */

57
58 public class ExportRepositoryAction extends InfoGlueAbstractAction
59 {
60     private final static Logger logger = Logger.getLogger(ExportRepositoryAction.class.getName());
61
62     private Integer JavaDoc repositoryId = null;
63     
64     private String JavaDoc fileUrl = "";
65     private String JavaDoc fileName = "";
66
67     /**
68      * This shows the dialog before export.
69      * @return
70      * @throws Exception
71      */

72
73     public String JavaDoc doInput() throws Exception JavaDoc
74     {
75         return "input";
76     }
77     
78     /**
79      * This handles the actual exporting.
80      */

81     
82     protected String JavaDoc doExecute() throws Exception JavaDoc
83     {
84         Database db = CastorDatabaseService.getDatabase();
85         
86         try
87         {
88             Mapping map = new Mapping();
89             String JavaDoc exportFormat = CmsPropertyHandler.getExportFormat();
90
91             if(exportFormat.equalsIgnoreCase("2"))
92             {
93                 logger.info("MappingFile:" + CastorDatabaseService.class.getResource("/xml_mapping_site_2.5.xml").toString());
94                 map.loadMapping(CastorDatabaseService.class.getResource("/xml_mapping_site_2.5.xml").toString());
95             }
96             else
97             {
98                 logger.info("MappingFile:" + CastorDatabaseService.class.getResource("/xml_mapping_site.xml").toString());
99                 map.loadMapping(CastorDatabaseService.class.getResource("/xml_mapping_site.xml").toString());
100             }
101             
102             // All ODMG database access requires a transaction
103
db.begin();
104
105             Repository repository = RepositoryController.getController().getRepositoryWithId(this.repositoryId, db);
106             SiteNode siteNode = SiteNodeController.getController().getRootSiteNode(this.repositoryId, db);
107             Content content = ContentController.getContentController().getRootContent(this.repositoryId, db);
108             List JavaDoc contentTypeDefinitions = ContentTypeDefinitionController.getController().getContentTypeDefinitionList(db);
109             
110             InfoGlueExportImpl infoGlueExportImpl = new InfoGlueExportImpl();
111             
112             VisualFormatter visualFormatter = new VisualFormatter();
113             String JavaDoc fileName = "Export_" + repository.getName() + "_" + visualFormatter.formatDate(new Date JavaDoc(), "yyyy-MM-dd") + ".xml";
114             String JavaDoc filePath = CmsPropertyHandler.getDigitalAssetPath();
115             String JavaDoc fileSystemName = filePath + File.separator + fileName;
116                         
117             fileUrl = CmsPropertyHandler.getWebServerAddress() + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + fileName;
118             this.fileName = fileName;
119                         
120             String JavaDoc encoding = "UTF-8";
121             File JavaDoc file = new File JavaDoc(fileSystemName);
122             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(file);
123             OutputStreamWriter JavaDoc osw = new OutputStreamWriter JavaDoc(fos, encoding);
124             Marshaller marshaller = new Marshaller(osw);
125             marshaller.setMapping(map);
126             marshaller.setEncoding(encoding);
127             
128             infoGlueExportImpl.setRootContent((ContentImpl)content);
129             infoGlueExportImpl.setRootSiteNode((SiteNodeImpl)siteNode);
130             infoGlueExportImpl.setContentTypeDefinitions(contentTypeDefinitions);
131             
132             marshaller.marshal(infoGlueExportImpl);
133             
134             osw.flush();
135             osw.close();
136             
137             //fos.flush();
138
//fos.close();
139

140             db.commit();
141             db.close();
142
143         }
144         catch (Exception JavaDoc e)
145         {
146             logger.error("An error was found exporting a repository: " + e.getMessage(), e);
147             db.rollback();
148         }
149         
150         return "success";
151     }
152
153
154     public void setRepositoryId(Integer JavaDoc repositoryId)
155     {
156         this.repositoryId = repositoryId;
157     }
158
159     public String JavaDoc getFileName()
160     {
161         return fileName;
162     }
163
164     public String JavaDoc getFileUrl()
165     {
166         return fileUrl;
167     }
168
169     public Integer JavaDoc getRepositoryId()
170     {
171         return repositoryId;
172     }
173
174 }
175
Popular Tags