be.nille.generator.parser.ParserService.java Source code

Java tutorial

Introduction

Here is the source code for be.nille.generator.parser.ParserService.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package be.nille.generator.parser;

import be.nille.generator.data.db.ColumnMetaData;
import be.nille.generator.data.db.TableMetaData;
import be.nille.generator.data.entity.EntityData;
import be.nille.generator.data.entity.FieldData;
import be.nille.generator.data.parser.EntityParserData;
import be.nille.generator.data.parser.FieldParserData;
import be.nille.generator.data.parser.ParserData;
import be.nille.generator.db.DatabaseManager;
import com.squareup.javawriter.JavaWriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import static javax.lang.model.element.Modifier.PRIVATE;
import static javax.lang.model.element.Modifier.PUBLIC;
import java.util.EnumSet;
import java.util.List;
import static javax.lang.model.element.Modifier.FINAL;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Niels
 */
public class ParserService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ParserService.class);

    private final String GET_PREFIX = "get";
    private final String SET_PREFIX = "set";

    private EntityParserData createEntityParseData(TableMetaData tableMetaData) {
        EntityParserData entityParserData = new EntityParserData();
        entityParserData.setTableData(tableMetaData);
        //create the entityData
        EntityData entityData = new EntityData();
        entityData.setName(tableMetaData.getName());
        entityData.setPackageName("be.nille.example.model");
        entityParserData.setEntityData(entityData);

        return entityParserData;
    }

    private FieldParserData createFieldParserData(ColumnMetaData columnMetaData) {
        FieldParserData fieldParserData = new FieldParserData();
        fieldParserData.setColumnData(columnMetaData);
        //create the fieldData
        FieldData fieldData = new FieldData();
        fieldData.setName(columnMetaData.getName().toLowerCase());
        //Here we should do a mapping of types
        fieldData.setType("String");
        fieldParserData.setFieldData(fieldData);
        return fieldParserData;
    }

    public ParserData createParserData() throws Exception {
        ParserData parserData = new ParserData();
        DatabaseManager manager = new DatabaseManager("jdbc:hsqldb:mem:test", "SA", "", "PUBLIC");
        Connection conn = manager.getConnection();
        DatabaseMetaData databaseMetaData = manager.getDatabaseMetaData();
        List<TableMetaData> tableData = manager.getTables(databaseMetaData, "PUBLIC");
        for (TableMetaData tableMetaData : tableData) {
            //create EntityParserData
            EntityParserData entityParserData = createEntityParseData(tableMetaData);
            parserData.getData().add(entityParserData);
            List<ColumnMetaData> columnData = manager.getColumns(databaseMetaData, "PUBLIC",
                    tableMetaData.getName());
            for (ColumnMetaData columnMetaData : columnData) {
                //create FieldParserData
                FieldParserData fieldParserData = createFieldParserData(columnMetaData);
                entityParserData.getData().add(fieldParserData);
            }
            parseEntityData(entityParserData);
        }
        manager.closeConnection();
        return parserData;
    }

    public void testService() throws IOException {

        try (JavaWriter writer = new JavaWriter(getPrintWriter())) {
            writer.emitPackage("com.example")
                    .emitImports("javax.persistence.Column", "javax.persistence.Entity",
                            "javax.persistence.GeneratedValue", "javax.persistence.Id", "javax.persistence.Table")
                    .emitEmptyLine().emitAnnotation("Entity").emitAnnotation("Column(name=\"TBL_PERSON\")")
                    .beginType("com.example.Person", "class", EnumSet.of(PUBLIC, FINAL))

                    .emitField("String", "firstName", EnumSet.of(PRIVATE))
                    .emitField("String", "lastName", EnumSet.of(PRIVATE))
                    .emitJavadoc("Returns the person's full name.")
                    .beginMethod("String", "getName", EnumSet.of(PUBLIC))
                    .emitStatement("return firstName + \" \" + lastName").endMethod().endType();
        }
    }

    private Writer getPrintWriter() {
        return new PrintWriter(System.out);
    }

    public void parseEntityData(EntityParserData parserData) throws IOException {
        StringWriter sw = new StringWriter();
        JavaWriter writer = new JavaWriter(sw);

        //write packageName
        String entityName = ParserUtils.createEntityName(parserData.getEntityData().getName());

        writer.emitPackage(parserData.getEntityData().getPackageName())
                .beginType(entityName, "class", EnumSet.of(PUBLIC)).emitEmptyLine();
        //write fields
        for (FieldParserData fieldData : parserData.getData()) {
            writer.emitField(fieldData.getFieldData().getType(),
                    ParserUtils.createFieldName(fieldData.getFieldData().getName()), EnumSet.of(PRIVATE));
        }
        writer.emitEmptyLine();

        //write getters and setters
        for (FieldParserData fieldData : parserData.getData()) {

            String fieldType = fieldData.getFieldData().getType();
            String fieldName = fieldData.getFieldData().getName();
            // the getter
            writer.beginMethod(fieldType, GET_PREFIX + upperCaseFirst(fieldName), EnumSet.of(PUBLIC))
                    .emitStatement("return " + fieldName).endMethod();

            // the setter
            writer.beginMethod("void", SET_PREFIX + upperCaseFirst(fieldName), EnumSet.of(PUBLIC), fieldType,
                    fieldName).emitStatement("this." + fieldName + " = " + fieldName).endMethod();

        }

        writer.endType();
        writer.close();
        System.out.print(sw.toString());

    }

    private String upperCaseFirst(String string) {
        return StringUtils.capitalize(string);

    }

}