KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.List JavaDoc;
20
21 import org.alfresco.error.AlfrescoRuntimeException;
22 import org.alfresco.model.ContentModel;
23 import org.alfresco.repo.action.ParameterDefinitionImpl;
24 import org.alfresco.service.cmr.action.Action;
25 import org.alfresco.service.cmr.action.ParameterDefinition;
26 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
27 import org.alfresco.service.cmr.dictionary.DictionaryService;
28 import org.alfresco.service.cmr.repository.ContentReader;
29 import org.alfresco.service.cmr.repository.ContentService;
30 import org.alfresco.service.cmr.repository.ContentWriter;
31 import org.alfresco.service.cmr.repository.CopyService;
32 import org.alfresco.service.cmr.repository.MimetypeService;
33 import org.alfresco.service.cmr.repository.NoTransformerException;
34 import org.alfresco.service.cmr.repository.NodeRef;
35 import org.alfresco.service.cmr.repository.NodeService;
36 import org.alfresco.service.namespace.QName;
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39
40 /**
41  * Transfor action executer
42  *
43  * @author Roy Wetherall
44  */

45 public class TransformActionExecuter extends ActionExecuterAbstractBase
46 {
47     /**
48      * The logger
49      */

50     private static Log logger = LogFactory.getLog(TransformActionExecuter.class);
51     
52     /**
53      * Action constants
54      */

55     public static final String JavaDoc NAME = "transform";
56     public static final String JavaDoc PARAM_MIME_TYPE = "mime-type";
57     public static final String JavaDoc PARAM_DESTINATION_FOLDER = "destination-folder";
58     public static final String JavaDoc PARAM_ASSOC_TYPE_QNAME = "assoc-type";
59     public static final String JavaDoc PARAM_ASSOC_QNAME = "assoc-name";
60     
61     private DictionaryService dictionaryService;
62     private NodeService nodeService;
63     private ContentService contentService;
64     private CopyService copyService;
65     private MimetypeService mimetypeService;
66     
67     /**
68      * Set the mime type service
69      *
70      * @param mimetypeService the mime type service
71      */

72     public void setMimetypeService(MimetypeService mimetypeService)
73     {
74         this.mimetypeService = mimetypeService;
75     }
76     
77     /**
78      * Set the node service
79      *
80      * @param nodeService set the node service
81      */

82     public void setNodeService(NodeService nodeService)
83     {
84         this.nodeService = nodeService;
85     }
86     
87     /**
88      * Set the dictionary service
89      *
90      * @param dictionaryService the dictionary service
91      */

92     public void setDictionaryService(DictionaryService dictionaryService)
93     {
94         this.dictionaryService = dictionaryService;
95     }
96     
97     /**
98      * Set the content service
99      *
100      * @param contentService the content service
101      */

102     public void setContentService(ContentService contentService)
103     {
104         this.contentService = contentService;
105     }
106     
107     /**
108      * Set the copy service
109      *
110      * @param copyService the copy service
111      */

112     public void setCopyService(CopyService copyService)
113     {
114         this.copyService = copyService;
115     }
116     
117     /**
118      * Add parameter definitions
119      */

120     @Override JavaDoc
121     protected void addParameterDefintions(List JavaDoc<ParameterDefinition> paramList)
122     {
123         paramList.add(new ParameterDefinitionImpl(PARAM_MIME_TYPE, DataTypeDefinition.TEXT, true, getParamDisplayLabel(PARAM_MIME_TYPE)));
124         paramList.add(new ParameterDefinitionImpl(PARAM_DESTINATION_FOLDER, DataTypeDefinition.NODE_REF, true, getParamDisplayLabel(PARAM_DESTINATION_FOLDER)));
125         paramList.add(new ParameterDefinitionImpl(PARAM_ASSOC_TYPE_QNAME, DataTypeDefinition.QNAME, true, getParamDisplayLabel(PARAM_ASSOC_TYPE_QNAME)));
126         paramList.add(new ParameterDefinitionImpl(PARAM_ASSOC_QNAME, DataTypeDefinition.QNAME, true, getParamDisplayLabel(PARAM_ASSOC_QNAME)));
127     }
128
129     /**
130      * @see org.alfresco.repo.action.executer.ActionExecuterAbstractBase#executeImpl(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.cmr.repository.NodeRef)
131      */

132     @Override JavaDoc
133     protected void executeImpl(
134             Action ruleAction,
135             NodeRef actionedUponNodeRef)
136     {
137         if (this.nodeService.exists(actionedUponNodeRef) == false)
138         {
139             // node doesn't exist - can't do anything
140
return;
141         }
142         // First check that the node is a sub-type of content
143
QName typeQName = this.nodeService.getType(actionedUponNodeRef);
144         if (this.dictionaryService.isSubClass(typeQName, ContentModel.TYPE_CONTENT) == false)
145         {
146             // it is not content, so can't transform
147
return;
148         }
149         // Get the mime type
150
String JavaDoc mimeType = (String JavaDoc)ruleAction.getParameterValue(PARAM_MIME_TYPE);
151         
152         // Get the details of the copy destination
153
NodeRef destinationParent = (NodeRef)ruleAction.getParameterValue(PARAM_DESTINATION_FOLDER);
154         QName destinationAssocTypeQName = (QName)ruleAction.getParameterValue(PARAM_ASSOC_TYPE_QNAME);
155         QName destinationAssocQName = (QName)ruleAction.getParameterValue(PARAM_ASSOC_QNAME);
156         
157         // Copy the content node
158
NodeRef copyNodeRef = this.copyService.copy(
159                 actionedUponNodeRef,
160                 destinationParent,
161                 destinationAssocTypeQName,
162                 destinationAssocQName,
163                 false);
164         
165         
166         // Get the content reader
167
ContentReader contentReader = this.contentService.getReader(actionedUponNodeRef, ContentModel.PROP_CONTENT);
168         if (contentReader == null)
169         {
170             // for some reason, this action is premature
171
throw new AlfrescoRuntimeException(
172                     "Attempting to execute content transformation rule " +
173                     "but content has not finished writing, i.e. no URL is available.");
174         }
175         String JavaDoc originalMimetype = contentReader.getMimetype();
176
177         // get the writer and set it up
178
ContentWriter contentWriter = this.contentService.getWriter(copyNodeRef, ContentModel.PROP_CONTENT, true);
179         contentWriter.setMimetype(mimeType); // new mimetype
180
contentWriter.setEncoding(contentReader.getEncoding()); // original encoding
181

182         // Adjust the name of the copy
183
String JavaDoc originalName = (String JavaDoc)nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_NAME);
184         String JavaDoc newName = transformName(originalName, originalMimetype, mimeType);
185         nodeService.setProperty(copyNodeRef, ContentModel.PROP_NAME, newName);
186         String JavaDoc originalTitle = (String JavaDoc)nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_TITLE);
187         if (originalTitle != null && originalTitle.length() > 0)
188         {
189             String JavaDoc newTitle = transformName(originalTitle, originalMimetype, mimeType);
190             nodeService.setProperty(copyNodeRef, ContentModel.PROP_TITLE, newTitle);
191         }
192         
193         // Try and transform the content
194
try
195         {
196             doTransform(ruleAction, contentReader, contentWriter);
197         }
198         catch(NoTransformerException e)
199         {
200             if (logger.isDebugEnabled())
201             {
202                 logger.debug("No transformer found to execute rule: \n" +
203                         " reader: " + contentReader + "\n" +
204                         " writer: " + contentWriter + "\n" +
205                         " action: " + this);
206             }
207             // TODO: Revisit this for alternative solutions
208
nodeService.deleteNode(copyNodeRef);
209         }
210     }
211     
212     protected void doTransform(Action ruleAction, ContentReader contentReader, ContentWriter contentWriter)
213     {
214         this.contentService.transform(contentReader, contentWriter);
215     }
216     
217     /**
218      * Transform name from original extension to new extension
219      *
220      * @param original
221      * @param originalMimetype
222      * @param newMimetype
223      * @return
224      */

225     private String JavaDoc transformName(String JavaDoc original, String JavaDoc originalMimetype, String JavaDoc newMimetype)
226     {
227         // get the current extension
228
int dotIndex = original.lastIndexOf('.');
229         StringBuilder JavaDoc sb = new StringBuilder JavaDoc(original.length());
230         if (dotIndex > -1)
231         {
232             // we found it
233
sb.append(original.substring(0, dotIndex));
234         }
235         else
236         {
237             // no extension
238
sb.append(original);
239         }
240         // add the new extension
241
String JavaDoc newExtension = mimetypeService.getExtension(newMimetype);
242         sb.append('.').append(newExtension);
243         // done
244
return sb.toString();
245     }
246     
247 }
248
Popular Tags