View Javadoc
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   * Represents a stored procedure method.
24   *
25   * @author Reinhard Seiler {@literal <rseiler.developer@gmail.com>}
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       * Adds a variable to the stored procedure method.
46       *
47       * @param argument the argument of the method
48       * @return returns itself
49       */
50      public StoredProcedureMethod addArgument(Variable argument) {
51          arguments.add(argument);
52          return this;
53      }
54  
55      /**
56       * Returns if null should be used instead of throwing an exception if an entity is expected but nothing is returned
57       * from the database.
58       *
59       * @return true if null should be used as return value
60       */
61      public boolean useNullInsteadOfAnException() {
62          return returnNull;
63      }
64  
65      /**
66       * The qualified class name of the stored procedure method.
67       *
68       * @return the qualified class name
69       */
70      public String getQualifiedClassName() {
71          StringBuilder sb = new StringBuilder(storedProcedure.getName());
72  
73          if (mappingConstructor != null) {
74              String name = StringUtil.transformToJavaClassName(mappingConstructor.getName());
75              sb.append("With").append(name);
76          }
77  
78          if (rowMapper != null) {
79              String name = StringUtil.getSimpleClassName(rowMapper.getType());
80              sb.append("With").append(name);
81          }
82  
83          String spClassName = StringUtil.transformToJavaClassName(sb.toString());
84          String spPackage = StringUtil.getPackage(qualifiedDtoClassName);
85          return StringUtil.getQualifiedStoredProcedureClassName(spPackage, spClassName);
86      }
87  
88      /**
89       * The package of the of the stored procedure method.
90       *
91       * @return the package
92       */
93      public String getPackage() {
94          return StringUtil.getPackage(getQualifiedClassName());
95      }
96  
97      /**
98       * The simple class name.
99       *
100      * @return simple class name
101      */
102     public String getSimpleClassName() {
103         String spName = StringUtil.transformToJavaClassName(getStoredProcedureName());
104         StringBuilder sb = new StringBuilder(spName);
105 
106         if (mappingConstructor != null) {
107             String name = StringUtil.transformToJavaClassName(mappingConstructor.getName());
108             sb.append("With").append(name);
109         }
110 
111         if (rowMapper != null) {
112             String name = StringUtil.getSimpleClassName(rowMapper.getType());
113             sb.append("With").append(name);
114         }
115 
116         return sb.toString();
117     }
118 
119     /**
120      * The @StoredProcedure annotation.
121      *
122      * @return the @StoredProcedure annotation
123      */
124     public String getStoredProcedureName() {
125         return storedProcedure.getName();
126     }
127 
128     /**
129      * Generates the name of the variable name which should be used for the DTO class.
130      *
131      * @return the field name
132      */
133     public String getDtoFieldName() {
134         return StringUtil.firstCharToLowerCase(getSimpleClassName());
135     }
136 
137     /**
138      * The method name.
139      *
140      * @return method name
141      */
142     public String getMethodName() {
143         return methodName;
144     }
145 
146     /**
147      * The TypeInfo of the return value of the stored procedure method.;
148      *
149      * @return the TypeInfo
150      */
151     public TypeInfo getReturnTypeInfo() {
152         return returnTypeInfo;
153     }
154 
155     /**
156      * The arguments of the stored procedure method.
157      *
158      * @return the arguments
159      */
160     public List<Variable> getArguments() {
161         return arguments;
162     }
163 
164     /**
165      * Returns the qualified class name of the RowMapper.
166      *
167      * @return the qualified class name
168      */
169     public String getQualifiedRowMapperClass() {
170         if (rowMapper != null) {
171             return rowMapper.getType();
172         }
173 
174         String name = mappingConstructor != null ? mappingConstructor.getName() : "Default";
175         String returnType = returnTypeInfo.getGenericType().orElse(returnTypeInfo.getType());
176         return StringUtil.getQualifiedMapperClassName(returnType, name);
177     }
178 
179     /**
180      * The annotations of the stored procedure method.
181      *
182      * @return the annotations
183      */
184     public List<AnnotationInfo> getAnnotations() {
185         return annotations;
186     }
187 
188     @Override
189     public String toString() {
190         return "StoredProcedureMethod{" +
191                 "qualifiedDtoClassName='" + qualifiedDtoClassName + '\'' +
192                 ", annotations=" + annotations +
193                 ", storedProcedure=" + storedProcedure +
194                 ", rowMapper=" + rowMapper +
195                 ", returnNull=" + returnNull +
196                 ", mappingConstructor=" + mappingConstructor +
197                 ", methodName='" + methodName + '\'' +
198                 ", returnTypeInfo=" + returnTypeInfo +
199                 ", arguments=" + arguments +
200                 '}';
201     }
202 
203     public static class Builder {
204 
205         private final StoredProcedureMethod storedProcedureMethod;
206 
207         public Builder() {
208             this.storedProcedureMethod = new StoredProcedureMethod();
209         }
210 
211         public Builder dtoClassName(String dtoClassName) {
212             storedProcedureMethod.qualifiedDtoClassName = dtoClassName;
213             return this;
214         }
215 
216         public Builder methodName(String methodName) {
217             storedProcedureMethod.methodName = methodName;
218             return this;
219         }
220 
221         public Builder returnTypeInfo(TypeInfo returnTypeInfo) {
222             storedProcedureMethod.returnTypeInfo = returnTypeInfo;
223             return this;
224         }
225 
226         public Builder annotations(List<AnnotationMirror> annotations) {
227             List<AnnotationInfo> annotationInfos = new LinkedList<>();
228 
229             for (AnnotationMirror annotation : annotations) {
230                 AnnotationInfo annotationInfo = new AnnotationInfo(annotation.getAnnotationType().toString());
231 
232                 for (Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : annotation.getElementValues().entrySet()) {
233                     String simpleName = entry.getKey().getSimpleName().toString();
234                     Object value = entry.getValue().getValue();
235                     String type = entry.getValue().toString();
236                     annotationInfo.addAnnotationValueInfo(ElementConverter.convert(simpleName, value, type));
237                 }
238 
239                 annotationInfos.add(annotationInfo);
240             }
241 
242             storedProcedureMethod.annotations = annotationInfos;
243             return this;
244         }
245 
246         public Builder annotationInfos(List<AnnotationInfo> annotations) {
247             storedProcedureMethod.annotations = annotations;
248             return this;
249         }
250 
251         public Builder storedProcedure(StoredProcedure storedProcedure) {
252             storedProcedureMethod.storedProcedure = new StoredProcedureData(storedProcedure);
253             return this;
254         }
255 
256         public Builder rowMapper(RowMapper rowMapper) {
257             storedProcedureMethod.rowMapper = rowMapper != null ? new RowMapperData(rowMapper) : null;
258             return this;
259         }
260 
261         public Builder returnNull(ReturnNull returnNull) {
262             storedProcedureMethod.returnNull = returnNull != null;
263             return this;
264         }
265 
266         public Builder mappingConstructor(MappingConstructor mappingConstructor) {
267             storedProcedureMethod.mappingConstructor = mappingConstructor != null ? new MappingConstructorData(mappingConstructor) : null;
268             return this;
269         }
270 
271         public StoredProcedureMethod build() {
272             return storedProcedureMethod;
273         }
274 
275     }
276 
277 }