1 | 0 | |
2 | |
|
3 | |
|
4 | |
|
5 | |
package pt.digitalis.dif.dem.objects.parameters.types; |
6 | |
|
7 | |
import java.math.BigDecimal; |
8 | |
import java.util.ArrayList; |
9 | |
import java.util.Date; |
10 | |
import java.util.List; |
11 | |
|
12 | |
import pt.digitalis.dif.controller.interfaces.IDIFContext; |
13 | |
import pt.digitalis.dif.dem.interfaces.IStageInstance; |
14 | |
import pt.digitalis.dif.dem.objects.parameters.constraints.IParameterConstraint; |
15 | |
import pt.digitalis.dif.dem.objects.parameters.errors.ParameterError; |
16 | |
import pt.digitalis.dif.dem.objects.parameters.errors.ParameterErrorList; |
17 | |
import pt.digitalis.dif.dem.objects.parameters.errors.ParameterErrorType; |
18 | |
import pt.digitalis.dif.exception.objects.ParameterException; |
19 | |
import pt.digitalis.dif.startup.DIFGeneralConfigurationParameters; |
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | 0 | public class LongParameter extends AbstractParameter<Number> { |
31 | |
|
32 | |
|
33 | |
@SuppressWarnings("serial") |
34 | 0 | final static private List<String> supportedClasses = new ArrayList<String>() { |
35 | |
|
36 | |
{ |
37 | 0 | add(Long.class.getCanonicalName()); |
38 | |
} |
39 | |
}; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
@Override |
45 | |
protected String automaticConstraints() |
46 | |
{ |
47 | 0 | return "numeric"; |
48 | |
} |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public List<String> getSupportedClasses() |
54 | |
{ |
55 | 0 | return supportedClasses; |
56 | |
} |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public BigDecimal getValueAsBigDecimal(IDIFContext context) throws ParameterException |
62 | |
{ |
63 | 0 | return BigDecimal.valueOf(getValueAsLong(context)); |
64 | |
} |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public boolean getValueAsBoolean(IDIFContext context) throws ParameterException |
70 | |
{ |
71 | 0 | return !getValueAsLong(context).equals(0L); |
72 | |
} |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
public Date getValueAsDate(IDIFContext context) |
78 | |
{ |
79 | 0 | return null; |
80 | |
} |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public Double getValueAsDouble(IDIFContext context) throws ParameterException |
86 | |
{ |
87 | 0 | return getValueAsLong(context).doubleValue(); |
88 | |
} |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public Long getValueAsLong(IDIFContext context) throws ParameterException |
94 | |
{ |
95 | 0 | return (Long) getValue(context); |
96 | |
} |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public boolean isNumeric() |
102 | |
{ |
103 | 0 | return true; |
104 | |
} |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
public ParameterErrorList setValueFromString(String value, IStageInstance stageInstance, |
111 | |
boolean initializationInProgress) |
112 | |
{ |
113 | |
|
114 | 0 | ParameterErrorList list = new ParameterErrorList(this, value); |
115 | |
ParameterError error; |
116 | 0 | IParameterConstraint constraint = getConstraints().get("numeric"); |
117 | |
IDIFContext context; |
118 | |
|
119 | 0 | if (stageInstance == null) |
120 | 0 | context = null; |
121 | |
else |
122 | 0 | context = stageInstance.getContext(); |
123 | |
|
124 | |
|
125 | 0 | if (!constraint.validateConstraint(value, stageInstance)) |
126 | |
{ |
127 | |
String language; |
128 | |
|
129 | 0 | if (context == null) |
130 | 0 | language = DIFGeneralConfigurationParameters.getInstance().getDefaultLanguage(); |
131 | |
else |
132 | 0 | language = context.getLanguage(); |
133 | 0 | error = new ParameterError(constraint.validationErrorMessage(language), ParameterErrorType.CONSTRAINT); |
134 | 0 | error.setConstraint("numeric", constraint); |
135 | |
|
136 | 0 | list.addError(error); |
137 | |
} |
138 | |
else |
139 | |
{ |
140 | |
try |
141 | |
{ |
142 | 0 | if (value == null || "".equals(value) || "undefined".equals(value)) |
143 | 0 | return setValue(null, stageInstance, initializationInProgress); |
144 | |
else |
145 | 0 | return setValue(Long.parseLong(value.trim()), stageInstance, initializationInProgress); |
146 | |
|
147 | |
} |
148 | 0 | catch (Exception e) |
149 | |
{ |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
String language; |
157 | |
|
158 | 0 | if (context == null) |
159 | 0 | language = DIFGeneralConfigurationParameters.getInstance().getDefaultLanguage(); |
160 | |
else |
161 | 0 | language = context.getLanguage(); |
162 | |
|
163 | 0 | error = new ParameterError(getMessages().getMessages(language).get("valueTooLong"), |
164 | 0 | ParameterErrorType.OTHER); |
165 | 0 | error.setException(e); |
166 | 0 | list.addError(error); |
167 | |
} |
168 | |
} |
169 | |
|
170 | 0 | return list; |
171 | |
} |
172 | |
} |