KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > workflowtool > function > CategoryErrorPopulator


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23 package org.infoglue.cms.applications.workflowtool.function;
24
25 import java.util.Collection JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import org.infoglue.cms.applications.workflowtool.util.RangeCheck;
30
31 import com.opensymphony.workflow.WorkflowException;
32
33
34 /**
35  *
36  */

37 public class CategoryErrorPopulator extends ErrorPopulator
38 {
39     /**
40      *
41      */

42     private static final String JavaDoc NAME_ARGUMENT = "name";
43     
44     /**
45      *
46      */

47     private static final String JavaDoc CATEGORY_ERROR_PROPERTYSET_PREFIX = ERROR_PROPERTYSET_PREFIX + "category_";
48     
49     /**
50      *
51      */

52     private static final String JavaDoc NON_DEFAULT_NAME_ARGUMENT = "nonDefaultName";
53     
54     /**
55      *
56      */

57     private static final String JavaDoc MIN_ARGUMENT = "min";
58     
59     /**
60      *
61      */

62     private static final String JavaDoc MAX_ARGUMENT = "max";
63     
64     /**
65      *
66      */

67     private static final String JavaDoc EXACTLY_MESSAGE_KEY = "3601";
68
69     /**
70      *
71      */

72     private static final String JavaDoc EXACTLY_ONE_MESSAGE_KEY = "3602";
73     
74     /**
75      *
76      */

77     private static final String JavaDoc LESS_THAN_MESSAGE_KEY = "3603";
78     
79     /**
80      *
81      */

82     private static final String JavaDoc GREATER_THAN_MESSAGE_KEY = "3604";
83
84     /**
85      *
86      */

87     private static final String JavaDoc GREATER_THAN_ONE_MESSAGE_KEY = "3605";
88     
89     /**
90      *
91      */

92     private static final String JavaDoc BETWEEN_MESSAGE_KEY = "3606";
93     
94     /**
95      *
96      */

97     private static final String JavaDoc BETWEEN_ONE_AND_MANY_MESSAGE_KEY = "3607";
98     
99     /**
100      *
101      */

102     private Map JavaDoc categories;
103     
104     /**
105      *
106      */

107     private String JavaDoc attributeName;
108     
109     /**
110      *
111      */

112     private RangeCheck range;
113     
114     
115     
116     /**
117      *
118      */

119     public CategoryErrorPopulator()
120     {
121         super();
122     }
123     
124     /**
125      *
126      */

127     protected void clean() throws WorkflowException
128     {
129         clean(getErrorKey());
130     }
131     
132     /**
133      *
134      */

135     protected void populate() throws WorkflowException
136     {
137         final int count = getCategoryCount();
138         final int result = range.check(count);
139         if(result != RangeCheck.OK)
140         {
141             setPropertySetString(getErrorKey(), getErrorKey(result));
142         }
143     }
144
145     /**
146      *
147      */

148     private String JavaDoc getErrorKey(final int result) throws WorkflowException
149     {
150         switch(result)
151         {
152         case RangeCheck.EXACTLY:
153             return getStringManager().getString(EXACTLY_MESSAGE_KEY, range.getMin());
154         case RangeCheck.EXACTLY_ONE:
155             return getStringManager().getString(EXACTLY_ONE_MESSAGE_KEY, range.getMin());
156         case RangeCheck.LESS_THAN:
157             return getStringManager().getString(LESS_THAN_MESSAGE_KEY, range.getMax());
158         case RangeCheck.GREATER_THAN:
159             return getStringManager().getString(GREATER_THAN_MESSAGE_KEY, range.getMin());
160         case RangeCheck.GREATER_THAN_ONE:
161             return getStringManager().getString(GREATER_THAN_ONE_MESSAGE_KEY, range.getMin());
162         case RangeCheck.BETWEEN:
163             return getStringManager().getString(BETWEEN_MESSAGE_KEY, range.getMin(), range.getMax());
164         case RangeCheck.BETWEEN_ONE_AND_MANY:
165             return getStringManager().getString(BETWEEN_ONE_AND_MANY_MESSAGE_KEY, range.getMin(), range.getMax());
166         default:
167             throwException("Illegal result value [" + result + "]");
168         }
169         return null;
170     }
171     
172     /**
173      *
174      */

175     private int getCategoryCount()
176     {
177         final Collection JavaDoc category = (Collection JavaDoc) categories.get(attributeName);
178         return (category == null) ? 0 : category.size();
179     }
180     
181     /**
182      *
183      */

184     protected void initialize() throws WorkflowException
185     {
186         super.initialize();
187         categories = (Map JavaDoc) getParameter(CategoryProvider.CATEGORIES_PARAMETER, new HashMap JavaDoc());
188         attributeName = getArgument(NAME_ARGUMENT);
189         final Integer JavaDoc min = getIntegerArgument(MIN_ARGUMENT);
190         final Integer JavaDoc max = getIntegerArgument(MAX_ARGUMENT);
191         checkArguments(min, max);
192         range = new RangeCheck(min, max);
193     }
194     
195     /**
196      *
197      */

198     private String JavaDoc getErrorKey() throws WorkflowException
199     {
200         return CATEGORY_ERROR_PROPERTYSET_PREFIX + (argumentExists(NON_DEFAULT_NAME_ARGUMENT) ? getArgument(NON_DEFAULT_NAME_ARGUMENT) : attributeName);
201     }
202     
203     /**
204      *
205      */

206     private Integer JavaDoc getIntegerArgument(final String JavaDoc key) throws WorkflowException
207     {
208         if(argumentExists(key))
209         {
210             try
211             {
212                 return new Integer JavaDoc(getArgument(key));
213             }
214             catch(Exception JavaDoc e)
215             {
216                 throwException(e);
217             }
218         }
219         return null;
220     }
221     
222     /**
223      *
224      */

225     private void checkArguments(final Integer JavaDoc min, final Integer JavaDoc max) throws WorkflowException
226     {
227         if(min != null && min.intValue() < 0)
228         {
229             throwException("min must be a natural.");
230         }
231         if(max != null && max.intValue() < 0)
232         {
233             throwException("max must be a natural.");
234         }
235         if(min != null && max != null && min.intValue() > max.intValue())
236         {
237             throwException("max must be greater than min.");
238         }
239     }
240 }
241
Popular Tags