1 17 package org.alfresco.repo.action.executer; 18 19 import java.io.File ; 20 import java.io.FileNotFoundException ; 21 import java.io.FileOutputStream ; 22 import java.io.Serializable ; 23 import java.text.MessageFormat ; 24 import java.util.HashMap ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 import org.alfresco.i18n.I18NUtil; 29 import org.alfresco.model.ContentModel; 30 import org.alfresco.repo.action.ParameterDefinitionImpl; 31 import org.alfresco.repo.content.MimetypeMap; 32 import org.alfresco.repo.exporter.ACPExportPackageHandler; 33 import org.alfresco.service.cmr.action.Action; 34 import org.alfresco.service.cmr.action.ActionServiceException; 35 import org.alfresco.service.cmr.action.ParameterDefinition; 36 import org.alfresco.service.cmr.dictionary.DataTypeDefinition; 37 import org.alfresco.service.cmr.repository.ChildAssociationRef; 38 import org.alfresco.service.cmr.repository.ContentService; 39 import org.alfresco.service.cmr.repository.ContentWriter; 40 import org.alfresco.service.cmr.repository.MimetypeService; 41 import org.alfresco.service.cmr.repository.NodeRef; 42 import org.alfresco.service.cmr.repository.NodeService; 43 import org.alfresco.service.cmr.repository.StoreRef; 44 import org.alfresco.service.cmr.view.ExporterCrawlerParameters; 45 import org.alfresco.service.cmr.view.ExporterService; 46 import org.alfresco.service.cmr.view.Location; 47 import org.alfresco.service.namespace.NamespaceService; 48 import org.alfresco.service.namespace.QName; 49 import org.alfresco.util.TempFileProvider; 50 51 56 public class ExporterActionExecuter extends ActionExecuterAbstractBase 57 { 58 public static final String NAME = "export"; 59 public static final String PARAM_STORE = "store"; 60 public static final String PARAM_PACKAGE_NAME = "package-name"; 61 public static final String PARAM_DESTINATION_FOLDER = "destination"; 62 public static final String PARAM_INCLUDE_CHILDREN = "include-children"; 63 public static final String PARAM_INCLUDE_SELF = "include-self"; 64 public static final String PARAM_ENCODING = "encoding"; 65 66 private static final String TEMP_FILE_PREFIX = "alf"; 67 68 71 private ExporterService exporterService; 72 73 76 private MimetypeService mimetypeService; 77 78 81 private NodeService nodeService; 82 83 86 private ContentService contentService; 87 88 93 public void setExporterService(ExporterService exporterService) 94 { 95 this.exporterService = exporterService; 96 } 97 98 103 public void setMimetypeService(MimetypeService mimetypeService) 104 { 105 this.mimetypeService = mimetypeService; 106 } 107 108 113 public void setNodeService(NodeService nodeService) 114 { 115 this.nodeService = nodeService; 116 } 117 118 123 public void setContentService(ContentService contentService) 124 { 125 this.contentService = contentService; 126 } 127 128 131 public void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef) 132 { 133 File zipFile = null; 134 try 135 { 136 String packageName = (String )ruleAction.getParameterValue(PARAM_PACKAGE_NAME); 137 File dataFile = new File (packageName); 138 File contentDir = new File (packageName); 139 140 zipFile = TempFileProvider.createTempFile(TEMP_FILE_PREFIX, ACPExportPackageHandler.ACP_EXTENSION); 142 ACPExportPackageHandler zipHandler = new ACPExportPackageHandler(new FileOutputStream (zipFile), 143 dataFile, contentDir, mimetypeService); 144 145 ExporterCrawlerParameters params = new ExporterCrawlerParameters(); 146 boolean includeChildren = true; 147 Boolean withKids = (Boolean )ruleAction.getParameterValue(PARAM_INCLUDE_CHILDREN); 148 if (withKids != null) 149 { 150 includeChildren = withKids.booleanValue(); 151 } 152 params.setCrawlChildNodes(includeChildren); 153 154 boolean includeSelf = false; 155 Boolean andMe = (Boolean )ruleAction.getParameterValue(PARAM_INCLUDE_SELF); 156 if (andMe != null) 157 { 158 includeSelf = andMe.booleanValue(); 159 } 160 params.setCrawlSelf(includeSelf); 161 162 params.setExportFrom(new Location(actionedUponNodeRef)); 163 164 this.exporterService.exportView(zipHandler, params, null); 166 167 NodeRef zip = createExportZip(ruleAction, actionedUponNodeRef); 170 ContentWriter writer = this.contentService.getWriter(zip, ContentModel.PROP_CONTENT, true); 171 writer.setEncoding((String )ruleAction.getParameterValue(PARAM_ENCODING)); 172 writer.setMimetype(MimetypeMap.MIMETYPE_ACP); 173 writer.putContent(zipFile); 174 } 175 catch (FileNotFoundException fnfe) 176 { 177 throw new ActionServiceException("export.package.error", fnfe); 178 } 179 finally 180 { 181 if (zipFile != null) 183 { 184 zipFile.delete(); 185 } 186 } 187 } 188 189 192 protected void addParameterDefintions(List <ParameterDefinition> paramList) 193 { 194 paramList.add(new ParameterDefinitionImpl(PARAM_PACKAGE_NAME, DataTypeDefinition.TEXT, true, 195 getParamDisplayLabel(PARAM_PACKAGE_NAME))); 196 paramList.add(new ParameterDefinitionImpl(PARAM_ENCODING, DataTypeDefinition.TEXT, true, 197 getParamDisplayLabel(PARAM_ENCODING))); 198 paramList.add(new ParameterDefinitionImpl(PARAM_STORE, DataTypeDefinition.TEXT, true, 199 getParamDisplayLabel(PARAM_STORE))); 200 paramList.add(new ParameterDefinitionImpl(PARAM_DESTINATION_FOLDER, DataTypeDefinition.NODE_REF, true, 201 getParamDisplayLabel(PARAM_DESTINATION_FOLDER))); 202 paramList.add(new ParameterDefinitionImpl(PARAM_INCLUDE_CHILDREN, DataTypeDefinition.BOOLEAN, false, 203 getParamDisplayLabel(PARAM_INCLUDE_CHILDREN))); 204 paramList.add(new ParameterDefinitionImpl(PARAM_INCLUDE_SELF, DataTypeDefinition.BOOLEAN, false, 205 getParamDisplayLabel(PARAM_INCLUDE_SELF))); 206 } 207 208 214 private NodeRef createExportZip(Action ruleAction, NodeRef actionedUponNodeRef) 215 { 216 NodeRef exportDest = (NodeRef)ruleAction.getParameterValue(PARAM_DESTINATION_FOLDER); 218 String packageName = (String )ruleAction.getParameterValue(PARAM_PACKAGE_NAME); 219 220 if (packageName.indexOf(".") == -1) 222 { 223 packageName = packageName + "." + ACPExportPackageHandler.ACP_EXTENSION; 224 } 225 226 Map <QName, Serializable > contentProps = new HashMap <QName, Serializable >(1); 228 contentProps.put(ContentModel.PROP_NAME, packageName); 229 230 String assocName = QName.createValidLocalName(packageName); 232 ChildAssociationRef assocRef = this.nodeService.createNode( 233 exportDest, ContentModel.ASSOC_CONTAINS, 234 QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, assocName), 235 ContentModel.TYPE_CONTENT, contentProps); 236 237 NodeRef zipNodeRef = assocRef.getChildRef(); 238 239 String desc = ""; 241 String storeRef = (String )ruleAction.getParameterValue(PARAM_STORE); 242 NodeRef rootNode = this.nodeService.getRootNode(new StoreRef(storeRef)); 243 if (rootNode.equals(actionedUponNodeRef)) 244 { 245 desc = I18NUtil.getMessage("export.root.package.description"); 246 } 247 else 248 { 249 String spaceName = (String )this.nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_NAME); 250 String pattern = I18NUtil.getMessage("export.package.description"); 251 if (pattern != null && spaceName != null) 252 { 253 desc = MessageFormat.format(pattern, spaceName); 254 } 255 } 256 257 Map <QName, Serializable > titledProps = new HashMap <QName, Serializable >(3, 1.0f); 259 titledProps.put(ContentModel.PROP_TITLE, packageName); 260 titledProps.put(ContentModel.PROP_DESCRIPTION, desc); 261 this.nodeService.addAspect(zipNodeRef, ContentModel.ASPECT_TITLED, titledProps); 262 263 return zipNodeRef; 264 } 265 } 266 | Popular Tags |