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.xmlmapper;
17  
18  import static org.mybatis.generator.internal.util.messages.Messages.getString;
19  
20  import org.mybatis.generator.api.FullyQualifiedTable;
21  import org.mybatis.generator.api.dom.xml.Attribute;
22  import org.mybatis.generator.api.dom.xml.Document;
23  import org.mybatis.generator.api.dom.xml.XmlElement;
24  import org.mybatis.generator.codegen.AbstractXmlGenerator;
25  import org.mybatis.generator.codegen.XmlConstants;
26  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.AbstractXmlElementGenerator;
27  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.BaseColumnListElementGenerator;
28  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.BlobColumnListElementGenerator;
29  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.CountByExampleElementGenerator;
30  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.DeleteByExampleElementGenerator;
31  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.DeleteByPrimaryKeyElementGenerator;
32  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.ExampleWhereClauseElementGenerator;
33  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.InsertElementGenerator;
34  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.InsertSelectiveElementGenerator;
35  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.ResultMapWithBLOBsElementGenerator;
36  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.ResultMapWithoutBLOBsElementGenerator;
37  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.SelectByExampleWithBLOBsElementGenerator;
38  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.SelectByExampleWithoutBLOBsElementGenerator;
39  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.SelectByPrimaryKeyElementGenerator;
40  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.UpdateByExampleSelectiveElementGenerator;
41  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.UpdateByExampleWithBLOBsElementGenerator;
42  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.UpdateByExampleWithoutBLOBsElementGenerator;
43  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.UpdateByPrimaryKeySelectiveElementGenerator;
44  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.UpdateByPrimaryKeyWithBLOBsElementGenerator;
45  import org.mybatis.generator.codegen.mybatis3.xmlmapper.elements.UpdateByPrimaryKeyWithoutBLOBsElementGenerator;
46  
47  /**
48   * 
49   * @author Jeff Butler
50   * 
51   */
52  public class XMLMapperGenerator extends AbstractXmlGenerator {
53  
54      public XMLMapperGenerator() {
55          super();
56      }
57  
58      protected XmlElement getSqlMapElement() {
59          FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
60          progressCallback.startTask(getString(
61                  "Progress.12", table.toString())); //$NON-NLS-1$
62          XmlElement answer = new XmlElement("mapper"); //$NON-NLS-1$
63          String namespace = introspectedTable.getMyBatis3SqlMapNamespace();
64          answer.addAttribute(new Attribute("namespace", //$NON-NLS-1$
65                  namespace));
66  
67          context.getCommentGenerator().addRootComment(answer);
68  
69          addResultMapWithoutBLOBsElement(answer);
70          addResultMapWithBLOBsElement(answer);
71          addExampleWhereClauseElement(answer);
72          addMyBatis3UpdateByExampleWhereClauseElement(answer);
73          addBaseColumnListElement(answer);
74          addBlobColumnListElement(answer);
75          addSelectByExampleWithBLOBsElement(answer);
76          addSelectByExampleWithoutBLOBsElement(answer);
77          addSelectByPrimaryKeyElement(answer);
78          addDeleteByPrimaryKeyElement(answer);
79          addDeleteByExampleElement(answer);
80  //        addInsertElement(answer);
81          addInsertSelectiveElement(answer);
82          addCountByExampleElement(answer);
83          addUpdateByExampleSelectiveElement(answer);
84          addUpdateByExampleWithBLOBsElement(answer);
85          addUpdateByExampleWithoutBLOBsElement(answer);
86          addUpdateByPrimaryKeySelectiveElement(answer);
87          addUpdateByPrimaryKeyWithBLOBsElement(answer);
88  //        addUpdateByPrimaryKeyWithoutBLOBsElement(answer);
89  
90          return answer;
91      }
92  
93      protected void addResultMapWithoutBLOBsElement(XmlElement parentElement) {
94          if (introspectedTable.getRules().generateBaseResultMap()) {
95              AbstractXmlElementGenerator elementGenerator = new ResultMapWithoutBLOBsElementGenerator(false);
96              initializeAndExecuteGenerator(elementGenerator, parentElement);
97          }
98      }
99  
100     protected void addResultMapWithBLOBsElement(XmlElement parentElement) {
101         if (introspectedTable.getRules().generateResultMapWithBLOBs()) {
102             AbstractXmlElementGenerator elementGenerator = new ResultMapWithBLOBsElementGenerator();
103             initializeAndExecuteGenerator(elementGenerator, parentElement);
104         }
105     }
106 
107     protected void addExampleWhereClauseElement(XmlElement parentElement) {
108         if (introspectedTable.getRules().generateSQLExampleWhereClause()) {
109             AbstractXmlElementGenerator elementGenerator = new ExampleWhereClauseElementGenerator(
110                     false);
111             initializeAndExecuteGenerator(elementGenerator, parentElement);
112         }
113     }
114 
115     protected void addMyBatis3UpdateByExampleWhereClauseElement(
116             XmlElement parentElement) {
117         if (introspectedTable.getRules()
118                 .generateMyBatis3UpdateByExampleWhereClause()) {
119             AbstractXmlElementGenerator elementGenerator = new ExampleWhereClauseElementGenerator(
120                     true);
121             initializeAndExecuteGenerator(elementGenerator, parentElement);
122         }
123     }
124 
125     protected void addBaseColumnListElement(XmlElement parentElement) {
126         if (introspectedTable.getRules().generateBaseColumnList()) {
127             AbstractXmlElementGenerator elementGenerator = new BaseColumnListElementGenerator();
128             initializeAndExecuteGenerator(elementGenerator, parentElement);
129         }
130     }
131 
132     protected void addBlobColumnListElement(XmlElement parentElement) {
133         if (introspectedTable.getRules().generateBlobColumnList()) {
134             AbstractXmlElementGenerator elementGenerator = new BlobColumnListElementGenerator();
135             initializeAndExecuteGenerator(elementGenerator, parentElement);
136         }
137     }
138 
139     protected void addSelectByExampleWithoutBLOBsElement(
140             XmlElement parentElement) {
141         if (introspectedTable.getRules().generateSelectByExampleWithoutBLOBs()) {
142             AbstractXmlElementGenerator elementGenerator = new SelectByExampleWithoutBLOBsElementGenerator();
143             initializeAndExecuteGenerator(elementGenerator, parentElement);
144         }
145     }
146 
147     protected void addSelectByExampleWithBLOBsElement(XmlElement parentElement) {
148         if (introspectedTable.getRules().generateSelectByExampleWithBLOBs()) {
149             AbstractXmlElementGenerator elementGenerator = new SelectByExampleWithBLOBsElementGenerator();
150             initializeAndExecuteGenerator(elementGenerator, parentElement);
151         }
152     }
153 
154     protected void addSelectByPrimaryKeyElement(XmlElement parentElement) {
155         if (introspectedTable.getRules().generateSelectByPrimaryKey()) {
156             AbstractXmlElementGenerator elementGenerator = new SelectByPrimaryKeyElementGenerator();
157             initializeAndExecuteGenerator(elementGenerator, parentElement);
158         }
159     }
160 
161     protected void addDeleteByExampleElement(XmlElement parentElement) {
162         if (introspectedTable.getRules().generateDeleteByExample()) {
163             AbstractXmlElementGenerator elementGenerator = new DeleteByExampleElementGenerator();
164             initializeAndExecuteGenerator(elementGenerator, parentElement);
165         }
166     }
167 
168     protected void addDeleteByPrimaryKeyElement(XmlElement parentElement) {
169         if (introspectedTable.getRules().generateDeleteByPrimaryKey()) {
170             AbstractXmlElementGenerator elementGenerator = new DeleteByPrimaryKeyElementGenerator(false);
171             initializeAndExecuteGenerator(elementGenerator, parentElement);
172         }
173     }
174 
175     protected void addInsertElement(XmlElement parentElement) {
176         if (introspectedTable.getRules().generateInsert()) {
177             AbstractXmlElementGenerator elementGenerator = new InsertElementGenerator(false);
178             initializeAndExecuteGenerator(elementGenerator, parentElement);
179         }
180     }
181 
182     protected void addInsertSelectiveElement(XmlElement parentElement) {
183         if (introspectedTable.getRules().generateInsertSelective()) {
184             AbstractXmlElementGenerator elementGenerator = new InsertSelectiveElementGenerator();
185             initializeAndExecuteGenerator(elementGenerator, parentElement);
186         }
187     }
188 
189     protected void addCountByExampleElement(XmlElement parentElement) {
190         if (introspectedTable.getRules().generateCountByExample()) {
191             AbstractXmlElementGenerator elementGenerator = new CountByExampleElementGenerator();
192             initializeAndExecuteGenerator(elementGenerator, parentElement);
193         }
194     }
195 
196     protected void addUpdateByExampleSelectiveElement(XmlElement parentElement) {
197         if (introspectedTable.getRules().generateUpdateByExampleSelective()) {
198             AbstractXmlElementGenerator elementGenerator = new UpdateByExampleSelectiveElementGenerator();
199             initializeAndExecuteGenerator(elementGenerator, parentElement);
200         }
201     }
202 
203     protected void addUpdateByExampleWithBLOBsElement(XmlElement parentElement) {
204         if (introspectedTable.getRules().generateUpdateByExampleWithBLOBs()) {
205             AbstractXmlElementGenerator elementGenerator = new UpdateByExampleWithBLOBsElementGenerator();
206             initializeAndExecuteGenerator(elementGenerator, parentElement);
207         }
208     }
209 
210     protected void addUpdateByExampleWithoutBLOBsElement(
211             XmlElement parentElement) {
212         if (introspectedTable.getRules().generateUpdateByExampleWithoutBLOBs()) {
213             AbstractXmlElementGenerator elementGenerator = new UpdateByExampleWithoutBLOBsElementGenerator();
214             initializeAndExecuteGenerator(elementGenerator, parentElement);
215         }
216     }
217 
218     protected void addUpdateByPrimaryKeySelectiveElement(
219             XmlElement parentElement) {
220         if (introspectedTable.getRules().generateUpdateByPrimaryKeySelective()) {
221             AbstractXmlElementGenerator elementGenerator = new UpdateByPrimaryKeySelectiveElementGenerator();
222             initializeAndExecuteGenerator(elementGenerator, parentElement);
223         }
224     }
225 
226     protected void addUpdateByPrimaryKeyWithBLOBsElement(
227             XmlElement parentElement) {
228         if (introspectedTable.getRules().generateUpdateByPrimaryKeyWithBLOBs()) {
229             AbstractXmlElementGenerator elementGenerator = new UpdateByPrimaryKeyWithBLOBsElementGenerator();
230             initializeAndExecuteGenerator(elementGenerator, parentElement);
231         }
232     }
233 
234     protected void addUpdateByPrimaryKeyWithoutBLOBsElement(
235             XmlElement parentElement) {
236         if (introspectedTable.getRules()
237                 .generateUpdateByPrimaryKeyWithoutBLOBs()) {
238             AbstractXmlElementGenerator elementGenerator = new UpdateByPrimaryKeyWithoutBLOBsElementGenerator(false);
239             initializeAndExecuteGenerator(elementGenerator, parentElement);
240         }
241     }
242 
243     protected void initializeAndExecuteGenerator(
244             AbstractXmlElementGenerator elementGenerator,
245             XmlElement parentElement) {
246         elementGenerator.setContext(context);
247         elementGenerator.setIntrospectedTable(introspectedTable);
248         elementGenerator.setProgressCallback(progressCallback);
249         elementGenerator.setWarnings(warnings);
250         elementGenerator.addElements(parentElement);
251     }
252 
253     @Override
254     public Document getDocument() {
255         Document document = new Document(
256                 XmlConstants.MYBATIS3_MAPPER_PUBLIC_ID,
257                 XmlConstants.MYBATIS3_MAPPER_SYSTEM_ID);
258         document.setRootElement(getSqlMapElement());
259 
260         if (!context.getPlugins().sqlMapDocumentGenerated(document,
261                 introspectedTable)) {
262             document = null;
263         }
264 
265         return document;
266     }
267 }