1 17 package org.alfresco.repo.action.executer; 18 19 import java.io.Serializable ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import org.alfresco.model.ContentModel; 25 import org.alfresco.repo.content.metadata.MetadataExtracter; 26 import org.alfresco.repo.content.metadata.MetadataExtracterRegistry; 27 import org.alfresco.service.cmr.action.Action; 28 import org.alfresco.service.cmr.action.ParameterDefinition; 29 import org.alfresco.service.cmr.dictionary.ClassDefinition; 30 import org.alfresco.service.cmr.dictionary.DictionaryService; 31 import org.alfresco.service.cmr.dictionary.PropertyDefinition; 32 import org.alfresco.service.cmr.repository.ContentReader; 33 import org.alfresco.service.cmr.repository.ContentService; 34 import org.alfresco.service.cmr.repository.NodeRef; 35 import org.alfresco.service.cmr.repository.NodeService; 36 import org.alfresco.service.namespace.QName; 37 38 48 public class ContentMetadataExtracter extends ActionExecuterAbstractBase 49 { 50 53 public static final String NAME = "extract-metadata"; 54 55 74 75 78 private NodeService nodeService; 79 80 85 public void setNodeService(NodeService nodeService) 86 { 87 this.nodeService = nodeService; 88 } 89 90 93 private ContentService contentService; 94 95 98 public void setContentService(ContentService contentService) 99 { 100 this.contentService = contentService; 101 } 102 103 106 private DictionaryService dictionaryService; 107 108 111 public void setDictionaryService(DictionaryService dictService) 112 { 113 this.dictionaryService = dictService; 114 } 115 116 119 private MetadataExtracterRegistry metadataExtracterRegistry; 120 121 124 public void setMetadataExtracterRegistry(MetadataExtracterRegistry metadataExtracterRegistry) 125 { 126 this.metadataExtracterRegistry = metadataExtracterRegistry; 127 } 128 129 133 public void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef) 134 { 135 if (this.nodeService.exists(actionedUponNodeRef) == true) 136 { 137 ContentReader cr = contentService.getReader(actionedUponNodeRef, ContentModel.PROP_CONTENT); 138 139 if (cr != null && cr.getMimetype() != null) 141 { 142 MetadataExtracter me = metadataExtracterRegistry.getExtracter(cr.getMimetype()); 143 if (me != null) 144 { 145 Map <QName, Serializable > newProps = new HashMap <QName, Serializable >(7, 0.5f); 146 me.extract(cr, newProps); 147 148 Map <QName, Serializable > allProps = nodeService.getProperties(actionedUponNodeRef); 149 150 155 156 boolean changed = false; 157 for (QName key : newProps.keySet()) 158 { 159 ClassDefinition propClass = dictionaryService.getProperty(key).getContainerClass(); 161 if (propClass.isAspect() && 162 nodeService.hasAspect(actionedUponNodeRef, propClass.getName()) == false) 163 { 164 Map <QName, Serializable > aspectProps = new HashMap <QName, Serializable >(3, 1.0f); 165 for (QName defKey : propClass.getProperties().keySet()) 166 { 167 if (dictionaryService.getProperty(defKey).isMandatory()) 168 { 169 aspectProps.put(defKey, allProps.get(defKey)); 170 allProps.remove(defKey); 171 } 172 } 173 nodeService.addAspect(actionedUponNodeRef, propClass.getName(), aspectProps); 174 } 175 176 Serializable value = newProps.get(key); 177 if (value == null) 178 { 179 continue; } 181 182 Serializable oldValue = allProps.get(key); 184 if (oldValue == null || oldValue.toString().length() == 0) 185 { 186 allProps.put(key, value); 187 changed = true; 188 } 189 } 190 191 if (changed) 192 { 193 nodeService.setProperties(actionedUponNodeRef, allProps); 194 } 195 } 196 } 197 } 198 } 199 200 @Override 201 protected void addParameterDefintions(List <ParameterDefinition> arg0) 202 { 203 } 205 } | Popular Tags |