Generate Schema Java Code

In this example you are going to learn how to generate java code from your schema.

JavaCodeBuilder will be used in the code generation process.

Java Code Listing

package com.northconcepts.datapipeline.foundations.examples.schema;

import com.northconcepts.datapipeline.core.FieldType;
import com.northconcepts.datapipeline.foundations.core.Attributes;
import com.northconcepts.datapipeline.foundations.core.Tags;
import com.northconcepts.datapipeline.foundations.schema.SchemaDef;
import com.northconcepts.datapipeline.foundations.schema.EntityDef;
import com.northconcepts.datapipeline.foundations.schema.NumericFieldDef;
import com.northconcepts.datapipeline.foundations.schema.TextFieldDef;
import com.northconcepts.datapipeline.foundations.schema.IndexDef;
import com.northconcepts.datapipeline.foundations.schema.EntityRelationshipDef;
import com.northconcepts.datapipeline.foundations.schema.RelationshipCardinality;
import com.northconcepts.datapipeline.foundations.schema.ForeignKeyAction;
import com.northconcepts.datapipeline.foundations.schema.PrimaryKeyType;
import com.northconcepts.datapipeline.sourcecode.CodeWriter;
import com.northconcepts.datapipeline.sourcecode.JavaCodeBuilder;

public class GenerateJavaCodeFromSchema {

    public static void main(String[] args) {

        JavaCodeBuilder code = new JavaCodeBuilder();
        CodeWriter codeWriter = code.getSourceWriter();
        codeWriter.println("public class DataPipelineExample {");
        codeWriter.println();
        codeWriter.indent();
        codeWriter.println("public static void main(String[] args) throws Throwable {");
        codeWriter.indent();

        SchemaDef schema = createSchema();

        schema.generateJavaCode(code);

        // Adding semicolon to avoid compilation errors.
        codeWriter.println(";");

        codeWriter.outdent();
        codeWriter.println("}");
        codeWriter.outdent();
        codeWriter.println("}");

        System.out.println(code.getSource());

    }
    
    public static SchemaDef createSchema(){
        SchemaDef schema = new SchemaDef()
                .setName("Code Generation")
                .setDescription("Schema description")
                .setAttributes(new Attributes().setValue("key", "value"))
                .setTags(new Tags().add("schema").add("dpf"));

        schema.addEntity(new EntityDef("suppliers")
                .addField(new NumericFieldDef("supplier_id", FieldType.LONG).setPrimaryKeyPosition(1).setPrimaryKeyType(PrimaryKeyType.IDENTITY))
                .addField(new TextFieldDef("supplier_name", FieldType.STRING))
                .addField(new TextFieldDef("city", FieldType.STRING).setStrictArrays(false))
                .addField(new TextFieldDef("country", FieldType.STRING))
                .addField(new TextFieldDef("contact_details", FieldType.STRING).setArray(true))
                .addIndex(new IndexDef("idx_supplier_name", "supplier_name").setUnique(false))
                .addIndex(new IndexDef("idx_city", "city").setUnique(false))
        );

        schema.addEntity(new EntityDef("products")
                .addField(new NumericFieldDef("product_id", FieldType.LONG))
                .addField(new NumericFieldDef("supplier_id", FieldType.LONG).setPrimaryKeyPosition(11))
                .addField(new TextFieldDef("product_name", FieldType.STRING).setPrimaryKeyPosition(22))
                .addField(new TextFieldDef("color", FieldType.STRING))
                .addField(new NumericFieldDef("weight", FieldType.DOUBLE))
                .addField(new NumericFieldDef("price", FieldType.DOUBLE))
                .addField(new NumericFieldDef("discount", FieldType.DOUBLE))
                .addIndex(new IndexDef("idx_supplier_id_product_name", "supplier_id", "product_name"))
                .addIndex(new IndexDef("idx_price_discount", "price", "discount"))
                .addIndex(new IndexDef("idx_color", "color"))
        );

        schema.addEntityRelationship(new EntityRelationshipDef("fk_suppliers_products")
                .setDescription("this is fk_suppliers_products.")
                .setPrimaryEntityName("suppliers")
                .setForeignEntityName("products")
                .setCardinality(RelationshipCardinality.ONE_TO_MANY)
                .setForeignKeyFieldNames("supplier_id")
                .setOnUpdateAction(ForeignKeyAction.CASCADE)
                .setOnDeleteAction(ForeignKeyAction.RESTRICT));
        
        return schema;
        
    }

}

Code Walkthrough

  1. An instance of CodeWriter is created and will be used for writing the java code.
  2. All the imports will be added automatically so the only thing you need to do is write the class and the main method.
  3. codeWriter.indent() and codeWriter.outdent() are basically used for formatting purposes i.e. they determine where the code will begin.
  4. createSchema() method is used to create a test schema that will be used to generate the java code.
  5. schema.generateJavaCode(code) will add the imports and generated schema code.
  6. code.getSource() will output the generated java code.

