if (id != null && id.longValue() == DomainVO.ROOT_DOMAIN) {
s_logger.error("Can not remove domain " + id + " as it is ROOT domain");
return false;
}
DomainVO domain = findById(id);
if(domain == null) {
s_logger.error("Unable to remove domain as domain " + id + " no longer exists");
return false;
}
if(domain.getParent() == null) {
s_logger.error("Invalid domain " + id + ", orphan?");
return false;
}
String sql = "SELECT * from account where domain_id = " + id + " and removed is null";
String sql1 = "SELECT * from domain where parent = " + id + " and removed is null";
boolean success = false;
Transaction txn = Transaction.currentTxn();
try {
txn.start();
DomainVO parentDomain = super.lockRow(domain.getParent(), true);
if(parentDomain == null) {
s_logger.error("Unable to load parent domain: " + domain.getParent());
return false;
}
PreparedStatement stmt = txn.prepareAutoCloseStatement(sql);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
return false;
}
stmt = txn.prepareAutoCloseStatement(sql1);
rs = stmt.executeQuery();
if (rs.next()) {
return false;
}
parentDomain.setChildCount(parentDomain.getChildCount() - 1);
update(parentDomain.getId(), parentDomain);
success = super.remove(id);
txn.commit();
} catch (SQLException ex) {
success = false;
s_logger.error("error removing domain: " + id, ex);