1 23 package org.infoglue.cms.applications.workflowtool.function; 24 25 import java.util.Collection ; 26 import java.util.HashMap ; 27 import java.util.Map ; 28 29 import org.infoglue.cms.applications.workflowtool.util.RangeCheck; 30 31 import com.opensymphony.workflow.WorkflowException; 32 33 34 37 public class CategoryErrorPopulator extends ErrorPopulator 38 { 39 42 private static final String NAME_ARGUMENT = "name"; 43 44 47 private static final String CATEGORY_ERROR_PROPERTYSET_PREFIX = ERROR_PROPERTYSET_PREFIX + "category_"; 48 49 52 private static final String NON_DEFAULT_NAME_ARGUMENT = "nonDefaultName"; 53 54 57 private static final String MIN_ARGUMENT = "min"; 58 59 62 private static final String MAX_ARGUMENT = "max"; 63 64 67 private static final String EXACTLY_MESSAGE_KEY = "3601"; 68 69 72 private static final String EXACTLY_ONE_MESSAGE_KEY = "3602"; 73 74 77 private static final String LESS_THAN_MESSAGE_KEY = "3603"; 78 79 82 private static final String GREATER_THAN_MESSAGE_KEY = "3604"; 83 84 87 private static final String GREATER_THAN_ONE_MESSAGE_KEY = "3605"; 88 89 92 private static final String BETWEEN_MESSAGE_KEY = "3606"; 93 94 97 private static final String BETWEEN_ONE_AND_MANY_MESSAGE_KEY = "3607"; 98 99 102 private Map categories; 103 104 107 private String attributeName; 108 109 112 private RangeCheck range; 113 114 115 116 119 public CategoryErrorPopulator() 120 { 121 super(); 122 } 123 124 127 protected void clean() throws WorkflowException 128 { 129 clean(getErrorKey()); 130 } 131 132 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 148 private String 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 175 private int getCategoryCount() 176 { 177 final Collection category = (Collection ) categories.get(attributeName); 178 return (category == null) ? 0 : category.size(); 179 } 180 181 184 protected void initialize() throws WorkflowException 185 { 186 super.initialize(); 187 categories = (Map ) getParameter(CategoryProvider.CATEGORIES_PARAMETER, new HashMap ()); 188 attributeName = getArgument(NAME_ARGUMENT); 189 final Integer min = getIntegerArgument(MIN_ARGUMENT); 190 final Integer max = getIntegerArgument(MAX_ARGUMENT); 191 checkArguments(min, max); 192 range = new RangeCheck(min, max); 193 } 194 195 198 private String getErrorKey() throws WorkflowException 199 { 200 return CATEGORY_ERROR_PROPERTYSET_PREFIX + (argumentExists(NON_DEFAULT_NAME_ARGUMENT) ? getArgument(NON_DEFAULT_NAME_ARGUMENT) : attributeName); 201 } 202 203 206 private Integer getIntegerArgument(final String key) throws WorkflowException 207 { 208 if(argumentExists(key)) 209 { 210 try 211 { 212 return new Integer (getArgument(key)); 213 } 214 catch(Exception e) 215 { 216 throwException(e); 217 } 218 } 219 return null; 220 } 221 222 225 private void checkArguments(final Integer min, final Integer 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 |