Search in sources :

Example 16 with ColumnDef

use of com.developmentontheedge.be5.metadata.model.ColumnDef in project be5 by DevelopmentOnTheEdge.

the class AppSync method fixPrimaryKey.

private void fixPrimaryKey(TableDef orphanDdl, TableDef ddl) {
    ColumnDef pk = ddl.getColumns().get(ddl.getEntity().getPrimaryKey());
    // Orphans have no primary key set: try to set the same column as in original table
    if (pk != null) {
        ColumnDef orphanPk = orphanDdl.getColumns().getCaseInsensitive(pk.getName());
        if (orphanPk != null) {
            orphanDdl.getIndicesUsingColumn(orphanPk.getName()).stream().filter(idx -> idx.getSize() == 1 && idx.isUnique()).findFirst().ifPresent(idx -> {
                // Remove primary key index
                DataElementUtils.remove(idx);
                orphanDdl.getEntity().setPrimaryKey(orphanPk.getName());
                orphanPk.setPrimaryKey(true);
            });
        }
    }
}
Also used : IndexColumnDef(com.developmentontheedge.be5.metadata.model.IndexColumnDef) ColumnDef(com.developmentontheedge.be5.metadata.model.ColumnDef)

Example 17 with ColumnDef

use of com.developmentontheedge.be5.metadata.model.ColumnDef in project be5 by DevelopmentOnTheEdge.

the class AppSync method createEntities.

private void createEntities() throws ExtendedSqlException, SQLException {
    Rdbms databaseSystem = DatabaseUtils.getRdbms(connector);
    DbmsTypeManager typeManager = databaseSystem == null ? new DefaultTypeManager() : databaseSystem.getTypeManager();
    boolean casePreserved = typeManager.normalizeIdentifierCase("aA").equals("aA");
    entities = new ArrayList<>();
    Project project = new Project("internal-db");
    project.setDatabaseSystem(be5Project.getDatabaseSystem());
    Module module = new Module("temp", project);
    for (String table : tableTypes.keySet()) {
        if (!"TABLE".equals(tableTypes.get(table.toLowerCase())))
            continue;
        List<SqlColumnInfo> columnInfos = columns.get(table.toLowerCase());
        if (columnInfos == null)
            continue;
        Entity entity = new Entity(table, module, EntityType.TABLE);
        entities.add(entity);
        TableDef tableDef = new TableDef(entity);
        for (SqlColumnInfo info : columnInfos) {
            ColumnDef column = new ColumnDef(info.getName(), tableDef.getColumns());
            column.setType(createColumnType(info));
            typeManager.correctType(column.getType());
            // PENDING
            column.setPrimaryKey(info.getName().equalsIgnoreCase(entity.getPrimaryKey()));
            column.setCanBeNull(info.isCanBeNull());
            String defaultValue = info.getDefaultValue();
            column.setAutoIncrement(info.isAutoIncrement());
            if (!info.isAutoIncrement()) {
                column.setDefaultValue(defaultValue);
            }
            if (column.isPrimaryKey() && typeManager.getKeyType().equals(typeManager.getTypeClause(column.getType()))) {
                column.getType().setTypeName(SqlColumnType.TYPE_KEY);
            }
            // column.setOriginModuleName( module.getName() );
            DataElementUtils.saveQuiet(column);
        }
        List<IndexInfo> indexInfos = indices.get(table.toLowerCase(Locale.ENGLISH));
        if (indexInfos != null) {
            INDEX: for (IndexInfo info : indexInfos) {
                if (!casePreserved)
                    info.setName(info.getName().toUpperCase(Locale.ENGLISH));
                IndexDef index = new IndexDef(info.getName(), tableDef.getIndices());
                index.setUnique(info.isUnique());
                for (String indexCol : info.getColumns()) {
                    IndexColumnDef indexColumnDef = IndexColumnDef.createFromString(indexCol, index);
                    if (tableDef.getColumns().get(indexColumnDef.getName()) == null) {
                        if (debug) {
                            warnings.add("Unsupported functional index found: " + index.getName() + " (problem is here: " + indexCol + "); skipped");
                        }
                        continue INDEX;
                    }
                    DataElementUtils.saveQuiet(indexColumnDef);
                }
                if (index.isUnique() && index.getSize() == 1) {
                    IndexColumnDef indexColumnDef = index.iterator().next();
                    if (!indexColumnDef.isFunctional()) {
                        if (index.getName().equalsIgnoreCase(table + "_pkey")) {
                            entity.setPrimaryKey(indexColumnDef.getName());
                            continue;
                        }
                    }
                }
                DataElementUtils.saveQuiet(index);
            }
        }
        DataElementUtils.saveQuiet(tableDef);
    }
    if (sqlExecutor.getConnector().getType() != DbmsType.MYSQL)
        return;
    // For MySQL only now
    for (Entity entity : entities) {
        final String table = entity.getName();
        if (!"VIEW".equalsIgnoreCase(tableTypes.get(table)))
            continue;
        String createTable;
        ResultSet rs = sqlExecutor.executeNamedQuery("sql.getTableDefinition", table);
        try {
            if (!rs.next())
                continue;
            createTable = rs.getString(2);
        } finally {
            sqlExecutor.getConnector().close(rs);
        }
        int as = createTable.indexOf(" AS ");
        if (as < 0)
            continue;
        createTable = createTable.substring(as + " AS ".length());
        ViewDef def = new ViewDef(entity);
        def.setDefinition(createTable);
        DataElementUtils.saveQuiet(def);
    }
}
Also used : DefaultTypeManager(com.developmentontheedge.be5.metadata.sql.type.DefaultTypeManager) Entity(com.developmentontheedge.be5.metadata.model.Entity) IndexColumnDef(com.developmentontheedge.be5.metadata.model.IndexColumnDef) Rdbms(com.developmentontheedge.be5.metadata.sql.Rdbms) IndexColumnDef(com.developmentontheedge.be5.metadata.model.IndexColumnDef) ColumnDef(com.developmentontheedge.be5.metadata.model.ColumnDef) IndexInfo(com.developmentontheedge.be5.metadata.sql.pojo.IndexInfo) ViewDef(com.developmentontheedge.be5.metadata.model.ViewDef) TableDef(com.developmentontheedge.be5.metadata.model.TableDef) Project(com.developmentontheedge.be5.metadata.model.Project) ResultSet(java.sql.ResultSet) Module(com.developmentontheedge.be5.metadata.model.Module) SqlColumnInfo(com.developmentontheedge.be5.metadata.sql.pojo.SqlColumnInfo) IndexDef(com.developmentontheedge.be5.metadata.model.IndexDef) DbmsTypeManager(com.developmentontheedge.be5.metadata.sql.type.DbmsTypeManager)

