1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
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  
31  
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          
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  }