KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection 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.service.cmr.action.ActionCondition;
27 import org.alfresco.service.cmr.action.ParameterDefinition;
28 import org.alfresco.service.cmr.dictionary.DictionaryService;
29 import org.alfresco.service.cmr.dictionary.PropertyDefinition;
30 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
31 import org.alfresco.service.cmr.repository.NodeRef;
32 import org.alfresco.service.cmr.repository.NodeService;
33 import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
34 import org.alfresco.service.namespace.QName;
35
36 /**
37  * In category evaluator implementation.
38  *
39  * @author Roy Wetherall
40  */

41 public class InCategoryEvaluator extends ActionConditionEvaluatorAbstractBase
42 {
43     /**
44      * Rule constants
45      */

46     public static final String JavaDoc NAME = "in-category";
47     public static final String JavaDoc PARAM_CATEGORY_ASPECT = "category-aspect";
48     public static final String JavaDoc PARAM_CATEGORY_VALUE = "category-value";
49     
50     /**
51      * The node service
52      */

53     private NodeService nodeService;
54     
55     /**
56      * The dictionary service
57      */

58     private DictionaryService dictionaryService;
59     
60     /**
61      * Sets the node service
62      *
63      * @param nodeService the node service
64      */

65     public void setNodeService(NodeService nodeService)
66     {
67         this.nodeService = nodeService;
68     }
69     
70     /**
71      * Sets the dictionary service
72      *
73      * @param dictionaryService the dictionary service
74      */

75     public void setDictionaryService(DictionaryService dictionaryService)
76     {
77         this.dictionaryService = dictionaryService;
78     }
79
80     /**
81      * Add the parameter definitions
82      */

83     @Override JavaDoc
84     protected void addParameterDefintions(List JavaDoc<ParameterDefinition> paramList)
85     {
86         paramList.add(new ParameterDefinitionImpl(PARAM_CATEGORY_ASPECT, DataTypeDefinition.QNAME, true, getParamDisplayLabel(PARAM_CATEGORY_ASPECT)));
87         paramList.add(new ParameterDefinitionImpl(PARAM_CATEGORY_VALUE, DataTypeDefinition.NODE_REF, true, getParamDisplayLabel(PARAM_CATEGORY_VALUE)));
88     }
89     
90     /**
91      * @see org.alfresco.repo.action.evaluator.ActionConditionEvaluatorAbstractBase#evaluateImpl(org.alfresco.service.cmr.repository.NodeRef, org.alfresco.service.cmr.repository.NodeRef)
92      */

93     @Override JavaDoc
94     protected boolean evaluateImpl(
95             ActionCondition ruleCondition,
96             NodeRef actionedUponNodeRef)
97     {
98         boolean result = false;
99
100         // Double check that the node still exists
101
if (this.nodeService.exists(actionedUponNodeRef) == true)
102         {
103             // Get the rule parameter values
104
QName categoryAspect = (QName)ruleCondition.getParameterValue(PARAM_CATEGORY_ASPECT);
105             NodeRef categoryValue = (NodeRef)ruleCondition.getParameterValue(PARAM_CATEGORY_VALUE);
106             
107             // Check that the apect is classifiable and is currently applied to the node
108
if (this.dictionaryService.isSubClass(categoryAspect, ContentModel.ASPECT_CLASSIFIABLE) == true &&
109                 this.nodeService.hasAspect(actionedUponNodeRef, categoryAspect) == true)
110             {
111                 // Get the category property qname
112
QName categoryProperty = null;
113                 Map JavaDoc<QName, PropertyDefinition> propertyDefs = this.dictionaryService.getAspect(categoryAspect).getProperties();
114                 for (Map.Entry JavaDoc<QName, PropertyDefinition> entry : propertyDefs.entrySet())
115                 {
116                     if (DataTypeDefinition.CATEGORY.equals(entry.getValue().getDataType().getName()) == true)
117                     {
118                         // Found the category property
119
categoryProperty = entry.getKey();
120                         break;
121                     }
122                 }
123                 
124                 if (categoryProperty != null)
125                 {
126                     // Check to see if the category value is in the list of currently set category values
127
Serializable JavaDoc value = this.nodeService.getProperty(actionedUponNodeRef, categoryProperty);
128                     if (value != null)
129                     {
130                         Collection JavaDoc<NodeRef> actualCategories = DefaultTypeConverter.INSTANCE.getCollection(NodeRef.class, value);
131                         for (NodeRef nodeRef : actualCategories)
132                         {
133                             if (nodeRef != null && nodeRef.equals(categoryValue) == true)
134                             {
135                                 result = true;
136                                 break;
137                             }
138                         }
139                     }
140                 }
141             }
142             
143         }
144         
145         return result;
146     }
147 }
148
Popular Tags