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.io.Serializable; |
19 | |
import java.util.List; |
20 | |
|
21 | |
import org.kuali.student.contract.exception.DictionaryExecutionException; |
22 | |
import org.kuali.student.contract.model.MessageStructure; |
23 | |
import org.kuali.student.contract.model.ServiceContractModel; |
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 PureJavaInfcBeanWriter extends JavaClassWriter |
33 | |
{ |
34 | |
|
35 | |
private ServiceContractModel model; |
36 | |
private String directory; |
37 | |
private String rootPackage; |
38 | |
private String service; |
39 | |
private XmlType type; |
40 | |
private ModelFinder finder; |
41 | |
|
42 | |
public PureJavaInfcBeanWriter (ServiceContractModel model, |
43 | |
String directory, |
44 | |
String rootPackage, |
45 | |
String service, |
46 | |
XmlType type) |
47 | |
{ |
48 | 0 | super (directory, calcPackage (service, rootPackage), calcClassName (type. |
49 | |
getName ())); |
50 | 0 | this.model = model; |
51 | 0 | this.finder = new ModelFinder (model); |
52 | 0 | this.directory = directory; |
53 | 0 | this.rootPackage = rootPackage; |
54 | 0 | this.service = service; |
55 | 0 | this.type = type; |
56 | 0 | } |
57 | |
|
58 | |
public static String calcPackage (String service, String rootPackage) |
59 | |
{ |
60 | 0 | if (service.contains (",")) |
61 | |
{ |
62 | 0 | service = "common"; |
63 | |
} |
64 | 0 | return PureJavaInfcServiceWriter.calcPackage (service, rootPackage); |
65 | |
} |
66 | |
|
67 | |
public static String calcClassName (String name) |
68 | |
{ |
69 | 0 | if (name.endsWith ("Info")) |
70 | |
{ |
71 | 0 | name = name.substring (0, name.length () - "Info".length ()); |
72 | |
} |
73 | 0 | name = name + "Bean"; |
74 | 0 | return GetterSetterNameCalculator.calcInitUpper (name); |
75 | |
|
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public void write () |
83 | |
{ |
84 | 0 | indentPrintln ("public class " + calcClassName (type.getName ())); |
85 | 0 | incrementIndent (); |
86 | 0 | indentPrint (" implements " |
87 | |
+ PureJavaInfcInfcWriter.calcClassName (type.getName ())); |
88 | 0 | importsAdd (PureJavaInfcInfcWriter.calcPackage (service, rootPackage) |
89 | |
+ "." + PureJavaInfcInfcWriter.calcClassName (type.getName ())); |
90 | 0 | this.importsAdd (Serializable.class.getName ()); |
91 | 0 | indentPrintln (", Serializable"); |
92 | 0 | openBrace (); |
93 | |
|
94 | 0 | indentPrintln (""); |
95 | 0 | indentPrintln ("private static final long serialVersionUID = 1L;"); |
96 | |
|
97 | 0 | List<MessageStructure> list = |
98 | |
finder.findMessageStructures (type.getName ()); |
99 | 0 | if (list.size () == 0) |
100 | |
{ |
101 | 0 | throw new DictionaryExecutionException ("xmlType " + type.getName () |
102 | |
+ " has no fields defined in the message structure tab"); |
103 | |
} |
104 | 0 | for (MessageStructure ms : list) |
105 | |
{ |
106 | 0 | String realType = stripList (PureJavaInfcInfcWriter.calcClassName (ms.getType ())); |
107 | 0 | String fieldType = this.calcFieldTypeToUse (ms.getType (), realType); |
108 | 0 | String name = initLower (ms.getShortName ()); |
109 | 0 | indentPrintln (""); |
110 | 0 | indentPrintln ("private " + fieldType + " " + name + ";"); |
111 | 0 | indentPrintln (""); |
112 | 0 | indentPrintln ("/**"); |
113 | 0 | indentPrintWrappedComment ("Set " + ms.getName ()); |
114 | 0 | indentPrintln ("*"); |
115 | 0 | indentPrintln ("* Type: " + ms.getType ()); |
116 | 0 | indentPrintln ("*"); |
117 | 0 | indentPrintWrappedComment (ms.getDescription ()); |
118 | 0 | indentPrintln ("*/"); |
119 | 0 | indentPrintln ("@Override"); |
120 | 0 | indentPrintln ("public void " + calcSetter (ms) + "(" + fieldType + " " |
121 | |
+ name + ")"); |
122 | 0 | openBrace (); |
123 | 0 | indentPrintln ("this." + name + " = " + name + ";"); |
124 | 0 | closeBrace (); |
125 | |
|
126 | 0 | indentPrintln (""); |
127 | 0 | indentPrintln ("/**"); |
128 | 0 | indentPrintWrappedComment ("Get " + ms.getName ()); |
129 | 0 | indentPrintln ("*"); |
130 | 0 | indentPrintln ("* Type: " + ms.getType ()); |
131 | 0 | indentPrintln ("*"); |
132 | 0 | indentPrintWrappedComment (ms.getDescription ()); |
133 | 0 | indentPrintln ("*/"); |
134 | 0 | indentPrintln ("@Override"); |
135 | 0 | indentPrintln ("public " + fieldType + " " + calcGetter (ms) + "()"); |
136 | 0 | openBrace (); |
137 | 0 | indentPrintln ("return this." + name + ";"); |
138 | 0 | closeBrace (); |
139 | 0 | indentPrint (""); |
140 | |
|
141 | 0 | indentPrint (""); |
142 | 0 | } |
143 | 0 | indentPrintln (""); |
144 | 0 | closeBrace (); |
145 | |
|
146 | 0 | this.writeJavaClassAndImportsOutToFile (); |
147 | 0 | this.getOut ().close (); |
148 | 0 | } |
149 | |
|
150 | |
private String stripList (String str) |
151 | |
{ |
152 | 0 | return GetterSetterNameCalculator.stripList (str); |
153 | |
} |
154 | |
|
155 | |
private String initLower (String str) |
156 | |
{ |
157 | 0 | if (str == null) |
158 | |
{ |
159 | 0 | return null; |
160 | |
} |
161 | 0 | if (str.isEmpty ()) |
162 | |
{ |
163 | 0 | return str; |
164 | |
} |
165 | 0 | if (str.length () == 1) |
166 | |
{ |
167 | 0 | return str.toLowerCase (); |
168 | |
} |
169 | 0 | return str.substring (0, 1).toLowerCase () + str.substring (1); |
170 | |
} |
171 | |
|
172 | |
|
173 | |
private String calcGetter (MessageStructure ms) |
174 | |
{ |
175 | 0 | return new GetterSetterNameCalculator (ms, this, model).calcGetter (); |
176 | |
} |
177 | |
|
178 | |
private String calcSetter (MessageStructure ms) |
179 | |
{ |
180 | 0 | return new GetterSetterNameCalculator (ms, this, model).calcSetter (); |
181 | |
} |
182 | |
|
183 | |
private String calcFieldTypeToUse (String type, String realType) |
184 | |
{ |
185 | 0 | XmlType t = finder.findXmlType (this.stripList (type)); |
186 | 0 | String pckName = calcPackage (t.getService (), rootPackage); |
187 | 0 | return MessageStructureTypeCalculator.calculate (this, model, type, realType, |
188 | |
pckName); |
189 | |
} |
190 | |
|
191 | |
} |