예제 #1
0
 public void doCodeGen() throws IOException {
   if (schema == null) {
     loadSchema();
   }
   doCodeGen(schema);
   if (new File(configuration.getSpringXmlBaseDir(), "log4j.xml").exists() == false) {
     String log4j =
         TemplateUtil.renderTemplate(
             getClassTemplate(getClass(), "log4j.xml.tmpl"),
             ParamMapBuilder.newBuilder().buildMap());
     writeCode(new File(configuration.getSpringXmlBaseDir()), "log4j.xml", log4j);
   }
   ObjectMapper mapper = new ObjectMapper();
   String json = mapper.writeValueAsString(schema.getAllEntities());
   writeCode(new File(configuration.getSpringXmlBaseDir()), "object_mode.json", json);
 }
  public String genField() throws IOException {
    String entityVarName = JavaIdentifierUtil.toVariableName(entity.getName());
    String fieldVarName =
        field.getVarName() != null
            ? field.getVarName()
            : JavaIdentifierUtil.toVariableName(field.getName());
    String label = field.getLabel() == null ? field.getName() : field.getLabel();

    String uiType = field.getUitype();
    String element = "input";
    String attrValue = field.isNullable() ? "" : "required";

    if (field.isReadonly()) {
      attrValue = attrValue + " ng-readonly='true' ";
    }

    if (uiType == null) {
      uiType = "text";
    } else if (uiType.equalsIgnoreCase("datetime-local")) {
      uiType = "datetime_local";
    }

    HtmlFormInputType htmlFormInputType = HtmlFormInputType.TEXT;
    try {
      htmlFormInputType = HtmlFormInputType.valueOf(uiType.toUpperCase());
    } catch (Exception ex) {;
    }
    if (uiType == null || htmlFormInputType == HtmlFormInputType.TEXT) {
      if (field.getJdbcType() == Types.DATE) {
        htmlFormInputType = HtmlFormInputType.DATE;
        uiType = "date";
      } else if (field.getJdbcType() == Types.TIMESTAMP) {
        htmlFormInputType = HtmlFormInputType.DATETIME_LOCAL;
        uiType = "datetime-local";
      } else if (field.getJdbcType() == Types.TIME) {
        htmlFormInputType = HtmlFormInputType.TIME;
        uiType = "time";
      }
    }

    // System.out.println(String.format("%s %s > %s", field.getName(),
    // uiType, JdbcSqlTypeMap.getJdbcTyepName(field.getJdbcType())));

    String generated = "";
    String HTML_INPUT_TYPE = "";
    switch (htmlFormInputType) {
      case TEXTAREA:
        generated =
            TemplateUtil.renderTemplate(
                IOUtils.toString(getClass().getResourceAsStream("angularTextarea.html.tmpl")),
                ParamMapBuilder.newBuilder()
                    .addMapEntry("FIELD_VAR_NAME", fieldVarName)
                    .addMapEntry("HTML_TYPE", HTML_INPUT_TYPE)
                    .addMapEntry("ENTITY_VAR_NAME", entityVarName)
                    .addMapEntry("FIELD_ATTRIBUTES", attrValue)
                    .addMapEntry("FIELD_LABEL", label)
                    .buildMap());
        break;
      case CHECKBOX:
        StringBuilder checkboxSelections = new StringBuilder();
        String[] chkvals = getEnumValues();
        for (int i = 0; i < chkvals.length; i++) {
          String opt =
              TemplateUtil.renderTemplate(
                  IOUtils.toString(getClass().getResourceAsStream("angularCheckboxOpt.html.tmpl")),
                  ParamMapBuilder.newBuilder()
                      .addMapEntry("OPTION_VALUE", chkvals[i])
                      .addMapEntry("ENTITY_VAR_NAME", entityVarName)
                      .addMapEntry(
                          "FIELD_VAR_NAME_SEQ", String.format("%s%02d", fieldVarName, i + 1))
                      .addMapEntry("FIELD_VAR_NAME", fieldVarName)
                      .addMapEntry("FIELD_LABEL", label)
                      .buildMap());
          checkboxSelections.append(opt).append("\n");
        }
        generated =
            TemplateUtil.renderTemplate(
                IOUtils.toString(getClass().getResourceAsStream("angularCheckbox.html.tmpl")),
                ParamMapBuilder.newBuilder()
                    .addMapEntry("SELECTIONS", checkboxSelections.toString())
                    .addMapEntry("FIELD_LABEL", label)
                    .buildMap());
        break;
      case RADIO:
        StringBuilder radioSelections = new StringBuilder();
        String[] enumvals = getEnumValues();
        for (int i = 0; i < enumvals.length; i++) {
          String opt =
              TemplateUtil.renderTemplate(
                  IOUtils.toString(getClass().getResourceAsStream("angularRadioOpt.html.tmpl")),
                  ParamMapBuilder.newBuilder()
                      .addMapEntry("OPTION_VALUE", enumvals[i])
                      .addMapEntry("ENTITY_VAR_NAME", entityVarName)
                      .addMapEntry("FIELD_NAME", field.getObjectName())
                      .addMapEntry(
                          "FIELD_VAR_NAME_SEQ", String.format("%s%02d", fieldVarName, i + 1))
                      .addMapEntry("FIELD_VAR_NAME", fieldVarName)
                      .addMapEntry("FIELD_LABEL", label)
                      .buildMap());
          radioSelections.append(opt).append("\n");
        }
        generated =
            TemplateUtil.renderTemplate(
                IOUtils.toString(getClass().getResourceAsStream("angularRadio.html.tmpl")),
                ParamMapBuilder.newBuilder()
                    .addMapEntry("SELECTIONS", radioSelections.toString())
                    .addMapEntry("FIELD_LABEL", label)
                    .buildMap());
        break;
      case SELECT:
        StringBuilder selectSelections = new StringBuilder();
        String[] enumSvals = getEnumValues();
        for (int i = 0; i < enumSvals.length; i++) {
          String opt =
              TemplateUtil.renderTemplate(
                  IOUtils.toString(getClass().getResourceAsStream("angularSelectOpt.html.tmpl")),
                  ParamMapBuilder.newBuilder()
                      .addMapEntry("OPTION_VALUE", enumSvals[i])
                      .buildMap());
          selectSelections.append(opt).append("\n");
        }
        generated =
            TemplateUtil.renderTemplate(
                IOUtils.toString(getClass().getResourceAsStream("angularSelect.html.tmpl")),
                ParamMapBuilder.newBuilder()
                    .addMapEntry("FIELD_LABEL", label)
                    .addMapEntry("ENTITY_VAR_NAME", entityVarName)
                    .addMapEntry("FIELD_VAR_NAME", fieldVarName)
                    .addMapEntry("SELECTIONS", selectSelections.toString())
                    .buildMap());
        break;
      default:
        HTML_INPUT_TYPE = uiType;
        if (htmlFormInputType == HtmlFormInputType.DATETIME_LOCAL) {
          HTML_INPUT_TYPE = "datetime_local";
        }
        String datePicker = "";
        String fmt = field.getFormat();
        if (this.field.isDatetimeField()) {
          if (fmt == null) {
            fmt =
                this.field.getJdbcType() == Types.DATE
                    ? "yyyy-MM-dd"
                    : (this.field.getJdbcType() == Types.TIMESTAMP)
                        ? "yyyy-MM-dd HH:mm:ss"
                        : "HH:mm:ss";
          }
          datePicker =
              String.format(
                  "<datetimepicker data-ng-model=\"%s.%s\"></datetimepicker>",
                  entityVarName, fieldVarName);

        } else {
          fmt = null;
        }
        String templ = "angularEntityField.input.html.tmpl";
        if (!field.isReadonly()) {
          switch (this.field.getJdbcType()) {
            case Types.DATE:
              templ = "angularEntityFieldDatePicker.html.tmpl";
              break;
            case Types.TIMESTAMP:
              templ = "angularEntityFieldDatetimePicker.html.tmpl";
              break;
            case Types.TIME:
              templ = "angularEntityFieldTimePicker.html.tmpl";
              break;
            default:
              templ = "angularEntityField.input.html.tmpl";
              break;
          }
        }
        generated =
            TemplateUtil.renderTemplate(
                IOUtils.toString(getClass().getResourceAsStream(templ)),
                ParamMapBuilder.newBuilder()
                    .addMapEntry("FIELD_VAR_NAME", fieldVarName)
                    .addMapEntry("FIELD_NAME", field.getObjectName())
                    .addMapEntry("HTML_TYPE", HTML_INPUT_TYPE)
                    .addMapEntry("ENTITY_VAR_NAME", entityVarName)
                    .addMapEntry("DATEPICKER_PICKER_FOR_DATETIME", datePicker)
                    .addMapEntry("FIELD_ATTRIBUTES", attrValue)
                    .addMapEntry("FIELD_INPUT_TYPE", element)
                    .addMapEntry("FIELD_LABEL", label)
                    .buildMap());
        break;
    }
    return generated;
  }
