Merge pull request #788 from sinthuja/master

Fixes for Oracle database
revert-70aa11f8
Geeth 8 years ago committed by GitHub
commit 2ce5b0ae2b

@ -18,6 +18,8 @@
package org.wso2.carbon.device.mgt.core.dao.impl; package org.wso2.carbon.device.mgt.core.dao.impl;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.core.dao.GroupDAO; import org.wso2.carbon.device.mgt.core.dao.GroupDAO;

@ -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);
} }
@ -320,12 +320,12 @@ public class GenericOperationDAOImpl implements OperationDAO {
"de.DEVICE_ID, d.DEVICE_IDENTIFICATION, \n" + "de.DEVICE_ID, d.DEVICE_IDENTIFICATION, \n" +
"d.DEVICE_TYPE_ID, dt.NAME AS DEVICE_TYPE_NAME, eom.STATUS, eom.CREATED_TIMESTAMP, \n" + "d.DEVICE_TYPE_ID, dt.NAME AS DEVICE_TYPE_NAME, eom.STATUS, eom.CREATED_TIMESTAMP, \n" +
"eom.UPDATED_TIMESTAMP, op.OPERATION_CODE, op.TYPE AS OPERATION_TYPE, dor.OPERATION_RESPONSE, \n" + "eom.UPDATED_TIMESTAMP, op.OPERATION_CODE, op.TYPE AS OPERATION_TYPE, dor.OPERATION_RESPONSE, \n" +
"dor.RECEIVED_TIMESTAMP FROM DM_ENROLMENT_OP_MAPPING AS eom \n" + "dor.RECEIVED_TIMESTAMP FROM DM_ENROLMENT_OP_MAPPING eom \n" +
"INNER JOIN DM_OPERATION AS op ON op.ID=eom.OPERATION_ID\n" + "INNER JOIN DM_OPERATION op ON op.ID=eom.OPERATION_ID\n" +
"INNER JOIN DM_ENROLMENT AS de ON de.ID=eom.ENROLMENT_ID\n" + "INNER JOIN DM_ENROLMENT de ON de.ID=eom.ENROLMENT_ID\n" +
"INNER JOIN DM_DEVICE AS d ON d.ID=de.DEVICE_ID \n" + "INNER JOIN DM_DEVICE d ON d.ID=de.DEVICE_ID \n" +
"INNER JOIN DM_DEVICE_TYPE AS dt ON dt.ID=d.DEVICE_TYPE_ID\n" + "INNER JOIN DM_DEVICE_TYPE dt ON dt.ID=d.DEVICE_TYPE_ID\n" +
"LEFT JOIN DM_DEVICE_OPERATION_RESPONSE AS dor ON dor.ENROLMENT_ID=de.id \n" + "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE dor ON dor.ENROLMENT_ID=de.id \n" +
"AND dor.OPERATION_ID = eom.OPERATION_ID\n" + "AND dor.OPERATION_ID = eom.OPERATION_ID\n" +
"WHERE eom.OPERATION_ID = ? AND de.TENANT_ID = ?"; "WHERE eom.OPERATION_ID = ? AND de.TENANT_ID = ?";
@ -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);
} }

