KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > action > evaluator > ComparePropertyValueEvaluator


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.evaluator;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
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 /**
44  * Compare property value evaluator
45  *
46  * @author Roy Wetherall
47  */

48 public class ComparePropertyValueEvaluator extends ActionConditionEvaluatorAbstractBase
49 {
50     /**
51      * Evaluator constants
52      */

53     public final static String JavaDoc NAME = "compare-property-value";
54     public final static String JavaDoc PARAM_PROPERTY = "property";
55     public final static String JavaDoc PARAM_CONTENT_PROPERTY = "content-property";
56     public final static String JavaDoc PARAM_VALUE = "value";
57     public final static String JavaDoc PARAM_OPERATION = "operation";
58     
59     /**
60      * The default property to check if none is specified in the properties
61      */

62     private final static QName DEFAULT_PROPERTY = ContentModel.PROP_NAME;
63     
64     /**
65      * I18N message ID's
66      */

67     private static final String JavaDoc MSGID_INVALID_OPERATION = "compare_property_value_evaluator.invalid_operation";
68     private static final String JavaDoc MSGID_NO_CONTENT_PROPERTY = "compare_property_value_evaluator.no_content_property";
69     
70     /**
71      * Map of comparators used by different property types
72      */

73     private Map JavaDoc<QName, PropertyValueComparator> comparators = new HashMap JavaDoc<QName, PropertyValueComparator>();
74     
75     /**
76      * The node service
77      */

78     protected NodeService nodeService;
79     
80     /**
81      * The content service
82      */

83     protected ContentService contentService;
84     
85     /**
86      * The dictionary service
87      */

88     protected DictionaryService dictionaryService;
89     
90     /**
91      * Set node service
92      *
93      * @param nodeService the node service
94      */

95     public void setNodeService(NodeService nodeService)
96     {
97         this.nodeService = nodeService;
98     }
99     
100     /**
101      * Set the content service
102      *
103      * @param contentService the content service
104      */

105     public void setContentService(ContentService contentService)
106     {
107         this.contentService = contentService;
108     }
109     
110     /**
111      * Set the dictionary service
112      *
113      * @param dictionaryService the dictionary service
114      */

115     public void setDictionaryService(DictionaryService dictionaryService)
116     {
117         this.dictionaryService = dictionaryService;
118     }
119     
120     /**
121      * Set the list of property value comparators
122      *
123      * @param comparators the list of property value comparators
124      */

125     public void setPropertyValueComparators(List JavaDoc<PropertyValueComparator> comparators)
126     {
127         for (PropertyValueComparator comparator : comparators)
128         {
129             comparator.registerComparator(this);
130         }
131     }
132     
133     /**
134      * Registers a comparator for a given property data type.
135      *
136      * @param dataType property data type
137      * @param comparator property value comparator
138      */

139     public void registerComparator(QName dataType, PropertyValueComparator comparator)
140     {
141         this.comparators.put(dataType, comparator);
142     }
143     
144     /**
145      * Add paremeter defintions
146      */

147     @Override JavaDoc
148     protected void addParameterDefintions(List JavaDoc<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     /**
157      * @see ActionConditionEvaluatorAbstractBase#evaluateImpl(ActionCondition, NodeRef)
158      */

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             // Get the name value of the node
168
QName propertyQName = (QName)ruleCondition.getParameterValue(PARAM_PROPERTY);
169             if (propertyQName == null)
170             {
171                 propertyQName = DEFAULT_PROPERTY;
172             }
173             
174             // Get the origional value and the value to match
175
Serializable JavaDoc propertyValue = this.nodeService.getProperty(actionedUponNodeRef, propertyQName);
176             Serializable JavaDoc compareValue = ruleCondition.getParameterValue(PARAM_VALUE);
177             
178             // Get the operation
179
ComparePropertyValueOperation operation = null;
180             String JavaDoc operationString = (String JavaDoc)ruleCondition.getParameterValue(PARAM_OPERATION);
181             if (operationString != null)
182             {
183                 operation = ComparePropertyValueOperation.valueOf(operationString);
184             }
185             
186             // Look at the type of the property (assume to be ANY if none found in dicitionary)
187
QName propertyTypeQName = DataTypeDefinition.ANY;
188             PropertyDefinition propertyDefintion = this.dictionaryService.getProperty(propertyQName);
189             if (propertyDefintion != null)
190             {
191                 propertyTypeQName = propertyDefintion.getDataType().getName();
192             }
193             
194             // Sort out what to do if the property is a content property
195
if (DataTypeDefinition.CONTENT.equals(propertyTypeQName) == true)
196             {
197                 // Get the content property name
198
ContentPropertyName contentProperty = null;
199                 String JavaDoc contentPropertyString = (String JavaDoc)ruleCondition.getParameterValue(PARAM_CONTENT_PROPERTY);
200                 if (contentPropertyString == null)
201                 {
202                     // Error if no content property has been set
203
throw new ActionServiceException(MSGID_NO_CONTENT_PROPERTY);
204                 }
205                 else
206                 {
207                     contentProperty = ContentPropertyName.valueOf(contentPropertyString);
208                 }
209                 
210                 // Get the content data
211
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                 // Try and get a matching comparator
241
PropertyValueComparator comparator = this.comparators.get(propertyTypeQName);
242                 if (comparator != null)
243                 {
244                     // Call the comparator for the property type
245
result = comparator.compare(propertyValue, compareValue, operation);
246                 }
247                 else
248                 {
249                     // The default behaviour is to assume the property can only be compared using equals
250
if (operation != null && operation != ComparePropertyValueOperation.EQUALS)
251                     {
252                         // Error since only the equals operation is valid
253
throw new ActionServiceException(
254                                 MSGID_INVALID_OPERATION,
255                                 new Object JavaDoc[]{operation.toString(), propertyTypeQName.toString()});
256                     }
257                     
258                     // Use equals to compare the values
259
result = compareValue.equals(propertyValue);
260                 }
261             }
262         }
263         
264         return result;
265     }
266 }
267
Popular Tags