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