Example 18 with ColumnDef

use of com.developmentontheedge.be5.metadata.model.ColumnDef in project be5 by DevelopmentOnTheEdge.

the class EntityModels method createEntities.

// private String artifactIdToPackage(String path)
// {
// StringBuilder s = new StringBuilder();
// boolean toUpperCase = false;
// path = path.toLowerCase();
// for (int i = 0; i < path.length(); i++)
// {
// if(path.charAt(i) == '-')
// {
// toUpperCase = true;
// continue;
// }
// if(toUpperCase){
// s.append(Character.toUpperCase(path.charAt(i)));
// toUpperCase = false;
// }else{
// s.append(path.charAt(i));
// }
// }
// 
// return s.toString();
// }
private void createEntities(String generatedSourcesPath, String packageName, Configuration cfg) throws IOException {
    Template entityTpl = cfg.getTemplate("entity.ftl");
    Meta meta = injector.getMeta();
    List<Entity> entities = meta.getOrderedEntities("ru");
    for (Entity entity : entities) {
        if (entity.getName().startsWith("_"))
            continue;
        String entityClassName = Strings.capitalize(entity.getName());
        Map<String, Object> input = new HashMap<>();
        input.put("entityClassName", entityClassName);
        input.put("packageName", packageName);
        Map<String, ColumnDef> columns = meta.getColumns(entity);
        List<ColumnsInfo> columnsInfos = new ArrayList<>();
        for (ColumnDef columnDef : columns.values()) {
            columnsInfos.add(new ColumnsInfo(columnDef.getName(), meta.getColumnType(columnDef).getSimpleName()));
        }
        input.put("columns", columnsInfos);
        Utils.createFile(generatedSourcesPath, packageName, entityClassName + ".java", entityTpl, input);
        entityCount++;
    }
}
Also used : Meta(com.developmentontheedge.be5.api.services.Meta) Entity(com.developmentontheedge.be5.metadata.model.Entity) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ColumnDef(com.developmentontheedge.be5.metadata.model.ColumnDef) Template(freemarker.template.Template)

Example 19 with ColumnDef

use of com.developmentontheedge.be5.metadata.model.ColumnDef in project be5 by DevelopmentOnTheEdge.

the class DpsHelper method getDeleteSpecialValues.

public Object[] getDeleteSpecialValues(BeModelElement modelElements) {
    Map<String, ColumnDef> columns = meta.getColumns(getEntity(modelElements));
    Timestamp currentTime = new Timestamp(new Date().getTime());
    List<Object> list = new ArrayList<>();
    if (columns.containsKey(IS_DELETED_COLUMN_NAME)) {
        list.add("yes");
        if (columns.containsKey(WHO_MODIFIED_COLUMN_NAME))
            list.add(UserInfoHolder.getUserName());
        if (columns.containsKey(MODIFICATION_DATE_COLUMN_NAME))
            list.add(currentTime);
        if (columns.containsKey(IP_MODIFIED_COLUMN_NAME))
            list.add(UserInfoHolder.getRemoteAddr());
    }
    return list.toArray();
}
Also used : ArrayList(java.util.ArrayList) ColumnDef(com.developmentontheedge.be5.metadata.model.ColumnDef) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Example 20 with ColumnDef

use of com.developmentontheedge.be5.metadata.model.ColumnDef in project be5 by DevelopmentOnTheEdge.

the class DpsHelper method getDynamicPropertyWithoutTags.

