private InnerClass getGeneratedCriteriaInnerClass(
TopLevelClass topLevelClass) {
Field field;
Method method;
InnerClass answer = new InnerClass(FullyQualifiedJavaType
.getGeneratedCriteriaInstance());
answer.setVisibility(JavaVisibility.PROTECTED);
answer.setStatic(true);
answer.setAbstract(true);
context.getCommentGenerator().addClassComment(answer,
introspectedTable);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("GeneratedCriteria"); //$NON-NLS-1$
method.setConstructor(true);
method.addBodyLine("super();"); //$NON-NLS-1$
if (generateForJava5) {
method
.addBodyLine("criteriaWithoutValue = new ArrayList<String>();"); //$NON-NLS-1$
method
.addBodyLine("criteriaWithSingleValue = new ArrayList<Map<String, Object>>();"); //$NON-NLS-1$
method
.addBodyLine("criteriaWithListValue = new ArrayList<Map<String, Object>>();"); //$NON-NLS-1$
method
.addBodyLine("criteriaWithBetweenValue = new ArrayList<Map<String, Object>>();"); //$NON-NLS-1$
} else {
method.addBodyLine("criteriaWithoutValue = new ArrayList();"); //$NON-NLS-1$
method.addBodyLine("criteriaWithSingleValue = new ArrayList();"); //$NON-NLS-1$
method.addBodyLine("criteriaWithListValue = new ArrayList();"); //$NON-NLS-1$
method.addBodyLine("criteriaWithBetweenValue = new ArrayList();"); //$NON-NLS-1$
}
answer.addMethod(method);
List<String> criteriaLists = new ArrayList<String>();
criteriaLists.add("criteriaWithoutValue"); //$NON-NLS-1$
criteriaLists.add("criteriaWithSingleValue"); //$NON-NLS-1$
criteriaLists.add("criteriaWithListValue"); //$NON-NLS-1$
criteriaLists.add("criteriaWithBetweenValue"); //$NON-NLS-1$
for (IntrospectedColumn introspectedColumn : introspectedTable
.getNonBLOBColumns()) {
if (stringHasValue(introspectedColumn
.getTypeHandler())) {
criteriaLists.addAll(addtypeHandledObjectsAndMethods(
introspectedColumn, method, answer));
}
}
// now generate the isValid method
method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setName("isValid"); //$NON-NLS-1$
method.setReturnType(FullyQualifiedJavaType
.getBooleanPrimitiveInstance());
StringBuilder sb = new StringBuilder();
Iterator<String> strIter = criteriaLists.iterator();
sb.append("return "); //$NON-NLS-1$
sb.append(strIter.next());
sb.append(".size() > 0"); //$NON-NLS-1$
method.addBodyLine(sb.toString());
while (strIter.hasNext()) {
sb.setLength(0);
OutputUtilities.javaIndent(sb, 1);
sb.append("|| "); //$NON-NLS-1$
sb.append(strIter.next());
sb.append(".size() > 0"); //$NON-NLS-1$
if (!strIter.hasNext()) {
sb.append(';');
}
method.addBodyLine(sb.toString());
}
answer.addMethod(method);
// now we need to generate the methods that will be used in the SqlMap
// to generate the dynamic where clause
topLevelClass.addImportedType(FullyQualifiedJavaType
.getNewMapInstance());
topLevelClass.addImportedType(FullyQualifiedJavaType
.getNewListInstance());
topLevelClass.addImportedType(FullyQualifiedJavaType
.getNewHashMapInstance());
topLevelClass.addImportedType(FullyQualifiedJavaType
.getNewArrayListInstance());
field = new Field();
field.setVisibility(JavaVisibility.PROTECTED);
FullyQualifiedJavaType listOfStrings;
if (generateForJava5) {
listOfStrings = new FullyQualifiedJavaType(
"java.util.List<java.lang.String>"); //$NON-NLS-1$
} else {
listOfStrings = new FullyQualifiedJavaType("java.util.List"); //$NON-NLS-1$
}
field.setType(listOfStrings);
field.setName("criteriaWithoutValue"); //$NON-NLS-1$
answer.addField(field);
method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(field.getType());
method.setName(getGetterMethodName(field.getName(), field
.getType()));
method.addBodyLine("return criteriaWithoutValue;"); //$NON-NLS-1$
answer.addMethod(method);
FullyQualifiedJavaType listOfMaps;
if (generateForJava5) {
listOfMaps = new FullyQualifiedJavaType(
"java.util.List<java.util.Map<java.lang.String, java.lang.Object>>"); //$NON-NLS-1$
} else {
listOfMaps = new FullyQualifiedJavaType("java.util.List"); //$NON-NLS-1$
}
field = new Field();
field.setVisibility(JavaVisibility.PROTECTED);
field.setType(listOfMaps);
field.setName("criteriaWithSingleValue"); //$NON-NLS-1$
answer.addField(field);
method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(field.getType());
method.setName(getGetterMethodName(field.getName(), field
.getType()));
method.addBodyLine("return criteriaWithSingleValue;"); //$NON-NLS-1$
answer.addMethod(method);
field = new Field();
field.setVisibility(JavaVisibility.PROTECTED);
field.setType(listOfMaps);
field.setName("criteriaWithListValue"); //$NON-NLS-1$
answer.addField(field);
method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(field.getType());
method.setName(getGetterMethodName(field.getName(), field
.getType()));
method.addBodyLine("return criteriaWithListValue;"); //$NON-NLS-1$
answer.addMethod(method);
field = new Field();
field.setVisibility(JavaVisibility.PROTECTED);
field.setType(listOfMaps);
field.setName("criteriaWithBetweenValue"); //$NON-NLS-1$
answer.addField(field);
method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setReturnType(field.getType());
method.setName(getGetterMethodName(field.getName(), field
.getType()));
method.addBodyLine("return criteriaWithBetweenValue;"); //$NON-NLS-1$
answer.addMethod(method);
// now add the methods for simplifying the individual field set methods
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("addCriterion"); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addBodyLine("if (condition == null) {"); //$NON-NLS-1$
method
.addBodyLine("throw new RuntimeException(\"Value for condition cannot be null\");"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
method.addBodyLine("criteriaWithoutValue.add(condition);"); //$NON-NLS-1$
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("addCriterion"); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getObjectInstance(), "value")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "property")); //$NON-NLS-1$
method.addBodyLine("if (value == null) {"); //$NON-NLS-1$
method
.addBodyLine("throw new RuntimeException(\"Value for \" + property + \" cannot be null\");"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
if (generateForJava5) {
method
.addBodyLine("Map<String, Object> map = new HashMap<String, Object>();"); //$NON-NLS-1$
} else {
method.addBodyLine("Map map = new HashMap();"); //$NON-NLS-1$
}
method.addBodyLine("map.put(\"condition\", condition);"); //$NON-NLS-1$
method.addBodyLine("map.put(\"value\", value);"); //$NON-NLS-1$
method.addBodyLine("criteriaWithSingleValue.add(map);"); //$NON-NLS-1$
answer.addMethod(method);
FullyQualifiedJavaType listOfObjects;
if (generateForJava5) {
listOfObjects = new FullyQualifiedJavaType(
"java.util.List<? extends java.lang.Object>"); //$NON-NLS-1$
} else {
listOfObjects = new FullyQualifiedJavaType("java.util.List"); //$NON-NLS-1$
}
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("addCriterion"); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(listOfObjects, "values")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "property")); //$NON-NLS-1$
method.addBodyLine("if (values == null || values.size() == 0) {"); //$NON-NLS-1$
method
.addBodyLine("throw new RuntimeException(\"Value list for \" + property + \" cannot be null or empty\");"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
if (generateForJava5) {
method
.addBodyLine("Map<String, Object> map = new HashMap<String, Object>();"); //$NON-NLS-1$
} else {
method.addBodyLine("Map map = new HashMap();"); //$NON-NLS-1$
}
method.addBodyLine("map.put(\"condition\", condition);"); //$NON-NLS-1$
method.addBodyLine("map.put(\"values\", values);"); //$NON-NLS-1$
method.addBodyLine("criteriaWithListValue.add(map);"); //$NON-NLS-1$
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("addCriterion"); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getObjectInstance(), "value1")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getObjectInstance(), "value2")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "property")); //$NON-NLS-1$
method.addBodyLine("if (value1 == null || value2 == null) {"); //$NON-NLS-1$
method
.addBodyLine("throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
if (generateForJava5) {
method.addBodyLine("List<Object> list = new ArrayList<Object>();"); //$NON-NLS-1$
} else {
method.addBodyLine("List list = new ArrayList();"); //$NON-NLS-1$
}
method.addBodyLine("list.add(value1);"); //$NON-NLS-1$
method.addBodyLine("list.add(value2);"); //$NON-NLS-1$
if (generateForJava5) {
method
.addBodyLine("Map<String, Object> map = new HashMap<String, Object>();"); //$NON-NLS-1$
} else {
method.addBodyLine("Map map = new HashMap();"); //$NON-NLS-1$
}
method.addBodyLine("map.put(\"condition\", condition);"); //$NON-NLS-1$
method.addBodyLine("map.put(\"values\", list);"); //$NON-NLS-1$
method.addBodyLine("criteriaWithBetweenValue.add(map);"); //$NON-NLS-1$
answer.addMethod(method);
FullyQualifiedJavaType listOfDates;
if (generateForJava5) {
listOfDates = new FullyQualifiedJavaType(
"java.util.List<java.util.Date>"); //$NON-NLS-1$
} else {
listOfDates = new FullyQualifiedJavaType("java.util.List"); //$NON-NLS-1$
}
if (introspectedTable.hasJDBCDateColumns()) {
topLevelClass.addImportedType(FullyQualifiedJavaType
.getDateInstance());
topLevelClass.addImportedType(FullyQualifiedJavaType
.getNewIteratorInstance());
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("addCriterionForJDBCDate"); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getDateInstance(), "value")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "property")); //$NON-NLS-1$
method.addBodyLine("if (value == null) {"); //$NON-NLS-1$
method
.addBodyLine("throw new RuntimeException(\"Value for \" + property + \" cannot be null\");"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
method
.addBodyLine("addCriterion(condition, new java.sql.Date(value.getTime()), property);"); //$NON-NLS-1$
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("addCriterionForJDBCDate"); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(listOfDates, "values")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "property")); //$NON-NLS-1$
method.addBodyLine("if (values == null || values.size() == 0) {"); //$NON-NLS-1$
method
.addBodyLine("throw new RuntimeException(\"Value list for \" + property + \" cannot be null or empty\");"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
if (generateForJava5) {
method
.addBodyLine("List<java.sql.Date> dateList = new ArrayList<java.sql.Date>();"); //$NON-NLS-1$
method.addBodyLine("Iterator<Date> iter = values.iterator();"); //$NON-NLS-1$
method.addBodyLine("while (iter.hasNext()) {"); //$NON-NLS-1$
method
.addBodyLine("dateList.add(new java.sql.Date(iter.next().getTime()));"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
} else {
method.addBodyLine("List dateList = new ArrayList();"); //$NON-NLS-1$
method.addBodyLine("Iterator iter = values.iterator();"); //$NON-NLS-1$
method.addBodyLine("while (iter.hasNext()) {"); //$NON-NLS-1$
method
.addBodyLine("dateList.add(new java.sql.Date(((Date)iter.next()).getTime()));"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
}
method.addBodyLine("addCriterion(condition, dateList, property);"); //$NON-NLS-1$
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("addCriterionForJDBCDate"); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getDateInstance(), "value1")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getDateInstance(), "value2")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "property")); //$NON-NLS-1$
method.addBodyLine("if (value1 == null || value2 == null) {"); //$NON-NLS-1$
method
.addBodyLine("throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
method
.addBodyLine("addCriterion(condition, new java.sql.Date(value1.getTime()), new java.sql.Date(value2.getTime()), property);"); //$NON-NLS-1$
answer.addMethod(method);
}
if (introspectedTable.hasJDBCTimeColumns()) {
topLevelClass.addImportedType(FullyQualifiedJavaType
.getDateInstance());
topLevelClass.addImportedType(FullyQualifiedJavaType
.getNewIteratorInstance());
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("addCriterionForJDBCTime"); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getDateInstance(), "value")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "property")); //$NON-NLS-1$
method.addBodyLine("if (value == null) {"); //$NON-NLS-1$
method
.addBodyLine("throw new RuntimeException(\"Value for \" + property + \" cannot be null\");"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
method
.addBodyLine("addCriterion(condition, new java.sql.Time(value.getTime()), property);"); //$NON-NLS-1$
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("addCriterionForJDBCTime"); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(listOfDates, "values")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "property")); //$NON-NLS-1$
method.addBodyLine("if (values == null || values.size() == 0) {"); //$NON-NLS-1$
method
.addBodyLine("throw new RuntimeException(\"Value list for \" + property + \" cannot be null or empty\");"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
if (generateForJava5) {
method
.addBodyLine("List<java.sql.Time> timeList = new ArrayList<java.sql.Time>();"); //$NON-NLS-1$
method.addBodyLine("Iterator<Date> iter = values.iterator();"); //$NON-NLS-1$
method.addBodyLine("while (iter.hasNext()) {"); //$NON-NLS-1$
method
.addBodyLine("timeList.add(new java.sql.Time(iter.next().getTime()));"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
} else {
method.addBodyLine("List timeList = new ArrayList();"); //$NON-NLS-1$
method.addBodyLine("Iterator iter = values.iterator();"); //$NON-NLS-1$
method.addBodyLine("while (iter.hasNext()) {"); //$NON-NLS-1$
method
.addBodyLine("timeList.add(new java.sql.Time(((Date)iter.next()).getTime()));"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
}
method.addBodyLine("addCriterion(condition, timeList, property);"); //$NON-NLS-1$
answer.addMethod(method);
method = new Method();
method.setVisibility(JavaVisibility.PROTECTED);
method.setName("addCriterionForJDBCTime"); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "condition")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getDateInstance(), "value1")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getDateInstance(), "value2")); //$NON-NLS-1$
method.addParameter(new Parameter(FullyQualifiedJavaType
.getStringInstance(), "property")); //$NON-NLS-1$
method.addBodyLine("if (value1 == null || value2 == null) {"); //$NON-NLS-1$
method
.addBodyLine("throw new RuntimeException(\"Between values for \" + property + \" cannot be null\");"); //$NON-NLS-1$
method.addBodyLine("}"); //$NON-NLS-1$
method
.addBodyLine("addCriterion(condition, new java.sql.Time(value1.getTime()), new java.sql.Time(value2.getTime()), property);"); //$NON-NLS-1$
answer.addMethod(method);
}
for (IntrospectedColumn introspectedColumn : introspectedTable
.getNonBLOBColumns()) {
topLevelClass.addImportedType(introspectedColumn
.getFullyQualifiedJavaType());
// here we need to add the individual methods for setting the
// conditions for a field
answer.addMethod(getSetNullMethod(introspectedColumn));
answer.addMethod(getSetNotNullMethod(introspectedColumn));
answer.addMethod(getSetEqualMethod(introspectedColumn));
answer.addMethod(getSetNotEqualMethod(introspectedColumn));
answer.addMethod(getSetGreaterThanMethod(introspectedColumn));
answer
.addMethod(getSetGreaterThenOrEqualMethod(introspectedColumn));
answer.addMethod(getSetLessThanMethod(introspectedColumn));
answer.addMethod(getSetLessThanOrEqualMethod(introspectedColumn));
if (introspectedColumn.isJdbcCharacterColumn()) {
answer.addMethod(getSetLikeMethod(introspectedColumn));
answer.addMethod(getSetNotLikeMethod(introspectedColumn));
}
answer.addMethod(getSetInOrNotInMethod(introspectedColumn, true));
answer.addMethod(getSetInOrNotInMethod(introspectedColumn, false));
answer.addMethod(getSetBetweenOrNotBetweenMethod(
introspectedColumn, true));
answer.addMethod(getSetBetweenOrNotBetweenMethod(
introspectedColumn, false));
}
return answer;
}