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.core.api.config.property.ConfigurationService;
20 import org.kuali.rice.krad.datadictionary.state.StateMapping;
21 import org.kuali.rice.krad.datadictionary.validation.ViewAttributeValueReader;
22 import org.kuali.rice.krad.datadictionary.validation.constraint.BaseConstraint;
23 import org.kuali.rice.krad.datadictionary.validation.result.DictionaryValidationResult;
24 import org.kuali.rice.krad.service.DictionaryValidationService;
25 import org.kuali.rice.krad.service.KRADServiceLocator;
26 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
27 import org.kuali.rice.krad.service.ViewValidationService;
28 import org.kuali.rice.krad.uif.util.ConstraintStateUtils;
29 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
30 import org.kuali.rice.krad.uif.view.View;
31 import org.kuali.rice.krad.uif.view.ViewModel;
32 import org.kuali.rice.krad.util.ErrorMessage;
33 import org.kuali.rice.krad.util.GlobalVariables;
34 import org.kuali.rice.krad.util.MessageMap;
35 import org.springframework.util.AutoPopulatingList;
36
37 import java.text.MessageFormat;
38 import java.util.ArrayList;
39 import java.util.List;
40
41
42
43
44
45
46
47
48 public class ViewValidationServiceImpl implements ViewValidationService {
49
50 protected DictionaryValidationService dictionaryValidationService;
51
52
53
54
55 @Override
56 public DictionaryValidationResult validateView(ViewModel model) {
57 return validateView(model.getPostedView(), model);
58 }
59
60
61
62
63 @Override
64 public DictionaryValidationResult validateView(View view, ViewModel model) {
65 return validateView(view, model, null);
66 }
67
68
69
70
71 @Override
72 public void validateViewSimulation(View view, ViewModel model){
73 validateViewSimulation(view, model, null);
74 }
75
76
77
78
79 @Override
80 public void validateViewSimulation(View view, ViewModel model, String untilState){
81 StateMapping stateMapping = view.getStateMapping();
82
83 String path = view.getStateObjectBindingPath();
84 Object object;
85 if(StringUtils.isNotBlank(path)){
86 object = ObjectPropertyUtils.getPropertyValue(model, path);
87 }
88 else{
89 object = model;
90 }
91
92
93 if(stateMapping != null && !stateMapping.getStates().isEmpty()){
94 int startIndex = stateMapping.getStates().indexOf(stateMapping.getNextState(object));
95 if(startIndex == -1){
96
97 startIndex = 0;
98 }
99
100 for(int i = startIndex; i < stateMapping.getStates().size(); i++){
101 String state = stateMapping.getStates().get(i);
102
103 validateView(view, model, state);
104 GlobalVariables.getMessageMap().merge(GlobalVariables.getMessageMap().getErrorMessages(),
105 GlobalVariables.getMessageMap().getWarningMessages());
106 GlobalVariables.getMessageMap().clearErrorMessages();
107
108 if(untilState != null && untilState.equals(state)){
109 break;
110 }
111 }
112 validateView(view, model, stateMapping.getCurrentState(object));
113 }
114 else{
115 validateView(view, model, null);
116 }
117
118 }
119
120
121
122
123 @Override
124 public DictionaryValidationResult validateView(View view, ViewModel model, String forcedValidationState) {
125 String path = view.getStateObjectBindingPath();
126 Object object;
127
128 if(StringUtils.isNotBlank(path)){
129 object = ObjectPropertyUtils.getPropertyValue(model, path);
130 }
131 else{
132 object = model;
133 }
134
135 String validationState = null;
136 StateMapping stateMapping = view.getStateMapping();
137 if(StringUtils.isNotBlank(forcedValidationState)){
138
139 validationState = forcedValidationState;
140 }
141 else if(stateMapping != null){
142
143 validationState = stateMapping.getCurrentState(object);
144
145 }
146
147 return getDictionaryValidationService().validate(new ViewAttributeValueReader(view, model), true,
148 validationState, stateMapping);
149 }
150
151
152
153
154 @Override
155 public DictionaryValidationResult validateViewAgainstNextState(View view, ViewModel model) {
156 String path = view.getStateObjectBindingPath();
157 Object object;
158
159 if(StringUtils.isNotBlank(path)){
160 object = ObjectPropertyUtils.getPropertyValue(model, path);
161 }
162 else{
163 object = model;
164 }
165
166 String validationState = null;
167
168 StateMapping stateMapping = view.getStateMapping();
169
170 if(stateMapping != null){
171
172 validationState = stateMapping.getNextState(object);
173 }
174 return getDictionaryValidationService().validate(new ViewAttributeValueReader(view, model), true,
175 validationState, stateMapping);
176 }
177
178
179
180
181
182
183 public DictionaryValidationService getDictionaryValidationService() {
184 if (dictionaryValidationService == null) {
185 this.dictionaryValidationService = KRADServiceLocatorWeb.getDictionaryValidationService();
186 }
187 return dictionaryValidationService;
188 }
189
190
191
192
193
194
195 public void setDictionaryValidationService(DictionaryValidationService dictionaryValidationService) {
196 this.dictionaryValidationService = dictionaryValidationService;
197 }
198 }