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.ibatis2.model;
17  
18  import static org.mybatis.generator.internal.util.JavaBeansUtil.getGetterMethodName;
19  import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
20  import static org.mybatis.generator.internal.util.messages.Messages.getString;
21  
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import org.mybatis.generator.api.CommentGenerator;
27  import org.mybatis.generator.api.FullyQualifiedTable;
28  import org.mybatis.generator.api.IntrospectedColumn;
29  import org.mybatis.generator.api.dom.OutputUtilities;
30  import org.mybatis.generator.api.dom.java.CompilationUnit;
31  import org.mybatis.generator.api.dom.java.Field;
32  import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
33  import org.mybatis.generator.api.dom.java.InnerClass;
34  import org.mybatis.generator.api.dom.java.JavaVisibility;
35  import org.mybatis.generator.api.dom.java.Method;
36  import org.mybatis.generator.api.dom.java.Parameter;
37  import org.mybatis.generator.api.dom.java.TopLevelClass;
38  import org.mybatis.generator.codegen.AbstractJavaGenerator;
39  import org.mybatis.generator.codegen.ibatis2.Ibatis2FormattingUtilities;
40  import org.mybatis.generator.internal.rules.Rules;
41  
42  /**
43   * 
44   * @author Jeff Butler
45   * 
46   */
47  public class ExampleGenerator extends AbstractJavaGenerator {
48  
49      private boolean generateForJava5;
50  
51      public ExampleGenerator(boolean generateForJava5) {
52          super();
53          this.generateForJava5 = generateForJava5;
54      }
55  
56      @Override
57      public List<CompilationUnit> getCompilationUnits() {
58          FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
59          progressCallback.startTask(getString(
60                  "Progress.6", table.toString())); //$NON-NLS-1$
61          CommentGenerator commentGenerator = context.getCommentGenerator();
62  
63          FullyQualifiedJavaType type = new FullyQualifiedJavaType(
64                  introspectedTable.getExampleType());
65          TopLevelClass topLevelClass = new TopLevelClass(type);
66          topLevelClass.setVisibility(JavaVisibility.PUBLIC);
67          commentGenerator.addJavaFileComment(topLevelClass);
68  
69          // add default constructor
70          Method method = new Method();
71          method.setVisibility(JavaVisibility.PUBLIC);
72          method.setConstructor(true);
73          method.setName(type.getShortName());
74          if (generateForJava5) {
75              method.addBodyLine("oredCriteria = new ArrayList<Criteria>();"); //$NON-NLS-1$
76          } else {
77              method.addBodyLine("oredCriteria = new ArrayList();"); //$NON-NLS-1$
78          }
79  
80          commentGenerator.addGeneralMethodComment(method, introspectedTable);
81          topLevelClass.addMethod(method);
82  
83          // add shallow copy constructor if the update by
84          // example methods are enabled - because the parameter
85          // class for update by example methods will subclass this class
86          Rules rules = introspectedTable.getRules();
87          if (rules.generateUpdateByExampleSelective()
88                  || rules.generateUpdateByExampleWithBLOBs()
89                  || rules.generateUpdateByExampleWithoutBLOBs()) {
90              method = new Method();
91              method.setVisibility(JavaVisibility.PROTECTED);
92              method.setConstructor(true);
93              method.setName(type.getShortName());
94              method.addParameter(new Parameter(type, "example")); //$NON-NLS-1$
95              method.addBodyLine("this.orderByClause = example.orderByClause;"); //$NON-NLS-1$
96              method.addBodyLine("this.oredCriteria = example.oredCriteria;"); //$NON-NLS-1$
97              method.addBodyLine("this.distinct = example.distinct;"); //$NON-NLS-1$
98              commentGenerator.addGeneralMethodComment(method, introspectedTable);
99              topLevelClass.addMethod(method);
100         }
101 
102         // add field, getter, setter for orderby clause
103         Field field = new Field();
104         field.setVisibility(JavaVisibility.PROTECTED);
105         field.setType(FullyQualifiedJavaType.getStringInstance());
106         field.setName("orderByClause"); //$NON-NLS-1$
107         commentGenerator.addFieldComment(field, introspectedTable);
108         topLevelClass.addField(field);
109 
110         method = new Method();
111         method.setVisibility(JavaVisibility.PUBLIC);
112         method.setName("setOrderByClause"); //$NON-NLS-1$
113         method.addParameter(new Parameter(FullyQualifiedJavaType
114                 .getStringInstance(), "orderByClause")); //$NON-NLS-1$
115         method.addBodyLine("this.orderByClause = orderByClause;"); //$NON-NLS-1$
116         commentGenerator.addGeneralMethodComment(method, introspectedTable);
117         topLevelClass.addMethod(method);
118 
119         method = new Method();
120         method.setVisibility(JavaVisibility.PUBLIC);
121         method.setReturnType(FullyQualifiedJavaType.getStringInstance());
122         method.setName("getOrderByClause"); //$NON-NLS-1$
123         method.addBodyLine("return orderByClause;"); //$NON-NLS-1$
124         commentGenerator.addGeneralMethodComment(method, introspectedTable);
125         topLevelClass.addMethod(method);
126 
127         // add field, getter, setter for distinct
128         field = new Field();
129         field.setVisibility(JavaVisibility.PROTECTED);
130         field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
131         field.setName("distinct"); //$NON-NLS-1$
132         commentGenerator.addFieldComment(field, introspectedTable);
133         topLevelClass.addField(field);
134 
135         method = new Method();
136         method.setVisibility(JavaVisibility.PUBLIC);
137         method.setName("setDistinct"); //$NON-NLS-1$
138         method.addParameter(new Parameter(FullyQualifiedJavaType
139                 .getBooleanPrimitiveInstance(), "distinct")); //$NON-NLS-1$
140         method.addBodyLine("this.distinct = distinct;"); //$NON-NLS-1$
141         commentGenerator.addGeneralMethodComment(method, introspectedTable);
142         topLevelClass.addMethod(method);
143 
144         method = new Method();
145         method.setVisibility(JavaVisibility.PUBLIC);
146         method.setReturnType(FullyQualifiedJavaType
147                 .getBooleanPrimitiveInstance());
148         method.setName("isDistinct"); //$NON-NLS-1$
149         method.addBodyLine("return distinct;"); //$NON-NLS-1$
150         commentGenerator.addGeneralMethodComment(method, introspectedTable);
151         topLevelClass.addMethod(method);
152 
153         // add field and methods for the list of ored criteria
154         field = new Field();
155         field.setVisibility(JavaVisibility.PROTECTED);
156 
157         FullyQualifiedJavaType fqjt;
158         if (generateForJava5) {
159             fqjt = new FullyQualifiedJavaType("java.util.List<Criteria>"); //$NON-NLS-1$
160         } else {
161             fqjt = new FullyQualifiedJavaType("java.util.List"); //$NON-NLS-1$
162         }
163 
164         field.setType(fqjt);
165         field.setName("oredCriteria"); //$NON-NLS-1$
166         commentGenerator.addFieldComment(field, introspectedTable);
167         topLevelClass.addField(field);
168 
169         method = new Method();
170         method.setVisibility(JavaVisibility.PUBLIC);
171         method.setReturnType(fqjt);
172         method.setName("getOredCriteria"); //$NON-NLS-1$
173         method.addBodyLine("return oredCriteria;"); //$NON-NLS-1$
174         commentGenerator.addGeneralMethodComment(method, introspectedTable);
175         topLevelClass.addMethod(method);
176 
177         method = new Method();
178         method.setVisibility(JavaVisibility.PUBLIC);
179         method.setName("or"); //$NON-NLS-1$
180         method.addParameter(new Parameter(FullyQualifiedJavaType
181                 .getCriteriaInstance(), "criteria")); //$NON-NLS-1$
182         method.addBodyLine("oredCriteria.add(criteria);"); //$NON-NLS-1$
183         commentGenerator.addGeneralMethodComment(method, introspectedTable);
184         topLevelClass.addMethod(method);
185 
186         method = new Method();
187         method.setVisibility(JavaVisibility.PUBLIC);
188         method.setName("or"); //$NON-NLS-1$
189         method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
190         method.addBodyLine("Criteria criteria = createCriteriaInternal();"); //$NON-NLS-1$
191         method.addBodyLine("oredCriteria.add(criteria);"); //$NON-NLS-1$
192         method.addBodyLine("return criteria;"); //$NON-NLS-1$
193         commentGenerator.addGeneralMethodComment(method, introspectedTable);
194         topLevelClass.addMethod(method);
195 
196         method = new Method();
197         method.setVisibility(JavaVisibility.PUBLIC);
198         method.setName("createCriteria"); //$NON-NLS-1$
199         method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
200         method.addBodyLine("Criteria criteria = createCriteriaInternal();"); //$NON-NLS-1$
201         method.addBodyLine("if (oredCriteria.size() == 0) {"); //$NON-NLS-1$
202         method.addBodyLine("oredCriteria.add(criteria);"); //$NON-NLS-1$
203         method.addBodyLine("}"); //$NON-NLS-1$
204         method.addBodyLine("return criteria;"); //$NON-NLS-1$
205         commentGenerator.addGeneralMethodComment(method, introspectedTable);
206         topLevelClass.addMethod(method);
207 
208         method = new Method();
209         method.setVisibility(JavaVisibility.PROTECTED);
210         method.setName("createCriteriaInternal"); //$NON-NLS-1$
211         method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
212         method.addBodyLine("Criteria criteria = new Criteria();"); //$NON-NLS-1$
213         method.addBodyLine("return criteria;"); //$NON-NLS-1$
214         commentGenerator.addGeneralMethodComment(method, introspectedTable);
215         topLevelClass.addMethod(method);
216 
217         method = new Method();
218         method.setVisibility(JavaVisibility.PUBLIC);
219         method.setName("clear"); //$NON-NLS-1$
220         method.addBodyLine("oredCriteria.clear();"); //$NON-NLS-1$
221         method.addBodyLine("orderByClause = null;"); //$NON-NLS-1$
222         method.addBodyLine("distinct = false;"); //$NON-NLS-1$
223         commentGenerator.addGeneralMethodComment(method, introspectedTable);
224         topLevelClass.addMethod(method);
225 
226         // now generate the inner class that holds the AND conditions
227         topLevelClass
228                 .addInnerClass(getGeneratedCriteriaInnerClass(topLevelClass));
229 
230         topLevelClass.addInnerClass(getCriteriaInnerClass());
231 
232         List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
233         if (context.getPlugins().modelExampleClassGenerated(
234                 topLevelClass, introspectedTable)) {
235             answer.add(topLevelClass);
236         }
237         return answer;
238     }
239 
240     private InnerClass getCriteriaInnerClass() {
241         Method method;
242 
243         InnerClass answer = new InnerClass(FullyQualifiedJavaType
244                 .getCriteriaInstance());
245 
246         answer.setVisibility(JavaVisibility.PUBLIC);
247         answer.setStatic(true);
248         answer.setSuperClass(FullyQualifiedJavaType
249                 .getGeneratedCriteriaInstance());
250 
251         context.getCommentGenerator().addClassComment(answer,
252                 introspectedTable, true);
253 
254         method = new Method();
255         method.setVisibility(JavaVisibility.PROTECTED);
256         method.setName("Criteria"); //$NON-NLS-1$
257         method.setConstructor(true);
258         method.addBodyLine("super();"); //$NON-NLS-1$
259         answer.addMethod(method);
260 
261         return answer;
262     }
263 
264     private InnerClass getGeneratedCriteriaInnerClass(
265             TopLevelClass topLevelClass) {
266         Field field;
267         Method method;
268 
269         InnerClass answer = new InnerClass(FullyQualifiedJavaType
270                 .getGeneratedCriteriaInstance());
271 
272         answer.setVisibility(JavaVisibility.PROTECTED);
273         answer.setStatic(true);
274         answer.setAbstract(true);
275         context.getCommentGenerator().addClassComment(answer,
276                 introspectedTable);
277 
278         method = new Method();
279         method.setVisibility(JavaVisibility.PROTECTED);
280         method.setName("GeneratedCriteria"); //$NON-NLS-1$
281         method.setConstructor(true);
282         method.addBodyLine("super();"); //$NON-NLS-1$
283         if (generateForJava5) {
284             method
285                     .addBodyLine("criteriaWithoutValue = new ArrayList<String>();"); //$NON-NLS-1$
286             method
287                     .addBodyLine("criteriaWithSingleValue = new ArrayList<Map<String, Object>>();"); //$NON-NLS-1$
288             method
289                     .addBodyLine("criteriaWithListValue = new ArrayList<Map<String, Object>>();"); //$NON-NLS-1$
290             method
291                     .addBodyLine("criteriaWithBetweenValue = new ArrayList<Map<String, Object>>();"); //$NON-NLS-1$
292 
293         } else {
294             method.addBodyLine("criteriaWithoutValue = new ArrayList();"); //$NON-NLS-1$
295             method.addBodyLine("criteriaWithSingleValue = new ArrayList();"); //$NON-NLS-1$
296             method.addBodyLine("criteriaWithListValue = new ArrayList();"); //$NON-NLS-1$
297             method.addBodyLine("criteriaWithBetweenValue = new ArrayList();"); //$NON-NLS-1$
298         }
299         answer.addMethod(method);
300 
301         List<String> criteriaLists = new ArrayList<String>();
302         criteriaLists.add("criteriaWithoutValue"); //$NON-NLS-1$
303         criteriaLists.add("criteriaWithSingleValue"); //$NON-NLS-1$
304         criteriaLists.add("criteriaWithListValue"); //$NON-NLS-1$
305         criteriaLists.add("criteriaWithBetweenValue"); //$NON-NLS-1$
306 
307         for (IntrospectedColumn introspectedColumn : introspectedTable
308                 .getNonBLOBColumns()) {
309             if (stringHasValue(introspectedColumn
310                     .getTypeHandler())) {
311                 criteriaLists.addAll(addtypeHandledObjectsAndMethods(
312                         introspectedColumn, method, answer));
313             }
314         }
315 
316         // now generate the isValid method
317         method = new Method();
318         method.setVisibility(JavaVisibility.PUBLIC);
319         method.setName("isValid"); //$NON-NLS-1$
320         method.setReturnType(FullyQualifiedJavaType
321                 .getBooleanPrimitiveInstance());
322         StringBuilder sb = new StringBuilder();
323         Iterator<String> strIter = criteriaLists.iterator();
324         sb.append("return "); //$NON-NLS-1$
325         sb.append(strIter.next());
326         sb.append(".size() > 0"); //$NON-NLS-1$
327         method.addBodyLine(sb.toString());
328         while (strIter.hasNext()) {
329             sb.setLength(0);
330             OutputUtilities.javaIndent(sb, 1);
331             sb.append("|| "); //$NON-NLS-1$
332             sb.append(strIter.next());
333             sb.append(".size() > 0"); //$NON-NLS-1$
334             if (!strIter.hasNext()) {
335                 sb.append(';');
336             }
337             method.addBodyLine(sb.toString());
338         }
339         answer.addMethod(method);
340 
341         // now we need to generate the methods that will be used in the SqlMap
342         // to generate the dynamic where clause
343         topLevelClass.addImportedType(FullyQualifiedJavaType
344                 .getNewMapInstance());
345         topLevelClass.addImportedType(FullyQualifiedJavaType
346                 .getNewListInstance());
347         topLevelClass.addImportedType(FullyQualifiedJavaType
348                 .getNewHashMapInstance());
349         topLevelClass.addImportedType(FullyQualifiedJavaType
350                 .getNewArrayListInstance());
351 
352         field = new Field();
353         field.setVisibility(JavaVisibility.PROTECTED);
354         FullyQualifiedJavaType listOfStrings;
355         if (generateForJava5) {
356             listOfStrings = new FullyQualifiedJavaType(
357                     "java.util.List<java.lang.String>"); //$NON-NLS-1$
358         } else {
359             listOfStrings = new FullyQualifiedJavaType("java.util.List"); //$NON-NLS-1$
360         }
361         field.setType(listOfStrings);
362         field.setName("criteriaWithoutValue"); //$NON-NLS-1$
363         answer.addField(field);
364 
365         method = new Method();
366         method.setVisibility(JavaVisibility.PUBLIC);
367         method.setReturnType(field.getType());
368         method.setName(getGetterMethodName(field.getName(), field
369                 .getType()));
370         method.addBodyLine("return criteriaWithoutValue;"); //$NON-NLS-1$
371         answer.addMethod(method);
372 
373         FullyQualifiedJavaType listOfMaps;
374         if (generateForJava5) {
375             listOfMaps = new FullyQualifiedJavaType(
376                     "java.util.List<java.util.Map<java.lang.String, java.lang.Object>>"); //$NON-NLS-1$
377         } else {
378             listOfMaps = new FullyQualifiedJavaType("java.util.List"); //$NON-NLS-1$
379         }
380 
381         field = new Field();
382         field.setVisibility(JavaVisibility.PROTECTED);
383         field.setType(listOfMaps);
384         field.setName("criteriaWithSingleValue"); //$NON-NLS-1$
385         answer.addField(field);
386 
387         method = new Method();
388         method.setVisibility(JavaVisibility.PUBLIC);
389         method.setReturnType(field.getType());
390         method.setName(getGetterMethodName(field.getName(), field
391                 .getType()));
392         method.addBodyLine("return criteriaWithSingleValue;"); //$NON-NLS-1$
393         answer.addMethod(method);
394 
395         field = new Field();
396         field.setVisibility(JavaVisibility.PROTECTED);
397         field.setType(listOfMaps);
398         field.setName("criteriaWithListValue"); //$NON-NLS-1$
399         answer.addField(field);
400 
401         method = new Method();
402         method.setVisibility(JavaVisibility.PUBLIC);
403         method.setReturnType(field.getType());
404         method.setName(getGetterMethodName(field.getName(), field
405                 .getType()));
406         method.addBodyLine("return criteriaWithListValue;"); //$NON-NLS-1$
407         answer.addMethod(method);
408 
409         field = new Field();
410         field.setVisibility(JavaVisibility.PROTECTED);
411         field.setType(listOfMaps);
412         field.setName("criteriaWithBetweenValue"); //$NON-NLS-1$
413         answer.addField(field);
414 
415         method = new Method();
416         method.setVisibility(JavaVisibility.PUBLIC);
417         method.setReturnType(field.getType());
418         method.setName(getGetterMethodName(field.getName(), field
419                 .getType()));
420         method.addBodyLine("return criteriaWithBetweenValue;"); //$NON-NLS-1$
421         answer.addMethod(method);
422 
423         // now add the methods for simplifying the individual field set methods
424         method = new Method();
425         method.setVisibility(JavaVisibility.PROTECTED);
426         method.setName("addCriterion"); //$NON-NLS-1$
427         method.addParameter(new Parameter(FullyQualifiedJavaType
428                 .getStringInstance(), "condition")); //$NON-NLS-1$
429         method.addBodyLine("if (condition == null) {"); //$NON-NLS-1$
430         method
431                 .addBodyLine("throw new RuntimeException(\"Value for condition cannot be null\");"); //$NON-NLS-1$
432         method.addBodyLine("}"); //$NON-NLS-1$
433         method.addBodyLine("criteriaWithoutValue.add(condition);"); //$NON-NLS-1$
434         answer.addMethod(method);
435 
436         method = new Method();
437         method.setVisibility(JavaVisibility.PROTECTED);
438         method.setName("addCriterion"); //$NON-NLS-1$
439         method.addParameter(new Parameter(FullyQualifiedJavaType
440                 .getStringInstance(), "condition")); //$NON-NLS-1$
441         method.addParameter(new Parameter(FullyQualifiedJavaType
442                 .getObjectInstance(), "value")); //$NON-NLS-1$
443         method.addParameter(new Parameter(FullyQualifiedJavaType
444                 .getStringInstance(), "property")); //$NON-NLS-1$
445         method.addBodyLine("if (value == null) {"); //$NON-NLS-1$
446         method
447                 .addBodyLine("throw new RuntimeException(\"Value for \" + property + \" cannot be null\");"); //$NON-NLS-1$
448         method.addBodyLine("}"); //$NON-NLS-1$
449         if (generateForJava5) {
450             method
451                     .addBodyLine("Map<String, Object> map = new HashMap<String, Object>();"); //$NON-NLS-1$
452         } else {
453             method.addBodyLine("Map map = new HashMap();"); //$NON-NLS-1$
454         }
455 
456         method.addBodyLine("map.put(\"condition\", condition);"); //$NON-NLS-1$
457         method.addBodyLine("map.put(\"value\", value);"); //$NON-NLS-1$
458         method.addBodyLine("criteriaWithSingleValue.add(map);"); //$NON-NLS-1$
459         answer.addMethod(method);
460 
461         FullyQualifiedJavaType listOfObjects;
462         if (generateForJava5) {
463             listOfObjects = new FullyQualifiedJavaType(
464                     "java.util.List<? extends java.lang.Object>"); //$NON-NLS-1$
465         } else {
466             listOfObjects = new FullyQualifiedJavaType("java.util.List"); //$NON-NLS-1$
467         }
468 
469         method = new Method();
470         method.setVisibility(JavaVisibility.PROTECTED);
471         method.setName("addCriterion"); //$NON-NLS-1$
472         method.addParameter(new Parameter(FullyQualifiedJavaType
473                 .getStringInstance(), "condition")); //$NON-NLS-1$
474         method.addParameter(new Parameter(listOfObjects, "values")); //$NON-NLS-1$
475         method.addParameter(new Parameter(FullyQualifiedJavaType
476                 .getStringInstance(), "property")); //$NON-NLS-1$
477         method.addBodyLine("if (values == null || values.size() == 0) {"); //$NON-NLS-1$
478         method
479                 .addBodyLine("throw new RuntimeException(\"Value list for \" + property + \" cannot be null or empty\");"); //$NON-NLS-1$
480         method.addBodyLine("}"); //$NON-NLS-1$
481         if (generateForJava5) {
482             method
483                     .addBodyLine("Map<String, Object> map = new HashMap<String, Object>();"); //$NON-NLS-1$
484         } else {
485             method.addBodyLine("Map map = new HashMap();"); //$NON-NLS-1$
486         }
487 
488         method.addBodyLine("map.put(\"condition\", condition);"); //$NON-NLS-1$
489         method.addBodyLine("map.put(\"values\", values);"); //$NON-NLS-1$
490         method.addBodyLine("criteriaWithListValue.add(map);"); //$NON-NLS-1$
491         answer.addMethod(method);
492 
493         method = new Method();
494         method.setVisibility(JavaVisibility.PROTECTED);
495         method.setName("addCriterion"); //$NON-NLS-1$
496         method.addParameter(new Parameter(FullyQualifiedJavaType
497                 .getStringInstance(), "condition")); //$NON-NLS-1$
498         method.addParameter(new Parameter(FullyQualifiedJavaType
499                 .getObjectInstance(), "value1")); //$NON-NLS-1$
500         method.addParameter(new Parameter(FullyQualifiedJavaType
501                 .getObjectInstance(), "value2")); //$NON-NLS-1$
502         method.addParameter(new Parameter(FullyQualifiedJavaType
503                 .getStringInstance(), "property")); //$NON-NLS-1$
504         method.addBodyLine("if (value1 == null || value2 == null) {"); //$NON-NLS-1$
505         method
506                 .addBodyLine("throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");"); //$NON-NLS-1$
507         method.addBodyLine("}"); //$NON-NLS-1$
508         if (generateForJava5) {
509             method.addBodyLine("List<Object> list = new ArrayList<Object>();"); //$NON-NLS-1$
510         } else {
511             method.addBodyLine("List list = new ArrayList();"); //$NON-NLS-1$
512         }
513 
514         method.addBodyLine("list.add(value1);"); //$NON-NLS-1$
515         method.addBodyLine("list.add(value2);"); //$NON-NLS-1$
516         if (generateForJava5) {
517             method
518                     .addBodyLine("Map<String, Object> map = new HashMap<String, Object>();"); //$NON-NLS-1$
519         } else {
520             method.addBodyLine("Map map = new HashMap();"); //$NON-NLS-1$
521         }
522         method.addBodyLine("map.put(\"condition\", condition);"); //$NON-NLS-1$
523         method.addBodyLine("map.put(\"values\", list);"); //$NON-NLS-1$
524         method.addBodyLine("criteriaWithBetweenValue.add(map);"); //$NON-NLS-1$
525         answer.addMethod(method);
526 
527         FullyQualifiedJavaType listOfDates;
528         if (generateForJava5) {
529             listOfDates = new FullyQualifiedJavaType(
530                     "java.util.List<java.util.Date>"); //$NON-NLS-1$
531         } else {
532             listOfDates = new FullyQualifiedJavaType("java.util.List"); //$NON-NLS-1$
533         }
534 
535         if (introspectedTable.hasJDBCDateColumns()) {
536             topLevelClass.addImportedType(FullyQualifiedJavaType
537                     .getDateInstance());
538             topLevelClass.addImportedType(FullyQualifiedJavaType
539                     .getNewIteratorInstance());
540             method = new Method();
541             method.setVisibility(JavaVisibility.PROTECTED);
542             method.setName("addCriterionForJDBCDate"); //$NON-NLS-1$
543             method.addParameter(new Parameter(FullyQualifiedJavaType
544                     .getStringInstance(), "condition")); //$NON-NLS-1$
545             method.addParameter(new Parameter(FullyQualifiedJavaType
546                     .getDateInstance(), "value")); //$NON-NLS-1$
547             method.addParameter(new Parameter(FullyQualifiedJavaType
548                     .getStringInstance(), "property")); //$NON-NLS-1$
549             method.addBodyLine("if (value == null) {"); //$NON-NLS-1$
550             method
551                     .addBodyLine("throw new RuntimeException(\"Value for \" + property + \" cannot be null\");"); //$NON-NLS-1$
552             method.addBodyLine("}"); //$NON-NLS-1$
553             method
554                     .addBodyLine("addCriterion(condition, new java.sql.Date(value.getTime()), property);"); //$NON-NLS-1$
555             answer.addMethod(method);
556 
557             method = new Method();
558             method.setVisibility(JavaVisibility.PROTECTED);
559             method.setName("addCriterionForJDBCDate"); //$NON-NLS-1$
560             method.addParameter(new Parameter(FullyQualifiedJavaType
561                     .getStringInstance(), "condition")); //$NON-NLS-1$
562             method.addParameter(new Parameter(listOfDates, "values")); //$NON-NLS-1$
563             method.addParameter(new Parameter(FullyQualifiedJavaType
564                     .getStringInstance(), "property")); //$NON-NLS-1$
565             method.addBodyLine("if (values == null || values.size() == 0) {"); //$NON-NLS-1$
566             method
567                     .addBodyLine("throw new RuntimeException(\"Value list for \" + property + \" cannot be null or empty\");"); //$NON-NLS-1$
568             method.addBodyLine("}"); //$NON-NLS-1$
569             if (generateForJava5) {
570                 method
571                         .addBodyLine("List<java.sql.Date> dateList = new ArrayList<java.sql.Date>();"); //$NON-NLS-1$
572                 method.addBodyLine("Iterator<Date> iter = values.iterator();"); //$NON-NLS-1$
573                 method.addBodyLine("while (iter.hasNext()) {"); //$NON-NLS-1$
574                 method
575                         .addBodyLine("dateList.add(new java.sql.Date(iter.next().getTime()));"); //$NON-NLS-1$
576                 method.addBodyLine("}"); //$NON-NLS-1$
577             } else {
578                 method.addBodyLine("List dateList = new ArrayList();"); //$NON-NLS-1$
579                 method.addBodyLine("Iterator iter = values.iterator();"); //$NON-NLS-1$
580                 method.addBodyLine("while (iter.hasNext()) {"); //$NON-NLS-1$
581                 method
582                         .addBodyLine("dateList.add(new java.sql.Date(((Date)iter.next()).getTime()));"); //$NON-NLS-1$
583                 method.addBodyLine("}"); //$NON-NLS-1$
584             }
585             method.addBodyLine("addCriterion(condition, dateList, property);"); //$NON-NLS-1$
586             answer.addMethod(method);
587 
588             method = new Method();
589             method.setVisibility(JavaVisibility.PROTECTED);
590             method.setName("addCriterionForJDBCDate"); //$NON-NLS-1$
591             method.addParameter(new Parameter(FullyQualifiedJavaType
592                     .getStringInstance(), "condition")); //$NON-NLS-1$
593             method.addParameter(new Parameter(FullyQualifiedJavaType
594                     .getDateInstance(), "value1")); //$NON-NLS-1$
595             method.addParameter(new Parameter(FullyQualifiedJavaType
596                     .getDateInstance(), "value2")); //$NON-NLS-1$
597             method.addParameter(new Parameter(FullyQualifiedJavaType
598                     .getStringInstance(), "property")); //$NON-NLS-1$
599             method.addBodyLine("if (value1 == null || value2 == null) {"); //$NON-NLS-1$
600             method
601                     .addBodyLine("throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");"); //$NON-NLS-1$
602             method.addBodyLine("}"); //$NON-NLS-1$
603             method
604                     .addBodyLine("addCriterion(condition, new java.sql.Date(value1.getTime()), new java.sql.Date(value2.getTime()), property);"); //$NON-NLS-1$
605             answer.addMethod(method);
606         }
607 
608         if (introspectedTable.hasJDBCTimeColumns()) {
609             topLevelClass.addImportedType(FullyQualifiedJavaType
610                     .getDateInstance());
611             topLevelClass.addImportedType(FullyQualifiedJavaType
612                     .getNewIteratorInstance());
613             method = new Method();
614             method.setVisibility(JavaVisibility.PROTECTED);
615             method.setName("addCriterionForJDBCTime"); //$NON-NLS-1$
616             method.addParameter(new Parameter(FullyQualifiedJavaType
617                     .getStringInstance(), "condition")); //$NON-NLS-1$
618             method.addParameter(new Parameter(FullyQualifiedJavaType
619                     .getDateInstance(), "value")); //$NON-NLS-1$
620             method.addParameter(new Parameter(FullyQualifiedJavaType
621                     .getStringInstance(), "property")); //$NON-NLS-1$
622             method.addBodyLine("if (value == null) {"); //$NON-NLS-1$
623             method
624                     .addBodyLine("throw new RuntimeException(\"Value for \" + property + \" cannot be null\");"); //$NON-NLS-1$
625             method.addBodyLine("}"); //$NON-NLS-1$
626             method
627                     .addBodyLine("addCriterion(condition, new java.sql.Time(value.getTime()), property);"); //$NON-NLS-1$
628             answer.addMethod(method);
629 
630             method = new Method();
631             method.setVisibility(JavaVisibility.PROTECTED);
632             method.setName("addCriterionForJDBCTime"); //$NON-NLS-1$
633             method.addParameter(new Parameter(FullyQualifiedJavaType
634                     .getStringInstance(), "condition")); //$NON-NLS-1$
635             method.addParameter(new Parameter(listOfDates, "values")); //$NON-NLS-1$
636             method.addParameter(new Parameter(FullyQualifiedJavaType
637                     .getStringInstance(), "property")); //$NON-NLS-1$
638             method.addBodyLine("if (values == null || values.size() == 0) {"); //$NON-NLS-1$
639             method
640                     .addBodyLine("throw new RuntimeException(\"Value list for \" + property + \" cannot be null or empty\");"); //$NON-NLS-1$
641             method.addBodyLine("}"); //$NON-NLS-1$
642             if (generateForJava5) {
643                 method
644                         .addBodyLine("List<java.sql.Time> timeList = new ArrayList<java.sql.Time>();"); //$NON-NLS-1$
645                 method.addBodyLine("Iterator<Date> iter = values.iterator();"); //$NON-NLS-1$
646                 method.addBodyLine("while (iter.hasNext()) {"); //$NON-NLS-1$
647                 method
648                         .addBodyLine("timeList.add(new java.sql.Time(iter.next().getTime()));"); //$NON-NLS-1$
649                 method.addBodyLine("}"); //$NON-NLS-1$
650             } else {
651                 method.addBodyLine("List timeList = new ArrayList();"); //$NON-NLS-1$
652                 method.addBodyLine("Iterator iter = values.iterator();"); //$NON-NLS-1$
653                 method.addBodyLine("while (iter.hasNext()) {"); //$NON-NLS-1$
654                 method
655                         .addBodyLine("timeList.add(new java.sql.Time(((Date)iter.next()).getTime()));"); //$NON-NLS-1$
656                 method.addBodyLine("}"); //$NON-NLS-1$
657             }
658             method.addBodyLine("addCriterion(condition, timeList, property);"); //$NON-NLS-1$
659             answer.addMethod(method);
660 
661             method = new Method();
662             method.setVisibility(JavaVisibility.PROTECTED);
663             method.setName("addCriterionForJDBCTime"); //$NON-NLS-1$
664             method.addParameter(new Parameter(FullyQualifiedJavaType
665                     .getStringInstance(), "condition")); //$NON-NLS-1$
666             method.addParameter(new Parameter(FullyQualifiedJavaType
667                     .getDateInstance(), "value1")); //$NON-NLS-1$
668             method.addParameter(new Parameter(FullyQualifiedJavaType
669                     .getDateInstance(), "value2")); //$NON-NLS-1$
670             method.addParameter(new Parameter(FullyQualifiedJavaType
671                     .getStringInstance(), "property")); //$NON-NLS-1$
672             method.addBodyLine("if (value1 == null || value2 == null) {"); //$NON-NLS-1$
673             method
674                     .addBodyLine("throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");"); //$NON-NLS-1$
675             method.addBodyLine("}"); //$NON-NLS-1$
676             method
677                     .addBodyLine("addCriterion(condition, new java.sql.Time(value1.getTime()), new java.sql.Time(value2.getTime()), property);"); //$NON-NLS-1$
678             answer.addMethod(method);
679         }
680 
681         for (IntrospectedColumn introspectedColumn : introspectedTable
682                 .getNonBLOBColumns()) {
683             topLevelClass.addImportedType(introspectedColumn
684                     .getFullyQualifiedJavaType());
685 
686             // here we need to add the individual methods for setting the
687             // conditions for a field
688             answer.addMethod(getSetNullMethod(introspectedColumn));
689             answer.addMethod(getSetNotNullMethod(introspectedColumn));
690             answer.addMethod(getSetEqualMethod(introspectedColumn));
691             answer.addMethod(getSetNotEqualMethod(introspectedColumn));
692             answer.addMethod(getSetGreaterThanMethod(introspectedColumn));
693             answer
694                     .addMethod(getSetGreaterThenOrEqualMethod(introspectedColumn));
695             answer.addMethod(getSetLessThanMethod(introspectedColumn));
696             answer.addMethod(getSetLessThanOrEqualMethod(introspectedColumn));
697 
698             if (introspectedColumn.isJdbcCharacterColumn()) {
699                 answer.addMethod(getSetLikeMethod(introspectedColumn));
700                 answer.addMethod(getSetNotLikeMethod(introspectedColumn));
701             }
702 
703             answer.addMethod(getSetInOrNotInMethod(introspectedColumn, true));
704             answer.addMethod(getSetInOrNotInMethod(introspectedColumn, false));
705             answer.addMethod(getSetBetweenOrNotBetweenMethod(
706                     introspectedColumn, true));
707             answer.addMethod(getSetBetweenOrNotBetweenMethod(
708                     introspectedColumn, false));
709         }
710 
711         return answer;
712     }
713 
714     /**
715      * This method adds all the extra methods and fields required to support a
716      * user defined type handler on some column.
717      * 
718      * @param introspectedColumn
719      * @param constructor
720      * @param innerClass
721      * @return a list of the names of all Lists added to the class by this
722      *         method
723      */
724     private List<String> addtypeHandledObjectsAndMethods(
725             IntrospectedColumn introspectedColumn, Method constructor,
726             InnerClass innerClass) {
727         List<String> answer = new ArrayList<String>();
728         StringBuilder sb = new StringBuilder();
729 
730         // add new private fields and public accessors in the class
731         FullyQualifiedJavaType listOfMaps;
732         if (generateForJava5) {
733             listOfMaps = new FullyQualifiedJavaType(
734                     "java.util.List<java.util.Map<java.lang.String, java.lang.Object>>"); //$NON-NLS-1$
735         } else {
736             listOfMaps = new FullyQualifiedJavaType("java.util.List"); //$NON-NLS-1$
737         }
738 
739         sb.setLength(0);
740         sb.append(introspectedColumn.getJavaProperty());
741         sb.append("CriteriaWithSingleValue"); //$NON-NLS-1$
742         answer.add(sb.toString());
743 
744         Field field = new Field();
745         field.setVisibility(JavaVisibility.PROTECTED);
746         field.setType(listOfMaps);
747         field.setName(sb.toString());
748         innerClass.addField(field);
749 
750         Method method = new Method();
751         method.setVisibility(JavaVisibility.PUBLIC);
752         method.setReturnType(field.getType());
753         method.setName(getGetterMethodName(field.getName(), field
754                 .getType()));
755         sb.insert(0, "return "); //$NON-NLS-1$
756         sb.append(';');
757         method.addBodyLine(sb.toString());
758         innerClass.addMethod(method);
759 
760         sb.setLength(0);
761         sb.append(introspectedColumn.getJavaProperty());
762         sb.append("CriteriaWithListValue"); //$NON-NLS-1$
763         answer.add(sb.toString());
764 
765         field = new Field();
766         field.setVisibility(JavaVisibility.PROTECTED);
767         field.setType(listOfMaps);
768         field.setName(sb.toString());
769         innerClass.addField(field);
770 
771         method = new Method();
772         method.setVisibility(JavaVisibility.PUBLIC);
773         method.setReturnType(field.getType());
774         method.setName(getGetterMethodName(field.getName(), field
775                 .getType()));
776         sb.insert(0, "return "); //$NON-NLS-1$
777         sb.append(';');
778         method.addBodyLine(sb.toString());
779         innerClass.addMethod(method);
780 
781         sb.setLength(0);
782         sb.append(introspectedColumn.getJavaProperty());
783         sb.append("CriteriaWithBetweenValue"); //$NON-NLS-1$
784         answer.add(sb.toString());
785 
786         field = new Field();
787         field.setVisibility(JavaVisibility.PROTECTED);
788         field.setType(listOfMaps);
789         field.setName(sb.toString());
790         innerClass.addField(field);
791 
792         method = new Method();
793         method.setVisibility(JavaVisibility.PUBLIC);
794         method.setReturnType(field.getType());
795         method.setName(getGetterMethodName(field.getName(), field
796                 .getType()));
797         sb.insert(0, "return "); //$NON-NLS-1$
798         sb.append(';');
799         method.addBodyLine(sb.toString());
800         innerClass.addMethod(method);
801 
802         // add constructor initialization
803         sb.setLength(0);
804         sb.append(introspectedColumn.getJavaProperty());
805         if (generateForJava5) {
806             sb
807                     .append("CriteriaWithSingleValue = new ArrayList<Map<String, Object>>();"); //$NON-NLS-1$;
808         } else {
809             sb.append("CriteriaWithSingleValue = new ArrayList();"); //$NON-NLS-1$;
810         }
811         constructor.addBodyLine(sb.toString());
812 
813         sb.setLength(0);
814         sb.append(introspectedColumn.getJavaProperty());
815         if (generateForJava5) {
816             sb
817                     .append("CriteriaWithListValue = new ArrayList<Map<String, Object>>();"); //$NON-NLS-1$
818         } else {
819             sb.append("CriteriaWithListValue = new ArrayList();"); //$NON-NLS-1$
820         }
821         constructor.addBodyLine(sb.toString());
822 
823         sb.setLength(0);
824         sb.append(introspectedColumn.getJavaProperty());
825         if (generateForJava5) {
826             sb
827                     .append("CriteriaWithBetweenValue = new ArrayList<Map<String, Object>>();"); //$NON-NLS-1$
828         } else {
829             sb.append("CriteriaWithBetweenValue = new ArrayList();"); //$NON-NLS-1$
830         }
831         constructor.addBodyLine(sb.toString());
832 
833         // now add the methods for simplifying the individual field set methods
834         method = new Method();
835         method.setVisibility(JavaVisibility.PROTECTED);
836         sb.setLength(0);
837         sb.append("add"); //$NON-NLS-1$
838         sb.append(introspectedColumn.getJavaProperty());
839         sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
840         sb.append("Criterion"); //$NON-NLS-1$
841 
842         method.setName(sb.toString());
843         method.addParameter(new Parameter(FullyQualifiedJavaType
844                 .getStringInstance(), "condition")); //$NON-NLS-1$
845         if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
846             method.addParameter(new Parameter(introspectedColumn
847                     .getFullyQualifiedJavaType().getPrimitiveTypeWrapper(),
848                     "value")); //$NON-NLS-1$
849         } else {
850             method.addParameter(new Parameter(introspectedColumn
851                 .getFullyQualifiedJavaType(), "value")); //$NON-NLS-1$
852         }
853         method.addParameter(new Parameter(FullyQualifiedJavaType
854                 .getStringInstance(), "property")); //$NON-NLS-1$
855         if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
856             method.addBodyLine("if (value == null) {"); //$NON-NLS-1$
857             method
858                 .addBodyLine("throw new RuntimeException(\"Value for \" + property + \" cannot be null\");"); //$NON-NLS-1$
859             method.addBodyLine("}"); //$NON-NLS-1$
860         }
861         if (generateForJava5) {
862             method
863                     .addBodyLine("Map<String, Object> map = new HashMap<String, Object>();"); //$NON-NLS-1$
864         } else {
865             method.addBodyLine("Map map = new HashMap();"); //$NON-NLS-1$
866         }
867         method.addBodyLine("map.put(\"condition\", condition);"); //$NON-NLS-1$
868         method.addBodyLine("map.put(\"value\", value);"); //$NON-NLS-1$
869 
870         sb.setLength(0);
871         sb.append(introspectedColumn.getJavaProperty());
872         sb.append("CriteriaWithSingleValue.add(map);"); //$NON-NLS-1$
873         method.addBodyLine(sb.toString());
874         innerClass.addMethod(method);
875 
876         FullyQualifiedJavaType listOfObjects = FullyQualifiedJavaType
877                 .getNewListInstance();
878         if (generateForJava5) {
879             if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
880                 listOfObjects.addTypeArgument(introspectedColumn
881                         .getFullyQualifiedJavaType().getPrimitiveTypeWrapper());
882             } else {
883                 listOfObjects.addTypeArgument(introspectedColumn
884                     .getFullyQualifiedJavaType());
885             }
886         }
887 
888         sb.setLength(0);
889         sb.append("add"); //$NON-NLS-1$
890         sb.append(introspectedColumn.getJavaProperty());
891         sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
892         sb.append("Criterion"); //$NON-NLS-1$
893 
894         method = new Method();
895         method.setVisibility(JavaVisibility.PROTECTED);
896         method.setName(sb.toString());
897         method.addParameter(new Parameter(FullyQualifiedJavaType
898                 .getStringInstance(), "condition")); //$NON-NLS-1$
899         method.addParameter(new Parameter(listOfObjects, "values")); //$NON-NLS-1$
900         method.addParameter(new Parameter(FullyQualifiedJavaType
901                 .getStringInstance(), "property")); //$NON-NLS-1$
902         method.addBodyLine("if (values == null || values.size() == 0) {"); //$NON-NLS-1$
903         method
904             .addBodyLine("throw new RuntimeException(\"Value list for \" + property + \" cannot be null or empty\");"); //$NON-NLS-1$
905         method.addBodyLine("}"); //$NON-NLS-1$
906         if (generateForJava5) {
907             method
908                     .addBodyLine("Map<String, Object> map = new HashMap<String, Object>();"); //$NON-NLS-1$
909         } else {
910             method.addBodyLine("Map map = new HashMap();"); //$NON-NLS-1$
911         }
912         method.addBodyLine("map.put(\"condition\", condition);"); //$NON-NLS-1$
913         method.addBodyLine("map.put(\"values\", values);"); //$NON-NLS-1$
914 
915         sb.setLength(0);
916         sb.append(introspectedColumn.getJavaProperty());
917         sb.append("CriteriaWithListValue.add(map);"); //$NON-NLS-1$
918         method.addBodyLine(sb.toString());
919         innerClass.addMethod(method);
920 
921         sb.setLength(0);
922         sb.append("add"); //$NON-NLS-1$
923         sb.append(introspectedColumn.getJavaProperty());
924         sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
925         sb.append("Criterion"); //$NON-NLS-1$
926 
927         method = new Method();
928         method.setVisibility(JavaVisibility.PROTECTED);
929         method.setName(sb.toString());
930         method.addParameter(new Parameter(FullyQualifiedJavaType
931                 .getStringInstance(), "condition")); //$NON-NLS-1$
932         if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
933             method.addParameter(new Parameter(introspectedColumn
934                     .getFullyQualifiedJavaType().getPrimitiveTypeWrapper(),
935                     "value1")); //$NON-NLS-1$
936                 method.addParameter(new Parameter(introspectedColumn
937                     .getFullyQualifiedJavaType().getPrimitiveTypeWrapper(),
938                     "value2")); //$NON-NLS-1$
939         } else {
940             method.addParameter(new Parameter(introspectedColumn
941                 .getFullyQualifiedJavaType(), "value1")); //$NON-NLS-1$
942             method.addParameter(new Parameter(introspectedColumn
943                 .getFullyQualifiedJavaType(), "value2")); //$NON-NLS-1$
944         }
945         method.addParameter(new Parameter(FullyQualifiedJavaType
946                 .getStringInstance(), "property")); //$NON-NLS-1$
947         method.addBodyLine("if (value1 == null || value2 == null) {"); //$NON-NLS-1$
948         method
949                 .addBodyLine("throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");"); //$NON-NLS-1$
950         method.addBodyLine("}"); //$NON-NLS-1$
951         if (generateForJava5) {
952             String shortName;
953             if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
954                 shortName = introspectedColumn.getFullyQualifiedJavaType()
955                         .getPrimitiveTypeWrapper().getShortName();
956             } else {
957                 shortName = introspectedColumn.getFullyQualifiedJavaType()
958                         .getShortName();
959             }
960             sb.setLength(0);
961             sb.append("List<"); //$NON-NLS-1$
962             sb.append(shortName);
963             sb.append("> list = new ArrayList<"); //$NON-NLS-1$
964             sb.append(shortName);
965             sb.append(">();"); //$NON-NLS-1$
966             method.addBodyLine(sb.toString());
967         } else {
968             method.addBodyLine("List list = new ArrayList();"); //$NON-NLS-1$
969         }
970         method.addBodyLine("list.add(value1);"); //$NON-NLS-1$
971         method.addBodyLine("list.add(value2);"); //$NON-NLS-1$
972         if (generateForJava5) {
973             method
974                     .addBodyLine("Map<String, Object> map = new HashMap<String, Object>();"); //$NON-NLS-1$
975         } else {
976             method.addBodyLine("Map map = new HashMap();"); //$NON-NLS-1$
977         }
978         method.addBodyLine("map.put(\"condition\", condition);"); //$NON-NLS-1$
979         method.addBodyLine("map.put(\"values\", list);"); //$NON-NLS-1$
980 
981         sb.setLength(0);
982         sb.append(introspectedColumn.getJavaProperty());
983         sb.append("CriteriaWithBetweenValue.add(map);"); //$NON-NLS-1$
984         method.addBodyLine(sb.toString());
985         innerClass.addMethod(method);
986 
987         return answer;
988     }
989 
990     private Method getSetNullMethod(IntrospectedColumn introspectedColumn) {
991         return getNoValueMethod(introspectedColumn, "IsNull", "is null"); //$NON-NLS-1$ //$NON-NLS-2$
992     }
993 
994     private Method getSetNotNullMethod(IntrospectedColumn introspectedColumn) {
995         return getNoValueMethod(introspectedColumn, "IsNotNull", "is not null"); //$NON-NLS-1$ //$NON-NLS-2$
996     }
997 
998     private Method getSetEqualMethod(IntrospectedColumn introspectedColumn) {
999         return getSingleValueMethod(introspectedColumn, "EqualTo", "="); //$NON-NLS-1$ //$NON-NLS-2$
1000     }
1001 
1002     private Method getSetNotEqualMethod(IntrospectedColumn introspectedColumn) {
1003         return getSingleValueMethod(introspectedColumn, "NotEqualTo", "<>"); //$NON-NLS-1$ //$NON-NLS-2$
1004     }
1005 
1006     private Method getSetGreaterThanMethod(IntrospectedColumn introspectedColumn) {
1007         return getSingleValueMethod(introspectedColumn, "GreaterThan", ">"); //$NON-NLS-1$ //$NON-NLS-2$
1008     }
1009 
1010     private Method getSetGreaterThenOrEqualMethod(
1011             IntrospectedColumn introspectedColumn) {
1012         return getSingleValueMethod(introspectedColumn,
1013                 "GreaterThanOrEqualTo", ">="); //$NON-NLS-1$ //$NON-NLS-2$
1014     }
1015 
1016     private Method getSetLessThanMethod(IntrospectedColumn introspectedColumn) {
1017         return getSingleValueMethod(introspectedColumn, "LessThan", "<"); //$NON-NLS-1$ //$NON-NLS-2$
1018     }
1019 
1020     private Method getSetLessThanOrEqualMethod(
1021             IntrospectedColumn introspectedColumn) {
1022         return getSingleValueMethod(introspectedColumn,
1023                 "LessThanOrEqualTo", "<="); //$NON-NLS-1$ //$NON-NLS-2$
1024     }
1025 
1026     private Method getSetLikeMethod(IntrospectedColumn introspectedColumn) {
1027         return getSingleValueMethod(introspectedColumn, "Like", "like"); //$NON-NLS-1$ //$NON-NLS-2$
1028     }
1029 
1030     private Method getSetNotLikeMethod(IntrospectedColumn introspectedColumn) {
1031         return getSingleValueMethod(introspectedColumn, "NotLike", "not like"); //$NON-NLS-1$ //$NON-NLS-2$
1032     }
1033 
1034     private Method getSingleValueMethod(IntrospectedColumn introspectedColumn,
1035             String nameFragment, String operator) {
1036         Method method = new Method();
1037         method.setVisibility(JavaVisibility.PUBLIC);
1038         method.addParameter(new Parameter(introspectedColumn
1039                 .getFullyQualifiedJavaType(), "value")); //$NON-NLS-1$
1040         StringBuilder sb = new StringBuilder();
1041         sb.append(introspectedColumn.getJavaProperty());
1042         sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
1043         sb.insert(0, "and"); //$NON-NLS-1$
1044         sb.append(nameFragment);
1045         method.setName(sb.toString());
1046         method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
1047         sb.setLength(0);
1048 
1049         if (introspectedColumn.isJDBCDateColumn()) {
1050             sb.append("addCriterionForJDBCDate(\""); //$NON-NLS-1$
1051         } else if (introspectedColumn.isJDBCTimeColumn()) {
1052             sb.append("addCriterionForJDBCTime(\""); //$NON-NLS-1$
1053         } else if (stringHasValue(introspectedColumn
1054                 .getTypeHandler())) {
1055             sb.append("add"); //$NON-NLS-1$
1056             sb.append(introspectedColumn.getJavaProperty());
1057             sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
1058             sb.append("Criterion(\""); //$NON-NLS-1$
1059         } else {
1060             sb.append("addCriterion(\""); //$NON-NLS-1$
1061         }
1062 
1063         sb.append(Ibatis2FormattingUtilities
1064                 .getAliasedActualColumnName(introspectedColumn));
1065         sb.append(' ');
1066         sb.append(operator);
1067         sb.append("\", "); //$NON-NLS-1$
1068 
1069         if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive() && !introspectedTable.isJava5Targeted()) {
1070             sb.append("new "); //$NON-NLS-1$
1071             sb.append(introspectedColumn.getFullyQualifiedJavaType()
1072                     .getPrimitiveTypeWrapper().getShortName());
1073             sb.append("(value)"); //$NON-NLS-1$
1074         } else {
1075             sb.append("value"); //$NON-NLS-1$
1076         }
1077 
1078         sb.append(", \""); //$NON-NLS-1$
1079         sb.append(introspectedColumn.getJavaProperty());
1080         sb.append("\");"); //$NON-NLS-1$
1081         method.addBodyLine(sb.toString());
1082         method.addBodyLine("return (Criteria) this;"); //$NON-NLS-1$
1083 
1084         return method;
1085     }
1086 
1087     /**
1088      * Generates methods that set between and not between conditions
1089      * 
1090      * @param introspectedColumn
1091      * @param betweenMethod
1092      * @return a generated method for the between or not between method
1093      */
1094     private Method getSetBetweenOrNotBetweenMethod(
1095             IntrospectedColumn introspectedColumn, boolean betweenMethod) {
1096         Method method = new Method();
1097         method.setVisibility(JavaVisibility.PUBLIC);
1098         FullyQualifiedJavaType type = introspectedColumn
1099                 .getFullyQualifiedJavaType();
1100 
1101         method.addParameter(new Parameter(type, "value1")); //$NON-NLS-1$
1102         method.addParameter(new Parameter(type, "value2")); //$NON-NLS-1$
1103         StringBuilder sb = new StringBuilder();
1104         sb.append(introspectedColumn.getJavaProperty());
1105         sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
1106         sb.insert(0, "and"); //$NON-NLS-1$
1107         if (betweenMethod) {
1108             sb.append("Between"); //$NON-NLS-1$
1109         } else {
1110             sb.append("NotBetween"); //$NON-NLS-1$
1111         }
1112         method.setName(sb.toString());
1113         method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
1114         sb.setLength(0);
1115 
1116         if (introspectedColumn.isJDBCDateColumn()) {
1117             sb.append("addCriterionForJDBCDate(\""); //$NON-NLS-1$
1118         } else if (introspectedColumn.isJDBCTimeColumn()) {
1119             sb.append("addCriterionForJDBCTime(\""); //$NON-NLS-1$
1120         } else if (stringHasValue(introspectedColumn
1121                 .getTypeHandler())) {
1122             sb.append("add"); //$NON-NLS-1$
1123             sb.append(introspectedColumn.getJavaProperty());
1124             sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
1125             sb.append("Criterion(\""); //$NON-NLS-1$
1126         } else {
1127             sb.append("addCriterion(\""); //$NON-NLS-1$
1128         }
1129 
1130         sb.append(Ibatis2FormattingUtilities
1131                 .getAliasedActualColumnName(introspectedColumn));
1132         if (betweenMethod) {
1133             sb.append(" between"); //$NON-NLS-1$
1134         } else {
1135             sb.append(" not between"); //$NON-NLS-1$
1136         }
1137         sb.append("\", "); //$NON-NLS-1$
1138         if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive() && !introspectedTable.isJava5Targeted()) {
1139             sb.append("new "); //$NON-NLS-1$
1140             sb.append(introspectedColumn.getFullyQualifiedJavaType()
1141                     .getPrimitiveTypeWrapper().getShortName());
1142             sb.append("(value1), "); //$NON-NLS-1$
1143             sb.append("new "); //$NON-NLS-1$
1144             sb.append(introspectedColumn.getFullyQualifiedJavaType()
1145                     .getPrimitiveTypeWrapper().getShortName());
1146             sb.append("(value2)"); //$NON-NLS-1$
1147         } else {
1148             sb.append("value1, value2"); //$NON-NLS-1$
1149         }
1150 
1151         sb.append(", \""); //$NON-NLS-1$
1152         sb.append(introspectedColumn.getJavaProperty());
1153         sb.append("\");"); //$NON-NLS-1$
1154         method.addBodyLine(sb.toString());
1155         method.addBodyLine("return (Criteria) this;"); //$NON-NLS-1$
1156 
1157         return method;
1158     }
1159 
1160     /**
1161      * 
1162      * @param introspectedColumn
1163      * @param inMethod
1164      *            if true generates an "in" method, else generates a "not in"
1165      *            method
1166      * @return a generated method for the in or not in method
1167      */
1168     private Method getSetInOrNotInMethod(IntrospectedColumn introspectedColumn,
1169             boolean inMethod) {
1170         Method method = new Method();
1171         method.setVisibility(JavaVisibility.PUBLIC);
1172         FullyQualifiedJavaType type = FullyQualifiedJavaType
1173                 .getNewListInstance();
1174         if (generateForJava5) {
1175             if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
1176                 type.addTypeArgument(introspectedColumn
1177                         .getFullyQualifiedJavaType().getPrimitiveTypeWrapper());
1178             } else {
1179                 type.addTypeArgument(introspectedColumn
1180                         .getFullyQualifiedJavaType());
1181             }
1182         }
1183 
1184         method.addParameter(new Parameter(type, "values")); //$NON-NLS-1$
1185         StringBuilder sb = new StringBuilder();
1186         sb.append(introspectedColumn.getJavaProperty());
1187         sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
1188         sb.insert(0, "and"); //$NON-NLS-1$
1189         if (inMethod) {
1190             sb.append("In"); //$NON-NLS-1$
1191         } else {
1192             sb.append("NotIn"); //$NON-NLS-1$
1193         }
1194         method.setName(sb.toString());
1195         method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
1196         sb.setLength(0);
1197 
1198         if (introspectedColumn.isJDBCDateColumn()) {
1199             sb.append("addCriterionForJDBCDate(\""); //$NON-NLS-1$
1200         } else if (introspectedColumn.isJDBCTimeColumn()) {
1201             sb.append("addCriterionForJDBCTime(\""); //$NON-NLS-1$
1202         } else if (stringHasValue(introspectedColumn
1203                 .getTypeHandler())) {
1204             sb.append("add"); //$NON-NLS-1$
1205             sb.append(introspectedColumn.getJavaProperty());
1206             sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
1207             sb.append("Criterion(\""); //$NON-NLS-1$
1208         } else {
1209             sb.append("addCriterion(\""); //$NON-NLS-1$
1210         }
1211 
1212         sb.append(Ibatis2FormattingUtilities
1213                 .getAliasedActualColumnName(introspectedColumn));
1214         if (inMethod) {
1215             sb.append(" in"); //$NON-NLS-1$
1216         } else {
1217             sb.append(" not in"); //$NON-NLS-1$
1218         }
1219         sb.append("\", values, \""); //$NON-NLS-1$
1220         sb.append(introspectedColumn.getJavaProperty());
1221         sb.append("\");"); //$NON-NLS-1$
1222         method.addBodyLine(sb.toString());
1223         method.addBodyLine("return (Criteria) this;"); //$NON-NLS-1$
1224 
1225         return method;
1226     }
1227 
1228     private Method getNoValueMethod(IntrospectedColumn introspectedColumn,
1229             String nameFragment, String operator) {
1230         Method method = new Method();
1231         method.setVisibility(JavaVisibility.PUBLIC);
1232         StringBuilder sb = new StringBuilder();
1233         sb.append(introspectedColumn.getJavaProperty());
1234         sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
1235         sb.insert(0, "and"); //$NON-NLS-1$
1236         sb.append(nameFragment);
1237         method.setName(sb.toString());
1238         method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
1239         sb.setLength(0);
1240         sb.append("addCriterion(\""); //$NON-NLS-1$
1241         sb.append(Ibatis2FormattingUtilities
1242                 .getAliasedActualColumnName(introspectedColumn));
1243         sb.append(' ');
1244         sb.append(operator);
1245         sb.append("\");"); //$NON-NLS-1$
1246         method.addBodyLine(sb.toString());
1247         method.addBodyLine("return (Criteria) this;"); //$NON-NLS-1$
1248 
1249         return method;
1250     }
1251 }