/**
   * @param introspectedColumn
   * @param inMethod if true generates an "in" method, else generates a "not in" method
   * @return a generated method for the in or not in method
   */
  private Method getSetInOrNotInMethod(IntrospectedColumn introspectedColumn, boolean inMethod) {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    FullyQualifiedJavaType type = FullyQualifiedJavaType.getNewListInstance();
    if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
      type.addTypeArgument(
          introspectedColumn.getFullyQualifiedJavaType().getPrimitiveTypeWrapper());
    } else {
      type.addTypeArgument(introspectedColumn.getFullyQualifiedJavaType());
    }

    method.addParameter(new Parameter(type, "values")); // $NON-NLS-1$
    StringBuilder sb = new StringBuilder();
    sb.append(introspectedColumn.getJavaProperty());
    sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
    sb.insert(0, "and"); // $NON-NLS-1$
    if (inMethod) {
      sb.append("In"); // $NON-NLS-1$
    } else {
      sb.append("NotIn"); // $NON-NLS-1$
    }
    method.setName(sb.toString());
    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
    sb.setLength(0);

    if (introspectedColumn.isJDBCDateColumn()) {
      sb.append("addCriterionForJDBCDate(\""); // $NON-NLS-1$
    } else if (introspectedColumn.isJDBCTimeColumn()) {
      sb.append("addCriterionForJDBCTime(\""); // $NON-NLS-1$
    } else if (stringHasValue(introspectedColumn.getTypeHandler())) {
      sb.append("add"); // $NON-NLS-1$
      sb.append(introspectedColumn.getJavaProperty());
      sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
      sb.append("Criterion(\""); // $NON-NLS-1$
    } else {
      sb.append("addCriterion(\""); // $NON-NLS-1$
    }

    sb.append(MyBatis3FormattingUtilities.getAliasedActualColumnName(introspectedColumn));
    if (inMethod) {
      sb.append(" in"); // $NON-NLS-1$
    } else {
      sb.append(" not in"); // $NON-NLS-1$
    }
    sb.append("\", values, \""); // $NON-NLS-1$
    sb.append(introspectedColumn.getJavaProperty());
    sb.append("\");"); // $NON-NLS-1$
    method.addBodyLine(sb.toString());
    method.addBodyLine("return (Criteria) this;"); // $NON-NLS-1$

    return method;
  }
  private Method getSingleValueMethod(
      IntrospectedColumn introspectedColumn, String nameFragment, String operator) {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.addParameter(
        new Parameter(introspectedColumn.getFullyQualifiedJavaType(), "value")); // $NON-NLS-1$
    StringBuilder sb = new StringBuilder();
    sb.append(introspectedColumn.getJavaProperty());
    sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
    sb.insert(0, "and"); // $NON-NLS-1$
    sb.append(nameFragment);
    method.setName(sb.toString());
    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
    sb.setLength(0);

    if (introspectedColumn.isJDBCDateColumn()) {
      sb.append("addCriterionForJDBCDate(\""); // $NON-NLS-1$
    } else if (introspectedColumn.isJDBCTimeColumn()) {
      sb.append("addCriterionForJDBCTime(\""); // $NON-NLS-1$
    } else if (stringHasValue(introspectedColumn.getTypeHandler())) {
      sb.append("add"); // $NON-NLS-1$
      sb.append(introspectedColumn.getJavaProperty());
      sb.setCharAt(3, Character.toUpperCase(sb.charAt(3)));
      sb.append("Criterion(\""); // $NON-NLS-1$
    } else {
      sb.append("addCriterion(\""); // $NON-NLS-1$
    }

    sb.append(MyBatis3FormattingUtilities.getAliasedActualColumnName(introspectedColumn));
    sb.append(' ');
    sb.append(operator);
    sb.append("\", "); // $NON-NLS-1$

    if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
      sb.append("new "); // $NON-NLS-1$
      sb.append(
          introspectedColumn.getFullyQualifiedJavaType().getPrimitiveTypeWrapper().getShortName());
      sb.append("(value)"); // $NON-NLS-1$
    } else {
      sb.append("value"); // $NON-NLS-1$
    }

    sb.append(", \""); // $NON-NLS-1$
    sb.append(introspectedColumn.getJavaProperty());
    sb.append("\");"); // $NON-NLS-1$
    method.addBodyLine(sb.toString());
    method.addBodyLine("return (Criteria) this;"); // $NON-NLS-1$

    return method;
  }
  private InnerClass getCriteriaInnerClass(TopLevelClass topLevelClass) {
    Method method;

    InnerClass answer = new InnerClass(FullyQualifiedJavaType.getCriteriaInstance());

    answer.setVisibility(JavaVisibility.PUBLIC);
    answer.setStatic(true);
    answer.setSuperClass(FullyQualifiedJavaType.getGeneratedCriteriaInstance());

    context.getCommentGenerator().addClassComment(answer, introspectedTable, true);

    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    method.setName("Criteria"); // $NON-NLS-1$
    method.setConstructor(true);
    method.addBodyLine("super();"); // $NON-NLS-1$
    answer.addMethod(method);

    return answer;
  }
  private Method getNoValueMethod(
      IntrospectedColumn introspectedColumn, String nameFragment, String operator) {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    StringBuilder sb = new StringBuilder();
    sb.append(introspectedColumn.getJavaProperty());
    sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
    sb.insert(0, "and"); // $NON-NLS-1$
    sb.append(nameFragment);
    method.setName(sb.toString());
    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
    sb.setLength(0);
    sb.append("addCriterion(\""); // $NON-NLS-1$
    sb.append(MyBatis3FormattingUtilities.getAliasedActualColumnName(introspectedColumn));
    sb.append(' ');
    sb.append(operator);
    sb.append("\");"); // $NON-NLS-1$
    method.addBodyLine(sb.toString());
    method.addBodyLine("return (Criteria) this;"); // $NON-NLS-1$

    return method;
  }
  @Override
  public List<CompilationUnit> getCompilationUnits() {
    FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
    progressCallback.startTask(getString("Progress.6", table.toString())); // $NON-NLS-1$
    CommentGenerator commentGenerator = context.getCommentGenerator();

    FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getExampleType());
    TopLevelClass topLevelClass = new TopLevelClass(type);
    topLevelClass.setVisibility(JavaVisibility.PUBLIC);
    commentGenerator.addJavaFileComment(topLevelClass);

    // add default constructor
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setConstructor(true);
    method.setName(type.getShortName());
    method.addBodyLine("oredCriteria = new ArrayList<Criteria>();"); // $NON-NLS-1$

    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);

    // add field, getter, setter for orderby clause
    Field field = new Field();
    field.setVisibility(JavaVisibility.PROTECTED);
    field.setType(FullyQualifiedJavaType.getStringInstance());
    field.setName("orderByClause"); // $NON-NLS-1$
    commentGenerator.addFieldComment(field, introspectedTable);
    topLevelClass.addField(field);

    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setName("setOrderByClause"); // $NON-NLS-1$
    method.addParameter(
        new Parameter(FullyQualifiedJavaType.getStringInstance(), "orderByClause")); // $NON-NLS-1$
    method.addBodyLine("this.orderByClause = orderByClause;"); // $NON-NLS-1$
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);

    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getStringInstance());
    method.setName("getOrderByClause"); // $NON-NLS-1$
    method.addBodyLine("return orderByClause;"); // $NON-NLS-1$
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);

    // add field, getter, setter for distinct
    field = new Field();
    field.setVisibility(JavaVisibility.PROTECTED);
    field.setType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
    field.setName("distinct"); // $NON-NLS-1$
    commentGenerator.addFieldComment(field, introspectedTable);
    topLevelClass.addField(field);

    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setName("setDistinct"); // $NON-NLS-1$
    method.addParameter(
        new Parameter(
            FullyQualifiedJavaType.getBooleanPrimitiveInstance(), "distinct")); // $NON-NLS-1$
    method.addBodyLine("this.distinct = distinct;"); // $NON-NLS-1$
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);

    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
    method.setName("isDistinct"); // $NON-NLS-1$
    method.addBodyLine("return distinct;"); // $NON-NLS-1$
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);

    // add field and methods for the list of ored criteria
    field = new Field();
    field.setVisibility(JavaVisibility.PROTECTED);

    FullyQualifiedJavaType fqjt =
        new FullyQualifiedJavaType("java.util.List<Criteria>"); // $NON-NLS-1$
    field.setType(fqjt);
    field.setName("oredCriteria"); // $NON-NLS-1$
    commentGenerator.addFieldComment(field, introspectedTable);
    topLevelClass.addField(field);

    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(fqjt);
    method.setName("getOredCriteria"); // $NON-NLS-1$
    method.addBodyLine("return oredCriteria;"); // $NON-NLS-1$
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);

    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setName("or"); // $NON-NLS-1$
    method.addParameter(
        new Parameter(FullyQualifiedJavaType.getCriteriaInstance(), "criteria")); // $NON-NLS-1$
    method.addBodyLine("oredCriteria.add(criteria);"); // $NON-NLS-1$
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);

    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setName("or"); // $NON-NLS-1$
    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
    method.addBodyLine("Criteria criteria = createCriteriaInternal();"); // $NON-NLS-1$
    method.addBodyLine("oredCriteria.add(criteria);"); // $NON-NLS-1$
    method.addBodyLine("return criteria;"); // $NON-NLS-1$
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);

    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setName("createCriteria"); // $NON-NLS-1$
    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
    method.addBodyLine("Criteria criteria = createCriteriaInternal();"); // $NON-NLS-1$
    method.addBodyLine("if (oredCriteria.size() == 0) {"); // $NON-NLS-1$
    method.addBodyLine("oredCriteria.add(criteria);"); // $NON-NLS-1$
    method.addBodyLine("}"); // $NON-NLS-1$
    method.addBodyLine("return criteria;"); // $NON-NLS-1$
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);

    method = new Method();
    method.setVisibility(JavaVisibility.PROTECTED);
    method.setName("createCriteriaInternal"); // $NON-NLS-1$
    method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
    method.addBodyLine("Criteria criteria = new Criteria();"); // $NON-NLS-1$
    method.addBodyLine("return criteria;"); // $NON-NLS-1$
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);

    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setName("clear"); // $NON-NLS-1$
    method.addBodyLine("oredCriteria.clear();"); // $NON-NLS-1$
    method.addBodyLine("orderByClause = null;"); // $NON-NLS-1$
    method.addBodyLine("distinct = false;"); // $NON-NLS-1$
    commentGenerator.addGeneralMethodComment(method, introspectedTable);
    topLevelClass.addMethod(method);

    // now generate the inner class that holds the AND conditions
    topLevelClass.addInnerClass(getGeneratedCriteriaInnerClass(topLevelClass));

    topLevelClass.addInnerClass(getCriteriaInnerClass(topLevelClass));

    topLevelClass.addInnerClass(getCriterionInnerClass(topLevelClass));

    List<CompilationUnit> answer = new ArrayList<CompilationUnit>();
    if (context.getPlugins().modelExampleClassGenerated(topLevelClass, introspectedTable)) {
      answer.add(topLevelClass);
    }
    return answer;
  }