Merge branch 'lasantha' into 'master'

fixing issues with activity retrieval

See merge request entgra/carbon-device-mgt!669
revert-70ac1926
Ruwan Yatawara 4 years ago
commit 2cb0936c66

@ -0,0 +1,42 @@
/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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.device.mgt.common.operation.mgt;
import java.util.List;
public class ActivityHolder {
private List<Activity> activityList;
private List<Integer> largeResponseIDs;
public List<Activity> getActivityList() {
return activityList;
}
public void setActivityList(List<Activity> activityList) {
this.activityList = activityList;
}
public List<Integer> getLargeResponseIDs() {
return largeResponseIDs;
}
public void setLargeResponseIDs(List<Integer> largeResponseIDs) {
this.largeResponseIDs = largeResponseIDs;
}
}

@ -0,0 +1,42 @@
/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. 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.device.mgt.common.operation.mgt;
import java.util.Map;
public class ActivityMapper {
private Activity activity;
private Map<Integer, ActivityStatus> activityStatusMap;
public Activity getActivity() {
return activity;
}
public void setActivity(Activity activity) {
this.activity = activity;
}
public Map<Integer, ActivityStatus> getActivityStatusMap() {
return activityStatusMap;
}
public void setActivityStatusMap(Map<Integer, ActivityStatus> activityStatusMap) {
this.activityStatusMap = activityStatusMap;
}
}

