@SuppressWarnings("rawtypes") @Override public Map tableToMetaAttributes(TableIdentifier tableIdentifier) { Map map = new HashMap<>(); addMeta(map, "extends", "BaseEntity"); addMeta(map, "extra-import", "javax.persistence.AccessType", "org.sitoolkit.ad.archetype.tips.infrastructure.data.jpa.BaseEntity", "org.sitoolkit.ad.archetype.tips.infrastructure.data.jpa.BaseEntityListener"); return map; }
@Override public String tableToClassName(TableIdentifier tableIdentifier) { String classname = super.tableToClassName(tableIdentifier); if ( classname.contains("Tb") ) classname = classname.replace("Tb", ""); LOGGER.debug("Table: {} is Class: {}", tableIdentifier.getName(), classname); return classname; }
/** * Manages the "database field to property name" transformation: * <ul> * <li>If a database field has a filled remark property, its content will be used</li> * <li>Otherwise, the database field name will be used</li> * </ul> */ @Override public String columnToPropertyName(TableIdentifier table, String column) { String tableName = format(table.getName()); String columnName = format(column); if (FIELDS_COMMENT.containsKey(tableName)) { Map<String, String> map = FIELDS_COMMENT.get(tableName); if (map.containsKey(columnName)) { return super.columnToPropertyName(table, map.get(columnName)); } } return super.columnToPropertyName(table, column); }
/** * Manages the "database table to class name" transformation: * <ul> * <li>If a database table has a filled remark property, its content will be used</li> * <li>Otherwise, the database table name will be used</li> * </ul> */ @Override public String tableToClassName(TableIdentifier tableIdentifier) { String tableName = format(tableIdentifier.getName()); if (TABLES_COMMENT.containsKey(tableName)) { return super.tableToClassName(new TableIdentifier(TABLES_COMMENT.get(tableName))); } return super.tableToClassName(tableIdentifier); }
@Override public boolean excludeColumn(TableIdentifier identifier, String columnName) { return excludedColumns.contains(columnName.toLowerCase()); }
@Override public void translate(TranslatorContext tc) { DatabaseCollector collector = tc.getDatabaseCollector(); Map<String, TableIdentifier> mapped = new HashMap<>(); RDBMS rdbms = setupRDBMS(tc); rdbms.setDialect((String) tc.getMap().get("rdbmsDialect")); for (Iterator<Table> i = collector.iterateTables(); i.hasNext();) { Table table = i.next(); if(table.getColumnSpan()==0) { LOGGER.warn("Table without column found and it will be ignored. Its name is '" + table + "'."); continue; } Map<String, TableIdentifier> fks = new HashMap<>(); TableIdentifier tableIdentifier = TableIdentifier.create(table); String id = tableIdentifier.toString(); if(!mapped.containsKey(id)) { mapped.put(id, tableIdentifier); } else { throw new IllegalStateException("Table mapped twice"); } Join join = new Join(); join.setProjectionMappings(new ArrayList<ProjectionMapping>()); join.setTables(new ArrayList<com.redhat.lightblue.metadata.rdbms.model.Table>()); for (Iterator<ForeignKey> j = table.getForeignKeyIterator(); j.hasNext();) { ForeignKey fk = j.next(); Table referencedTable = fk.getReferencedTable(); TableIdentifier ti = TableIdentifier.create(referencedTable); for (Iterator<Column> z = fk.getColumns().iterator(); z.hasNext();) { Column c = z.next(); fks.put(c.getName(),ti); } } Boolean mapfk = (Boolean) tc.getMap().get("mapfk"); for (Iterator<Column> j = table.getColumnIterator(); j.hasNext();) { Column column = j.next(); if(fks.get(column.getName())== null || mapfk ){ ColumnToField field = setupColumnToField(table, column); rdbms.getSQLMapping().getColumnToFieldMap().add(field); ProjectionMapping projectionMapping = setupProjectionMapping(column); join.getProjectionMappings().add(projectionMapping); } } com.redhat.lightblue.metadata.rdbms.model.Table rdbmTable = new com.redhat.lightblue.metadata.rdbms.model.Table(); rdbmTable.setName(table.getName()); join.getTables().add(rdbmTable); rdbms.getSQLMapping().getJoins().add(join); } }
@Override public void translate(TranslatorContext tc) { DatabaseCollector collector = tc.getDatabaseCollector(); Map<String, TableIdentifier> mapped = new HashMap<>(); RDBMS rdbms = setupRDBMS(tc); Map<String, Join> tableToJoin = new HashMap<>(); Map<TableIdentifier,PrimaryKey> pks = new HashMap<>(); rdbms.setDialect((String) tc.getMap().get("rdbmsDialect")); for (Iterator<Table> i = collector.iterateTables(); i.hasNext();) { Table table = i.next(); if(table.getColumnSpan()==0) { LOGGER.warn("Table without column found and it will be ignored. Its name is '" + table + "'."); continue; } /* // TODO analyze this case if(revengStrategy.isManyToManyTable(table)) {} */ Map<String, ForeignKey> fks = new HashMap<>(); TableIdentifier tableIdentifier = TableIdentifier.create(table); String id = tableIdentifier.toString(); pks.put(tableIdentifier,table.getPrimaryKey()); if(!mapped.containsKey(id)) { mapped.put(id, tableIdentifier); } else { throw new IllegalStateException("Table mapped twice"); } Join join = null; if(tableToJoin.get(id) == null){ join = new Join(); join.setProjectionMappings(new ArrayList<ProjectionMapping>()); join.setTables(new ArrayList<com.redhat.lightblue.metadata.rdbms.model.Table>()); join.setJoinTablesStatement(""); rdbms.getSQLMapping().getJoins().add(join); tableToJoin.put(id,join); }else{ join = tableToJoin.get(id); } for (Iterator<ForeignKey> j = table.getForeignKeyIterator(); j.hasNext();) { ForeignKey fk = j.next(); Table referencedTable = fk.getReferencedTable(); TableIdentifier ti = TableIdentifier.create(referencedTable); tableToJoin.put(ti.toString(),join); for (Iterator<Column> z = fk.getColumns().iterator(); z.hasNext();) { Column c = z.next(); fks.put(c.getName(),fk); String joinTable = join.getJoinTablesStatement(); if(joinTable.length() != 0){ joinTable = joinTable + " AND "; } join.setJoinTablesStatement(joinTable + table.getName() + "." + c.getName() + "=" + referencedTable.getName() + "." + c.getName()); } } for (Iterator<Column> j = table.getColumnIterator(); j.hasNext();) { Column column = j.next(); if(fks.get(column.getName())== null ){ ColumnToField field = setupColumnToField(table, column); rdbms.getSQLMapping().getColumnToFieldMap().add(field); ProjectionMapping projectionMapping = setupProjectionMapping(column); join.getProjectionMappings().add(projectionMapping); } } com.redhat.lightblue.metadata.rdbms.model.Table rdbmTable = new com.redhat.lightblue.metadata.rdbms.model.Table(); rdbmTable.setName(table.getName()); join.getTables().add(rdbmTable); } }