@Override public DataStructureSchemaModel createSchema(String location, String name, String hash) throws DataStructuresException { DataStructureSchemaModel schemaModel = new DataStructureSchemaModel(); schemaModel.setLocation(location); schemaModel.setName(name); schemaModel.setType(IDataStructureModel.TYPE_SCHEMA); schemaModel.setHash(hash); schemaModel.setCreatedBy(UserFacade.getName()); schemaModel.setCreatedAt(new Timestamp(new java.util.Date().getTime())); try { Connection connection = null; try { connection = dataSource.getConnection(); schemaPersistenceManager.insert(connection, schemaModel); return schemaModel; } finally { if (connection != null) { connection.close(); } } } catch (SQLException e) { throw new DataStructuresException(e); } }
/** * Synchronize schema. * * @param schemaModel * the schema model * @throws SynchronizationException * the synchronization exception */ private void synchronizeSchema(DataStructureSchemaModel schemaModel) throws SynchronizationException { try { if (!dataStructuresCoreService.existsSchema(schemaModel.getLocation())) { dataStructuresCoreService.createSchema(schemaModel.getLocation(), schemaModel.getName(), schemaModel.getHash()); DATA_STRUCTURE_SCHEMA_MODELS.put(schemaModel.getName(), schemaModel); addDataStructureModelsFromSchema(schemaModel); logger.info("Synchronized a new Schema file [{}] from location: {}", schemaModel.getName(), schemaModel.getLocation()); } else { DataStructureSchemaModel existing = dataStructuresCoreService.getSchema(schemaModel.getLocation()); if (!schemaModel.equals(existing)) { dataStructuresCoreService.updateSchema(schemaModel.getLocation(), schemaModel.getName(), schemaModel.getHash()); DATA_STRUCTURE_SCHEMA_MODELS.put(schemaModel.getName(), schemaModel); addDataStructureModelsFromSchema(schemaModel); logger.info("Synchronized a modified Schema file [{}] from location: {}", schemaModel.getName(), schemaModel.getLocation()); } } SCHEMA_SYNCHRONIZED.add(schemaModel.getLocation()); } catch (DataStructuresException e) { throw new SynchronizationException(e); } }
private void addDataStructureModelsFromSchema(DataStructureSchemaModel schemaModel) { for (DataStructureTableModel tableModel : schemaModel.getTables()) { DATA_STRUCTURE_MODELS.put(tableModel.getName(), tableModel); } for (DataStructureViewModel viewModel : schemaModel.getViews()) { DATA_STRUCTURE_MODELS.put(viewModel.getName(), viewModel); } }
DataStructureSchemaModel result = new DataStructureSchemaModel(); setContentModelAttributes(location, content, result, IDataStructureModel.TYPE_SCHEMA); DataStructureTableModel table = new DataStructureTableModel(); setTableAttributes(location, result, structure, type, table); result.getTables().add(table); } else if ("view".equalsIgnoreCase(type)) { DataStructureViewModel view = new DataStructureViewModel(); setViewAttributes(location, result, structure, type, view); result.getViews().add(view); } else if ("foreignKey".equalsIgnoreCase(type)) { foreignKey.setReferencedColumns(structure.get("referencedColumns").getAsString().split(",")); String tableName = structure.get("table").getAsString(); for (DataStructureTableModel table : result.getTables()) { if (table.getName().equals(tableName)) {
private static void setViewAttributes(String location, DataStructureSchemaModel result, JsonObject structure, String type, DataStructureViewModel view) { view.setLocation(location); view.setName(structure.get("name").getAsString()); view.setType(type); view.setQuery(structure.get("columns").getAsJsonArray().get(0).getAsJsonObject().get("query").getAsString()); view.setCreatedAt(result.getCreatedAt()); view.setCreatedBy(result.getCreatedBy()); view.setHash(result.getHash()); }
if (!SCHEMA_SYNCHRONIZED.contains(schemaModel.getLocation())) { dataStructuresCoreService.removeSchema(schemaModel.getLocation()); logger.warn("Cleaned up Schema Data file [{}] from location: {}", schemaModel.getName(), schemaModel.getLocation());
private static void setTableAttributes(String location, DataStructureSchemaModel result, JsonObject structure, String type, DataStructureTableModel table) { table.setLocation(location); table.setName(structure.get("name").getAsString()); table.setType(type); table.setCreatedAt(result.getCreatedAt()); table.setCreatedBy(result.getCreatedBy()); table.setHash(result.getHash()); JsonElement columnElement = structure.get("columns"); if (columnElement.isJsonObject()) { JsonObject column = columnElement.getAsJsonObject(); DataStructureTableColumnModel columnModel = new DataStructureTableColumnModel(); setColumnAttributes(column, columnModel); table.getColumns().add(columnModel); } else if (columnElement.isJsonArray()) { JsonArray columns = columnElement.getAsJsonArray(); for (int j=0; j<columns.size(); j++) { JsonObject column = columns.get(j).getAsJsonObject(); DataStructureTableColumnModel columnModel = new DataStructureTableColumnModel(); setColumnAttributes(column, columnModel); table.getColumns().add(columnModel); } } else { throw new IllegalArgumentException(format("Error in parsing columns of table [{0}] in schema [{1}]", table.getName(), location)); } }