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