All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dbtools.gen.android.AndroidBaseManagerRenderer Maven / Gradle / Ivy

There is a newer version: 11.0.0
Show newest version
/*
 * AndroidBaseRecordManager.java
 *
 * Created on Sep 9, 2010
 *
 * Copyright 2010 Jeff Campbell. All rights reserved. Unauthorized reproduction
 * is a violation of applicable law. This material contains certain
 * confidential or proprietary information and trade secrets of Jeff Campbell.
 */
package org.dbtools.gen.android;


import org.dbtools.codegen.java.Access;
import org.dbtools.codegen.java.JavaClass;
import org.dbtools.codegen.java.JavaMethod;
import org.dbtools.codegen.java.JavaVariable;
import org.dbtools.gen.AnnotationConsts;
import org.dbtools.gen.GenConfig;
import org.dbtools.schema.schemafile.SchemaEntity;
import org.dbtools.schema.schemafile.SchemaEntityType;
import org.dbtools.schema.schemafile.SchemaTable;

import java.util.Arrays;

/**
 * @author Jeff
 */
public class AndroidBaseManagerRenderer {

    private JavaClass myClass;
    private GenConfig genConfig;

    public void generate(SchemaEntity entity, String packageName, AndroidGeneratedEntityInfo generatedEntityInfo) {
        String recordClassName = AndroidRecordRenderer.createClassName(entity);
        String className = getClassName(entity);
        myClass = new JavaClass(packageName, className);
        myClass.setAbstract(true);

        // header comment
        // Do not place date in file because it will cause a new check-in to scm        
        String fileHeaderComment;
        fileHeaderComment = "/*\n";
        fileHeaderComment += " * " + className + ".java\n";
        fileHeaderComment += " *\n";
        fileHeaderComment += " * GENERATED FILE - DO NOT EDIT\n";
        fileHeaderComment += " * \n";
        fileHeaderComment += " */\n";
        myClass.setFileHeaderComment(fileHeaderComment);

        // Since this is generated code.... suppress all warnings
        myClass.addAnnotation("@SuppressWarnings(\"all\")");

        // generate all of the main methods
        createManager(entity, packageName, recordClassName, generatedEntityInfo);
    }

