View Javadoc
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.mybatis3.javamapper;
17  
18  import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
19  import static org.mybatis.generator.internal.util.messages.Messages.getString;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.mybatis.generator.api.CommentGenerator;
25  import org.mybatis.generator.api.dom.java.CompilationUnit;
26  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
27  import org.mybatis.generator.api.dom.java.Interface;
28  import org.mybatis.generator.api.dom.java.JavaVisibility;
29  import org.mybatis.generator.codegen.AbstractJavaClientGenerator;
30  import org.mybatis.generator.codegen.AbstractXmlGenerator;
31  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.AbstractJavaMapperMethodGenerator;
32  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.DeleteByPrimaryKeyMethodGenerator;
33  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.InsertMethodGenerator;
34  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.SelectAllMethodGenerator;
35  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.SelectByPrimaryKeyMethodGenerator;
36  import org.mybatis.generator.codegen.mybatis3.javamapper.elements.UpdateByPrimaryKeyWithoutBLOBsMethodGenerator;
37  import org.mybatis.generator.codegen.mybatis3.xmlmapper.SimpleXMLMapperGenerator;
38  import org.mybatis.generator.config.PropertyRegistry;
39  
40  /**
41   * @author Jeff Butler
42   * 
43   */
44  public class SimpleJavaClientGenerator extends AbstractJavaClientGenerator {
45  
46      /**
47       * 
48       */
49      public SimpleJavaClientGenerator() {
50          super(true);
51      }
52  
53      public SimpleJavaClientGenerator(boolean requiresMatchedXMLGenerator) {
54          super(requiresMatchedXMLGenerator);
55      }
56      
57      @Override
58      public List<CompilationUnit> getCompilationUnits() {
59          progressCallback.startTask(getString("Progress.17", //$NON-NLS-1$
60                  introspectedTable.getFullyQualifiedTable().toString()));
61          CommentGenerator commentGenerator = context.getCommentGenerator();
62  
63          FullyQualifiedJavaType type = new FullyQualifiedJavaType(
64                  introspectedTable.getMyBatis3JavaMapperType());
65          Interface interfaze = new Interface(type);
66          interfaze.setVisibility(JavaVisibility.PUBLIC);
67          commentGenerator.addJavaFileComment(interfaze);
68  
69          String rootInterface = introspectedTable
70              .getTableConfigurationProperty(PropertyRegistry.ANY_ROOT_INTERFACE);
71          if (!stringHasValue(rootInterface)) {
72              rootInterface = context.getJavaClientGeneratorConfiguration()
73                  .getProperty(PropertyRegistry.ANY_ROOT_INTERFACE);
74          }
75  
76          if (stringHasValue(rootInterface)) {
77              FullyQualifiedJavaType fqjt = new FullyQualifiedJavaType(
78                      rootInterface);
79              interfaze.addSuperInterface(fqjt);
80              interfaze.addImportedType(fqjt);
81          }
82          
83          addDeleteByPrimaryKeyMethod(interfaze);
84          addInsertMethod(interfaze);
85          addSelectByPrimaryKeyMethod(interfaze);
86          addSelectAllMethod(interfaze);
87          addUpdateByPrimaryKeyMethod(interfaze);
88  
89          List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
90          if (context.getPlugins().clientGenerated(interfaze, null,
91                  introspectedTable)) {
92              answer.add(interfaze);
93          }
94          
95          List<CompilationUnit> extraCompilationUnits = getExtraCompilationUnits();
96          if (extraCompilationUnits != null) {
97              answer.addAll(extraCompilationUnits);
98          }
99  
100         return answer;
101     }
102 
103     protected void addDeleteByPrimaryKeyMethod(Interface interfaze) {
104         if (introspectedTable.getRules().generateDeleteByPrimaryKey()) {
105             AbstractJavaMapperMethodGenerator methodGenerator = new DeleteByPrimaryKeyMethodGenerator(true);
106             initializeAndExecuteGenerator(methodGenerator, interfaze);
107         }
108     }
109 
110     protected void addInsertMethod(Interface interfaze) {
111         if (introspectedTable.getRules().generateInsert()) {
112             AbstractJavaMapperMethodGenerator methodGenerator = new InsertMethodGenerator(true);
113             initializeAndExecuteGenerator(methodGenerator, interfaze);
114         }
115     }
116 
117     protected void addSelectByPrimaryKeyMethod(Interface interfaze) {
118         if (introspectedTable.getRules().generateSelectByPrimaryKey()) {
119             AbstractJavaMapperMethodGenerator methodGenerator = new SelectByPrimaryKeyMethodGenerator(true);
120             initializeAndExecuteGenerator(methodGenerator, interfaze);
121         }
122     }
123 
124     protected void addSelectAllMethod(Interface interfaze) {
125         AbstractJavaMapperMethodGenerator methodGenerator = new SelectAllMethodGenerator();
126         initializeAndExecuteGenerator(methodGenerator, interfaze);
127     }
128 
129     protected void addUpdateByPrimaryKeyMethod(Interface interfaze) {
130         if (introspectedTable.getRules().generateUpdateByPrimaryKeySelective()) {
131             AbstractJavaMapperMethodGenerator methodGenerator = new UpdateByPrimaryKeyWithoutBLOBsMethodGenerator();
132             initializeAndExecuteGenerator(methodGenerator, interfaze);
133         }
134     }
135 
136     protected void initializeAndExecuteGenerator(
137             AbstractJavaMapperMethodGenerator methodGenerator,
138             Interface interfaze) {
139         methodGenerator.setContext(context);
140         methodGenerator.setIntrospectedTable(introspectedTable);
141         methodGenerator.setProgressCallback(progressCallback);
142         methodGenerator.setWarnings(warnings);
143         methodGenerator.addInterfaceElements(interfaze);
144     }
145 
146     public List<CompilationUnit> getExtraCompilationUnits() {
147         return null;
148     }
149 
150     @Override
151     public AbstractXmlGenerator getMatchedXMLGenerator() {
152         return new SimpleXMLMapperGenerator();
153     }
154 }