1 17 package org.alfresco.repo.action.evaluator; 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.action.ParameterDefinitionImpl; 26 import org.alfresco.repo.action.evaluator.compare.ComparePropertyValueOperation; 27 import org.alfresco.repo.action.evaluator.compare.ContentPropertyName; 28 import org.alfresco.repo.action.evaluator.compare.PropertyValueComparator; 29 import org.alfresco.service.cmr.action.ActionCondition; 30 import org.alfresco.service.cmr.action.ActionServiceException; 31 import org.alfresco.service.cmr.action.ParameterDefinition; 32 import org.alfresco.service.cmr.dictionary.DataTypeDefinition; 33 import org.alfresco.service.cmr.dictionary.DictionaryService; 34 import org.alfresco.service.cmr.dictionary.PropertyDefinition; 35 import org.alfresco.service.cmr.repository.ContentData; 36 import org.alfresco.service.cmr.repository.ContentReader; 37 import org.alfresco.service.cmr.repository.ContentService; 38 import org.alfresco.service.cmr.repository.NodeRef; 39 import org.alfresco.service.cmr.repository.NodeService; 40 import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter; 41 import org.alfresco.service.namespace.QName; 42 43 48 public class ComparePropertyValueEvaluator extends ActionConditionEvaluatorAbstractBase 49 { 50 53 public final static String NAME = "compare-property-value"; 54 public final static String PARAM_PROPERTY = "property"; 55 public final static String PARAM_CONTENT_PROPERTY = "content-property"; 56 public final static String PARAM_VALUE = "value"; 57 public final static String PARAM_OPERATION = "operation"; 58 59 62 private final static QName DEFAULT_PROPERTY = ContentModel.PROP_NAME; 63 64 67 private static final String MSGID_INVALID_OPERATION = "compare_property_value_evaluator.invalid_operation"; 68 private static final String MSGID_NO_CONTENT_PROPERTY = "compare_property_value_evaluator.no_content_property"; 69 70 73 private Map <QName, PropertyValueComparator> comparators = new HashMap <QName, PropertyValueComparator>(); 74 75 78 protected NodeService nodeService; 79 80 83 protected ContentService contentService; 84 85 88 protected DictionaryService dictionaryService; 89 90 95 public void setNodeService(NodeService nodeService) 96 { 97 this.nodeService = nodeService; 98 } 99 100 105 public void setContentService(ContentService contentService) 106 { 107 this.contentService = contentService; 108 } 109 110 115 public void setDictionaryService(DictionaryService dictionaryService) 116 { 117 this.dictionaryService = dictionaryService; 118 } 119 120 125 public void setPropertyValueComparators(List <PropertyValueComparator> comparators) 126 { 127 for (PropertyValueComparator comparator : comparators) 128 { 129 comparator.registerComparator(this); 130 } 131 } 132 133 139 public void registerComparator(QName dataType, PropertyValueComparator comparator) 140 { 141 this.comparators.put(dataType, comparator); 142 } 143 144 147 @Override 148 protected void addParameterDefintions(List <ParameterDefinition> paramList) 149 { 150 paramList.add(new ParameterDefinitionImpl(PARAM_PROPERTY, DataTypeDefinition.QNAME, false, getParamDisplayLabel(PARAM_PROPERTY))); 151 paramList.add(new ParameterDefinitionImpl(PARAM_CONTENT_PROPERTY, DataTypeDefinition.TEXT, false, getParamDisplayLabel(PARAM_CONTENT_PROPERTY))); 152 paramList.add(new ParameterDefinitionImpl(PARAM_VALUE, DataTypeDefinition.ANY, true, getParamDisplayLabel(PARAM_VALUE))); 153 paramList.add(new ParameterDefinitionImpl(PARAM_OPERATION, DataTypeDefinition.TEXT, false, getParamDisplayLabel(PARAM_OPERATION))); 154 } 155 156 159 public boolean evaluateImpl( 160 ActionCondition ruleCondition, 161 NodeRef actionedUponNodeRef) 162 { 163 boolean result = false; 164 165 if (this.nodeService.exists(actionedUponNodeRef) == true) 166 { 167 QName propertyQName = (QName)ruleCondition.getParameterValue(PARAM_PROPERTY); 169 if (propertyQName == null) 170 { 171 propertyQName = DEFAULT_PROPERTY; 172 } 173 174 Serializable propertyValue = this.nodeService.getProperty(actionedUponNodeRef, propertyQName); 176 Serializable compareValue = ruleCondition.getParameterValue(PARAM_VALUE); 177 178 ComparePropertyValueOperation operation = null; 180 String operationString = (String )ruleCondition.getParameterValue(PARAM_OPERATION); 181 if (operationString != null) 182 { 183 operation = ComparePropertyValueOperation.valueOf(operationString); 184 } 185 186 QName propertyTypeQName = DataTypeDefinition.ANY; 188 PropertyDefinition propertyDefintion = this.dictionaryService.getProperty(propertyQName); 189 if (propertyDefintion != null) 190 { 191 propertyTypeQName = propertyDefintion.getDataType().getName(); 192 } 193 194 if (DataTypeDefinition.CONTENT.equals(propertyTypeQName) == true) 196 { 197 ContentPropertyName contentProperty = null; 199 String contentPropertyString = (String )ruleCondition.getParameterValue(PARAM_CONTENT_PROPERTY); 200 if (contentPropertyString == null) 201 { 202 throw new ActionServiceException(MSGID_NO_CONTENT_PROPERTY); 204 } 205 else 206 { 207 contentProperty = ContentPropertyName.valueOf(contentPropertyString); 208 } 209 210 if (propertyValue != null) 212 { 213 ContentData contentData = DefaultTypeConverter.INSTANCE.convert(ContentData.class, propertyValue); 214 switch (contentProperty) 215 { 216 case ENCODING: 217 { 218 propertyTypeQName = DataTypeDefinition.TEXT; 219 propertyValue = contentData.getEncoding(); 220 break; 221 } 222 case SIZE: 223 { 224 propertyTypeQName = DataTypeDefinition.LONG; 225 propertyValue = contentData.getSize(); 226 break; 227 } 228 case MIME_TYPE: 229 { 230 propertyTypeQName = DataTypeDefinition.TEXT; 231 propertyValue = contentData.getMimetype(); 232 break; 233 } 234 } 235 } 236 } 237 238 if (propertyValue != null) 239 { 240 PropertyValueComparator comparator = this.comparators.get(propertyTypeQName); 242 if (comparator != null) 243 { 244 result = comparator.compare(propertyValue, compareValue, operation); 246 } 247 else 248 { 249 if (operation != null && operation != ComparePropertyValueOperation.EQUALS) 251 { 252 throw new ActionServiceException( 254 MSGID_INVALID_OPERATION, 255 new Object []{operation.toString(), propertyTypeQName.toString()}); 256 } 257 258 result = compareValue.equals(propertyValue); 260 } 261 } 262 } 263 264 return result; 265 } 266 } 267 | Popular Tags |