Fixing issues related to oracle database.

revert-70aa11f8
sinthuja 8 years ago
parent 1c13269fc8
commit 124c0b4cf8

@ -19,6 +19,7 @@
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.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.core.dao.GroupDAO;
@ -204,8 +205,13 @@ public class GroupDAOImpl implements GroupDAO {
hasOwner = true;
}
if (hasLimit) {
if (conn.getMetaData().getDatabaseProductName().contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE) ||
conn.getMetaData().getDatabaseProductName().contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL)) {
sql += " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
} else {
sql += " LIMIT ?, ?";
}
}
int paramIndex = 1;
stmt = conn.prepareStatement(sql);
@ -267,8 +273,13 @@ public class GroupDAOImpl implements GroupDAO {
}
sql += ")";
if (hasLimit) {
if (conn.getMetaData().getDatabaseProductName().contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE) ||
conn.getMetaData().getDatabaseProductName().contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL)) {
sql += " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
} else {
sql += " LIMIT ?, ?";
}
}
int paramIndex = 1;
stmt = conn.prepareStatement(sql);
@ -513,7 +524,13 @@ public class GroupDAOImpl implements GroupDAO {
" DM_DEVICE d, (" +
"SELECT dgm.DEVICE_ID FROM DM_DEVICE_GROUP_MAP dgm WHERE dgm.GROUP_ID = ?) dgm1 " +
"WHERE d.ID = dgm1.DEVICE_ID AND d.TENANT_ID = ?) gd, DM_DEVICE_TYPE t " +
"WHERE gd.DEVICE_TYPE_ID = t.ID) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ? LIMIT ? , ?";
"WHERE gd.DEVICE_TYPE_ID = t.ID) d1 WHERE d1.DEVICE_ID = e.DEVICE_ID AND TENANT_ID = ?";
if (conn.getMetaData().getDatabaseProductName().contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE) ||
conn.getMetaData().getDatabaseProductName().contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_MSSQL)) {
sql += " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
} else {
sql += " LIMIT ?, ?";
}
stmt = conn.prepareStatement(sql);
stmt.setInt(1, groupId);

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

