smazano connection pri přesunu node na jinou mapu
parent
123776549f
commit
a0d147ee90
|
|
@ -29,13 +29,17 @@ public final class Database {
|
|||
// Veřejný konstruktor pro případy, kdy je v kódu voláno new Database()
|
||||
public Database() {}
|
||||
|
||||
// -------- inicializace --------
|
||||
///
|
||||
/// -------- inicializace --------
|
||||
///
|
||||
|
||||
public static void connect(String host, String port, String database, String user, String password) {
|
||||
ConnectionManager.init(host, port, database, user, password);
|
||||
}
|
||||
|
||||
// -------- User --------
|
||||
///
|
||||
/// -------- User --------
|
||||
///
|
||||
|
||||
public static boolean verifyUser(String username, String password) {
|
||||
return userDao.verifyUser(username, password);
|
||||
|
|
@ -60,8 +64,9 @@ public final class Database {
|
|||
public static void deleteUser(int userId) {
|
||||
userDao.deleteUser(userId);
|
||||
}
|
||||
|
||||
// -------- ObjectType --------
|
||||
///
|
||||
/// -------- ObjectType --------
|
||||
///
|
||||
|
||||
public static ArrayList<ObjectType> getObjectType() {
|
||||
return new ArrayList<>(objectTypeDao.getObjectTypes());
|
||||
|
|
@ -75,7 +80,9 @@ public final class Database {
|
|||
objectTypeDao.deleteDeviceType(id);
|
||||
}
|
||||
|
||||
// -------- Site --------
|
||||
///
|
||||
/// -------- Site --------
|
||||
///
|
||||
|
||||
public static ArrayList<Site> getSiteList() {
|
||||
return new ArrayList<>(siteDao.getSiteList());
|
||||
|
|
@ -93,7 +100,9 @@ public final class Database {
|
|||
siteDao.setSiteLock(state, map);
|
||||
}
|
||||
|
||||
// -------- Node --------
|
||||
///
|
||||
/// -------- Node --------
|
||||
///
|
||||
|
||||
public static ArrayList<Node> getNodeList(int siteId) {
|
||||
return new ArrayList<>(nodeDao.getNodesForSite(siteId));
|
||||
|
|
@ -115,7 +124,9 @@ public final class Database {
|
|||
nodeDao.updateNode(node);
|
||||
}
|
||||
|
||||
// -------- NodeConnection (spojení) --------
|
||||
///
|
||||
/// -------- NodeConnection (spojení) --------
|
||||
///
|
||||
|
||||
public static List<NodeConnection> getNodeConnectionList(int nodeId) {
|
||||
return connectionDao.getConnectionsForNode(nodeId);
|
||||
|
|
@ -136,9 +147,15 @@ public final class Database {
|
|||
public static void editNodeconnection(NodeConnection connection) {
|
||||
connectionDao.editNodeconnection(connection);
|
||||
}
|
||||
|
||||
public static void deteleConnectionAfterNodeMove(int id) {
|
||||
connectionDao.deteleConnectionAfterNodeMove(id);
|
||||
}
|
||||
|
||||
// -------- Event --------
|
||||
|
||||
///
|
||||
/// -------- Event --------
|
||||
///
|
||||
|
||||
public static long insertEvent(int nodeId, int status, long time) {
|
||||
return eventDao.insertEvent(nodeId, status, time);
|
||||
}
|
||||
|
|
@ -151,7 +168,9 @@ public final class Database {
|
|||
return eventDao.getAllEvents();
|
||||
}
|
||||
|
||||
// -------- SNMP --------
|
||||
///
|
||||
/// -------- SNMP --------
|
||||
///
|
||||
|
||||
public static List<SnmpProfile> getSnmpProfile() {
|
||||
return snmpDao.getAllProfile();
|
||||
|
|
@ -177,6 +196,22 @@ public final class Database {
|
|||
return snmpDao.getSnmpValueMapList();
|
||||
}
|
||||
|
||||
public static int addSnmpTemplate(SnmpTemplate template) {
|
||||
return snmpDao.addSnmpTemplate(template);
|
||||
}
|
||||
|
||||
public static void editSnmpTemplate(SnmpTemplate template) {
|
||||
snmpDao.editSnmpTemplate(template);
|
||||
}
|
||||
|
||||
public static void deleteSnmpTemplate(int id) {
|
||||
snmpDao.deleteSnmpTemplate(id);
|
||||
}
|
||||
|
||||
///
|
||||
/// -------- probe --------
|
||||
///
|
||||
|
||||
public static int addNodeProbe(NodeProbe probe) {
|
||||
return probeDao.addNodeProbe(probe);
|
||||
}
|
||||
|
|
@ -189,19 +224,6 @@ public final class Database {
|
|||
probeDao.editNodeProbe(probe);
|
||||
}
|
||||
|
||||
public static int addSnmpTemplate(SnmpTemplate template) {
|
||||
return snmpDao.addSnmpTemplate(template);
|
||||
}
|
||||
|
||||
public static void editSnmpTemplate(SnmpTemplate template) {
|
||||
snmpDao.editSnmpTemplate(template);
|
||||
}
|
||||
|
||||
public static void deleteSnmpTemplate(int id) {
|
||||
snmpDao.deleteSnmpTemplate(id);
|
||||
}
|
||||
|
||||
|
||||
// -------- zachování zastaralých metod pro kompatibilitu --------
|
||||
|
||||
@Deprecated
|
||||
|
|
|
|||
|
|
@ -12,14 +12,15 @@ public abstract class AbstractDao {
|
|||
}
|
||||
|
||||
// -------- veřejné helpery pro všechny potomky i fasádu --------
|
||||
|
||||
public int insertAndGetId(String sql, Object... params) {
|
||||
try (PreparedStatement pstmt = getConnection().prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
setParameters(pstmt, params);
|
||||
//System.err.println(pstmt.toString());
|
||||
pstmt.executeUpdate();
|
||||
try (ResultSet rs = pstmt.getGeneratedKeys()) {
|
||||
if (rs.next()) return rs.getInt(1);
|
||||
if (rs.next()) {
|
||||
return rs.getInt(1);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LogFile.printErr("Insert failed: " + e.getMessage() + " SQL: " + sql);
|
||||
|
|
@ -45,13 +46,26 @@ public abstract class AbstractDao {
|
|||
}
|
||||
}
|
||||
|
||||
// -------- chráněné helpery pro query (zůstávají protected) --------
|
||||
public boolean deleteConnectionsByNodeId(String table, int nodeId) {
|
||||
String sql = "DELETE FROM " + table + " WHERE nodeFrom = ? OR nodeTo = ?";
|
||||
try (PreparedStatement pstmt = getConnection().prepareStatement(sql)) {
|
||||
pstmt.setInt(1, nodeId);
|
||||
pstmt.setInt(2, nodeId);
|
||||
return pstmt.executeUpdate() > 0;
|
||||
} catch (SQLException e) {
|
||||
LogFile.printErr("Delete connections failed: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// -------- chráněné helpery pro query (zůstávají protected) --------
|
||||
protected <T> T querySingle(String sql, ResultSetMapper<T> mapper, Object... params) {
|
||||
try (PreparedStatement pstmt = getConnection().prepareStatement(sql)) {
|
||||
setParameters(pstmt, params);
|
||||
try (ResultSet rs = pstmt.executeQuery()) {
|
||||
if (rs.next()) return mapper.map(rs);
|
||||
if (rs.next()) {
|
||||
return mapper.map(rs);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LogFile.printErr("Query failed: " + e.getMessage());
|
||||
|
|
@ -64,7 +78,9 @@ public abstract class AbstractDao {
|
|||
try (PreparedStatement pstmt = getConnection().prepareStatement(sql)) {
|
||||
setParameters(pstmt, params);
|
||||
try (ResultSet rs = pstmt.executeQuery()) {
|
||||
while (rs.next()) list.add(mapper.map(rs));
|
||||
while (rs.next()) {
|
||||
list.add(mapper.map(rs));
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LogFile.printErr("Query failed: " + e.getMessage());
|
||||
|
|
@ -80,6 +96,7 @@ public abstract class AbstractDao {
|
|||
|
||||
@FunctionalInterface
|
||||
protected interface ResultSetMapper<T> {
|
||||
|
||||
T map(ResultSet rs) throws SQLException;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -154,4 +154,12 @@ public class ConnectionDao extends AbstractDao {
|
|||
LogFile.printErr("Chyba při aktualizaci connection: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public void deteleConnectionAfterNodeMove(int id) {
|
||||
if (deleteConnectionsByNodeId(TABLE, id)) {
|
||||
LogFile.printInfo("Node connection ID " + id + " removed from database");
|
||||
} else {
|
||||
LogFile.printErr("Node connection ID " + id + " not removed from database");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,6 +47,9 @@ public class RegisterNodeMoveToSite implements Register {
|
|||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// smazat connection
|
||||
Database.deteleConnectionAfterNodeMove(node.getId());
|
||||
|
||||
// upravime v databazi
|
||||
Database.editNode(node);
|
||||
|
|
|
|||
Loading…
Reference in New Issue