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.List; |
19 | |
|
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.XmlType; |
25 | |
import org.kuali.student.contract.model.util.ModelFinder; |
26 | |
import org.kuali.student.contract.writer.JavaClassWriter; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public class PureJavaInfcServiceWriter extends JavaClassWriter |
33 | |
{ |
34 | |
|
35 | |
private ServiceContractModel model; |
36 | |
private ModelFinder finder; |
37 | |
private String directory; |
38 | |
private String rootPackage; |
39 | |
private String servKey; |
40 | |
private List<ServiceMethod> methods; |
41 | |
|
42 | |
public PureJavaInfcServiceWriter (ServiceContractModel model, |
43 | |
String directory, |
44 | |
String rootPackage, |
45 | |
String servKey, |
46 | |
List<ServiceMethod> methods) |
47 | |
{ |
48 | 0 | super (directory, calcPackage (servKey, rootPackage), calcClassName (servKey)); |
49 | 0 | this.model = model; |
50 | 0 | this.finder = new ModelFinder (model); |
51 | 0 | this.directory = directory; |
52 | 0 | this.rootPackage = rootPackage; |
53 | 0 | this.servKey = servKey; |
54 | 0 | this.methods = methods; |
55 | 0 | } |
56 | |
|
57 | |
public static String calcPackage (String servKey, String rootPackage) |
58 | |
{ |
59 | 0 | String pack = rootPackage + "." + servKey.toLowerCase () + "."; |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | 0 | pack = pack + "api"; |
77 | 0 | return pack; |
78 | |
} |
79 | |
|
80 | |
public static String calcClassName (String servKey) |
81 | |
{ |
82 | 0 | return GetterSetterNameCalculator.calcInitUpper (servKey + "ServiceInfc"); |
83 | |
} |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public void write () |
90 | |
{ |
91 | 0 | indentPrintln ("public interface " + calcClassName (servKey)); |
92 | 0 | openBrace (); |
93 | |
|
94 | 0 | for (ServiceMethod method : methods) |
95 | |
{ |
96 | 0 | indentPrintln (""); |
97 | 0 | indentPrintln ("/**"); |
98 | 0 | indentPrintWrappedComment (method.getDescription ()); |
99 | 0 | indentPrintln ("* "); |
100 | 0 | for (ServiceMethodParameter param : method.getParameters ()) |
101 | |
{ |
102 | 0 | indentPrintWrappedComment ("@param " + param.getName () + " - " |
103 | |
+ param.getType () + " - " |
104 | |
+ param.getDescription ()); |
105 | |
} |
106 | 0 | indentPrintWrappedComment ("@return " + method.getReturnValue (). |
107 | |
getDescription ()); |
108 | 0 | indentPrintln ("*/"); |
109 | 0 | String type = method.getReturnValue ().getType (); |
110 | 0 | String realType = stripList (PureJavaInfcInfcWriter.calcClassName (type)); |
111 | 0 | indentPrint ("public " + calcType (type, realType) + " " + method.getName () |
112 | |
+ "("); |
113 | |
|
114 | 0 | String comma = ""; |
115 | 0 | for (ServiceMethodParameter param : method.getParameters ()) |
116 | |
{ |
117 | 0 | type = param.getType (); |
118 | 0 | realType = stripList (PureJavaInfcInfcWriter.calcClassName (type)); |
119 | 0 | print (comma); |
120 | 0 | print (calcType (type, realType)); |
121 | 0 | print (" "); |
122 | 0 | print (param.getName ()); |
123 | 0 | comma = ", "; |
124 | |
} |
125 | 0 | println (")"); |
126 | |
|
127 | 0 | comma = "throws "; |
128 | 0 | incrementIndent (); |
129 | 0 | for (ServiceMethodError error : method.getErrors ()) |
130 | |
{ |
131 | 0 | indentPrint (comma); |
132 | 0 | String exceptionClassName = calcExceptionClassName (error); |
133 | 0 | String exceptionPackageName = this.calcExceptionPackageName (error); |
134 | 0 | println (exceptionClassName); |
135 | 0 | this.importsAdd (exceptionPackageName + "." + exceptionClassName); |
136 | 0 | comma = " ,"; |
137 | 0 | } |
138 | 0 | decrementIndent (); |
139 | 0 | indentPrintln (";"); |
140 | |
|
141 | 0 | } |
142 | |
|
143 | 0 | closeBrace (); |
144 | |
|
145 | 0 | this.writeJavaClassAndImportsOutToFile (); |
146 | 0 | this.getOut ().close (); |
147 | 0 | } |
148 | |
|
149 | |
private String stripList (String str) |
150 | |
{ |
151 | 0 | return GetterSetterNameCalculator.stripList (str); |
152 | |
} |
153 | |
|
154 | |
private String calcExceptionClassName (ServiceMethodError error) |
155 | |
{ |
156 | 0 | if (error.getClassName () == null) |
157 | |
{ |
158 | 0 | return ServiceExceptionWriter.calcClassName (error.getType ()); |
159 | |
} |
160 | 0 | return error.getClassName (); |
161 | |
} |
162 | |
|
163 | |
private String calcExceptionPackageName (ServiceMethodError error) |
164 | |
{ |
165 | 0 | if (error.getClassName () == null) |
166 | |
{ |
167 | 0 | return ServiceExceptionWriter.calcPackage (rootPackage); |
168 | |
} |
169 | 0 | return error.getPackageName (); |
170 | |
} |
171 | |
|
172 | |
private String calcType (String type, String realType) |
173 | |
{ |
174 | 0 | XmlType t = finder.findXmlType (this.stripList (type)); |
175 | 0 | String pckName = PureJavaInfcInfcWriter.calcPackage (t.getService (), rootPackage); |
176 | 0 | return MessageStructureTypeCalculator.calculate (this, model, type, realType, |
177 | |
pckName); |
178 | |
} |
179 | |
} |