@Override public DataStructureDataReplaceModel createReplace(String location, String name, String hash) throws DataStructuresException { DataStructureDataReplaceModel dataModel = new DataStructureDataReplaceModel(); dataModel.setLocation(location); dataModel.setName(name); dataModel.setType(IDataStructureModel.TYPE_REPLACE); dataModel.setHash(hash); dataModel.setCreatedBy(UserFacade.getName()); dataModel.setCreatedAt(new Timestamp(new java.util.Date().getTime())); try { Connection connection = null; try { connection = dataSource.getConnection(); replacePersistenceManager.insert(connection, dataModel); return dataModel; } finally { if (connection != null) { connection.close(); } } } catch (SQLException e) { throw new DataStructuresException(e); } }
/** * Synchronize replace. * * @param dataModel * the data model * @throws SynchronizationException * the synchronization exception */ private void synchronizeReplace(DataStructureDataReplaceModel dataModel) throws SynchronizationException { try { if (!dataStructuresCoreService.existsReplace(dataModel.getLocation())) { dataStructuresCoreService.createReplace(dataModel.getLocation(), dataModel.getName(), dataModel.getHash()); DATA_STRUCTURE_REPLACE_MODELS.put(dataModel.getName(), dataModel); logger.info("Synchronized a new Replace Data file [{}] from location: {}", dataModel.getName(), dataModel.getLocation()); } else { DataStructureDataReplaceModel existing = dataStructuresCoreService.getReplace(dataModel.getLocation()); if (!dataModel.equals(existing)) { dataStructuresCoreService.updateReplace(dataModel.getLocation(), dataModel.getName(), dataModel.getHash()); DATA_STRUCTURE_REPLACE_MODELS.put(dataModel.getName(), dataModel); logger.info("Synchronized a modified Replace Data file [{}] from location: {}", dataModel.getName(), dataModel.getLocation()); } } REPLACE_SYNCHRONIZED.add(dataModel.getLocation()); } catch (DataStructuresException e) { throw new SynchronizationException(e); } }
/** * Process the data rows in the 'replace' mode * * @param model * the model * @throws Exception * in case of database error */ public void executeReplaceUpdate(DataStructureDataReplaceModel model) throws Exception { logger.info("Processing rows in mode 'replace': " + model.getLocation()); String tableName = model.getName(); deleteAllDataFromTable(tableName); byte[] content = model.getContent().getBytes(); if (content.length != 0) { TableImporter tableDataInserter = new TableImporter(dataSource, content, tableName); tableDataInserter.insert(); moveSequence(tableName); // move the sequence just in case } }
/** * Creates a data model from the raw content. * * @param content * the data definition * @return the data model instance */ public static DataStructureDataReplaceModel parseReplace(String location, String content) { DataStructureDataReplaceModel result = new DataStructureDataReplaceModel(); setContentModelAttributes(location, content, result, IDataStructureModel.TYPE_REPLACE); return result; }
@Override public void updateReplace(String location, String name, String hash) throws DataStructuresException { try { Connection connection = null; try { connection = dataSource.getConnection(); DataStructureDataReplaceModel dataModel = getReplace(location); dataModel.setName(name); dataModel.setHash(hash); replacePersistenceManager.update(connection, dataModel); } finally { if (connection != null) { connection.close(); } } } catch (SQLException e) { throw new DataStructuresException(e); } }
if (!REPLACE_SYNCHRONIZED.contains(dataModel.getLocation())) { dataStructuresCoreService.removeReplace(dataModel.getLocation()); logger.warn("Cleaned up Replace Data file [{}] from location: {}", dataModel.getName(), dataModel.getLocation());
dataModel.setLocation(registryPath); synchronizeReplace(dataModel); return;