コード例 #1
0
 /**
  * prepares the model for template generation
  *
  * @param resources
  */
 protected void preprocess(List<Resource> resources) {
   for (Resource resource : resources) {
     for (Model model : resource.getModels()) {
       //	apply keyword mapping
       for (ModelField modelField : model.getFields()) {
         modelField.setName(reservedWordMapper.translate(modelField.getName()));
       }
     }
   }
 }
コード例 #2
0
  /**
   * Generates assembler classes if the API returns more than one object.
   *
   * @param resources
   * @param templateGroup
   */
  private void generateModelClassesForInput(
      List<Resource> resources, StringTemplateGroup templateGroup) {
    List<String> generatedClasses = new ArrayList<String>();
    for (Resource resource : resources) {
      if (resource.getEndPoints() != null) {
        for (Endpoint endpoint : resource.getEndPoints()) {
          if (endpoint.getOperations() != null) {
            for (EndpointOperation operation : endpoint.getOperations()) {
              ResourceMethod method =
                  operation.generateMethod(
                      endpoint, resource, dataTypeMappingProvider, nameGenerator);
              if (method.getInputModel() != null) {
                Model model = method.getInputModel();
                if (model != null) {
                  if (!generatedClasses.contains(model.getName())) {
                    List<String> imports = new ArrayList<String>();
                    imports.addAll(this.config.getDefaultModelImports());
                    for (ModelField param : model.getFields()) {
                      param.setName(reservedWordMapper.translate(param.getName()));
                      for (String importDef :
                          param
                              .getFieldDefinition(
                                  this.getDataTypeMappingProvider(), config, nameGenerator)
                              .getImportDefinitions()) {
                        if (!imports.contains(importDef)) {
                          imports.add(importDef);
                        }
                      }
                    }
                    StringTemplate template = templateGroup.getInstanceOf(MODEL_OBJECT_TEMPLATE);

                    template.setAttribute("fields", model.getFields());
                    template.setAttribute("imports", imports);
                    template.setAttribute("extends", config.getDefaultModelBaseClass());
                    template.setAttribute(
                        "annotationPackageName", languageConfig.getAnnotationPackageName());
                    template.setAttribute("className", model.getGenratedClassName());
                    template.setAttribute(PACKAGE_NAME, config.getModelPackageName());
                    File aFile =
                        new File(
                            languageConfig.getModelClassLocation()
                                + model.getGenratedClassName()
                                + languageConfig.getClassFileExtension());
                    writeFile(aFile, template.toString(), "Input model class");
                    generatedClasses.add(model.getName());
                  }
                }
              }
            }
          }
        }
      }
    }
  }
コード例 #3
0
  /**
   * Creates a wrapper model class that contains all model classes as list of objects. This class is
   * used for storing test data
   */
  private void generateWrapperClassForTestData(
      List<String> generatedClassNames, StringTemplateGroup templateGroup) {
    Model model = new Model();
    model.setName("TestData");
    model.setDescription(
        "Class used to store all the test data. This should not be used for any development");
    List<ModelField> modelFields = new ArrayList<ModelField>();
    model.setFields(modelFields);
    for (String className : generatedClassNames) {
      ModelField aParam = new ModelField();
      aParam.setName(this.getNameGenerator().applyMethodNamingPolicy(className) + "List");
      aParam.setParamType(this.getDataTypeMappingProvider().getListReturnTypeSignature(className));
      modelFields.add(aParam);
    }

    List<String> imports = new ArrayList<String>();
    imports.addAll(this.config.getDefaultModelImports());
    imports.addAll(this.getDataTypeMappingProvider().getListIncludes());
    for (ModelField param : model.getFields()) {
      for (String importDef :
          param
              .getFieldDefinition(this.getDataTypeMappingProvider(), config, nameGenerator)
              .getImportDefinitions()) {
        if (!imports.contains(importDef)) {
          imports.add(importDef);
        }
      }
    }
    StringTemplate template = templateGroup.getInstanceOf(MODEL_OBJECT_TEMPLATE);
    template.setAttribute("fields", model.getFields());
    template.setAttribute("imports", imports);
    template.setAttribute("annotationPackageName", languageConfig.getAnnotationPackageName());
    template.setAttribute("extends", config.getDefaultModelBaseClass());
    template.setAttribute(PACKAGE_NAME, config.getModelPackageName());
    template.setAttribute("className", model.getGenratedClassName());
    File aFile =
        new File(
            languageConfig.getModelClassLocation()
                + model.getGenratedClassName()
                + languageConfig.getClassFileExtension());
    writeFile(aFile, template.toString(), "Wrapper class for test data file");
  }