public DynamicProperty getDynamicPropertyWithoutTags(ColumnDef columnDef, BeModelElement modelElements) {
    DynamicProperty dp = new DynamicProperty(columnDef.getName(), meta.getColumnType(columnDef));
    if (modelElements.getClass() == Query.class) {
        dp.setDisplayName(userAwareMeta.getColumnTitle(columnDef.getEntity().getName(), modelElements.getName(), columnDef.getName()));
    } else if (modelElements.getClass() == Entity.class) {
        dp.setDisplayName(userAwareMeta.getColumnTitle(columnDef.getEntity().getName(), columnDef.getName()));
    } else if (modelElements.getClass() == JavaOperation.class || modelElements.getClass() == GroovyOperation.class) {
        dp.setDisplayName(userAwareMeta.getLocalizedOperationField(columnDef.getEntity().getName(), modelElements.getName(), columnDef.getName()));
    }
    if (columnDef.getDefaultValue() != null) {
        dp.setValue(meta.getColumnDefaultValue(columnDef));
    }
    if (columnDef.isCanBeNull() || (columnDef.getTypeString().equals(SqlColumnType.TYPE_BOOL) && columnDef.getDefaultValue() != null)) {
        dp.setCanBeNull(true);
    }
    if (SqlColumnType.TYPE_VARCHAR.equals(columnDef.getType().getTypeName()) || SqlColumnType.TYPE_CHAR.equals(columnDef.getType().getTypeName())) {
        dp.setAttribute(BeanInfoConstants.COLUMN_SIZE_ATTR, columnDef.getType().getSize());
    }
    if (SqlColumnType.TYPE_DECIMAL.equals(columnDef.getType().getTypeName())) {
        int size = columnDef.getType().getSize();
        dp.setAttribute(BeanInfoConstants.VALIDATION_RULES, Arrays.asList(getRange(size, false), step(getPrecision(columnDef.getType().getPrecision()))));
    }
    if (SqlColumnType.TYPE_CURRENCY.equals(columnDef.getType().getTypeName())) {
        dp.setAttribute(BeanInfoConstants.VALIDATION_RULES, Arrays.asList(getRange(columnDef.getType().getSize(), false), step(0.01)));
    }
    if (SqlColumnType.TYPE_INT.equals(columnDef.getType().getTypeName()) || SqlColumnType.TYPE_UINT.equals(columnDef.getType().getTypeName())) {
        boolean unsigned = SqlColumnType.TYPE_UINT.equals(columnDef.getType().getTypeName());
        dp.setAttribute(BeanInfoConstants.VALIDATION_RULES, Arrays.asList(range(unsigned ? 0 : Integer.MIN_VALUE, Integer.MAX_VALUE), step(1)));
    }
    if (SqlColumnType.TYPE_BIGINT.equals(columnDef.getType().getTypeName()) || SqlColumnType.TYPE_UBIGINT.equals(columnDef.getType().getTypeName())) {
        boolean unsigned = SqlColumnType.TYPE_UBIGINT.equals(columnDef.getType().getTypeName());
        dp.setAttribute(BeanInfoConstants.VALIDATION_RULES, Arrays.asList(range(unsigned ? 0 : Long.MIN_VALUE, Long.MAX_VALUE), step(1)));
    }
    if (columnDef.getName().endsWith(HIDDEN_COLUMN_PREFIX))
        dp.setHidden(true);
    return dp;
}
Also used : Entity(com.developmentontheedge.be5.metadata.model.Entity) DynamicProperty(com.developmentontheedge.beans.DynamicProperty) GroovyOperation(com.developmentontheedge.be5.metadata.model.GroovyOperation) JavaOperation(com.developmentontheedge.be5.metadata.model.JavaOperation)

Aggregations

ColumnDef (com.developmentontheedge.be5.metadata.model.ColumnDef)18 DynamicProperty (com.developmentontheedge.beans.DynamicProperty)7 IndexColumnDef (com.developmentontheedge.be5.metadata.model.IndexColumnDef)6 ArrayList (java.util.ArrayList)6 TableDef (com.developmentontheedge.be5.metadata.model.TableDef)5 ColumnFunction (com.developmentontheedge.be5.metadata.model.ColumnFunction)4 Entity (com.developmentontheedge.be5.metadata.model.Entity)4 Rdbms (com.developmentontheedge.be5.metadata.sql.Rdbms)4 DbmsTypeManager (com.developmentontheedge.be5.metadata.sql.type.DbmsTypeManager)4 Test (org.junit.Test)4 IndexDef (com.developmentontheedge.be5.metadata.model.IndexDef)3 HashMap (java.util.HashMap)3 ProjectElementException (com.developmentontheedge.be5.metadata.exception.ProjectElementException)2 Project (com.developmentontheedge.be5.metadata.model.Project)2 BeCaseInsensitiveCollection (com.developmentontheedge.be5.metadata.model.base.BeCaseInsensitiveCollection)2 BeModelElement (com.developmentontheedge.be5.metadata.model.base.BeModelElement)2 Timestamp (java.sql.Timestamp)2 Date (java.util.Date)2 Map (java.util.Map)2 Meta (com.developmentontheedge.be5.api.services.Meta)1