1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.generator.codegen.ibatis2;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.mybatis.generator.api.GeneratedJavaFile;
22 import org.mybatis.generator.api.GeneratedXmlFile;
23 import org.mybatis.generator.api.IntrospectedTable;
24 import org.mybatis.generator.api.ProgressCallback;
25 import org.mybatis.generator.api.dom.java.CompilationUnit;
26 import org.mybatis.generator.api.dom.xml.Document;
27 import org.mybatis.generator.codegen.AbstractGenerator;
28 import org.mybatis.generator.codegen.AbstractJavaGenerator;
29 import org.mybatis.generator.codegen.AbstractXmlGenerator;
30 import org.mybatis.generator.codegen.ibatis2.dao.DAOGenerator;
31 import org.mybatis.generator.codegen.ibatis2.dao.templates.GenericCIDAOTemplate;
32 import org.mybatis.generator.codegen.ibatis2.dao.templates.GenericSIDAOTemplate;
33 import org.mybatis.generator.codegen.ibatis2.dao.templates.IbatisDAOTemplate;
34 import org.mybatis.generator.codegen.ibatis2.dao.templates.SpringDAOTemplate;
35 import org.mybatis.generator.codegen.ibatis2.model.BaseRecordGenerator;
36 import org.mybatis.generator.codegen.ibatis2.model.ExampleGenerator;
37 import org.mybatis.generator.codegen.ibatis2.model.PrimaryKeyGenerator;
38 import org.mybatis.generator.codegen.ibatis2.model.RecordWithBLOBsGenerator;
39 import org.mybatis.generator.codegen.ibatis2.sqlmap.SqlMapGenerator;
40 import org.mybatis.generator.config.PropertyRegistry;
41 import org.mybatis.generator.internal.ObjectFactory;
42
43
44
45
46
47
48 public class IntrospectedTableIbatis2Java2Impl extends IntrospectedTable {
49 protected List<AbstractJavaGenerator> javaModelGenerators;
50 protected List<AbstractJavaGenerator> daoGenerators;
51 protected AbstractXmlGenerator sqlMapGenerator;
52
53 public IntrospectedTableIbatis2Java2Impl() {
54 super(TargetRuntime.IBATIS2);
55 javaModelGenerators = new ArrayList<AbstractJavaGenerator>();
56 daoGenerators = new ArrayList<AbstractJavaGenerator>();
57 }
58
59 @Override
60 public void calculateGenerators(List<String> warnings,
61 ProgressCallback progressCallback) {
62 calculateJavaModelGenerators(warnings, progressCallback);
63 calculateDAOGenerators(warnings, progressCallback);
64 calculateSqlMapGenerator(warnings, progressCallback);
65 }
66
67 protected void calculateSqlMapGenerator(List<String> warnings,
68 ProgressCallback progressCallback) {
69 sqlMapGenerator = new SqlMapGenerator();
70 initializeAbstractGenerator(sqlMapGenerator, warnings, progressCallback);
71 }
72
73 protected void calculateDAOGenerators(List<String> warnings,
74 ProgressCallback progressCallback) {
75 if (context.getJavaClientGeneratorConfiguration() == null) {
76 return;
77 }
78
79 String type = context.getJavaClientGeneratorConfiguration()
80 .getConfigurationType();
81
82 AbstractJavaGenerator javaGenerator;
83 if ("IBATIS".equalsIgnoreCase(type)) {
84 javaGenerator = new DAOGenerator(new IbatisDAOTemplate(),
85 isJava5Targeted());
86 } else if ("SPRING".equalsIgnoreCase(type)) {
87 javaGenerator = new DAOGenerator(new SpringDAOTemplate(),
88 isJava5Targeted());
89 } else if ("GENERIC-CI".equalsIgnoreCase(type)) {
90 javaGenerator = new DAOGenerator(new GenericCIDAOTemplate(),
91 isJava5Targeted());
92 } else if ("GENERIC-SI".equalsIgnoreCase(type)) {
93 javaGenerator = new DAOGenerator(new GenericSIDAOTemplate(),
94 isJava5Targeted());
95 } else {
96 javaGenerator = (AbstractJavaGenerator) ObjectFactory
97 .createInternalObject(type);
98 }
99
100 initializeAbstractGenerator(javaGenerator, warnings, progressCallback);
101 daoGenerators.add(javaGenerator);
102 }
103
104 protected void calculateJavaModelGenerators(List<String> warnings,
105 ProgressCallback progressCallback) {
106 if (getRules().generateExampleClass()) {
107 AbstractJavaGenerator javaGenerator = new ExampleGenerator(
108 isJava5Targeted());
109 initializeAbstractGenerator(javaGenerator, warnings,
110 progressCallback);
111 javaModelGenerators.add(javaGenerator);
112 }
113
114 if (getRules().generatePrimaryKeyClass()) {
115 AbstractJavaGenerator javaGenerator = new PrimaryKeyGenerator();
116 initializeAbstractGenerator(javaGenerator, warnings,
117 progressCallback);
118 javaModelGenerators.add(javaGenerator);
119 }
120
121 if (getRules().generateBaseRecordClass()) {
122 AbstractJavaGenerator javaGenerator = new BaseRecordGenerator();
123 initializeAbstractGenerator(javaGenerator, warnings,
124 progressCallback);
125 javaModelGenerators.add(javaGenerator);
126 }
127
128 if (getRules().generateRecordWithBLOBsClass()) {
129 AbstractJavaGenerator javaGenerator = new RecordWithBLOBsGenerator();
130 initializeAbstractGenerator(javaGenerator, warnings,
131 progressCallback);
132 javaModelGenerators.add(javaGenerator);
133 }
134 }
135
136 protected void initializeAbstractGenerator(
137 AbstractGenerator abstractGenerator, List<String> warnings,
138 ProgressCallback progressCallback) {
139 abstractGenerator.setContext(context);
140 abstractGenerator.setIntrospectedTable(this);
141 abstractGenerator.setProgressCallback(progressCallback);
142 abstractGenerator.setWarnings(warnings);
143 }
144
145 @Override
146 public List<GeneratedJavaFile> getGeneratedJavaFiles() {
147 List<GeneratedJavaFile> answer = new ArrayList<GeneratedJavaFile>();
148
149 for (AbstractJavaGenerator javaGenerator : javaModelGenerators) {
150 List<CompilationUnit> compilationUnits = javaGenerator
151 .getCompilationUnits();
152 for (CompilationUnit compilationUnit : compilationUnits) {
153 GeneratedJavaFile gjf = new GeneratedJavaFile(compilationUnit,
154 context.getJavaModelGeneratorConfiguration()
155 .getTargetProject(),
156 context.getProperty(PropertyRegistry.CONTEXT_JAVA_FILE_ENCODING),
157 context.getJavaFormatter());
158 answer.add(gjf);
159 }
160 }
161
162 for (AbstractJavaGenerator javaGenerator : daoGenerators) {
163 List<CompilationUnit> compilationUnits = javaGenerator
164 .getCompilationUnits();
165 for (CompilationUnit compilationUnit : compilationUnits) {
166 GeneratedJavaFile gjf = new GeneratedJavaFile(compilationUnit,
167 context.getJavaClientGeneratorConfiguration()
168 .getTargetProject(),
169 context.getProperty(PropertyRegistry.CONTEXT_JAVA_FILE_ENCODING),
170 context.getJavaFormatter());
171 answer.add(gjf);
172 }
173 }
174
175 return answer;
176 }
177
178 @Override
179 public List<GeneratedXmlFile> getGeneratedXmlFiles() {
180 List<GeneratedXmlFile> answer = new ArrayList<GeneratedXmlFile>();
181
182 Document document = sqlMapGenerator.getDocument();
183 GeneratedXmlFile gxf = new GeneratedXmlFile(document,
184 getIbatis2SqlMapFileName(), getIbatis2SqlMapPackage(), context
185 .getSqlMapGeneratorConfiguration().getTargetProject(),
186 true, context.getXmlFormatter());
187 if (context.getPlugins().sqlMapGenerated(gxf, this)) {
188 answer.add(gxf);
189 }
190
191 return answer;
192 }
193
194 @Override
195 public boolean isJava5Targeted() {
196 return false;
197 }
198
199 @Override
200 public int getGenerationSteps() {
201 return javaModelGenerators.size() + daoGenerators.size() + 1;
202
203
204 }
205
206 @Override
207 public boolean requiresXMLGenerator() {
208 return true;
209 }
210 }