@Override public DataStructureViewModel createView(String location, String name, String hash) throws DataStructuresException { DataStructureViewModel viewModel = new DataStructureViewModel(); viewModel.setLocation(location); viewModel.setName(name); viewModel.setType(IDataStructureModel.TYPE_VIEW); viewModel.setHash(hash); viewModel.setCreatedBy(UserFacade.getName()); viewModel.setCreatedAt(new Timestamp(new java.util.Date().getTime())); try { Connection connection = null; try { connection = dataSource.getConnection(); viewPersistenceManager.insert(connection, viewModel); return viewModel; } finally { if (connection != null) { connection.close(); } } } catch (SQLException e) { throw new DataStructuresException(e); } }
if (!dataStructuresCoreService.existsView(viewModel.getLocation())) { DataStructureViewModel duplicated = dataStructuresCoreService.getViewByName(viewModel.getName()); if (duplicated != null) { throw new SynchronizationException( format("View [{0}] defined by the model at: [{1}] has already been defined by the model at: [{2}]", viewModel.getName(), viewModel.getLocation(), duplicated.getLocation())); dataStructuresCoreService.createView(viewModel.getLocation(), viewModel.getName(), viewModel.getHash()); DATA_STRUCTURE_MODELS.put(viewModel.getName(), viewModel); logger.info("Synchronized a new View [{}] from location: {}", viewModel.getName(), viewModel.getLocation()); } else { DataStructureViewModel existing = dataStructuresCoreService.getView(viewModel.getLocation()); if (!viewModel.equals(existing)) { dataStructuresCoreService.updateView(viewModel.getLocation(), viewModel.getName(), viewModel.getHash()); DATA_STRUCTURE_MODELS.put(viewModel.getName(), viewModel); logger.info("Synchronized a modified View [{}] from location: {}", viewModel.getName(), viewModel.getLocation()); VIEWS_SYNCHRONIZED.add(viewModel.getLocation()); } catch (DataStructuresException e) { throw new SynchronizationException(e);
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()); }
@Override public void updateView(String location, String name, String hash) throws DataStructuresException { try { Connection connection = null; try { connection = dataSource.getConnection(); DataStructureViewModel viewModel = getView(location); viewModel.setName(name); viewModel.setHash(hash); viewPersistenceManager.update(connection, viewModel); } finally { if (connection != null) { connection.close(); } } } catch (SQLException e) { throw new DataStructuresException(e); } }
/** * Execute the corresponding statement. * * @param connection the connection * @param viewModel the view model * @throws SQLException the SQL exception */ public static void execute(Connection connection, DataStructureViewModel viewModel) throws SQLException { logger.info("Processing Create View: " + viewModel.getName()); if (!SqlFactory.getNative(connection).exists(connection, viewModel.getName())) { String sql = SqlFactory.getNative(connection).create().view(viewModel.getName()).asSelect(viewModel.getQuery()).build(); PreparedStatement statement = connection.prepareStatement(sql); try { logger.info(sql); statement.executeUpdate(); } catch (SQLException e) { logger.error(sql); logger.error(e.getMessage(), e); } finally { if (statement != null) { statement.close(); } } } }
if (!VIEWS_SYNCHRONIZED.contains(viewModel.getLocation())) { dataStructuresCoreService.removeView(viewModel.getLocation()); executeViewDrop(connection, viewModel); logger.warn("Cleaned up View [{}] from location: {}", viewModel.getName(), viewModel.getLocation());
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); } }
/** * Creates a view model from the raw content. * * @param content * the view definition * @return the view model instance */ public static DataStructureViewModel parseView(String content) { DataStructureViewModel result = GsonHelper.GSON.fromJson(content, DataStructureViewModel.class); result.setHash(DigestUtils.md5Hex(content)); return result; }
/** * Register predelivered view. * * @param viewModelPath * the view model path * @throws IOException * Signals that an I/O exception has occurred. */ public void registerPredeliveredView(String viewModelPath) throws IOException { String json = loadResourceContent(viewModelPath); DataStructureViewModel viewModel = dataStructuresCoreService.parseView(json); viewModel.setLocation(viewModelPath); VIEWS_PREDELIVERED.put(viewModelPath, viewModel); }
result.getTables().add(table); } else if ("view".equalsIgnoreCase(type)) { DataStructureViewModel view = new DataStructureViewModel(); setViewAttributes(location, result, structure, type, view); result.getViews().add(view);
/** * Execute the corresponding statement. * * @param connection the connection * @param viewModel the view model * @throws SQLException the SQL exception */ public static void execute(Connection connection, DataStructureViewModel viewModel) throws SQLException { logger.info("Processing Drop View: " + viewModel.getName()); if (SqlFactory.getNative(connection).exists(connection, viewModel.getName())) { String sql = SqlFactory.getNative(connection).drop().view(viewModel.getName()).build(); PreparedStatement statement = connection.prepareStatement(sql); try { logger.info(sql); statement.executeUpdate(); } catch (SQLException e) { logger.error(sql); logger.error(e.getMessage(), e); } finally { if (statement != null) { statement.close(); } } } }
/** * Creates a view model from the raw content. * * @param bytes * the view definition * @return the view model instance */ public static DataStructureViewModel parseView(byte[] bytes) { DataStructureViewModel result = GsonHelper.GSON.fromJson( new InputStreamReader(new ByteArrayInputStream(bytes), StandardCharsets.UTF_8), DataStructureViewModel.class); result.setHash(DigestUtils.md5Hex(bytes)); return result; }
viewModel.setLocation(registryPath); synchronizeView(viewModel); return;