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 | |
|
36 | |
private ServiceMethod method; |
37 | |
private ServiceContractModel model; |
38 | |
|
39 | |
public ServiceMethodValidator (ServiceMethod method, ServiceContractModel model) |
40 | 0 | { |
41 | 0 | this.method = method; |
42 | 0 | this.model = model; |
43 | 0 | } |
44 | |
|
45 | |
private Collection errors; |
46 | |
|
47 | |
@Override |
48 | |
public Collection<String> validate () |
49 | |
{ |
50 | 0 | errors = new ArrayList (); |
51 | 0 | basicValidation (); |
52 | 0 | for (ServiceMethodParameter param : method.getParameters ()) |
53 | |
{ |
54 | 0 | errors.addAll (new ServiceMethodParameterValidator (param, method).validate ()); |
55 | |
} |
56 | 0 | errors.addAll (new ServiceMethodReturnValueValidator (method.getReturnValue (), method). |
57 | |
validate ()); |
58 | 0 | for (ServiceMethodError param : method.getErrors ()) |
59 | |
{ |
60 | 0 | errors.addAll (new ServiceMethodErrorValidator (param, method).validate ()); |
61 | |
} |
62 | 0 | return errors; |
63 | |
} |
64 | |
|
65 | |
private void basicValidation () |
66 | |
{ |
67 | 0 | if (method.getService ().equals ("")) |
68 | |
{ |
69 | 0 | addError ("Service is required"); |
70 | |
} |
71 | |
else |
72 | |
{ |
73 | 0 | if (findService (method.getService ()) == null) |
74 | |
{ |
75 | 0 | addError ("Service, [" + method.getService () + |
76 | |
"] could not be found in the list of services"); |
77 | |
} |
78 | |
} |
79 | 0 | if (method.getName ().equals ("")) |
80 | |
{ |
81 | 0 | addError ("Name is required"); |
82 | |
} |
83 | 0 | if (method.getDescription ().equals ("")) |
84 | |
{ |
85 | 0 | addError ("Description is required"); |
86 | |
} |
87 | 0 | if (method.getReturnValue () == null) |
88 | |
{ |
89 | 0 | addError ("Return value is required"); |
90 | |
} |
91 | 0 | } |
92 | |
|
93 | |
private Service findService (String service) |
94 | |
{ |
95 | |
|
96 | 0 | if ( ! (model instanceof ServiceContractModel)) |
97 | |
{ |
98 | 0 | return null; |
99 | |
} |
100 | 0 | return new ModelFinder (model).findService (service); |
101 | |
} |
102 | |
|
103 | |
private void addError (String msg) |
104 | |
{ |
105 | 0 | String error = "Error in service method: " + method.getService () + "." + |
106 | |
method.getName () + ": " + msg; |
107 | 0 | if ( ! errors.contains (error)) |
108 | |
{ |
109 | 0 | errors.add (error); |
110 | |
} |
111 | 0 | } |
112 | |
|
113 | |
} |