1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.remote.impl.mojo;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import org.kuali.student.contract.model.Service;
21
22 import org.kuali.student.contract.model.ServiceContractModel;
23 import org.kuali.student.contract.model.ServiceMethod;
24 import org.kuali.student.contract.model.ServiceMethodError;
25 import org.kuali.student.contract.model.ServiceMethodParameter;
26 import org.kuali.student.contract.model.XmlType;
27 import org.kuali.student.contract.model.util.ModelFinder;
28 import org.kuali.student.contract.writer.JavaClassWriter;
29 import org.kuali.student.contract.writer.service.GetterSetterNameCalculator;
30 import org.kuali.student.contract.writer.service.MessageStructureTypeCalculator;
31 import org.kuali.student.contract.writer.service.ServiceExceptionWriter;
32
33
34
35
36
37 public class RemoteImplServiceWriter extends JavaClassWriter {
38
39 private ServiceContractModel model;
40 private ModelFinder finder;
41 private String directory;
42 private String rootPackage;
43 private String servKey;
44 private List<ServiceMethod> methods;
45
46 public RemoteImplServiceWriter(ServiceContractModel model,
47 String directory,
48 String rootPackage,
49 String servKey,
50 List<ServiceMethod> methods) {
51 super(directory + "/main/java", calcPackage(servKey, rootPackage), calcClassName(servKey));
52 this.model = model;
53 this.finder = new ModelFinder(model);
54 this.directory = directory;
55 this.rootPackage = rootPackage;
56 this.servKey = servKey;
57 this.methods = methods;
58 }
59
60 public static String calcPackage(String servKey, String rootPackage) {
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 return rootPackage;
81 }
82
83 public static String calcClassName(String servKey) {
84 String name = GetterSetterNameCalculator.calcInitUpper(servKey + "ServiceRemoteImpl");
85 if (name.startsWith("RICE.")) {
86 name = name.substring("RICE.".length());
87 }
88 return name;
89 }
90
91 public static String calcDecoratorClassName(String servKey) {
92 String name = GetterSetterNameCalculator.calcInitUpper(servKey + "ServiceDecorator");
93 if (name.startsWith("RICE.")) {
94 name = name.substring("RICE.".length());
95 }
96 return name;
97 }
98
99 private static enum MethodType {
100
101 VALIDATE, CREATE, UPDATE
102 };
103
104 private MethodType calcMethodType(ServiceMethod method) {
105 if (method.getName().startsWith("validate")) {
106 return MethodType.VALIDATE;
107 }
108 if (method.getName().startsWith("create")) {
109 return MethodType.CREATE;
110 }
111 if (method.getName().startsWith("update")) {
112 return MethodType.UPDATE;
113 }
114 return null;
115 }
116
117
118
119
120
121
122 public void write() {
123 indentPrint("public class " + calcClassName(servKey));
124 println(" extends " + calcDecoratorClassName(servKey));
125
126 openBrace();
127 writeHostUrlGetterSetter();
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205 closeBrace();
206
207 this.writeJavaClassAndImportsOutToFile();
208 this.getOut().close();
209 }
210
211 private String calcType(String type, String realType) {
212 XmlType t = finder.findXmlType(this.stripList(type));
213 return MessageStructureTypeCalculator.calculate(this, model, type, realType,
214 t.getJavaPackage());
215 }
216
217 private String stripList(String str) {
218 return GetterSetterNameCalculator.stripList(str);
219 }
220
221 private String calcExceptionClassName(ServiceMethodError error) {
222 if (error.getClassName() == null) {
223 return ServiceExceptionWriter.calcClassName(error.getType());
224 }
225 return error.getClassName();
226 }
227
228 private String calcExceptionPackageName(ServiceMethodError error) {
229 if (error.getClassName() == null) {
230 return ServiceExceptionWriter.calcPackage(rootPackage);
231 }
232 return error.getPackageName();
233 }
234
235 private void writeHostUrlGetterSetter() {
236
237 String initUpper = GetterSetterNameCalculator.calcInitUpper(servKey);
238 if (initUpper.startsWith("RICE.")) {
239 initUpper = initUpper.substring("RICE.".length());
240 }
241
242 indentPrintln("private String hostUrl;");
243 println("");
244 indentPrintln("public String getHostUrl()");
245 openBrace();
246 indentPrintln("return hostUrl;");
247 closeBrace();
248 indentPrintln("public void setHostUrl(String hostUrl)");
249 openBrace();
250 indentPrintln("this.hostUrl = hostUrl;");
251 indentPrintln("if (hostUrl == null)");
252 openBrace();
253 indentPrintln("this.setNextDecorator(null);");
254 indentPrintln("return;");
255 closeBrace();
256 indentPrintln("URL wsdlURL;");
257 indentPrintln("try");
258 openBrace();
259 String constantsFile = initUpper + "ServiceConstants";
260
261 indentPrintln("String urlStr = hostUrl + \"/services/\" + " + constantsFile + ".SERVICE_NAME_LOCAL_PART + \"?wsdl\";");
262 indentPrintln("wsdlURL = new URL(urlStr);");
263 closeBrace();
264 indentPrintln("catch (MalformedURLException ex)");
265 openBrace();
266 indentPrintln("throw new IllegalArgumentException(ex);");
267 closeBrace();
268
269 importsAdd("java.net.MalformedURLException");
270 importsAdd("java.net.URL");
271 importsAdd("javax.xml.namespace.QName");
272 importsAdd("javax.xml.ws.Service");
273 Service service = finder.findService(servKey);
274 importsAdd(service.getImplProject() + "." + service.getName());
275
276 indentPrintln("QName qname = new QName(" + constantsFile + ".NAMESPACE, " + constantsFile + ".SERVICE_NAME_LOCAL_PART);");
277 indentPrintln("Service factory = Service.create(wsdlURL, qname);");
278 indentPrintln(service.getName() + " port = factory.getPort(" + service.getName() + ".class);");
279 indentPrintln("this.setNextDecorator(port);");
280 closeBrace();
281
282 }
283 }