@ -148,7 +148,7 @@ public class OperationMappingDAOImpl implements OperationMappingDAO {
//devices to be active at that moment. Hence filtering by 'ACTIVE' & 'UNREACHABLE' device states. //devices to be active at that moment. Hence filtering by 'ACTIVE' & 'UNREACHABLE' device states.
String sql = "SELECT ENROLMENT_ID, D.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFIER, MIN(CREATED_TIMESTAMP) " + String sql = "SELECT ENROLMENT_ID, D.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFIER, MIN(CREATED_TIMESTAMP) " +
"AS CREATED_TIMESTAMP, E.STATUS AS ENROLMENT_STATUS, E.TENANT_ID FROM " + "AS CREATED_TIMESTAMP, E.STATUS AS ENROLMENT_STATUS, E.TENANT_ID FROM " +
"DM_ENROLMENT_OP_MAPPING AS OP INNER JOIN DM_ENROLMENT E ON OP.ENROLMENT_ID = E.ID INNER JOIN " + "DM_ENROLMENT_OP_MAPPING OP INNER JOIN DM_ENROLMENT E ON OP.ENROLMENT_ID = E.ID INNER JOIN " +
"DM_DEVICE D ON E.DEVICE_ID = D.ID WHERE " + "DM_DEVICE D ON E.DEVICE_ID = D.ID WHERE " +
"OP.STATUS IN ('"+ Operation.Status.PENDING.name() + "','" + Operation.Status.REPEATED.name() + "') " + "OP.STATUS IN ('"+ Operation.Status.PENDING.name() + "','" + Operation.Status.REPEATED.name() + "') " +
"AND OP.CREATED_TIMESTAMP BETWEEN ? AND ? AND E.STATUS IN ('" + EnrolmentInfo.Status.ACTIVE.name() + "AND OP.CREATED_TIMESTAMP BETWEEN ? AND ? AND E.STATUS IN ('" + EnrolmentInfo.Status.ACTIVE.name() +
@ -183,7 +183,7 @@ public class OperationMappingDAOImpl implements OperationMappingDAO {
//We are specifically looking for operation mappings in 'Pending' & 'Repeated' states. Further we want //We are specifically looking for operation mappings in 'Pending' & 'Repeated' states. Further we want
//devices to be active at that moment. Hence filtering by 'ACTIVE' & 'UNREACHABLE' device states. //devices to be active at that moment. Hence filtering by 'ACTIVE' & 'UNREACHABLE' device states.
String sql = "SELECT OP.ENROLMENT_ID AS EID, MAX(OP.UPDATED_TIMESTAMP) AS LAST_CONNECTED_TIME FROM " + String sql = "SELECT OP.ENROLMENT_ID AS EID, MAX(OP.UPDATED_TIMESTAMP) AS LAST_CONNECTED_TIME FROM " +
"DM_ENROLMENT_OP_MAPPING AS OP INNER JOIN DM_ENROLMENT E ON OP.ENROLMENT_ID = E.ID INNER JOIN " + "DM_ENROLMENT_OP_MAPPING OP INNER JOIN DM_ENROLMENT E ON OP.ENROLMENT_ID = E.ID INNER JOIN " +
"DM_DEVICE D ON E.DEVICE_ID = D.ID WHERE " + "DM_DEVICE D ON E.DEVICE_ID = D.ID WHERE " +
"OP.STATUS = '" + Operation.Status.COMPLETED.name() + "'" + "OP.STATUS = '" + Operation.Status.COMPLETED.name() + "'" +
"AND OP.UPDATED_TIMESTAMP >= ? AND E.STATUS IN ('" + EnrolmentInfo.Status.ACTIVE.name() + "AND OP.UPDATED_TIMESTAMP >= ? AND E.STATUS IN ('" + EnrolmentInfo.Status.ACTIVE.name() +

@ -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) {
operationMappings = new LinkedList<>();
operationMappingsTenantMap.put(tenantID, operationMappings);
} }
if (id != 0) { operationMapping = new OperationMapping();
stmt = connection.prepareStatement( operationMapping.setOperationId(rs.getInt("OPERATION_ID"));
"UPDATE DM_ENROLMENT_OP_MAPPING SET UPDATED_TIMESTAMP = ? " + "WHERE ID = ?"); DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
stmt.setLong(1, System.currentTimeMillis() / 1000); deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION"));
stmt.setInt(2, id); deviceIdentifier.setType(rs.getString("DEVICE_TYPE"));
stmt.executeUpdate(); operationMapping.setDeviceIdentifier(deviceIdentifier);
result = true; 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;
}
} }

