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