Merging the AS clause removal queries with generic DAO from oracle.

4.x.x
sinthuja 8 years ago
parent 9398e7ee0b
commit ade596d3c4

@ -135,9 +135,9 @@ public class GenericOperationDAOImpl implements OperationDAO {
ResultSet rs = null; ResultSet rs = null;
try { try {
Connection connection = OperationManagementDAOFactory.getConnection(); Connection connection = OperationManagementDAOFactory.getConnection();
String query = "SELECT EOM.ID FROM DM_ENROLMENT_OP_MAPPING AS EOM INNER JOIN DM_OPERATION DM " + String query = "SELECT EOM.ID FROM DM_ENROLMENT_OP_MAPPING EOM INNER JOIN DM_OPERATION DM "
"ON DM.ID = EOM.OPERATION_ID WHERE EOM.ENROLMENT_ID = ? AND DM.OPERATION_CODE = ? " + + "ON DM.ID = EOM.OPERATION_ID WHERE EOM.ENROLMENT_ID = ? AND DM.OPERATION_CODE = ? "
"AND EOM.STATUS = ?;"; + "AND EOM.STATUS = ?";
stmt = connection.prepareStatement(query); stmt = connection.prepareStatement(query);
stmt.setInt(1, enrolmentId); stmt.setInt(1, enrolmentId);
stmt.setString(2, operationCode); stmt.setString(2, operationCode);
@ -149,8 +149,8 @@ public class GenericOperationDAOImpl implements OperationDAO {
id = rs.getInt("ID"); id = rs.getInt("ID");
} }
if (id != 0) { if (id != 0) {
stmt = connection.prepareStatement("UPDATE DM_ENROLMENT_OP_MAPPING SET STATUS = ?, " + stmt = connection.prepareStatement(
"UPDATED_TIMESTAMP = ? WHERE ID = ?"); "UPDATE DM_ENROLMENT_OP_MAPPING SET STATUS = ?, " + "UPDATED_TIMESTAMP = ? WHERE ID = ?");
stmt.setString(1, newStatus.toString()); stmt.setString(1, newStatus.toString());
stmt.setLong(2, System.currentTimeMillis() / 1000); stmt.setLong(2, System.currentTimeMillis() / 1000);
stmt.setInt(3, id); stmt.setInt(3, id);
@ -158,8 +158,8 @@ public class GenericOperationDAOImpl implements OperationDAO {
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while update device mapping operation status " + throw new OperationManagementDAOException(
"metadata", e); "Error occurred while update device mapping operation status " + "metadata", e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt); OperationManagementDAOUtil.cleanupResources(stmt);
} }
@ -173,9 +173,9 @@ public class GenericOperationDAOImpl implements OperationDAO {
boolean result = false; boolean result = false;
try { try {
Connection connection = OperationManagementDAOFactory.getConnection(); Connection connection = OperationManagementDAOFactory.getConnection();
String query = "SELECT EOM.ID FROM DM_ENROLMENT_OP_MAPPING AS EOM INNER JOIN DM_OPERATION DM " + String query = "SELECT EOM.ID FROM DM_ENROLMENT_OP_MAPPING EOM INNER JOIN DM_OPERATION DM "
"ON DM.ID = EOM.OPERATION_ID WHERE EOM.ENROLMENT_ID = ? AND DM.OPERATION_CODE = ? AND " + + "ON DM.ID = EOM.OPERATION_ID WHERE EOM.ENROLMENT_ID = ? AND DM.OPERATION_CODE = ? AND "
"EOM.STATUS = ?;"; + "EOM.STATUS = ?";
stmt = connection.prepareStatement(query); stmt = connection.prepareStatement(query);
stmt.setInt(1, enrolmentId); stmt.setInt(1, enrolmentId);
stmt.setString(2, operationCode); stmt.setString(2, operationCode);
@ -187,16 +187,16 @@ public class GenericOperationDAOImpl implements OperationDAO {
id = rs.getInt("ID"); id = rs.getInt("ID");
} }
if (id != 0) { if (id != 0) {
stmt = connection.prepareStatement("UPDATE DM_ENROLMENT_OP_MAPPING SET UPDATED_TIMESTAMP = ? " + stmt = connection.prepareStatement(
"WHERE ID = ?"); "UPDATE DM_ENROLMENT_OP_MAPPING SET UPDATED_TIMESTAMP = ? " + "WHERE ID = ?");
stmt.setLong(1, System.currentTimeMillis() / 1000); stmt.setLong(1, System.currentTimeMillis() / 1000);
stmt.setInt(2, id); stmt.setInt(2, id);
stmt.executeUpdate(); stmt.executeUpdate();
result = true; result = true;
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while update device mapping operation status " + throw new OperationManagementDAOException(
"metadata", e); "Error occurred while update device mapping operation status " + "metadata", e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt); OperationManagementDAOUtil.cleanupResources(stmt);
} }
@ -548,9 +548,9 @@ public class GenericOperationDAOImpl implements OperationDAO {
ResultSet rs = null; ResultSet rs = null;
try { try {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT COUNT(*) AS COUNT FROM DM_ENROLMENT_OP_MAPPING AS m \n" + String sql = "SELECT COUNT(*) AS COUNT FROM DM_ENROLMENT_OP_MAPPING m \n"
"INNER JOIN DM_ENROLMENT AS d ON m.ENROLMENT_ID = d.ID \n" + + "INNER JOIN DM_ENROLMENT d ON m.ENROLMENT_ID = d.ID \n"
"WHERE m.UPDATED_TIMESTAMP > ? AND d.TENANT_ID = ?;"; + "WHERE m.UPDATED_TIMESTAMP > ? AND d.TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setLong(1, timestamp); stmt.setLong(1, timestamp);
stmt.setInt(2, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); stmt.setInt(2, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
@ -559,8 +559,8 @@ public class GenericOperationDAOImpl implements OperationDAO {
return rs.getInt("COUNT"); return rs.getInt("COUNT");
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while getting the activity count from " + throw new OperationManagementDAOException(
"the database.", e); "Error occurred while getting the activity count from " + "the database.", e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs); OperationManagementDAOUtil.cleanupResources(stmt, rs);
} }

@ -138,77 +138,47 @@ public class OracleOperationDAOImpl extends GenericOperationDAOImpl {
} }
@Override @Override
public void updateEnrollmentOperationsStatus(int enrolmentId, String operationCode, public Map<Integer, List<OperationMapping>> getOperationMappingsByStatus(Operation.Status opStatus, Operation.PushNotificationStatus pushNotificationStatus,
Operation.Status existingStatus, Operation.Status newStatus) throws OperationManagementDAOException { int limit) throws OperationManagementDAOException {
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet rs = null; ResultSet rs = null;
OperationMapping operationMapping;
Map<Integer, List<OperationMapping>> operationMappingsTenantMap = new HashMap<>();
try { try {
Connection connection = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
String query = "SELECT EOM.ID FROM DM_ENROLMENT_OP_MAPPING EOM INNER JOIN DM_OPERATION DM " String sql = "SELECT op.ENROLMENT_ID, op.OPERATION_ID, d.DEVICE_IDENTIFICATION, dt.NAME as DEVICE_TYPE, d" +
+ "ON DM.ID = EOM.OPERATION_ID WHERE EOM.ENROLMENT_ID = ? AND DM.OPERATION_CODE = ? " ".TENANT_ID FROM DM_DEVICE d, DM_ENROLMENT_OP_MAPPING op, DM_DEVICE_TYPE dt WHERE op.STATUS = ? " +
+ "AND EOM.STATUS = ?"; "AND op.PUSH_NOTIFICATION_STATUS = ? AND d.DEVICE_TYPE_ID = dt.ID AND d.ID=op.ENROLMENT_ID AND " +
stmt = connection.prepareStatement(query); "ROWNUM <= ? ORDER BY op.OPERATION_ID";
stmt.setInt(1, enrolmentId);
stmt.setString(2, operationCode);
stmt.setString(3, existingStatus.toString());
// This will return only one result always.
rs = stmt.executeQuery();
int id = 0;
while (rs.next()) {
id = rs.getInt("ID");
}
if (id != 0) {
stmt = connection.prepareStatement(
"UPDATE DM_ENROLMENT_OP_MAPPING SET STATUS = ?, " + "UPDATED_TIMESTAMP = ? WHERE ID = ?");
stmt.setString(1, newStatus.toString());
stmt.setLong(2, System.currentTimeMillis() / 1000);
stmt.setInt(3, id);
stmt.executeUpdate();
}
} catch (SQLException e) {
throw new OperationManagementDAOException(
"Error occurred while update device mapping operation status " + "metadata", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt);
}
}
@Override stmt = conn.prepareStatement(sql);
public boolean updateTaskOperation(int enrolmentId, String operationCode) throws OperationManagementDAOException { stmt.setString(1, opStatus.toString());
PreparedStatement stmt = null; stmt.setString(2, pushNotificationStatus.toString());
ResultSet rs = null; stmt.setInt(3, limit);
boolean result = false;
try {
Connection connection = OperationManagementDAOFactory.getConnection();
String query = "SELECT EOM.ID FROM DM_ENROLMENT_OP_MAPPING EOM INNER JOIN DM_OPERATION DM "
+ "ON DM.ID = EOM.OPERATION_ID WHERE EOM.ENROLMENT_ID = ? AND DM.OPERATION_CODE = ? AND "
+ "EOM.STATUS = ?";
stmt = connection.prepareStatement(query);
stmt.setInt(1, enrolmentId);
stmt.setString(2, operationCode);
stmt.setString(3, Operation.Status.PENDING.toString());
// This will return only one result always.
rs = stmt.executeQuery(); rs = stmt.executeQuery();
int id = 0; while (rs.next()) {
if (rs.next()) { int tenantID = rs.getInt("TENANT_ID");
id = rs.getInt("ID"); List<OperationMapping> operationMappings = operationMappingsTenantMap.get(tenantID);
} if (operationMappings == null) {
if (id != 0) { operationMappings = new LinkedList<>();
stmt = connection.prepareStatement( operationMappingsTenantMap.put(tenantID, operationMappings);
"UPDATE DM_ENROLMENT_OP_MAPPING SET UPDATED_TIMESTAMP = ? " + "WHERE ID = ?"); }
stmt.setLong(1, System.currentTimeMillis() / 1000); operationMapping = new OperationMapping();
stmt.setInt(2, id); operationMapping.setOperationId(rs.getInt("OPERATION_ID"));
stmt.executeUpdate(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
result = true; deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION"));
deviceIdentifier.setType(rs.getString("DEVICE_TYPE"));
operationMapping.setDeviceIdentifier(deviceIdentifier);
operationMapping.setEnrollmentId(rs.getInt("ENROLMENT_ID"));
operationMapping.setTenantId(tenantID);
operationMappings.add(operationMapping);
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException( throw new OperationManagementDAOException("SQL error while getting operation mappings from database. ", e);
"Error occurred while update device mapping operation status " + "metadata", e);
} finally { } finally {
OperationManagementDAOUtil.cleanupResources(stmt); OperationManagementDAOUtil.cleanupResources(stmt, rs);
} }
return result; return operationMappingsTenantMap;
} }
@Override @Override
@ -340,74 +310,4 @@ public class OracleOperationDAOImpl extends GenericOperationDAOImpl {
} }
return activities; return activities;
} }
@Override
public int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT COUNT(*) COUNT FROM DM_ENROLMENT_OP_MAPPING m \n"
+ "INNER JOIN DM_ENROLMENT d ON m.ENROLMENT_ID = d.ID \n"
+ "WHERE m.UPDATED_TIMESTAMP > ? AND d.TENANT_ID = ?";
stmt = conn.prepareStatement(sql);
stmt.setLong(1, timestamp);
stmt.setInt(2, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
rs = stmt.executeQuery();
if (rs.next()) {
return rs.getInt("COUNT");
}
} catch (SQLException e) {
throw new OperationManagementDAOException(
"Error occurred while getting the activity count from " + "the database.", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
return 0;
}
@Override
public Map<Integer, List<OperationMapping>> getOperationMappingsByStatus(Operation.Status opStatus, Operation.PushNotificationStatus pushNotificationStatus,
int limit) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
OperationMapping operationMapping;
Map<Integer, List<OperationMapping>> operationMappingsTenantMap = new HashMap<>();
try {
Connection conn = OperationManagementDAOFactory.getConnection();
String sql = "SELECT op.ENROLMENT_ID, op.OPERATION_ID, d.DEVICE_IDENTIFICATION, dt.NAME as DEVICE_TYPE, d" +
".TENANT_ID FROM DM_DEVICE d, DM_ENROLMENT_OP_MAPPING op, DM_DEVICE_TYPE dt WHERE op.STATUS = ? " +
"AND op.PUSH_NOTIFICATION_STATUS = ? AND d.DEVICE_TYPE_ID = dt.ID AND d.ID=op.ENROLMENT_ID AND " +
"ROWNUM <= ? ORDER BY op.OPERATION_ID";
stmt = conn.prepareStatement(sql);
stmt.setString(1, opStatus.toString());
stmt.setString(2, pushNotificationStatus.toString());
stmt.setInt(3, limit);
rs = stmt.executeQuery();
while (rs.next()) {
int tenantID = rs.getInt("TENANT_ID");
List<OperationMapping> operationMappings = operationMappingsTenantMap.get(tenantID);
if (operationMappings == null) {
operationMappings = new LinkedList<>();
operationMappingsTenantMap.put(tenantID, operationMappings);
}
operationMapping = new OperationMapping();
operationMapping.setOperationId(rs.getInt("OPERATION_ID"));
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION"));
deviceIdentifier.setType(rs.getString("DEVICE_TYPE"));
operationMapping.setDeviceIdentifier(deviceIdentifier);
operationMapping.setEnrollmentId(rs.getInt("ENROLMENT_ID"));
operationMapping.setTenantId(tenantID);
operationMappings.add(operationMapping);
}
} catch (SQLException e) {
throw new OperationManagementDAOException("SQL error while getting operation mappings from database. ", e);
} finally {
OperationManagementDAOUtil.cleanupResources(stmt, rs);
}
return operationMappingsTenantMap;
}
} }
Loading…
Cancel
Save