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