예제 #3
0
  public void doCodeGen(Schema schema) throws IOException {
    schema.buildRelationship();
    if (configuration.isGenSpringContext()) {
      SpringContextGenerator springContext = new SpringContextGenerator(configuration, schema);
      springContext.doCodeGen();
    }

    if (configuration.isGenPomXml()) {
      PomXmlGenerator pomgen = new PomXmlGenerator(configuration, schema);
      pomgen.doCodeGen();
    }

    List<Entity> entites = schema.getAllEntities();
    for (Entity entity : entites) {
      if (configuration.getIncludes().size() > 0
          && !configuration.shouldInclude(entity.getName())) {
        continue;
      }
      if (configuration.shouldIgnore(entity.getName())) {
        continue;
      }

      if (configuration.isGenMyBatisMapper()) {
        JavaMyBatisMapperGenerator myBatisGen =
            new JavaMyBatisMapperGenerator(configuration, entity);
        myBatisGen.doCodeGen();
      }

      if (configuration.isGenRestController()) {
        JavaRestControllerGenerator restGen =
            new JavaRestControllerGenerator(configuration, entity);
        restGen.doCodeGen();
      }
      if (configuration.isGenMyBatisXmlMapper()) {
        MyBatisXmlEntityGenerator myBatisXmlGen =
            new MyBatisXmlEntityGenerator(configuration, entity);
        myBatisXmlGen.doCodeGen();
      }
      if (configuration.isGenDomainObject()) {
        JavaDomainObjectGenerator domainGen = new JavaDomainObjectGenerator(configuration, entity);
        domainGen.doCodeGen();
      }
    }

    if (configuration.isGenScaffoldTools()) {
      String filePath = configuration.getRestPackageName().replaceAll("[\\.]", "/");
      writeCode(
          new File(configuration.getJavaBaseDir(), filePath),
          "JScaffoldObjectModelController.java",
          TemplateUtil.renderTemplate(
              IOUtils.toString(
                  getClass().getResourceAsStream("JScaffoldObjectModelController.java.tmpl")),
              ParamMapBuilder.newBuilder()
                  .addMapEntry("REST_PACKAGE_NAME", configuration.getRestPackageName())
                  .buildMap()));
    }

    if (configuration.isGenAngular()) {
      System.out.println("Generating Angular artifacts");
      AngularUIGenerator augularGenerator = new AngularUIGenerator(configuration, schema);

      augularGenerator.doCodeGen();
    }

    if (configuration.isEnableSSL()) {
      SSLCertificateGenerator sslGen = new SSLCertificateGenerator(configuration);
      sslGen.doCodeGen();
    }
  }