1 package at.rseiler.spbee.core.pojo;
2
3 import at.rseiler.spbee.core.annotation.MappingConstructor;
4 import at.rseiler.spbee.core.annotation.ReturnNull;
5 import at.rseiler.spbee.core.annotation.RowMapper;
6 import at.rseiler.spbee.core.annotation.StoredProcedure;
7 import at.rseiler.spbee.core.pojo.annotation.MappingConstructorData;
8 import at.rseiler.spbee.core.pojo.annotation.RowMapperData;
9 import at.rseiler.spbee.core.pojo.annotation.StoredProcedureData;
10 import at.rseiler.spbee.core.util.ElementConverter;
11 import at.rseiler.spbee.core.util.StringUtil;
12
13 import javax.lang.model.element.AnnotationMirror;
14 import javax.lang.model.element.AnnotationValue;
15 import javax.lang.model.element.ExecutableElement;
16 import java.io.Serializable;
17 import java.util.ArrayList;
18 import java.util.LinkedList;
19 import java.util.List;
20 import java.util.Map.Entry;
21
22
23
24
25
26
27 public class StoredProcedureMethod implements Serializable {
28
29 private static final long serialVersionUID = 3118412026258156516L;
30
31 private String qualifiedDtoClassName;
32 private String methodName;
33 private TypeInfo returnTypeInfo;
34 private List<AnnotationInfo> annotations = new ArrayList<>();
35 private boolean returnNull;
36 private StoredProcedureData storedProcedure;
37 private RowMapperData rowMapper;
38 private MappingConstructorData mappingConstructor;
39 private List<Variable> arguments = new LinkedList<>();
40
41 private StoredProcedureMethod() {
42 }
43
44
45
46
47
48
49 public StoredProcedureMethod addArgument(Variable argument) {
50 arguments.add(argument);
51 return this;
52 }
53
54
55
56
57
58
59
60 public boolean useNullInsteadOfAnException() {
61 return returnNull;
62 }
63
64
65
66
67
68
69 public String getQualifiedClassName() {
70 StringBuilder sb = new StringBuilder(storedProcedure.getName());
71
72 if (mappingConstructor != null) {
73 String name = StringUtil.transformToJavaClassName(mappingConstructor.getName());
74 sb.append("With").append(name);
75 }
76
77 if (rowMapper != null) {
78 String name = StringUtil.getSimpleClassName(rowMapper.getType());
79 sb.append("With").append(name);
80 }
81
82 String spClassName = StringUtil.transformToJavaClassName(sb.toString());
83 String spPackage = StringUtil.getPackage(qualifiedDtoClassName);
84 return StringUtil.getQualifiedStoredProcedureClassName(spPackage, spClassName);
85 }
86
87
88
89
90
91
92 public String getPackage() {
93 return StringUtil.getPackage(getQualifiedClassName());
94 }
95
96
97
98
99
100
101 public String getSimpleClassName() {
102 String spName = StringUtil.transformToJavaClassName(getStoredProcedureName());
103 StringBuilder sb = new StringBuilder(spName);
104
105 if (mappingConstructor != null) {
106 String name = StringUtil.transformToJavaClassName(mappingConstructor.getName());
107 sb.append("With").append(name);
108 }
109
110 if (rowMapper != null) {
111 String name = StringUtil.getSimpleClassName(rowMapper.getType());
112 sb.append("With").append(name);
113 }
114
115 return sb.toString();
116 }
117
118
119
120
121
122
123 public String getStoredProcedureName() {
124 return storedProcedure.getName();
125 }
126
127
128
129
130
131
132 public String getDtoFieldName() {
133 return StringUtil.firstCharToLowerCase(getSimpleClassName());
134 }
135
136
137
138
139
140
141 public String getMethodName() {
142 return methodName;
143 }
144
145
146
147
148
149
150 public TypeInfo getReturnTypeInfo() {
151 return returnTypeInfo;
152 }
153
154
155
156
157
158
159 public List<Variable> getArguments() {
160 return arguments;
161 }
162
163
164
165
166
167
168 public String getQualifiedRowMapperClass() {
169 if (rowMapper != null) {
170 return rowMapper.getType();
171 }
172
173 String name = mappingConstructor != null ? mappingConstructor.getName() : "Default";
174 String returnType = returnTypeInfo.getGenericType().orElse(returnTypeInfo.getType());
175 return StringUtil.getQualifiedMapperClassName(returnType, name);
176 }
177
178
179
180
181
182
183 public List<AnnotationInfo> getAnnotations() {
184 return annotations;
185 }
186
187 @Override
188 public String toString() {
189 return "StoredProcedureMethod{" +
190 "qualifiedDtoClassName='" + qualifiedDtoClassName + '\'' +
191 ", annotations=" + annotations +
192 ", storedProcedure=" + storedProcedure +
193 ", rowMapper=" + rowMapper +
194 ", returnNull=" + returnNull +
195 ", mappingConstructor=" + mappingConstructor +
196 ", methodName='" + methodName + '\'' +
197 ", returnTypeInfo=" + returnTypeInfo +
198 ", arguments=" + arguments +
199 '}';
200 }
201
202 public static class Builder {
203
204 private final StoredProcedureMethod storedProcedureMethod;
205
206 public Builder() {
207 this.storedProcedureMethod = new StoredProcedureMethod();
208 }
209
210 public Builder dtoClassName(String dtoClassName) {
211 storedProcedureMethod.qualifiedDtoClassName = dtoClassName;
212 return this;
213 }
214
215 public Builder methodName(String methodName) {
216 storedProcedureMethod.methodName = methodName;
217 return this;
218 }
219
220 public Builder returnTypeInfo(TypeInfo returnTypeInfo) {
221 storedProcedureMethod.returnTypeInfo = returnTypeInfo;
222 return this;
223 }
224
225 public Builder annotations(List<AnnotationMirror> annotations) {
226 List<AnnotationInfo> annotationInfos = new LinkedList<>();
227
228 for (AnnotationMirror annotation : annotations) {
229 AnnotationInfo annotationInfo = new AnnotationInfo(annotation.getAnnotationType().toString());
230
231 for (Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : annotation.getElementValues().entrySet()) {
232 String simpleName = entry.getKey().getSimpleName().toString();
233 Object value = entry.getValue().getValue();
234 String type = entry.getValue().toString();
235 annotationInfo.addAnnotationValueInfo(ElementConverter.convert(simpleName, value, type));
236 }
237
238 annotationInfos.add(annotationInfo);
239 }
240
241 storedProcedureMethod.annotations = annotationInfos;
242 return this;
243 }
244
245 public Builder annotationInfos(List<AnnotationInfo> annotations) {
246 storedProcedureMethod.annotations = annotations;
247 return this;
248 }
249
250 public Builder storedProcedure(StoredProcedure storedProcedure) {
251 storedProcedureMethod.storedProcedure = new StoredProcedureData(storedProcedure);
252 return this;
253 }
254
255 public Builder rowMapper(RowMapper rowMapper) {
256 storedProcedureMethod.rowMapper = rowMapper != null ? new RowMapperData(rowMapper) : null;
257 return this;
258 }
259
260 public Builder returnNull(ReturnNull returnNull) {
261 storedProcedureMethod.returnNull = returnNull != null;
262 return this;
263 }
264
265 public Builder mappingConstructor(MappingConstructor mappingConstructor) {
266 storedProcedureMethod.mappingConstructor = mappingConstructor != null ? new MappingConstructorData(mappingConstructor) : null;
267 return this;
268 }
269
270 public StoredProcedureMethod build() {
271 return storedProcedureMethod;
272 }
273
274 }
275
276 }