    private void createManager(SchemaEntity entity, String packageName, String recordClassName, AndroidGeneratedEntityInfo generatedEntityInfo) {
        String recordConstClassName = recordClassName + "Const";
        String managerClassName = recordClassName + "Manager";
        SchemaEntityType type = entity.getType();

        String databaseManagerPackage = packageName.substring(0, packageName.lastIndexOf('.'));
        if (genConfig.isIncludeDatabaseNameInPackage()) {
            databaseManagerPackage = databaseManagerPackage.substring(0, databaseManagerPackage.lastIndexOf('.'));
        }

        myClass.addImport(databaseManagerPackage + ".DatabaseManager");

        switch (type) {
            case TABLE:
                SchemaTable tableEntity = (SchemaTable) entity;
                if (tableEntity.isReadonly()) {
                    myClass.addImport(genConfig.isRxJavaSupport() ? "org.dbtools.android.domain.RxAndroidBaseManagerReadOnly" : "org.dbtools.android.domain.AndroidBaseManagerReadOnly");
                    myClass.setExtends(genConfig.isRxJavaSupport() ? "RxAndroidBaseManagerReadOnly<" + recordClassName + ">" : "AndroidBaseManagerReadOnly<" + recordClassName + ">");
                } else {
                    myClass.addImport(genConfig.isRxJavaSupport() ? "org.dbtools.android.domain.RxAndroidBaseManagerWritable" : "org.dbtools.android.domain.AndroidBaseManagerWritable");
                    myClass.setExtends(genConfig.isRxJavaSupport() ? "RxAndroidBaseManagerWritable<" + recordClassName + ">" : "AndroidBaseManagerWritable<" + recordClassName + ">");
                }
                break;
            case VIEW:
            case QUERY:
                myClass.addImport(genConfig.isRxJavaSupport() ? "org.dbtools.android.domain.RxAndroidBaseManagerReadOnly" : "org.dbtools.android.domain.AndroidBaseManagerReadOnly");
                myClass.setExtends(genConfig.isRxJavaSupport() ? "RxAndroidBaseManagerReadOnly<" + recordClassName + ">" : "AndroidBaseManagerReadOnly<" + recordClassName + ">");
                break;
        }

        addMethodAnnotations(AnnotationConsts.NONNULL, myClass.addMethod(Access.PUBLIC, "String", "getDatabaseName", "return " + recordConstClassName + ".DATABASE;"));
        addMethodAnnotations(AnnotationConsts.NONNULL, myClass.addMethod(Access.PUBLIC, recordClassName, "newRecord", "return new " + recordClassName + "();"));

        if (type != SchemaEntityType.QUERY) {
            addMethodAnnotations(AnnotationConsts.NONNULL, myClass.addMethod(Access.PUBLIC, "String", "getTableName", "return " + recordConstClassName + ".TABLE;"));
        }

        addMethodAnnotations(AnnotationConsts.NONNULL, myClass.addMethod(Access.PUBLIC, "String[]", "getAllColumns", "return " + recordConstClassName + ".ALL_COLUMNS;"));

        JavaVariable databaseNameParam = new JavaVariable("String", "databaseName");
        if (genConfig.isJsr305Support()) {
            databaseNameParam.addAnnotation(AnnotationConsts.NONNULL);
        }

        // constructor
        myClass.setCreateDefaultConstructor(false);
        JavaVariable constructorParam = new JavaVariable("DatabaseManager", "databaseManager");
        myClass.addConstructor(Access.PUBLIC, Arrays.asList(constructorParam), "super(databaseManager);");

        switch (type) {
            default:
            case TABLE:
                if (generatedEntityInfo.isPrimaryKeyAdded()) {
                    addMethodAnnotations(AnnotationConsts.NONNULL, myClass.addMethod(Access.PUBLIC, "String", "getPrimaryKey", "return " + recordConstClassName + "." + AndroidBaseRecordRenderer.PRIMARY_KEY_COLUMN + ";"));
                } else {
                    addMethodAnnotations(AnnotationConsts.NONNULL, myClass.addMethod(Access.PUBLIC, "String", "getPrimaryKey", "return \"NO_PRIMARY_KEY\";"));
                }
                addMethodAnnotations(AnnotationConsts.NONNULL, myClass.addMethod(Access.PUBLIC, "String", "getDropSql", "return " + recordConstClassName + ".DROP_TABLE;"));
                addMethodAnnotations(AnnotationConsts.NONNULL, myClass.addMethod(Access.PUBLIC, "String", "getCreateSql", "return " + recordConstClassName + ".CREATE_TABLE;"));
                addMethodAnnotations(AnnotationConsts.NONNULL, myClass.addMethod(Access.PUBLIC, "String", "getInsertSql", "return " + recordConstClassName + ".INSERT_STATEMENT;"));
                addMethodAnnotations(AnnotationConsts.NONNULL, myClass.addMethod(Access.PUBLIC, "String", "getUpdateSql", "return " + recordConstClassName + ".UPDATE_STATEMENT;"));
                break;
            case VIEW:
                addMethodAnnotations(AnnotationConsts.NONNULL, myClass.addMethod(Access.PUBLIC, "String", "getPrimaryKey", "return \"\";"));
                addMethodAnnotations(AnnotationConsts.NONNULL, myClass.addMethod(Access.PUBLIC, "String", "getDropSql", "return " + managerClassName + ".DROP_VIEW;"));
                addMethodAnnotations(AnnotationConsts.NONNULL, myClass.addMethod(Access.PUBLIC, "String", "getCreateSql", "return " + managerClassName + ".CREATE_VIEW;"));
                addMethodAnnotations(AnnotationConsts.NONNULL, myClass.addMethod(Access.PUBLIC, "String", "getInsertSql", "return \"\";"));
                addMethodAnnotations(AnnotationConsts.NONNULL, myClass.addMethod(Access.PUBLIC, "String", "getUpdateSql", "return \"\";"));
                break;
            case QUERY:
                JavaMethod getQueryMethod = myClass.addMethod(Access.PUBLIC, "String", "getQuery", "");
                getQueryMethod.setAbstract(true);

                addMethodAnnotations(AnnotationConsts.NONNULL, myClass.addMethod(Access.PUBLIC, "String", "getTableName", "return getQuery();"));
                addMethodAnnotations(AnnotationConsts.NONNULL, myClass.addMethod(Access.PUBLIC, "String", "getPrimaryKey", "return \"\";"));
                addMethodAnnotations(AnnotationConsts.NONNULL, myClass.addMethod(Access.PUBLIC, "String", "getDropSql", "return \"\";"));
                addMethodAnnotations(AnnotationConsts.NONNULL, myClass.addMethod(Access.PUBLIC, "String", "getCreateSql", "return \"\";"));
                addMethodAnnotations(AnnotationConsts.NONNULL, myClass.addMethod(Access.PUBLIC, "String", "getInsertSql", "return \"\";"));
                addMethodAnnotations(AnnotationConsts.NONNULL, myClass.addMethod(Access.PUBLIC, "String", "getUpdateSql", "return \"\";"));
                break;
        }
    }

    private void addMethodAnnotations(String annotation, JavaMethod javaMethod) {
        if (genConfig.isJsr305Support()) {
            javaMethod.addAnnotation(annotation);
        }
    }

    public static String getClassName(SchemaEntity table) {
        String recordClassName = AndroidRecordRenderer.createClassName(table);
        return recordClassName + "BaseManager";
    }

    public void writeToFile(String outDir) {
        myClass.writeToDisk(outDir);
    }

    public void setGenConfig(GenConfig genConfig) {
        this.genConfig = genConfig;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy