KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > action > executer > LinkCategoryActionExecuter


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.executer;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.alfresco.model.ContentModel;
26 import org.alfresco.repo.action.ParameterDefinitionImpl;
27 import org.alfresco.service.cmr.action.Action;
28 import org.alfresco.service.cmr.action.ParameterDefinition;
29 import org.alfresco.service.cmr.dictionary.DictionaryService;
30 import org.alfresco.service.cmr.dictionary.PropertyDefinition;
31 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
32 import org.alfresco.service.cmr.repository.NodeRef;
33 import org.alfresco.service.cmr.repository.NodeService;
34 import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
35 import org.alfresco.service.namespace.QName;
36
37 /**
38  * Link category action executor
39  *
40  * @author Roy Wetherall
41  */

42 public class LinkCategoryActionExecuter extends ActionExecuterAbstractBase
43 {
44     /**
45      * Rule constants
46      */

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

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

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

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

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

84     @Override JavaDoc
85     protected void addParameterDefintions(List JavaDoc<ParameterDefinition> paramList)
86     {
87         paramList.add(new ParameterDefinitionImpl(PARAM_CATEGORY_ASPECT, DataTypeDefinition.QNAME, true, getParamDisplayLabel(PARAM_CATEGORY_ASPECT)));
88         paramList.add(new ParameterDefinitionImpl(PARAM_CATEGORY_VALUE, DataTypeDefinition.NODE_REF, true, getParamDisplayLabel(PARAM_CATEGORY_VALUE)));
89     }
90     
91     /**
92      * Execute action implementation
93      */

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