View Javadoc

1   /*
2    * Copyright 2009 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.osedu.org/licenses/ECL-2.0
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.student.contract.model.validation;
17  
18  import org.kuali.student.contract.model.DictionaryModel;
19  import org.kuali.student.contract.model.Service;
20  import org.kuali.student.contract.model.ServiceContractModel;
21  import org.kuali.student.contract.model.ServiceMethod;
22  import org.kuali.student.contract.model.ServiceMethodError;
23  import org.kuali.student.contract.model.ServiceMethodParameter;
24  import org.kuali.student.contract.model.util.ModelFinder;
25  
26  import java.util.ArrayList;
27  import java.util.Collection;
28  
29  /**
30   * This validates a single serviceMethodinoary entry
31   * @author nwright
32   */
33  public class ServiceMethodValidator implements ModelValidator {
34  
35      private ServiceMethod method;
36      private ServiceContractModel model;
37  
38      public ServiceMethodValidator(ServiceMethod method, ServiceContractModel model) {
39          this.method = method;
40          this.model = model;
41      }
42      private Collection errors;
43  
44      @Override
45      public Collection<String> validate() {
46          errors = new ArrayList();
47          basicValidation();
48          for (ServiceMethodParameter param : method.getParameters()) {
49              errors.addAll(new ServiceMethodParameterValidator(param, method).validate());
50          }
51          errors.addAll(new ServiceMethodReturnValueValidator(method.getReturnValue(), method).validate());
52          for (ServiceMethodError param : method.getErrors()) {
53              errors.addAll(new ServiceMethodErrorValidator(param, method).validate());
54          }
55          return errors;
56      }
57  
58      private void basicValidation() {
59          if (method.getService().equals("")) {
60              addError("Service is required");
61          } else {
62              if (findService(method.getService()) == null) {
63                  addError("Service, [" + method.getService()
64                          + "] could not be found in the list of services");
65              }
66          }
67          if (method.getName().equals("")) {
68              addError("Name is required");
69          }
70          if (method.getDescription().equals("")) {
71              addError("Description is required");
72          }
73          if (method.getReturnValue() == null) {
74              addError("Return value is required");
75          }
76      }
77  
78      private Service findService(String service) {
79          // if we are only working with the searchModel then can't validate service
80          if (!(model instanceof ServiceContractModel)) {
81              return null;
82          }
83          return new ModelFinder(model).findService(service);
84      }
85  
86      private void addError(String msg) {
87          String error = "Error in service method: " + method.getService() + "."
88                  + method.getName() + ": " + msg;
89          if (!errors.contains(error)) {
90              errors.add(error);
91          }
92      }
93  }