used try with resources

pull/113/head
prathabanKavin 2 years ago
parent 796963a14d
commit 839cc16399

@ -49,14 +49,12 @@ public class ConfigOperationMSSQLDAOImpl extends GenericOperationDAOImpl {
@Override
public int addOperation(Operation operation) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
operation.setCreatedTimeStamp(new Timestamp(new Date().getTime()).toString());
Connection connection = OperationManagementDAOFactory.getConnection();
String sql = "INSERT INTO DM_OPERATION(TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE, " +
"INITIATED_BY, OPERATION_DETAILS) VALUES (?, ?, ?, ?, ?, ?)";
stmt = connection.prepareStatement(sql, new String[]{"id"});
try (PreparedStatement stmt = connection.prepareStatement(sql, new String[]{"id"})) {
stmt.setString(1, operation.getType().toString());
stmt.setLong(2, DeviceManagementDAOUtil.getCurrentUTCTime());
stmt.setLong(3, 0);
@ -64,17 +62,18 @@ public class ConfigOperationMSSQLDAOImpl extends GenericOperationDAOImpl {
stmt.setString(5, operation.getInitiatedBy());
stmt.setObject(6, operation);
stmt.executeUpdate();
rs = stmt.getGeneratedKeys();
try (ResultSet rs = stmt.getGeneratedKeys()) {
int id = -1;
if (rs.next()) {
id = rs.getInt(1);
}
return id;
}
}
} catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while adding command operation", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
String msg = "Error occurred while adding command operation" + e;
log.error(msg);
throw new OperationManagementDAOException(msg);
}
}
@ -122,19 +121,15 @@ public class ConfigOperationMSSQLDAOImpl extends GenericOperationDAOImpl {
@Override
public Operation getOperation(int operationId) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
ConfigOperation configOperation = null;
ByteArrayInputStream bais;
ObjectInputStream ois;
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT ID, ENABLED, OPERATION_DETAILS FROM DM_OPERATION WHERE ID = ? AND TYPE='CONFIG'";
stmt = conn.prepareStatement(sql);
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, operationId);
rs = stmt.executeQuery();
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
byte[] operationDetails = rs.getBytes("OPERATION_DETAILS");
bais = new ByteArrayInputStream(operationDetails);
@ -143,18 +138,24 @@ public class ConfigOperationMSSQLDAOImpl extends GenericOperationDAOImpl {
configOperation.setId(rs.getInt("ID"));
configOperation.setEnabled(rs.getBoolean("ENABLED"));
}
}
}
} catch (IOException e) {
throw new OperationManagementDAOException("IO Error occurred while de serialize the policy operation " +
"object", e);
String msg = "IO Error occurred while de serialize the policy operation " +
"object" + e;
log.error(msg);
throw new OperationManagementDAOException(msg);
} catch (ClassNotFoundException e) {
throw new OperationManagementDAOException("Class not found error occurred while de serialize the policy " +
"operation object", e);
String msg = "Class not found error occurred while de serialize the policy " +
"operation object" + e;
log.error(msg);
throw new OperationManagementDAOException(msg);
} catch (SQLException e) {
throw new OperationManagementDAOException("SQL Error occurred while retrieving the policy operation " +
String msg = "SQL Error occurred while retrieving the policy operation " +
"object available for the id '"
+ operationId, e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
+ operationId;
log.error(msg, e);
throw new OperationManagementDAOException(msg, e);
}
return configOperation;
}
@ -162,11 +163,8 @@ public class ConfigOperationMSSQLDAOImpl extends GenericOperationDAOImpl {
@Override
public List<? extends Operation> getOperationsByDeviceAndStatus(int enrolmentId, Operation.Status status)
throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
ConfigOperation configOperation;
List<Operation> operations = new ArrayList<>();
ByteArrayInputStream bais = null;
ObjectInputStream ois = null;
try {
@ -174,12 +172,10 @@ public class ConfigOperationMSSQLDAOImpl extends GenericOperationDAOImpl {
String sql = "SELECT co.ID, co.OPERATION_DETAILS FROM DM_OPERATION co " +
"INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING WHERE ENROLMENT_ID = ? " +
"AND STATUS = ?) dm ON dm.OPERATION_ID = co.ID WHERE co.TYPE = 'CONFIG'";
stmt = conn.prepareStatement(sql);
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, enrolmentId);
stmt.setString(2, status.toString());
rs = stmt.executeQuery();
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
byte[] operationDetails = rs.getBytes("OPERATION_DETAILS");
bais = new ByteArrayInputStream(operationDetails);
@ -189,15 +185,23 @@ public class ConfigOperationMSSQLDAOImpl extends GenericOperationDAOImpl {
configOperation.setId(rs.getInt("ID"));
operations.add(configOperation);
}
}
}
} catch (IOException e) {
throw new OperationManagementDAOException("IO Error occurred while de serialize the configuration " +
"operation object", e);
String msg = "IO Error occurred while de serialize the configuration " +
"operation object" + e;
log.error(msg);
throw new OperationManagementDAOException(msg);
} catch (ClassNotFoundException e) {
throw new OperationManagementDAOException("Class not found error occurred while de serialize the " +
"configuration operation object", e);
String msg = "Class not found error occurred while de serialize the " +
"configuration operation object" + e;
log.error(msg);
throw new OperationManagementDAOException(msg);
} catch (SQLException e) {
throw new OperationManagementDAOException("SQL error occurred while retrieving the operation available " +
"for the device'" + enrolmentId + "' with status '" + status.toString(), e);
String msg = "SQL error occurred while retrieving the operation available " +
"for the device'" + enrolmentId + "' with status '" + status.toString();
log.error(msg, e);
throw new OperationManagementDAOException(msg, e);
} finally {
if (bais != null) {
try {
@ -213,7 +217,6 @@ public class ConfigOperationMSSQLDAOImpl extends GenericOperationDAOImpl {
log.warn("Error occurred while closing ObjectOutputStream", e);
}
}
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
return operations;
}

Loading…
Cancel
Save