@ -23,6 +23,7 @@ import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityHolder;
import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse;
import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants;
@ -375,6 +376,9 @@ public class GenericOperationDAOImpl implements OperationDAO {
for (Map.Entry<String, List<OperationResponse>> deviceOpRes : deviceOpResponseMap.entrySet()) { for (Map.Entry<String, List<OperationResponse>> deviceOpRes : deviceOpResponseMap.entrySet()) {
for (ActivityStatus status : activityStatuses) { for (ActivityStatus status : activityStatuses) {
if (deviceOpRes.getKey().equalsIgnoreCase(status.getDeviceIdentifier().getId())) { if (deviceOpRes.getKey().equalsIgnoreCase(status.getDeviceIdentifier().getId())) {
if (status.getResponses() == null) {
status.setResponses(new ArrayList<>());
}
status.getResponses().addAll(deviceOpRes.getValue()); status.getResponses().addAll(deviceOpRes.getValue());
} }
} }
@ -942,349 +946,117 @@ public class GenericOperationDAOImpl implements OperationDAO {
@Override @Override
public List<Activity> getActivitiesUpdatedAfterByUser(long timestamp, String user, int limit, int offset) public List<Activity> getActivitiesUpdatedAfterByUser(long timestamp, String user, int limit, int offset)
throws OperationManagementDAOException { throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
List<Activity> activities = new ArrayList<>();
try { try {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
String sql = "SELECT " + String sql = "SELECT " +
" opr.ENROLMENT_ID, " + " eom.ENROLMENT_ID," +
" opr.CREATED_TIMESTAMP, " + " eom.CREATED_TIMESTAMP," +
" opr.UPDATED_TIMESTAMP, " + " eom.UPDATED_TIMESTAMP," +
" opr.OPERATION_ID, " + " eom.OPERATION_ID," +
" opr.OPERATION_CODE, " + " eom.OPERATION_CODE," +
" opr.OPERATION_TYPE, " + " eom.INITIATED_BY," +
" opr.STATUS, " + " eom.TYPE," +
" opr.DEVICE_ID, " + " eom.STATUS," +
" opr.DEVICE_IDENTIFICATION, " + " eom.DEVICE_ID," +
" opr.DEVICE_TYPE, " + " eom.DEVICE_IDENTIFICATION," +
" ops.RECEIVED_TIMESTAMP, " + " eom.DEVICE_TYPE," +
" ops.ID AS OP_RES_ID, " + " opr.ID AS OP_RES_ID," +
" ops.OPERATION_RESPONSE , " + " opr.RECEIVED_TIMESTAMP," +
" ops.IS_LARGE_RESPONSE , " + " opr.OPERATION_RESPONSE," +
" opr.INITIATED_BY " + " opr.IS_LARGE_RESPONSE " +
" FROM " +
" (SELECT " +
" opm.ID MAPPING_ID, " +
" opm.ENROLMENT_ID, " +
" opm.CREATED_TIMESTAMP, " +
" opm.UPDATED_TIMESTAMP, " +
" opm.OPERATION_ID, " +
" op.OPERATION_CODE, " +
" op.INITIATED_BY, " +
" op.TYPE AS OPERATION_TYPE, " +
" opm.STATUS, " +
" en.DEVICE_ID, " +
" de.DEVICE_IDENTIFICATION, " +
" dt.NAME AS DEVICE_TYPE, " +
" de.TENANT_ID " +
"FROM " + "FROM " +
" DM_ENROLMENT_OP_MAPPING opm " + " DM_ENROLMENT_OP_MAPPING eom " +
" INNER JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID " + "LEFT JOIN " +
" INNER JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID " + " DM_DEVICE_OPERATION_RESPONSE opr ON opr.EN_OP_MAP_ID = eom.ID " +
" INNER JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID " + "INNER JOIN " +
" INNER JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID " + " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING ORDER BY OPERATION_ID ASC limit ? , ? ) eom_ordered " +
" WHERE " + " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " +
" opm.UPDATED_TIMESTAMP > ? AND op.INITIATED_BY = ?" +
" AND de.TENANT_ID = ? " +
" ORDER BY opm.UPDATED_TIMESTAMP " +
" LIMIT ? OFFSET ?) opr " +
" LEFT JOIN DM_DEVICE_OPERATION_RESPONSE ops ON opr.MAPPING_ID = ops.EN_OP_MAP_ID " +
"WHERE " + "WHERE " +
" opr.UPDATED_TIMESTAMP > ? AND opr.INITIATED_BY = ?" + " eom.UPDATED_TIMESTAMP > ? " +
" AND opr.TENANT_ID = ? "; " AND eom.TENANT_ID = ? " +
" AND eom.INITIATED_BY = ? " +
stmt = conn.prepareStatement(sql); "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP";
stmt.setLong(1, timestamp); try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(2, user); stmt.setInt(1, offset);
stmt.setInt(3, tenantId); stmt.setInt(2, limit);
stmt.setInt(4, limit); stmt.setLong(3, timestamp);
stmt.setInt(5, offset); stmt.setInt(4, tenantId);
stmt.setLong(6, timestamp); stmt.setString(5, user);
stmt.setString(7, user);
stmt.setInt(8, tenantId); try (ResultSet rs = stmt.executeQuery()) {
ActivityHolder activityHolder = OperationDAOUtil.getActivityHolder(rs);
rs = stmt.executeQuery(); List<Integer> largeResponseIDs = activityHolder.getLargeResponseIDs();
List<Activity> activities = activityHolder.getActivityList();
int operationId = 0;
int enrolmentId = 0;
int responseId = 0;
Activity activity = null;
ActivityStatus activityStatus = null;
List<Integer> largeResponseIDs = new ArrayList<>();
while (rs.next()) {
if (operationId != rs.getInt("OPERATION_ID")) {
activity = new Activity();
activities.add(activity);
List<ActivityStatus> statusList = new ArrayList<>();
activityStatus = new ActivityStatus();
operationId = rs.getInt("OPERATION_ID");
enrolmentId = rs.getInt("ENROLMENT_ID");
activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE")));
activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString());
activity.setCode(rs.getString("OPERATION_CODE"));
activity.setInitiatedBy(rs.getString("INITIATED_BY"));
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION"));
deviceIdentifier.setType(rs.getString("DEVICE_TYPE"));
activityStatus.setDeviceIdentifier(deviceIdentifier);
activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS")));
List<OperationResponse> operationResponses = new ArrayList<>();
if (rs.getInt("UPDATED_TIMESTAMP") != 0) {
activityStatus.setUpdatedTimestamp(new java.util.Date(
rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString());
}
if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) {
responseId = rs.getInt("OP_RES_ID");
if (rs.getBoolean("IS_LARGE_RESPONSE")) {
largeResponseIDs.add(responseId);
} else {
operationResponses.add(OperationDAOUtil.getOperationResponse(rs));
}
}
activityStatus.setResponses(operationResponses);
statusList.add(activityStatus);
activity.setActivityStatus(statusList);
activity.setActivityId(OperationDAOUtil.getActivityId(rs.getInt("OPERATION_ID")));
}
if (operationId == rs.getInt("OPERATION_ID") && enrolmentId != rs.getInt("ENROLMENT_ID")) {
activityStatus = new ActivityStatus();
activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE")));
activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString());
activity.setCode(rs.getString("OPERATION_CODE"));
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION"));
deviceIdentifier.setType(rs.getString("DEVICE_TYPE"));
activityStatus.setDeviceIdentifier(deviceIdentifier);
activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS")));
List<OperationResponse> operationResponses = new ArrayList<>();
if (rs.getInt("UPDATED_TIMESTAMP") != 0) {
activityStatus.setUpdatedTimestamp(new java.util.Date(
rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString());
}
if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) {
responseId = rs.getInt("OP_RES_ID");
if (rs.getBoolean("IS_LARGE_RESPONSE")) {
largeResponseIDs.add(responseId);
} else {
operationResponses.add(OperationDAOUtil.getOperationResponse(rs));
}
}
activityStatus.setResponses(operationResponses);
activity.getActivityStatus().add(activityStatus);
enrolmentId = rs.getInt("ENROLMENT_ID");
}
if (rs.getInt("OP_RES_ID") != 0 && responseId != rs.getInt("OP_RES_ID")) {
if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) {
responseId = rs.getInt("OP_RES_ID");
if (rs.getBoolean("IS_LARGE_RESPONSE")) {
largeResponseIDs.add(responseId);
} else {
activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs));
}
}
}
}
if (!largeResponseIDs.isEmpty()) { if (!largeResponseIDs.isEmpty()) {
populateLargeOperationResponses(activities, largeResponseIDs); populateLargeOperationResponses(activities, largeResponseIDs);
} }
return activities;
}
}
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while getting the operation details from " + String msg = "Error occurred while getting the operation details from the database. ";
"the database. " + e.getMessage(), e); log.error(msg, e);
} finally { throw new OperationManagementDAOException(msg, e);
OperationManagementDAOUtil.cleanupResources(stmt, rs);
} }
return activities;
} }
@Override @Override
public List<Activity> getActivitiesUpdatedAfter(long timestamp, int limit, public List<Activity> getActivitiesUpdatedAfter(long timestamp, int limit, int offset)
int offset) throws OperationManagementDAOException { throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
List<Activity> activities = new ArrayList<>();
List<Integer> largeResponseIDs = new ArrayList<>();
try { try {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
String sql = "SELECT " + String sql = "SELECT " +
" opr.ENROLMENT_ID, " + " eom.ENROLMENT_ID," +
" opr.CREATED_TIMESTAMP, " + " eom.CREATED_TIMESTAMP," +
" opr.UPDATED_TIMESTAMP, " + " eom.UPDATED_TIMESTAMP," +
" opr.OPERATION_ID, " + " eom.OPERATION_ID," +
" opr.OPERATION_CODE, " + " eom.OPERATION_CODE," +
" opr.OPERATION_TYPE, " + " eom.INITIATED_BY," +
" opr.STATUS, " + " eom.TYPE," +
" opr.DEVICE_ID, " + " eom.STATUS," +
" opr.DEVICE_IDENTIFICATION, " + " eom.DEVICE_ID," +
" opr.DEVICE_TYPE, " + " eom.DEVICE_IDENTIFICATION," +
" ops.RECEIVED_TIMESTAMP, " + " eom.DEVICE_TYPE," +
" ops.IS_LARGE_RESPONSE, " +
" ops.ID AS OP_RES_ID," + " ops.ID AS OP_RES_ID," +
" ops.OPERATION_RESPONSE " + " ops.RECEIVED_TIMESTAMP," +
" FROM " + " ops.OPERATION_RESPONSE," +
" (SELECT " + " ops.IS_LARGE_RESPONSE " +
" opm.ID MAPPING_ID, " +
" opm.ENROLMENT_ID, " +
" opm.CREATED_TIMESTAMP, " +
" opm.UPDATED_TIMESTAMP, " +
" opm.OPERATION_ID, " +
" op.OPERATION_CODE, " +
" op.TYPE AS OPERATION_TYPE, " +
" opm.STATUS, " +
" en.DEVICE_ID, " +
" de.DEVICE_IDENTIFICATION, " +
" dt.NAME AS DEVICE_TYPE, " +
" de.TENANT_ID " +
"FROM " + "FROM " +
" DM_ENROLMENT_OP_MAPPING opm " + " DM_ENROLMENT_OP_MAPPING AS eom " +
" INNER JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID " + "INNER JOIN " +
" INNER JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID " + " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING ORDER BY OPERATION_ID ASC limit ? , ? ) AS eom_ordered " +
" INNER JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID " + " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " +
" INNER JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID " + "LEFT JOIN " +
" WHERE " + " DM_DEVICE_OPERATION_RESPONSE AS ops ON ops.EN_OP_MAP_ID = eom.ID " +
" opm.UPDATED_TIMESTAMP > ? " +
" AND de.TENANT_ID = ? " +
" ORDER BY opm.UPDATED_TIMESTAMP " +
" LIMIT ? OFFSET ?) opr " +
" LEFT JOIN DM_DEVICE_OPERATION_RESPONSE ops ON opr.MAPPING_ID = ops.EN_OP_MAP_ID " +
"WHERE " + "WHERE " +
" opr.UPDATED_TIMESTAMP > ? " + " eom.UPDATED_TIMESTAMP > ? " +
" AND opr.TENANT_ID = ? "; " AND eom.TENANT_ID = ? " +
"ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP";
stmt = conn.prepareStatement(sql);
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setLong(1, timestamp); stmt.setInt(1, offset);
stmt.setInt(2, tenantId); stmt.setInt(2, limit);
stmt.setInt(3, limit); stmt.setLong(3, timestamp);
stmt.setInt(4, offset); stmt.setInt(4, tenantId);
stmt.setLong(5, timestamp); try (ResultSet rs = stmt.executeQuery()) {
stmt.setInt(6, tenantId); ActivityHolder activityHolder = OperationDAOUtil.getActivityHolder(rs);
List<Integer> largeResponseIDs = activityHolder.getLargeResponseIDs();
rs = stmt.executeQuery(); List<Activity> activities = activityHolder.getActivityList();
int operationId = 0;
int enrolmentId = 0;
int responseId = 0;
Activity activity = null;
ActivityStatus activityStatus = null;
while (rs.next()) {
if (operationId != rs.getInt("OPERATION_ID")) {
activity = new Activity();
activities.add(activity);
List<ActivityStatus> statusList = new ArrayList<>();
activityStatus = new ActivityStatus();
operationId = rs.getInt("OPERATION_ID");
enrolmentId = rs.getInt("ENROLMENT_ID");
activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE")));
activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString());
activity.setCode(rs.getString("OPERATION_CODE"));
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION"));
deviceIdentifier.setType(rs.getString("DEVICE_TYPE"));
activityStatus.setDeviceIdentifier(deviceIdentifier);
activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS")));
List<OperationResponse> operationResponses = new ArrayList<>();
if (rs.getInt("UPDATED_TIMESTAMP") != 0) {
activityStatus.setUpdatedTimestamp(new java.util.Date(
rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString());
}
if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) {
responseId = rs.getInt("OP_RES_ID");
if (rs.getBoolean("IS_LARGE_RESPONSE")) {
largeResponseIDs.add(responseId);
} else {
operationResponses.add(OperationDAOUtil.getOperationResponse(rs));
}
}
activityStatus.setResponses(operationResponses);
statusList.add(activityStatus);
activity.setActivityStatus(statusList);
activity.setActivityId(OperationDAOUtil.getActivityId(rs.getInt("OPERATION_ID")));
}
if (operationId == rs.getInt("OPERATION_ID") && enrolmentId != rs.getInt("ENROLMENT_ID")) {
activityStatus = new ActivityStatus();
activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE")));
activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString());
activity.setCode(rs.getString("OPERATION_CODE"));
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION"));
deviceIdentifier.setType(rs.getString("DEVICE_TYPE"));
activityStatus.setDeviceIdentifier(deviceIdentifier);
activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS")));
List<OperationResponse> operationResponses = new ArrayList<>();
if (rs.getInt("UPDATED_TIMESTAMP") != 0) {
activityStatus.setUpdatedTimestamp(new java.util.Date(
rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString());
}
if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) {
responseId = rs.getInt("OP_RES_ID");
if (rs.getBoolean("IS_LARGE_RESPONSE")) {
largeResponseIDs.add(responseId);
} else {
operationResponses.add(OperationDAOUtil.getOperationResponse(rs));
}
}
activityStatus.setResponses(operationResponses);
activity.getActivityStatus().add(activityStatus);
enrolmentId = rs.getInt("ENROLMENT_ID");
}
if (rs.getInt("OP_RES_ID") != 0 && responseId != rs.getInt("OP_RES_ID")) {
if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) {
responseId = rs.getInt("OP_RES_ID");
if (rs.getBoolean("IS_LARGE_RESPONSE")) {
largeResponseIDs.add(responseId);
} else {
activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs));
}
}
}
}
if (!largeResponseIDs.isEmpty()) { if (!largeResponseIDs.isEmpty()) {
populateLargeOperationResponses(activities, largeResponseIDs); populateLargeOperationResponses(activities, largeResponseIDs);
} }
return activities;
}
}
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while getting the operation details from " + String msg = "Error occurred while getting the operation details from the database.";
"the database. " + e.getMessage(), e); log.error(msg,e);
} finally { throw new OperationManagementDAOException(msg, e);
OperationManagementDAOUtil.cleanupResources(stmt, rs);
} }
return activities;
} }
@Override @Override
@ -1293,7 +1065,7 @@ 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 WHERE " + String sql = "SELECT COUNT(DISTINCT(OPERATION_ID)) AS COUNT FROM DM_ENROLMENT_OP_MAPPING WHERE " +
"UPDATED_TIMESTAMP > ? AND TENANT_ID = ?"; "UPDATED_TIMESTAMP > ? AND TENANT_ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setLong(1, timestamp); stmt.setLong(1, timestamp);
@ -1317,7 +1089,7 @@ 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 " + String sql = "SELECT COUNT(DISTINCT(OPERATION_ID)) AS COUNT " +
"FROM DM_ENROLMENT_OP_MAPPING AS m " + "FROM DM_ENROLMENT_OP_MAPPING AS m " +
" INNER JOIN " + " INNER JOIN " +
" DM_OPERATION dp ON dp.ID = m.OPERATION_ID " + " DM_OPERATION dp ON dp.ID = m.OPERATION_ID " +
@ -1490,11 +1262,6 @@ public class GenericOperationDAOImpl implements OperationDAO {
operation.setId(rs.getInt("ID")); operation.setId(rs.getInt("ID"));
operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); operation.setType(Operation.Type.valueOf(rs.getString("TYPE")));
operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString());
// if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) {
// operation.setReceivedTimeStamp("");
// } else {
// operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString());
// }
if (rs.getLong("UPDATED_TIMESTAMP") == 0) { if (rs.getLong("UPDATED_TIMESTAMP") == 0) {
operation.setReceivedTimeStamp(""); operation.setReceivedTimeStamp("");
} else { } else {
@ -1692,11 +1459,6 @@ public class GenericOperationDAOImpl implements OperationDAO {
operation.setId(rs.getInt("ID")); operation.setId(rs.getInt("ID"));
operation.setType(Operation.Type.valueOf(rs.getString("TYPE"))); operation.setType(Operation.Type.valueOf(rs.getString("TYPE")));
operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString()); operation.setCreatedTimeStamp(rs.getTimestamp("CREATED_TIMESTAMP").toString());
// if (rs.getTimestamp("RECEIVED_TIMESTAMP") == null) {
// operation.setReceivedTimeStamp("");
// } else {
// operation.setReceivedTimeStamp(rs.getTimestamp("RECEIVED_TIMESTAMP").toString());
// }
if (rs.getLong("UPDATED_TIMESTAMP") == 0) { if (rs.getLong("UPDATED_TIMESTAMP") == 0) {
operation.setReceivedTimeStamp(""); operation.setReceivedTimeStamp("");
} else { } else {

@ -23,6 +23,7 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityHolder;
import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
@ -200,17 +201,11 @@ public class MySQLOperationDAOImpl extends GenericOperationDAOImpl {
} }
@Override @Override
public List<Activity> getActivitiesUpdatedAfter(long timestamp, int limit, public List<Activity> getActivitiesUpdatedAfter(long timestamp, int limit, int offset)
int offset)
throws OperationManagementDAOException { throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
List<Activity> activities = new ArrayList<>();
try { try {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
String sql = "SELECT " + String sql = "SELECT " +
" eom.ENROLMENT_ID," + " eom.ENROLMENT_ID," +
" eom.CREATED_TIMESTAMP," + " eom.CREATED_TIMESTAMP," +
@ -231,140 +226,44 @@ public class MySQLOperationDAOImpl extends GenericOperationDAOImpl {
" DM_ENROLMENT_OP_MAPPING eom FORCE INDEX (IDX_ENROLMENT_OP_MAPPING) " + " DM_ENROLMENT_OP_MAPPING eom FORCE INDEX (IDX_ENROLMENT_OP_MAPPING) " +
"LEFT JOIN " + "LEFT JOIN " +
" DM_DEVICE_OPERATION_RESPONSE opr ON opr.EN_OP_MAP_ID = eom.ID " + " DM_DEVICE_OPERATION_RESPONSE opr ON opr.EN_OP_MAP_ID = eom.ID " +
"INNER JOIN " +
" (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING ORDER BY OPERATION_ID ASC limit ? , ? ) eom_ordered " +
" ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " +
"WHERE " + "WHERE " +
" eom.UPDATED_TIMESTAMP > ? " + " eom.UPDATED_TIMESTAMP > ? " +
" AND eom.TENANT_ID = ? " + " AND eom.TENANT_ID = ? " +
"ORDER BY eom.UPDATED_TIMESTAMP " + "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP";
"LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(sql);
stmt.setLong(1, timestamp);
stmt.setInt(2, tenantId);
stmt.setInt(3, limit);
stmt.setInt(4, offset);
rs = stmt.executeQuery();
int operationId = 0;
int enrolmentId = 0;
int responseId = 0;
Activity activity = null;
ActivityStatus activityStatus = null;
List<Integer> largeResponseIDs = new ArrayList<>();
while (rs.next()) {
if (operationId != rs.getInt("OPERATION_ID")) {
activity = new Activity();
activities.add(activity);
List<ActivityStatus> statusList = new ArrayList<>();
activityStatus = new ActivityStatus();
operationId = rs.getInt("OPERATION_ID");
enrolmentId = rs.getInt("ENROLMENT_ID");
activity.setType(Activity.Type.valueOf(rs.getString("TYPE"))); try (PreparedStatement stmt = conn.prepareStatement(sql)) {
activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); stmt.setInt(1, offset);
activity.setCode(rs.getString("OPERATION_CODE")); stmt.setInt(2, limit);
activity.setInitiatedBy(rs.getString("INITIATED_BY")); stmt.setLong(3, timestamp);
stmt.setInt(4, tenantId);
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); try (ResultSet rs = stmt.executeQuery()) {
deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION")); ActivityHolder activityHolder = OperationDAOUtil.getActivityHolder(rs);
deviceIdentifier.setType(rs.getString("DEVICE_TYPE")); List<Integer> largeResponseIDs = activityHolder.getLargeResponseIDs();
activityStatus.setDeviceIdentifier(deviceIdentifier); List<Activity> activities = activityHolder.getActivityList();
activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS")));
List<OperationResponse> operationResponses = new ArrayList<>();
if (rs.getInt("UPDATED_TIMESTAMP") != 0) {
activityStatus.setUpdatedTimestamp(new java.util.Date(
rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString());
}
if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) {
responseId = rs.getInt("OP_RES_ID");
if (rs.getBoolean("IS_LARGE_RESPONSE")) {
largeResponseIDs.add(responseId);
} else {
operationResponses.add(OperationDAOUtil.getOperationResponse(rs));
}
}
activityStatus.setResponses(operationResponses);
statusList.add(activityStatus);
activity.setActivityStatus(statusList);
activity.setActivityId(OperationDAOUtil.getActivityId(rs.getInt("OPERATION_ID")));
}
if (operationId == rs.getInt("OPERATION_ID") && enrolmentId != rs.getInt("ENROLMENT_ID")) {
activityStatus = new ActivityStatus();
activity.setType(Activity.Type.valueOf(rs.getString("TYPE")));
activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString());
activity.setCode(rs.getString("OPERATION_CODE"));
activity.setInitiatedBy(rs.getString("INITIATED_BY"));
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION"));
deviceIdentifier.setType(rs.getString("DEVICE_TYPE"));
activityStatus.setDeviceIdentifier(deviceIdentifier);
activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS")));
List<OperationResponse> operationResponses = new ArrayList<>();
if (rs.getInt("UPDATED_TIMESTAMP") != 0) {
activityStatus.setUpdatedTimestamp(new java.util.Date(
rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString());
}
if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) {
responseId = rs.getInt("OP_RES_ID");
if (rs.getBoolean("IS_LARGE_RESPONSE")) {
largeResponseIDs.add(responseId);
} else {
operationResponses.add(OperationDAOUtil.getOperationResponse(rs));
}
}
activityStatus.setResponses(operationResponses);
activity.getActivityStatus().add(activityStatus);
enrolmentId = rs.getInt("ENROLMENT_ID");
}
if (rs.getInt("OP_RES_ID") != 0 && responseId != rs.getInt("OP_RES_ID")) {
if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) {
responseId = rs.getInt("OP_RES_ID");
if (rs.getBoolean("IS_LARGE_RESPONSE")) {
largeResponseIDs.add(responseId);
} else {
activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs));
}
}
}
}
if (!largeResponseIDs.isEmpty()) { if (!largeResponseIDs.isEmpty()) {
populateLargeOperationResponses(activities, largeResponseIDs); populateLargeOperationResponses(activities, largeResponseIDs);
} }
return activities;
}
}
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while getting the operation details from " + String msg = "Error occurred while getting the operation details from the database.";
"the database.", e); log.error(msg, e);
} finally { throw new OperationManagementDAOException(msg, e);
OperationManagementDAOUtil.cleanupResources(stmt, rs);
} }
return activities;
} }
@Override @Override
public List<Activity> getActivitiesUpdatedAfterByUser(long timestamp, String user, int limit, int offset) public List<Activity> getActivitiesUpdatedAfterByUser(long timestamp, String user, int limit, int offset)
throws OperationManagementDAOException { throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
List<Activity> activities = new ArrayList<>();
List<Integer> largeResponseIDs = new ArrayList<>();
try { try {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
String sql = "SELECT " + String sql = "SELECT " +
" eom.ENROLMENT_ID," + " eom.ENROLMENT_ID," +
" eom.CREATED_TIMESTAMP," + " eom.CREATED_TIMESTAMP," +
@ -385,136 +284,36 @@ public class MySQLOperationDAOImpl extends GenericOperationDAOImpl {
" DM_ENROLMENT_OP_MAPPING eom FORCE INDEX (IDX_ENROLMENT_OP_MAPPING) " + " DM_ENROLMENT_OP_MAPPING eom FORCE INDEX (IDX_ENROLMENT_OP_MAPPING) " +
"LEFT JOIN " + "LEFT JOIN " +
" DM_DEVICE_OPERATION_RESPONSE opr ON opr.EN_OP_MAP_ID = eom.ID " + " DM_DEVICE_OPERATION_RESPONSE opr ON opr.EN_OP_MAP_ID = eom.ID " +
"INNER JOIN " +
" (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING WHERE INITIATED_BY = ? " +
" ORDER BY OPERATION_ID ASC limit ? , ? ) eom_ordered " +
" ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " +
"WHERE " + "WHERE " +
" eom.UPDATED_TIMESTAMP > ? " + " eom.UPDATED_TIMESTAMP > ? " +
" AND eom.TENANT_ID = ? " + " AND eom.TENANT_ID = ? " +
" AND eom.INITIATED_BY = ? " + "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP";
"ORDER BY eom.UPDATED_TIMESTAMP " +
"LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(sql);
stmt.setLong(1, timestamp);
stmt.setInt(2, tenantId);
stmt.setString(3, user);
stmt.setInt(4, limit);
stmt.setInt(5, offset);
// stmt.setLong(1, timestamp);
// stmt.setString(2, user);
// stmt.setInt(3, tenantId);
// stmt.setInt(4, limit);
// stmt.setInt(5, offset);
// stmt.setLong(6, timestamp);
// stmt.setString(7, user);
// stmt.setInt(8, tenantId);
rs = stmt.executeQuery();
int operationId = 0;
int enrolmentId = 0;
int responseId = 0;
Activity activity = null;
ActivityStatus activityStatus = null;
while (rs.next()) {
if (operationId != rs.getInt("OPERATION_ID")) {
activity = new Activity();
activities.add(activity);
List<ActivityStatus> statusList = new ArrayList<>();
activityStatus = new ActivityStatus();
operationId = rs.getInt("OPERATION_ID");
enrolmentId = rs.getInt("ENROLMENT_ID");
activity.setType(Activity.Type.valueOf(rs.getString("TYPE")));
activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString());
activity.setCode(rs.getString("OPERATION_CODE"));
activity.setInitiatedBy(rs.getString("INITIATED_BY"));
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION"));
deviceIdentifier.setType(rs.getString("DEVICE_TYPE"));
activityStatus.setDeviceIdentifier(deviceIdentifier);
activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS")));
List<OperationResponse> operationResponses = new ArrayList<>();
if (rs.getInt("UPDATED_TIMESTAMP") != 0) {
activityStatus.setUpdatedTimestamp(new java.util.Date(
rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString());
}
if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) {
responseId = rs.getInt("OP_RES_ID");
if (rs.getBoolean("IS_LARGE_RESPONSE")) {
largeResponseIDs.add(responseId);
} else {
operationResponses.add(OperationDAOUtil.getOperationResponse(rs));
}
}
activityStatus.setResponses(operationResponses);
statusList.add(activityStatus);
activity.setActivityStatus(statusList);
activity.setActivityId(OperationDAOUtil.getActivityId(rs.getInt("OPERATION_ID")));
}
if (operationId == rs.getInt("OPERATION_ID") && enrolmentId != rs.getInt("ENROLMENT_ID")) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
activityStatus = new ActivityStatus(); stmt.setString(1, user);
stmt.setInt(2, offset);
activity.setType(Activity.Type.valueOf(rs.getString("TYPE"))); stmt.setInt(3, limit);
activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); stmt.setLong(4, timestamp);
activity.setCode(rs.getString("OPERATION_CODE")); stmt.setInt(5, tenantId);
activity.setInitiatedBy(rs.getString("INITIATED_BY"));
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION"));
deviceIdentifier.setType(rs.getString("DEVICE_TYPE"));
activityStatus.setDeviceIdentifier(deviceIdentifier);
activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS")));
List<OperationResponse> operationResponses = new ArrayList<>();
if (rs.getInt("UPDATED_TIMESTAMP") != 0) {
activityStatus.setUpdatedTimestamp(new java.util.Date(
rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString());
}
if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) {
responseId = rs.getInt("OP_RES_ID");
if (rs.getBoolean("IS_LARGE_RESPONSE")) {
largeResponseIDs.add(responseId);
} else {
operationResponses.add(OperationDAOUtil.getOperationResponse(rs));
}
}
activityStatus.setResponses(operationResponses);
activity.getActivityStatus().add(activityStatus);
enrolmentId = rs.getInt("ENROLMENT_ID");
}
if (rs.getInt("OP_RES_ID") != 0 && responseId != rs.getInt("OP_RES_ID")) { try (ResultSet rs = stmt.executeQuery()) {
if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) { ActivityHolder activityHolder = OperationDAOUtil.getActivityHolder(rs);
responseId = rs.getInt("OP_RES_ID"); List<Integer> largeResponseIDs = activityHolder.getLargeResponseIDs();
if (rs.getBoolean("IS_LARGE_RESPONSE")) { List<Activity> activities = activityHolder.getActivityList();
largeResponseIDs.add(responseId);
} else {
activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs));
}
}
}
}
if (!largeResponseIDs.isEmpty()) { if (!largeResponseIDs.isEmpty()) {
populateLargeOperationResponses(activities, largeResponseIDs); populateLargeOperationResponses(activities, largeResponseIDs);
} }
return activities;
}
}
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException("Error occurred while getting the operation details from " + String msg = "Error occurred while getting the operation details from the database.";
"the database.", e); log.error(msg, e);
} finally { throw new OperationManagementDAOException(msg, e);
OperationManagementDAOUtil.cleanupResources(stmt, rs);
} }
return activities;
} }
} }

