View Javadoc

1   /**
2    * Copyright 2005-2013 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.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   * Implementation of Validation service for views, uses the same validation mechanisms as DictionaryValidationService
32   * but uses a different AttributeValueReader to get the correct information from InputFields - which
33   * include any AttributeDefinition defined attributes, if defined and not overriden
34   *
35   * @see ViewValidationService
36   */
37  public class ViewValidationServiceImpl implements ViewValidationService {
38  
39      protected DictionaryValidationService dictionaryValidationService;
40  
41      /**
42       * @see ViewValidationService#validateView(org.kuali.rice.krad.uif.view.ViewModel)
43       */
44      @Override
45      public DictionaryValidationResult validateView(ViewModel model) {
46          return validateView(model.getPostedView(), model);
47      }
48  
49      /**
50       * @see ViewValidationService#validateView(View, ViewModel)
51       */
52      @Override
53      public DictionaryValidationResult validateView(View view, ViewModel model) {
54          return validateView(view, model, null);
55      }
56  
57      /**
58       * @see ViewValidationService#validateViewSimulation(View, ViewModel)
59       */
60      @Override
61      public void validateViewSimulation(View view, ViewModel model) {
62          validateViewSimulation(view, model, null);
63      }
64  
65      /**
66       * @see ViewValidationService#validateViewSimulation(View, ViewModel, String)
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                  //Assume checking against all states that exist
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      * @see ViewValidationService#validateView(View, ViewModel, String)
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             //use forced selected state if passed in
124             validationState = forcedValidationState;
125         } else if (stateMapping != null) {
126             //default is current state
127             validationState = stateMapping.getCurrentState(object);
128 
129         }
130 
131         return getDictionaryValidationService().validate(new ViewAttributeValueReader(view, model), true,
132                 validationState, stateMapping);
133     }
134 
135     /**
136      * @see ViewValidationService#validateViewAgainstNextState(View, ViewModel)
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             //validation state is the next state for this call
155             validationState = stateMapping.getNextState(object);
156         }
157         return getDictionaryValidationService().validate(new ViewAttributeValueReader(view, model), true,
158                 validationState, stateMapping);
159     }
160 
161     /**
162      * Gets the DictionaryValidationService to use for View validation
163      *
164      * @return DictionaryValidationService
165      */
166     public DictionaryValidationService getDictionaryValidationService() {
167         if (dictionaryValidationService == null) {
168             this.dictionaryValidationService = KRADServiceLocatorWeb.getDictionaryValidationService();
169         }
170         return dictionaryValidationService;
171     }
172 
173     /**
174      * Set the DictionaryValidationService
175      *
176      * @param dictionaryValidationService
177      */
178     public void setDictionaryValidationService(DictionaryValidationService dictionaryValidationService) {
179         this.dictionaryValidationService = dictionaryValidationService;
180     }
181 }