1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.contract.writer.service; |
17 | |
|
18 | |
import java.util.Collection; |
19 | |
import java.util.HashMap; |
20 | |
import java.util.HashSet; |
21 | |
import java.util.List; |
22 | |
import java.util.Map; |
23 | |
import java.util.Set; |
24 | |
|
25 | |
import org.kuali.student.contract.model.Service; |
26 | |
import org.kuali.student.contract.model.ServiceContractModel; |
27 | |
import org.kuali.student.contract.model.ServiceMethod; |
28 | |
import org.kuali.student.contract.model.ServiceMethodError; |
29 | |
import org.kuali.student.contract.model.XmlType; |
30 | |
import org.kuali.student.contract.model.util.ServicesFilter; |
31 | |
import org.kuali.student.contract.model.validation.DictionaryValidationException; |
32 | |
import org.kuali.student.contract.model.validation.ServiceContractModelValidator; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public class PureJavaInfcWriter |
39 | |
{ |
40 | |
|
41 | |
private ServiceContractModel model; |
42 | |
private String directory; |
43 | |
private String rootPackage; |
44 | |
public static final String DEFAULT_ROOT_PACKAGE = "org.kuali.student.service"; |
45 | |
private ServicesFilter filter; |
46 | |
|
47 | |
public PureJavaInfcWriter (ServiceContractModel model, |
48 | |
String directory, |
49 | |
String rootPackage, |
50 | |
ServicesFilter filter) |
51 | 0 | { |
52 | 0 | this.model = model; |
53 | 0 | this.directory = directory; |
54 | 0 | this.rootPackage = rootPackage; |
55 | 0 | this.filter = filter; |
56 | 0 | } |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
public void write () |
63 | |
{ |
64 | 0 | this.validate (); |
65 | |
|
66 | 0 | for (Service service : filterServices ()) |
67 | |
{ |
68 | 0 | new PureJavaInfcWriterForOneService (model, directory, rootPackage, service.getKey ()).write (); |
69 | |
} |
70 | |
|
71 | |
|
72 | 0 | System.out.println ("Generating common Info interfaces"); |
73 | 0 | for (XmlType xmlType : getXmlTypesUsedByMoreThanOneByService ()) |
74 | |
{ |
75 | 0 | System.out.println ("Generating info interface for " + xmlType.getName ()); |
76 | 0 | new PureJavaInfcInfcWriter (model, directory, rootPackage, xmlType.getService (), xmlType).write (); |
77 | 0 | new PureJavaInfcBeanWriter (model, directory, rootPackage, xmlType.getService (), xmlType).write (); |
78 | |
} |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | 0 | } |
90 | |
|
91 | |
private Set<XmlType> getXmlTypesUsedByMoreThanOneByService () |
92 | |
{ |
93 | 0 | Set<XmlType> set = new HashSet (); |
94 | 0 | for (XmlType type : model.getXmlTypes ()) |
95 | |
{ |
96 | 0 | if (type.getService ().contains (",")) |
97 | |
{ |
98 | 0 | if (type.getPrimitive ().equalsIgnoreCase (XmlType.COMPLEX)) |
99 | |
{ |
100 | 0 | System.out.println (type.getName () + "==>" + type.getService ()); |
101 | 0 | set.add (type); |
102 | |
} |
103 | |
} |
104 | |
} |
105 | 0 | return set; |
106 | |
} |
107 | |
|
108 | |
private Map<String, ServiceMethodError> getServiceMethodErrors () |
109 | |
{ |
110 | 0 | Map<String, ServiceMethodError> errors = new HashMap (); |
111 | 0 | for (ServiceMethod method : model.getServiceMethods ()) |
112 | |
{ |
113 | 0 | for (ServiceMethodError error : method.getErrors ()) |
114 | |
{ |
115 | 0 | errors.put (error.getType (), error); |
116 | |
} |
117 | |
} |
118 | 0 | return errors; |
119 | |
} |
120 | |
|
121 | |
private List<Service> filterServices () |
122 | |
{ |
123 | 0 | if (filter == null) |
124 | |
{ |
125 | 0 | return model.getServices (); |
126 | |
} |
127 | 0 | return filter.filter (model.getServices ()); |
128 | |
} |
129 | |
|
130 | |
private void validate () |
131 | |
{ |
132 | 0 | Collection<String> errors = |
133 | |
new ServiceContractModelValidator (model).validate (); |
134 | 0 | if (errors.size () > 0) |
135 | |
{ |
136 | 0 | StringBuffer buf = new StringBuffer (); |
137 | 0 | buf.append (errors.size () + " errors found while validating the data."); |
138 | 0 | int cnt = 0; |
139 | 0 | for (String msg : errors) |
140 | |
{ |
141 | 0 | cnt ++; |
142 | 0 | buf.append ("\n"); |
143 | 0 | buf.append ("*error*" + cnt + ":" + msg); |
144 | |
} |
145 | |
|
146 | 0 | throw new DictionaryValidationException (buf.toString ()); |
147 | |
} |
148 | 0 | } |
149 | |
} |