View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Implementation of Validation service for views, uses the same validation mechanisms as DictionaryValidationService
33   * but uses a different AttributeValueReader to get the correct information from InputFields - which
34   * include any AttributeDefinition defined attributes, if defined and not overriden
35   *
36   * @see ViewValidationService
37   */
38  public class ViewValidationServiceImpl implements ViewValidationService {
39  
40      protected DictionaryValidationService dictionaryValidationService;
41  
42      /**
43       * @see ViewValidationService#validateView(org.kuali.rice.krad.uif.view.ViewModel)
44       */
45      @Override
46      public DictionaryValidationResult validateView(ViewModel model) {
47          return validateView(model, null);
48      }
49  
50      /**
51       * @see ViewValidationService#validateViewSimulation(ViewModel)
52       */
53      @Override
54      public void validateViewSimulation(ViewModel model) {
55          validateViewSimulation(model, null);
56      }
57  
58      /**
59       * @see ViewValidationService#validateViewSimulation(ViewModel, String)
60       */
61      @Override
62      public void validateViewSimulation(ViewModel model, String untilState) {
63          // Get state mapping for view from post data
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          // Get state object path from post data
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                  //Assume checking against all states that exist
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      * @see ViewValidationService#validateView(ViewModel, String)
116      */
117     @Override
118     public DictionaryValidationResult validateView(ViewModel model, String forcedValidationState) {
119         // Get state object path from post data
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         // Get state mapping for view from post data
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             //use forced selected state if passed in
149             validationState = forcedValidationState;
150         } else if (stateMapping != null) {
151             //default is current state
152             validationState = stateMapping.getCurrentState(object);
153 
154         }
155 
156         return getDictionaryValidationService().validate(new ViewAttributeValueReader(model), true,
157                 validationState, stateMapping);
158     }
159 
160     /**
161      * @see ViewValidationService#validateViewAgainstNextState(ViewModel)
162      */
163     @Override
164     public DictionaryValidationResult validateViewAgainstNextState(ViewModel model) {
165         // Get state object path from post data
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         // Get state mapping for view from post data
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             //validation state is the next state for this call
195             validationState = stateMapping.getNextState(object);
196         }
197         return getDictionaryValidationService().validate(new ViewAttributeValueReader(model), true,
198                 validationState, stateMapping);
199     }
200 
201     /**
202      * Gets the DictionaryValidationService to use for View validation
203      *
204      * @return DictionaryValidationService
205      */
206     public DictionaryValidationService getDictionaryValidationService() {
207         if (dictionaryValidationService == null) {
208             this.dictionaryValidationService = KRADServiceLocatorWeb.getDictionaryValidationService();
209         }
210         return dictionaryValidationService;
211     }
212 
213     /**
214      * Set the DictionaryValidationService
215      *
216      * @param dictionaryValidationService
217      */
218     public void setDictionaryValidationService(DictionaryValidationService dictionaryValidationService) {
219         this.dictionaryValidationService = dictionaryValidationService;
220     }
221 }