1 | 0 | |
2 | |
|
3 | |
|
4 | |
|
5 | |
package pt.digitalis.dif.dem.objects.parameters.constraints; |
6 | |
|
7 | |
import java.util.ArrayList; |
8 | |
import java.util.HashMap; |
9 | |
import java.util.List; |
10 | |
import java.util.Map; |
11 | |
|
12 | |
import pt.digitalis.dif.dem.interfaces.IStageInstance; |
13 | |
import pt.digitalis.dif.dem.managers.IMessageManager; |
14 | |
import pt.digitalis.dif.dem.objects.messages.MessageList; |
15 | |
import pt.digitalis.dif.ioc.DIFIoCRegistry; |
16 | |
import pt.digitalis.utils.common.StringUtils; |
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | 0 | public abstract class AbstractParameterConstraint implements IParameterConstraint { |
25 | |
|
26 | |
|
27 | |
static IMessageManager messageManager; |
28 | |
|
29 | |
|
30 | |
static private MessageList messages; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
static private IMessageManager getMessageManager() |
38 | |
{ |
39 | 0 | if (messageManager == null) |
40 | 0 | messageManager = DIFIoCRegistry.getRegistry().getImplementation(IMessageManager.class); |
41 | |
|
42 | 0 | return messageManager; |
43 | |
} |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
protected static MessageList getMessages() |
51 | |
{ |
52 | |
|
53 | 0 | if (messages == null) |
54 | |
{ |
55 | 0 | messages = getMessageManager().collectEntityMessagesFromRepository(AbstractParameterConstraint.class); |
56 | |
} |
57 | |
|
58 | 0 | return messages; |
59 | |
} |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
protected List<String> getErrorIDs() |
67 | |
{ |
68 | 0 | return new ArrayList<String>(); |
69 | |
} |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
abstract protected Map<String, String> getErrorMessageValues(boolean parseValues); |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public ParameterConstraintResult getValidationResult(Object value, IStageInstance stageInstance) |
86 | |
{ |
87 | 0 | Boolean constraintResult = validateConstraint(value, stageInstance); |
88 | 0 | String language = null; |
89 | 0 | if (stageInstance != null && stageInstance.getContext() != null) |
90 | |
{ |
91 | 0 | language = stageInstance.getContext().getLanguage(); |
92 | |
} |
93 | |
|
94 | 0 | return new ParameterConstraintResult(this, constraintResult, getErrorIDs(), language); |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public boolean validateConstraint(Object value, IStageInstance stageInstance) |
102 | |
{ |
103 | 0 | if (value == null) |
104 | 0 | return validateConstraint((String) null, stageInstance); |
105 | |
else |
106 | 0 | return validateConstraint(value.toString(), stageInstance); |
107 | |
} |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
public String validationErrorMessage() |
113 | |
{ |
114 | 0 | return this.validationErrorMessage(true); |
115 | |
} |
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
public String validationErrorMessage(boolean parseValues) |
121 | |
{ |
122 | 0 | String message = getMessages().getMessages().get(this.getClass().getSimpleName() + ".message"); |
123 | 0 | Map<String, String> substitutions = new HashMap<String, String>(); |
124 | 0 | substitutions.putAll(getErrorMessageValues(parseValues)); |
125 | |
|
126 | 0 | if (!parseValues) |
127 | 0 | substitutions.remove("value"); |
128 | |
|
129 | 0 | message = StringUtils.parseMessage(message, substitutions); |
130 | |
|
131 | 0 | return message; |
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
public String validationErrorMessage(boolean parseValues, String language) |
139 | |
{ |
140 | 0 | String message = getMessages().getMessages(language).get(this.getClass().getSimpleName() + ".message"); |
141 | 0 | Map<String, String> substitutions = new HashMap<String, String>(); |
142 | 0 | substitutions.putAll(getErrorMessageValues(parseValues)); |
143 | |
|
144 | 0 | if (!parseValues) |
145 | 0 | substitutions.remove("value"); |
146 | |
|
147 | 0 | message = StringUtils.parseMessage(message, substitutions); |
148 | |
|
149 | 0 | return message; |
150 | |
} |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
public String validationErrorMessage(String language) |
156 | |
{ |
157 | 0 | return this.validationErrorMessage(true, language); |
158 | |
} |
159 | |
|
160 | |
} |