String hbaseRestServerPropertyValue = hbaseRestServerProperty
.getValue();
logger.debug("Updating connectionId: " + connection.getId());
ServletContext servletContext = ServletActionContext
.getServletContext();
HBaseConnectionPool pool = (HBaseConnectionPool) servletContext
.getAttribute(CruxConstants.HBASE_POOL);
Connection connectionToTest = new Connection();
ConnectionProperty property = new ConnectionProperty();
property.setProperty(CruxConstants.HBASE_ZOOKEEPER_PROPERTY);
property.setValue(hbaseRestServerPropertyValue);
connectionToTest.addProperty(property);
try {
HBaseFacade hbaseFacade = getHBaseFacade();
boolean isValidConnection = hbaseFacade
.isValidConnection(connectionToTest);
logger.debug("isValidConnection : " + isValidConnection);
if (isValidConnection) {
connection = connectionDAO.findById(connection.getId());
Connection connectionToUpdate = connection;
connection.setName(connectionName);
hbaseRestServerProperty = connection.getProperties().get(
CruxConstants.HBASE_ZOOKEEPER_PROPERTY);
hbaseRestServerProperty.setValue(hbaseRestServerPropertyValue);
pool.update(connectionToUpdate, connection);
// Now checking whether this connection holds all the associated
// childrens.
String[] tables = hbaseFacade.getTableList(connection);
ArrayList<String> tableList = new ArrayList<String>();
for (int i = 0; i < tables.length; i++) {
tableList.add(tables[i]);
}
long count = 0l;
for (Mapping mapping : mappingDAO.findAll()) {
long id = connection.getId();
if (id == mapping.getConnection().getId()) {
String tableName = mapping.getTableName();
if (!tableList.contains(tableName)) {
count++;
} else {
ArrayList<String> columnFamilyList = new ArrayList<String>();
ArrayList<HColumnDescriptor> columnList = new ArrayList<HColumnDescriptor>(
hbaseFacade.getColumnFamilies(connection,
tableName));
for (HColumnDescriptor hcolumnDescriptor : columnList) {
columnFamilyList.add(hcolumnDescriptor
.getNameAsString());
}
Iterator<ColumnAlias> iterator = mapping
.getColumnAlias().values().iterator();
while (iterator.hasNext()) {
ColumnAlias columnAlias = iterator.next();
String columnFamily = columnAlias
.getColumnFamily();
if (!columnFamilyList.contains(columnFamily)) {
count++;
}
}
}
}
}
if (count == 0l) {
logger.debug("connection is saving.");
connectionDAO.save(connection);
} else {
error.setMessage("Connection can't be updated because it does not contain all "
+ "the tables and column family of the assosiated mapping.");
try {
connectionDAO.transaction.rollback();
pool.update(connection, connectionToUpdate);
} catch (Exception e1) {
e1.printStackTrace();
error.setMessage(e1.getMessage());
}
}