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 | |
import java.util.Map; |
12 | |
|
13 | |
import pt.digitalis.dif.dem.annotations.AnnotationTags; |
14 | |
import pt.digitalis.dif.dem.interfaces.IStageInstance; |
15 | |
import pt.digitalis.dif.dem.managers.IMessageManager; |
16 | |
import pt.digitalis.dif.dem.objects.parameters.IParameter; |
17 | |
import pt.digitalis.dif.exception.objects.ParameterException; |
18 | |
import pt.digitalis.dif.ioc.DIFIoCRegistry; |
19 | |
import pt.digitalis.dif.utils.ObjectFormatter; |
20 | |
import pt.digitalis.utils.common.StringUtils; |
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | 0 | public abstract class AbstractParameterRule<T> implements IParameterRule<T> { |
31 | |
|
32 | |
|
33 | |
private ParameterRuleAction action; |
34 | |
|
35 | |
|
36 | 0 | private String descriptionEndValue = null; |
37 | |
|
38 | |
|
39 | 0 | private String descriptionStartValue = null; |
40 | |
|
41 | |
|
42 | 0 | private String descriptionValue = null; |
43 | |
|
44 | |
|
45 | 0 | private String endValue = null; |
46 | |
|
47 | |
|
48 | |
protected String parameterID; |
49 | |
|
50 | |
|
51 | 0 | private List<String> parameters = new ArrayList<String>(); |
52 | |
|
53 | |
|
54 | 0 | private String startValue = null; |
55 | |
|
56 | |
|
57 | 0 | private String value = null; |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
public ParameterRuleAction getAction() |
63 | |
{ |
64 | 0 | return action; |
65 | |
} |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public String getDescriptionEndValue() |
71 | |
{ |
72 | 0 | return descriptionEndValue; |
73 | |
} |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
public String getDescriptionStartValue() |
79 | |
{ |
80 | 0 | return descriptionStartValue; |
81 | |
} |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
public String getDescriptionValue() |
87 | |
{ |
88 | 0 | return descriptionValue; |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
public String getEndValue() |
95 | |
{ |
96 | 0 | return endValue; |
97 | |
} |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
protected Map<String, String> getMessages(IStageInstance stage) |
107 | |
{ |
108 | 0 | return DIFIoCRegistry.getRegistry().getImplementation(IMessageManager.class) |
109 | 0 | .collectEntityMessagesFromRepository(this.getClass()).getMessages(stage.getContext().getLanguage()); |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
@SuppressWarnings("unchecked") |
122 | |
public IParameter<T> getParameter(IStageInstance stage) throws ParameterException |
123 | |
{ |
124 | 0 | return (IParameter<T>) stage.getParameters().getAllAvailableParameters().getParameter(parameterID); |
125 | |
} |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public String getParameterName(IStageInstance stage, String parameterID) |
138 | |
{ |
139 | 0 | String message = stage.getMessage(parameterID); |
140 | |
|
141 | 0 | if (message == null) |
142 | 0 | message = StringUtils.camelCaseToString(parameterID); |
143 | |
|
144 | 0 | return message; |
145 | |
} |
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
public List<String> getParameters() |
151 | |
{ |
152 | 0 | return parameters; |
153 | |
} |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
public String getStartValue() |
159 | |
{ |
160 | 0 | return startValue; |
161 | |
} |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
public String getValue() |
167 | |
{ |
168 | 0 | return value; |
169 | |
} |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
public AbstractParameterRule<T> init(String parameterID, String parameters, ParameterRuleAction action, |
177 | |
String value, String first, String last) |
178 | |
{ |
179 | 0 | this.parameterID = parameterID; |
180 | 0 | this.action = action; |
181 | |
|
182 | 0 | if (parameters != null && !"".equals(parameters)) |
183 | |
{ |
184 | 0 | List<String> parametersPassed = Arrays.asList(parameters.split(",")); |
185 | |
|
186 | 0 | for (String parameterPassed: parametersPassed) |
187 | 0 | this.parameters.add(parameterPassed.trim().toLowerCase()); |
188 | |
} |
189 | |
|
190 | 0 | if (value != null && !value.contains(AnnotationTags.NONE.toString()) && value.contains(":")) |
191 | |
{ |
192 | 0 | String[] splitCommaValues = value.split(","); |
193 | 0 | for (String v: splitCommaValues) |
194 | |
{ |
195 | 0 | String[] splitValue = v.split(":"); |
196 | 0 | if (this.value != null) |
197 | |
{ |
198 | 0 | this.value += ","; |
199 | |
} |
200 | |
else |
201 | |
{ |
202 | 0 | this.value = ""; |
203 | |
} |
204 | 0 | this.value += splitValue[0]; |
205 | |
|
206 | 0 | if (this.descriptionValue != null) |
207 | |
{ |
208 | 0 | this.descriptionValue += ","; |
209 | |
} |
210 | |
else |
211 | |
{ |
212 | 0 | this.descriptionValue = ""; |
213 | |
} |
214 | 0 | this.descriptionValue += splitValue[1]; |
215 | |
} |
216 | |
} |
217 | |
else |
218 | |
{ |
219 | 0 | this.value = value; |
220 | |
} |
221 | |
|
222 | 0 | if (first != null && !first.contains(AnnotationTags.NONE.toString()) && first.contains(":")) |
223 | |
{ |
224 | 0 | String[] splitValue = first.split(":"); |
225 | 0 | this.startValue = splitValue[0]; |
226 | 0 | this.descriptionStartValue = splitValue[1]; |
227 | |
} |
228 | |
else |
229 | |
{ |
230 | 0 | this.startValue = first; |
231 | |
} |
232 | |
|
233 | 0 | if (last != null && !last.contains(AnnotationTags.NONE.toString()) && last.contains(":")) |
234 | |
{ |
235 | 0 | String[] splitValue = last.split(":"); |
236 | 0 | this.endValue = splitValue[0]; |
237 | 0 | this.descriptionEndValue = splitValue[1]; |
238 | |
} |
239 | |
else |
240 | |
{ |
241 | 0 | this.endValue = last; |
242 | |
} |
243 | |
|
244 | 0 | return this; |
245 | |
} |
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
public void setDescriptionEndValue(String descriptionEndValue) |
251 | |
{ |
252 | 0 | this.descriptionEndValue = descriptionEndValue; |
253 | 0 | } |
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
public void setDescriptionStartValue(String descriptionStartValue) |
259 | |
{ |
260 | 0 | this.descriptionStartValue = descriptionStartValue; |
261 | 0 | } |
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
public void setDescriptionValue(String descriptionValue) |
267 | |
{ |
268 | 0 | this.descriptionValue = descriptionValue; |
269 | 0 | } |
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
public AbstractParameterRule<T> setEndValue(String endValue) |
275 | |
{ |
276 | 0 | this.endValue = endValue; |
277 | |
|
278 | 0 | return this; |
279 | |
} |
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
public AbstractParameterRule<T> setStartValue(String startValue) |
285 | |
{ |
286 | 0 | this.startValue = startValue; |
287 | |
|
288 | 0 | return this; |
289 | |
} |
290 | |
|
291 | |
|
292 | |
|
293 | |
|
294 | |
public AbstractParameterRule<T> setValue(String value) |
295 | |
{ |
296 | 0 | this.value = value; |
297 | |
|
298 | 0 | return this; |
299 | |
} |
300 | |
|
301 | |
|
302 | |
|
303 | |
|
304 | |
@Override |
305 | |
public String toString() |
306 | |
{ |
307 | 0 | ObjectFormatter formatter = new ObjectFormatter(); |
308 | 0 | formatter.addItem("Action", this.action); |
309 | 0 | formatter.addItem("End Value", this.endValue); |
310 | 0 | formatter.addItem("Parameter ID", this.parameterID); |
311 | 0 | formatter.addItem("Parameters", this.parameters); |
312 | 0 | formatter.addItem("Start Value", this.startValue); |
313 | 0 | formatter.addItem("Value", this.value); |
314 | 0 | formatter.addItem("Description Value", this.descriptionValue); |
315 | 0 | formatter.addItem("Description Start Value", this.descriptionStartValue); |
316 | 0 | formatter.addItem("Description End Value", this.descriptionEndValue); |
317 | |
|
318 | 0 | return formatter.getFormatedObject(); |
319 | |
} |
320 | |
} |