1 17 package org.alfresco.repo.action.executer; 18 19 import java.util.List ; 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 45 public class TransformActionExecuter extends ActionExecuterAbstractBase 46 { 47 50 private static Log logger = LogFactory.getLog(TransformActionExecuter.class); 51 52 55 public static final String NAME = "transform"; 56 public static final String PARAM_MIME_TYPE = "mime-type"; 57 public static final String PARAM_DESTINATION_FOLDER = "destination-folder"; 58 public static final String PARAM_ASSOC_TYPE_QNAME = "assoc-type"; 59 public static final String 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 72 public void setMimetypeService(MimetypeService mimetypeService) 73 { 74 this.mimetypeService = mimetypeService; 75 } 76 77 82 public void setNodeService(NodeService nodeService) 83 { 84 this.nodeService = nodeService; 85 } 86 87 92 public void setDictionaryService(DictionaryService dictionaryService) 93 { 94 this.dictionaryService = dictionaryService; 95 } 96 97 102 public void setContentService(ContentService contentService) 103 { 104 this.contentService = contentService; 105 } 106 107 112 public void setCopyService(CopyService copyService) 113 { 114 this.copyService = copyService; 115 } 116 117 120 @Override 121 protected void addParameterDefintions(List <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 132 @Override 133 protected void executeImpl( 134 Action ruleAction, 135 NodeRef actionedUponNodeRef) 136 { 137 if (this.nodeService.exists(actionedUponNodeRef) == false) 138 { 139 return; 141 } 142 QName typeQName = this.nodeService.getType(actionedUponNodeRef); 144 if (this.dictionaryService.isSubClass(typeQName, ContentModel.TYPE_CONTENT) == false) 145 { 146 return; 148 } 149 String mimeType = (String )ruleAction.getParameterValue(PARAM_MIME_TYPE); 151 152 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 NodeRef copyNodeRef = this.copyService.copy( 159 actionedUponNodeRef, 160 destinationParent, 161 destinationAssocTypeQName, 162 destinationAssocQName, 163 false); 164 165 166 ContentReader contentReader = this.contentService.getReader(actionedUponNodeRef, ContentModel.PROP_CONTENT); 168 if (contentReader == null) 169 { 170 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 originalMimetype = contentReader.getMimetype(); 176 177 ContentWriter contentWriter = this.contentService.getWriter(copyNodeRef, ContentModel.PROP_CONTENT, true); 179 contentWriter.setMimetype(mimeType); contentWriter.setEncoding(contentReader.getEncoding()); 182 String originalName = (String )nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_NAME); 184 String newName = transformName(originalName, originalMimetype, mimeType); 185 nodeService.setProperty(copyNodeRef, ContentModel.PROP_NAME, newName); 186 String originalTitle = (String )nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_TITLE); 187 if (originalTitle != null && originalTitle.length() > 0) 188 { 189 String newTitle = transformName(originalTitle, originalMimetype, mimeType); 190 nodeService.setProperty(copyNodeRef, ContentModel.PROP_TITLE, newTitle); 191 } 192 193 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 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 225 private String transformName(String original, String originalMimetype, String newMimetype) 226 { 227 int dotIndex = original.lastIndexOf('.'); 229 StringBuilder sb = new StringBuilder (original.length()); 230 if (dotIndex > -1) 231 { 232 sb.append(original.substring(0, dotIndex)); 234 } 235 else 236 { 237 sb.append(original); 239 } 240 String newExtension = mimetypeService.getExtension(newMimetype); 242 sb.append('.').append(newExtension); 243 return sb.toString(); 245 } 246 247 } 248 | Popular Tags |