@ -18,12 +18,13 @@
package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.operation; package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.operation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityHolder;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMapping; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMapping;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
@ -32,7 +33,6 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOU
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.GenericOperationDAOImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.GenericOperationDAOImpl;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.util.OperationDAOUtil; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.util.OperationDAOUtil;
import java.io.IOException;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
@ -48,6 +48,8 @@ import java.util.Map;
*/ */
public class OracleOperationDAOImpl extends GenericOperationDAOImpl { public class OracleOperationDAOImpl extends GenericOperationDAOImpl {
private static final Log log = LogFactory.getLog(OracleOperationDAOImpl.class);
@Override @Override
public List<? extends Operation> getOperationsForDevice(int enrolmentId, PaginationRequest request) public List<? extends Operation> getOperationsForDevice(int enrolmentId, PaginationRequest request)
throws OperationManagementDAOException { throws OperationManagementDAOException {
@ -185,192 +187,56 @@ public class OracleOperationDAOImpl extends GenericOperationDAOImpl {
@Override @Override
public List<Activity> getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementDAOException { public List<Activity> getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
List<Activity> activities = new ArrayList<>();
List<Integer> largeResponseIDs = new ArrayList<>();
try { try {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
/*String sql = "SELECT opm.ENROLMENT_ID, opm.CREATED_TIMESTAMP, opm.UPDATED_TIMESTAMP, opm.OPERATION_ID,\n"
+ "op.OPERATION_CODE, op.TYPE OPERATION_TYPE, opm.STATUS, en.DEVICE_ID,\n"
+ "ops.RECEIVED_TIMESTAMP, ops.ID OP_RES_ID, ops.OPERATION_RESPONSE,\n"
+ "de.DEVICE_IDENTIFICATION, dt.NAME DEVICE_TYPE\n" + "FROM DM_ENROLMENT_OP_MAPPING opm\n"
+ "LEFT JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID \n"
+ "LEFT JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID \n"
+ "LEFT JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID \n"
+ "LEFT JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID \n"
+ "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE ops ON \n"
+ "opm.ENROLMENT_ID = ops.ENROLMENT_ID AND opm.OPERATION_ID = ops.OPERATION_ID \n"
+ "WHERE opm.UPDATED_TIMESTAMP > ? \n" + "AND de.TENANT_ID = ? \n";
if (timestamp == 0) {
sql += "ORDER BY opm.OPERATION_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
} else {
sql += "ORDER BY opm.UPDATED_TIMESTAMP asc OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
}
*/
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
String sql = "SELECT " + String sql = "SELECT " +
" opr.ENROLMENT_ID, " + " eom.ENROLMENT_ID," +
" opr.CREATED_TIMESTAMP, " + " eom.CREATED_TIMESTAMP," +
" opr.UPDATED_TIMESTAMP, " + " eom.UPDATED_TIMESTAMP," +
" opr.OPERATION_ID, " + " eom.OPERATION_ID," +
" opr.OPERATION_CODE, " + " eom.OPERATION_CODE," +
" opr.OPERATION_TYPE, " + " eom.INITIATED_BY," +
" opr.STATUS, " + " eom.TYPE," +
" opr.DEVICE_ID, " + " eom.STATUS," +
" opr.DEVICE_IDENTIFICATION, " + " eom.DEVICE_ID," +
" opr.DEVICE_TYPE, " + " eom.DEVICE_IDENTIFICATION," +
" eom.DEVICE_TYPE," +
" ops.ID AS OP_RES_ID," +
" ops.RECEIVED_TIMESTAMP," + " ops.RECEIVED_TIMESTAMP," +
" ops.ID OP_RES_ID, " + " ops.OPERATION_RESPONSE," +
" ops.IS_LARGE_RESPONSE, " + " ops.IS_LARGE_RESPONSE " +
" ops.OPERATION_RESPONSE " +
" FROM " +
" (SELECT " +
" opm.ID MAPPING_ID, " +
" opm.ENROLMENT_ID, " +
" opm.CREATED_TIMESTAMP, " +
" opm.UPDATED_TIMESTAMP, " +
" opm.OPERATION_ID, " +
" op.OPERATION_CODE, " +
" op.TYPE OPERATION_TYPE, " +
" opm.STATUS, " +
" en.DEVICE_ID, " +
" de.DEVICE_IDENTIFICATION, " +
" dt.NAME DEVICE_TYPE, " +
" de.TENANT_ID " +
"FROM " + "FROM " +
" DM_ENROLMENT_OP_MAPPING opm " + " DM_ENROLMENT_OP_MAPPING AS eom " +
" INNER JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID " + "INNER JOIN " +
" INNER JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID " + " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING ORDER BY OPERATION_ID ASC limit ? , ? ) AS eom_ordered " +
" INNER JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID " + " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " +
" INNER JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID " + "LEFT JOIN " +
" DM_DEVICE_OPERATION_RESPONSE AS ops ON ops.EN_OP_MAP_ID = eom.ID " +
"WHERE " + "WHERE " +
" opm.UPDATED_TIMESTAMP > ? " + " eom.UPDATED_TIMESTAMP > ? " +
" AND de.TENANT_ID = ? " + " AND eom.TENANT_ID = ? " +
" ORDER BY opm.UPDATED_TIMESTAMP " + "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP";
" OFFSET ? ROWS FETCH NEXT ? ROWS ONLY) opr " +
" LEFT JOIN DM_DEVICE_OPERATION_RESPONSE ops ON opr.MAPPING_ID = ops.EN_OP_MAP_ID " + try (PreparedStatement stmt = conn.prepareStatement(sql)) {
" WHERE " + stmt.setInt(1, offset);
" opr.UPDATED_TIMESTAMP > ? " + stmt.setInt(2, limit);
" AND opr.TENANT_ID = ? "; stmt.setLong(3, timestamp);
stmt.setInt(4, tenantId);
try (ResultSet rs = stmt.executeQuery()) {
stmt = conn.prepareStatement(sql); ActivityHolder activityHolder = OperationDAOUtil.getActivityHolder(rs);
stmt.setLong(1, timestamp); List<Integer> largeResponseIDs = activityHolder.getLargeResponseIDs();
stmt.setInt(2, tenantId); List<Activity> activities = activityHolder.getActivityList();
stmt.setInt(3, offset);
stmt.setInt(4, limit);
stmt.setLong(5, timestamp);
stmt.setInt(6, tenantId);
rs = stmt.executeQuery();
int operationId = 0;
int enrolmentId = 0;
int responseId = 0;
Activity activity = null;
ActivityStatus activityStatus = null;
while (rs.next()) {
if (operationId != rs.getInt("OPERATION_ID")) {
activity = new Activity();
activities.add(activity);
List<ActivityStatus> statusList = new ArrayList<>();
activityStatus = new ActivityStatus();
operationId = rs.getInt("OPERATION_ID");
enrolmentId = rs.getInt("ENROLMENT_ID");
activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE")));
activity.setCreatedTimeStamp(
new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString());
activity.setCode(rs.getString("OPERATION_CODE"));
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION"));
deviceIdentifier.setType(rs.getString("DEVICE_TYPE"));
activityStatus.setDeviceIdentifier(deviceIdentifier);
activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS")));
List<OperationResponse> operationResponses = new ArrayList<>();
if (rs.getInt("UPDATED_TIMESTAMP") != 0) {
activityStatus.setUpdatedTimestamp(
new java.util.Date(rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString());
}
if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) {
responseId = rs.getInt("OP_RES_ID");
if (rs.getBoolean("IS_LARGE_RESPONSE")) {
largeResponseIDs.add(responseId);
} else {
operationResponses.add(OperationDAOUtil.getOperationResponse(rs));
}
}
activityStatus.setResponses(operationResponses);
statusList.add(activityStatus);
activity.setActivityStatus(statusList);
activity.setActivityId(OperationDAOUtil.getActivityId(rs.getInt("OPERATION_ID")));
}
if (operationId == rs.getInt("OPERATION_ID") && enrolmentId != rs.getInt("ENROLMENT_ID")) {
activityStatus = new ActivityStatus();
activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE")));
activity.setCreatedTimeStamp(
new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString());
activity.setCode(rs.getString("OPERATION_CODE"));
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION"));
deviceIdentifier.setType(rs.getString("DEVICE_TYPE"));
activityStatus.setDeviceIdentifier(deviceIdentifier);
activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS")));
List<OperationResponse> operationResponses = new ArrayList<>();
if (rs.getInt("UPDATED_TIMESTAMP") != 0) {
activityStatus.setUpdatedTimestamp(
new java.util.Date(rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString());
}
if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) {
responseId = rs.getInt("OP_RES_ID");
if (rs.getBoolean("IS_LARGE_RESPONSE")) {
largeResponseIDs.add(responseId);
} else {
operationResponses.add(OperationDAOUtil.getOperationResponse(rs));
}
}
activityStatus.setResponses(operationResponses);
activity.getActivityStatus().add(activityStatus);
enrolmentId = rs.getInt("ENROLMENT_ID");
}
if (rs.getInt("OP_RES_ID") != 0 && responseId != rs.getInt("OP_RES_ID")) {
if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) {
responseId = rs.getInt("OP_RES_ID");
if (rs.getBoolean("IS_LARGE_RESPONSE")) {
largeResponseIDs.add(responseId);
} else {
activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs));
}
}
}
}
if (!largeResponseIDs.isEmpty()) { if (!largeResponseIDs.isEmpty()) {
populateLargeOperationResponses(activities, largeResponseIDs); populateLargeOperationResponses(activities, largeResponseIDs);
} }
return activities;
}
}
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException( String msg = "Error occurred while getting the operation details from the database.";
"Error occurred while getting the operation details from " + "the database.", e); log.error(msg, e);
} finally { throw new OperationManagementDAOException(msg, e);
OperationManagementDAOUtil.cleanupResources(stmt, rs);
} }
return activities;
} }
} }

