1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.admin.ui.mojo;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import javax.xml.namespace.QName;
23 import org.kuali.rice.core.api.resourceloader.GlobalResourceLoader;
24 import org.kuali.student.contract.model.MessageStructure;
25 import org.kuali.student.contract.model.Service;
26
27 import org.kuali.student.contract.model.ServiceContractModel;
28 import org.kuali.student.contract.model.ServiceMethod;
29 import org.kuali.student.contract.model.XmlType;
30 import org.kuali.student.contract.model.util.ModelFinder;
31 import org.kuali.student.contract.writer.JavaClassWriter;
32 import org.kuali.student.contract.writer.service.GetterSetterNameCalculator;
33
34
35
36
37
38 public class AdminUiInquirableWriter extends JavaClassWriter {
39
40 private ServiceContractModel model;
41 private ModelFinder finder;
42 private String directory;
43 private String rootPackage;
44 private String servKey;
45 private Service service;
46 private XmlType xmlType;
47 private List<ServiceMethod> methods;
48
49 public AdminUiInquirableWriter(ServiceContractModel model,
50 String directory,
51 String rootPackage,
52 String servKey,
53 XmlType xmlType,
54 List<ServiceMethod> methods) {
55 super(directory + "/" + "java", calcPackage(servKey, rootPackage, xmlType), calcClassName(servKey, xmlType));
56 this.model = model;
57 this.finder = new ModelFinder(model);
58 this.directory = directory;
59 this.rootPackage = rootPackage;
60 this.servKey = servKey;
61 service = finder.findService(servKey);
62 this.xmlType = xmlType;
63 this.methods = methods;
64 }
65
66 public static String calcPackage(String servKey, String rootPackage, XmlType xmlType) {
67 String pack = rootPackage + "." + servKey.toLowerCase();
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 return pack;
86
87 }
88
89 public static String calcClassName(String servKey, String xmlTypeName) {
90 return GetterSetterNameCalculator.calcInitUpper(xmlTypeName) + "AdminInquirableImpl";
91 }
92
93 public static String calcClassName(String servKey, XmlType xmlType) {
94 return calcClassName(servKey, xmlType.getName());
95 }
96
97 public static String calcDecoratorClassName(String servKey) {
98 return GetterSetterNameCalculator.calcInitUpper(servKey + "ServiceDecorator");
99 }
100
101 private static enum MethodType {
102
103 VALIDATE, CREATE, UPDATE
104 };
105
106 private MethodType calcMethodType(ServiceMethod method) {
107 if (method.getName().startsWith("validate")) {
108 return MethodType.VALIDATE;
109 }
110 if (method.getName().startsWith("create")) {
111 return MethodType.CREATE;
112 }
113 if (method.getName().startsWith("update")) {
114 return MethodType.UPDATE;
115 }
116 return null;
117 }
118
119
120
121
122
123
124 public void write() {
125 indentPrint("public class " + calcClassName(servKey, xmlType));
126 println(" extends InquirableImpl");
127 importsAdd("org.kuali.rice.krad.inquiry.InquirableImpl");
128 openBrace();
129 writeLogic();
130 closeBrace();
131
132 this.writeJavaClassAndImportsOutToFile();
133 this.getOut().close();
134 }
135
136 private void writeLogic() {
137
138 String initUpper = GetterSetterNameCalculator.calcInitUpper(servKey);
139 String initLower = GetterSetterNameCalculator.calcInitLower(servKey);
140 String infoClass = GetterSetterNameCalculator.calcInitUpper(xmlType.getName());
141 String serviceClass = GetterSetterNameCalculator.calcInitUpper(service.getName());
142 String serviceVar = GetterSetterNameCalculator.calcInitLower(service.getName());
143 importsAdd(service.getImplProject() + "." + service.getName());
144 importsAdd("org.apache.log4j.Logger");
145 indentPrintln("private static final Logger LOG = Logger.getLogger(" + calcClassName(servKey, xmlType) + ".class);");
146 indentPrintln("private transient " + serviceClass + " " + serviceVar + ";");
147 MessageStructure pk = this.getPrimaryKey(xmlType.getName());
148 indentPrintln("private final static String PRIMARY_KEY = \"" + pk.getShortName() + "\";");
149
150 println("");
151 indentPrintln("@Override");
152 importsAdd(xmlType.getJavaPackage() + "." + infoClass);
153 importsAdd(List.class.getName());
154 importsAdd(Map.class.getName());
155
156
157
158
159 importsAdd(GlobalResourceLoader.class.getName());
160 XmlType contextInfo = finder.findXmlType("contextInfo");
161 importsAdd(contextInfo.getJavaPackage() + "." + contextInfo.getName());
162 importsAdd("org.kuali.student.enrollment.common.util.ContextBuilder");
163
164 importsAdd(ArrayList.class.getName());
165 importsAdd(QName.class.getName());
166
167 indentPrintln("public " + infoClass + " retrieveDataObject(Map<String, String> parameters)");
168 openBrace();
169 indentPrintln("String key = parameters.get(PRIMARY_KEY);");
170 indentPrintln("try");
171 openBrace();
172 String getMethod = calcGetMethod();
173 if (getMethod == null) {
174 indentPrintln("// WARNING: Missing get method please add it to the service contract: " + servKey + "." + xmlType.getName());
175 getMethod = "get" + GetterSetterNameCalculator.calcInitUpper(xmlType.getName());
176 }
177 indentPrintln("" + infoClass + " info = this.get" + serviceClass + "()." + getMethod + "(key, getContextInfo());");
178 indentPrintln("return info;");
179 closeBrace();
180 indentPrintln("catch (Exception ex) {");
181 indentPrintln(" throw new RuntimeException(ex);");
182 indentPrintln("}");
183 closeBrace();
184 writeServiceGetterAndSetter (this, serviceClass, serviceVar, xmlType);
185 }
186
187 private MessageStructure getPrimaryKey(String xmlTypeName) {
188 for (MessageStructure ms : finder.findMessageStructures(xmlTypeName)) {
189 if (ms.isPrimaryKey()) {
190 return ms;
191 }
192 }
193 return null;
194 }
195 public static void writeServiceGetterAndSetter (JavaClassWriter out, String serviceClass, String serviceVar, XmlType xmlType) {
196
197 out.println ("");
198 out.indentPrintln("public void set" + serviceClass + "(" + serviceClass + " " + serviceVar + ")");
199 out.openBrace();
200 out.indentPrintln(" this." + serviceVar + " = " + serviceVar + ";");
201 out.closeBrace();
202 out.println("");
203 out.indentPrintln("public " + serviceClass + " get" + serviceClass + "()");
204 out.openBrace();
205 out.indentPrintln("if (" + serviceVar + " == null)");
206 out.openBrace();
207 String serviceConstants = calcServiceContantsName(serviceClass);
208 out.importsAdd(calcServiceContantsPackage(xmlType) + "." + serviceConstants);
209 out.indentPrintln("QName qname = new QName(" + serviceConstants + ".NAMESPACE," + serviceConstants + ".SERVICE_NAME_LOCAL_PART);");
210 out.indentPrintln(serviceVar + " = (" + serviceClass + ") GlobalResourceLoader.getService(qname);");
211 out.closeBrace();
212 out.indentPrintln("return this." + serviceVar + ";");
213 out.closeBrace();
214 out.println("");
215 out.indentPrintln("private ContextInfo getContextInfo() {");
216 out.indentPrintln(" return ContextBuilder.loadContextInfo();");
217 out.indentPrintln("}");
218 }
219
220 public static String calcServiceContantsName(String serviceClass) {
221 if (serviceClass.equals("LRCService")) {
222 return "LrcServiceConstants";
223 }
224 return serviceClass + "Constants";
225 }
226 private static Map<String, String> SERVICE_CLASS_PACKAGE;
227
228 {
229 SERVICE_CLASS_PACKAGE = new HashMap<String, String>();
230 SERVICE_CLASS_PACKAGE.put("lum", "org.kuali.student.r2.lum.util.constants");
231 SERVICE_CLASS_PACKAGE.put("lum", "org.kuali.student.r2.lum.util.constants");
232 SERVICE_CLASS_PACKAGE.put("core", "org.kuali.student.r2.core.constants");
233 SERVICE_CLASS_PACKAGE.put("enrollment", "org.kuali.student.r2.common.util.constants");
234 }
235
236 public static String calcServiceContantsPackage(XmlType xmlType) {
237 if (xmlType.getJavaPackage().contains(".enrollment.")) {
238 return SERVICE_CLASS_PACKAGE.get("enrollment");
239 }
240 if (xmlType.getJavaPackage().contains(".core.")) {
241 return SERVICE_CLASS_PACKAGE.get("core");
242 }
243 if (xmlType.getJavaPackage().contains(".lum.")) {
244 return SERVICE_CLASS_PACKAGE.get("lum");
245 }
246 return "org.kuali.student.r2.common.util.constants";
247 }
248
249 private String calcGetMethod() {
250 ServiceMethod method = this.findGetMethod();
251 if (method != null) {
252 return method.getName();
253 }
254 return null;
255 }
256
257 private ServiceMethod findGetMethod() {
258 String infoClass = GetterSetterNameCalculator.calcInitUpper(xmlType.getName());
259 for (ServiceMethod method : methods) {
260 if (method.getName().startsWith("get")) {
261 if (method.getReturnValue().getType().equalsIgnoreCase(infoClass)) {
262 if (method.getParameters().size() == 2) {
263 if (method.getParameters().get(0).getType().equalsIgnoreCase("String")) {
264 return method;
265 }
266 }
267 }
268 }
269 }
270 return null;
271 }
272 }