1 /**
2 * Copyright 2006-2016 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.mybatis.generator.codegen.ibatis2.dao.templates;
17
18 import java.text.MessageFormat;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.mybatis.generator.api.CommentGenerator;
23 import org.mybatis.generator.api.IntrospectedTable;
24 import org.mybatis.generator.api.dom.java.Field;
25 import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
26 import org.mybatis.generator.api.dom.java.Method;
27 import org.mybatis.generator.api.dom.java.Parameter;
28
29 /**
30 * Base class for DAO templates. Subclasses should override any of the
31 * configureXXX methods to specify the unique properties of the desired DAO
32 * objects.
33 *
34 * @author Jeff Butler
35 */
36 public abstract class AbstractDAOTemplate {
37
38 /** The interface imports. */
39 private List<FullyQualifiedJavaType> interfaceImports;
40
41 /** The implementation imports. */
42 private List<FullyQualifiedJavaType> implementationImports;
43
44 /** The super class. */
45 private FullyQualifiedJavaType superClass;
46
47 /** The checked exceptions. */
48 private List<FullyQualifiedJavaType> checkedExceptions;
49
50 /** The fields. */
51 private List<Field> fields;
52
53 /** The methods. */
54 private List<Method> methods;
55
56 /** The constructor template. */
57 private Method constructorTemplate;
58
59 /** The delete method template. */
60 private String deleteMethodTemplate;
61
62 /** The insert method template. */
63 private String insertMethodTemplate;
64
65 /** The update method template. */
66 private String updateMethodTemplate;
67
68 /** The query for object method template. */
69 private String queryForObjectMethodTemplate;
70
71 /** The query for list method template. */
72 private String queryForListMethodTemplate;
73
74 /** The configured. */
75 private boolean configured;
76
77 /**
78 * Instantiates a new abstract dao template.
79 */
80 public AbstractDAOTemplate() {
81 super();
82 interfaceImports = new ArrayList<FullyQualifiedJavaType>();
83 implementationImports = new ArrayList<FullyQualifiedJavaType>();
84 fields = new ArrayList<Field>();
85 methods = new ArrayList<Method>();
86 checkedExceptions = new ArrayList<FullyQualifiedJavaType>();
87 configured = false;
88 }
89
90 /**
91 * Gets the constructor clone.
92 *
93 * @param commentGenerator
94 * the comment generator
95 * @param type
96 * the type
97 * @param introspectedTable
98 * the introspected table
99 * @return the constructor clone
100 */
101 public final Method getConstructorClone(CommentGenerator commentGenerator,
102 FullyQualifiedJavaType type, IntrospectedTable introspectedTable) {
103 configure();
104 Method answer = new Method();
105 answer.setConstructor(true);
106 answer.setName(type.getShortName());
107 answer.setVisibility(constructorTemplate.getVisibility());
108 for (Parameter parm : constructorTemplate.getParameters()) {
109 answer.addParameter(parm);
110 }
111
112 for (String bodyLine : constructorTemplate.getBodyLines()) {
113 answer.addBodyLine(bodyLine);
114 }
115
116 for (FullyQualifiedJavaType fqjt : constructorTemplate.getExceptions()) {
117 answer.addException(fqjt);
118 }
119
120 commentGenerator.addGeneralMethodComment(answer, introspectedTable);
121
122 return answer;
123 }
124
125 /**
126 * Gets the delete method.
127 *
128 * @param sqlMapNamespace
129 * the sql map namespace
130 * @param statementId
131 * the statement id
132 * @param parameter
133 * the parameter
134 * @return the delete method
135 */
136 public final String getDeleteMethod(String sqlMapNamespace,
137 String statementId, String parameter) {
138 configure();
139 String answer = MessageFormat.format(deleteMethodTemplate,
140 new Object[] { sqlMapNamespace, statementId, parameter });
141
142 return answer;
143 }
144
145 /**
146 * Gets the interface imports.
147 *
148 * @return the interface imports
149 */
150 public final List<FullyQualifiedJavaType> getInterfaceImports() {
151 configure();
152 return interfaceImports;
153 }
154
155 /**
156 * Gets the implementation imports.
157 *
158 * @return the implementation imports
159 */
160 public final List<FullyQualifiedJavaType> getImplementationImports() {
161 configure();
162 return implementationImports;
163 }
164
165 /**
166 * Gets the insert method.
167 *
168 * @param sqlMapNamespace
169 * the sql map namespace
170 * @param statementId
171 * the statement id
172 * @param parameter
173 * the parameter
174 * @return the insert method
175 */
176 public final String getInsertMethod(String sqlMapNamespace,
177 String statementId, String parameter) {
178 configure();
179 String answer = MessageFormat.format(insertMethodTemplate,
180 new Object[] { sqlMapNamespace, statementId, parameter });
181
182 return answer;
183 }
184
185 /**
186 * Gets the query for list method.
187 *
188 * @param sqlMapNamespace
189 * the sql map namespace
190 * @param statementId
191 * the statement id
192 * @param parameter
193 * the parameter
194 * @return the query for list method
195 */
196 public final String getQueryForListMethod(String sqlMapNamespace,
197 String statementId, String parameter) {
198 configure();
199 String answer = MessageFormat.format(queryForListMethodTemplate,
200 new Object[] { sqlMapNamespace, statementId, parameter });
201
202 return answer;
203 }
204
205 /**
206 * Gets the query for object method.
207 *
208 * @param sqlMapNamespace
209 * the sql map namespace
210 * @param statementId
211 * the statement id
212 * @param parameter
213 * the parameter
214 * @return the query for object method
215 */
216 public final String getQueryForObjectMethod(String sqlMapNamespace,
217 String statementId, String parameter) {
218 configure();
219 String answer = MessageFormat.format(queryForObjectMethodTemplate,
220 new Object[] { sqlMapNamespace, statementId, parameter });
221
222 return answer;
223 }
224
225 /**
226 * Gets the super class.
227 *
228 * @return the super class
229 */
230 public final FullyQualifiedJavaType getSuperClass() {
231 configure();
232 return superClass;
233 }
234
235 /**
236 * Gets the update method.
237 *
238 * @param sqlMapNamespace
239 * the sql map namespace
240 * @param statementId
241 * the statement id
242 * @param parameter
243 * the parameter
244 * @return the update method
245 */
246 public final String getUpdateMethod(String sqlMapNamespace,
247 String statementId, String parameter) {
248 configure();
249 String answer = MessageFormat.format(updateMethodTemplate,
250 new Object[] { sqlMapNamespace, statementId, parameter });
251
252 return answer;
253 }
254
255 /**
256 * Gets the checked exceptions.
257 *
258 * @return the checked exceptions
259 */
260 public final List<FullyQualifiedJavaType> getCheckedExceptions() {
261 configure();
262 return checkedExceptions;
263 }
264
265 /**
266 * Gets the field clones.
267 *
268 * @param commentGenerator
269 * the comment generator
270 * @param introspectedTable
271 * the introspected table
272 * @return the field clones
273 */
274 public final List<Field> getFieldClones(CommentGenerator commentGenerator,
275 IntrospectedTable introspectedTable) {
276 configure();
277 List<Field> answer = new ArrayList<Field>();
278 for (Field oldField : fields) {
279 Field field = new Field();
280
281 field.setInitializationString(oldField.getInitializationString());
282 field.setFinal(oldField.isFinal());
283 field.setStatic(oldField.isStatic());
284 field.setName(oldField.getName());
285 field.setType(oldField.getType());
286 field.setVisibility(oldField.getVisibility());
287 commentGenerator.addFieldComment(field, introspectedTable);
288 answer.add(field);
289 }
290
291 return answer;
292 }
293
294 /**
295 * Gets the method clones.
296 *
297 * @param commentGenerator
298 * the comment generator
299 * @param introspectedTable
300 * the introspected table
301 * @return the method clones
302 */
303 public final List<Method> getMethodClones(
304 CommentGenerator commentGenerator,
305 IntrospectedTable introspectedTable) {
306 configure();
307 List<Method> answer = new ArrayList<Method>();
308 for (Method oldMethod : methods) {
309 Method method = new Method();
310
311 for (String bodyLine : oldMethod.getBodyLines()) {
312 method.addBodyLine(bodyLine);
313 }
314
315 for (FullyQualifiedJavaType fqjt : oldMethod.getExceptions()) {
316 method.addException(fqjt);
317 }
318
319 for (Parameter parm : oldMethod.getParameters()) {
320 method.addParameter(parm);
321 }
322
323 method.setConstructor(oldMethod.isConstructor());
324 method.setFinal(oldMethod.isFinal());
325 method.setStatic(oldMethod.isStatic());
326 method.setName(oldMethod.getName());
327 method.setReturnType(oldMethod.getReturnType());
328 method.setVisibility(oldMethod.getVisibility());
329
330 commentGenerator.addGeneralMethodComment(method, introspectedTable);
331
332 answer.add(method);
333 }
334
335 return answer;
336 }
337
338 /**
339 * Sets the constructor template.
340 *
341 * @param constructorTemplate
342 * the new constructor template
343 */
344 protected void setConstructorTemplate(Method constructorTemplate) {
345 this.constructorTemplate = constructorTemplate;
346 }
347
348 /**
349 * Sets the delete method template.
350 *
351 * @param deleteMethodTemplate
352 * the new delete method template
353 */
354 protected void setDeleteMethodTemplate(String deleteMethodTemplate) {
355 this.deleteMethodTemplate = deleteMethodTemplate;
356 }
357
358 /**
359 * Adds the field.
360 *
361 * @param field
362 * the field
363 */
364 protected void addField(Field field) {
365 fields.add(field);
366 }
367
368 /**
369 * Sets the insert method template.
370 *
371 * @param insertMethodTemplate
372 * the new insert method template
373 */
374 protected void setInsertMethodTemplate(String insertMethodTemplate) {
375 this.insertMethodTemplate = insertMethodTemplate;
376 }
377
378 /**
379 * Adds the method.
380 *
381 * @param method
382 * the method
383 */
384 protected void addMethod(Method method) {
385 methods.add(method);
386 }
387
388 /**
389 * Sets the query for list method template.
390 *
391 * @param queryForListMethodTemplate
392 * the new query for list method template
393 */
394 protected void setQueryForListMethodTemplate(
395 String queryForListMethodTemplate) {
396 this.queryForListMethodTemplate = queryForListMethodTemplate;
397 }
398
399 /**
400 * Sets the query for object method template.
401 *
402 * @param queryForObjectMethodTemplate
403 * the new query for object method template
404 */
405 protected void setQueryForObjectMethodTemplate(
406 String queryForObjectMethodTemplate) {
407 this.queryForObjectMethodTemplate = queryForObjectMethodTemplate;
408 }
409
410 /**
411 * Sets the super class.
412 *
413 * @param superClass
414 * the new super class
415 */
416 protected void setSuperClass(FullyQualifiedJavaType superClass) {
417 this.superClass = superClass;
418 }
419
420 /**
421 * Sets the update method template.
422 *
423 * @param updateMethodTemplate
424 * the new update method template
425 */
426 protected void setUpdateMethodTemplate(String updateMethodTemplate) {
427 this.updateMethodTemplate = updateMethodTemplate;
428 }
429
430 /**
431 * Adds the interface import.
432 *
433 * @param type
434 * the type
435 */
436 protected void addInterfaceImport(FullyQualifiedJavaType type) {
437 interfaceImports.add(type);
438 }
439
440 /**
441 * Adds the implementation import.
442 *
443 * @param type
444 * the type
445 */
446 protected void addImplementationImport(FullyQualifiedJavaType type) {
447 implementationImports.add(type);
448 }
449
450 /**
451 * Adds the checked exception.
452 *
453 * @param type
454 * the type
455 */
456 protected void addCheckedException(FullyQualifiedJavaType type) {
457 checkedExceptions.add(type);
458 }
459
460 /**
461 * This method is called in the constructor to configure the DAO template.
462 * Subclasses should implement the individual configureXXX methods to setup
463 * the relevant parts of the DAO template (super class, extra methods, etc.)
464 * that are relevant for this specific type of DAO.
465 */
466 private void configure() {
467 if (!configured) {
468 configureCheckedExceptions();
469 configureConstructorTemplate();
470 configureDeleteMethodTemplate();
471 configureFields();
472 configureImplementationImports();
473 configureInsertMethodTemplate();
474 configureInterfaceImports();
475 configureMethods();
476 configureQueryForListMethodTemplate();
477 configureQueryForObjectMethodTemplate();
478 configureSuperClass();
479 configureUpdateMethodTemplate();
480 configured = true;
481 }
482 }
483
484 /**
485 * Override this method to add checked exceptions to the throws clause of
486 * any generated DAO method. When overriding this method, call
487 * <code>addCheckedException(FullyQualifiedJavaType)</code> one or more
488 * times to add checked exception(s) to all generated DAO methods.
489 */
490 protected void configureCheckedExceptions() {
491 }
492
493 /**
494 * Override this method to add fields to any generated DAO implementation
495 * class. When overriding this method, call <code>addField(Field)</code> one
496 * or more times to add field(s) to the generated DAO implementation class.
497 */
498 protected void configureFields() {
499 }
500
501 /**
502 * Override this method to add imports to generated DAO implementation
503 * classes. When overriding this method, call
504 * <code>addImplementationImport(FullyQualifiedJavaType)</code> one or more
505 * times to add import(s) to generated DAO implementation classes.
506 */
507 protected void configureImplementationImports() {
508 }
509
510 /**
511 * Override this method to add imports to generated DAO interface classes.
512 * When overriding this method, call
513 * <code>addInterfaceImport(FullyQualifiedJavaType)</code> one or more times
514 * to add import(s) to generated DAO interface classes.
515 */
516 protected void configureInterfaceImports() {
517 }
518
519 /**
520 * Override this method to add methods to generated DAO implementation
521 * classes. When overriding this method, call <code>addMethod(Method)</code>
522 * one or more times to add method(s) to generated DAO implementation
523 * classes.
524 */
525 protected void configureMethods() {
526 }
527
528 /**
529 * Override this method to set the superclass for any generated DAO
530 * implementation class. When overriding this method call
531 * <code>setSuperClass(FullyQualifiedJavaType)</code> to set the superclass
532 * for generated DAO implementation classes.
533 */
534 protected void configureSuperClass() {
535 }
536
537 /**
538 * Override this method to configure a constructor for generated DAO
539 * implementation classes. During code generation, we will build a new
540 * constructor using the visibility, parameters, body lines, and exceptions
541 * set on the constructor template. When overriding this method, call
542 * <code>setConstructorTemplate(Method)</code> to set the constructor
543 * template.
544 */
545 protected abstract void configureConstructorTemplate();
546
547 /**
548 * Override this method to configure an insert method template. A method
549 * template is a string with three substitution markers that we will
550 * fill in when generating code. The substitution markers will be:
551 * <ul>
552 * <li>{0} - The SqlMap namespace</li>
553 * <li>{1} - The SqlMap statement id</li>
554 * <li>{2} - The parameter object</li>
555 * </ul>
556 *
557 * For example, when calling methods in the SqlMapClient interface, the
558 * template would be:
559 *
560 * sqlMapClient.insert(\"{0}.{1}\", {2});
561 *
562 * Overriding methods should call the
563 * <code>setInsertMethodTemplate(String)</code> method to set the template.
564 *
565 */
566 protected abstract void configureInsertMethodTemplate();
567
568 /**
569 * Override this method to configure a queryForList method template. A
570 * method template is a string with three substitution markers that we
571 * will fill in when generating code. The substitution markers will be:
572 * <ul>
573 * <li>{0} - The SqlMap namespace</li>
574 * <li>{1} - The SqlMap statement id</li>
575 * <li>{2} - The parameter object</li>
576 * </ul>
577 *
578 * For example, when calling methods in the SqlMapClient interface, the
579 * template would be:
580 *
581 * sqlMapClient.queryForList(\"{0}.{1}\", {2});
582 *
583 * Overriding methods should call the
584 * <code>setQueryForListMethodTemplate(String)</code> method to set the
585 * template.
586 */
587 protected abstract void configureQueryForListMethodTemplate();
588
589 /**
590 * Override this method to configure a queryForObject method template. A
591 * method template is a string with three substitution markers that we
592 * will fill in when generating code. The substitution markers will be:
593 * <ul>
594 * <li>{0} - The SqlMap namespace</li>
595 * <li>{1} - The SqlMap statement id</li>
596 * <li>{2} - The parameter object</li>
597 * </ul>
598 *
599 * For example, when calling methods in the SqlMapClient interface, the
600 * template would be:
601 *
602 * sqlMapClient.queryForObject(\"{0}.{1}\", {2});
603 *
604 * Overriding methods should call the
605 * <code>setQueryForObjectMethodTemplate(String)</code> method to set the
606 * template.
607 */
608 protected abstract void configureQueryForObjectMethodTemplate();
609
610 /**
611 * Override this method to configure an update method template. A method
612 * template is a string with three substitution markers that we will
613 * fill in when generating code. The substitution markers will be:
614 * <ul>
615 * <li>{0} - The SqlMap namespace</li>
616 * <li>{1} - The SqlMap statement id</li>
617 * <li>{2} - The parameter object</li>
618 * </ul>
619 *
620 * For example, when calling methods in the SqlMapClient interface, the
621 * template would be:
622 *
623 * sqlMapClient.update(\"{0}.{1}\", {2});
624 *
625 * Overriding methods should call the
626 * <code>setUpdateMethodTemplate(String)</code> method to set the template.
627 */
628 protected abstract void configureUpdateMethodTemplate();
629
630 /**
631 * Override this method to configure a delete method template. A method
632 * template is a string with three substitution markers that we will
633 * fill in when generating code. The substitution markers will be:
634 * <ul>
635 * <li>{0} - The SqlMap namespace</li>
636 * <li>{1} - The SqlMap statement id</li>
637 * <li>{2} - The parameter object</li>
638 * </ul>
639 *
640 * For example, when calling methods in the SqlMapClient interface, the
641 * template would be:
642 *
643 * sqlMapClient.delete(\"{0}.{1}\", {2});
644 *
645 * Overriding methods should call the
646 * <code>setDeleteMethodTemplate(String)</code> method to set the template.
647 */
648 protected abstract void configureDeleteMethodTemplate();
649 }