@ -41,6 +41,7 @@ import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityHolder;
import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation;
@ -51,7 +52,6 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOU
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.GenericOperationDAOImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl.GenericOperationDAOImpl;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.util.OperationDAOUtil; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.util.OperationDAOUtil;
import java.io.IOException;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
@ -67,7 +67,7 @@ import java.util.Map;
*/ */
public class SQLServerOperationDAOImpl extends GenericOperationDAOImpl { public class SQLServerOperationDAOImpl extends GenericOperationDAOImpl {
private static final Log log = LogFactory.getLog(GenericOperationDAOImpl.class); private static final Log log = LogFactory.getLog(SQLServerOperationDAOImpl.class);
@Override @Override
public List<? extends Operation> getOperationsForDevice(int enrolmentId, PaginationRequest request) public List<? extends Operation> getOperationsForDevice(int enrolmentId, PaginationRequest request)
@ -162,192 +162,59 @@ public class SQLServerOperationDAOImpl extends GenericOperationDAOImpl {
} }
@Override @Override
public List<Activity> getActivitiesUpdatedAfter(long timestamp, int limit, int offset) throws OperationManagementDAOException { public List<Activity> getActivitiesUpdatedAfter(long timestamp, int limit, int offset)
PreparedStatement stmt = null; throws OperationManagementDAOException {
ResultSet rs = null;
List<Activity> activities = new ArrayList<>();
List<Integer> largeResponseIDs = new ArrayList<>();
try { try {
Connection conn = OperationManagementDAOFactory.getConnection(); Connection conn = OperationManagementDAOFactory.getConnection();
/*String sql = "SELECT opm.ENROLMENT_ID, opm.CREATED_TIMESTAMP, opm.UPDATED_TIMESTAMP, opm.OPERATION_ID,\n"
+ "op.OPERATION_CODE, op.TYPE OPERATION_TYPE, opm.STATUS, en.DEVICE_ID,\n"
+ "ops.RECEIVED_TIMESTAMP, ops.ID OP_RES_ID, ops.OPERATION_RESPONSE,\n"
+ "de.DEVICE_IDENTIFICATION, dt.NAME DEVICE_TYPE\n" + "FROM DM_ENROLMENT_OP_MAPPING opm\n"
+ "LEFT JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID \n"
+ "LEFT JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID \n"
+ "LEFT JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID \n"
+ "LEFT JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID \n"
+ "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE ops ON \n"
+ "opm.ENROLMENT_ID = ops.ENROLMENT_ID AND opm.OPERATION_ID = ops.OPERATION_ID \n"
+ "WHERE opm.UPDATED_TIMESTAMP > ? \n" + "AND de.TENANT_ID = ? \n";
if (timestamp == 0) {
sql += "ORDER BY opm.OPERATION_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
} else {
sql += "ORDER BY opm.UPDATED_TIMESTAMP asc OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
}*/
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
String sql = "SELECT " + String sql = "SELECT " +
" opr.ENROLMENT_ID, " + " eom.ENROLMENT_ID," +
" opr.CREATED_TIMESTAMP, " + " eom.CREATED_TIMESTAMP," +
" opr.UPDATED_TIMESTAMP, " + " eom.UPDATED_TIMESTAMP," +
" opr.OPERATION_ID, " + " eom.OPERATION_ID," +
" opr.OPERATION_CODE, " + " eom.OPERATION_CODE," +
" opr.OPERATION_TYPE, " + " eom.INITIATED_BY," +
" opr.STATUS, " + " eom.TYPE," +
" opr.DEVICE_ID, " + " eom.STATUS," +
" opr.DEVICE_IDENTIFICATION, " + " eom.DEVICE_ID," +
" opr.DEVICE_TYPE, " + " eom.DEVICE_IDENTIFICATION," +
" eom.DEVICE_TYPE," +
" ops.ID AS OP_RES_ID," +
" ops.RECEIVED_TIMESTAMP," + " ops.RECEIVED_TIMESTAMP," +
" ops.ID OP_RES_ID, " + " ops.OPERATION_RESPONSE," +
" ops.IS_LARGE_RESPONSE, " + " ops.IS_LARGE_RESPONSE " +
" ops.OPERATION_RESPONSE " +
" FROM " +
" (SELECT " +
" opm.ID MAPPING_ID, " +
" opm.ENROLMENT_ID, " +
" opm.CREATED_TIMESTAMP, " +
" opm.UPDATED_TIMESTAMP, " +
" opm.OPERATION_ID, " +
" op.OPERATION_CODE, " +
" op.TYPE OPERATION_TYPE, " +
" opm.STATUS, " +
" en.DEVICE_ID, " +
" de.DEVICE_IDENTIFICATION, " +
" dt.NAME DEVICE_TYPE, " +
" de.TENANT_ID " +
"FROM " + "FROM " +
" DM_ENROLMENT_OP_MAPPING opm " + " DM_ENROLMENT_OP_MAPPING AS eom " +
" INNER JOIN DM_OPERATION op ON opm.OPERATION_ID = op.ID " + "INNER JOIN " +
" INNER JOIN DM_ENROLMENT en ON opm.ENROLMENT_ID = en.ID " + " (SELECT DISTINCT OPERATION_ID FROM DM_ENROLMENT_OP_MAPPING ORDER BY OPERATION_ID ASC limit ? , ? ) AS eom_ordered " +
" INNER JOIN DM_DEVICE de ON en.DEVICE_ID = de.ID " + " ON eom_ordered.OPERATION_ID = eom.OPERATION_ID " +
" INNER JOIN DM_DEVICE_TYPE dt ON dt.ID = de.DEVICE_TYPE_ID " + "LEFT JOIN " +
" DM_DEVICE_OPERATION_RESPONSE AS ops ON ops.EN_OP_MAP_ID = eom.ID " +
"WHERE " + "WHERE " +
" opm.UPDATED_TIMESTAMP > ? " + " eom.UPDATED_TIMESTAMP > ? " +
" AND de.TENANT_ID = ? " + " AND eom.TENANT_ID = ? " +
" ORDER BY opm.UPDATED_TIMESTAMP " + "ORDER BY eom.OPERATION_ID, eom.UPDATED_TIMESTAMP";
" OFFSET ? ROWS FETCH NEXT ? ROWS ONLY) opr " +
" LEFT JOIN DM_DEVICE_OPERATION_RESPONSE ops ON opr.MAPPING_ID = ops.EN_OP_MAP_ID " +
" WHERE" +
" opr.UPDATED_TIMESTAMP > ? " +
" AND opr.TENANT_ID = ? ";
stmt = conn.prepareStatement(sql);
stmt.setLong(1, timestamp);
stmt.setInt(2, tenantId);
stmt.setInt(3, offset);
stmt.setInt(4, limit);
stmt.setLong(5, timestamp);
stmt.setInt(6, tenantId);
rs = stmt.executeQuery();
int operationId = 0;
int enrolmentId = 0;
int responseId = 0;
Activity activity = null;
ActivityStatus activityStatus = null;
while (rs.next()) {
if (operationId != rs.getInt("OPERATION_ID")) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
activity = new Activity(); stmt.setInt(1, offset);
activities.add(activity); stmt.setInt(2, limit);
List<ActivityStatus> statusList = new ArrayList<>(); stmt.setLong(3, timestamp);
activityStatus = new ActivityStatus(); stmt.setInt(4, tenantId);
try (ResultSet rs = stmt.executeQuery()) {
operationId = rs.getInt("OPERATION_ID"); ActivityHolder activityHolder = OperationDAOUtil.getActivityHolder(rs);
enrolmentId = rs.getInt("ENROLMENT_ID"); List<Integer> largeResponseIDs = activityHolder.getLargeResponseIDs();
List<Activity> activities = activityHolder.getActivityList();
activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE")));
activity.setCreatedTimeStamp(
new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString());
activity.setCode(rs.getString("OPERATION_CODE"));
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION"));
deviceIdentifier.setType(rs.getString("DEVICE_TYPE"));
activityStatus.setDeviceIdentifier(deviceIdentifier);
activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS")));
List<OperationResponse> operationResponses = new ArrayList<>();
if (rs.getInt("UPDATED_TIMESTAMP") != 0) {
activityStatus.setUpdatedTimestamp(
new java.util.Date(rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString());
}
if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) {
responseId = rs.getInt("OP_RES_ID");
if (rs.getBoolean("IS_LARGE_RESPONSE")) {
largeResponseIDs.add(responseId);
} else {
operationResponses.add(OperationDAOUtil.getOperationResponse(rs));
}
}
activityStatus.setResponses(operationResponses);
statusList.add(activityStatus);
activity.setActivityStatus(statusList);
activity.setActivityId(OperationDAOUtil.getActivityId(rs.getInt("OPERATION_ID")));
}
if (operationId == rs.getInt("OPERATION_ID") && enrolmentId != rs.getInt("ENROLMENT_ID")) {
activityStatus = new ActivityStatus();
activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE")));
activity.setCreatedTimeStamp(
new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString());
activity.setCode(rs.getString("OPERATION_CODE"));
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION"));
deviceIdentifier.setType(rs.getString("DEVICE_TYPE"));
activityStatus.setDeviceIdentifier(deviceIdentifier);
activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS")));
List<OperationResponse> operationResponses = new ArrayList<>();
if (rs.getInt("UPDATED_TIMESTAMP") != 0) {
activityStatus.setUpdatedTimestamp(
new java.util.Date(rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString());
}
if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) {
responseId = rs.getInt("OP_RES_ID");
if (rs.getBoolean("IS_LARGE_RESPONSE")) {
largeResponseIDs.add(responseId);
} else {
operationResponses.add(OperationDAOUtil.getOperationResponse(rs));
}
}
activityStatus.setResponses(operationResponses);
activity.getActivityStatus().add(activityStatus);
enrolmentId = rs.getInt("ENROLMENT_ID");
}
if (rs.getInt("OP_RES_ID") != 0 && responseId != rs.getInt("OP_RES_ID")) {
if (rs.getTimestamp("RECEIVED_TIMESTAMP") != (null)) {
responseId = rs.getInt("OP_RES_ID");
if (rs.getBoolean("IS_LARGE_RESPONSE")) {
largeResponseIDs.add(responseId);
} else {
activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs));
}
}
}
}
if (!largeResponseIDs.isEmpty()) { if (!largeResponseIDs.isEmpty()) {
populateLargeOperationResponses(activities, largeResponseIDs); populateLargeOperationResponses(activities, largeResponseIDs);
} }
return activities;
}
}
} catch (SQLException e) { } catch (SQLException e) {
throw new OperationManagementDAOException( String msg = "Error occurred while getting the operation details from the database.";
"Error occurred while getting the operation details from " + "the database.", e); log.error(msg, e);
} finally { throw new OperationManagementDAOException(msg, e);
OperationManagementDAOUtil.cleanupResources(stmt, rs);
} }
return activities;
} }
@Override @Override

