KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > action > ParameterizedItemDefinitionImpl


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;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.alfresco.i18n.I18NUtil;
27 import org.alfresco.service.cmr.action.ParameterDefinition;
28 import org.alfresco.service.cmr.action.ParameterizedItemDefinition;
29 import org.alfresco.service.cmr.rule.RuleServiceException;
30
31 /**
32  * Rule item implementation class
33  *
34  * @author Roy Wetherall
35  */

36 public abstract class ParameterizedItemDefinitionImpl implements ParameterizedItemDefinition, Serializable JavaDoc
37 {
38     /**
39      * The name of the rule item
40      */

41     private String JavaDoc name;
42     
43     /**
44      * The title I18N key
45      */

46     private String JavaDoc titleKey;
47     
48     /**
49      * The description I18N key
50      */

51     private String JavaDoc descriptionKey;
52         
53     /**
54      * Indicates whether adHocProperties are allowed
55      */

56     private boolean adhocPropertiesAllowed = false;
57     
58     /**
59      * The list of parameters associated with the rule item
60      */

61     private List JavaDoc<ParameterDefinition> parameterDefinitions = new ArrayList JavaDoc<ParameterDefinition>();
62     
63     /**
64      * A map of the parameter definitions by name
65      */

66     private Map JavaDoc<String JavaDoc, ParameterDefinition> paramDefinitionsByName;
67
68     /**
69      * Error messages
70      */

71     private static final String JavaDoc ERR_NAME_DUPLICATION = "The names " +
72             "given to parameter definitions must be unique within the " +
73             "scope of the rule item definition.";
74
75     /**
76      * Constructor
77      *
78      * @param name the name
79      */

80     public ParameterizedItemDefinitionImpl(String JavaDoc name)
81     {
82         this.name = name;
83     }
84
85     /**
86      * @see org.alfresco.service.cmr.action.ParameterizedItemDefinition#getName()
87      */

88     public String JavaDoc getName()
89     {
90         return this.name;
91     }
92
93     /**
94      * Set the title of the rule item
95      *
96      * @param title the title
97      */

98     public void setTitleKey(String JavaDoc title)
99     {
100         this.titleKey = title;
101     }
102     
103     /**
104      * @see org.alfresco.service.cmr.action.ParameterizedItemDefinition#getTitle()
105      */

106     public String JavaDoc getTitle()
107     {
108         return I18NUtil.getMessage(this.titleKey);
109     }
110
111     /**
112      * Set the description I18N key
113      *
114      * @param descriptionKey the description key
115      */

116     public void setDescriptionKey(String JavaDoc descriptionKey)
117     {
118         this.descriptionKey = descriptionKey;
119     }
120     
121     /**
122      * @see org.alfresco.service.cmr.action.ParameterizedItemDefinition#getDescription()
123      */

124     public String JavaDoc getDescription()
125     {
126         return I18NUtil.getMessage(this.descriptionKey);
127     }
128
129     /**
130      * @see org.alfresco.service.cmr.action.ParameterizedItemDefinition#getAdhocPropertiesAllowed()
131      */

132     public boolean getAdhocPropertiesAllowed()
133     {
134         return this.adhocPropertiesAllowed;
135     }
136     
137     /**
138      * Set whether adhoc properties are allowed
139      *
140      * @param adhocPropertiesAllowed true is adhoc properties are allowed, false otherwise
141      */

142     public void setAdhocPropertiesAllowed(boolean adhocPropertiesAllowed)
143     {
144         this.adhocPropertiesAllowed = adhocPropertiesAllowed;
145     }
146     
147     /**
148      * Set the parameter definitions for the rule item
149      *
150      * @param parameterDefinitions the parameter definitions
151      */

152     public void setParameterDefinitions(
153             List JavaDoc<ParameterDefinition> parameterDefinitions)
154     {
155         if (hasDuplicateNames(parameterDefinitions) == true)
156         {
157             throw new RuleServiceException(ERR_NAME_DUPLICATION);
158         }
159         
160         this.parameterDefinitions = parameterDefinitions;
161         
162         // Create a map of the definitions to use for subsequent calls
163
this.paramDefinitionsByName = new HashMap JavaDoc<String JavaDoc, ParameterDefinition>(this.parameterDefinitions.size());
164         for (ParameterDefinition definition : this.parameterDefinitions)
165         {
166             this.paramDefinitionsByName.put(definition.getName(), definition);
167         }
168     }
169     
170     /**
171      * Determines whether the list of parameter defintions contains duplicate
172      * names of not.
173      *
174      * @param parameterDefinitions a list of parmeter definitions
175      * @return true if there are name duplications, false
176      * otherwise
177      */

178     private boolean hasDuplicateNames(List JavaDoc<ParameterDefinition> parameterDefinitions)
179     {
180         boolean result = false;
181         if (parameterDefinitions != null)
182         {
183             HashSet JavaDoc<String JavaDoc> temp = new HashSet JavaDoc<String JavaDoc>(parameterDefinitions.size());
184             for (ParameterDefinition definition : parameterDefinitions)
185             {
186                 temp.add(definition.getName());
187             }
188             result = (parameterDefinitions.size() != temp.size());
189         }
190         return result;
191     }
192     
193     /**
194      * @see org.alfresco.service.cmr.action.ParameterizedItemDefinition#getParameterDefinitions()
195      */

196     public List JavaDoc<ParameterDefinition> getParameterDefinitions()
197     {
198         return this.parameterDefinitions;
199     }
200     
201     /**
202      * @see org.alfresco.service.cmr.action.ParameterizedItemDefinition#getParameterDefintion(java.lang.String)
203      */

204     public ParameterDefinition getParameterDefintion(String JavaDoc name)
205     {
206         ParameterDefinition result = null;
207         if (paramDefinitionsByName != null)
208         {
209             result = this.paramDefinitionsByName.get(name);
210         }
211         return result;
212     }
213 }
214
Popular Tags