@ -148,11 +148,12 @@ public class OperationMappingDAOImpl implements OperationMappingDAO {
//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) " +
"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 " +
"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() +
"','" + EnrolmentInfo.Status.UNREACHABLE.name() + "') AND D.DEVICE_TYPE_ID = ? GROUP BY ENROLMENT_ID";
"','" + EnrolmentInfo.Status.UNREACHABLE.name() + "') AND D.DEVICE_TYPE_ID = ? GROUP BY " +
"ENROLMENT_ID, D.DEVICE_IDENTIFICATION,E.STATUS, E.TENANT_ID";
stmt = conn.prepareStatement(sql);
stmt.setLong(1, maxDuration);
stmt.setLong(2, minDuration);
@ -182,7 +183,7 @@ public class OperationMappingDAOImpl implements OperationMappingDAO {
//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.
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 " +
"OP.STATUS = '" + Operation.Status.COMPLETED.name() + "'" +
"AND OP.UPDATED_TIMESTAMP >= ? AND E.STATUS IN ('" + EnrolmentInfo.Status.ACTIVE.name() +

@ -23,6 +23,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device;
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.authorization.DeviceAccessAuthorizationException;
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService;
@ -62,7 +63,7 @@ public class ProcessorImpl implements Processor {
@Override
public List<Device> execute(SearchContext searchContext) throws SearchMgtException {
if(!Utils.validateOperators(searchContext.getConditions())){
if (!Utils.validateOperators(searchContext.getConditions())) {
throw new SearchMgtException("Invalid validator is provided.");
}
@ -268,10 +269,10 @@ public class ProcessorImpl implements Processor {
} else if (type.getColumnType().equals(ValueType.columnType.INTEGER)) {
stmt.setInt(x, type.getIntValue());
x++;
} else if (type.getColumnType().equals(ValueType.columnType.LONG)){
} else if (type.getColumnType().equals(ValueType.columnType.LONG)) {
stmt.setLong(x, type.getLongValue());
x++;
} else if(type.getColumnType().equals(ValueType.columnType.DOUBLE)){
} else if (type.getColumnType().equals(ValueType.columnType.DOUBLE)) {
stmt.setDouble(x, type.getDoubleValue());
x++;
}
@ -360,8 +361,9 @@ public class ProcessorImpl implements Processor {
try {
conn = this.getConnection();
String query = "SELECT * FROM DM_DEVICE_INFO WHERE DEVICE_ID IN (";
if (conn.getMetaData().getDatabaseProductName().contains("H2") || conn.getMetaData()
.getDatabaseProductName().contains("MySQL")) {
if (conn.getMetaData().getDatabaseProductName().contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_H2) || conn.getMetaData()
.getDatabaseProductName().contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_MYSQL) ||
conn.getMetaData().getDatabaseProductName().contains(DeviceManagementConstants.DataBaseTypes.DB_TYPE_ORACLE)) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < devices.size(); i++) {
builder.append("?,");
@ -374,8 +376,9 @@ public class ProcessorImpl implements Processor {
} else {
query += "?) ORDER BY DEVICE_ID";
stmt = conn.prepareStatement(query);
Array array = conn.createArrayOf("INT", Utils.getArrayOfDeviceIds(devices));
stmt.setArray(1, array);
for (int i = 0; i < devices.size(); i++) {
stmt.setInt(i, devices.get(i).getId());
}
}
rs = stmt.executeQuery();

@ -113,13 +113,12 @@ public class QueryBuilderImpl implements QueryBuilder {
queries.put(Constants.LOCATION, this.processLocation(locCondition));
}
if (log.isDebugEnabled()) {
log.debug("General Query : " + queries.get(Constants.GENERAL));
log.debug("Property with AND Query : " + queries.get(Constants.PROP_AND));
log.debug("Property with OR Query : " + queries.get(Constants.PROP_OR));
log.debug("Location related Query : " + queries.get(Constants.LOCATION));
}
// if (log.isDebugEnabled()) {
log.info("General Query : " + queries.get(Constants.GENERAL));
log.info("Property with AND Query : " + queries.get(Constants.PROP_AND));
log.info("Property with OR Query : " + queries.get(Constants.PROP_OR));
log.info("Location related Query : " + queries.get(Constants.LOCATION));
// }
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.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 " +
"AS DE_STATUS FROM DM_DEVICE_DETAIL AS DD INNER JOIN DM_DEVICE AS D ON D.ID=DD.DEVICE_ID\n" +
"LEFT JOIN DM_DEVICE_LOCATION AS 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_ENROLMENT AS DE ON D.ID=DE.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 DL ON DL.DEVICE_ID=D.ID \n" +
"INNER JOIN DM_DEVICE_TYPE DT ON DT.ID=D.DEVICE_TYPE_ID\n" +
"INNER JOIN DM_ENROLMENT DE ON D.ID=DE.DEVICE_ID\n" +
"WHERE D.TENANT_ID = ? ";
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" +
"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 " +
"FROM DM_DEVICE_DETAIL AS DD INNER JOIN DM_DEVICE AS D ON D.ID=DD.DEVICE_ID\n" +
"LEFT JOIN DM_DEVICE_LOCATION AS 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_ENROLMENT AS DE ON D.ID=DE.DEVICE_ID\n" +
"LEFT JOIN DM_DEVICE_INFO AS DI ON DI.DEVICE_ID=D.ID\n" +
"FROM DM_DEVICE_DETAIL DD INNER JOIN DM_DEVICE D ON D.ID=DD.DEVICE_ID\n" +
"LEFT JOIN DM_DEVICE_LOCATION DL ON DL.DEVICE_ID=D.ID \n" +
"INNER JOIN DM_DEVICE_TYPE DT ON DT.ID=D.DEVICE_TYPE_ID\n" +
"INNER JOIN DM_ENROLMENT DE ON D.ID=DE.DEVICE_ID\n" +
"LEFT JOIN DM_DEVICE_INFO DI ON DI.DEVICE_ID=D.ID\n" +
"WHERE D.TENANT_ID = ? ";
ValueType type = new ValueType();

@ -38,8 +38,7 @@ import java.util.List;
* FeatureDAO implementation for DB engines with ANSI SQL support.
*/
public final class GenericFeatureDAOImpl extends AbstractFeatureDAO {
private static final Log log = LogFactory.getLog(GenericFeatureDAOImpl.class);
private static final int BATCH_SIZE = 10;
@Override
public List<ProfileFeature> addProfileFeatures(List<ProfileFeature> features, int profileId) throws
@ -54,31 +53,36 @@ public final class GenericFeatureDAOImpl extends AbstractFeatureDAO {
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"});
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());
// if (conn.getMetaData().getDriverName().contains("H2")) {
// stmt.setBytes(4, PolicyManagerUtil.getBytes(feature.getContent()));
// } else {
stmt.setBytes(4, PolicyManagerUtil.getBytes(feature.getContent()));
//}
stmt.setInt(5, tenantId);
stmt.addBatch();
//Not adding the logic to check the size of the stmt and execute if the size records added is over 1000
noRecords++;
if (noRecords >= BATCH_SIZE && noRecords % BATCH_SIZE == 0) {
stmt.executeBatch();
generatedKeys = stmt.getGeneratedKeys();
int i = noRecords - 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 {
@ -90,4 +94,5 @@ public final class GenericFeatureDAOImpl extends AbstractFeatureDAO {
private Connection getConnection() throws FeatureManagerDAOException {
return PolicyManagementDAOFactory.getConnection();
}
}

@ -49,7 +49,7 @@ CREATE TABLE DM_ROLE_GROUP_MAP (
GROUP_ID NUMBER(10) DEFAULT NULL,
ROLE VARCHAR2(45) DEFAULT NULL,
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)
REFERENCES DM_GROUP (ID)
)

Loading…
Cancel
Save