public void addSetterComment(Method method, FullyQualifiedTable table, String columnName) {
    StringBuilder sb = new StringBuilder();

    method.addJavaDocLine("/**"); // $NON-NLS-1$
    method.addJavaDocLine(" * This method was generated by Apache iBATIS ibator."); // $NON-NLS-1$

    sb.append(" * This method sets the value of the database column "); // $NON-NLS-1$
    sb.append(table);
    sb.append('.');
    sb.append(columnName);
    method.addJavaDocLine(sb.toString());

    method.addJavaDocLine(" *"); // $NON-NLS-1$

    Parameter parm = method.getParameters().get(0);
    sb.setLength(0);
    sb.append(" * @param "); // $NON-NLS-1$
    sb.append(parm.getName());
    sb.append(" the value for "); // $NON-NLS-1$
    sb.append(table);
    sb.append('.');
    sb.append(columnName);
    method.addJavaDocLine(sb.toString());

    addIbatorJavadocTag(method);

    method.addJavaDocLine(" */"); // $NON-NLS-1$
  }
  @Override
  public void addImplementationElements(TopLevelClass topLevelClass) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = getMethodShell(importedTypes);

    // generate the implementation method
    StringBuilder sb = new StringBuilder();

    FullyQualifiedJavaType dao = introspectedTable.getDAOInterfaceType();

    FullyQualifiedJavaType returnType = method.getReturnType();

    sb.setLength(0);
    sb.append(returnType.getShortName());
    sb.append(" records = "); // $NON-NLS-1$
    sb.append(
        serviceTemplate.getQueryForObjectMethod(
            JavaBeansUtil.getPropertyName(dao.getShortName()),
            getServiceMethodNameCalculator().getSelectByConditionMethodName(introspectedTable),
            "condition")); //$NON-NLS-1$
    method.addBodyLine(sb.toString());
    method.addBodyLine("return records;"); // $NON-NLS-1$

    if (ibatorContext
        .getPlugins()
        .daoSelectByConditionMethodGenerated(method, topLevelClass, introspectedTable)) {
      topLevelClass.addImportedTypes(importedTypes);
      topLevelClass.addMethod(method);
    }
  }
