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