Output

import com.northconcepts.datapipeline.core.FieldType;
import com.northconcepts.datapipeline.foundations.core.Attributes;
import com.northconcepts.datapipeline.foundations.core.Tags;
import com.northconcepts.datapipeline.foundations.schema.EntityDef;
import com.northconcepts.datapipeline.foundations.schema.EntityRelationshipDef;
import com.northconcepts.datapipeline.foundations.schema.ForeignKeyAction;
import com.northconcepts.datapipeline.foundations.schema.IndexDef;
import com.northconcepts.datapipeline.foundations.schema.IndexFieldDef;
import com.northconcepts.datapipeline.foundations.schema.NumericFieldDef;
import com.northconcepts.datapipeline.foundations.schema.PrimaryKeyType;
import com.northconcepts.datapipeline.foundations.schema.RelationshipCardinality;
import com.northconcepts.datapipeline.foundations.schema.SchemaDef;
import com.northconcepts.datapipeline.foundations.schema.TextFieldDef;

public class DataPipelineExample {

  public static void main(String[] args) throws Throwable {
    new SchemaDef()
      .setName("Code Generation")
      .setDescription("Schema description")
      .setAttributes(new Attributes()
        .setValue("key", "value")
      )
      .setTags(new Tags("dpf", "schema"))
      .addEntity(new EntityDef()
        .setName("suppliers")
        .setAllowExtraFieldsInValidation(true)
        .setAllowExtraFieldsInMapping(true)
        .setAddMissingOptionalFields(false)
        .addField(new NumericFieldDef("supplier_id", FieldType.LONG)
          .setRequired(false)
          .setPrimaryKeyPosition(1)
          .setPrimaryKeyType(PrimaryKeyType.IDENTITY)
        )
        .addField(new TextFieldDef("supplier_name", FieldType.STRING)
          .setRequired(false)
          .setAllowBlank(true)
        )
        .addField(new TextFieldDef("city", FieldType.STRING)
          .setRequired(false)
          .setStrictArrays(false)
          .setAllowBlank(true)
        )
        .addField(new TextFieldDef("country", FieldType.STRING)
          .setRequired(false)
          .setAllowBlank(true)
        )
        .addField(new TextFieldDef("contact_details", FieldType.STRING)
          .setRequired(false)
          .setMinimumElements(0)
          .setStrictArrays(true)
          .setAllowBlank(true)
        )
        .addIndex(new IndexDef("idx_supplier_name")
          .setUnique(false)
          .addIndexField(new IndexFieldDef("supplier_name", true))
        )
        .addIndex(new IndexDef("idx_city")
          .setUnique(false)
          .addIndexField(new IndexFieldDef("city", true))
        )
      )
      .addEntity(new EntityDef()
        .setName("products")
        .setAllowExtraFieldsInValidation(true)
        .setAllowExtraFieldsInMapping(true)
        .setAddMissingOptionalFields(false)
        .addField(new NumericFieldDef("product_id", FieldType.LONG)
          .setRequired(false)
        )
        .addField(new NumericFieldDef("supplier_id", FieldType.LONG)
          .setRequired(false)
          .setPrimaryKeyPosition(11)
        )
        .addField(new TextFieldDef("product_name", FieldType.STRING)
          .setRequired(false)
          .setPrimaryKeyPosition(22)
          .setAllowBlank(true)
        )
        .addField(new TextFieldDef("color", FieldType.STRING)
          .setRequired(false)
          .setAllowBlank(true)
        )
        .addField(new NumericFieldDef("weight", FieldType.DOUBLE)
          .setRequired(false)
        )
        .addField(new NumericFieldDef("price", FieldType.DOUBLE)
          .setRequired(false)
        )
        .addField(new NumericFieldDef("discount", FieldType.DOUBLE)
          .setRequired(false)
        )
        .addIndex(new IndexDef("idx_supplier_id_product_name")
          .setUnique(false)
          .addIndexField(new IndexFieldDef("supplier_id", true))
          .addIndexField(new IndexFieldDef("product_name", true))
        )
        .addIndex(new IndexDef("idx_price_discount")
          .setUnique(false)
          .addIndexField(new IndexFieldDef("price", true))
          .addIndexField(new IndexFieldDef("discount", true))
        )
        .addIndex(new IndexDef("idx_color")
          .setUnique(false)
          .addIndexField(new IndexFieldDef("color", true))
        )
      )
      .addEntityRelationship(new EntityRelationshipDef("fk_suppliers_products")
        .setDescription("this is fk_suppliers_products.")
        .setCardinality(RelationshipCardinality.ONE_TO_MANY)
        .setPrimaryEntityName("suppliers")
        .setForeignEntityName("products")
        .addForeignKeyFieldNames("supplier_id")
        .setOnUpdateAction(ForeignKeyAction.CASCADE)
        .setOnDeleteAction(ForeignKeyAction.RESTRICT)
      )
    ;
  }
}
Mobile Analytics