Exemplo n.º 3
0
  @Override
  public void addImplementationElements(TopLevelClass topLevelClass) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = getMethodShell(importedTypes);

    FullyQualifiedJavaType returnType = method.getReturnType();
    FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();

    StringBuilder sb = new StringBuilder();

    if (returnType != null) {
      sb.append("Object newKey = "); // $NON-NLS-1$
    }

    sb.append(
        daoTemplate.getInsertMethod(
            table.getSqlMapNamespace(), XmlConstants.INSERT_STATEMENT_ID, "record")); // $NON-NLS-1$
    method.addBodyLine(sb.toString());

    if (returnType != null) {
      if ("Object".equals(returnType.getShortName())) { // $NON-NLS-1$
        // no need to cast if the return type is Object
        method.addBodyLine("return newKey;"); // $NON-NLS-1$
      } else {
        sb.setLength(0);

        if (returnType.isPrimitive()) {
          PrimitiveTypeWrapper ptw = returnType.getPrimitiveTypeWrapper();
          sb.append("return (("); // $NON-NLS-1$
          sb.append(ptw.getShortName());
          sb.append(") newKey"); // $NON-NLS-1$
          sb.append(")."); // $NON-NLS-1$
          sb.append(ptw.getToPrimitiveMethod());
          sb.append(';');
        } else {
          sb.append("return ("); // $NON-NLS-1$
          sb.append(returnType.getShortName());
          sb.append(") newKey;"); // $NON-NLS-1$
        }

        method.addBodyLine(sb.toString());
      }
    }

    if (ibatorContext
        .getPlugins()
        .daoInsertMethodGenerated(method, topLevelClass, introspectedTable)) {
      topLevelClass.addImportedTypes(importedTypes);
      topLevelClass.addMethod(method);
    }
  }
  public void addGeneralMethodComment(Method method, FullyQualifiedTable table) {
    StringBuilder sb = new StringBuilder();

    method.addJavaDocLine("/**"); // $NON-NLS-1$
    method.addJavaDocLine(" * This method was generated by Apache iBATIS ibator."); // $NON-NLS-1$

    sb.append(" * This method corresponds to the database table "); // $NON-NLS-1$
    sb.append(table);
    method.addJavaDocLine(sb.toString());

    addIbatorJavadocTag(method);

    method.addJavaDocLine(" */"); // $NON-NLS-1$
  }
  public void addGetterComment(Method method, FullyQualifiedTable table, String columnName) {
    StringBuilder sb = new StringBuilder();

    method.addJavaDocLine("/**"); // $NON-NLS-1$
    method.addJavaDocLine(" * This method was generated by Apache iBATIS ibator."); // $NON-NLS-1$

    sb.append(" * This method returns the value of the database column "); // $NON-NLS-1$
    sb.append(table);
    sb.append('.');
    sb.append(columnName);
    method.addJavaDocLine(sb.toString());

    method.addJavaDocLine(" *"); // $NON-NLS-1$

    sb.setLength(0);
    sb.append(" * @return the value of "); // $NON-NLS-1$
    sb.append(table);
    sb.append('.');
    sb.append(columnName);
    method.addJavaDocLine(sb.toString());

    addIbatorJavadocTag(method);

    method.addJavaDocLine(" */"); // $NON-NLS-1$
  }
  private Method getMethodShell(Set<FullyQualifiedJavaType> importedTypes) {
    FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
    FullyQualifiedJavaType type = introspectedTable.getConditionType();

    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);

    FullyQualifiedJavaType returnType = FullyQualifiedJavaType.getNewListInstance();
    returnType.addTypeArgument(introspectedTable.getConditionType());
    method.setReturnType(returnType);
    importedTypes.add(returnType);
    importedTypes.add(introspectedTable.getConditionType());

    method.setName(
        getServiceMethodNameCalculator().getSelectByConditionMethodName(introspectedTable));

    method.addParameter(new Parameter(type, "condition"));

    for (FullyQualifiedJavaType fqjt : serviceTemplate.getCheckedExceptions()) {
      method.addException(fqjt);
      importedTypes.add(fqjt);
    }

    ibatorContext.getCommentGenerator().addGeneralMethodComment(method, table);

    return method;
  }
  @Override
  public boolean modelExampleClassGenerated(
      TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {

    InnerClass criteria = null;
    // first, find the Criteria inner class
    for (InnerClass innerClass : topLevelClass.getInnerClasses()) {
      if ("Criteria".equals(innerClass.getType().getShortName())) { // $NON-NLS-1$
        criteria = innerClass;
        break;
      }
    }

    if (criteria == null) {
      // can't find the inner class for some reason, bail out.
      return true;
    }

    for (IntrospectedColumn introspectedColumn : introspectedTable.getNonBLOBColumns()) {
      if (!introspectedColumn.isJdbcCharacterColumn() || !introspectedColumn.isStringColumn()) {
        continue;
      }

      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("LikeInsensitive"); // $NON-NLS-1$
      method.setName(sb.toString());
      method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());

      sb.setLength(0);
      sb.append("addCriterion(\"upper("); // $NON-NLS-1$
      sb.append(Ibatis2FormattingUtilities.getAliasedActualColumnName(introspectedColumn));
      sb.append(") like\", value.toUpperCase(), \""); // $NON-NLS-1$
      sb.append(introspectedColumn.getJavaProperty());
      sb.append("\");"); // $NON-NLS-1$
      method.addBodyLine(sb.toString());
      method.addBodyLine("return this;"); // $NON-NLS-1$

      criteria.addMethod(method);
    }

    return true;
  }
Exemplo n.º 8
0
  private Method getMethodShell(Set<FullyQualifiedJavaType> importedTypes) {
    FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
    Method method = new Method();

    FullyQualifiedJavaType returnType;
    if (introspectedTable.getGeneratedKey() != null) {
      IntrospectedColumn introspectedColumn =
          introspectedTable.getColumn(introspectedTable.getGeneratedKey().getColumn());
      if (introspectedColumn == null) {
        // the specified column doesn't exist, so don't do the generated
        // key
        // (the warning has already been reported)
        returnType = null;
      } else {
        returnType = introspectedColumn.getFullyQualifiedJavaType();
        importedTypes.add(returnType);
      }
    } else {
      returnType = null;
    }

    method.setReturnType(returnType);
    method.setVisibility(JavaVisibility.PUBLIC);
    DAOMethodNameCalculator methodNameCalculator = getDAOMethodNameCalculator();
    method.setName(methodNameCalculator.getInsertMethodName(introspectedTable));

    FullyQualifiedJavaType parameterType = introspectedTable.getRules().calculateAllFieldsClass();

    importedTypes.add(parameterType);
    method.addParameter(new Parameter(parameterType, "record")); // $NON-NLS-1$

    for (FullyQualifiedJavaType fqjt : daoTemplate.getCheckedExceptions()) {
      method.addException(fqjt);
      importedTypes.add(fqjt);
    }

    ibatorContext.getCommentGenerator().addGeneralMethodComment(method, table);

    return method;
  }