View Javadoc

1   /**
2    * Copyright 2005-2012 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.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   * Implementation of Validation service for views, uses the same validation mechanisms as DictionaryValidationService
43   * but uses a different AttributeValueReader to get the correct information from InputFields - which
44   * include any AttributeDefinition defined attributes, if defined and not overriden
45   *
46   * @see ViewValidationService
47   */
48  public class ViewValidationServiceImpl implements ViewValidationService {
49  
50      protected DictionaryValidationService dictionaryValidationService;
51  
52      /**
53       * @see ViewValidationService#validateView(org.kuali.rice.krad.uif.view.ViewModel)
54       */
55      @Override
56      public DictionaryValidationResult validateView(ViewModel model) {
57          return validateView(model.getPostedView(), model);
58      }
59  
60      /**
61       * @see ViewValidationService#validateView(View, ViewModel)
62       */
63      @Override
64      public DictionaryValidationResult validateView(View view, ViewModel model) {
65          return validateView(view, model, null);
66      }
67  
68      /**
69           * @see ViewValidationService#validateViewSimulation(View, ViewModel)
70           */
71      @Override
72      public void validateViewSimulation(View view, ViewModel model){
73          validateViewSimulation(view, model, null);    
74      }
75  
76      /**
77           * @see ViewValidationService#validateViewSimulation(View, ViewModel, String)
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                  //Assume checking against all states that exist
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          * @see ViewValidationService#validateView(View, ViewModel, String)
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             //use forced selected state if passed in
139             validationState = forcedValidationState;
140         }
141         else if(stateMapping != null){
142             //default is current state
143             validationState = stateMapping.getCurrentState(object);
144 
145         }
146 
147         return getDictionaryValidationService().validate(new ViewAttributeValueReader(view, model), true,
148                 validationState, stateMapping);
149     }
150 
151     /**
152          * @see ViewValidationService#validateViewAgainstNextState(View, ViewModel)
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             //validation state is the next state for this call
172             validationState = stateMapping.getNextState(object);
173         }
174         return getDictionaryValidationService().validate(new ViewAttributeValueReader(view, model), true,
175                 validationState, stateMapping);
176     }
177 
178     /**
179      * Gets the DictionaryValidationService to use for View validation
180      *
181      * @return DictionaryValidationService
182      */
183     public DictionaryValidationService getDictionaryValidationService() {
184         if (dictionaryValidationService == null) {
185             this.dictionaryValidationService = KRADServiceLocatorWeb.getDictionaryValidationService();
186         }
187         return dictionaryValidationService;
188     }
189 
190     /**
191      * Set the DictionaryValidationService
192      *
193      * @param dictionaryValidationService
194      */
195     public void setDictionaryValidationService(DictionaryValidationService dictionaryValidationService) {
196         this.dictionaryValidationService = dictionaryValidationService;
197     }
198 }