1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.remote.impl.mojo;
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.impl.ServiceContractModelPescXsdLoader;
29 import org.kuali.student.contract.model.util.ModelFinder;
30 import org.kuali.student.contract.model.validation.DictionaryValidationException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35
36
37
38 public class RemoteImplWriterForOneService {
39
40 private static Logger log = LoggerFactory.getLogger(RemoteImplWriterForOneService.class);
41
42 private ServiceContractModel model;
43 private ModelFinder finder;
44 private String directory;
45 private String rootPackage;
46 private String servKey;
47
48 public RemoteImplWriterForOneService(ServiceContractModel model,
49 String directory,
50 String rootPackage,
51 String servKey) {
52 this.model = model;
53 this.finder = new ModelFinder(model);
54 this.directory = directory;
55 this.rootPackage = rootPackage;
56 this.servKey = servKey;
57 }
58
59
60
61
62
63 public void write() {
64 List<ServiceMethod> methods = finder.getServiceMethodsInService(servKey);
65 if (methods.size() == 0) {
66 log.warn("No methods defined for servKey: " + servKey);
67 return;
68 }
69
70
71 log.info("Generating remote impl and search unit test for " + servKey + " directory = " + directory);
72 new RemoteImplServiceWriter(model, directory, rootPackage, servKey, methods).write();
73
74
75 }
76
77 private Set<XmlType> getXmlTypesUsedJustByService() {
78 Set<XmlType> set = new HashSet();
79 for (XmlType type : model.getXmlTypes()) {
80 if (type.getService().equalsIgnoreCase(servKey)) {
81 if (type.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
82 set.add(type);
83 }
84 }
85 }
86 return set;
87 }
88
89 private Set<XmlType> getXmlTypesUsedByService(List<ServiceMethod> methods) {
90 Set<XmlType> set = new HashSet();
91 for (ServiceMethod method : methods) {
92 if (method.getReturnValue() != null) {
93 ServiceMethodReturnValue ret = method.getReturnValue();
94 XmlType xmlType = finder.findXmlType(stripListFromType(ret.getType()));
95 if (xmlType == null) {
96 throw new DictionaryValidationException("Method " + method.getService()
97 + "." + method.getName()
98 + "returns an unknown type, "
99 + ret.getType());
100 }
101 addTypeAndAllSubTypes(set, xmlType);
102 }
103 for (ServiceMethodParameter param : method.getParameters()) {
104 XmlType xmlType = finder.findXmlType(stripListFromType(param.getType()));
105 if (xmlType == null) {
106 throw new DictionaryValidationException("Parameter "
107 + method.getService() + "."
108 + method.getName() + "."
109 + param.getName()
110 + "has an unknown type, "
111 + param.getType());
112 }
113 addTypeAndAllSubTypes(set, xmlType);
114 }
115 }
116 return set;
117 }
118
119 private void addTypeAndAllSubTypes(Set<XmlType> set, XmlType xmlType) {
120 if (xmlType.getPrimitive().equalsIgnoreCase(XmlType.COMPLEX)) {
121 if (set.add(xmlType)) {
122 addXmlTypesUsedByMessageStructure(set, xmlType);
123 }
124 }
125 }
126
127 private String stripListFromType(String type) {
128 if (type.endsWith("List")) {
129 type = type.substring(0, type.length() - "List".length());
130 }
131 return type;
132 }
133
134 private void addXmlTypesUsedByMessageStructure(Set<XmlType> set,
135 XmlType xmlType) {
136 ModelFinder finder = new ModelFinder(model);
137 for (MessageStructure ms : finder.findMessageStructures(xmlType.getName())) {
138 XmlType subType = finder.findXmlType(stripListFromType(ms.getType()));
139 if (subType == null) {
140 throw new DictionaryValidationException("MessageStructure field "
141 + ms.getId()
142 + " has an unknown type, "
143 + ms.getType());
144 }
145 addTypeAndAllSubTypes(set, subType);
146 }
147 }
148 }