1 | 0 | |
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 | 0 | 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 | 0 | List<IParameter> invalidParameters = new ArrayList<IParameter>(); |
45 | |
|
46 | 0 | if (stage != null) |
47 | |
{ |
48 | |
|
49 | 0 | ICustomFormDefinition customFormDef = null; |
50 | 0 | FormFieldCustomization customFormFieldDef = null; |
51 | 0 | String formSubmitName = (String) stage.getContext().getRequest() |
52 | 0 | .getParameter(HTTPConstants.FORM_SUBMIT_NAME); |
53 | 0 | String formConfigID = (String) stage.getContext().getRequest() |
54 | 0 | .getParameter(HTTPConstants.FORM_SUBMIT__CONFIG_BUSINESS_ID); |
55 | 0 | boolean hasCheckedForCustomForm = false; |
56 | |
|
57 | 0 | for (String parameterID: getParameters()) |
58 | |
{ |
59 | 0 | IParameter param = null; |
60 | 0 | boolean skipValidation = false; |
61 | |
|
62 | |
try |
63 | |
{ |
64 | 0 | param = stage.getParameters().getAllAvailableParameters().getParameter(parameterID); |
65 | |
|
66 | 0 | if (param == null) |
67 | 0 | throw new ParameterException("Parameter does not exist: " + parameterID, null, null); |
68 | |
|
69 | 0 | boolean parameterIsFromForm = false; |
70 | 0 | String paramFormName = param.getFormLinked(); |
71 | |
|
72 | 0 | if (paramFormName != null && formSubmitName != null) |
73 | 0 | parameterIsFromForm = paramFormName.equals(formSubmitName); |
74 | |
|
75 | |
|
76 | 0 | if (parameterIsFromForm && param.isFormConfigurable()) |
77 | |
{ |
78 | 0 | if (!hasCheckedForCustomForm) |
79 | |
{ |
80 | 0 | customFormDef = DIFIoCRegistry.getRegistry().getImplementation(ICustomFormManager.class) |
81 | 0 | .getConfiguration(stage, formConfigID); |
82 | 0 | hasCheckedForCustomForm = true; |
83 | |
} |
84 | |
|
85 | 0 | if (customFormDef != null) |
86 | |
{ |
87 | 0 | customFormFieldDef = customFormDef.getCustomizedParameters().get( |
88 | 0 | param.getId().toLowerCase()); |
89 | |
|
90 | 0 | if (customFormFieldDef != null) |
91 | |
{ |
92 | 0 | if (customFormFieldDef.getMandatory() != FeatureState.DEFAULT) |
93 | 0 | ((IEditableParameter) param) |
94 | 0 | .setRequired(customFormFieldDef.getMandatory() == FeatureState.ON); |
95 | |
|
96 | 0 | if (customFormDef.getExcludedParameters().contains(param.getId())) |
97 | 0 | skipValidation = true; |
98 | |
} |
99 | |
} |
100 | |
} |
101 | |
|
102 | 0 | if (!skipValidation) |
103 | |
{ |
104 | 0 | boolean isBoolean = (param instanceof BooleanParameter); |
105 | |
|
106 | 0 | if (initializationInProgress) |
107 | 0 | param.refreshParameterValue(stage); |
108 | |
|
109 | 0 | if (valid) |
110 | |
{ |
111 | 0 | if (param.isRequired()) |
112 | |
{ |
113 | |
|
114 | 0 | if ((!isBoolean && param.getValue(stage.getContext()) == null) |
115 | 0 | || (isBoolean && !param.getValueAsBoolean(stage.getContext()))) |
116 | 0 | invalidParameters.add(param); |
117 | |
} |
118 | |
} |
119 | |
else |
120 | |
{ |
121 | 0 | if ((!isBoolean && param.getValue(stage.getContext()) != null) |
122 | 0 | || (isBoolean && param.getValueAsBoolean(stage.getContext()))) |
123 | 0 | invalidParameters.add(param); |
124 | |
} |
125 | |
} |
126 | |
|
127 | |
} |
128 | 0 | catch (ParameterException e) |
129 | |
{ |
130 | 0 | if (param != null) |
131 | 0 | invalidParameters.add(param); |
132 | |
} |
133 | |
} |
134 | |
} |
135 | |
|
136 | 0 | if (stage == null || invalidParameters.isEmpty()) |
137 | 0 | return new ParameterRuleResult(true); |
138 | |
else |
139 | |
{ |
140 | 0 | StringBuffer buffer = new StringBuffer(); |
141 | |
|
142 | 0 | if (invalidParameters.size() == 1) |
143 | 0 | buffer.append(this.getMessages(stage).get("field") + " "); |
144 | |
else |
145 | 0 | buffer.append(this.getMessages(stage).get("fieldPlural") + " "); |
146 | |
|
147 | 0 | int count = 0; |
148 | 0 | for (IParameter invalidParam: invalidParameters) |
149 | |
{ |
150 | 0 | stage.getContext().getRequest().addParameter(invalidParam.getId(), null); |
151 | 0 | invalidParam.setValue(null, stage, true); |
152 | |
|
153 | 0 | if (count > 0) |
154 | 0 | buffer.append(", "); |
155 | |
|
156 | 0 | buffer.append("\"" + getParameterName(stage, invalidParam.getId()) + "\""); |
157 | 0 | count++; |
158 | |
} |
159 | |
|
160 | 0 | if (valid) |
161 | |
{ |
162 | |
|
163 | 0 | if (invalidParameters.size() == 1) |
164 | 0 | buffer.append(" " + this.getMessages(stage).get("mustBeFilledWhen") + " "); |
165 | |
else |
166 | 0 | buffer.append(" " + this.getMessages(stage).get("mustBeFilledWhenPlural") + " "); |
167 | |
|
168 | 0 | buffer.append("\"" + getParameterName(stage, parameterID) + "\""); |
169 | |
|
170 | 0 | if (getValue() != null) |
171 | 0 | buffer.append(" " + this.getMessages(stage).get("is") + " " |
172 | 0 | + (super.getDescriptionValue() != null ? super.getDescriptionValue() : super.getValue())); |
173 | 0 | else if (getStartValue() != null && getEndValue() != null) |
174 | 0 | buffer.append(" " |
175 | 0 | + this.getMessages(stage).get("isBetween") |
176 | 0 | + " " |
177 | 0 | + (super.getDescriptionStartValue() != null ? super.getDescriptionStartValue() : super |
178 | 0 | .getStartValue().toString()) |
179 | 0 | + " " |
180 | 0 | + this.getMessages(stage).get("and") |
181 | 0 | + " " |
182 | 0 | + (super.getDescriptionEndValue() != null ? super.getDescriptionEndValue() : super |
183 | 0 | .getEndValue().toString())); |
184 | 0 | return new ParameterRuleResult(buffer.toString()); |
185 | |
} |
186 | |
else |
187 | |
{ |
188 | 0 | if (invalidParameters.size() == 1) |
189 | 0 | buffer.append(" " + this.getMessages(stage).get("canBeFilledIf") + " "); |
190 | |
else |
191 | 0 | buffer.append(" " + this.getMessages(stage).get("canBeFilledIfPlural") + " "); |
192 | |
|
193 | 0 | buffer.append("\"" + getParameterName(stage, parameterID) + "\""); |
194 | |
|
195 | 0 | if (getValue() != null) |
196 | 0 | buffer.append(" " + this.getMessages(stage).get("is") + " " |
197 | 0 | + (super.getDescriptionValue() != null ? super.getDescriptionValue() : super.getValue())); |
198 | 0 | else if (getStartValue() != null && getEndValue() != null) |
199 | 0 | buffer.append(" " |
200 | 0 | + this.getMessages(stage).get("isBetween") |
201 | 0 | + " " |
202 | 0 | + (super.getDescriptionStartValue() != null ? super.getDescriptionStartValue() : super |
203 | 0 | .getStartValue().toString()) |
204 | 0 | + " " |
205 | 0 | + this.getMessages(stage).get("and") |
206 | 0 | + " " |
207 | 0 | + (super.getDescriptionEndValue() != null ? super.getDescriptionEndValue() : super |
208 | 0 | .getEndValue().toString())); |
209 | |
else |
210 | 0 | buffer.append(" " + this.getMessages(stage).get("isFilled")); |
211 | |
|
212 | 0 | 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 | 0 | boolean valid = false; |
237 | |
|
238 | 0 | if (getValue() != null) |
239 | |
{ |
240 | 0 | if (parameter instanceof BooleanParameter && value == null) |
241 | 0 | valid = getValue().equals("false"); |
242 | |
else |
243 | |
{ |
244 | 0 | if (getValue().contains(",")) |
245 | |
{ |
246 | 0 | List<String> list = Arrays.asList(getValue().split(",")); |
247 | 0 | valid = (value != null && list.contains(value.toString())); |
248 | |
} |
249 | |
else |
250 | 0 | valid = (value != null && getValue().equals(value.toString())); |
251 | |
} |
252 | |
} |
253 | 0 | else if (getStartValue() != null && getEndValue() != null) |
254 | |
{ |
255 | |
|
256 | 0 | valid = value != null |
257 | 0 | && (getStartValue().compareTo(value.toString()) <= 0 && getEndValue().compareTo(value.toString()) >= 0); |
258 | |
} |
259 | |
else |
260 | 0 | valid = (value != null); |
261 | |
|
262 | 0 | return getErrorMessage(stage, valid, initializationInProgress); |
263 | |
|
264 | |
} |
265 | |
} |