@ -20,6 +20,11 @@ package org.wso2.carbon.device.mgt.core.operation.mgt.dao.util;
import org.apache.commons.logging.Log; 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.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityHolder;
import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityMapper;
import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationResponse;
import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation;
@ -27,12 +32,17 @@ import org.wso2.carbon.device.mgt.core.dto.operation.mgt.ConfigOperation;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.PolicyOperation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.PolicyOperation;
import org.wso2.carbon.device.mgt.core.dto.operation.mgt.ProfileOperation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.ProfileOperation;
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class OperationDAOUtil { public class OperationDAOUtil {
private static final Log log = LogFactory.getLog(OperationDAOUtil.class); private static final Log log = LogFactory.getLog(OperationDAOUtil.class);
@ -178,8 +188,113 @@ public class OperationDAOUtil {
operation.setActivityId(DeviceManagementConstants.OperationAttributes.ACTIVITY + operationId); operation.setActivityId(DeviceManagementConstants.OperationAttributes.ACTIVITY + operationId);
} }
public static String getActivityId(int operationId) { public static String getActivityId(int operationId) {
return DeviceManagementConstants.OperationAttributes.ACTIVITY + operationId; return DeviceManagementConstants.OperationAttributes.ACTIVITY + operationId;
} }
public static ActivityHolder getActivityHolder(ResultSet rs) throws OperationManagementDAOException {
Map<Integer, ActivityMapper> activityInfo = new TreeMap<>();
List<Integer> largeResponseIDs = new ArrayList<>();
List<Activity> activities = new ArrayList<>();
try {
while (rs.next()) {
ActivityMapper activityMapper = activityInfo.get(rs.getInt("OPERATION_ID"));
if (activityMapper != null) {
ActivityStatus activityStatus = activityMapper.getActivityStatusMap()
.get(rs.getInt("ENROLMENT_ID"));
if (activityStatus == null) {
activityStatus = new ActivityStatus();
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION"));
deviceIdentifier.setType(rs.getString("DEVICE_TYPE"));
activityStatus.setDeviceIdentifier(deviceIdentifier);
activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS")));
if (rs.getInt("UPDATED_TIMESTAMP") != 0) {
activityStatus.setUpdatedTimestamp(
new java.util.Date(rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString());
}
if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) {
if (rs.getBoolean("IS_LARGE_RESPONSE")) {
largeResponseIDs.add(rs.getInt("OP_RES_ID"));
} else {
List<OperationResponse> operationResponses = new ArrayList<>();
operationResponses.add(OperationDAOUtil.getOperationResponse(rs));
activityStatus.setResponses(operationResponses);
}
}
activityMapper.getActivityStatusMap().put(rs.getInt("ENROLMENT_ID"), activityStatus);
} else {
if (rs.getInt("OP_RES_ID") != 0 && rs.getTimestamp("RECEIVED_TIMESTAMP") != null) {
if (rs.getBoolean("IS_LARGE_RESPONSE")) {
largeResponseIDs.add(rs.getInt("OP_RES_ID"));
} else {
activityStatus.getResponses().add(OperationDAOUtil.getOperationResponse(rs));
}
}
}
} else {
Activity activity = new Activity();
List<ActivityStatus> statusList = new ArrayList<>();
ActivityStatus activityStatus = new ActivityStatus();
activity.setType(Activity.Type.valueOf(rs.getString("TYPE")));
activity.setCreatedTimeStamp(
new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString());
activity.setCode(rs.getString("OPERATION_CODE"));
activity.setInitiatedBy(rs.getString("INITIATED_BY"));
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(rs.getString("DEVICE_IDENTIFICATION"));
deviceIdentifier.setType(rs.getString("DEVICE_TYPE"));
activityStatus.setDeviceIdentifier(deviceIdentifier);
activityStatus.setStatus(ActivityStatus.Status.valueOf(rs.getString("STATUS")));
List<OperationResponse> operationResponses = new ArrayList<>();
if (rs.getInt("UPDATED_TIMESTAMP") != 0) {
activityStatus.setUpdatedTimestamp(
new java.util.Date(rs.getLong(("UPDATED_TIMESTAMP")) * 1000).toString());
}
if (rs.getTimestamp("RECEIVED_TIMESTAMP") != null) {
if (rs.getBoolean("IS_LARGE_RESPONSE")) {
largeResponseIDs.add(rs.getInt("OP_RES_ID"));
} else {
operationResponses.add(OperationDAOUtil.getOperationResponse(rs));
}
}
activityStatus.setResponses(operationResponses);
statusList.add(activityStatus);
activity.setActivityStatus(statusList);
activity.setActivityId(getActivityId(rs.getInt("OPERATION_ID")));
ActivityMapper newActivityMapper = new ActivityMapper();
Map<Integer, ActivityStatus> activityStatusMap = new TreeMap<>();
activityStatusMap.put(rs.getInt("ENROLMENT_ID"), activityStatus);
newActivityMapper.setActivity(activity);
newActivityMapper.setActivityStatusMap(activityStatusMap);
activityInfo.put(rs.getInt("OPERATION_ID"), newActivityMapper);
}
}
activityInfo.values().forEach(holder -> {
List<ActivityStatus> activityStatuses = new ArrayList<>(holder.getActivityStatusMap().values());
holder.getActivity().setActivityStatus(activityStatuses);
activities.add(holder.getActivity());
});
ActivityHolder activityHolder = new ActivityHolder();
activityHolder.setActivityList(activities);
activityHolder.setLargeResponseIDs(largeResponseIDs);
return activityHolder;
} catch (SQLException e) {
String msg = "Error occurred while getting activity data from the result set.";
log.error(msg, e);
throw new OperationManagementDAOException(msg, e);
}
}
} }

Loading…
Cancel
Save