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

nbcp.myoql.code.generator.db.mysql.BaseEntityInfo.kt Maven / Gradle / Ivy

The newest version!
package nbcp.myoql.code.generator.db.mysql

import nbcp.base.extend.AllFields

class BaseEntityInfo(var entity: EntityDbItemData, var baseEntityClass: Array>) {
    fun getBaseInterfaces(): List> {
        var list = mutableListOf>()
        baseEntityClass.forEach {
            if (it.isInterface == false) {
                return@forEach
            }

            if (itMatch(entity, it) == false) {
                return@forEach
            }

            list.add(it);
        }
        return list;
    }

    private fun itMatch(entity: EntityDbItemData, it: Class<*>): Boolean {
        return it.AllFields.all { field ->
            var fieldName = field.name;
            var column = entity.columns.find { it.fieldName == fieldName };
            if (column == null) {
                return@all false;
            }

            if (column.dbType.javaType != field.type) {
                return@all false;
            }

            return@all true
        }
    }

    fun getBaseClasses(): List> {
        var list = mutableListOf>()
        baseEntityClass.forEach {
            if (it.isInterface) {
                return@forEach
            }

            if (itMatch(entity, it) == false) {
                return@forEach
            }

            list.add(it);
        }
        return list;
    }


    fun getColumnsWithoutBaseClasses(): Set {
        var set = mutableSetOf()
        getBaseClasses().forEach { type ->
            set.addAll(type.AllFields.map { it.name })
        }

        return set;
    }

    fun getColumnsWithoutBaseInterfaces(): Set {
        var set = mutableSetOf()
        getBaseInterfaces().forEach { type ->
            set.addAll(type.AllFields.map { it.name })
        }

        return set;
    }

    fun getJpaStyleFields(): List {
        var baseColumns = getColumnsWithoutBaseClasses();
        var baseColumnsInInterface = getColumnsWithoutBaseInterfaces();
        return this.entity.columns
                .filter { column ->
                    return@filter baseColumns.contains(column.fieldName)
                }
                .map { column ->
                    """/** ${column.simpleComment} */
@Cn("${column.simpleComment}")
@Column(name = "${column.name}")
@DbName(name = "${column.name}")
private ${if (baseColumnsInInterface.contains(column.fieldName)) "override" else ""}${column.javaType} ${column.fieldName} ;"""
                }
    }

    fun getBaseClasseString(): String {
        var list = getBaseClasses();
        if (list.any() == false) return "";
        return " extends ${list.joinToString(",")}"
    }

    fun getBaseInterfaceString(): String {
        var list = getBaseInterfaces();
        if (list.any() == false) return "";
        return " implements ${list.joinToString(",")}"
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy