KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > action > executer > ExporterActionExecuter


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.action.executer;
18
19 import java.io.File JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.Serializable JavaDoc;
23 import java.text.MessageFormat JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
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 /**
52  * Exporter action executor
53  *
54  * @author gavinc
55  */

56 public class ExporterActionExecuter extends ActionExecuterAbstractBase
57 {
58     public static final String JavaDoc NAME = "export";
59     public static final String JavaDoc PARAM_STORE = "store";
60     public static final String JavaDoc PARAM_PACKAGE_NAME = "package-name";
61     public static final String JavaDoc PARAM_DESTINATION_FOLDER = "destination";
62     public static final String JavaDoc PARAM_INCLUDE_CHILDREN = "include-children";
63     public static final String JavaDoc PARAM_INCLUDE_SELF = "include-self";
64     public static final String JavaDoc PARAM_ENCODING = "encoding";
65
66     private static final String JavaDoc TEMP_FILE_PREFIX = "alf";
67     
68     /**
69      * The exporter service
70      */

71     private ExporterService exporterService;
72     
73     /**
74      * The Mime type service
75      */

76     private MimetypeService mimetypeService;
77
78     /**
79      * The node service
80      */

81     private NodeService nodeService;
82     
83     /**
84      * The content service
85      */

86     private ContentService contentService;
87     
88     /**
89      * Sets the ExporterService to use
90      *
91      * @param exporterService The ExporterService
92      */

93     public void setExporterService(ExporterService exporterService)
94     {
95         this.exporterService = exporterService;
96     }
97     
98     /**
99      * Sets the MimetypeService to use
100      *
101      * @param mimetypeService
102      */

103     public void setMimetypeService(MimetypeService mimetypeService)
104     {
105         this.mimetypeService = mimetypeService;
106     }
107     
108     /**
109      * Sets the NodeService to use
110      *
111      * @param nodeService The NodeService
112      */

113     public void setNodeService(NodeService nodeService)
114     {
115        this.nodeService = nodeService;
116     }
117     
118     /**
119      * Sets the ContentService to use
120      *
121      * @param contentService The ContentService
122      */

123     public void setContentService(ContentService contentService)
124     {
125        this.contentService = contentService;
126     }
127
128     /**
129      * @see org.alfresco.repo.action.executer.ActionExecuter#execute(org.alfresco.repo.ref.NodeRef, org.alfresco.repo.ref.NodeRef)
130      */

131     public void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef)
132     {
133         File JavaDoc zipFile = null;
134         try
135         {
136             String JavaDoc packageName = (String JavaDoc)ruleAction.getParameterValue(PARAM_PACKAGE_NAME);
137             File JavaDoc dataFile = new File JavaDoc(packageName);
138             File JavaDoc contentDir = new File JavaDoc(packageName);
139            
140             // create a temporary file to hold the zip
141
zipFile = TempFileProvider.createTempFile(TEMP_FILE_PREFIX, ACPExportPackageHandler.ACP_EXTENSION);
142             ACPExportPackageHandler zipHandler = new ACPExportPackageHandler(new FileOutputStream JavaDoc(zipFile),
143                  dataFile, contentDir, mimetypeService);
144            
145             ExporterCrawlerParameters params = new ExporterCrawlerParameters();
146             boolean includeChildren = true;
147             Boolean JavaDoc withKids = (Boolean JavaDoc)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 JavaDoc andMe = (Boolean JavaDoc)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             // perform the actual export
165
this.exporterService.exportView(zipHandler, params, null);
166            
167             // now the export is done we need to create a node in the repository
168
// to hold the exported package
169
NodeRef zip = createExportZip(ruleAction, actionedUponNodeRef);
170             ContentWriter writer = this.contentService.getWriter(zip, ContentModel.PROP_CONTENT, true);
171             writer.setEncoding((String JavaDoc)ruleAction.getParameterValue(PARAM_ENCODING));
172             writer.setMimetype(MimetypeMap.MIMETYPE_ACP);
173             writer.putContent(zipFile);
174         }
175         catch (FileNotFoundException JavaDoc fnfe)
176         {
177             throw new ActionServiceException("export.package.error", fnfe);
178         }
179         finally
180         {
181            // try and delete the temporary file
182
if (zipFile != null)
183            {
184               zipFile.delete();
185            }
186         }
187     }
188
189     /**
190      * @see org.alfresco.repo.action.ParameterizedItemAbstractBase#addParameterDefintions(java.util.List)
191      */

192     protected void addParameterDefintions(List JavaDoc<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     /**
209      * Creates the ZIP file node in the repository for the export
210      *
211      * @param ruleAction The rule being executed
212      * @return The NodeRef of the newly created ZIP file
213      */

214     private NodeRef createExportZip(Action ruleAction, NodeRef actionedUponNodeRef)
215     {
216         // create a node in the repository to represent the export package
217
NodeRef exportDest = (NodeRef)ruleAction.getParameterValue(PARAM_DESTINATION_FOLDER);
218         String JavaDoc packageName = (String JavaDoc)ruleAction.getParameterValue(PARAM_PACKAGE_NAME);
219
220         // add the default Alfresco content package extension if an extension hasn't been given
221
if (packageName.indexOf(".") == -1)
222         {
223             packageName = packageName + "." + ACPExportPackageHandler.ACP_EXTENSION;
224         }
225         
226         // set the name for the new node
227
Map JavaDoc<QName, Serializable JavaDoc> contentProps = new HashMap JavaDoc<QName, Serializable JavaDoc>(1);
228         contentProps.put(ContentModel.PROP_NAME, packageName);
229             
230         // create the node to represent the zip file
231
String JavaDoc 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         // build a description string to be set on the node representing the content package
240
String JavaDoc desc = "";
241         String JavaDoc storeRef = (String JavaDoc)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 JavaDoc spaceName = (String JavaDoc)this.nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_NAME);
250            String JavaDoc pattern = I18NUtil.getMessage("export.package.description");
251            if (pattern != null && spaceName != null)
252            {
253               desc = MessageFormat.format(pattern, spaceName);
254            }
255         }
256         
257         // apply the titled aspect to behave in the web client
258
Map JavaDoc<QName, Serializable JavaDoc> titledProps = new HashMap JavaDoc<QName, Serializable JavaDoc>(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