1
2
3
4
5
6 package pt.digitalis.dif.dem.objects.parameters.rules;
7
8 import java.util.ArrayList;
9 import java.util.Arrays;
10 import java.util.List;
11
12 import pt.digitalis.dif.controller.http.HTTPConstants;
13 import pt.digitalis.dif.dem.interfaces.ICustomFormDefinition;
14 import pt.digitalis.dif.dem.interfaces.IStageInstance;
15 import pt.digitalis.dif.dem.managers.ICustomFormManager;
16 import pt.digitalis.dif.dem.objects.FeatureState;
17 import pt.digitalis.dif.dem.objects.FormFieldCustomization;
18 import pt.digitalis.dif.dem.objects.parameters.IEditableParameter;
19 import pt.digitalis.dif.dem.objects.parameters.IParameter;
20 import pt.digitalis.dif.dem.objects.parameters.types.AbstractParameter;
21 import pt.digitalis.dif.dem.objects.parameters.types.BooleanParameter;
22 import pt.digitalis.dif.exception.objects.ParameterException;
23 import pt.digitalis.dif.ioc.DIFIoCRegistry;
24
25
26
27
28
29
30
31
32
33 public class ParameterRuleDependent<T> extends AbstractParameterRule<T> {
34
35
36
37
38
39
40
41 @SuppressWarnings({"rawtypes", "unchecked"})
42 private ParameterRuleResult getErrorMessage(IStageInstance stage, boolean valid, boolean initializationInProgress)
43 {
44 List<IParameter> invalidParameters = new ArrayList<IParameter>();
45
46 if (stage != null)
47 {
48
49 ICustomFormDefinition customFormDef = null;
50 FormFieldCustomization customFormFieldDef = null;
51 String formSubmitName = (String) stage.getContext().getRequest()
52 .getParameter(HTTPConstants.FORM_SUBMIT_NAME);
53 String formConfigID = (String) stage.getContext().getRequest()
54 .getParameter(HTTPConstants.FORM_SUBMIT__CONFIG_BUSINESS_ID);
55 boolean hasCheckedForCustomForm = false;
56
57 for (String parameterID: getParameters())
58 {
59 IParameter param = null;
60 boolean skipValidation = false;
61
62 try
63 {
64 param = stage.getParameters().getAllAvailableParameters().getParameter(parameterID);
65
66 if (param == null)
67 throw new ParameterException("Parameter does not exist: " + parameterID, null, null);
68
69 boolean parameterIsFromForm = false;
70 String paramFormName = param.getFormLinked();
71
72 if (paramFormName != null && formSubmitName != null)
73 parameterIsFromForm = paramFormName.equals(formSubmitName);
74
75
76 if (parameterIsFromForm && param.isFormConfigurable())
77 {
78 if (!hasCheckedForCustomForm)
79 {
80 customFormDef = DIFIoCRegistry.getRegistry().getImplementation(ICustomFormManager.class)
81 .getConfiguration(stage, formConfigID);
82 hasCheckedForCustomForm = true;
83 }
84
85 if (customFormDef != null)
86 {
87 customFormFieldDef = customFormDef.getCustomizedParameters().get(
88 param.getId().toLowerCase());
89
90 if (customFormFieldDef != null)
91 {
92 if (customFormFieldDef.getMandatory() != FeatureState.DEFAULT)
93 ((IEditableParameter) param)
94 .setRequired(customFormFieldDef.getMandatory() == FeatureState.ON);
95
96 if (customFormDef.getExcludedParameters().contains(param.getId()))
97 skipValidation = true;
98 }
99 }
100 }
101
102 if (!skipValidation)
103 {
104 boolean isBoolean = (param instanceof BooleanParameter);
105
106 if (initializationInProgress)
107 param.refreshParameterValue(stage);
108
109 if (valid)
110 {
111 if (param.isRequired())
112 {
113
114 if ((!isBoolean && param.getValue(stage.getContext()) == null)
115 || (isBoolean && !param.getValueAsBoolean(stage.getContext())))
116 invalidParameters.add(param);
117 }
118 }
119 else
120 {
121 if ((!isBoolean && param.getValue(stage.getContext()) != null)
122 || (isBoolean && param.getValueAsBoolean(stage.getContext())))
123 invalidParameters.add(param);
124 }
125 }
126
127 }
128 catch (ParameterException e)
129 {
130 if (param != null)
131 invalidParameters.add(param);
132 }
133 }
134 }
135
136 if (stage == null || invalidParameters.isEmpty())
137 return new ParameterRuleResult(true);
138 else
139 {
140 StringBuffer buffer = new StringBuffer();
141
142 if (invalidParameters.size() == 1)
143 buffer.append(this.getMessages(stage).get("field") + " ");
144 else
145 buffer.append(this.getMessages(stage).get("fieldPlural") + " ");
146
147 int count = 0;
148 for (IParameter invalidParam: invalidParameters)
149 {
150 stage.getContext().getRequest().addParameter(invalidParam.getId(), null);
151 invalidParam.setValue(null, stage, true);
152
153 if (count > 0)
154 buffer.append(", ");
155
156 buffer.append("\"" + getParameterName(stage, invalidParam.getId()) + "\"");
157 count++;
158 }
159
160 if (valid)
161 {
162
163 if (invalidParameters.size() == 1)
164 buffer.append(" " + this.getMessages(stage).get("mustBeFilledWhen") + " ");
165 else
166 buffer.append(" " + this.getMessages(stage).get("mustBeFilledWhenPlural") + " ");
167
168 buffer.append("\"" + getParameterName(stage, parameterID) + "\"");
169
170 if (getValue() != null)
171 buffer.append(" " + this.getMessages(stage).get("is") + " "
172 + (super.getDescriptionValue() != null ? super.getDescriptionValue() : super.getValue()));
173 else if (getStartValue() != null && getEndValue() != null)
174 buffer.append(" "
175 + this.getMessages(stage).get("isBetween")
176 + " "
177 + (super.getDescriptionStartValue() != null ? super.getDescriptionStartValue() : super
178 .getStartValue().toString())
179 + " "
180 + this.getMessages(stage).get("and")
181 + " "
182 + (super.getDescriptionEndValue() != null ? super.getDescriptionEndValue() : super
183 .getEndValue().toString()));
184 return new ParameterRuleResult(buffer.toString());
185 }
186 else
187 {
188 if (invalidParameters.size() == 1)
189 buffer.append(" " + this.getMessages(stage).get("canBeFilledIf") + " ");
190 else
191 buffer.append(" " + this.getMessages(stage).get("canBeFilledIfPlural") + " ");
192
193 buffer.append("\"" + getParameterName(stage, parameterID) + "\"");
194
195 if (getValue() != null)
196 buffer.append(" " + this.getMessages(stage).get("is") + " "
197 + (super.getDescriptionValue() != null ? super.getDescriptionValue() : super.getValue()));
198 else if (getStartValue() != null && getEndValue() != null)
199 buffer.append(" "
200 + this.getMessages(stage).get("isBetween")
201 + " "
202 + (super.getDescriptionStartValue() != null ? super.getDescriptionStartValue() : super
203 .getStartValue().toString())
204 + " "
205 + this.getMessages(stage).get("and")
206 + " "
207 + (super.getDescriptionEndValue() != null ? super.getDescriptionEndValue() : super
208 .getEndValue().toString()));
209 else
210 buffer.append(" " + this.getMessages(stage).get("isFilled"));
211
212 return new ParameterRuleResult(buffer.toString());
213 }
214 }
215 }
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232 public ParameterRuleResult getValidationResult(IStageInstance stage, T value, boolean initializationInProgress,
233 AbstractParameter<T> parameter) throws ParameterException
234 {
235
236 boolean valid = false;
237
238 if (getValue() != null)
239 {
240 if (parameter instanceof BooleanParameter && value == null)
241 valid = getValue().equals("false");
242 else
243 {
244 if (getValue().contains(","))
245 {
246 List<String> list = Arrays.asList(getValue().split(","));
247 valid = (value != null && list.contains(value.toString()));
248 }
249 else
250 valid = (value != null && getValue().equals(value.toString()));
251 }
252 }
253 else if (getStartValue() != null && getEndValue() != null)
254 {
255
256 valid = value != null
257 && (getStartValue().compareTo(value.toString()) <= 0 && getEndValue().compareTo(value.toString()) >= 0);
258 }
259 else
260 valid = (value != null);
261
262 return getErrorMessage(stage, valid, initializationInProgress);
263
264 }
265 }