From 29d6b38d1530b37d3b1f420fe92acf1f36d04c85 Mon Sep 17 00:00:00 2001 From: Chatura Dilan Perera Date: Mon, 29 Aug 2016 13:57:32 +0530 Subject: [PATCH 1/6] Fixed issue in activity getting with response --- .../mgt/dao/impl/GenericOperationDAOImpl.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index 173215e31a..9396124390 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -409,14 +409,15 @@ public class GenericOperationDAOImpl implements OperationDAO { String sql = "SELECT opm.ENROLMENT_ID, opm.CREATED_TIMESTAMP, opm.UPDATED_TIMESTAMP, opm.OPERATION_ID,\n" + "op.OPERATION_CODE, op.TYPE as OPERATION_TYPE, opm.STATUS, en.DEVICE_ID,\n" + - "op.RECEIVED_TIMESTAMP, ops.ID as OP_RES_ID, ops.OPERATION_RESPONSE,\n" + + "ops.RECEIVED_TIMESTAMP, ops.ID as OP_RES_ID, ops.OPERATION_RESPONSE,\n" + "de.DEVICE_IDENTIFICATION, dt.NAME as DEVICE_TYPE\n" + "FROM DM_ENROLMENT_OP_MAPPING AS opm\n" + - "LEFT JOIN DM_OPERATION AS op ON opm.OPERATION_ID = op.ID\n" + - "LEFT JOIN DM_ENROLMENT as en ON opm.ENROLMENT_ID = en.ID\n" + - "LEFT JOIN DM_DEVICE as de ON en.DEVICE_ID = de.ID\n" + - "LEFT JOIN DM_DEVICE_TYPE as dt ON dt.ID = de.DEVICE_TYPE_ID\n" + - "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE as ops ON ops.ID = opm.OPERATION_ID\n" + + "LEFT JOIN DM_OPERATION AS op ON opm.OPERATION_ID = op.ID \n" + + "LEFT JOIN DM_ENROLMENT as en ON opm.ENROLMENT_ID = en.ID \n" + + "LEFT JOIN DM_DEVICE as de ON en.DEVICE_ID = de.ID \n" + + "LEFT JOIN DM_DEVICE_TYPE as dt ON dt.ID = de.DEVICE_TYPE_ID \n" + + "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE as ops ON \n" + + "opm.ENROLMENT_ID = opm.ENROLMENT_ID AND opm.OPERATION_ID = ops.OPERATION_ID \n" + "WHERE opm.UPDATED_TIMESTAMP > ? \n" + "AND de.TENANT_ID = ? \n"; From 83442b0bca4e49bb9d7a814da5b0981c34c8ccae Mon Sep 17 00:00:00 2001 From: Chatura Dilan Perera Date: Mon, 29 Aug 2016 15:49:12 +0530 Subject: [PATCH 2/6] Fixed issue in activity retrival --- .../core/operation/mgt/dao/impl/GenericOperationDAOImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index 9396124390..8a16220032 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -417,7 +417,7 @@ public class GenericOperationDAOImpl implements OperationDAO { "LEFT JOIN DM_DEVICE as de ON en.DEVICE_ID = de.ID \n" + "LEFT JOIN DM_DEVICE_TYPE as dt ON dt.ID = de.DEVICE_TYPE_ID \n" + "LEFT JOIN DM_DEVICE_OPERATION_RESPONSE as ops ON \n" + - "opm.ENROLMENT_ID = opm.ENROLMENT_ID AND opm.OPERATION_ID = ops.OPERATION_ID \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"; From 475b832c5942e1937c5becac82f523d0896fe4a0 Mon Sep 17 00:00:00 2001 From: Chatura Dilan Perera Date: Mon, 29 Aug 2016 18:50:23 +0530 Subject: [PATCH 3/6] Changed return date format to UTC date --- .../carbon/device/mgt/jaxrs/common/GsonMessageBodyHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/common/GsonMessageBodyHandler.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/common/GsonMessageBodyHandler.java index d2f9d2113f..57e5c33e7e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/common/GsonMessageBodyHandler.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/common/GsonMessageBodyHandler.java @@ -50,7 +50,7 @@ public class GsonMessageBodyHandler implements MessageBodyWriter, Messag private Gson getGson() { if (gson == null) { final GsonBuilder gsonBuilder = new GsonBuilder(); - gson = gsonBuilder.create(); + gson = gsonBuilder.setDateFormat("EEE, d MMM yyyy HH:mm:ss Z").create(); } return gson; } From 9aa5f2793f38f01625c91b35d80c070da633d42a Mon Sep 17 00:00:00 2001 From: Chatura Dilan Perera Date: Tue, 30 Aug 2016 14:17:06 +0530 Subject: [PATCH 4/6] return date format as UTC in APIs --- .../carbon/device/mgt/jaxrs/common/GsonMessageBodyHandler.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/common/GsonMessageBodyHandler.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/common/GsonMessageBodyHandler.java index 57e5c33e7e..f069edee75 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/common/GsonMessageBodyHandler.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/common/GsonMessageBodyHandler.java @@ -40,6 +40,7 @@ import static javax.ws.rs.core.MediaType.APPLICATION_JSON; @Consumes(APPLICATION_JSON) public class GsonMessageBodyHandler implements MessageBodyWriter, MessageBodyReader { + public static final String DATE_FORMAT = "EEE, d MMM yyyy HH:mm:ss Z"; private Gson gson; private static final String UTF_8 = "UTF-8"; @@ -50,7 +51,7 @@ public class GsonMessageBodyHandler implements MessageBodyWriter, Messag private Gson getGson() { if (gson == null) { final GsonBuilder gsonBuilder = new GsonBuilder(); - gson = gsonBuilder.setDateFormat("EEE, d MMM yyyy HH:mm:ss Z").create(); + gson = gsonBuilder.setDateFormat(DATE_FORMAT).create(); } return gson; } From a77564ac2d0781d7c7bb99f57c3564769183bb6e Mon Sep 17 00:00:00 2001 From: harshanl Date: Tue, 30 Aug 2016 14:53:56 +0530 Subject: [PATCH 5/6] Added status for each device in Activity response --- .../common/operation/mgt/ActivityStatus.java | 2 +- .../operation/mgt/OperationManagerImpl.java | 341 +++++++++++------- .../mgt/core/util/DeviceManagerUtil.java | 6 +- 3 files changed, 212 insertions(+), 137 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/ActivityStatus.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/ActivityStatus.java index c706ee7a71..28aeb08a76 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/ActivityStatus.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/ActivityStatus.java @@ -30,7 +30,7 @@ import java.util.List; public class ActivityStatus { public enum Status { - IN_PROGRESS, PENDING, COMPLETED, ERROR, REPEATED + IN_PROGRESS, PENDING, COMPLETED, ERROR, REPEATED, INVALID, UNAUTHORIZED } @ApiModelProperty( diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 04a7d71322..97c04c0d6a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -24,10 +24,7 @@ import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.common.*; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants; -import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; -import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; -import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; -import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; +import org.wso2.carbon.device.mgt.common.operation.mgt.*; import org.wso2.carbon.device.mgt.common.push.notification.NotificationContext; import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationExecutionFailedException; @@ -90,22 +87,29 @@ public class OperationManagerImpl implements OperationManager { @Override public Activity addOperation(Operation operation, - List deviceIds) throws OperationManagementException, InvalidDeviceException { + List deviceIds) + throws OperationManagementException, InvalidDeviceException { if (log.isDebugEnabled()) { log.debug("operation:[" + operation.toString() + "]"); for (DeviceIdentifier deviceIdentifier : deviceIds) { log.debug("device identifier id:[" + deviceIdentifier.getId() + "] type:[" + - deviceIdentifier.getType() + "]"); + deviceIdentifier.getType() + "]"); } } try { - DeviceIDHolder deviceIDHolder = DeviceManagerUtil.validateDeviceIdentifiers(deviceIds); - List validDeviceIds = deviceIDHolder.getValidDeviceIDList(); + DeviceIDHolder deviceValidationResult = DeviceManagerUtil.validateDeviceIdentifiers(deviceIds); + List validDeviceIds = deviceValidationResult.getValidDeviceIDList(); if (validDeviceIds.size() > 0) { - List authorizedDeviceList = this.getAuthorizedDevices(operation, validDeviceIds); + DeviceIDHolder deviceAuthorizationResult = this.authorizeDevices(operation, validDeviceIds); + List authorizedDeviceList = deviceAuthorizationResult.getValidDeviceIDList(); if (authorizedDeviceList.size() <= 0) { log.info("User : " + getUser() + " is not authorized to perform operations on given device-list."); - return null; + Activity activity = new Activity(); + //Send the operation statuses only for admin triggered operations + String deviceType = validDeviceIds.get(0).getType(); + activity.setActivityStatus(this.getActivityStatus(deviceValidationResult, deviceAuthorizationResult, + deviceType)); + return activity; } OperationManagementDAOFactory.beginTransaction(); @@ -117,7 +121,7 @@ public class OperationManagerImpl implements OperationManager { boolean hasExistingTaskOperation; int enrolmentId; if (operationDto.getControl() == - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Control.NO_REPEAT) { + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Control.NO_REPEAT) { isNotRepeated = true; } @@ -134,8 +138,8 @@ public class OperationManagerImpl implements OperationManager { } } else if (isNotRepeated) { operationDAO.updateEnrollmentOperationsStatus(enrolmentId, operationCode, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.REPEATED); + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING, + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.REPEATED); operationMappingDAO.addOperationMapping(operationId, enrolmentId); } else { operationMappingDAO.addOperationMapping(operationId, enrolmentId); @@ -145,8 +149,8 @@ public class OperationManagerImpl implements OperationManager { notificationStrategy.execute(new NotificationContext(deviceId, operation)); } catch (PushNotificationExecutionFailedException e) { log.error("Error occurred while sending push notifications to " + - deviceId.getType() + " device carrying id '" + - deviceId + "'", e); + deviceId.getType() + " device carrying id '" + + deviceId + "'", e); } } } @@ -157,6 +161,12 @@ public class OperationManagerImpl implements OperationManager { activity.setCode(operationCode); activity.setCreatedTimeStamp(new Date().toString()); activity.setType(Activity.Type.valueOf(operationDto.getType().toString())); + //For now set the operation statuses only for admin triggered operations + if (!isScheduledOperation) { + //Get the device-type from 1st valid DeviceIdentifier. We know the 1st element is definitely there. + String deviceType = validDeviceIds.get(0).getType(); + activity.setActivityStatus(this.getActivityStatus(deviceValidationResult, deviceAuthorizationResult, deviceType)); + } return activity; } else { throw new InvalidDeviceException("Invalid device Identifiers found."); @@ -171,21 +181,64 @@ public class OperationManagerImpl implements OperationManager { } } - private List getAuthorizedDevices( + private List getActivityStatus(DeviceIDHolder deviceIdValidationResult, DeviceIDHolder deviceAuthResult, + String deviceType) { + List activityStatuses = new ArrayList<>(); + ActivityStatus activityStatus; + //Add the valid DeviceIds + for (DeviceIdentifier id : deviceIdValidationResult.getValidDeviceIDList()) { + activityStatus = new ActivityStatus(); + activityStatus.setDeviceIdentifier(id); + activityStatus.setStatus(ActivityStatus.Status.PENDING); + activityStatuses.add(activityStatus); + } + + //Add the invalid DeviceIds + for (String id : deviceIdValidationResult.getErrorDeviceIdList()) { + activityStatus = new ActivityStatus(); + activityStatus.setDeviceIdentifier(new DeviceIdentifier(id,deviceType)); + activityStatus.setStatus(ActivityStatus.Status.INVALID); + activityStatuses.add(activityStatus); + } + + //Add the unauthorized DeviceIds + for (String id : deviceAuthResult.getErrorDeviceIdList()) { + activityStatus = new ActivityStatus(); + activityStatus.setDeviceIdentifier(new DeviceIdentifier(id, deviceType)); + activityStatus.setStatus(ActivityStatus.Status.UNAUTHORIZED); + activityStatuses.add(activityStatus); + } + return activityStatuses; + } + + private DeviceIDHolder authorizeDevices( Operation operation, List deviceIds) throws OperationManagementException { List authorizedDeviceList; + List unAuthorizedDeviceList = new ArrayList<>(); + DeviceIDHolder deviceIDHolder = new DeviceIDHolder(); try { if (operation != null && isAuthenticationSkippedOperation(operation)) { authorizedDeviceList = deviceIds; } else { - authorizedDeviceList = DeviceManagementDataHolder.getInstance(). - getDeviceAccessAuthorizationService().isUserAuthorized(deviceIds).getAuthorizedDevices(); + boolean isAuthorized; + authorizedDeviceList = new ArrayList<>(); + for (DeviceIdentifier devId : deviceIds) { + isAuthorized = DeviceManagementDataHolder.getInstance().getDeviceAccessAuthorizationService(). + isUserAuthorized(devId); + if (isAuthorized) { + authorizedDeviceList.add(devId); + } else { + unAuthorizedDeviceList.add(devId.getId()); + } + } } } catch (DeviceAccessAuthorizationException e) { throw new OperationManagementException("Error occurred while authorizing access to the devices for user :" + - this.getUser(), e); + this.getUser(), e); } - return authorizedDeviceList; + deviceIDHolder.setValidDeviceIDList(authorizedDeviceList); + deviceIDHolder.setErrorDeviceIdList(unAuthorizedDeviceList); + return deviceIDHolder; } private Device getDevice(DeviceIdentifier deviceId) throws OperationManagementException { @@ -195,7 +248,7 @@ public class OperationManagerImpl implements OperationManager { return deviceDAO.getDevice(deviceId, tenantId); } catch (SQLException e) { throw new OperationManagementException("Error occurred while opening a connection the data " + - "source", e); + "source", e); } catch (DeviceManagementDAOException e) { OperationManagementDAOFactory.rollbackTransaction(); throw new OperationManagementException( @@ -211,7 +264,8 @@ public class OperationManagerImpl implements OperationManager { if (!isActionAuthorized(deviceId)) { throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" + - deviceId.getType() + "' device, which carries the identifier '" + deviceId.getId() + "'"); + deviceId.getType() + "' device, which carries the identifier '" + + deviceId.getId() + "'"); } int enrolmentId = this.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE); @@ -231,8 +285,8 @@ public class OperationManagerImpl implements OperationManager { } } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the list of " + - "operations assigned for '" + deviceId.getType() + - "' device '" + deviceId.getId() + "'", e); + "operations assigned for '" + deviceId.getType() + + "' device '" + deviceId.getId() + "'", e); } catch (SQLException e) { throw new OperationManagementException( "Error occurred while opening a connection to the data source", e); @@ -250,14 +304,15 @@ public class OperationManagerImpl implements OperationManager { if (!isActionAuthorized(deviceId)) { throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" + - deviceId.getType() + "' device, which carries the identifier '" + deviceId.getId() + "'"); + deviceId.getType() + "' device, which carries the identifier '" + + deviceId.getId() + "'"); } int enrolmentId = this.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE); if (enrolmentId < 0) { throw new OperationManagementException("Device not found for given device " + - "Identifier:" + deviceId.getId() + " and given type" + - deviceId.getType()); + "Identifier:" + deviceId.getId() + " and given type" + + deviceId.getType()); } try { @@ -275,8 +330,8 @@ public class OperationManagerImpl implements OperationManager { paginationResult.setRecordsFiltered(count); } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the list of " + - "operations assigned for '" + deviceId.getType() + - "' device '" + deviceId.getId() + "'", e); + "operations assigned for '" + deviceId.getType() + + "' device '" + deviceId.getId() + "'", e); } catch (SQLException e) { throw new OperationManagementException( "Error occurred while opening a connection to the data source", e); @@ -289,7 +344,7 @@ public class OperationManagerImpl implements OperationManager { @Override public List getPendingOperations(DeviceIdentifier deviceId) throws - OperationManagementException { + OperationManagementException { if (log.isDebugEnabled()) { log.debug("Device identifier id:[" + deviceId.getId() + "] type:[" + deviceId.getType() + "]"); } @@ -298,14 +353,15 @@ public class OperationManagerImpl implements OperationManager { if (!isActionAuthorized(deviceId)) { throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" + - deviceId.getType() + "' device, which carries the identifier '" + deviceId.getId() + "'"); + deviceId.getType() + "' device, which carries the identifier '" + + deviceId.getId() + "'"); } int enrolmentId = this.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE); if (enrolmentId < 0) { throw new OperationManagementException("Device not found for the given device Identifier:" + - deviceId.getId() + " and given type:" + - deviceId.getType()); + deviceId.getId() + " and given type:" + + deviceId.getType()); } try { @@ -326,8 +382,8 @@ public class OperationManagerImpl implements OperationManager { Collections.sort(operations, new OperationCreateTimeComparator()); } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the list of " + - "pending operations assigned for '" + deviceId.getType() + - "' device '" + deviceId.getId() + "'", e); + "pending operations assigned for '" + deviceId.getType() + + "' device '" + deviceId.getId() + "'", e); } catch (SQLException e) { throw new OperationManagementException( "Error occurred while opening a connection to the data source", e); @@ -346,36 +402,44 @@ public class OperationManagerImpl implements OperationManager { if (!isActionAuthorized(deviceId)) { throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" + - deviceId.getType() + "' device, which carries the identifier '" + deviceId.getId() + "'"); + deviceId.getType() + "' device, which carries the identifier '" + + deviceId.getId() + "'"); } int enrolmentId = this.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE); if (enrolmentId < 0) { throw new OperationManagementException("Device not found for given device " + - "Identifier:" + deviceId.getId() + " and given type" + - deviceId.getType()); + "Identifier:" + deviceId.getId() + " and given type" + + deviceId.getType()); } try { OperationManagementDAOFactory.openConnection(); org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO. - getNextOperation(enrolmentId); + getNextOperation( + enrolmentId); if (dtoOperation != null) { if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND. - equals(dtoOperation.getType())) { + equals(dtoOperation + .getType())) { org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation; commandOperation = (org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO. - getOperation(dtoOperation.getId()); + getOperation( + dtoOperation + .getId()); dtoOperation.setEnabled(commandOperation.isEnabled()); } else if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG. - equals(dtoOperation.getType())) { + equals(dtoOperation + .getType())) { dtoOperation = configOperationDAO.getOperation(dtoOperation.getId()); } else if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.PROFILE. - equals(dtoOperation.getType())) { + equals(dtoOperation + .getType())) { dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId()); } else if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.POLICY. - equals(dtoOperation.getType())) { + equals(dtoOperation + .getType())) { dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId()); } operation = OperationDAOUtil.convertOperation(dtoOperation); @@ -400,7 +464,8 @@ public class OperationManagerImpl implements OperationManager { if (!isActionAuthorized(deviceId)) { throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" + - deviceId.getType() + "' device, which carries the identifier '" + deviceId.getId() + "'"); + deviceId.getType() + "' device, which carries the identifier '" + + deviceId.getId() + "'"); } int enrolmentId = this.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE); @@ -410,8 +475,10 @@ public class OperationManagerImpl implements OperationManager { boolean isUpdated = false; if (operation.getStatus() != null) { isUpdated = operationDAO.updateOperationStatus(enrolmentId, operationId, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status. - valueOf(operation.getStatus().toString())); + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status + . + valueOf(operation.getStatus() + .toString())); } if (isUpdated && operation.getOperationResponse() != null) { operationDAO.addOperationResponse(enrolmentId, operationId, operation.getOperationResponse()); @@ -421,7 +488,7 @@ public class OperationManagerImpl implements OperationManager { OperationManagementDAOFactory.rollbackTransaction(); throw new OperationManagementException( "Error occurred while updating the operation: " + operationId + " status:" + - operation.getStatus(), e); + operation.getStatus(), e); } catch (TransactionManagementException e) { throw new OperationManagementException("Error occurred while initiating a transaction", e); } finally { @@ -456,30 +523,35 @@ public class OperationManagerImpl implements OperationManager { Operation operation = null; if (log.isDebugEnabled()) { log.debug("Operation Id: " + operationId + " Device Type: " + deviceId.getType() + " Device Identifier: " + - deviceId.getId()); + deviceId.getId()); } if (!isActionAuthorized(deviceId)) { throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" + - deviceId.getType() + "' device, which carries the identifier '" + deviceId.getId() + "'"); + deviceId.getType() + "' device, which carries the identifier '" + + deviceId.getId() + "'"); } int enrolmentId = this.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE); if (enrolmentId < 0) { throw new OperationManagementException("Device not found for given device identifier: " + - deviceId.getId() + " type: " + deviceId.getType()); + deviceId.getId() + " type: " + deviceId.getType()); } try { OperationManagementDAOFactory.openConnection(); org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO. - getOperationByDeviceAndId(enrolmentId, operationId); + getOperationByDeviceAndId( + enrolmentId, + operationId); if (dtoOperation.getType(). equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) { org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation; commandOperation = (org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO. - getOperation(dtoOperation.getId()); + getOperation( + dtoOperation + .getId()); dtoOperation.setEnabled(commandOperation.isEnabled()); } else if (dtoOperation.getType(). equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { @@ -494,16 +566,16 @@ public class OperationManagerImpl implements OperationManager { if (dtoOperation == null) { throw new OperationManagementException("Operation not found for operation Id:" + operationId + - " device id:" + deviceId.getId()); + " device id:" + deviceId.getId()); } operation = OperationDAOUtil.convertOperation(dtoOperation); } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the list of " + - "operations assigned for '" + deviceId.getType() + - "' device '" + deviceId.getId() + "'", e); + "operations assigned for '" + deviceId.getType() + + "' device '" + deviceId.getId() + "'", e); } catch (SQLException e) { throw new OperationManagementException("Error occurred while opening connection to the data source", - e); + e); } finally { OperationManagementDAOFactory.closeConnection(); } @@ -519,14 +591,15 @@ public class OperationManagerImpl implements OperationManager { if (!isActionAuthorized(deviceId)) { throw new OperationManagementException("User '" + getUser() + "' is not authorized to access the '" + - deviceId.getType() + "' device, which carries the identifier '" + deviceId.getId() + "'"); + deviceId.getType() + "' device, which carries the identifier '" + + deviceId.getId() + "'"); } int enrolmentId = this.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE); if (enrolmentId < 0) { throw new OperationManagementException( "Device not found for device id:" + deviceId.getId() + " " + "type:" + - deviceId.getType()); + deviceId.getType()); } try { @@ -535,11 +608,11 @@ public class OperationManagerImpl implements OperationManager { org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.valueOf(status.toString()); dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, dtoOpStatus)); dtoOperationList.addAll(configOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); dtoOperationList.addAll(profileOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); dtoOperationList.addAll(policyOperationDAO.getOperationsByDeviceAndStatus(enrolmentId, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING)); Operation operation; @@ -550,9 +623,9 @@ public class OperationManagerImpl implements OperationManager { } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the list of " + - "operations assigned for '" + deviceId.getType() + - "' device '" + - deviceId.getId() + "' and status:" + status.toString(), e); + "operations assigned for '" + deviceId.getType() + + "' device '" + + deviceId.getId() + "' and status:" + status.toString(), e); } catch (SQLException e) { throw new OperationManagementException( "Error occurred while opening a connection to the data source", e); @@ -568,31 +641,35 @@ public class OperationManagerImpl implements OperationManager { try { OperationManagementDAOFactory.openConnection(); org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = operationDAO. - getOperation(operationId); + getOperation( + operationId); if (dtoOperation == null) { throw new OperationManagementException("Operation not found for given Id:" + operationId); } - if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) { + if (dtoOperation.getType() + .equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) { org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation; commandOperation = (org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO. - getOperation(dtoOperation.getId()); + getOperation( + dtoOperation + .getId()); dtoOperation.setEnabled(commandOperation.isEnabled()); } else if (dtoOperation.getType(). equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { dtoOperation = configOperationDAO.getOperation(dtoOperation.getId()); } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type. - PROFILE)) { + PROFILE)) { dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId()); } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type. - POLICY)) { + POLICY)) { dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId()); } operation = OperationDAOUtil.convertOperation(dtoOperation); } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the operation with operation Id '" + - operationId, e); + operationId, e); } catch (SQLException e) { throw new OperationManagementException("Error occurred while opening a connection to the data source", e); } finally { @@ -601,61 +678,61 @@ public class OperationManagerImpl implements OperationManager { return operation; } -// @Override -// public Operation getOperationByActivityId(String activity) throws OperationManagementException { -// // This parses the operation id from activity id (ex : ACTIVITY_23) and converts to the integer. -// Operation operation; -// int enrollmentOpMappingId = Integer.parseInt( -// activity.replace(DeviceManagementConstants.OperationAttributes.ACTIVITY, "")); -// if (enrollmentOpMappingId == 0) { -// throw new IllegalArgumentException("Operation ID cannot be null or zero (0)."); -// } -// try { -// OperationManagementDAOFactory.openConnection(); -// org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = -// operationDAO.getOperationFromEnrollment(enrollmentOpMappingId); -// -// if (dtoOperation == null) { -// throw new OperationManagementException("Operation not found for given activity Id:" + activity); -// } -// org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status status = dtoOperation.getStatus(); -// if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) { -// org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation; -// commandOperation = -// (org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO. -// getOperation(dtoOperation.getId()); -// dtoOperation.setEnabled(commandOperation.isEnabled()); -// } else if (dtoOperation.getType(). -// equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { -// dtoOperation = configOperationDAO.getOperation(dtoOperation.getId()); -// } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type. -// PROFILE)) { -// dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId()); -// } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type. -// POLICY)) { -// dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId()); -// } -// operation = OperationDAOUtil.convertOperation(dtoOperation); -// int enrolmentId = operationDAO.getEnrolmentIdFromMappingId(enrollmentOpMappingId); -// if (enrolmentId != 0) { -// operation.setResponses(operationDAO.getOperationResponses(enrolmentId, operation.getId())); -// } -// -// operation.setStatus(Operation.Status.valueOf(status.toString())); -// operation.setActivityId(activity); -// -// } catch (SQLException e) { -// throw new OperationManagementException("Error occurred while opening a connection to the data source", e); -// } catch (OperationManagementDAOException e) { -// throw new OperationManagementException("Error occurred while retrieving the operation with activity Id '" + -// activity, e); -// } finally { -// OperationManagementDAOFactory.closeConnection(); -// } -// -// // return this.getOperation(operationId); -// return operation; -// } + // @Override + // public Operation getOperationByActivityId(String activity) throws OperationManagementException { + // // This parses the operation id from activity id (ex : ACTIVITY_23) and converts to the integer. + // Operation operation; + // int enrollmentOpMappingId = Integer.parseInt( + // activity.replace(DeviceManagementConstants.OperationAttributes.ACTIVITY, "")); + // if (enrollmentOpMappingId == 0) { + // throw new IllegalArgumentException("Operation ID cannot be null or zero (0)."); + // } + // try { + // OperationManagementDAOFactory.openConnection(); + // org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation = + // operationDAO.getOperationFromEnrollment(enrollmentOpMappingId); + // + // if (dtoOperation == null) { + // throw new OperationManagementException("Operation not found for given activity Id:" + activity); + // } + // org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status status = dtoOperation.getStatus(); + // if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.COMMAND)) { + // org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation commandOperation; + // commandOperation = + // (org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation) commandOperationDAO. + // getOperation(dtoOperation.getId()); + // dtoOperation.setEnabled(commandOperation.isEnabled()); + // } else if (dtoOperation.getType(). + // equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type.CONFIG)) { + // dtoOperation = configOperationDAO.getOperation(dtoOperation.getId()); + // } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type. + // PROFILE)) { + // dtoOperation = profileOperationDAO.getOperation(dtoOperation.getId()); + // } else if (dtoOperation.getType().equals(org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Type. + // POLICY)) { + // dtoOperation = policyOperationDAO.getOperation(dtoOperation.getId()); + // } + // operation = OperationDAOUtil.convertOperation(dtoOperation); + // int enrolmentId = operationDAO.getEnrolmentIdFromMappingId(enrollmentOpMappingId); + // if (enrolmentId != 0) { + // operation.setResponses(operationDAO.getOperationResponses(enrolmentId, operation.getId())); + // } + // + // operation.setStatus(Operation.Status.valueOf(status.toString())); + // operation.setActivityId(activity); + // + // } catch (SQLException e) { + // throw new OperationManagementException("Error occurred while opening a connection to the data source", e); + // } catch (OperationManagementDAOException e) { + // throw new OperationManagementException("Error occurred while retrieving the operation with activity Id '" + + // activity, e); + // } finally { + // OperationManagementDAOFactory.closeConnection(); + // } + // + // // return this.getOperation(operationId); + // return operation; + // } @Override public Activity getOperationByActivityId(String activity) throws OperationManagementException { @@ -672,7 +749,7 @@ public class OperationManagerImpl implements OperationManager { throw new OperationManagementException("Error occurred while opening a connection to the data source.", e); } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the operation with activity Id '" + - activity, e); + activity, e); } finally { OperationManagementDAOFactory.closeConnection(); } @@ -692,7 +769,7 @@ public class OperationManagerImpl implements OperationManager { throw new OperationManagementException("Error occurred while opening a connection to the data source.", e); } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while getting the activity list changed after a " + - "given time.", e); + "given time.", e); } finally { OperationManagementDAOFactory.closeConnection(); } @@ -708,7 +785,7 @@ public class OperationManagerImpl implements OperationManager { throw new OperationManagementException("Error occurred while opening a connection to the data source.", e); } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while getting the activity list changed after a " + - "given time.", e); + "given time.", e); } finally { OperationManagementDAOFactory.closeConnection(); } @@ -723,7 +800,7 @@ public class OperationManagerImpl implements OperationManager { throw new OperationManagementException("Error occurred while opening a connection to the data source.", e); } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while getting the activity count changed after a " + - "given time.", e); + "given time.", e); } finally { OperationManagementDAOFactory.closeConnection(); } @@ -805,8 +882,8 @@ public class OperationManagerImpl implements OperationManager { enrolmentId = deviceDAO.getEnrolmentByStatus(deviceId, status, tenantId); } catch (DeviceManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving metadata of '" + - deviceId.getType() + "' device carrying the identifier '" + - deviceId.getId() + "'", e); + deviceId.getType() + "' device carrying the identifier '" + + deviceId.getId() + "'", e); } catch (SQLException e) { throw new OperationManagementException( "Error occurred while opening a connection to the data source", e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index 9e8d37b662..17023a5a31 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -342,12 +342,10 @@ public final class DeviceManagerUtil { if (isValidDeviceIdentifier(deviceIdentifier)) { validDeviceIDList.add(deviceIdentifier); } else { - errorDeviceIdList.add(String.format(OperationMgtConstants.DeviceConstants. - DEVICE_ID_NOT_FOUND, deviceID)); + errorDeviceIdList.add(deviceID); } } catch (DeviceManagementException e) { - errorDeviceIdList.add(String.format(OperationMgtConstants.DeviceConstants.DEVICE_ID_SERVICE_NOT_FOUND, - deviceIDCounter)); + errorDeviceIdList.add(deviceID); } } From 675843d7f75de29adef2fa385f45540ea7789622 Mon Sep 17 00:00:00 2001 From: harshanl Date: Tue, 30 Aug 2016 15:53:07 +0530 Subject: [PATCH 6/6] Added Valid device list to Activity response --- .../core/operation/mgt/OperationManagerImpl.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 97c04c0d6a..bf09bd367c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -185,14 +185,6 @@ public class OperationManagerImpl implements OperationManager { String deviceType) { List activityStatuses = new ArrayList<>(); ActivityStatus activityStatus; - //Add the valid DeviceIds - for (DeviceIdentifier id : deviceIdValidationResult.getValidDeviceIDList()) { - activityStatus = new ActivityStatus(); - activityStatus.setDeviceIdentifier(id); - activityStatus.setStatus(ActivityStatus.Status.PENDING); - activityStatuses.add(activityStatus); - } - //Add the invalid DeviceIds for (String id : deviceIdValidationResult.getErrorDeviceIdList()) { activityStatus = new ActivityStatus(); @@ -208,6 +200,14 @@ public class OperationManagerImpl implements OperationManager { activityStatus.setStatus(ActivityStatus.Status.UNAUTHORIZED); activityStatuses.add(activityStatus); } + + //Add the authorized DeviceIds + for (DeviceIdentifier id : deviceAuthResult.getValidDeviceIDList()) { + activityStatus = new ActivityStatus(); + activityStatus.setDeviceIdentifier(id); + activityStatus.setStatus(ActivityStatus.Status.PENDING); + activityStatuses.add(activityStatus); + } return activityStatuses; }