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 | 0 | public class BigDecimalParameter extends AbstractParameter<BigDecimal> { |
30 | |
|
31 | |
|
32 | |
@SuppressWarnings("serial") |
33 | 0 | final static private List<String> supportedClasses = new ArrayList<String>() { |
34 | |
|
35 | |
{ |
36 | 0 | add(BigDecimal.class.getCanonicalName()); |
37 | |
} |
38 | |
}; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
@Override |
44 | |
protected String automaticConstraints() |
45 | |
{ |
46 | 0 | return "numeric"; |
47 | |
} |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
public List<String> getSupportedClasses() |
53 | |
{ |
54 | 0 | return supportedClasses; |
55 | |
} |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
public BigDecimal getValueAsBigDecimal(IDIFContext context) throws ParameterException |
61 | |
{ |
62 | 0 | return getValue(context); |
63 | |
} |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
public boolean getValueAsBoolean(IDIFContext context) throws ParameterException |
69 | |
{ |
70 | 0 | return getValue(context).compareTo(BigDecimal.ZERO) != 0; |
71 | |
} |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public Date getValueAsDate(IDIFContext context) |
77 | |
{ |
78 | 0 | return null; |
79 | |
} |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
public Double getValueAsDouble(IDIFContext context) throws ParameterException |
85 | |
{ |
86 | 0 | return getValue(context).doubleValue(); |
87 | |
} |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public Long getValueAsLong(IDIFContext context) throws ParameterException |
93 | |
{ |
94 | 0 | return getValue(context).longValue(); |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
public boolean isNumeric() |
101 | |
{ |
102 | 0 | return true; |
103 | |
} |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
public ParameterErrorList setValueFromString(String value, IStageInstance stageInstance, |
110 | |
boolean initializationInProgress) |
111 | |
{ |
112 | |
|
113 | 0 | ParameterErrorList list = new ParameterErrorList(this, value); |
114 | |
ParameterError error; |
115 | |
|
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 | |
|
134 | 0 | error = new ParameterError(constraint.validationErrorMessage(language), ParameterErrorType.CONSTRAINT); |
135 | 0 | error.setConstraint("numeric", constraint); |
136 | |
|
137 | 0 | list.addError(error); |
138 | |
} |
139 | |
else |
140 | |
{ |
141 | |
try |
142 | |
{ |
143 | 0 | if (value == null || "".equals(value)) |
144 | 0 | return setValue(null, stageInstance, initializationInProgress); |
145 | |
else |
146 | |
{ |
147 | 0 | value = value.replace(',', '.').trim(); |
148 | |
|
149 | 0 | return setValue(new BigDecimal(value), stageInstance, initializationInProgress); |
150 | |
} |
151 | |
} |
152 | 0 | catch (Exception e) |
153 | |
{ |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | 0 | error = new ParameterError("Error parsing value", ParameterErrorType.OTHER); |
161 | 0 | error.setException(e); |
162 | 0 | list.addError(error); |
163 | |
} |
164 | |
} |
165 | 0 | return list; |
166 | |
} |
167 | |
} |