1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.service.impl;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.kuali.rice.krad.datadictionary.state.StateMapping;
20 import org.kuali.rice.krad.datadictionary.validation.ViewAttributeValueReader;
21 import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
22 import org.kuali.rice.krad.service.DictionaryValidationService;
23 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
24 import org.kuali.rice.krad.service.ViewValidationService;
25 import org.kuali.rice.krad.uif.UifConstants;
26 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
27 import org.kuali.rice.krad.uif.view.View;
28 import org.kuali.rice.krad.uif.view.ViewModel;
29 import org.kuali.rice.krad.util.GlobalVariables;
30
31
32
33
34
35
36
37
38 public class ViewValidationServiceImpl implements ViewValidationService {
39
40 protected DictionaryValidationService dictionaryValidationService;
41
42
43
44
45 @Override
46 public DictionaryValidationResult validateView(ViewModel model) {
47 return validateView(model, null);
48 }
49
50
51
52
53 @Override
54 public void validateViewSimulation(ViewModel model) {
55 validateViewSimulation(model, null);
56 }
57
58
59
60
61 @Override
62 public void validateViewSimulation(ViewModel model, String untilState) {
63
64 Object stateMappingObject = model.getViewPostMetadata().getComponentPostData(
65 model.getViewPostMetadata().getId(), UifConstants.PostMetadata.STATE_MAPPING);
66
67 StateMapping stateMapping = null;
68 if (stateMappingObject != null) {
69 stateMapping = (StateMapping) stateMappingObject;
70 }
71
72
73 Object statePathObject = model.getViewPostMetadata().getComponentPostData(
74 model.getViewPostMetadata().getId(), UifConstants.PostMetadata.STATE_OBJECT_BINDING_PATH);
75
76 String path = null;
77 if (statePathObject != null) {
78 path = (String) statePathObject;
79 }
80
81 Object object;
82 if (StringUtils.isNotBlank(path)) {
83 object = ObjectPropertyUtils.getPropertyValue(model, path);
84 } else {
85 object = model;
86 }
87
88 if (stateMapping != null && !stateMapping.getStates().isEmpty()) {
89 int startIndex = stateMapping.getStates().indexOf(stateMapping.getNextState(object));
90 if (startIndex == -1) {
91
92 startIndex = 0;
93 }
94
95 for (int i = startIndex; i < stateMapping.getStates().size(); i++) {
96 String state = stateMapping.getStates().get(i);
97
98 validateView(model, state);
99 GlobalVariables.getMessageMap().merge(GlobalVariables.getMessageMap().getErrorMessages(),
100 GlobalVariables.getMessageMap().getWarningMessages());
101 GlobalVariables.getMessageMap().clearErrorMessages();
102
103 if (untilState != null && untilState.equals(state)) {
104 break;
105 }
106 }
107 validateView(model, stateMapping.getCurrentState(object));
108 } else {
109 validateView(model, null);
110 }
111
112 }
113
114
115
116
117 @Override
118 public DictionaryValidationResult validateView(ViewModel model, String forcedValidationState) {
119
120 Object statePathObject = model.getViewPostMetadata().getComponentPostData(model.getViewPostMetadata().getId(),
121 UifConstants.PostMetadata.STATE_OBJECT_BINDING_PATH);
122
123 String path = null;
124 if (statePathObject != null) {
125 path = (String) statePathObject;
126 }
127
128 Object object;
129
130 if (StringUtils.isNotBlank(path)) {
131 object = ObjectPropertyUtils.getPropertyValue(model, path);
132 } else {
133 object = model;
134 }
135
136 String validationState = null;
137
138
139 Object stateMappingObject = model.getViewPostMetadata().getComponentPostData(
140 model.getViewPostMetadata().getId(), UifConstants.PostMetadata.STATE_MAPPING);
141
142 StateMapping stateMapping = null;
143 if (stateMappingObject != null) {
144 stateMapping = (StateMapping) stateMappingObject;
145 }
146
147 if (StringUtils.isNotBlank(forcedValidationState)) {
148
149 validationState = forcedValidationState;
150 } else if (stateMapping != null) {
151
152 validationState = stateMapping.getCurrentState(object);
153
154 }
155
156 return getDictionaryValidationService().validate(new ViewAttributeValueReader(model), true,
157 validationState, stateMapping);
158 }
159
160
161
162
163 @Override
164 public DictionaryValidationResult validateViewAgainstNextState(ViewModel model) {
165
166 Object statePathObject = model.getViewPostMetadata().getComponentPostData(model.getViewPostMetadata().getId(),
167 UifConstants.PostMetadata.STATE_OBJECT_BINDING_PATH);
168
169 String path = null;
170 if (statePathObject != null) {
171 path = (String) statePathObject;
172 }
173
174 Object object;
175
176 if (StringUtils.isNotBlank(path)) {
177 object = ObjectPropertyUtils.getPropertyValue(model, path);
178 } else {
179 object = model;
180 }
181
182 String validationState = null;
183
184
185 Object stateMappingObject = model.getViewPostMetadata().getComponentPostData(
186 model.getViewPostMetadata().getId(), UifConstants.PostMetadata.STATE_MAPPING);
187
188 StateMapping stateMapping = null;
189 if (stateMappingObject != null) {
190 stateMapping = (StateMapping) stateMappingObject;
191 }
192
193 if (stateMapping != null) {
194
195 validationState = stateMapping.getNextState(object);
196 }
197 return getDictionaryValidationService().validate(new ViewAttributeValueReader(model), true,
198 validationState, stateMapping);
199 }
200
201
202
203
204
205
206 public DictionaryValidationService getDictionaryValidationService() {
207 if (dictionaryValidationService == null) {
208 this.dictionaryValidationService = KRADServiceLocatorWeb.getDictionaryValidationService();
209 }
210 return dictionaryValidationService;
211 }
212
213
214
215
216
217
218 public void setDictionaryValidationService(DictionaryValidationService dictionaryValidationService) {
219 this.dictionaryValidationService = dictionaryValidationService;
220 }
221 }