Search in sources :

Example 41 with Entity

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

the class EntityLocalizationsTest method testLocalizations.

@Test
public void testLocalizations() {
    Project proj = new Project("test");
    proj.getApplication().getLocalizations().addLocalization("de", "entity", Arrays.asList("@AllQueries", "topic"), "Hello", "Guten Tag");
    Entity entity = new Entity("entity", proj.getApplication(), EntityType.TABLE);
    DataElementUtils.save(entity);
    DataElementUtils.save(new Query("testQuery", entity));
    DataElementUtils.save(new Query("testQuery2", entity));
    EntityLocalizations el = proj.getApplication().getLocalizations().get("de").get("entity");
    Set<LocalizationRow> expected = StreamEx.of("testQuery", "testQuery2", "topic").map(topic -> new EntityLocalizations.LocalizationRow(topic, "Hello", "Guten Tag")).toSet();
    assertEquals(expected, el.getRawRows());
    assertEquals(expected, el.getRows());
    el.remove("Hello", Collections.singleton("topic"));
    expected = StreamEx.of("testQuery", "testQuery2").map(topic -> new EntityLocalizations.LocalizationRow(topic, "Hello", "Guten Tag")).toSet();
    assertEquals(expected, el.getRawRows());
    assertEquals(expected, el.getRows());
}
Also used : Arrays(java.util.Arrays) LocalizationRow(com.developmentontheedge.be5.metadata.model.EntityLocalizations.LocalizationRow) StreamEx(one.util.streamex.StreamEx) Set(java.util.Set) Test(org.junit.Test) Assert(org.junit.Assert) Collections(java.util.Collections) LocalizationRow(com.developmentontheedge.be5.metadata.model.EntityLocalizations.LocalizationRow) LocalizationRow(com.developmentontheedge.be5.metadata.model.EntityLocalizations.LocalizationRow) Test(org.junit.Test)

Example 42 with Entity

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

the class AppSync method getDdlStatements.

// /////////////////////////////////////////////////////////////////////////
// Synchronization
// 
protected String getDdlStatements(boolean dangerousOnly) throws ExtendedSqlException {
    Map<String, DdlElement> oldSchemes = new HashMap<>();
    Map<String, DdlElement> newSchemes = new HashMap<>();
    for (Module module : be5Project.getModulesAndApplication()) {
        for (Entity entity : module.getEntities()) {
            DdlElement scheme = entity.isAvailable() ? entity.getScheme() : null;
            if (scheme != null) {
                String normalizedName = entity.getName().toLowerCase();
                newSchemes.put(normalizedName, scheme);
            }
        }
    }
    for (Entity entity : entities) {
        DdlElement scheme = entity.isAvailable() ? entity.getScheme() : null;
        if (scheme != null) {
            String normalizedName = entity.getName().toLowerCase();
            oldSchemes.put(normalizedName, scheme);
        }
    }
    StringBuilder sb = new StringBuilder();
    for (Map.Entry<String, DdlElement> entity : newSchemes.entrySet()) {
        DdlElement oldScheme = oldSchemes.get(entity.getKey());
        DdlElement newScheme = entity.getValue();
        if (newScheme.withoutDbScheme()) {
            if (!dangerousOnly) {
            // PENDING - list of other type
            // warnings.addAll(newScheme.getWarnings());
            }
            if (oldScheme == null) {
                if (!dangerousOnly) {
                    sb.append(newScheme.getCreateDdl());
                }
                continue;
            }
            if (newScheme.equals(oldScheme) || newScheme.getDiffDdl(oldScheme, null).isEmpty())
                continue;
            // PENDING
            if (oldScheme instanceof TableDef && newScheme instanceof TableDef)
                fixPrimaryKey((TableDef) oldScheme, (TableDef) newScheme);
            sb.append(dangerousOnly ? newScheme.getDangerousDiffStatements(oldScheme, sqlExecutor) : newScheme.getDiffDdl(oldScheme, sqlExecutor));
        } else {
            System.out.println("Skip table with schema: " + newScheme.getEntityName());
        }
    }
    return sb.toString();
}
Also used : Entity(com.developmentontheedge.be5.metadata.model.Entity) HashMap(java.util.HashMap) DdlElement(com.developmentontheedge.be5.metadata.model.DdlElement) Module(com.developmentontheedge.be5.metadata.model.Module) HashMap(java.util.HashMap) Map(java.util.Map) TableDef(com.developmentontheedge.be5.metadata.model.TableDef)

Example 43 with Entity

use of com.developmentontheedge.be5.metadata.model.Entity 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 44 with Entity

use of com.developmentontheedge.be5.metadata.model.Entity 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 45 with Entity

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

the class EntityModels method createService.

private void createService(String generatedSourcesPath, String packageName, String serviceClassName, Configuration cfg) throws IOException {
    Template serviceTpl = cfg.getTemplate("service.ftl");
    Meta meta = injector.getMeta();
    List<Entity> entities = meta.getOrderedEntities("ru");
    Map<String, Object> input = new HashMap<>();
    input.put("serviceClassName", serviceClassName);
    input.put("packageName", packageName);
    List<String> entityNames = new ArrayList<>();
    for (Entity entity : entities) {
        if (entity.getName().startsWith("_"))
            continue;
        entityNames.add(entity.getName());
    }
    input.put("entityNames", entityNames);
    Utils.createFile(generatedSourcesPath, packageName, serviceClassName + ".java", serviceTpl, input);
}
Also used : Meta(com.developmentontheedge.be5.api.services.Meta) Entity(com.developmentontheedge.be5.metadata.model.Entity) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Template(freemarker.template.Template)

Aggregations

Entity (com.developmentontheedge.be5.metadata.model.Entity)36 Query (com.developmentontheedge.be5.metadata.model.Query)19 Module (com.developmentontheedge.be5.metadata.model.Module)16 Project (com.developmentontheedge.be5.metadata.model.Project)16 Test (org.junit.Test)11 Path (java.nio.file.Path)9 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)8 ColumnDef (com.developmentontheedge.be5.metadata.model.ColumnDef)7 Operation (com.developmentontheedge.be5.metadata.model.Operation)7 TableDef (com.developmentontheedge.be5.metadata.model.TableDef)7 Meta (com.developmentontheedge.be5.api.services.Meta)4 ReadException (com.developmentontheedge.be5.metadata.exception.ReadException)4 BeModelElement (com.developmentontheedge.be5.metadata.model.base.BeModelElement)4 Map (java.util.Map)4 ProjectElementException (com.developmentontheedge.be5.metadata.exception.ProjectElementException)3 DdlElement (com.developmentontheedge.be5.metadata.model.DdlElement)3 Localizations (com.developmentontheedge.be5.metadata.model.Localizations)3 LinkedHashMap (java.util.LinkedHashMap)3 Be5Exception (com.developmentontheedge.be5.api.exceptions.Be5Exception)2