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.HashSet;
19 import java.util.List;
20 import java.util.Set;
21
22 import org.kuali.student.contract.model.MessageStructure;
23 import org.kuali.student.contract.model.ServiceContractModel;
24 import org.kuali.student.contract.model.ServiceMethod;
25 import org.kuali.student.contract.model.ServiceMethodParameter;
26 import org.kuali.student.contract.model.ServiceMethodReturnValue;
27 import org.kuali.student.contract.model.XmlType;
28 import org.kuali.student.contract.model.util.ModelFinder;
29 import org.kuali.student.contract.model.validation.DictionaryValidationException;
30
31
32
33
34
35 public class PureJavaInfcWriterForOneService {
36
37 private ServiceContractModel model;
38 private ModelFinder finder;
39 private String directory;
40 private String rootPackage;
41 private String servKey;
42
43 public PureJavaInfcWriterForOneService(ServiceContractModel model,
44 String directory,
45 String rootPackage,
46 String servKey) {
47 this.model = model;
48 this.finder = new ModelFinder(model);
49 this.directory = directory;
50 this.rootPackage = rootPackage;
51 this.servKey = servKey;
52 }
53
54
55
56
57
58 public void write() {
59 List<ServiceMethod> methods = finder.getServiceMethodsInService(servKey);
60 if (methods.size() == 0) {
61 System.out.println("No methods defined for servKey: " + servKey);
62 return;
63 }
64
65
66 System.out.println("Generating servKeys API's for " + servKey);
67 new PureJavaInfcServiceWriter(model, directory, rootPackage, servKey, methods).write();
68
69
70 System.out.println("Generating info interfaces");
71 for (XmlType xmlType : getXmlTypesUsedJustByService()) {
72 System.out.println("Generating Beans for " + xmlType.getName());
73 new PureJavaInfcBeanWriter(model, directory, rootPackage, servKey, xmlType).write();
74 }
75
76
77 System.out.println("Generating Info interfaces");
78 for (XmlType xmlType : getXmlTypesUsedJustByService()) {
79 System.out.println("Generating info interface for " + xmlType.getName());
80 new PureJavaInfcInfcWriter(model, directory, rootPackage, servKey, xmlType).write();
81 }
82 }
83
84 private Set<XmlType> getXmlTypesUsedJustByService() {
85 Set<XmlType> set = new HashSet();
86 for (XmlType type : model.getXmlTypes()) {
87 if (type.getService().equalsIgnoreCase(servKey)) {
88 if (type.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
89 set.add(type);
90 }
91 }
92 }
93 return set;
94 }
95
96 private Set<XmlType> getXmlTypesUsedByService(List<ServiceMethod> methods) {
97 Set<XmlType> set = new HashSet();
98 for (ServiceMethod method : methods) {
99 if (method.getReturnValue() != null) {
100 ServiceMethodReturnValue ret = method.getReturnValue();
101 XmlType xmlType = finder.findXmlType(stripListFromType(ret.getType()));
102 if (xmlType == null) {
103 throw new DictionaryValidationException("Method " + method.getService()
104 + "." + method.getName()
105 + "returns an unknown type, "
106 + ret.getType());
107 }
108 addTypeAndAllSubTypes(set, xmlType);
109 }
110 for (ServiceMethodParameter param : method.getParameters()) {
111 XmlType xmlType = finder.findXmlType(stripListFromType(param.getType()));
112 if (xmlType == null) {
113 throw new DictionaryValidationException("Parameter "
114 + method.getService() + "."
115 + method.getName() + "."
116 + param.getName()
117 + "has an unknown type, "
118 + param.getType());
119 }
120 addTypeAndAllSubTypes(set, xmlType);
121 }
122 }
123 return set;
124 }
125
126 private void addTypeAndAllSubTypes(Set<XmlType> set, XmlType xmlType) {
127 if (xmlType.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
128 if (set.add(xmlType)) {
129 addXmlTypesUsedByMessageStructure(set, xmlType);
130 }
131 }
132 }
133
134 private String stripListFromType(String type) {
135 if (type.endsWith("List")) {
136 type = type.substring(0, type.length() - "List".length());
137 }
138 return type;
139 }
140
141 private void addXmlTypesUsedByMessageStructure(Set<XmlType> set,
142 XmlType xmlType) {
143 ModelFinder finder = new ModelFinder(model);
144 for (MessageStructure ms : finder.findMessageStructures(xmlType.getName())) {
145 XmlType subType = finder.findXmlType(stripListFromType(ms.getType()));
146 if (subType == null) {
147 throw new DictionaryValidationException("MessageStructure field "
148 + ms.getId()
149 + " has an unknown type, "
150 + ms.getType());
151 }
152 addTypeAndAllSubTypes(set, subType);
153 }
154 }
155 }