1 23 24 package org.infoglue.cms.applications.managementtool.actions; 25 26 import java.io.File ; 27 import java.io.FileOutputStream ; 28 import java.io.OutputStreamWriter ; 29 import java.util.Date ; 30 import java.util.List ; 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 57 58 public class ExportRepositoryAction extends InfoGlueAbstractAction 59 { 60 private final static Logger logger = Logger.getLogger(ExportRepositoryAction.class.getName()); 61 62 private Integer repositoryId = null; 63 64 private String fileUrl = ""; 65 private String fileName = ""; 66 67 72 73 public String doInput() throws Exception 74 { 75 return "input"; 76 } 77 78 81 82 protected String doExecute() throws Exception 83 { 84 Database db = CastorDatabaseService.getDatabase(); 85 86 try 87 { 88 Mapping map = new Mapping(); 89 String 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 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 contentTypeDefinitions = ContentTypeDefinitionController.getController().getContentTypeDefinitionList(db); 109 110 InfoGlueExportImpl infoGlueExportImpl = new InfoGlueExportImpl(); 111 112 VisualFormatter visualFormatter = new VisualFormatter(); 113 String fileName = "Export_" + repository.getName() + "_" + visualFormatter.formatDate(new Date (), "yyyy-MM-dd") + ".xml"; 114 String filePath = CmsPropertyHandler.getDigitalAssetPath(); 115 String fileSystemName = filePath + File.separator + fileName; 116 117 fileUrl = CmsPropertyHandler.getWebServerAddress() + "/" + CmsPropertyHandler.getDigitalAssetBaseUrl() + "/" + fileName; 118 this.fileName = fileName; 119 120 String encoding = "UTF-8"; 121 File file = new File (fileSystemName); 122 FileOutputStream fos = new FileOutputStream (file); 123 OutputStreamWriter osw = new OutputStreamWriter (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 140 db.commit(); 141 db.close(); 142 143 } 144 catch (Exception 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 repositoryId) 155 { 156 this.repositoryId = repositoryId; 157 } 158 159 public String getFileName() 160 { 161 return fileName; 162 } 163 164 public String getFileUrl() 165 { 166 return fileUrl; 167 } 168 169 public Integer getRepositoryId() 170 { 171 return repositoryId; 172 } 173 174 } 175 | Popular Tags |