@ -23,6 +23,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService;
@ -360,8 +361,9 @@ public class ProcessorImpl implements Processor {
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "SELECT * FROM DM_DEVICE_INFO WHERE DEVICE_ID IN ("; String query = "SELECT * FROM DM_DEVICE_INFO WHERE DEVICE_ID IN (";
if (conn.getMetaData().getDatabaseProductName().contains("H2") || conn.getMetaData() if (conn.getMetaData().getDatabaseProductName().contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2) || conn.getMetaData()
.getDatabaseProductName().contains("MySQL")) { .getDatabaseProductName().contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL) ||
conn.getMetaData().getDatabaseProductName().contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE)) {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
for (int i = 0; i < devices.size(); i++) { for (int i = 0; i < devices.size(); i++) {
builder.append("?,"); builder.append("?,");
@ -374,8 +376,9 @@ public class ProcessorImpl implements Processor {
} else { } else {
query += "?) ORDER BY DEVICE_ID"; query += "?) ORDER BY DEVICE_ID";
stmt = conn.prepareStatement(query); stmt = conn.prepareStatement(query);
Array array = conn.createArrayOf("INT", Utils.getArrayOfDeviceIds(devices)); for (int i = 0; i < devices.size(); i++) {
stmt.setArray(1, array); stmt.setInt(i, devices.get(i).getId());
}
} }
rs = stmt.executeQuery(); rs = stmt.executeQuery();

@ -119,7 +119,6 @@ public class QueryBuilderImpl implements QueryBuilder {
log.debug("Property with OR Query : " + queries.get(Constants.PROP_OR)); log.debug("Property with OR Query : " + queries.get(Constants.PROP_OR));
log.debug("Location related Query : " + queries.get(Constants.LOCATION)); log.debug("Location related Query : " + queries.get(Constants.LOCATION));
} }
return queries; return queries;
} }
@ -342,10 +341,10 @@ public class QueryBuilderImpl implements QueryBuilder {
"DD.SSID, DD.CPU_USAGE, DD.TOTAL_RAM_MEMORY, DD.AVAILABLE_RAM_MEMORY, \n" + "DD.SSID, DD.CPU_USAGE, DD.TOTAL_RAM_MEMORY, DD.AVAILABLE_RAM_MEMORY, \n" +
"DD.PLUGGED_IN, DD.UPDATE_TIMESTAMP, DL.LATITUDE, DL.LONGITUDE, DL.STREET1, DL.STREET2, DL.CITY, DL.ZIP, \n" + "DD.PLUGGED_IN, DD.UPDATE_TIMESTAMP, DL.LATITUDE, DL.LONGITUDE, DL.STREET1, DL.STREET2, DL.CITY, DL.ZIP, \n" +
"DL.STATE, DL.COUNTRY, DL.UPDATE_TIMESTAMP AS DL_UPDATED_TIMESTAMP, DE.OWNER, DE.OWNERSHIP, DE.STATUS " + "DL.STATE, DL.COUNTRY, DL.UPDATE_TIMESTAMP AS DL_UPDATED_TIMESTAMP, DE.OWNER, DE.OWNERSHIP, DE.STATUS " +
"AS DE_STATUS FROM DM_DEVICE_DETAIL AS DD INNER JOIN DM_DEVICE AS D ON D.ID=DD.DEVICE_ID\n" + "AS DE_STATUS FROM DM_DEVICE_DETAIL DD INNER JOIN DM_DEVICE D ON D.ID=DD.DEVICE_ID\n" +
"LEFT JOIN DM_DEVICE_LOCATION AS DL ON DL.DEVICE_ID=D.ID \n" + "LEFT JOIN DM_DEVICE_LOCATION DL ON DL.DEVICE_ID=D.ID \n" +
"INNER JOIN DM_DEVICE_TYPE AS DT ON DT.ID=D.DEVICE_TYPE_ID\n" + "INNER JOIN DM_DEVICE_TYPE DT ON DT.ID=D.DEVICE_TYPE_ID\n" +
"INNER JOIN DM_ENROLMENT AS DE ON D.ID=DE.DEVICE_ID\n" + "INNER JOIN DM_ENROLMENT DE ON D.ID=DE.DEVICE_ID\n" +
"WHERE D.TENANT_ID = ? "; "WHERE D.TENANT_ID = ? ";
ValueType type = new ValueType(); ValueType type = new ValueType();
@ -370,11 +369,11 @@ public class QueryBuilderImpl implements QueryBuilder {
"DD.PLUGGED_IN, DD.UPDATE_TIMESTAMP, DL.LATITUDE, DL.LONGITUDE, DL.STREET1, DL.STREET2, DL.CITY, DL.ZIP, \n" + "DD.PLUGGED_IN, DD.UPDATE_TIMESTAMP, DL.LATITUDE, DL.LONGITUDE, DL.STREET1, DL.STREET2, DL.CITY, DL.ZIP, \n" +
"DL.STATE, DL.COUNTRY, DL.UPDATE_TIMESTAMP AS DL_UPDATED_TIMESTAMP, DI.KEY_FIELD, DI.VALUE_FIELD, \n" + "DL.STATE, DL.COUNTRY, DL.UPDATE_TIMESTAMP AS DL_UPDATED_TIMESTAMP, DI.KEY_FIELD, DI.VALUE_FIELD, \n" +
"DE.OWNER, DE.OWNERSHIP, DE.STATUS AS DE_STATUS " + "DE.OWNER, DE.OWNERSHIP, DE.STATUS AS DE_STATUS " +
"FROM DM_DEVICE_DETAIL AS DD INNER JOIN DM_DEVICE AS D ON D.ID=DD.DEVICE_ID\n" + "FROM DM_DEVICE_DETAIL DD INNER JOIN DM_DEVICE D ON D.ID=DD.DEVICE_ID\n" +
"LEFT JOIN DM_DEVICE_LOCATION AS DL ON DL.DEVICE_ID=D.ID \n" + "LEFT JOIN DM_DEVICE_LOCATION DL ON DL.DEVICE_ID=D.ID \n" +
"INNER JOIN DM_DEVICE_TYPE AS DT ON DT.ID=D.DEVICE_TYPE_ID\n" + "INNER JOIN DM_DEVICE_TYPE DT ON DT.ID=D.DEVICE_TYPE_ID\n" +
"INNER JOIN DM_ENROLMENT AS DE ON D.ID=DE.DEVICE_ID\n" + "INNER JOIN DM_ENROLMENT DE ON D.ID=DE.DEVICE_ID\n" +
"LEFT JOIN DM_DEVICE_INFO AS DI ON DI.DEVICE_ID=D.ID\n" + "LEFT JOIN DM_DEVICE_INFO DI ON DI.DEVICE_ID=D.ID\n" +
"WHERE D.TENANT_ID = ? "; "WHERE D.TENANT_ID = ? ";
ValueType type = new ValueType(); ValueType type = new ValueType();

@ -29,6 +29,7 @@ import org.wso2.carbon.policy.mgt.core.dao.impl.MonitoringDAOImpl;
import org.wso2.carbon.policy.mgt.core.dao.impl.PolicyDAOImpl; import org.wso2.carbon.policy.mgt.core.dao.impl.PolicyDAOImpl;
import org.wso2.carbon.policy.mgt.core.dao.impl.ProfileDAOImpl; import org.wso2.carbon.policy.mgt.core.dao.impl.ProfileDAOImpl;
import org.wso2.carbon.policy.mgt.core.dao.impl.feature.GenericFeatureDAOImpl; import org.wso2.carbon.policy.mgt.core.dao.impl.feature.GenericFeatureDAOImpl;
import org.wso2.carbon.policy.mgt.core.dao.impl.feature.OracleServerFeatureDAOImpl;
import org.wso2.carbon.policy.mgt.core.dao.impl.feature.SQLServerFeatureDAOImpl; import org.wso2.carbon.policy.mgt.core.dao.impl.feature.SQLServerFeatureDAOImpl;
import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil;
@ -77,6 +78,7 @@ public class PolicyManagementDAOFactory {
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL: case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL:
return new SQLServerFeatureDAOImpl(); return new SQLServerFeatureDAOImpl();
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE: case DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE:
return new OracleServerFeatureDAOImpl();
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_POSTGRESQL: case DeviceManagementConstants.DataBaseTypes.DB_TYPE_POSTGRESQL:
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2: case DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2:
case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL: case DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL:

@ -0,0 +1,94 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.wso2.carbon.policy.mgt.core.dao.impl.feature;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature;
import org.wso2.carbon.policy.mgt.core.dao.FeatureManagerDAOException;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory;
import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil;
import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public class OracleServerFeatureDAOImpl extends AbstractFeatureDAO {
/**
* Batch sizes greater than 10 throws array out of bound exception.
*/
private static int BATCH_SIZE = 10;
@Override
public List<ProfileFeature> addProfileFeatures(List<ProfileFeature> features, int profileId) throws
FeatureManagerDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet generatedKeys = null;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
String query = "INSERT INTO DM_PROFILE_FEATURES (PROFILE_ID, FEATURE_CODE, DEVICE_TYPE, CONTENT, " +
"TENANT_ID) VALUES (?, ?, ?, ?, ?)";
stmt = conn.prepareStatement(query, new String[]{"id"});
int noRecords = 0;
for (ProfileFeature feature : features) {
stmt.setInt(1, profileId);
stmt.setString(2, feature.getFeatureCode());
stmt.setString(3, feature.getDeviceType());
stmt.setBytes(4, PolicyManagerUtil.getBytes(feature.getContent()));
stmt.setInt(5, tenantId);
stmt.addBatch();
noRecords++;
if (noRecords >= BATCH_SIZE && noRecords % BATCH_SIZE == 0) {
stmt.executeBatch();
generatedKeys = stmt.getGeneratedKeys();
int i = noRecords - this.BATCH_SIZE;
while (generatedKeys.next()) {
features.get(i).setId(generatedKeys.getInt(1));
i++;
}
}
}
stmt.executeBatch();
generatedKeys = stmt.getGeneratedKeys();
int i = 0;
if (noRecords > BATCH_SIZE) {
i = noRecords - BATCH_SIZE;
}
while (generatedKeys.next()) {
features.get(i).setId(generatedKeys.getInt(1));
i++;
}
} catch (SQLException | IOException e) {
throw new FeatureManagerDAOException("Error occurred while adding the feature list to the database.", e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, generatedKeys);
}
return features;
}
private Connection getConnection() throws FeatureManagerDAOException {
return PolicyManagementDAOFactory.getConnection();
}
}

@ -49,7 +49,7 @@ CREATE TABLE DM_ROLE_GROUP_MAP (
GROUP_ID NUMBER(10) DEFAULT NULL, GROUP_ID NUMBER(10) DEFAULT NULL,
ROLE VARCHAR2(45) DEFAULT NULL, ROLE VARCHAR2(45) DEFAULT NULL,
TENANT_ID NUMBER(10) DEFAULT 0, TENANT_ID NUMBER(10) DEFAULT 0,
CONSTRAINT PK_DM_GROUP PRIMARY KEY (ID), CONSTRAINT PK_DM_ROLE_GROUP PRIMARY KEY (ID),
CONSTRAINT fk_DM_ROLE_GROUP_MAP_GROUP2 FOREIGN KEY (GROUP_ID) CONSTRAINT fk_DM_ROLE_GROUP_MAP_GROUP2 FOREIGN KEY (GROUP_ID)
REFERENCES DM_GROUP (ID) REFERENCES DM_GROUP (ID)
) )

Loading…
Cancel
Save