From 06eb836f11fbc7000fdacd430c8f5c3a5f4d653a Mon Sep 17 00:00:00 2001 From: Kaveesha Date: Tue, 1 Jun 2021 10:49:33 +0530 Subject: [PATCH 01/17] Update DM_DEVICE_INFO table (cherry picked from commit e111c1dda8c945ecd09bf505d6f217d4a397237f) --- .../src/test/resources/sql/h2.sql | 2 +- .../src/test/resources/sql-files/h2.sql | 2 +- .../src/test/resources/sql/CreateH2TestDB.sql | 4 ++-- .../src/main/resources/dbscripts/cdm/h2.sql | 4 ++-- .../src/main/resources/dbscripts/cdm/mssql.sql | 4 ++-- .../src/main/resources/dbscripts/cdm/mysql.sql | 4 ++-- .../src/main/resources/dbscripts/cdm/oracle.sql | 4 ++-- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql index a380bf5204..63af00f07f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql @@ -398,7 +398,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_INFO ( DEVICE_ID INT NULL, ENROLMENT_ID INT NOT NULL, KEY_FIELD VARCHAR(45) NULL, - VALUE_FIELD VARCHAR(1000) NULL, + VALUE_FIELD VARCHAR(1500) NULL, PRIMARY KEY (ID), CONSTRAINT DM_DEVICE_INFO_DEVICE FOREIGN KEY (DEVICE_ID) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/sql-files/h2.sql b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/sql-files/h2.sql index eedbcd875a..7d7c1ece9c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/sql-files/h2.sql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/resources/sql-files/h2.sql @@ -400,7 +400,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_INFO ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INT NULL, KEY_FIELD VARCHAR(45) NULL, - VALUE_FIELD VARCHAR(1000) NULL, + VALUE_FIELD VARCHAR(1500) NULL, PRIMARY KEY (ID), CONSTRAINT DM_DEVICE_INFO_DEVICE FOREIGN KEY (DEVICE_ID) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql index e53054a6a0..d830a6387e 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql @@ -452,7 +452,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_INFO ( DEVICE_ID INT NULL, ENROLMENT_ID INT NOT NULL, KEY_FIELD VARCHAR(45) NULL, - VALUE_FIELD VARCHAR(1000) NULL, + VALUE_FIELD VARCHAR(1500) NULL, PRIMARY KEY (ID), CONSTRAINT DM_DEVICE_INFO_DEVICE FOREIGN KEY (DEVICE_ID) @@ -607,4 +607,4 @@ DM_DEVICE.DEVICE_TYPE_ID = DM_DEVICE_TYPE.ID AND DM_DEVICE.ID = DM_DEVICE_DETAIL.DEVICE_ID ORDER BY TENANT_ID, DEVICE_ID; --- END OF DASHBOARD RELATED VIEWS -- \ No newline at end of file +-- END OF DASHBOARD RELATED VIEWS -- diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index 0fb5fc7c42..bda86bc93b 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -424,7 +424,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_INFO ( DEVICE_ID INT NULL, ENROLMENT_ID INT NOT NULL, KEY_FIELD VARCHAR(45) NULL, - VALUE_FIELD VARCHAR(1000) NULL, + VALUE_FIELD VARCHAR(1500) NULL, PRIMARY KEY (ID), CONSTRAINT DM_DEVICE_INFO_DEVICE FOREIGN KEY (DEVICE_ID) @@ -722,4 +722,4 @@ CREATE TABLE IF NOT EXISTS DM_GEOFENCE_EVENT_MAPPING ( DM_DEVICE_EVENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); --- END OF DM_GEOFENCE_GROUP_MAPPING TABLE-- \ No newline at end of file +-- END OF DM_GEOFENCE_GROUP_MAPPING TABLE-- diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index c70277074a..247d796c52 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -474,7 +474,7 @@ CREATE TABLE DM_DEVICE_INFO ( DEVICE_ID INTEGER NULL, ENROLMENT_ID INTEGER NOT NULL, KEY_FIELD VARCHAR(45) NULL, - VALUE_FIELD VARCHAR(1000) NULL, + VALUE_FIELD VARCHAR(1500) NULL, PRIMARY KEY (ID), INDEX DM_DEVICE_INFO_DEVICE_idx (DEVICE_ID ASC), INDEX DM_DEVICE_INFO_DEVICE_ENROLLMENT_idx (ENROLMENT_ID ASC), @@ -714,4 +714,4 @@ CREATE TABLE DM_GEOFENCE ( PRIMARY KEY (ID) ); --- END OF DM_GEOFENCE TABLE-- \ No newline at end of file +-- END OF DM_GEOFENCE TABLE-- diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index 6ba30303b8..cc9e8f1273 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -491,7 +491,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_INFO ( DEVICE_ID INT NULL, ENROLMENT_ID INT NOT NULL, KEY_FIELD VARCHAR(45) NULL, - VALUE_FIELD VARCHAR(1000) NULL, + VALUE_FIELD VARCHAR(1500) NULL, PRIMARY KEY (ID), INDEX DM_DEVICE_INFO_DEVICE_idx (DEVICE_ID ASC), INDEX DM_DEVICE_INFO_DEVICE_ENROLLMENT_idx (ENROLMENT_ID ASC), @@ -786,4 +786,4 @@ CREATE TABLE IF NOT EXISTS DM_GEOFENCE_EVENT_MAPPING ( DM_DEVICE_EVENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ) ENGINE=InnoDB; --- END OF DM_GEOFENCE_GROUP_MAPPING TABLE-- \ No newline at end of file +-- END OF DM_GEOFENCE_GROUP_MAPPING TABLE-- diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql index de19680e81..007a7008f0 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -779,7 +779,7 @@ CREATE TABLE DM_DEVICE_INFO ( DEVICE_ID NUMBER(10) NOT NULL, ENROLMENT_ID NUMBER(10) NOT NULL, KEY_FIELD VARCHAR2(45) NULL, - VALUE_FIELD VARCHAR2(1000) NULL, + VALUE_FIELD VARCHAR2(1500) NULL, PRIMARY KEY (ID), CONSTRAINT DM_DEVICE_INFO_DEVICE FOREIGN KEY (DEVICE_ID) @@ -1083,4 +1083,4 @@ CREATE TABLE DM_GEOFENCE ( CONSTRAINT PK_DM_GEOFENCE PRIMARY KEY (ID) ); --- END OF DM_GEOFENCE TABLE-- \ No newline at end of file +-- END OF DM_GEOFENCE TABLE-- From 2b9ef49a52a542e1c5c005d7076a5d835ef3a96c Mon Sep 17 00:00:00 2001 From: Charitha Goonetilleke Date: Tue, 22 Jun 2021 04:43:59 +0000 Subject: [PATCH 02/17] Update .gitlab-ci.yml (cherry picked from commit c875f89eeacc3d151828ab1ae96d2aa19a6aeded) --- .gitlab-ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f22bfc39ca..2530bdcfa7 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -25,3 +25,4 @@ deploy: - mvn $MAVEN_CLI_OPTS deploy -Dmaven.test.skip=true only: - master@entgra/carbon-device-mgt + - kernel-4.6.x@entgra/carbon-device-mgt From 07b779323c6d6783357748664c5ceee8dba9e5b8 Mon Sep 17 00:00:00 2001 From: Kaveesha Date: Wed, 7 Jul 2021 00:55:27 +0530 Subject: [PATCH 03/17] Add API for get app subscribed device list from a subscription group (cherry picked from commit d84f642b558f5c633807274ec85ec808c0560dab) --- .../common/services/SubscriptionManager.java | 12 ++ .../mgt/core/dao/SubscriptionDAO.java | 12 ++ .../GenericSubscriptionDAOImpl.java | 41 +++++ .../core/impl/SubscriptionManagerImpl.java | 46 ++++++ .../services/SubscriptionManagementAPI.java | 97 +++++++++++ .../impl/SubscriptionManagementAPIImpl.java | 69 ++++++++ .../carbon/device/mgt/core/dao/DeviceDAO.java | 23 +++ .../core/dao/impl/AbstractDeviceDAOImpl.java | 150 ++++++++++++++++++ .../dao/impl/device/OracleDeviceDAOImpl.java | 73 +++++++++ .../impl/device/SQLServerDeviceDAOImpl.java | 74 +++++++++ .../DeviceManagementProviderService.java | 10 ++ .../DeviceManagementProviderServiceImpl.java | 35 ++++ 12 files changed, 642 insertions(+) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java index a535ffd389..600da94b4f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java @@ -161,4 +161,16 @@ public interface SubscriptionManager { */ PaginationResult getAppSubscriptionDetails(PaginationRequest request, String appUUID, String actionStatus, String action) throws ApplicationManagementException; + + /*** + * This method is responsible to provide application subscription devices data for given application release UUID. + * @param request PaginationRequest object holding the data for pagination + * @param appUUID UUID of the application release. + * @param subType subscription type of the application(eg: GROUP, USER, ...) + * @param subTypeName subscription type name of the application (Name of the group, Name of the user, ...). + * @return {@link PaginationResult} pagination result of the category details. + * @throws {@link ApplicationManagementException} Exception of the application management + */ + PaginationResult getAppInstalledSubscribeDevices(PaginationRequest request, String appUUID, String subType, + String subTypeName) throws ApplicationManagementException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java index ce8780a6a2..3ff0d95945 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java @@ -228,4 +228,16 @@ public interface SubscriptionDAO { throws ApplicationManagementDAOException; int getSubscribedGroupCount(int appReleaseId, int tenantId) throws ApplicationManagementDAOException; + + /** + * This method is used to get the details of subscribed groups + * + * @param tenantId id of the current tenant + * @param appReleaseId id of the application release.. + * @param subtype application subscribed type. + * @return subscribedDevices - list of app subscribed devices under the subtype. + * @throws {@link ApplicationManagementDAOException} if connections establishment fails. + */ + List getAppSubscribedDevicesForGroups(int appReleaseId, String subtype, int tenantId) + throws ApplicationManagementDAOException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index c2fd7f240a..4e6c6623f1 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -1271,4 +1271,45 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc throw new ApplicationManagementDAOException(msg, e); } } + + @Override + public List getAppSubscribedDevicesForGroups(int appReleaseId, String subType, int tenantId) + throws ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Request received in DAO Layer to get already subscribed devices for " + + "given app release id."); + } + // retrieve all device list by action triggered type and app release id + try { + Connection conn = this.getDBConnection(); + List subscribedGroupDevices = new ArrayList<>(); + String sql = "SELECT " + + "AP_DEVICE_SUBSCRIPTION.DM_DEVICE_ID AS DEVICES " + + "FROM AP_DEVICE_SUBSCRIPTION " + + "WHERE " + + "AP_APP_RELEASE_ID = ? AND ACTION_TRIGGERED_FROM=? AND " + + "UNSUBSCRIBED=FALSE AND TENANT_ID = ?"; + try (PreparedStatement ps = conn.prepareStatement(sql)) { + ps.setInt(1, appReleaseId); + ps.setString(2, subType.toLowerCase());; + ps.setInt(3, tenantId); + try (ResultSet rs = ps.executeQuery()) { + while (rs.next()) { + subscribedGroupDevices.add(rs.getInt("DEVICES")); + } + } + return subscribedGroupDevices; + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the DB connection to get already " + + "subscribed groups for given app release id."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "SQL Error occurred while getting subscribed devices for given " + + "app release id."; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java index 5aa222ce13..8219dbb4e7 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -1427,4 +1427,50 @@ public class SubscriptionManagerImpl implements SubscriptionManager { ConnectionManagerUtil.closeDBConnection(); } } + + @Override + public PaginationResult getAppInstalledSubscribeDevices(PaginationRequest request, String appUUID, String subType, + String subTypeName) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + DeviceManagementProviderService deviceManagementProviderService = HelperUtil + .getDeviceManagementProviderService(); + try { + ConnectionManagerUtil.openDBConnection(); + ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(appUUID, tenantId); + int applicationReleaseId = applicationDTO.getApplicationReleaseDTOs().get(0).getId(); + List subscriptionDeviceList = new ArrayList<>(); + //todo update the API for other subscription types + if (SubscriptionType.GROUP.toString().equalsIgnoreCase(subType)) { + subscriptionDeviceList = subscriptionDAO + .getAppSubscribedDevicesForGroups(applicationReleaseId, subType, tenantId); + } else { + String msg = "Found invalid sub type: " + subType; + log.error(msg); + throw new NotFoundException(msg); + } + if (subscriptionDeviceList.isEmpty()) { + PaginationResult paginationResult = new PaginationResult(); + paginationResult.setData(subscriptionDeviceList); + paginationResult.setRecordsFiltered(0); + paginationResult.setRecordsTotal(0); + return paginationResult; + } + return deviceManagementProviderService.getDevicesDetails(request, subscriptionDeviceList, subTypeName); + } catch (DeviceManagementException e) { + String msg = "service error occurred while getting device data from the device management service."; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (ApplicationManagementDAOException e) { + String msg = "Error occurred when get application release devices data for application release UUID: " + + appUUID; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } catch (DBConnectionException e) { + String msg = "DB Connection error occurred while getting category details that given application id"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java index be55b64639..2c8811358d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java @@ -446,4 +446,101 @@ public interface SubscriptionManagementAPI { defaultValue = "5") @QueryParam("limit") int limit ); + + @GET + @Path("/{uuid}/{subType}/{subTypeName}/devices") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Get device details in categories that have a given application install", + notes = "This will get the category's device details that have a given application install, if exists", + tags = "Subscription Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:app:subscription:uninstall") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully retrieved device details.", + response = List.class, + responseContainer = "List"), + @ApiResponse( + code = 404, + message = "Not Found. \n No Devices found which has application " + + "release of UUID.", + response = ErrorResponse.class), + @ApiResponse( + code = 400, + message = "Bad Request. \n Found invalid payload with the request.", + response = List.class), + @ApiResponse( + code = 403, + message = "Forbidden. \n Don't have permission to get the details.", + response = List.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while getting data", + response = ErrorResponse.class) + }) + Response getAppInstalledDevicesOnCategories( + @ApiParam( + name="uuid", + value="uuid of the application release.", + required = true) + @PathParam("uuid") String uuid, + @ApiParam( + name="subType", + value="Subscription type of the application release.", + required = true) + @PathParam("subType") String subType, + @ApiParam( + name="subTypeName", + value="Subscription type name of the application release.", + required = true) + @PathParam("subTypeName") String subTypeName, + @ApiParam( + name = "offset", + value = "The starting pagination index for the complete list of qualified items.", + defaultValue = "0") + @QueryParam("offset") int offset, + @ApiParam( + name = "limit", + value = "Provide how many device details you require from the starting " + + "pagination index/offset.", + defaultValue = "5") + @QueryParam("limit") int limit, + @ApiParam( + name = "name", + value = "The device name. For example, Nexus devices can have names, such as shamu, bullhead or angler.", + required = false) + @Size(max = 45) + String name, + @ApiParam( + name = "user", + value = "The username of the owner of the device.", + required = false) + @QueryParam("user") + String user, + @ApiParam( + name = "ownership", + allowableValues = "BYOD, COPE", + value = "Provide the ownership status of the device. The following values can be assigned:\n" + + "- BYOD: Bring Your Own Device\n" + + "- COPE: Corporate-Owned, Personally-Enabled", + required = false) + @QueryParam("ownership") + @Size(max = 45) + String ownership, + @ApiParam( + name = "status", + value = "Provide the device status details, such as active or inactive.") + @QueryParam("status") List status + ); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java index acf9662cb9..a6324c8fdb 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java @@ -400,4 +400,73 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } + + @GET + @Consumes("application/json") + @Produces("application/json") + @Path("/{uuid}/{subType}/{subTypeName}/devices") + public Response getAppInstalledDevicesOnCategories( + @PathParam("uuid") String uuid, + @PathParam("subType") String subType, + @PathParam("subTypeName") String subTypeName, + @DefaultValue("0") + @QueryParam("offset") int offset, + @DefaultValue("5") + @QueryParam("limit") int limit, + @QueryParam("name") String name, + @QueryParam("user") String user, + @QueryParam("ownership") String ownership, + @QueryParam("status") List status) { + try { + SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); + PaginationRequest request = new PaginationRequest(offset, limit); + + if (StringUtils.isNotBlank(name)) { + request.setDeviceName(name); + } + if (StringUtils.isNotBlank(user)) { + request.setOwner(user); + } + if (StringUtils.isNotBlank(ownership)) { + RequestValidationUtil.validateOwnershipType(ownership); + request.setOwnership(ownership); + } + if (status != null && !status.isEmpty()) { + boolean isStatusEmpty = true; + for (String statusString : status) { + if (StringUtils.isNotBlank(statusString)) { + isStatusEmpty = false; + break; + } + } + if (!isStatusEmpty) { + RequestValidationUtil.validateStatus(status); + request.setStatusList(status); + } + } + + //todo need to update the API for other subscription types + if (SubscriptionType.GROUP.toString().equalsIgnoreCase(subType)) { + PaginationResult subscribedCategoryDetails = subscriptionManager + .getAppInstalledSubscribeDevices(request, uuid, subType, subTypeName); + DeviceList devices = new DeviceList(); + devices.setList((List) subscribedCategoryDetails.getData()); + devices.setCount(subscribedCategoryDetails.getRecordsTotal()); + return Response.status(Response.Status.OK).entity(devices).build(); + } else { + String msg = "Found invalid sub type: " + subType; + log.error(msg); + return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); + } + } catch (NotFoundException e) { + String msg = "Application with application release UUID: " + uuid + " is not found"; + log.error(msg, e); + return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); + } catch (ApplicationManagementException e) { + String msg = "Error occurred while getting application with the application " + + "release uuid: " + uuid; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index 77b9d76c5c..9796dd40f4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -758,4 +758,27 @@ public interface DeviceDAO { String version) throws DeviceManagementDAOException; int getFunctioningDevicesInSystem() throws DeviceManagementDAOException; + + /** + * This method is used to get the details of devices when give deviceIDs list and group name. + * @param deviceIds device ids of the devices. + * @param tenantId Id of the current tenant. + * @param request paginated request object. + * @param groupName group name. + * @return devices - device details list + * @throws DeviceManagementDAOException if connections establishment fails. + */ + List getGroupedDevicesDetails(PaginationRequest request, List deviceIds, String groupName, + int tenantId) throws DeviceManagementDAOException; + + /** + * @param deviceIds device ids of the devices. + * @param tenantId tenant id + * @param request paginated request object. + * @param groupName group name. + * @return number of device count under the group name. + * @throws DeviceManagementDAOException if error occurred while processing the SQL statement. + */ + int getGroupedDevicesCount(PaginationRequest request, List deviceIds, String groupName, int tenantId) + throws DeviceManagementDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index bf387744e2..dd175d493a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -3022,4 +3022,154 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { throw new DeviceManagementDAOException(msg, e); } } + + @Override + public List getGroupedDevicesDetails(PaginationRequest request, List deviceIds, String groupName, + int tenantId) throws DeviceManagementDAOException { + int limitValue = request.getRowCount(); + int offsetValue = request.getStartIndex(); + List status = request.getStatusList(); + String name = request.getDeviceName(); + String user = request.getOwner(); + String ownership = request.getOwnership(); + try { + List devices = new ArrayList<>(); + if (deviceIds.isEmpty()) { + return devices; + } + Connection conn = this.getConnection(); + int index = 1; + StringJoiner joiner = new StringJoiner(",", + "SELECT " + + "DM_DEVICE.ID AS DEVICE_ID, " + + "DM_DEVICE.NAME AS DEVICE_NAME, " + + "DM_DEVICE.DESCRIPTION AS DESCRIPTION, " + + "DM_DEVICE.DEVICE_TYPE_ID, " + + "DM_DEVICE.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFICATION, " + + "e.ID AS ENROLMENT_ID, " + + "e.OWNER, " + + "e.OWNERSHIP, " + + "e.DATE_OF_ENROLMENT, " + + "e.DATE_OF_LAST_UPDATE, " + + "e.STATUS, " + + "e.IS_TRANSFERRED, " + + "device_types.NAME AS DEVICE_TYPE " + + "FROM DM_DEVICE_GROUP_MAP " + + "INNER JOIN DM_DEVICE ON " + + "DM_DEVICE_GROUP_MAP.DEVICE_ID = DM_DEVICE.ID " + + "INNER JOIN DM_GROUP ON " + + "DM_DEVICE_GROUP_MAP.GROUP_ID = DM_GROUP.ID " + + "INNER JOIN DM_ENROLMENT e ON " + + "DM_DEVICE.ID = e.DEVICE_ID AND " + + "DM_DEVICE.TENANT_ID = e.TENANT_ID " + + "INNER JOIN (SELECT ID, NAME FROM DM_DEVICE_TYPE) AS device_types ON " + + "device_types.ID = DM_DEVICE.DEVICE_TYPE_ID " + + "WHERE DM_DEVICE.ID IN (", + ") AND DM_DEVICE.TENANT_ID = ?"); + + deviceIds.stream().map(ignored -> "?").forEach(joiner::add); + String query = joiner.toString(); + if (StringUtils.isNotBlank(groupName)) { + query += " AND DM_GROUP.GROUP_NAME = ?"; + } + if (StringUtils.isNotBlank(name)) { + query += " AND DM_DEVICE.NAME LIKE ?"; + } + if (StringUtils.isNotBlank(user)) { + query += " AND e.OWNER = ?"; + } + if (StringUtils.isNotBlank(ownership)) { + query += " AND e.OWNERSHIP = ?"; + } + if (status != null && !status.isEmpty()) { + query += buildStatusQuery(status); + } + + query += "LIMIT ? OFFSET ?"; + + try (PreparedStatement ps = conn.prepareStatement(query)) { + for (Integer deviceId : deviceIds) { + ps.setInt(index++, deviceId); + } + ps.setInt(index++, tenantId); + if (StringUtils.isNotBlank(groupName)) { + ps.setString(index++, groupName); + } + if (StringUtils.isNotBlank(name)) { + ps.setString(index++, name); + } + if (StringUtils.isNotBlank(user)) { + ps.setString(index++, user); + } + if (StringUtils.isNotBlank(ownership)) { + ps.setString(index++, ownership); + } + if (status != null && !status.isEmpty()) { + for (String deviceStatus : status) { + ps.setString(index++, deviceStatus); + } + } + ps.setInt(index++, limitValue); + ps.setInt(index, offsetValue); + + try (ResultSet rs = ps.executeQuery()) { + while (rs.next()) { + devices.add(DeviceManagementDAOUtil.loadDevice(rs)); + } + return devices; + } + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving information of all registered devices " + + "according to device ids and the limit area."; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + + @Override + public int getGroupedDevicesCount(PaginationRequest request, List deviceIds, String groupName, + int tenantId) throws DeviceManagementDAOException { + try { + Connection conn = this.getConnection(); + if (deviceIds.isEmpty()) { + return 0; + } + int index = 1; + StringJoiner joiner = new StringJoiner(",", + "SELECT " + + "COUNT(DM_DEVICE_GROUP_MAP.DEVICE_ID) AS DEVICE_COUNT " + + "FROM DM_DEVICE_GROUP_MAP " + + "INNER JOIN DM_GROUP ON " + + "DM_DEVICE_GROUP_MAP.GROUP_ID = DM_GROUP.ID " + + "WHERE DM_DEVICE_GROUP_MAP.DEVICE_ID IN (", + ") AND DM_GROUP.TENANT_ID = ?"); + deviceIds.stream().map(ignored -> "?").forEach(joiner::add); + String query = joiner.toString(); + if (StringUtils.isNotBlank(groupName)) { + query += " AND DM_GROUP.GROUP_NAME = ?"; + } + + try (PreparedStatement ps = conn.prepareStatement(query)) { + for (Integer deviceId : deviceIds) { + ps.setInt(index++, deviceId); + } + ps.setInt(index++, tenantId); + if (StringUtils.isNotBlank(groupName)) { + ps.setString(index, groupName); + } + try (ResultSet rs = ps.executeQuery()) { + if (rs.next()) { + return rs.getInt("DEVICE_COUNT"); + } + return 0; + } + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving information of all registered devices " + + "according to device ids and the limit area."; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java index 4393a4546e..f2a91cae45 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/OracleDeviceDAOImpl.java @@ -1194,6 +1194,79 @@ public class OracleDeviceDAOImpl extends AbstractDeviceDAOImpl { } } + @Override + public List getGroupedDevicesDetails(PaginationRequest request, List deviceIds, String groupName, + int tenantId) throws DeviceManagementDAOException { + int limitValue = request.getRowCount(); + int offsetValue = request.getStartIndex(); + try { + List devices = new ArrayList<>(); + if (deviceIds.isEmpty()) { + return devices; + } + Connection conn = this.getConnection(); + int index = 1; + StringJoiner joiner = new StringJoiner(",", + "SELECT " + + "DM_DEVICE.ID AS DEVICE_ID, " + + "DM_DEVICE.NAME AS DEVICE_NAME, " + + "DM_DEVICE.DESCRIPTION AS DESCRIPTION, " + + "DM_DEVICE.DEVICE_TYPE_ID, " + + "DM_DEVICE.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFICATION, " + + "e.ID AS ENROLMENT_ID, " + + "e.OWNER, " + + "e.OWNERSHIP, " + + "e.DATE_OF_ENROLMENT, " + + "e.DATE_OF_LAST_UPDATE, " + + "e.STATUS, " + + "e.IS_TRANSFERRED, " + + "device_types.NAME AS DEVICE_TYPE " + + "FROM DM_DEVICE_GROUP_MAP " + + "INNER JOIN DM_DEVICE ON " + + "DM_DEVICE_GROUP_MAP.DEVICE_ID = DM_DEVICE.ID " + + "INNER JOIN DM_GROUP ON " + + "DM_DEVICE_GROUP_MAP.GROUP_ID = DM_GROUP.ID " + + "INNER JOIN DM_ENROLMENT e ON " + + "DM_DEVICE.ID = e.DEVICE_ID AND " + + "DM_DEVICE.TENANT_ID = e.TENANT_ID " + + "INNER JOIN (SELECT ID, NAME FROM DM_DEVICE_TYPE) AS device_types ON " + + "device_types.ID = DM_DEVICE.DEVICE_TYPE_ID " + + "WHERE DM_DEVICE.ID IN (", + ") AND DM_DEVICE.TENANT_ID = ?"); + + deviceIds.stream().map(ignored -> "?").forEach(joiner::add); + String query = joiner.toString(); + if (StringUtils.isNotBlank(groupName)) { + query += " AND DM_GROUP.GROUP_NAME = ?"; + } + query += " ORDER BY DEVICE_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + + try (PreparedStatement ps = conn.prepareStatement(query)) { + for (Integer deviceId : deviceIds) { + ps.setInt(index++, deviceId); + } + ps.setInt(index++, tenantId); + if (StringUtils.isNotBlank(groupName)) { + ps.setString(index++, groupName); + } + ps.setInt(index++, offsetValue); + ps.setInt(index, limitValue); + + try (ResultSet rs = ps.executeQuery()) { + while (rs.next()) { + devices.add(DeviceManagementDAOUtil.loadDevice(rs)); + } + return devices; + } + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving information of all registered devices " + + "according to device ids and the limit area."; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + private Connection getConnection() throws SQLException { return DeviceManagementDAOFactory.getConnection(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java index 8911beca49..ae4b25b431 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/device/SQLServerDeviceDAOImpl.java @@ -18,6 +18,7 @@ package org.wso2.carbon.device.mgt.core.dao.impl.device; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Count; @@ -1132,6 +1133,79 @@ public class SQLServerDeviceDAOImpl extends AbstractDeviceDAOImpl { } } + @Override + public List getGroupedDevicesDetails(PaginationRequest request, List deviceIds, String groupName, + int tenantId) throws DeviceManagementDAOException { + int limitValue = request.getRowCount(); + int offsetValue = request.getStartIndex(); + try { + List devices = new ArrayList<>(); + if (deviceIds.isEmpty()) { + return devices; + } + Connection conn = this.getConnection(); + int index = 1; + StringJoiner joiner = new StringJoiner(",", + "SELECT " + + "DM_DEVICE.ID AS DEVICE_ID, " + + "DM_DEVICE.NAME AS DEVICE_NAME, " + + "DM_DEVICE.DESCRIPTION AS DESCRIPTION, " + + "DM_DEVICE.DEVICE_TYPE_ID, " + + "DM_DEVICE.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFICATION, " + + "e.ID AS ENROLMENT_ID, " + + "e.OWNER, " + + "e.OWNERSHIP, " + + "e.DATE_OF_ENROLMENT, " + + "e.DATE_OF_LAST_UPDATE, " + + "e.STATUS, " + + "e.IS_TRANSFERRED, " + + "device_types.NAME AS DEVICE_TYPE " + + "FROM DM_DEVICE_GROUP_MAP " + + "INNER JOIN DM_DEVICE ON " + + "DM_DEVICE_GROUP_MAP.DEVICE_ID = DM_DEVICE.ID " + + "INNER JOIN DM_GROUP ON " + + "DM_DEVICE_GROUP_MAP.GROUP_ID = DM_GROUP.ID " + + "INNER JOIN DM_ENROLMENT e ON " + + "DM_DEVICE.ID = e.DEVICE_ID AND " + + "DM_DEVICE.TENANT_ID = e.TENANT_ID " + + "INNER JOIN (SELECT ID, NAME FROM DM_DEVICE_TYPE) AS device_types ON " + + "device_types.ID = DM_DEVICE.DEVICE_TYPE_ID " + + "WHERE DM_DEVICE.ID IN (", + ") AND DM_DEVICE.TENANT_ID = ?"); + + deviceIds.stream().map(ignored -> "?").forEach(joiner::add); + String query = joiner.toString(); + if (StringUtils.isNotBlank(groupName)) { + query += " AND DM_GROUP.GROUP_NAME = ?"; + } + query += " ORDER BY DEVICE_ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; + + try (PreparedStatement ps = conn.prepareStatement(query)) { + for (Integer deviceId : deviceIds) { + ps.setInt(index++, deviceId); + } + ps.setInt(index++, tenantId); + if (StringUtils.isNotBlank(groupName)) { + ps.setString(index++, groupName); + } + ps.setInt(index++, offsetValue); + ps.setInt(index, limitValue); + + try (ResultSet rs = ps.executeQuery()) { + while (rs.next()) { + devices.add(DeviceManagementDAOUtil.loadDevice(rs)); + } + return devices; + } + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving information of all registered devices " + + "according to device ids and the limit area."; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + //TODO: Override for MSSQL /* @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 6ff661568c..6265384aad 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -992,4 +992,14 @@ public interface DeviceManagementProviderService { License getLicenseConfig (String deviceTypeName) throws DeviceManagementException; + /** + * This method retrieves a list of devices details. + * @param request paginated request object. + * @param devicesIds devices ids list + * @param groupName name of the group + * @return {@link PaginationResult} + * @throws DeviceManagementException if any service level or DAO level error occurs. + */ + PaginationResult getDevicesDetails(PaginationRequest request, List devicesIds, String groupName) + throws DeviceManagementException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 1f4bac57d1..70cbee4731 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -4429,4 +4429,39 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return deviceManagementService.getLicenseConfig(); } + @Override + public PaginationResult getDevicesDetails(PaginationRequest request, List devicesIds, + String groupName) throws DeviceManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + if (log.isDebugEnabled()) { + log.debug("Getting all devices details for device ids: " + devicesIds); + } + PaginationResult paginationResult = new PaginationResult(); + List subscribedDeviceDetails; + try { + DeviceManagementDAOFactory.openConnection(); + subscribedDeviceDetails = deviceDAO.getGroupedDevicesDetails(request, devicesIds, groupName, tenantId); + if (subscribedDeviceDetails.isEmpty()) { + paginationResult.setData(new ArrayList<>()); + paginationResult.setRecordsFiltered(0); + paginationResult.setRecordsTotal(0); + return paginationResult; + } + int count = deviceDAO.getGroupedDevicesCount(request, devicesIds, groupName, tenantId); + paginationResult.setRecordsFiltered(count); + paginationResult.setRecordsTotal(count); + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving device list for device ids " + devicesIds; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + paginationResult.setData(populateAllDeviceInfo(subscribedDeviceDetails)); + return paginationResult; + } } From a8ec609bb010c711f9e046310116ceb06fa174ea Mon Sep 17 00:00:00 2001 From: lasantha Date: Fri, 16 Jul 2021 15:25:15 +0530 Subject: [PATCH 04/17] Improve App subscribing flow (cherry picked from commit d0ef3beec045b3d29398aa6c1cc896939b6505ac) --- .../mgt/core/impl/SubscriptionManagerImpl.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java index 8219dbb4e7..0aad9bc7c2 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -848,16 +848,16 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } - subscriptionDAO.addDeviceSubscription(username, subInsertingDeviceIds, subType, - Operation.Status.PENDING.toString(), applicationReleaseId, tenantId); if (!subUpdatingDeviceIds.isEmpty()) { subscriptionDAO.updateDeviceSubscription(username, subUpdatingDeviceIds, action, subType, Operation.Status.PENDING.toString(), applicationReleaseId, tenantId); } - subUpdatingDeviceIds.addAll(subInsertingDeviceIds); - if (!subUpdatingDeviceIds.isEmpty()) { + + if (!subInsertingDeviceIds.isEmpty()) { + subscriptionDAO.addDeviceSubscription(username, subInsertingDeviceIds, subType, + Operation.Status.PENDING.toString(), applicationReleaseId, tenantId); List deviceSubIds = new ArrayList<>( - subscriptionDAO.getDeviceSubIds(subUpdatingDeviceIds, applicationReleaseId, tenantId)); + subscriptionDAO.getDeviceSubIds(subInsertingDeviceIds, applicationReleaseId, tenantId)); subscriptionDAO.addOperationMapping(operationId, deviceSubIds, tenantId); } } From bb4780f58aeb049c2fcd6e5a217e0bedc799e2ca Mon Sep 17 00:00:00 2001 From: Dharmakeerthi Lasantha Date: Mon, 19 Jul 2021 10:18:32 +0000 Subject: [PATCH 05/17] Revert "Merge branch 'master' into 'master'" This reverts merge request !785 (cherry picked from commit eb13d1b70dfc5c1c79bc86c8c36877f28d2e5204) --- .../mgt/core/impl/SubscriptionManagerImpl.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java index 0aad9bc7c2..8219dbb4e7 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -848,16 +848,16 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } + subscriptionDAO.addDeviceSubscription(username, subInsertingDeviceIds, subType, + Operation.Status.PENDING.toString(), applicationReleaseId, tenantId); if (!subUpdatingDeviceIds.isEmpty()) { subscriptionDAO.updateDeviceSubscription(username, subUpdatingDeviceIds, action, subType, Operation.Status.PENDING.toString(), applicationReleaseId, tenantId); } - - if (!subInsertingDeviceIds.isEmpty()) { - subscriptionDAO.addDeviceSubscription(username, subInsertingDeviceIds, subType, - Operation.Status.PENDING.toString(), applicationReleaseId, tenantId); + subUpdatingDeviceIds.addAll(subInsertingDeviceIds); + if (!subUpdatingDeviceIds.isEmpty()) { List deviceSubIds = new ArrayList<>( - subscriptionDAO.getDeviceSubIds(subInsertingDeviceIds, applicationReleaseId, tenantId)); + subscriptionDAO.getDeviceSubIds(subUpdatingDeviceIds, applicationReleaseId, tenantId)); subscriptionDAO.addOperationMapping(operationId, deviceSubIds, tenantId); } } From 2b4ba189c7e35ad6f4bd7476bf93fd21987b43b8 Mon Sep 17 00:00:00 2001 From: navodzoysa Date: Wed, 21 Jul 2021 21:34:42 +0530 Subject: [PATCH 06/17] Fix date range shown in reverse in MDM Reports (cherry picked from commit 5d23d1d6f651dc6b41b0d7b6364fdef4fec4f4aa) --- .../device/mgt/core/report/mgt/ReportManagementServiceImpl.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/report/mgt/ReportManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/report/mgt/ReportManagementServiceImpl.java index 533b1cb1bc..7d2ff24875 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/report/mgt/ReportManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/report/mgt/ReportManagementServiceImpl.java @@ -315,7 +315,7 @@ public class ReportManagementServiceImpl implements ReportManagementService { } //Map date blocks and counts resultObject.addProperty( - dateFormat.format(endDate) + " - " + dateFormat.format(previousDate), sum); + dateFormat.format(previousDate) + " - " + dateFormat.format(endDate), sum); endDate = previousDate; } From abefba8b44e99b348a1138684246747d5a6fb94e Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 24 Jun 2021 13:10:20 +0530 Subject: [PATCH 07/17] Add PARENT_PATH column to DM_GROUP table (cherry picked from commit e403a92db9137c843bafcc73f31cea91da746e93) --- .../src/main/resources/dbscripts/cdm/h2.sql | 1 + .../src/main/resources/dbscripts/cdm/mssql.sql | 1 + .../src/main/resources/dbscripts/cdm/mysql.sql | 1 + .../src/main/resources/dbscripts/cdm/oracle.sql | 1 + .../src/main/resources/dbscripts/cdm/postgresql.sql | 3 ++- 5 files changed, 6 insertions(+), 1 deletion(-) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index bda86bc93b..f6894eb4ac 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -23,6 +23,7 @@ CREATE TABLE IF NOT EXISTS DM_GROUP ( STATUS VARCHAR(50) DEFAULT NULL, DESCRIPTION TEXT DEFAULT NULL, OWNER VARCHAR(255) DEFAULT NULL, + PARENT_PATH VARCHAR(255) DEFAULT NULL, TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID) ); diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index 247d796c52..c27f891445 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -32,6 +32,7 @@ IF NOT EXISTS(SELECT * STATUS VARCHAR(50) DEFAULT NULL, DESCRIPTION VARCHAR(MAX) DEFAULT NULL, OWNER VARCHAR(255) DEFAULT NULL, + PARENT_PATH VARCHAR(255) DEFAULT NULL, TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID) ); diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index cc9e8f1273..a69b79b783 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -27,6 +27,7 @@ CREATE TABLE IF NOT EXISTS DM_GROUP ( STATUS VARCHAR(50) DEFAULT NULL, DESCRIPTION TEXT DEFAULT NULL, OWNER VARCHAR(255) DEFAULT NULL, + PARENT_PATH VARCHAR(255) DEFAULT NULL, TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID) ) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql index 007a7008f0..e2e4693bd2 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -50,6 +50,7 @@ CREATE TABLE DM_GROUP ( GROUP_NAME VARCHAR2(100) DEFAULT NULL, STATUS VARCHAR2(50) DEFAULT NULL, OWNER VARCHAR2(255) DEFAULT NULL, + PARENT_PATH VARCHAR2(255) DEFAULT NULL, TENANT_ID NUMBER(10) DEFAULT 0, CONSTRAINT PK_DM_GROUP PRIMARY KEY (ID) ) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql index db431497e8..bd8eb8771a 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -21,6 +21,7 @@ CREATE TABLE IF NOT EXISTS DM_GROUP ( GROUP_NAME VARCHAR(100) DEFAULT NULL, DESCRIPTION TEXT DEFAULT NULL, OWNER VARCHAR(45) DEFAULT NULL, + PARENT_PATH VARCHAR(255) DEFAULT NULL, TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID) ) @@ -732,4 +733,4 @@ CREATE TABLE IF NOT EXISTS DM_GEOFENCE ( PRIMARY KEY (ID) ); --- END OF DM_GEOFENCE TABLE-- \ No newline at end of file +-- END OF DM_GEOFENCE TABLE-- From 5bc39dd7dcd569fbe6ae4b318f063eab816b7eb8 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 24 Jun 2021 13:15:12 +0530 Subject: [PATCH 08/17] Update device group and request beans classes to have new variable (cherry picked from commit f1892f726d6a36c64cc4d96eaed274ff3363b37a) --- .../mgt/common/GroupPaginationRequest.java | 35 +++++++++++++ .../mgt/common/group/mgt/DeviceGroup.java | 50 +++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/GroupPaginationRequest.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/GroupPaginationRequest.java index d7a74f199b..9402d2ab37 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/GroupPaginationRequest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/GroupPaginationRequest.java @@ -14,6 +14,23 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * + * Copyright (c) 2021, Entgra (pvt) Ltd. (https://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; @@ -28,6 +45,8 @@ public class GroupPaginationRequest { private String owner; private String groupName; private String status; + private String parentPath; + private int depth; public GroupPaginationRequest(int start, int rowCount) { this.startIndex = start; @@ -74,6 +93,22 @@ public class GroupPaginationRequest { this.groupName = groupName; } + public String getParentPath() { + return parentPath; + } + + public void setParentPath(String parentPath) { + this.parentPath = parentPath; + } + + public int getDepth() { + return depth; + } + + public void setDepth(int depth) { + this.depth = depth; + } + @Override public String toString() { return "Group Name '" + this.groupName + "' num of rows: " + this.rowCount + " start index: " + this.startIndex diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroup.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroup.java index 20ed01fb49..a3511033d5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroup.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroup.java @@ -14,6 +14,23 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * + * Copyright (c) 2021, Entgra (pvt) Ltd. (https://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.group.mgt; @@ -21,6 +38,7 @@ import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import java.io.Serializable; +import java.util.List; import java.util.Map; /** @@ -47,6 +65,15 @@ public class DeviceGroup implements Serializable { @ApiModelProperty(name = "status", value = "The status of group that needs updating/retrieval.") private String status; + @ApiModelProperty(name = "parentGroupId", value = "Group ID of parent group") + private int parentGroupId; + + @ApiModelProperty(name = "parentPath", value = "Path of parent group") + private String parentPath; + + @ApiModelProperty(name = "childrenGroups", value = "Children groups") + private List childrenGroups; + public String getStatus() { return status; } @@ -103,4 +130,27 @@ public class DeviceGroup implements Serializable { this.groupProperties = groupProperties; } + public int getParentGroupId() { + return parentGroupId; + } + + public void setParentGroupId(int parentGroupId) { + this.parentGroupId = parentGroupId; + } + + public String getParentPath() { + return parentPath; + } + + public void setParentPath(String parentPath) { + this.parentPath = parentPath; + } + + public List getChildrenGroups() { + return childrenGroups; + } + + public void setChildrenGroups(List childrenGroups) { + this.childrenGroups = childrenGroups; + } } From 3e310d4dbf23cca80d19b923465b839f1f2d3359 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 24 Jun 2021 19:57:45 +0530 Subject: [PATCH 09/17] Add and update dao layer methods related to hierarchical changes (cherry picked from commit e22d9fd8fc5bed372c384e0c3019e65851f24d23) --- .../carbon/device/mgt/core/dao/GroupDAO.java | 78 +++- .../core/dao/impl/AbstractGroupDAOImpl.java | 360 +++++++++++++++++- .../dao/impl/group/GenericGroupDAOImpl.java | 150 +------- .../dao/impl/group/OracleGroupDAOImpl.java | 38 +- .../impl/group/PostgreSQLGroupDAOImpl.java | 156 ++------ .../dao/impl/group/SQLServerGroupDAOImpl.java | 38 +- .../core/dao/util/GroupManagementDAOUtil.java | 17 + 7 files changed, 542 insertions(+), 295 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GroupDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GroupDAO.java index 79b536feaa..9ba4e36283 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GroupDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GroupDAO.java @@ -14,6 +14,23 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * + * Copyright (c) 2021, Entgra (pvt) Ltd. (https://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.core.dao; @@ -74,6 +91,15 @@ public interface GroupDAO { */ void deleteAllGroupProperties(int groupId, int tenantId) throws GroupManagementDAOException; + /** + * Remove properties of device groups. + * + * @param groupIds to be deleted. + * @param tenantId of the group. + * @throws GroupManagementDAOException on error during deletion of group properties of groups + */ + void deleteAllGroupsProperties(List groupIds, int tenantId) throws GroupManagementDAOException; + /** * Retrives all properties stored against a group. * @@ -95,6 +121,15 @@ public interface GroupDAO { void updateGroup(DeviceGroup deviceGroup, int groupId, int tenantId) throws GroupManagementDAOException; + /** + * Update existing Device Groups. + * + * @param deviceGroups groups to update. + * @param tenantId of the group. + * @throws GroupManagementDAOException on error during updating of groups + */ + void updateGroups(List deviceGroups, int tenantId) throws GroupManagementDAOException; + /** * Delete an existing Device Group. * @@ -104,6 +139,24 @@ public interface GroupDAO { */ void deleteGroup(int groupId, int tenantId) throws GroupManagementDAOException; + /** + * Delete mappings of Device Groups. + * + * @param groupIds of Device Groups. + * @param tenantId of the group. + * @throws GroupManagementDAOException on error during deletion of mappings of groups + */ + void deleteGroupsMapping(List groupIds, int tenantId) throws GroupManagementDAOException; + + /** + * Delete existing Device Groups. + * + * @param groupIds of Device Groups. + * @param tenantId of the group. + * @throws GroupManagementDAOException on error during deletion of groups + */ + void deleteGroups(List groupIds, int tenantId) throws GroupManagementDAOException; + /** * Get device group by id. * @@ -114,6 +167,25 @@ public interface GroupDAO { */ DeviceGroup getGroup(int groupId, int tenantId) throws GroupManagementDAOException; + /** + * Get children groups by parent path. + * + * @param parentPath of parent group. + * @param tenantId of the group. + * @return {@link List} list of children device groups + * @throws GroupManagementDAOException on error during retrieval of children groups + */ + List getChildrenGroups(String parentPath, int tenantId) throws GroupManagementDAOException; + + /** + * Get root groups. + * + * @param tenantId of the group. + * @return {@link List} list of root device groups + * @throws GroupManagementDAOException on error during retrieval of root groups + */ + List getRootGroups(int tenantId) throws GroupManagementDAOException; + /** * Get the groups of device with device id provided * @param deviceId @@ -306,10 +378,11 @@ public interface GroupDAO { * * @param roles of the group. * @param tenantId of user's tenant. + * @param parentPath of the group. * @return count of device groups. * @throws GroupManagementDAOException */ - int getGroupsCount(String[] roles, int tenantId) throws GroupManagementDAOException; + int getGroupsCount(String[] roles, int tenantId, String parentPath) throws GroupManagementDAOException; /** * Get all device groups which owned by user. @@ -336,10 +409,11 @@ public interface GroupDAO { * * @param username of the owner. * @param tenantId of user's tenant. + * @param parentPath of the group. * @return count of device groups. * @throws GroupManagementDAOException */ - int getOwnGroupsCount(String username, int tenantId) throws GroupManagementDAOException; + int getOwnGroupsCount(String username, int tenantId, String parentPath) throws GroupManagementDAOException; /** * Get device Ids of devices which are assigned to groups. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java index 04d4bce584..c3426e3521 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java @@ -14,18 +14,34 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * + * Copyright (c) 2021, Entgra (pvt) Ltd. (https://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.core.dao.impl; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.solr.common.StringUtils; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; -import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.GroupDAO; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; @@ -49,6 +65,127 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { private static final Log log = LogFactory.getLog(AbstractGroupDAOImpl.class); + @Override + public List getGroups(GroupPaginationRequest request, int tenantId) + throws GroupManagementDAOException { + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP " + + "WHERE TENANT_ID = ?"; + if (StringUtils.isNotBlank(request.getGroupName())) { + sql += " AND UPPER(GROUP_NAME) LIKE ?"; + } + if (StringUtils.isNotBlank(request.getOwner())) { + sql += " AND UPPER(OWNER) LIKE ?"; + } + if (StringUtils.isNotBlank(request.getStatus())) { + sql += " AND STATUS = ?"; + } + if (StringUtils.isNotBlank(request.getParentPath())) { + sql += " AND PARENT_PATH LIKE ?"; + } + if (request.getRowCount() != 0) { + sql += " LIMIT ? OFFSET ?"; + } + + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + int paramIndex = 1; + stmt.setInt(paramIndex++, tenantId); + if (StringUtils.isNotBlank(request.getGroupName())) { + stmt.setString(paramIndex++, request.getGroupName() + "%"); + } + if (StringUtils.isNotBlank(request.getOwner())) { + stmt.setString(paramIndex++, request.getOwner() + "%"); + } + if (StringUtils.isNotBlank(request.getStatus())) { + stmt.setString(paramIndex++, request.getStatus().toUpperCase()); + } + if (StringUtils.isNotBlank(request.getParentPath())) { + stmt.setString(paramIndex++, request.getParentPath()); + } + if (request.getRowCount() != 0) { + stmt.setInt(paramIndex++, request.getRowCount()); + stmt.setInt(paramIndex, request.getStartIndex()); + } + List deviceGroupList = new ArrayList<>(); + try (ResultSet resultSet = stmt.executeQuery()) { + while (resultSet.next()) { + deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet)); + } + } + return deviceGroupList; + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving groups in tenant: " + tenantId; + log.error(msg); + throw new GroupManagementDAOException(msg, e); + } + } + + @Override + public List getGroups(GroupPaginationRequest request, List deviceGroupIds, + int tenantId) throws GroupManagementDAOException { + int deviceGroupIdsCount = deviceGroupIds.size(); + if (deviceGroupIdsCount == 0) { + return new ArrayList<>(); + } + + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP WHERE TENANT_ID = ?"; + if (StringUtils.isNotBlank(request.getGroupName())) { + sql += " AND GROUP_NAME LIKE ?"; + } + if (StringUtils.isNotBlank(request.getOwner())) { + sql += " AND OWNER LIKE ?"; + } + if (StringUtils.isNotBlank(request.getParentPath())) { + sql += " AND PARENT_PATH LIKE ?"; + } + sql += " AND ID IN ("; + for (int i = 0; i < deviceGroupIdsCount; i++) { + sql += (deviceGroupIdsCount - 1 != i) ? "?," : "?"; + } + sql += ")"; + if (request.getRowCount() != 0) { + sql += " LIMIT ? OFFSET ?"; + } + + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + int paramIndex = 1; + stmt.setInt(paramIndex++, tenantId); + if (StringUtils.isNotBlank(request.getGroupName())) { + stmt.setString(paramIndex++, request.getGroupName() + "%"); + } + if (StringUtils.isNotBlank(request.getOwner())) { + stmt.setString(paramIndex++, request.getOwner() + "%"); + } + if (StringUtils.isNotBlank(request.getParentPath())) { + stmt.setString(paramIndex++, request.getParentPath()); + } + for (Integer deviceGroupId : deviceGroupIds) { + stmt.setInt(paramIndex++, deviceGroupId); + } + if (request.getRowCount() != 0) { + stmt.setInt(paramIndex++, request.getRowCount()); + stmt.setInt(paramIndex, request.getStartIndex()); + } + List deviceGroupList = new ArrayList<>(); + try (ResultSet resultSet = stmt.executeQuery()) { + while (resultSet.next()) { + deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet)); + } + } + return deviceGroupList; + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving groups of groups IDs " + deviceGroupIds.toString() + + " in tenant: " + tenantId; + log.error(msg); + throw new GroupManagementDAOException(msg, e); + } + } + @Override public int addGroup(DeviceGroup deviceGroup, int tenantId) throws GroupManagementDAOException { PreparedStatement stmt = null; @@ -59,9 +196,11 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { Connection conn = GroupManagementDAOFactory.getConnection(); String sql; if (deviceGroup.getStatus() == null || deviceGroup.getStatus().isEmpty()) { - sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID) VALUES (?, ?, ?, ?)"; + sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID, PARENT_PATH) " + + "VALUES (?, ?, ?, ?, ?)"; } else { - sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID, STATUS) VALUES (?, ?, ?, ?, ?)"; + sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID, PARENT_PATH, STATUS) " + + "VALUES (?, ?, ?, ?, ?, ?)"; hasStatus = true; } stmt = conn.prepareStatement(sql, new String[]{"ID"}); @@ -69,8 +208,9 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { stmt.setString(2, deviceGroup.getName()); stmt.setString(3, deviceGroup.getOwner()); stmt.setInt(4, tenantId); + stmt.setString(5, deviceGroup.getParentPath()); if (hasStatus) { - stmt.setString(5, deviceGroup.getStatus()); + stmt.setString(6, deviceGroup.getStatus()); } stmt.executeUpdate(); @@ -151,10 +291,12 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { try { Connection conn = GroupManagementDAOFactory.getConnection(); String sql = - "UPDATE DM_GROUP SET DESCRIPTION = ?, GROUP_NAME = ?, OWNER = ? WHERE ID = ? AND TENANT_ID = ?"; + "UPDATE DM_GROUP SET DESCRIPTION = ?, GROUP_NAME = ?, OWNER = ?, PARENT_PATH = ? WHERE ID = ? " + + "AND TENANT_ID = ?"; if (deviceGroup.getStatus() != null && !deviceGroup.getStatus().isEmpty()) { - sql = "UPDATE DM_GROUP SET DESCRIPTION = ?, GROUP_NAME = ?, OWNER = ?, STATUS = ? WHERE ID = ? AND TENANT_ID = ?"; + sql = "UPDATE DM_GROUP SET DESCRIPTION = ?, GROUP_NAME = ?, OWNER = ?, PARENT_PATH = ?, STATUS = ? " + + "WHERE ID = ? AND TENANT_ID = ?"; hasStatus = true; } stmt = conn.prepareStatement(sql); @@ -162,11 +304,12 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { stmt.setString(paramIndex++, deviceGroup.getDescription()); stmt.setString(paramIndex++, deviceGroup.getName()); stmt.setString(paramIndex++, deviceGroup.getOwner()); + stmt.setString(paramIndex++, deviceGroup.getParentPath()); if (hasStatus) { stmt.setString(paramIndex++, deviceGroup.getStatus()); } stmt.setInt(paramIndex++, groupId); - stmt.setInt(paramIndex++, tenantId); + stmt.setInt(paramIndex, tenantId); stmt.executeUpdate(); } catch (SQLException e) { throw new GroupManagementDAOException("Error occurred while updating deviceGroup '" + @@ -176,6 +319,32 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { } } + @Override + public void updateGroups(List deviceGroups, int tenantId) throws GroupManagementDAOException { + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + String sql = "UPDATE DM_GROUP SET DESCRIPTION = ?, GROUP_NAME = ?, OWNER = ?, STATUS = ?, " + + "PARENT_PATH = ? WHERE ID = ? AND TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)){ + for (DeviceGroup deviceGroup : deviceGroups) { + stmt.setString(1, deviceGroup.getDescription()); + stmt.setString(2, deviceGroup.getName()); + stmt.setString(3, deviceGroup.getOwner()); + stmt.setString(4, deviceGroup.getStatus()); + stmt.setString(5, deviceGroup.getParentPath()); + stmt.setInt(6, deviceGroup.getGroupId()); + stmt.setInt(7, tenantId); + stmt.addBatch(); + } + stmt.executeBatch(); + } + } catch (SQLException e) { + String msg = "Error occurred while updating groups as batch"; + log.error(msg); + throw new GroupManagementDAOException(msg, e); + } + } + @Override public void deleteGroup(int groupId, int tenantId) throws GroupManagementDAOException { Connection conn; @@ -217,6 +386,64 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { } } + @Override + public void deleteGroupsMapping(List groupIds, int tenantId) throws GroupManagementDAOException { + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_ROLE_GROUP_MAP WHERE GROUP_ID = ? AND TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + for (int groupId : groupIds) { + stmt.setInt(1, groupId); + stmt.setInt(2, tenantId); + stmt.addBatch(); + } + stmt.executeBatch(); + } + sql = "DELETE FROM DM_DEVICE_GROUP_MAP WHERE GROUP_ID = ? AND TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + for (int groupId : groupIds) { + stmt.setInt(1, groupId); + stmt.setInt(2, tenantId); + stmt.addBatch(); + } + stmt.executeBatch(); + } + sql = "DELETE FROM DM_DEVICE_GROUP_POLICY WHERE DEVICE_GROUP_ID = ? AND TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + for (int groupId : groupIds) { + stmt.setInt(1, groupId); + stmt.setInt(2, tenantId); + stmt.addBatch(); + } + stmt.executeBatch(); + } + } catch (SQLException e) { + String msg = "Error occurred while removing mappings of groups as batches"; + log.error(msg); + throw new GroupManagementDAOException(msg, e); + } + } + + @Override + public void deleteGroups(List groupIds, int tenantId) throws GroupManagementDAOException { + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + String sql = "DELETE FROM DM_GROUP WHERE ID = ? AND TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + for (int groupId : groupIds) { + stmt.setInt(1, groupId); + stmt.setInt(2, tenantId); + stmt.addBatch(); + } + stmt.executeBatch(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting groups as batches"; + log.error(msg); + throw new GroupManagementDAOException(msg, e); + } + } + public void deleteAllGroupProperties(int groupId, int tenantId) throws GroupManagementDAOException { PreparedStatement stmt = null; @@ -235,6 +462,25 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { } } + public void deleteAllGroupsProperties(List groupIds, int tenantId) throws GroupManagementDAOException { + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + String sql = "DELETE FROM GROUP_PROPERTIES WHERE GROUP_ID = ? AND TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + for (Integer groupId : groupIds) { + stmt.setInt(1, groupId); + stmt.setInt(2, tenantId); + stmt.addBatch(); + } + stmt.executeUpdate(); + } + } catch (SQLException e) { + String msg = "Error occurred while deleting properties of groups as batches"; + log.error(msg); + throw new GroupManagementDAOException(msg, e); + } + } + public Map getAllGroupProperties(int groupId, int tenantId) throws GroupManagementDAOException { PreparedStatement stmt = null; @@ -266,7 +512,8 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { ResultSet resultSet = null; try { Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS FROM DM_GROUP WHERE ID = ? AND TENANT_ID = ?"; + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP WHERE ID = ? " + + "AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, groupId); stmt.setInt(2, tenantId); @@ -284,6 +531,56 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { } } + @Override + public List getChildrenGroups(String parentPath, int tenantId) throws GroupManagementDAOException { + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP " + + "WHERE PARENT_PATH LIKE ? AND TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, parentPath + "%"); + stmt.setInt(2, tenantId); + List deviceGroupList = new ArrayList<>(); + try (ResultSet resultSet = stmt.executeQuery()) { + while (resultSet.next()) { + deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet)); + } + } + return deviceGroupList; + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving children group having parent path '" + parentPath + + "' in tenant: " + tenantId; + log.error(msg); + throw new GroupManagementDAOException(msg, e); + } + } + + @Override + public List getRootGroups(int tenantId) throws GroupManagementDAOException { + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP " + + "WHERE PARENT_PATH LIKE ? AND TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, "/"); + stmt.setInt(2, tenantId); + List deviceGroupList = new ArrayList<>(); + try (ResultSet resultSet = stmt.executeQuery()) { + deviceGroupList = new ArrayList<>(); + while (resultSet.next()) { + deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet)); + } + } + return deviceGroupList; + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving root groups in tenant: " + tenantId; + log.error(msg); + throw new GroupManagementDAOException(msg, e); + } + } + @Override public List getGroups(int deviceId, int tenantId) throws GroupManagementDAOException { PreparedStatement stmt = null; @@ -291,7 +588,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { List deviceGroupBuilders = new ArrayList<>(); try { Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT G.ID, G.GROUP_NAME, G.DESCRIPTION, G.OWNER, G.STATUS FROM DM_GROUP G " + + String sql = "SELECT G.ID, G.GROUP_NAME, G.DESCRIPTION, G.OWNER, G.STATUS, G.PARENT_PATH FROM DM_GROUP G " + "INNER JOIN DM_DEVICE_GROUP_MAP GM ON G.ID = GM.GROUP_ID " + "WHERE GM.DEVICE_ID = ? AND GM.TENANT_ID = ?"; stmt = conn.prepareStatement(sql); @@ -316,7 +613,8 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { List deviceGroupList = null; try { Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS FROM DM_GROUP WHERE TENANT_ID = ?"; + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP " + + "WHERE TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); resultSet = stmt.executeQuery(); @@ -389,6 +687,9 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { sql += " AND STATUS = ?"; hasStatus = true; } + if (StringUtils.isNotBlank(request.getParentPath())) { + sql += " AND PARENT_PATH = ?"; + } int paramIndex = 1; stmt = conn.prepareStatement(sql); @@ -397,10 +698,13 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { stmt.setString(paramIndex++, groupName + "%"); } if (hasOwner) { - stmt.setString(paramIndex, owner + "%"); + stmt.setString(paramIndex++, owner + "%"); } if (hasStatus) { - stmt.setString(paramIndex, request.getStatus()); + stmt.setString(paramIndex++, request.getStatus()); + } + if (StringUtils.isNotBlank(request.getParentPath())) { + stmt.setString(paramIndex, request.getParentPath()); } resultSet = stmt.executeQuery(); if (resultSet.next()) { @@ -422,7 +726,8 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { try { Connection conn = GroupManagementDAOFactory.getConnection(); String sql = - "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS FROM DM_GROUP WHERE GROUP_NAME = ? AND TENANT_ID = ?"; + "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP " + + "WHERE GROUP_NAME = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, groupName); stmt.setInt(2, tenantId); @@ -592,7 +897,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { List deviceGroupList = null; try { Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS FROM DM_GROUP g, " + + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP g, " + "(SELECT GROUP_ID FROM DM_ROLE_GROUP_MAP WHERE ROLE IN ("; int index = 0; @@ -658,7 +963,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { } @Override - public int getGroupsCount(String[] roles, int tenantId) throws GroupManagementDAOException { + public int getGroupsCount(String[] roles, int tenantId, String parentPath) throws GroupManagementDAOException { int rolesCount = roles.length; if (rolesCount == 0) { return 0; @@ -672,14 +977,20 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { for (int i = 0; i < rolesCount; i++) { sql += (rolesCount - 1 != i) ? "?," : "?"; } - sql += ")) gr WHERE g.ID = gr.GROUP_ID AND TENANT_ID = ? GROUP BY g.ID"; - + sql += ")) gr WHERE g.ID = gr.GROUP_ID AND TENANT_ID = ? "; + if (StringUtils.isNotBlank(parentPath)) { + sql += " AND g.PARENT_PATH = ? "; + } + sql += "GROUP BY g.ID"; stmt = conn.prepareStatement(sql); int index = 0; while (index++ < rolesCount) { stmt.setString(index, roles[index - 1]); } - stmt.setInt(index, tenantId); + stmt.setInt(index++, tenantId); + if (StringUtils.isNotBlank(parentPath)) { + stmt.setString(index, parentPath); + } resultSet = stmt.executeQuery(); if (resultSet.next()) { return resultSet.getInt("GROUP_COUNT"); @@ -700,7 +1011,8 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { List deviceGroupList = null; try { Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS FROM DM_GROUP WHERE OWNER = ? AND TENANT_ID = ?"; + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP " + + "WHERE OWNER = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, username); stmt.setInt(2, tenantId); @@ -744,15 +1056,21 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { } @Override - public int getOwnGroupsCount(String username, int tenantId) throws GroupManagementDAOException { + public int getOwnGroupsCount(String username, int tenantId, String parentPath) throws GroupManagementDAOException { PreparedStatement stmt = null; ResultSet resultSet = null; try { Connection conn = GroupManagementDAOFactory.getConnection(); String sql = "SELECT COUNT(ID) AS GROUP_COUNT FROM DM_GROUP WHERE OWNER = ? AND TENANT_ID = ?"; + if (StringUtils.isNotBlank(parentPath)) { + sql += " AND PARENT_PATH = ?"; + } stmt = conn.prepareStatement(sql); stmt.setString(1, username); stmt.setInt(2, tenantId); + if (StringUtils.isNotBlank(parentPath)) { + stmt.setString(3, parentPath); + } resultSet = stmt.executeQuery(); if (resultSet.next()) { return resultSet.getInt("GROUP_COUNT"); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/GenericGroupDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/GenericGroupDAOImpl.java index d980e9e0ab..96ca67edd0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/GenericGroupDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/GenericGroupDAOImpl.java @@ -14,18 +14,32 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * + * Copyright (c) 2021, Entgra (pvt) Ltd. (https://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.core.dao.impl.group; import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; -import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.impl.AbstractGroupDAOImpl; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; -import org.wso2.carbon.device.mgt.core.dao.util.GroupManagementDAOUtil; import java.sql.Connection; import java.sql.PreparedStatement; @@ -38,134 +52,6 @@ import java.util.List; * This class represents implementation of GroupDAO */ public class GenericGroupDAOImpl extends AbstractGroupDAOImpl { - @Override - public List getGroups(GroupPaginationRequest request, int tenantId) - throws GroupManagementDAOException { - PreparedStatement stmt = null; - ResultSet resultSet = null; - List deviceGroupList = null; - - String groupName = request.getGroupName(); - boolean hasGroupName = false; - String owner = request.getOwner(); - String status = request.getStatus(); - boolean hasOwner = false; - boolean hasStatus = false; - boolean hasLimit = request.getRowCount() != 0; - - try { - Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS FROM DM_GROUP WHERE TENANT_ID = ?"; - if (groupName != null && !groupName.isEmpty()) { - sql += " AND UPPER(GROUP_NAME) LIKE ?"; - hasGroupName = true; - } - if (owner != null && !owner.isEmpty()) { - sql += " AND UPPER(OWNER) LIKE ?"; - hasOwner = true; - } - if (status != null && !status.isEmpty()) { - sql += " AND STATUS = ?"; - hasStatus = true; - } - if (hasLimit) { - sql += " LIMIT ?, ?"; - } - - int paramIndex = 1; - stmt = conn.prepareStatement(sql); - stmt.setInt(paramIndex++, tenantId); - if (hasGroupName) { - stmt.setString(paramIndex++, groupName + "%"); - } - if (hasOwner) { - stmt.setString(paramIndex++, owner + "%"); - } - if (hasStatus) { - stmt.setString(paramIndex++, status.toUpperCase()); - } - if (hasLimit) { - stmt.setInt(paramIndex++, request.getStartIndex()); - stmt.setInt(paramIndex, request.getRowCount()); - } - resultSet = stmt.executeQuery(); - deviceGroupList = new ArrayList<>(); - while (resultSet.next()) { - deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet)); - } - } catch (SQLException e) { - throw new GroupManagementDAOException("Error occurred while listing all groups in tenant: " + tenantId, e); - } finally { - GroupManagementDAOUtil.cleanupResources(stmt, resultSet); - } - return deviceGroupList; - } - - @Override - public List getGroups(GroupPaginationRequest request, List deviceGroupIds, - int tenantId) throws GroupManagementDAOException { - int deviceGroupIdsCount = deviceGroupIds.size(); - if (deviceGroupIdsCount == 0) { - return new ArrayList<>(); - } - PreparedStatement stmt = null; - ResultSet resultSet = null; - List deviceGroupList = null; - - String groupName = request.getGroupName(); - boolean hasGroupName = false; - String owner = request.getOwner(); - boolean hasOwner = false; - boolean hasLimit = request.getRowCount() != 0; - - try { - Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS FROM DM_GROUP WHERE TENANT_ID = ?"; - if (groupName != null && !groupName.isEmpty()) { - sql += " AND GROUP_NAME LIKE ?"; - hasGroupName = true; - } - if (owner != null && !owner.isEmpty()) { - sql += " AND OWNER LIKE ?"; - hasOwner = true; - } - sql += " AND ID IN ("; - for (int i = 0; i < deviceGroupIdsCount; i++) { - sql += (deviceGroupIdsCount - 1 != i) ? "?," : "?"; - } - sql += ")"; - if (hasLimit) { - sql += " LIMIT ?, ?"; - } - - int paramIndex = 1; - stmt = conn.prepareStatement(sql); - stmt.setInt(paramIndex++, tenantId); - if (hasGroupName) { - stmt.setString(paramIndex++, groupName + "%"); - } - if (hasOwner) { - stmt.setString(paramIndex++, owner + "%"); - } - for (Integer deviceGroupId : deviceGroupIds) { - stmt.setInt(paramIndex++, deviceGroupId); - } - if (hasLimit) { - stmt.setInt(paramIndex++, request.getStartIndex()); - stmt.setInt(paramIndex, request.getRowCount()); - } - resultSet = stmt.executeQuery(); - deviceGroupList = new ArrayList<>(); - while (resultSet.next()) { - deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet)); - } - } catch (SQLException e) { - throw new GroupManagementDAOException("Error occurred while listing all groups in tenant: " + tenantId, e); - } finally { - GroupManagementDAOUtil.cleanupResources(stmt, resultSet); - } - return deviceGroupList; - } @Override public List getDevices(int groupId, int startIndex, int rowCount, int tenantId) @@ -209,4 +95,4 @@ public class GenericGroupDAOImpl extends AbstractGroupDAOImpl { } return devices; } -} \ No newline at end of file +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/OracleGroupDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/OracleGroupDAOImpl.java index 4074840e0f..e7473b6b53 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/OracleGroupDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/OracleGroupDAOImpl.java @@ -14,10 +14,28 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * + * Copyright (c) 2021, Entgra (pvt) Ltd. (https://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.core.dao.impl.group; +import org.apache.commons.lang.StringUtils; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; @@ -56,7 +74,8 @@ public class OracleGroupDAOImpl extends AbstractGroupDAOImpl { try { Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS FROM DM_GROUP WHERE TENANT_ID = ?"; + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP " + + "WHERE TENANT_ID = ?"; if (groupName != null && !groupName.isEmpty()) { sql += " AND GROUP_NAME LIKE ?"; hasGroupName = true; @@ -69,6 +88,9 @@ public class OracleGroupDAOImpl extends AbstractGroupDAOImpl { sql += " AND STATUS = ?"; hasStatus = true; } + if (StringUtils.isNotBlank(request.getParentPath())) { + sql += " AND PARENT_PATH LIKE ?"; + } if (hasLimit) { sql += " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; } @@ -85,6 +107,9 @@ public class OracleGroupDAOImpl extends AbstractGroupDAOImpl { if (hasStatus) { stmt.setString(paramIndex++, status.toUpperCase()); } + if (StringUtils.isNotBlank(request.getParentPath())) { + stmt.setString(paramIndex++, request.getParentPath()); + } if (hasLimit) { stmt.setInt(paramIndex++, request.getStartIndex()); stmt.setInt(paramIndex, request.getRowCount()); @@ -121,7 +146,8 @@ public class OracleGroupDAOImpl extends AbstractGroupDAOImpl { try { Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS FROM DM_GROUP WHERE TENANT_ID = ?"; + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP " + + "WHERE TENANT_ID = ?"; if (groupName != null && !groupName.isEmpty()) { sql += " AND GROUP_NAME LIKE ?"; hasGroupName = true; @@ -130,6 +156,9 @@ public class OracleGroupDAOImpl extends AbstractGroupDAOImpl { sql += " AND OWNER LIKE ?"; hasOwner = true; } + if (StringUtils.isNotBlank(request.getParentPath())) { + sql += " AND PARENT_PATH LIKE ?"; + } sql += " AND ID IN ("; for (int i = 0; i < deviceGroupIdsCount; i++) { sql += (deviceGroupIdsCount - 1 != i) ? "?," : "?"; @@ -148,6 +177,9 @@ public class OracleGroupDAOImpl extends AbstractGroupDAOImpl { if (hasOwner) { stmt.setString(paramIndex++, owner + "%"); } + if (StringUtils.isNotBlank(request.getParentPath())) { + stmt.setString(paramIndex++, request.getParentPath()); + } for (Integer deviceGroupId : deviceGroupIds) { stmt.setInt(paramIndex++, deviceGroupId); } @@ -210,4 +242,4 @@ public class OracleGroupDAOImpl extends AbstractGroupDAOImpl { } return devices; } -} \ No newline at end of file +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/PostgreSQLGroupDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/PostgreSQLGroupDAOImpl.java index 4140a5c48a..a01ef5b6b8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/PostgreSQLGroupDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/PostgreSQLGroupDAOImpl.java @@ -14,13 +14,29 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * + * Copyright (c) 2021, Entgra (pvt) Ltd. (https://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.core.dao.impl.group; import org.apache.solr.common.StringUtils; import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; @@ -50,10 +66,10 @@ public class PostgreSQLGroupDAOImpl extends AbstractGroupDAOImpl { Connection conn = GroupManagementDAOFactory.getConnection(); String sql; if(StringUtils.isEmpty(deviceGroup.getStatus())) { - sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID) " + + sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID, PARENT_PATH) " + "VALUES (?, ?, ?, ?) RETURNING ID"; } else { - sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID, STATUS) " + + sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID, PARENT_PATH, STATUS) " + "VALUES (?, ?, ?, ?, ?) RETURNING ID"; hasStatus = true; } @@ -62,8 +78,9 @@ public class PostgreSQLGroupDAOImpl extends AbstractGroupDAOImpl { stmt.setString(2, deviceGroup.getName()); stmt.setString(3, deviceGroup.getOwner()); stmt.setInt(4, tenantId); + stmt.setString(5, deviceGroup.getParentPath()); if(hasStatus) { - stmt.setString(5, deviceGroup.getStatus()); + stmt.setString(6, deviceGroup.getStatus()); } stmt.execute(); rs = stmt.getGeneratedKeys(); @@ -79,135 +96,6 @@ public class PostgreSQLGroupDAOImpl extends AbstractGroupDAOImpl { } } - @Override - public List getGroups(GroupPaginationRequest request, int tenantId) - throws GroupManagementDAOException { - PreparedStatement stmt = null; - ResultSet resultSet = null; - List deviceGroupList = null; - - String groupName = request.getGroupName(); - boolean hasGroupName = false; - String owner = request.getOwner(); - String status = request.getStatus(); - boolean hasOwner = false; - boolean hasStatus = false; - boolean hasLimit = request.getRowCount() != 0; - - try { - Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS FROM DM_GROUP WHERE TENANT_ID = ?"; - if (groupName != null && !groupName.isEmpty()) { - sql += " AND GROUP_NAME LIKE ?"; - hasGroupName = true; - } - if (owner != null && !owner.isEmpty()) { - sql += " AND OWNER LIKE ?"; - hasOwner = true; - } - if (status != null && !status.isEmpty()) { - sql += " AND STATUS = ?"; - hasStatus = true; - } - if (hasLimit) { - sql += " LIMIT ? OFFSET ?"; - } - - int paramIndex = 1; - stmt = conn.prepareStatement(sql); - stmt.setInt(paramIndex++, tenantId); - if (hasGroupName) { - stmt.setString(paramIndex++, groupName + "%"); - } - if (hasOwner) { - stmt.setString(paramIndex++, owner + "%"); - } - if (hasStatus) { - stmt.setString(paramIndex++, status.toUpperCase()); - } - if (hasLimit) { - stmt.setInt(paramIndex++, request.getRowCount()); - stmt.setInt(paramIndex, request.getStartIndex()); - } - resultSet = stmt.executeQuery(); - deviceGroupList = new ArrayList<>(); - while (resultSet.next()) { - deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet)); - } - } catch (SQLException e) { - throw new GroupManagementDAOException("Error occurred while listing all groups in tenant: " + tenantId, e); - } finally { - GroupManagementDAOUtil.cleanupResources(stmt, resultSet); - } - return deviceGroupList; - } - - @Override - public List getGroups(GroupPaginationRequest request, List deviceGroupIds, - int tenantId) throws GroupManagementDAOException { - int deviceGroupIdsCount = deviceGroupIds.size(); - if (deviceGroupIdsCount == 0) { - return new ArrayList<>(); - } - PreparedStatement stmt = null; - ResultSet resultSet = null; - List deviceGroupList = null; - - String groupName = request.getGroupName(); - boolean hasGroupName = false; - String owner = request.getOwner(); - boolean hasOwner = false; - boolean hasLimit = request.getRowCount() != 0; - - try { - Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS FROM DM_GROUP WHERE TENANT_ID = ?"; - if (groupName != null && !groupName.isEmpty()) { - sql += " AND GROUP_NAME LIKE ?"; - hasGroupName = true; - } - if (owner != null && !owner.isEmpty()) { - sql += " AND OWNER LIKE ?"; - hasOwner = true; - } - sql += " AND ID IN ("; - for (int i = 0; i < deviceGroupIdsCount; i++) { - sql += (deviceGroupIdsCount - 1 != i) ? "?," : "?"; - } - sql += ")"; - if (hasLimit) { - sql += " LIMIT ? OFFSET ?"; - } - - int paramIndex = 1; - stmt = conn.prepareStatement(sql); - stmt.setInt(paramIndex++, tenantId); - if (hasGroupName) { - stmt.setString(paramIndex++, groupName + "%"); - } - if (hasOwner) { - stmt.setString(paramIndex++, owner + "%"); - } - for (Integer deviceGroupId : deviceGroupIds) { - stmt.setInt(paramIndex++, deviceGroupId); - } - if (hasLimit) { - stmt.setInt(paramIndex++, request.getRowCount()); - stmt.setInt(paramIndex, request.getStartIndex()); - } - resultSet = stmt.executeQuery(); - deviceGroupList = new ArrayList<>(); - while (resultSet.next()) { - deviceGroupList.add(GroupManagementDAOUtil.loadGroup(resultSet)); - } - } catch (SQLException e) { - throw new GroupManagementDAOException("Error occurred while listing all groups in tenant: " + tenantId, e); - } finally { - GroupManagementDAOUtil.cleanupResources(stmt, resultSet); - } - return deviceGroupList; - } - @Override public List getDevices(int groupId, int startIndex, int rowCount, int tenantId) throws GroupManagementDAOException { @@ -250,4 +138,4 @@ public class PostgreSQLGroupDAOImpl extends AbstractGroupDAOImpl { } return devices; } -} \ No newline at end of file +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/SQLServerGroupDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/SQLServerGroupDAOImpl.java index 879d450349..5065628b08 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/SQLServerGroupDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/group/SQLServerGroupDAOImpl.java @@ -14,10 +14,28 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * + * Copyright (c) 2021, Entgra (pvt) Ltd. (https://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.core.dao.impl.group; +import org.apache.commons.lang.StringUtils; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; @@ -56,7 +74,8 @@ public class SQLServerGroupDAOImpl extends AbstractGroupDAOImpl { try { Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS FROM DM_GROUP WHERE TENANT_ID = ?"; + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP " + + "WHERE TENANT_ID = ?"; if (groupName != null && !groupName.isEmpty()) { sql += " AND GROUP_NAME LIKE ?"; hasGroupName = true; @@ -69,6 +88,9 @@ public class SQLServerGroupDAOImpl extends AbstractGroupDAOImpl { sql += " AND STATUS = ?"; hasStatus = true; } + if (StringUtils.isNotBlank(request.getParentPath())) { + sql += " AND PARENT_PATH LIKE ?"; + } if (hasLimit) { sql += " ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; } @@ -85,6 +107,9 @@ public class SQLServerGroupDAOImpl extends AbstractGroupDAOImpl { if (hasStatus) { stmt.setString(paramIndex++, status.toUpperCase()); } + if (StringUtils.isNotBlank(request.getParentPath())) { + stmt.setString(paramIndex++, request.getParentPath()); + } if (hasLimit) { stmt.setInt(paramIndex++, request.getStartIndex()); stmt.setInt(paramIndex, request.getRowCount()); @@ -121,7 +146,8 @@ public class SQLServerGroupDAOImpl extends AbstractGroupDAOImpl { try { Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS FROM DM_GROUP WHERE TENANT_ID = ?"; + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP " + + "WHERE TENANT_ID = ?"; if (groupName != null && !groupName.isEmpty()) { sql += " AND GROUP_NAME LIKE ?"; hasGroupName = true; @@ -130,6 +156,9 @@ public class SQLServerGroupDAOImpl extends AbstractGroupDAOImpl { sql += " AND OWNER LIKE ?"; hasOwner = true; } + if (StringUtils.isNotBlank(request.getParentPath())) { + sql += " AND PARENT_PATH LIKE ?"; + } sql += " AND ID IN ("; for (int i = 0; i < deviceGroupIdsCount; i++) { sql += (deviceGroupIdsCount - 1 != i) ? "?," : "?"; @@ -148,6 +177,9 @@ public class SQLServerGroupDAOImpl extends AbstractGroupDAOImpl { if (hasOwner) { stmt.setString(paramIndex++, owner + "%"); } + if (StringUtils.isNotBlank(request.getParentPath())) { + stmt.setString(paramIndex++, request.getParentPath()); + } for (Integer deviceGroupId : deviceGroupIds) { stmt.setInt(paramIndex++, deviceGroupId); } @@ -210,4 +242,4 @@ public class SQLServerGroupDAOImpl extends AbstractGroupDAOImpl { } return devices; } -} \ No newline at end of file +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/GroupManagementDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/GroupManagementDAOUtil.java index b3acd1d313..3025fb6d0a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/GroupManagementDAOUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/GroupManagementDAOUtil.java @@ -14,6 +14,22 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * Copyright (c) 2021, Entgra (pvt) Ltd. (https://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.core.dao.util; @@ -86,6 +102,7 @@ public final class GroupManagementDAOUtil { group.setName(resultSet.getString("GROUP_NAME")); group.setOwner(resultSet.getString("OWNER")); group.setStatus(resultSet.getString("STATUS")); + group.setParentPath(resultSet.getString("PARENT_PATH")); return group; } From 90d8b94455fccaf7507040e8dfd60cb93ab51877 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 24 Jun 2021 20:31:11 +0530 Subject: [PATCH 10/17] Add and update service layer related to hierarchical changes (cherry picked from commit 0ae6c6246aa15c577720b01ebb412c4e382e2e91) --- .../GroupManagementProviderService.java | 57 +++- .../GroupManagementProviderServiceImpl.java | 269 +++++++++++++++--- .../mgt/core/util/DeviceManagerUtil.java | 13 + 3 files changed, 291 insertions(+), 48 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java index f729218314..c5e7417c86 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java @@ -14,6 +14,23 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * + * Copyright (c) 2021, Entgra (pvt) Ltd. (https://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.core.service; @@ -60,10 +77,11 @@ public interface GroupManagementProviderService { * Delete existing device group. * * @param groupId to be deleted. + * @param isDeleteChildren to delete the children groups or not. * @return status of the delete operation. * @throws GroupManagementException */ - boolean deleteGroup(int groupId) throws GroupManagementException; + boolean deleteGroup(int groupId, boolean isDeleteChildren) throws GroupManagementException; /** * Get the device group provided the device group id. @@ -75,6 +93,17 @@ public interface GroupManagementProviderService { */ DeviceGroup getGroup(int groupId, boolean requireGroupProps) throws GroupManagementException; + /** + * Get the device group provided the device group id and depth of children groups. + * + * @param groupId of the group. + * @param requireGroupProps to include group properties. + * @param depth of children groups to retrieve. + * @return {@link DeviceGroup} group with details. + * @throws GroupManagementException on error during retrieval of group + */ + DeviceGroup getGroup(int groupId, boolean requireGroupProps, int depth) throws GroupManagementException; + /** * Get the device group provided the device group name. * @@ -85,6 +114,17 @@ public interface GroupManagementProviderService { */ DeviceGroup getGroup(String groupName, boolean requireGroupProps) throws GroupManagementException; + /** + * Get the device group provided the device group id and depth of children groups. + * + * @param groupName of the group. + * @param requireGroupProps to include group properties. + * @param depth of children groups to retrieve. + * @return {@link DeviceGroup} group with details. + * @throws GroupManagementException on error during retrieval of group + */ + DeviceGroup getGroup(String groupName, boolean requireGroupProps, int depth) throws GroupManagementException; + /** * Get all device groups in tenant. * @@ -127,6 +167,18 @@ public interface GroupManagementProviderService { PaginationResult getGroups(String username, GroupPaginationRequest paginationRequest, boolean requireGroupProps) throws GroupManagementException; + /** + * Get device groups with children groups hierarchically which belongs to specified user with pagination. + * + * @param username of the user. + * @param request to filter results + * @param requireGroupProps to include group properties + * @return {@link PaginationResult} paginated groups. + * @throws GroupManagementException on error during retrieval of groups with hierarchy + */ + PaginationResult getGroupsWithHierarchy(String username, GroupPaginationRequest request, + boolean requireGroupProps) throws GroupManagementException; + /** * Get all device group count in tenant * @@ -147,10 +199,11 @@ public interface GroupManagementProviderService { * Get device group count of user * * @param username of the user + * @param parentPath of the group * @return group count * @throws GroupManagementException */ - int getGroupCount(String username) throws GroupManagementException; + int getGroupCount(String username, String parentPath) throws GroupManagementException; /** * Manage device group sharing with user with list of roles. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java index b28cbefe25..35f4915481 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -14,10 +14,28 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * + * Copyright (c) 2021, Entgra (pvt) Ltd. (https://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.core.service; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.CarbonConstants; @@ -36,14 +54,12 @@ import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupNotExistException; import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException; -import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.GroupDAO; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; -import org.wso2.carbon.device.mgt.core.event.config.EventOperationTaskConfiguration; import org.wso2.carbon.device.mgt.core.event.config.GroupAssignmentEventOperationExecutor; import org.wso2.carbon.device.mgt.core.geo.task.GeoFenceEventOperationManager; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; @@ -56,10 +72,11 @@ import org.wso2.carbon.user.api.UserStoreManager; import java.sql.SQLException; import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; +import java.util.stream.Collectors; public class GroupManagementProviderServiceImpl implements GroupManagementProviderService { @@ -95,6 +112,20 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid GroupManagementDAOFactory.beginTransaction(); DeviceGroup existingGroup = this.groupDAO.getGroup(deviceGroup.getName(), tenantId); if (existingGroup == null) { + if (deviceGroup.getParentGroupId() == 0) { + deviceGroup.setParentPath("/"); + } else { + DeviceGroup immediateParentGroup = groupDAO.getGroup(deviceGroup.getParentGroupId(), tenantId); + if (immediateParentGroup == null) { + String msg = "Parent group with group ID '" + deviceGroup.getParentGroupId() + + "' does not exist. Hence creating of group '" + deviceGroup.getName() + + "' was not success"; + log.error(msg); + throw new GroupManagementException(msg); + } + String parentPath = DeviceManagerUtil.createParentPath(immediateParentGroup); + deviceGroup.setParentPath(parentPath); + } int updatedGroupID = this.groupDAO.addGroup(deviceGroup, tenantId); if (deviceGroup.getGroupProperties() != null && deviceGroup.getGroupProperties().size() > 0) { this.groupDAO.addGroupProperties(deviceGroup, updatedGroupID, tenantId); @@ -146,6 +177,20 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid GroupManagementDAOFactory.beginTransaction(); DeviceGroup existingGroup = this.groupDAO.getGroup(groupId, tenantId); if (existingGroup != null) { + if (deviceGroup.getParentGroupId() == 0) { + deviceGroup.setParentPath("/"); + } else { + DeviceGroup immediateParentGroup = groupDAO.getGroup(deviceGroup.getParentGroupId(), tenantId); + if (immediateParentGroup == null) { + String msg = "Parent group with group ID '" + deviceGroup.getParentGroupId() + + "' does not exist. Hence updating of group '" + groupId + + "' was not success"; + log.error(msg); + throw new GroupManagementException(msg); + } + String parentPath = DeviceManagerUtil.createParentPath(immediateParentGroup); + deviceGroup.setParentPath(parentPath); + } this.groupDAO.updateGroup(deviceGroup, groupId, tenantId); if (deviceGroup.getGroupProperties() != null && deviceGroup.getGroupProperties().size() > 0) { this.groupDAO.updateGroupProperties(deviceGroup, groupId, tenantId); @@ -178,7 +223,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid * {@inheritDoc} */ @Override - public boolean deleteGroup(int groupId) throws GroupManagementException { + public boolean deleteGroup(int groupId, boolean isDeleteChildren) throws GroupManagementException { if (log.isDebugEnabled()) { log.debug("Delete group: " + groupId); } @@ -189,8 +234,37 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); try { GroupManagementDAOFactory.beginTransaction(); - this.groupDAO.deleteGroup(groupId, tenantId); - this.groupDAO.deleteAllGroupProperties(groupId, tenantId); + List childrenGroups = new ArrayList<>(); + List groupIdsToDelete = new ArrayList<>(); + if (deviceGroup.getChildrenGroups() != null && !deviceGroup.getChildrenGroups().isEmpty()) { + String parentPath = DeviceManagerUtil.createParentPath(deviceGroup); + childrenGroups = groupDAO.getChildrenGroups(parentPath, tenantId); + if (isDeleteChildren) { + groupIdsToDelete = childrenGroups.stream().map(DeviceGroup::getGroupId) + .collect(Collectors.toList()); + } else { + for (DeviceGroup childrenGroup : childrenGroups) { + String newParentPath = childrenGroup.getParentPath() + .replace("/" + deviceGroup.getGroupId(), ""); + if (StringUtils.isEmpty(newParentPath)) { + newParentPath = "/"; + } + childrenGroup.setParentPath(newParentPath); + } + } + } + if (isDeleteChildren) { + groupIdsToDelete.add(groupId); + groupDAO.deleteGroupsMapping(groupIdsToDelete, tenantId); + groupDAO.deleteGroups(groupIdsToDelete, tenantId); + groupDAO.deleteAllGroupsProperties(groupIdsToDelete, tenantId); + } else { + groupDAO.deleteGroup(groupId, tenantId); + groupDAO.deleteAllGroupProperties(groupId, tenantId); + if (!childrenGroups.isEmpty()) { + groupDAO.updateGroups(childrenGroups, tenantId); + } + } GroupManagementDAOFactory.commitTransaction(); if (log.isDebugEnabled()) { log.debug("DeviceGroup " + deviceGroup.getName() + " removed."); @@ -219,6 +293,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid */ @Override public DeviceGroup getGroup(int groupId, boolean requireGroupProps) throws GroupManagementException { + return getGroup(groupId, requireGroupProps, 1); + } + + @Override + public DeviceGroup getGroup(int groupId, boolean requireGroupProps, int depth) throws GroupManagementException { if (log.isDebugEnabled()) { log.debug("Get group by id: " + groupId); } @@ -227,8 +306,13 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid try { GroupManagementDAOFactory.openConnection(); deviceGroup = this.groupDAO.getGroup(groupId, tenantId); - if (requireGroupProps) { - populateGroupProperties(deviceGroup, tenantId); + if (deviceGroup != null) { + String parentPath = DeviceManagerUtil.createParentPath(deviceGroup); + List childrenGroups = groupDAO.getChildrenGroups(parentPath, tenantId); + createGroupWithChildren(deviceGroup, childrenGroups, requireGroupProps, tenantId, depth, 0); + if (requireGroupProps) { + populateGroupProperties(deviceGroup, tenantId); + } } return deviceGroup; } catch (GroupManagementDAOException e) { @@ -236,11 +320,8 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(msg, e); throw new GroupManagementException(msg, e); } catch (SQLException e) { - String msg = "Error occurred while opening a connection to the data source."; - log.error(msg, e); - throw new GroupManagementException(msg, e); - } catch (Exception e) { - String msg = "Error occurred in getGroup for groupId: " + groupId; + String msg = "Error occurred while opening a connection to the data source to retrieve the group '" + + groupId + "'"; log.error(msg, e); throw new GroupManagementException(msg, e); } finally { @@ -253,6 +334,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid */ @Override public DeviceGroup getGroup(String groupName, boolean requireGroupProps) throws GroupManagementException { + return getGroup(groupName, requireGroupProps, 1); + } + + @Override + public DeviceGroup getGroup(String groupName, boolean requireGroupProps, int depth) throws GroupManagementException { if (groupName == null) { String msg = "Received empty groupName for getGroup"; log.error(msg); @@ -266,8 +352,13 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid try { GroupManagementDAOFactory.openConnection(); deviceGroup = this.groupDAO.getGroup(groupName, tenantId); - if (requireGroupProps) { - populateGroupProperties(deviceGroup, tenantId); + if (deviceGroup != null) { + String parentPath = DeviceManagerUtil.createParentPath(deviceGroup); + List childrenGroups = groupDAO.getChildrenGroups(parentPath, tenantId); + createGroupWithChildren(deviceGroup, childrenGroups, requireGroupProps, tenantId, depth , 0); + if (requireGroupProps) { + populateGroupProperties(deviceGroup, tenantId); + } } return deviceGroup; } catch (GroupManagementDAOException e) { @@ -275,11 +366,8 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(msg, e); throw new GroupManagementException(msg, e); } catch (SQLException e) { - String msg = "Error occurred while opening a connection to the data source."; - log.error(msg, e); - throw new GroupManagementException(msg, e); - } catch (Exception e) { - String msg = "Error occurred in getGroup with name " + groupName; + String msg = "Error occurred while opening a connection to the data source to retrieve group with name '" + + groupName + "'"; log.error(msg, e); throw new GroupManagementException(msg, e); } finally { @@ -296,12 +384,10 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.openConnection(); - deviceGroups = this.groupDAO.getGroups(tenantId); - if (requireGroupProps) { - if (deviceGroups != null && !deviceGroups.isEmpty()) { - for (DeviceGroup group : deviceGroups) { - populateGroupProperties(group, tenantId); - } + deviceGroups = groupDAO.getRootGroups(tenantId); + for (DeviceGroup deviceGroup : deviceGroups) { + if (requireGroupProps) { + populateGroupProperties(deviceGroup, tenantId); } } return deviceGroups; @@ -334,16 +420,15 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.debug("Get groups with pagination " + request.toString()); } request = DeviceManagerUtil.validateGroupListPageSize(request); - List deviceGroups; + List rootGroups; try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.openConnection(); - deviceGroups = this.groupDAO.getGroups(request, tenantId); - if (requireGroupProps) { - if (deviceGroups != null && !deviceGroups.isEmpty()) { - for (DeviceGroup group : deviceGroups) { - populateGroupProperties(group, tenantId); - } + request.setParentPath("/"); + rootGroups = this.groupDAO.getGroups(request, tenantId); + for (DeviceGroup rootGroup : rootGroups) { + if (requireGroupProps) { + populateGroupProperties(rootGroup, tenantId); } } } catch (GroupManagementDAOException e) { @@ -362,11 +447,68 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid GroupManagementDAOFactory.closeConnection(); } PaginationResult groupResult = new PaginationResult(); - groupResult.setData(deviceGroups); + groupResult.setData(rootGroups); groupResult.setRecordsTotal(getGroupCount(request)); return groupResult; } + @Override + public PaginationResult getGroupsWithHierarchy(String username, GroupPaginationRequest request, + boolean requireGroupProps) throws GroupManagementException { + if (request == null) { + String msg = "Received incomplete data for retrieve groups with hierarchy"; + log.error(msg); + throw new GroupManagementException(msg); + } + if (log.isDebugEnabled()) { + log.debug("Get groups with hierarchy " + request.toString()); + } + DeviceManagerUtil.validateGroupListPageSize(request); + List rootGroups; + try { + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + request.setParentPath("/"); + if (StringUtils.isBlank(username)) { + GroupManagementDAOFactory.openConnection(); + rootGroups = groupDAO.getGroups(request, tenantId); + } else { + List allDeviceGroupIdsOfUser = getGroupIds(username); + GroupManagementDAOFactory.openConnection(); + rootGroups = this.groupDAO.getGroups(request, allDeviceGroupIdsOfUser, tenantId); + } + String parentPath; + List childrenGroups; + for (DeviceGroup rootGroup : rootGroups) { + parentPath = DeviceManagerUtil.createParentPath(rootGroup); + childrenGroups = groupDAO.getChildrenGroups(parentPath, tenantId); + createGroupWithChildren( + rootGroup, childrenGroups, requireGroupProps, tenantId, request.getDepth(), 0); + if (requireGroupProps) { + populateGroupProperties(rootGroup, tenantId); + } + } + } catch (GroupManagementDAOException e) { + String msg = "Error occurred while retrieving all groups with hierarchy"; + log.error(msg, e); + throw new GroupManagementException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while opening a connection to the data source to retrieve all groups " + + "with hierarchy"; + log.error(msg, e); + throw new GroupManagementException(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + PaginationResult groupResult = new PaginationResult(); + groupResult.setData(rootGroups); + if (StringUtils.isBlank(username)) { + groupResult.setRecordsTotal(getGroupCount(request)); + } else { + groupResult.setRecordsTotal(getGroupCount(username, request.getParentPath())); + } + return groupResult; + } + @Override public List getGroups(String username, boolean requireGroupProps) throws GroupManagementException { if (username == null || username.isEmpty()) { @@ -457,16 +599,15 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } request = DeviceManagerUtil.validateGroupListPageSize(request); List allDeviceGroupIdsOfUser = getGroupIds(currentUser); - List allMatchingGroups; + List rootGroups; try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.openConnection(); - allMatchingGroups = this.groupDAO.getGroups(request, allDeviceGroupIdsOfUser, tenantId); - if (requireGroupProps) { - if (allMatchingGroups != null && !allMatchingGroups.isEmpty()) { - for (DeviceGroup group : allMatchingGroups) { - populateGroupProperties(group, tenantId); - } + request.setParentPath("/"); + rootGroups = this.groupDAO.getGroups(request, allDeviceGroupIdsOfUser, tenantId); + for (DeviceGroup rootGroup : rootGroups) { + if (requireGroupProps) { + populateGroupProperties(rootGroup, tenantId); } } } catch (GroupManagementDAOException | SQLException e) { @@ -481,8 +622,8 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid GroupManagementDAOFactory.closeConnection(); } PaginationResult groupResult = new PaginationResult(); - groupResult.setData(allMatchingGroups); - groupResult.setRecordsTotal(getGroupCount(currentUser)); + groupResult.setData(rootGroups); + groupResult.setRecordsTotal(getGroupCount(currentUser, request.getParentPath())); return groupResult; } @@ -557,7 +698,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid * {@inheritDoc} */ @Override - public int getGroupCount(String username) throws GroupManagementException { + public int getGroupCount(String username, String parentPath) throws GroupManagementException { if (username == null || username.isEmpty()) { String msg = "Received empty user name for getGroupCount"; log.error(msg); @@ -574,8 +715,8 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid .getUserStoreManager(); String[] roleList = userStoreManager.getRoleListOfUser(username); GroupManagementDAOFactory.openConnection(); - count = groupDAO.getOwnGroupsCount(username, tenantId); - count += groupDAO.getGroupsCount(roleList, tenantId); + count = groupDAO.getOwnGroupsCount(username, tenantId, parentPath); + count += groupDAO.getGroupsCount(roleList, tenantId, parentPath); return count; } catch (UserStoreException e) { String msg = "Error occurred while retrieving role list of user '" + username + "'"; @@ -1131,4 +1272,40 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } } } + + /** + * Recursive method to create group with children based on params to provide hierarchical grouping. + * @param parentGroup to which children group should be set. + * @param childrenGroups which are descendants of parent group. + * @param requireGroupProps to include device properties. + * @param tenantId of the group. + * @param depth of children groups set and when reaches recursive call returns to callee. + * @param counter to track the recursive calls and to stop when reaches the depth. + * @throws GroupManagementDAOException on error during population of group properties. + */ + private void createGroupWithChildren(DeviceGroup parentGroup, List childrenGroups, + boolean requireGroupProps, int tenantId, int depth, int counter) throws GroupManagementDAOException { + if (childrenGroups.isEmpty() || depth == counter) { + return; + } + List immediateChildrenGroups = new ArrayList<>(); + Iterator iterator = childrenGroups.iterator(); + while (iterator.hasNext()) { + DeviceGroup childGroup = iterator.next(); + int immediateParentID = Integer.parseInt(StringUtils.substringAfterLast( + childGroup.getParentPath(), "/")); + if (immediateParentID == parentGroup.getGroupId()) { + if (requireGroupProps) { + populateGroupProperties(childGroup, tenantId); + } + immediateChildrenGroups.add(childGroup); + iterator.remove(); + } + } + parentGroup.setChildrenGroups(immediateChildrenGroups); + counter++; + for (DeviceGroup nextParentGroup : immediateChildrenGroups) { + createGroupWithChildren(nextParentGroup, childrenGroups, requireGroupProps, tenantId, depth, counter); + } + } } 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 7822754208..334c9b848c 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 @@ -1162,4 +1162,17 @@ public final class DeviceManagerUtil { .getUserStoreManager(); return userStoreManager.getUserClaimValue(username, claimUri, null); } + + /** + * Create the parent path that the children groups can have + * @param deviceGroup parent group + * @return created parent path + */ + public static String createParentPath(DeviceGroup deviceGroup) { + if ("/".equals(deviceGroup.getParentPath())) { + return deviceGroup.getParentPath() + deviceGroup.getGroupId(); + } else { + return deviceGroup.getParentPath() + "/" + deviceGroup.getGroupId(); + } + } } From a12b942d86ce9162c4c0facf069df19988051108 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 24 Jun 2021 20:41:11 +0530 Subject: [PATCH 11/17] Add and update api layer related to hierarchical changes (cherry picked from commit e18aa57fe8b37f0c3a5b6701e8108276f28ca6bc) --- .../service/api/GroupManagementService.java | 123 +++++++++++++++++- .../admin/GroupManagementAdminService.java | 100 ++++++++++++++ .../impl/GroupManagementServiceImpl.java | 65 +++++++-- .../GroupManagementAdminServiceImpl.java | 51 ++++++++ 4 files changed, 327 insertions(+), 12 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java index f61d212493..6ad3b14bd7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java @@ -15,6 +15,22 @@ * specific language governing permissions and limitations * under the License. * + * + * Copyright (c) 2021, Entgra (pvt) Ltd. (https://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.jaxrs.service.api; @@ -45,6 +61,7 @@ import org.wso2.carbon.device.mgt.jaxrs.util.Constants; import javax.validation.Valid; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; +import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; @@ -243,6 +260,87 @@ public interface GroupManagementService { @QueryParam("requireGroupProps") boolean requireGroupProps); + + @GET + @Path("/hierarchy") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_GET, + value = "Getting the List of Hierarchical Groups", + notes = "Returns all groups enrolled with the system hierarchically.", + tags = "Device Group Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:groups:groups") + }) + } + ) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Successfully fetched the list of device hierarchical groups.", + response = DeviceGroupList.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource has been modified the last time.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 304, + message = "Not Modified. \n Empty body because the client has already the latest version of " + + "the requested resource."), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching the groups list.", + response = ErrorResponse.class) + }) + Response getGroupsWithHierarchy( + @ApiParam( + name = "name", + value = "Name of the group.") + @QueryParam("name") + String name, + @ApiParam( + name = "owner", + value = "Owner of the group.") + @QueryParam("owner") + String owner, + @ApiParam( + name = "requireGroupProps", + value = "Request group properties to include in the response", + defaultValue = "false") + @QueryParam("requireGroupProps") + boolean requireGroupProps, + @ApiParam( + name = "depth", + value = "Depth of the group hierarchy.") + @DefaultValue("3") + @QueryParam("depth") + int depth, + @ApiParam( + name = "offset", + value = "The starting pagination index for the complete list of qualified items.", + defaultValue = "0") + @DefaultValue("0") + @QueryParam("offset") + int offset, + @ApiParam( + name = "limit", + value = "Provide how many records require from the starting pagination index/offset.", + defaultValue = "5") + @DefaultValue("5") + @QueryParam("limit") + int limit); + @Path("/count") @GET @ApiOperation( @@ -412,7 +510,14 @@ public interface GroupManagementService { value = "Request group properties to include in the response", defaultValue = "false") @QueryParam("requireGroupProps") - boolean requireGroupProps); + boolean requireGroupProps, + @ApiParam( + name = "depth", + value = "Depth of the group hierarchy.", + defaultValue = "1") + @DefaultValue("1") + @QueryParam("depth") + int depth); @Path("/name/{groupName}") @GET @@ -471,7 +576,14 @@ public interface GroupManagementService { value = "Request group properties to include in the response", defaultValue = "false") @QueryParam("requireGroupProps") - boolean requireGroupProps); + boolean requireGroupProps, + @ApiParam( + name = "depth", + value = "Depth of the group hierarchy.", + defaultValue = "1") + @DefaultValue("1") + @QueryParam("depth") + int depth); @Path("/id/{groupId}") @PUT @@ -580,7 +692,12 @@ public interface GroupManagementService { name = "groupId", value = "ID of the group to be deleted.", required = true) - @PathParam("groupId") int groupId); + @PathParam("groupId") int groupId, + @ApiParam( + name = "isDeleteChildren", + value = "Is the children groups needs to be deleted.", + required = true) + @QueryParam("isDeleteChildren") boolean isDeleteChildren); @Path("/id/{groupId}/share") @POST diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/GroupManagementAdminService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/GroupManagementAdminService.java index f631b1dc51..d7b8553b20 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/GroupManagementAdminService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/GroupManagementAdminService.java @@ -15,6 +15,22 @@ * specific language governing permissions and limitations * under the License. * + * + * Copyright (c) 2021, Entgra (pvt) Ltd. (https://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.jaxrs.service.api.admin; @@ -163,6 +179,90 @@ public interface GroupManagementAdminService { @QueryParam("requireGroupProps") boolean requireGroupProps); + @GET + @Path("hierarchy") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_GET, + value = "Getting the List of Hierarchical Groups", + notes = "Returns all groups enrolled with the system hierarchically.", + tags = "Device Group Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:admin-groups:view") + }) + } + ) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Successfully fetched the list of device groups hierarchically.", + response = DeviceGroupList.class, + responseHeaders = { + @ResponseHeader( + name = "Content-Type", + description = "The content type of the body"), + @ResponseHeader( + name = "ETag", + description = "Entity Tag of the response resource.\n" + + "Used by caches, or in conditional requests."), + @ResponseHeader( + name = "Last-Modified", + description = "Date and time the resource has been modified the last time.\n" + + "Used by caches, or in conditional requests."), + }), + @ApiResponse( + code = 304, + message = "Not Modified. \n Empty body because the client has already the latest version of " + + "the requested resource."), + @ApiResponse( + code = 406, + message = "Not Acceptable.\n The requested media type is not supported."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Server error occurred while fetching the groups list.", + response = ErrorResponse.class) + }) + Response getGroupsWithHierarchy(@ApiParam( + name = "name", + value = "Name of the group.") + @QueryParam("name") + String name, + @ApiParam( + name = "owner", + value = "Owner of the group.") + @QueryParam("owner") + String owner, + @ApiParam( + name = "status", + value = "status of group to be retrieve.") + @QueryParam("status") + String status, + @ApiParam( + name = "requireGroupProps", + value = "Request group properties to include in the response", + defaultValue = "false") + @QueryParam("requireGroupProps") + boolean requireGroupProps, + @ApiParam( + name = "depth", + value = "Depth of the group hierarchy.") + @DefaultValue("3") + @QueryParam("depth") + int depth, + @ApiParam( + name = "offset", + value = "The starting pagination index for the complete list of qualified items.", + defaultValue = "0") + @DefaultValue("0") + @QueryParam("offset") + int offset, + @ApiParam( + name = "limit", + value = "Provide how many records require from the starting pagination index/offset.", + defaultValue = "5") + @DefaultValue("5") + @QueryParam("limit") + int limit); + @Path("/count") @GET @ApiOperation( diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java index 0502f37ab3..3fed8e7504 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java @@ -15,6 +15,22 @@ * specific language governing permissions and limitations * under the License. * + * + * Copyright (c) 2021, Entgra (pvt) Ltd. (https://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.jaxrs.service.impl; @@ -36,7 +52,6 @@ import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupNotExistException; import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException; -import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList; @@ -47,10 +62,12 @@ import org.wso2.carbon.device.mgt.jaxrs.service.api.GroupManagementService; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint; -import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException; -import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; import java.util.ArrayList; import java.util.List; @@ -89,11 +106,41 @@ public class GroupManagementServiceImpl implements GroupManagementService { } } + @GET + @Path("/hierarchy") + @Override + public Response getGroupsWithHierarchy( + @QueryParam("name") String name, + @QueryParam("owner") String owner, + @QueryParam("requireGroupProps") boolean requireGroupProps, + @DefaultValue("3") @QueryParam("depth") int depth, + @DefaultValue("0") @QueryParam("offset") int offset, + @DefaultValue("5") @QueryParam("limit") int limit) { + try { + RequestValidationUtil.validatePaginationParameters(offset, limit); + String currentUser = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + GroupPaginationRequest request = new GroupPaginationRequest(offset, limit); + request.setGroupName(name); + request.setOwner(owner); + request.setDepth(depth); + PaginationResult deviceGroupsResult = DeviceMgtAPIUtils.getGroupManagementProviderService() + .getGroupsWithHierarchy(currentUser, request, requireGroupProps); + DeviceGroupList deviceGroupList = new DeviceGroupList(); + deviceGroupList.setList(deviceGroupsResult.getData()); + deviceGroupList.setCount(deviceGroupsResult.getRecordsTotal()); + return Response.status(Response.Status.OK).entity(deviceGroupList).build(); + } catch (GroupManagementException e) { + String error = "Error occurred while retrieving groups with hierarchy."; + log.error(error, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build(); + } + } + @Override public Response getGroupCount() { try { String currentUser = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - int count = DeviceMgtAPIUtils.getGroupManagementProviderService().getGroupCount(currentUser); + int count = DeviceMgtAPIUtils.getGroupManagementProviderService().getGroupCount(currentUser, null); return Response.status(Response.Status.OK).entity(count).build(); } catch (GroupManagementException e) { String msg = "Error occurred while retrieving group count."; @@ -125,10 +172,10 @@ public class GroupManagementServiceImpl implements GroupManagementService { } @Override - public Response getGroup(int groupId, boolean requireGroupProps) { + public Response getGroup(int groupId, boolean requireGroupProps, int depth) { try { GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); - DeviceGroup deviceGroup = service.getGroup(groupId, requireGroupProps); + DeviceGroup deviceGroup = service.getGroup(groupId, requireGroupProps, depth); if (deviceGroup != null) { return Response.status(Response.Status.OK).entity(deviceGroup).build(); } else { @@ -142,7 +189,7 @@ public class GroupManagementServiceImpl implements GroupManagementService { } @Override - public Response getGroup(String groupName, boolean requireGroupProps) { + public Response getGroup(String groupName, boolean requireGroupProps, int depth) { try { GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); DeviceGroup deviceGroup = service.getGroup(groupName, requireGroupProps); @@ -178,9 +225,9 @@ public class GroupManagementServiceImpl implements GroupManagementService { } @Override - public Response deleteGroup(int groupId) { + public Response deleteGroup(int groupId, boolean isDeleteChildren) { try { - if (DeviceMgtAPIUtils.getGroupManagementProviderService().deleteGroup(groupId)) { + if (DeviceMgtAPIUtils.getGroupManagementProviderService().deleteGroup(groupId, isDeleteChildren)) { return Response.status(Response.Status.OK).build(); } else { return Response.status(Response.Status.NOT_FOUND).entity("Group not found.").build(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java index b742a1d8e1..a461b71b50 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java @@ -15,6 +15,22 @@ * specific language governing permissions and limitations * under the License. * + * + * Copyright (c) 2021, Entgra (pvt) Ltd. (https://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.jaxrs.service.impl.admin; @@ -31,6 +47,10 @@ import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.GroupManagementAdminSe import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; import java.util.ArrayList; @@ -74,6 +94,37 @@ public class GroupManagementAdminServiceImpl implements GroupManagementAdminServ } } + @GET + @Path("/hierarchy") + @Override + public Response getGroupsWithHierarchy( + @QueryParam("name") String name, + @QueryParam("owner") String owner, + @QueryParam("status") String status, + @QueryParam("requireGroupProps") boolean requireGroupProps, + @DefaultValue("3") @QueryParam("depth") int depth, + @DefaultValue("0") @QueryParam("offset") int offset, + @DefaultValue("5") @QueryParam("limit") int limit) { + try { + RequestValidationUtil.validatePaginationParameters(offset, limit); + GroupPaginationRequest request = new GroupPaginationRequest(offset, limit); + request.setGroupName(name); + request.setOwner(owner); + request.setStatus(status); + request.setDepth(depth); + PaginationResult deviceGroupsResult = DeviceMgtAPIUtils.getGroupManagementProviderService() + .getGroupsWithHierarchy(null, request, requireGroupProps); + DeviceGroupList deviceGroupList = new DeviceGroupList(); + deviceGroupList.setList(deviceGroupsResult.getData()); + deviceGroupList.setCount(deviceGroupsResult.getRecordsTotal()); + return Response.status(Response.Status.OK).entity(deviceGroupList).build(); + } catch (GroupManagementException e) { + String error = "Error occurred while retrieving groups with hierarchy."; + log.error(error, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build(); + } + } + @Override public Response getGroupCount(String status) { try { From dabc0dc8ec7901a0b35647780959cb5053802b96 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 24 Jun 2021 21:06:30 +0530 Subject: [PATCH 12/17] Fix test cases related to hierarchical group changes (cherry picked from commit 24e69d5492b1be2c76c26e754769c6082f527650) --- .../impl/GroupManagementServiceImplTest.java | 46 +++++++++++++------ ...ManagementProviderServiceNegativeTest.java | 21 ++++++++- .../GroupManagementProviderServiceTest.java | 23 ++++++++-- .../src/test/resources/sql/h2.sql | 1 + .../src/test/resources/sql/CreateH2TestDB.sql | 1 + 5 files changed, 72 insertions(+), 20 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImplTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImplTest.java index 20136b172d..2d430c09f6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImplTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/test/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImplTest.java @@ -15,11 +15,26 @@ * specific language governing permissions and limitations * under the License. * + * + * Copyright (c) 2021, Entgra (pvt) Ltd. (https://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.jaxrs.service.impl; -import org.mockito.Mock; import org.mockito.Mockito; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PowerMockIgnore; @@ -130,13 +145,14 @@ public class GroupManagementServiceImplTest { .toReturn(groupManagementProviderService); PowerMockito.stub(PowerMockito.method(PrivilegedCarbonContext.class, "getThreadLocalCarbonContext")) .toReturn(context); - Mockito.doReturn(2).when(groupManagementProviderService).getGroupCount(Mockito.anyString()); + Mockito.doReturn(2).when(groupManagementProviderService) + .getGroupCount(Mockito.anyString(), Mockito.anyString()); Response response = groupManagementService.getGroupCount(); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "GetGroupCount request failed with valid parameters"); Mockito.reset(groupManagementProviderService); Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService) - .getGroupCount(Mockito.anyString()); + .getGroupCount(Mockito.anyString(), Mockito.anyString()); response = groupManagementService.getGroupCount(); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "GetGroupCount request succeeded with in-valid parameters"); @@ -174,16 +190,16 @@ public class GroupManagementServiceImplTest { public void testGetGroup() throws GroupManagementException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getGroupManagementProviderService")) .toReturn(groupManagementProviderService); - Mockito.doReturn(new DeviceGroup()).when(groupManagementProviderService).getGroup(1, false); - Mockito.doReturn(null).when(groupManagementProviderService).getGroup(2, false); - Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService).getGroup(3, false); - Response response = groupManagementService.getGroup(1, false); + Mockito.doReturn(new DeviceGroup()).when(groupManagementProviderService).getGroup(1, false, 1); + Mockito.doReturn(null).when(groupManagementProviderService).getGroup(2, false, 1); + Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService).getGroup(3, false, 1); + Response response = groupManagementService.getGroup(1, false, 1); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "getGroup request failed for a request with valid parameters"); - response = groupManagementService.getGroup(2, false); + response = groupManagementService.getGroup(2, false, 1); Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode(), "getGroup request returned a group for a non-existing group"); - response = groupManagementService.getGroup(3, false); + response = groupManagementService.getGroup(3, false, 1); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "getGroup request returned a group for a in-valid request"); } @@ -216,16 +232,16 @@ public class GroupManagementServiceImplTest { public void testDeleteGroup() throws GroupManagementException { PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getGroupManagementProviderService")) .toReturn(groupManagementProviderService); - Mockito.doReturn(true).when(groupManagementProviderService).deleteGroup(1); - Mockito.doReturn(false).when(groupManagementProviderService).deleteGroup(2); - Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService).deleteGroup(3); - Response response = groupManagementService.deleteGroup(1); + Mockito.doReturn(true).when(groupManagementProviderService).deleteGroup(1, false); + Mockito.doReturn(false).when(groupManagementProviderService).deleteGroup(2, false); + Mockito.doThrow(new GroupManagementException()).when(groupManagementProviderService).deleteGroup(3, false); + Response response = groupManagementService.deleteGroup(1, false); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), "delete group request failed for a request with valid parameters"); - response = groupManagementService.deleteGroup(2); + response = groupManagementService.deleteGroup(2, false); Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode(), "Non-existing group was successfully deleted"); - response = groupManagementService.deleteGroup(3); + response = groupManagementService.deleteGroup(3, false); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "Deletion succeeded with an erroneous condition."); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceNegativeTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceNegativeTest.java index fd34165031..ec8004e475 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceNegativeTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceNegativeTest.java @@ -8,6 +8,23 @@ * * 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. + * + * Copyright (c) 2021, Entgra (pvt) Ltd. (https://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 @@ -103,7 +120,7 @@ public class GroupManagementProviderServiceNegativeTest extends BaseDeviceManage expectedExceptions = {GroupManagementException.class}, expectedExceptionsMessageRegExp = "Error occurred " + "while retrieving group count of user.*") public void testGetGroupCountWithUserName() throws GroupManagementException { - groupManagementProviderService.getGroupCount("test"); + groupManagementProviderService.getGroupCount("test", null); } @@ -145,7 +162,7 @@ public class GroupManagementProviderServiceNegativeTest extends BaseDeviceManage expectedExceptions = {GroupManagementException.class}, expectedExceptionsMessageRegExp = "Received empty " + "user name for getGroupCount.*") public void testGetGroupCountWithUserName2() throws GroupManagementException { - groupManagementProviderService.getGroupCount(null); + groupManagementProviderService.getGroupCount(null, null); } @Test(description = "This method tests getGroups method under negative circumstances", diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java index 4ffb675fe7..b645eac9a0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceTest.java @@ -14,6 +14,23 @@ * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. + * + * + * Copyright (c) 2021, Entgra (pvt) Ltd. (https://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.core.service; @@ -119,13 +136,13 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest @Test(dependsOnMethods = ("createGroup")) public void deleteGroup() throws GroupManagementException { DeviceGroup deviceGroup = groupManagementProviderService.getGroup(TestUtils.createDeviceGroup4().getName(), false); - Assert.assertTrue(groupManagementProviderService.deleteGroup(deviceGroup.getGroupId())); + Assert.assertTrue(groupManagementProviderService.deleteGroup(deviceGroup.getGroupId(), false)); } @Test(dependsOnMethods = ("createGroup")) public void deleteGroupNotExists() throws GroupManagementException { - groupManagementProviderService.deleteGroup(8); + groupManagementProviderService.deleteGroup(8, false); } @@ -190,7 +207,7 @@ public class GroupManagementProviderServiceTest extends BaseDeviceManagementTest @Test(dependsOnMethods = ("createGroup")) public void getGroupCountByUsername(String username) throws GroupManagementException { - int x = groupManagementProviderService.getGroupCount(username); + int x = groupManagementProviderService.getGroupCount(username, null); Assert.assertNotNull(x); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql index 63af00f07f..2c846cb1af 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/sql/h2.sql @@ -14,6 +14,7 @@ CREATE TABLE IF NOT EXISTS DM_GROUP ( STATUS VARCHAR(50) DEFAULT NULL, DESCRIPTION TEXT DEFAULT NULL, OWNER VARCHAR(45) DEFAULT NULL, + PARENT_PATH VARCHAR(255) DEFAULT NULL, TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID) ); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql index d830a6387e..7238d37790 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql @@ -18,6 +18,7 @@ CREATE TABLE IF NOT EXISTS DM_GROUP ( DATE_OF_CREATE BIGINT DEFAULT NULL, DATE_OF_LAST_UPDATE BIGINT DEFAULT NULL, OWNER VARCHAR(45) DEFAULT NULL, + PARENT_PATH VARCHAR(255) DEFAULT NULL, TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID) ); From 996309f063ff9aed29337aa049f9888025bb6f6b Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 15 Jul 2021 21:37:36 +0530 Subject: [PATCH 13/17] Update all children groups when updating a group (cherry picked from commit ed087e2a0cfd9ac055a4b0a0da588f24c97a9739) --- .../GroupManagementProviderServiceImpl.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java index 35f4915481..808a355cbe 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -177,6 +177,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid GroupManagementDAOFactory.beginTransaction(); DeviceGroup existingGroup = this.groupDAO.getGroup(groupId, tenantId); if (existingGroup != null) { + List groupsToUpdate = new ArrayList<>(); + String immediateParentID = StringUtils.substringAfterLast(existingGroup.getParentPath(), "/"); + String parentPath = ""; if (deviceGroup.getParentGroupId() == 0) { deviceGroup.setParentPath("/"); } else { @@ -188,10 +191,20 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(msg); throw new GroupManagementException(msg); } - String parentPath = DeviceManagerUtil.createParentPath(immediateParentGroup); + parentPath = DeviceManagerUtil.createParentPath(immediateParentGroup); deviceGroup.setParentPath(parentPath); } - this.groupDAO.updateGroup(deviceGroup, groupId, tenantId); + deviceGroup.setGroupId(groupId); + groupsToUpdate.add(deviceGroup); + if (StringUtils.isNotBlank(immediateParentID)) { + List childrenGroups = groupDAO.getChildrenGroups(DeviceManagerUtil.createParentPath(existingGroup), tenantId); + for (DeviceGroup childrenGroup : childrenGroups) { + childrenGroup.setParentPath(childrenGroup.getParentPath() + .replace(existingGroup.getParentPath(), parentPath)); + groupsToUpdate.add(childrenGroup); + } + } + this.groupDAO.updateGroups(groupsToUpdate, tenantId); if (deviceGroup.getGroupProperties() != null && deviceGroup.getGroupProperties().size() > 0) { this.groupDAO.updateGroupProperties(deviceGroup, groupId, tenantId); } From aea460c0d5f2d0da1416f27ae2bee770b4a285d0 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Fri, 16 Jul 2021 00:13:30 +0530 Subject: [PATCH 14/17] Move slash to a constant (cherry picked from commit 489aaabc445087abcc9d972d759f68332dfe05de) --- .../group/mgt/DeviceGroupConstants.java | 4 ++++ .../GroupManagementProviderServiceImpl.java | 21 ++++++++++--------- .../mgt/core/util/DeviceManagerUtil.java | 6 ++++-- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroupConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroupConstants.java index 039219bfe8..b1a185d98f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroupConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroupConstants.java @@ -84,4 +84,8 @@ public class DeviceGroupConstants { public static final String[] DEFAULT_VIEW_EVENTS_PERMISSIONS = {"/permission/device-mgt/user/groups/device_events"}; } + + public static final class HierarchicalGroup { + public static final String SEPERATOR = "/"; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java index 808a355cbe..6a678c1fca 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -113,7 +113,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid DeviceGroup existingGroup = this.groupDAO.getGroup(deviceGroup.getName(), tenantId); if (existingGroup == null) { if (deviceGroup.getParentGroupId() == 0) { - deviceGroup.setParentPath("/"); + deviceGroup.setParentPath(DeviceGroupConstants.HierarchicalGroup.SEPERATOR); } else { DeviceGroup immediateParentGroup = groupDAO.getGroup(deviceGroup.getParentGroupId(), tenantId); if (immediateParentGroup == null) { @@ -178,10 +178,10 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid DeviceGroup existingGroup = this.groupDAO.getGroup(groupId, tenantId); if (existingGroup != null) { List groupsToUpdate = new ArrayList<>(); - String immediateParentID = StringUtils.substringAfterLast(existingGroup.getParentPath(), "/"); + String immediateParentID = StringUtils.substringAfterLast(existingGroup.getParentPath(), DeviceGroupConstants.HierarchicalGroup.SEPERATOR); String parentPath = ""; if (deviceGroup.getParentGroupId() == 0) { - deviceGroup.setParentPath("/"); + deviceGroup.setParentPath(DeviceGroupConstants.HierarchicalGroup.SEPERATOR); } else { DeviceGroup immediateParentGroup = groupDAO.getGroup(deviceGroup.getParentGroupId(), tenantId); if (immediateParentGroup == null) { @@ -197,7 +197,8 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid deviceGroup.setGroupId(groupId); groupsToUpdate.add(deviceGroup); if (StringUtils.isNotBlank(immediateParentID)) { - List childrenGroups = groupDAO.getChildrenGroups(DeviceManagerUtil.createParentPath(existingGroup), tenantId); + List childrenGroups = groupDAO.getChildrenGroups( + DeviceManagerUtil.createParentPath(existingGroup), tenantId); for (DeviceGroup childrenGroup : childrenGroups) { childrenGroup.setParentPath(childrenGroup.getParentPath() .replace(existingGroup.getParentPath(), parentPath)); @@ -258,9 +259,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } else { for (DeviceGroup childrenGroup : childrenGroups) { String newParentPath = childrenGroup.getParentPath() - .replace("/" + deviceGroup.getGroupId(), ""); + .replace(DeviceGroupConstants.HierarchicalGroup.SEPERATOR + deviceGroup.getGroupId(), ""); if (StringUtils.isEmpty(newParentPath)) { - newParentPath = "/"; + newParentPath = DeviceGroupConstants.HierarchicalGroup.SEPERATOR; } childrenGroup.setParentPath(newParentPath); } @@ -437,7 +438,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.openConnection(); - request.setParentPath("/"); + request.setParentPath(DeviceGroupConstants.HierarchicalGroup.SEPERATOR); rootGroups = this.groupDAO.getGroups(request, tenantId); for (DeviceGroup rootGroup : rootGroups) { if (requireGroupProps) { @@ -480,7 +481,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid List rootGroups; try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - request.setParentPath("/"); + request.setParentPath(DeviceGroupConstants.HierarchicalGroup.SEPERATOR); if (StringUtils.isBlank(username)) { GroupManagementDAOFactory.openConnection(); rootGroups = groupDAO.getGroups(request, tenantId); @@ -616,7 +617,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.openConnection(); - request.setParentPath("/"); + request.setParentPath(DeviceGroupConstants.HierarchicalGroup.SEPERATOR); rootGroups = this.groupDAO.getGroups(request, allDeviceGroupIdsOfUser, tenantId); for (DeviceGroup rootGroup : rootGroups) { if (requireGroupProps) { @@ -1306,7 +1307,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid while (iterator.hasNext()) { DeviceGroup childGroup = iterator.next(); int immediateParentID = Integer.parseInt(StringUtils.substringAfterLast( - childGroup.getParentPath(), "/")); + childGroup.getParentPath(), DeviceGroupConstants.HierarchicalGroup.SEPERATOR)); if (immediateParentID == parentGroup.getGroupId()) { if (requireGroupProps) { populateGroupProperties(childGroup, tenantId); 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 334c9b848c..36315a1315 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 @@ -70,6 +70,7 @@ import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException; import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException; import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; +import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.common.exceptions.MetadataManagementException; import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementException; @@ -1169,10 +1170,11 @@ public final class DeviceManagerUtil { * @return created parent path */ public static String createParentPath(DeviceGroup deviceGroup) { - if ("/".equals(deviceGroup.getParentPath())) { + if (DeviceGroupConstants.HierarchicalGroup.SEPERATOR.equals(deviceGroup.getParentPath())) { return deviceGroup.getParentPath() + deviceGroup.getGroupId(); } else { - return deviceGroup.getParentPath() + "/" + deviceGroup.getGroupId(); + return deviceGroup.getParentPath() + DeviceGroupConstants.HierarchicalGroup.SEPERATOR + + deviceGroup.getGroupId(); } } } From 4d2f5985294bf13a2f644784493c934170781f09 Mon Sep 17 00:00:00 2001 From: vigneshan Date: Thu, 29 Jul 2021 10:45:38 +0530 Subject: [PATCH 15/17] Modify login cache implementatation with LRU cache (cherry picked from commit 0eb74d9f60c74c13adbd30c5adf72ea9d2c1e3e6) --- .../mgt/core/config/ui/UIConfiguration.java | 10 ++ .../ui/request/interceptor/LoginHandler.java | 12 +- .../request/interceptor/SsoLoginHandler.java | 44 +++-- ...LoginCacheManager.java => LoginCache.java} | 34 ++-- .../interceptor/util/HandlerConstants.java | 2 +- .../request/interceptor/util/HandlerUtil.java | 159 +++++++++++++++++- .../src/main/resources/conf/mdm-ui-config.xml | 2 + 7 files changed, 213 insertions(+), 50 deletions(-) rename components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/cache/{LoginCacheManager.java => LoginCache.java} (67%) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/ui/UIConfiguration.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/ui/UIConfiguration.java index 1c4a1e91cd..f494007330 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/ui/UIConfiguration.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/ui/UIConfiguration.java @@ -32,6 +32,7 @@ public class UIConfiguration { private List scopes; private boolean isSsoEnable; private int sessionTimeOut; + private int loginCacheCapacity; @XmlElement(name = "AppRegistration", required=true) public AppRegistration getAppRegistration() { @@ -69,4 +70,13 @@ public class UIConfiguration { public void setSessionTimeOut(int sessionTimeOut) { this.sessionTimeOut = sessionTimeOut; } + + @XmlElement(name = "LoginCacheCapacity") + public int getLoginCacheCapacity() { + return loginCacheCapacity; + } + + public void setLoginCacheCapacity(int loginCacheCapacity) { + this.loginCacheCapacity = loginCacheCapacity; + } } diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/LoginHandler.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/LoginHandler.java index 60dd31ab2d..ad6b90ee16 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/LoginHandler.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/LoginHandler.java @@ -24,7 +24,7 @@ import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.JsonSyntaxException; import io.entgra.ui.request.interceptor.beans.AuthData; -import io.entgra.ui.request.interceptor.cache.LoginCacheManager; +import io.entgra.ui.request.interceptor.cache.LoginCache; import io.entgra.ui.request.interceptor.cache.OAuthApp; import io.entgra.ui.request.interceptor.cache.OAuthAppCacheKey; import io.entgra.ui.request.interceptor.exceptions.LoginException; @@ -39,7 +39,6 @@ import org.apache.http.entity.ContentType; import org.apache.http.entity.StringEntity; import org.apache.http.protocol.HTTP; import io.entgra.ui.request.interceptor.beans.ProxyResponse; -import org.json.JSONString; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; @@ -80,10 +79,9 @@ public class LoginHandler extends HttpServlet { httpSession.setMaxInactiveInterval(sessionTimeOut); // Check if OAuth app cache exists. If not create a new application. - LoginCacheManager loginCacheManager = new LoginCacheManager(); - loginCacheManager.initializeCacheManager(); + LoginCache loginCache = HandlerUtil.getLoginCache(httpSession); OAuthAppCacheKey oAuthAppCacheKey = new OAuthAppCacheKey(HandlerConstants.PUBLISHER_APPLICATION_NAME, username); - OAuthApp oAuthApp = loginCacheManager.getOAuthAppCache(oAuthAppCacheKey); + OAuthApp oAuthApp = loginCache.getOAuthAppCache(oAuthAppCacheKey); if (oAuthApp == null) { HttpPost apiRegEndpoint = new HttpPost(gatewayUrl + HandlerConstants.APP_REG_ENDPOINT); @@ -111,8 +109,6 @@ public class LoginHandler extends HttpServlet { clientSecret = jClientAppResultAsJsonObject.get("client_secret").getAsString(); encodedClientApp = Base64.getEncoder() .encodeToString((clientId + HandlerConstants.COLON + clientSecret).getBytes()); - - oAuthAppCacheKey = new OAuthAppCacheKey(HandlerConstants.PUBLISHER_APPLICATION_NAME, username); oAuthApp = new OAuthApp( HandlerConstants.PUBLISHER_APPLICATION_NAME, username, @@ -120,7 +116,7 @@ public class LoginHandler extends HttpServlet { clientSecret, encodedClientApp ); - loginCacheManager.addOAuthAppToCache(oAuthAppCacheKey, oAuthApp); + loginCache.addOAuthAppToCache(oAuthAppCacheKey, oAuthApp); } if (getTokenAndPersistInSession(req, resp, clientId, clientSecret, encodedClientApp, scopes)) { diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/SsoLoginHandler.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/SsoLoginHandler.java index fb4f93b0c9..10a488e294 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/SsoLoginHandler.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/SsoLoginHandler.java @@ -23,7 +23,7 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.JsonSyntaxException; -import io.entgra.ui.request.interceptor.cache.LoginCacheManager; +import io.entgra.ui.request.interceptor.cache.LoginCache; import io.entgra.ui.request.interceptor.cache.OAuthApp; import io.entgra.ui.request.interceptor.cache.OAuthAppCacheKey; import io.entgra.ui.request.interceptor.util.HandlerConstants; @@ -81,9 +81,9 @@ public class SsoLoginHandler extends HttpServlet { private JsonObject uiConfigJsonObject; private HttpSession httpSession; - - private LoginCacheManager loginCacheManager; + private LoginCache loginCache; private OAuthApp oAuthApp; + private OAuthAppCacheKey oAuthAppCacheKey; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) { @@ -99,13 +99,23 @@ public class SsoLoginHandler extends HttpServlet { baseContextPath = req.getContextPath(); applicationName = baseContextPath.substring(1, baseContextPath.indexOf("-ui-request-handler")); - // Check if oauth app cache is available - loginCacheManager = new LoginCacheManager(); - loginCacheManager.initializeCacheManager(); - oAuthApp = loginCacheManager.getOAuthAppCache( - new OAuthAppCacheKey(applicationName, adminUsername) - ); + String iotsCorePort = System.getProperty(HandlerConstants.IOT_CORE_HTTPS_PORT_ENV_VAR); + if (HandlerConstants.HTTP_PROTOCOL.equals(req.getScheme())) { + iotsCorePort = System.getProperty(HandlerConstants.IOT_CORE_HTTP_PORT_ENV_VAR); + } + gatewayUrl = req.getScheme() + HandlerConstants.SCHEME_SEPARATOR + System.getProperty(HandlerConstants.IOT_GW_HOST_ENV_VAR) + + HandlerConstants.COLON + HandlerUtil.getGatewayPort(req.getScheme()); + iotsCoreUrl = req.getScheme() + HandlerConstants.SCHEME_SEPARATOR + System.getProperty(HandlerConstants.IOT_CORE_HOST_ENV_VAR) + + HandlerConstants.COLON + iotsCorePort; + + // Fetch ui config and persists in session + String uiConfigUrl = iotsCoreUrl + HandlerConstants.UI_CONFIG_ENDPOINT; + uiConfigJsonObject = HandlerUtil.getUIConfigAndPersistInSession(uiConfigUrl, gatewayUrl, httpSession, resp); + // Retrieving login cache and do a DCR if the cache is not available. + loginCache = HandlerUtil.getLoginCache(httpSession); + oAuthAppCacheKey = new OAuthAppCacheKey(applicationName, adminUsername); + oAuthApp = loginCache.getOAuthAppCache(oAuthAppCacheKey); if (oAuthApp == null) { dynamicClientRegistration(req, resp); } @@ -143,19 +153,6 @@ public class SsoLoginHandler extends HttpServlet { */ private void dynamicClientRegistration(HttpServletRequest req, HttpServletResponse resp) { try { - String iotsCorePort = System.getProperty(HandlerConstants.IOT_CORE_HTTPS_PORT_ENV_VAR); - - if (HandlerConstants.HTTP_PROTOCOL.equals(req.getScheme())) { - iotsCorePort = System.getProperty(HandlerConstants.IOT_CORE_HTTP_PORT_ENV_VAR); - } - - gatewayUrl = req.getScheme() + HandlerConstants.SCHEME_SEPARATOR + System.getProperty(HandlerConstants.IOT_GW_HOST_ENV_VAR) - + HandlerConstants.COLON + HandlerUtil.getGatewayPort(req.getScheme()); - iotsCoreUrl = req.getScheme() + HandlerConstants.SCHEME_SEPARATOR + System.getProperty(HandlerConstants.IOT_CORE_HOST_ENV_VAR) - + HandlerConstants.COLON + iotsCorePort; - String uiConfigUrl = iotsCoreUrl + HandlerConstants.UI_CONFIG_ENDPOINT; - - uiConfigJsonObject = HandlerUtil.getUIConfigAndPersistInSession(uiConfigUrl, gatewayUrl, httpSession, resp); JsonArray tags = uiConfigJsonObject.get("appRegistration").getAsJsonObject().get("tags").getAsJsonArray(); JsonArray scopes = uiConfigJsonObject.get("scopes").getAsJsonArray(); sessionTimeOut = Integer.parseInt(String.valueOf(uiConfigJsonObject.get("sessionTimeOut"))); @@ -191,9 +188,8 @@ public class SsoLoginHandler extends HttpServlet { } // cache the oauth app credentials - OAuthAppCacheKey oAuthAppCacheKey = new OAuthAppCacheKey(applicationName, adminUsername); oAuthApp = new OAuthApp(applicationName, adminUsername, clientId, clientSecret, encodedClientApp); - loginCacheManager.addOAuthAppToCache(oAuthAppCacheKey, oAuthApp); + loginCache.addOAuthAppToCache(oAuthAppCacheKey, oAuthApp); } // Get the details of the registered application diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/cache/LoginCacheManager.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/cache/LoginCache.java similarity index 67% rename from components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/cache/LoginCacheManager.java rename to components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/cache/LoginCache.java index 3ecd741350..06ca1cef6b 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/cache/LoginCacheManager.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/cache/LoginCache.java @@ -18,25 +18,19 @@ package io.entgra.ui.request.interceptor.cache; -import io.entgra.ui.request.interceptor.util.HandlerConstants; - -import javax.cache.Cache; -import javax.cache.CacheManager; -import javax.cache.Caching; +import java.util.LinkedHashMap; /** * Contains necessary functions to manage oAuth app cache during login handling */ -public class LoginCacheManager { +public class LoginCache { - private CacheManager cacheManager = null; - private Cache cache = null; + private final LinkedHashMap cache; + private final int capacity; - /** - * Initialize the cache manager if it is not already initialized - */ - public void initializeCacheManager() { - cacheManager = Caching.getCacheManagerFactory().getCacheManager(HandlerConstants.LOGIN_CACHE); + public LoginCache(int capacity) { + this.capacity = capacity; + this.cache = new LinkedHashMap<>(capacity); } /** @@ -46,7 +40,9 @@ public class LoginCacheManager { * @param oAuthApp - The value of the cache which contains OAuth app data */ public void addOAuthAppToCache(OAuthAppCacheKey oAuthAppCacheKey, OAuthApp oAuthApp) { - cache = cacheManager.getCache(HandlerConstants.LOGIN_CACHE); + if (cache.size() == capacity) { + cache.remove(cache.entrySet().iterator().next().getKey()); + } cache.put(oAuthAppCacheKey, oAuthApp); } @@ -57,7 +53,13 @@ public class LoginCacheManager { * @return - Returns OAuthApp object */ public OAuthApp getOAuthAppCache(OAuthAppCacheKey oAuthAppCacheKey) { - cache = cacheManager.getCache(HandlerConstants.LOGIN_CACHE); - return cache.get(oAuthAppCacheKey); + OAuthApp oAuthApp = cache.get(oAuthAppCacheKey); + if (oAuthApp != null) { + if (cache.size() == capacity) { + cache.remove(oAuthAppCacheKey); + cache.put(oAuthAppCacheKey, oAuthApp); + } + } + return oAuthApp; } } diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerConstants.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerConstants.java index acddc17ce9..feda440f3b 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerConstants.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerConstants.java @@ -55,7 +55,7 @@ public class HandlerConstants { public static final String PASSWORD_GRANT_TYPE = "password"; public static final String JWT_BEARER_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:jwt-bearer"; public static final String PRODUCTION_KEY = "PRODUCTION"; - public static final String LOGIN_CACHE = "LOGIN_CACHE"; + public static final String LOGIN_CACHE_CAPACITY_KEY = "loginCacheCapacity"; public static final String SCHEME_SEPARATOR = "://"; public static final String COLON = ":"; diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerUtil.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerUtil.java index 5acec832ce..ab702eb9ad 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerUtil.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerUtil.java @@ -23,13 +23,17 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; +import io.entgra.ui.request.interceptor.beans.AuthData; +import io.entgra.ui.request.interceptor.cache.LoginCache; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.Consts; +import org.apache.http.HttpHeaders; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.entity.ContentType; @@ -60,6 +64,9 @@ import java.io.StringWriter; public class HandlerUtil { private static final Log log = LogFactory.getLog(HandlerUtil.class); + private static LoginCache loginCache = null; + private static boolean isLoginCacheInitialized = false; + private static AuthData authData; /*** * @@ -166,9 +173,11 @@ public class HandlerUtil { /*** + * Handle error requests. * * @param resp {@link HttpServletResponse} - * Return Error Response. + * @param proxyResponse {@link ProxyResponse} + * @throws IOException If error occurred when trying to send the error response. */ public static void handleError(HttpServletResponse resp, ProxyResponse proxyResponse) throws IOException { Gson gson = new Gson(); @@ -188,6 +197,22 @@ public class HandlerUtil { } } + /** + * Handle error requests with custom error codes. + * + * @param resp {@link HttpServletResponse} + * @param errorCode HTTP error status code + * @throws IOException If error occurred when trying to send the error response. + */ + public static void handleError(HttpServletResponse resp, int errorCode) + throws IOException { + ProxyResponse proxyResponse = new ProxyResponse(); + proxyResponse.setCode(errorCode); + proxyResponse.setExecutorResponse( + HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + HandlerUtil.getStatusKey(errorCode)); + HandlerUtil.handleError(resp, proxyResponse); + } + /*** * * @param resp {@link HttpServletResponse} @@ -400,4 +425,136 @@ public class HandlerUtil { return stringOutput; } + + /*** + * Search a key from a given json string object. + * + * @param jsonObjectString - json object in string format. + * @param key - the key to be searched. + * @return string value of the key value. + */ + private static String searchFromJsonObjectString(String jsonObjectString, String key) { + JsonParser jsonParser = new JsonParser(); + JsonElement jsonElement = jsonParser.parse(jsonObjectString); + JsonObject jsonObject = jsonElement.getAsJsonObject(); + return jsonObject.get(key).getAsString(); + } + + /*** + * Initializes the login cache. + * + * @param httpSession - current active HttpSession. + */ + private static void initializeLoginCache(HttpSession httpSession) { + String uiConfig = httpSession.getAttribute(HandlerConstants.UI_CONFIG_KEY).toString(); + int capacity = Integer.parseInt(searchFromJsonObjectString(uiConfig, HandlerConstants.LOGIN_CACHE_CAPACITY_KEY)); + loginCache = new LoginCache(capacity); + } + + /*** + * Retrieves login cache and initializes if its not done already. + * + * @param httpSession - current active HttpSession. + */ + public static LoginCache getLoginCache(HttpSession httpSession) { + if (!isLoginCacheInitialized || loginCache == null) { + isLoginCacheInitialized = true; + initializeLoginCache(httpSession); + } + return loginCache; + } + + /** + * Retry request again after refreshing the access token + * + * @param req incoming {@link HttpServletRequest} + * @param resp resp {@link HttpServletResponse} + * @param httpRequest subclass of {@link HttpRequestBase} related to the current request. + * @return {@link ProxyResponse} if successful and null if failed. + * @throws IOException If an error occurs when try to retry the request. + */ + public static ProxyResponse retryRequestWithRefreshedToken(HttpServletRequest req, HttpServletResponse resp, + HttpRequestBase httpRequest, String apiEndpoint) throws IOException { + if (refreshToken(req, resp, apiEndpoint)) { + HttpSession session = req.getSession(false); + if (session == null) { + log.error("Unauthorized, You are not logged in. Please log in to the portal"); + handleError(resp, HttpStatus.SC_UNAUTHORIZED); + return null; + } + httpRequest.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BEARER + authData.getAccessToken()); + ProxyResponse proxyResponse = HandlerUtil.execute(httpRequest); + if (proxyResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) { + log.error("Error occurred while invoking the API after refreshing the token."); + HandlerUtil.handleError(resp, proxyResponse); + return null; + } + return proxyResponse; + } + return null; + } + + /*** + * This method is responsible to get the refresh token + * + * @param req {@link HttpServletRequest} + * @param resp {@link HttpServletResponse} + * @return If successfully renew tokens, returns TRUE otherwise return FALSE + * @throws IOException If an error occurs while witting error response to client side or invoke token renewal API + */ + private static boolean refreshToken(HttpServletRequest req, HttpServletResponse resp, String gatewayUrl) + throws IOException { + if (log.isDebugEnabled()) { + log.debug("refreshing the token"); + } + HttpPost tokenEndpoint = new HttpPost( + gatewayUrl + HandlerConstants.TOKEN_ENDPOINT); + HttpSession session = req.getSession(false); + if (session == null) { + log.error("Couldn't find a session, hence it is required to login and proceed."); + handleError(resp, HttpStatus.SC_UNAUTHORIZED); + return false; + } + + authData = (AuthData) session.getAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY); + StringEntity tokenEndpointPayload = new StringEntity( + "grant_type=refresh_token&refresh_token=" + authData.getRefreshToken() + "&scope=PRODUCTION", + ContentType.APPLICATION_FORM_URLENCODED); + + tokenEndpoint.setEntity(tokenEndpointPayload); + String encodedClientApp = authData.getEncodedClientApp(); + tokenEndpoint.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BASIC + + encodedClientApp); + tokenEndpoint.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.toString()); + + ProxyResponse tokenResultResponse = HandlerUtil.execute(tokenEndpoint); + if (tokenResultResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) { + log.error("Error occurred while refreshing access token."); + HandlerUtil.handleError(resp, tokenResultResponse); + return false; + } + + JsonParser jsonParser = new JsonParser(); + JsonElement jTokenResult = jsonParser.parse(tokenResultResponse.getData()); + + if (jTokenResult.isJsonObject()) { + JsonObject jTokenResultAsJsonObject = jTokenResult.getAsJsonObject(); + AuthData newAuthData = new AuthData(); + + newAuthData.setAccessToken(jTokenResultAsJsonObject.get("access_token").getAsString()); + newAuthData.setRefreshToken(jTokenResultAsJsonObject.get("refresh_token").getAsString()); + newAuthData.setScope(jTokenResultAsJsonObject.get("scope").getAsString()); + newAuthData.setClientId(authData.getClientId()); + newAuthData.setClientSecret(authData.getClientSecret()); + newAuthData.setEncodedClientApp(authData.getEncodedClientApp()); + newAuthData.setUsername(authData.getUsername()); + authData = newAuthData; + session.setAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY, newAuthData); + return true; + } + + log.error("Error Occurred in token renewal process."); + handleError(resp, HttpStatus.SC_INTERNAL_SERVER_ERROR); + return false; + } } diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml index 36fe025034..aa3f169a33 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/mdm-ui-config.xml @@ -22,6 +22,8 @@ true 3600 + + 10000 application_management From 36f132d363071462d6b7fbbe42a2c5e601181a5f Mon Sep 17 00:00:00 2001 From: vigneshan Date: Thu, 29 Jul 2021 10:46:08 +0530 Subject: [PATCH 16/17] Improve refresh token handling mechanism (cherry picked from commit b9d591adb5ecfc0b29ffdb04531a48256acf55a0) --- .../request/interceptor/InvokerHandler.java | 135 +++--------------- .../ui/request/interceptor/UserHandler.java | 10 +- 2 files changed, 23 insertions(+), 122 deletions(-) diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/InvokerHandler.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/InvokerHandler.java index 51a6c06674..1100928cc0 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/InvokerHandler.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/InvokerHandler.java @@ -18,9 +18,6 @@ package io.entgra.ui.request.interceptor; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; import io.entgra.ui.request.interceptor.beans.AuthData; import io.entgra.ui.request.interceptor.util.HandlerConstants; import io.entgra.ui.request.interceptor.util.HandlerUtil; @@ -43,7 +40,6 @@ import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.cookie.SM; import org.apache.http.entity.ContentType; import org.apache.http.entity.InputStreamEntity; -import org.apache.http.entity.StringEntity; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.InputStreamBody; @@ -84,7 +80,7 @@ public class InvokerHandler extends HttpServlet { ProxyResponse proxyResponse = HandlerUtil.execute(postRequest); if (HandlerConstants.TOKEN_IS_EXPIRED.equals(proxyResponse.getExecutorResponse())) { - proxyResponse = retryRequestWithRefreshedToken(req, resp, postRequest); + proxyResponse = HandlerUtil.retryRequestWithRefreshedToken(req, resp, postRequest, apiEndpoint); if (proxyResponse == null) { return; } @@ -112,15 +108,19 @@ public class InvokerHandler extends HttpServlet { getRequest.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BEARER + authData.getAccessToken()); ProxyResponse proxyResponse = HandlerUtil.execute(getRequest); if (HandlerConstants.TOKEN_IS_EXPIRED.equals(proxyResponse.getExecutorResponse())) { - proxyResponse = retryRequestWithRefreshedToken(req, resp, getRequest); + proxyResponse = HandlerUtil.retryRequestWithRefreshedToken(req, resp, getRequest, apiEndpoint); if (proxyResponse == null) { return; } } if (proxyResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) { - log.error("Error occurred while invoking the GET API endpoint."); - HandlerUtil.handleError(resp, proxyResponse); - return; + if (proxyResponse.getCode() == HttpStatus.SC_UNAUTHORIZED) { + proxyResponse = HandlerUtil.retryRequestWithRefreshedToken(req, resp, getRequest, apiEndpoint); + } else { + log.error("Error occurred while invoking the GET API endpoint."); + HandlerUtil.handleError(resp, proxyResponse); + return; + } } HandlerUtil.handleSuccess(resp, proxyResponse); } @@ -138,7 +138,7 @@ public class InvokerHandler extends HttpServlet { headRequest.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BEARER + authData.getAccessToken()); ProxyResponse proxyResponse = HandlerUtil.execute(headRequest); if (HandlerConstants.TOKEN_IS_EXPIRED.equals(proxyResponse.getExecutorResponse())) { - proxyResponse = retryRequestWithRefreshedToken(req, resp, headRequest); + proxyResponse = HandlerUtil.retryRequestWithRefreshedToken(req, resp, headRequest, apiEndpoint); if (proxyResponse == null) { return; } @@ -165,7 +165,7 @@ public class InvokerHandler extends HttpServlet { ProxyResponse proxyResponse = HandlerUtil.execute(putRequest); if (HandlerConstants.TOKEN_IS_EXPIRED.equals(proxyResponse.getExecutorResponse())) { - proxyResponse = retryRequestWithRefreshedToken(req, resp, putRequest); + proxyResponse = HandlerUtil.retryRequestWithRefreshedToken(req, resp, putRequest, apiEndpoint); if (proxyResponse == null) { return; } @@ -193,7 +193,7 @@ public class InvokerHandler extends HttpServlet { deleteRequest.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BEARER + authData.getAccessToken()); ProxyResponse proxyResponse = HandlerUtil.execute(deleteRequest); if (HandlerConstants.TOKEN_IS_EXPIRED.equals(proxyResponse.getExecutorResponse())) { - proxyResponse = retryRequestWithRefreshedToken(req, resp, deleteRequest); + proxyResponse = HandlerUtil.retryRequestWithRefreshedToken(req, resp, deleteRequest, apiEndpoint); if (proxyResponse == null) { return; } @@ -304,7 +304,7 @@ public class InvokerHandler extends HttpServlet { apiEndpoint = System.getProperty("iot.reporting.webapp.host"); if (StringUtils.isBlank(apiEndpoint)){ log.error("Reporting Endpoint is not defined in the iot-server.sh properly."); - handleError(resp, HttpStatus.SC_INTERNAL_SERVER_ERROR); + HandlerUtil.handleError(resp, HttpStatus.SC_INTERNAL_SERVER_ERROR); return false; } } @@ -312,125 +312,22 @@ public class InvokerHandler extends HttpServlet { HttpSession session = req.getSession(false); if (session == null) { log.error("Unauthorized, You are not logged in. Please log in to the portal"); - handleError(resp, HttpStatus.SC_UNAUTHORIZED); + HandlerUtil.handleError(resp, HttpStatus.SC_UNAUTHORIZED); return false; } authData = (AuthData) session.getAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY); if (authData == null) { log.error("Unauthorized, Access token not found in the current session"); - handleError(resp, HttpStatus.SC_UNAUTHORIZED); + HandlerUtil.handleError(resp, HttpStatus.SC_UNAUTHORIZED); return false; } if (req.getMethod() == null) { log.error("Bad Request, Request method is empty"); - handleError(resp, HttpStatus.SC_BAD_REQUEST); + HandlerUtil.handleError(resp, HttpStatus.SC_BAD_REQUEST); return false; } return true; } - - /** - * Retry request again after refreshing the access token - * - * @param req incoming {@link HttpServletRequest} - * @param resp resp {@link HttpServletResponse} - * @param httpRequest subclass of {@link HttpRequestBase} related to the current request. - * @return {@link ProxyResponse} if successful and null if failed. - * @throws IOException If an error occurs when try to retry the request. - */ - private ProxyResponse retryRequestWithRefreshedToken(HttpServletRequest req, HttpServletResponse resp, - HttpRequestBase httpRequest) throws IOException { - if (refreshToken(req, resp)) { - httpRequest.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BEARER + authData.getAccessToken()); - ProxyResponse proxyResponse = HandlerUtil.execute(httpRequest); - if (proxyResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) { - log.error("Error occurred while invoking the API after refreshing the token."); - HandlerUtil.handleError(resp, proxyResponse); - return null; - } - return proxyResponse; - } - return null; - } - - /*** - * This method is responsible to get the refresh token - * - * @param req {@link HttpServletRequest} - * @param resp {@link HttpServletResponse} - * @return If successfully renew tokens, returns TRUE otherwise return FALSE - * @throws IOException If an error occurs while witting error response to client side or invoke token renewal API - */ - private static boolean refreshToken(HttpServletRequest req, HttpServletResponse resp) - throws IOException { - if (log.isDebugEnabled()) { - log.debug("refreshing the token"); - } - HttpPost tokenEndpoint = new HttpPost( - apiEndpoint + HandlerConstants.TOKEN_ENDPOINT); - HttpSession session = req.getSession(false); - if (session == null) { - log.error("Couldn't find a session, hence it is required to login and proceed."); - handleError(resp, HttpStatus.SC_UNAUTHORIZED); - return false; - } - - StringEntity tokenEndpointPayload = new StringEntity( - "grant_type=refresh_token&refresh_token=" + authData.getRefreshToken() + "&scope=PRODUCTION", - ContentType.APPLICATION_FORM_URLENCODED); - - tokenEndpoint.setEntity(tokenEndpointPayload); - String encodedClientApp = authData.getEncodedClientApp(); - tokenEndpoint.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BASIC + - encodedClientApp); - tokenEndpoint.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.toString()); - - ProxyResponse tokenResultResponse = HandlerUtil.execute(tokenEndpoint); - if (tokenResultResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) { - log.error("Error occurred while refreshing access token."); - HandlerUtil.handleError(resp, tokenResultResponse); - return false; - } - - JsonParser jsonParser = new JsonParser(); - JsonElement jTokenResult = jsonParser.parse(tokenResultResponse.getData()); - - if (jTokenResult.isJsonObject()) { - JsonObject jTokenResultAsJsonObject = jTokenResult.getAsJsonObject(); - AuthData newAuthData = new AuthData(); - - newAuthData.setAccessToken(jTokenResultAsJsonObject.get("access_token").getAsString()); - newAuthData.setRefreshToken(jTokenResultAsJsonObject.get("refresh_token").getAsString()); - newAuthData.setScope(jTokenResultAsJsonObject.get("scope").getAsString()); - newAuthData.setClientId(authData.getClientId()); - newAuthData.setClientSecret(authData.getClientSecret()); - newAuthData.setEncodedClientApp(authData.getEncodedClientApp()); - newAuthData.setUsername(authData.getUsername()); - authData = newAuthData; - session.setAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY, newAuthData); - return true; - } - - log.error("Error Occurred in token renewal process."); - handleError(resp, HttpStatus.SC_INTERNAL_SERVER_ERROR); - return false; - } - - /** - * Handle error requests - * - * @param resp {@link HttpServletResponse} - * @param errorCode HTTP error status code - * @throws IOException If error occurred when trying to send the error response. - */ - private static void handleError(HttpServletResponse resp, int errorCode) - throws IOException { - ProxyResponse proxyResponse = new ProxyResponse(); - proxyResponse.setCode(errorCode); - proxyResponse.setExecutorResponse( - HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + HandlerUtil.getStatusKey(errorCode)); - HandlerUtil.handleError(resp, proxyResponse); - } } diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/UserHandler.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/UserHandler.java index 276413b217..270cf7c03c 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/UserHandler.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/UserHandler.java @@ -76,9 +76,13 @@ public class UserHandler extends HttpServlet { ProxyResponse tokenStatus = HandlerUtil.execute(tokenEndpoint); if (tokenStatus.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) { - log.error("Error occurred while invoking the API to get token status."); - HandlerUtil.handleError(resp, tokenStatus); - return; + if (tokenStatus.getCode() == HttpStatus.SC_UNAUTHORIZED) { + tokenStatus = HandlerUtil.retryRequestWithRefreshedToken(req, resp, tokenEndpoint, serverUrl); + } else { + log.error("Error occurred while invoking the API to get token status."); + HandlerUtil.handleError(resp, tokenStatus); + return; + } } String tokenData = tokenStatus.getData(); if (tokenData == null) { From 5795db1360e6ae5de55dba59e1c442208c5ca85d Mon Sep 17 00:00:00 2001 From: Muhammad Rashad Date: Mon, 2 Aug 2021 05:09:51 +0000 Subject: [PATCH 17/17] Block Uninstall Policy Feature (cherry picked from commit 556886daec3eddb082b1a362c6f0a4dc9e30fd3f) --- .../common/services/SubscriptionManager.java | 24 +++++----- .../core/impl/SubscriptionManagerImpl.java | 25 +++++----- .../task/ScheduledAppSubscriptionTask.java | 9 +++- .../ScheduledAppSubscriptionTaskManager.java | 8 +++- .../application/mgt/core/util/Constants.java | 1 + .../services/SubscriptionManagementAPI.java | 14 +++++- .../impl/SubscriptionManagementAPIImpl.java | 48 ++++++++++++------- .../impl/DeviceManagementServiceImpl.java | 3 +- .../device/mgt/common/MDMAppConstants.java | 1 + .../app/mgt/android/AppStoreApplication.java | 9 ++++ .../mgt/android/EnterpriseApplication.java | 9 ++++ .../app/mgt/android/WebApplication.java | 9 ++++ .../core/util/MDMAndroidOperationUtil.java | 4 ++ 13 files changed, 116 insertions(+), 48 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java index 600da94b4f..bc2eaabc47 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java @@ -24,8 +24,10 @@ import org.wso2.carbon.device.application.mgt.common.exception.SubscriptionManag import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; +import org.wso2.carbon.device.mgt.common.app.mgt.App; import java.util.List; +import java.util.Properties; /** * This interface manages all the operations related with ApplicationDTO Subscription. @@ -33,22 +35,22 @@ import java.util.List; public interface SubscriptionManager { /** * Performs bulk subscription operation for a given application and a subscriber list. - * - * @param applicationUUID UUID of the application to subscribe/unsubscribe + * @param applicationUUID UUID of the application to subscribe/unsubscribe * @param params list of subscribers. This list can be of either - * {@link org.wso2.carbon.device.mgt.common.DeviceIdentifier} if {@param subType} is equal + * {@link DeviceIdentifier} if {@param subType} is equal * to DEVICE or * {@link String} if {@param subType} is USER, ROLE or GROUP * @param subType subscription type. E.g. DEVICE, USER, ROLE, GROUP {@see { - * @param action subscription action. E.g. INSTALL/UNINSTALL {@see { - * @param generic type of the method. - * @return {@link ApplicationInstallResponse} - * @throws ApplicationManagementException if error occurs when subscribing to the given application - * @link org.wso2.carbon.device.application.mgt.common.SubscriptionType}} - * @link org.wso2.carbon.device.application.mgt.common.SubAction}} + * @param action subscription action. E.g. INSTALL/UNINSTALL {@see { + * @param generic type of the method. + * @return {@link ApplicationInstallResponse} + * @throws ApplicationManagementException if error occurs when subscribing to the given application + * @link org.wso2.carbon.device.application.mgt.common.SubscriptionType}} + * @link org.wso2.carbon.device.application.mgt.common.SubAction}} + * @param properties */ ApplicationInstallResponse performBulkAppOperation(String applicationUUID, List params, String subType, - String action) throws ApplicationManagementException; + String action, Properties properties) throws ApplicationManagementException; /** * Create an entry related to the scheduled task in the database. @@ -121,7 +123,7 @@ public interface SubscriptionManager { * @throws ApplicationManagementException if error occurred while installing given applications into the given * device */ - void installAppsForDevice(DeviceIdentifier deviceIdentifier, List releaseUUID) + void installAppsForDevice(DeviceIdentifier deviceIdentifier, List apps) throws ApplicationManagementException; /*** diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java index 8219dbb4e7..2de1094c1f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -121,7 +121,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { @Override public ApplicationInstallResponse performBulkAppOperation(String applicationUUID, List params, - String subType, String action) throws ApplicationManagementException { + String subType, String action, Properties properties) throws ApplicationManagementException { if (log.isDebugEnabled()) { log.debug("Install application release which has UUID " + applicationUUID + " to " + params.size() + " users."); @@ -134,7 +134,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { params); ApplicationInstallResponse applicationInstallResponse = performActionOnDevices( applicationSubscriptionInfo.getAppSupportingDeviceTypeName(), applicationSubscriptionInfo.getDevices(), - applicationDTO, subType, applicationSubscriptionInfo.getSubscribers(), action); + applicationDTO, subType, applicationSubscriptionInfo.getSubscribers(), action, properties); applicationInstallResponse.setErrorDeviceIdentifiers(applicationSubscriptionInfo.getErrorDeviceIdentifiers()); return applicationInstallResponse; @@ -347,7 +347,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } @Override - public void installAppsForDevice(DeviceIdentifier deviceIdentifier, List releaseUUIDs) + public void installAppsForDevice(DeviceIdentifier deviceIdentifier, List apps) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); @@ -370,7 +370,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager { List appInstallingDevices = new ArrayList<>(); - for (String releaseUUID : releaseUUIDs) { + for (App app : apps) { + String releaseUUID = app.getId(); try { ConnectionManagerUtil.openDBConnection(); ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(releaseUUID, tenantId); @@ -409,7 +410,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { if (!appInstallingDevices.isEmpty()) { performBulkAppOperation(releaseUUID, appInstallingDevices, SubscriptionType.DEVICE.toString(), - SubAction.INSTALL.toString()); + SubAction.INSTALL.toString(), app.getProperties()); } } } @@ -621,7 +622,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { * data. */ private ApplicationInstallResponse performActionOnDevices(String deviceType, List devices, - ApplicationDTO applicationDTO, String subType, List subscribers, String action) + ApplicationDTO applicationDTO, String subType, List subscribers, String action, Properties properties) throws ApplicationManagementException { //Get app subscribing info of each device @@ -667,11 +668,11 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } for (Map.Entry> entry : deviceIdentifierMap.entrySet()) { Activity activity = addAppOperationOnDevices(applicationDTO, new ArrayList<>(entry.getValue()), - entry.getKey(), action); + entry.getKey(), action, properties); activityList.add(activity); } } else { - Activity activity = addAppOperationOnDevices(applicationDTO, deviceIdentifiers, deviceType, action); + Activity activity = addAppOperationOnDevices(applicationDTO, deviceIdentifiers, deviceType, action, properties); activityList.add(activity); } @@ -982,13 +983,13 @@ public class SubscriptionManagerImpl implements SubscriptionManager { * @throws ApplicationManagementException if found an invalid device. */ private Activity addAppOperationOnDevices(ApplicationDTO applicationDTO, - List deviceIdentifierList, String deviceType, String action) + List deviceIdentifierList, String deviceType, String action, Properties properties) throws ApplicationManagementException { DeviceManagementProviderService deviceManagementProviderService = HelperUtil .getDeviceManagementProviderService(); try { Application application = APIUtil.appDtoToAppResponse(applicationDTO); - Operation operation = generateOperationPayloadByDeviceType(deviceType, application, action); + Operation operation = generateOperationPayloadByDeviceType(deviceType, application, action, properties); return deviceManagementProviderService.addOperation(deviceType, operation, deviceIdentifierList); } catch (OperationManagementException e) { String msg = "Error occurred while adding the application install operation to devices"; @@ -1010,7 +1011,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { * @throws ApplicationManagementException if unknown application type is found to generate operation payload or * invalid action is found to generate operation payload. */ - private Operation generateOperationPayloadByDeviceType(String deviceType, Application application, String action) + private Operation generateOperationPayloadByDeviceType(String deviceType, Application application, String action, Properties properties) throws ApplicationManagementException { try { if (ApplicationType.CUSTOM.toString().equalsIgnoreCase(application.getType())) { @@ -1046,6 +1047,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { app.setLocation(application.getApplicationReleases().get(0).getInstallerPath()); app.setIdentifier(application.getPackageName()); app.setName(application.getName()); + app.setProperties(properties); if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { return MDMAndroidOperationUtil.createInstallAppOperation(app); } else { @@ -1067,7 +1069,6 @@ public class SubscriptionManagerImpl implements SubscriptionManager { app.setType(mobileAppType); app.setLocation(plistDownloadEndpoint); app.setIconImage(application.getApplicationReleases().get(0).getIconPath()); - Properties properties = new Properties(); properties.put(MDMAppConstants.IOSConstants.IS_PREVENT_BACKUP, true); properties.put(MDMAppConstants.IOSConstants.IS_REMOVE_APP, true); properties.put(MDMAppConstants.IOSConstants.I_TUNES_ID, application.getPackageName()); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/task/ScheduledAppSubscriptionTask.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/task/ScheduledAppSubscriptionTask.java index 8a95efc0aa..d8f491e37d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/task/ScheduledAppSubscriptionTask.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/task/ScheduledAppSubscriptionTask.java @@ -37,6 +37,7 @@ import org.wso2.carbon.device.mgt.core.task.impl.RandomlyAssignedScheduleTask; import java.util.List; import java.util.Map; +import java.util.Properties; import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -45,6 +46,7 @@ public class ScheduledAppSubscriptionTask extends RandomlyAssignedScheduleTask { private static final String TASK_NAME = "SCHEDULE_APP_SUBSCRIPTION"; private SubscriptionManager subscriptionManager; + private String payload; private String subscribers; private String subscriptionType; private String application; @@ -57,6 +59,7 @@ public class ScheduledAppSubscriptionTask extends RandomlyAssignedScheduleTask { @Override public void setProperties(Map map) { this.subscribers = map.get(Constants.SUBSCRIBERS); + this.payload = map.get(Constants.PAYLOAD); this.subscriptionType = map.get(Constants.SUB_TYPE); this.application = map.get(Constants.APP_UUID); this.action = map.get(Constants.ACTION); @@ -89,8 +92,9 @@ public class ScheduledAppSubscriptionTask extends RandomlyAssignedScheduleTask { new TypeToken>() { }.getType()); try { + Properties properties = new Gson().fromJson(payload, Properties.class); subscriptionManager.performBulkAppOperation(this.application, deviceIdentifiers, - this.subscriptionType, this.action); + this.subscriptionType, this.action, properties); subscriptionDTO.setStatus(ExecutionStatus.EXECUTED); } catch (ApplicationManagementException e) { log.error( @@ -102,8 +106,9 @@ public class ScheduledAppSubscriptionTask extends RandomlyAssignedScheduleTask { List subscriberList = Pattern.compile(",").splitAsStream(this.subscribers).collect( Collectors.toList()); try { + Properties properties = new Gson().fromJson(payload, Properties.class); subscriptionManager.performBulkAppOperation(this.application, subscriberList, - this.subscriptionType, this.action); + this.subscriptionType, this.action, properties); subscriptionDTO.setStatus(ExecutionStatus.EXECUTED); } catch (ApplicationManagementException e) { log.error( diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/task/ScheduledAppSubscriptionTaskManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/task/ScheduledAppSubscriptionTaskManager.java index 326a358e1b..b274b1850f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/task/ScheduledAppSubscriptionTaskManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/task/ScheduledAppSubscriptionTaskManager.java @@ -46,6 +46,7 @@ import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Properties; import java.util.stream.Collectors; public class ScheduledAppSubscriptionTaskManager { @@ -73,7 +74,7 @@ public class ScheduledAppSubscriptionTaskManager { * @throws ApplicationOperationTaskException if error occurred while scheduling the subscription */ public void scheduleAppSubscriptionTask(String applicationUUID, List subscribers, - SubscriptionType subscriptionType, SubAction action, long timestamp) + SubscriptionType subscriptionType, SubAction action, long timestamp, Properties properties) throws ApplicationOperationTaskException { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date(timestamp * 1000)); @@ -106,7 +107,6 @@ public class ScheduledAppSubscriptionTaskManager { taskProperties.put(Constants.APP_UUID, applicationUUID); taskProperties.put(Constants.TENANT_DOMAIN, carbonContext.getTenantDomain(true)); taskProperties.put(Constants.SUBSCRIBER, carbonContext.getUsername()); - String subscribersString; if (SubscriptionType.DEVICE.equals(subscriptionType)) { subscribersString = new Gson().toJson(subscribers); @@ -115,6 +115,10 @@ public class ScheduledAppSubscriptionTaskManager { subscribersString = subscribers.stream().map(String.class::cast).collect(Collectors.joining(",")); taskProperties.put(Constants.SUBSCRIBERS, subscribersString); } + if(properties != null) { + String payload = new Gson().toJson(properties); + taskProperties.put(Constants.PAYLOAD, payload); + } if (log.isDebugEnabled()) { log.debug("Scheduling a task to " + action.toString() + " application: " + applicationUUID + " to/from the following " + subscriptionType.toString() + "S [" + subscribersString + "] at: " diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/Constants.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/Constants.java index 866fbdd524..27498f77bb 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/Constants.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/Constants.java @@ -58,6 +58,7 @@ public class Constants { public static final String SUB_TYPE = "SUBSCRIPTION_TYPE"; public static final String ACTION = "ACTION"; public static final String APP_UUID = "APP_UUID"; + public static final String APP_PROPERTIES = "APP_PROPERTIES"; public static final String SUBSCRIBER = "SUBSCRIBER"; public static final String TENANT_DOMAIN = "TENANT_DOMAIN"; public static final String TENANT_ID = "__TENANT_ID_PROP__"; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java index 2c8811358d..174b6921ee 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/SubscriptionManagementAPI.java @@ -130,7 +130,12 @@ public interface SubscriptionManagementAPI { name = "timestamp", value = "Timestamp of scheduled install/uninstall operation" ) - @QueryParam("timestamp") long timestamp + @QueryParam("timestamp") long timestamp, + @ApiParam( + name = "block-uninstall", + value = "App removal status of the install operation" + ) + @QueryParam("block-uninstall") Boolean isUninstallBlocked ); @POST @@ -183,7 +188,12 @@ public interface SubscriptionManagementAPI { name = "timestamp", value = "Timestamp of scheduled install/uninstall operation" ) - @QueryParam("timestamp") long timestamp + @QueryParam("timestamp") long timestamp, + @ApiParam( + name = "block-uninstall", + value = "App removal status of the install operation" + ) + @QueryParam("block-uninstall") Boolean isUninstallBlocked ); @POST diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java index a6324c8fdb..4a51353997 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java @@ -34,8 +34,6 @@ import org.wso2.carbon.device.application.mgt.common.BasicUserInfoList; import org.wso2.carbon.device.application.mgt.common.RoleList; import org.wso2.carbon.device.application.mgt.common.DeviceGroupList; import org.wso2.carbon.device.application.mgt.store.api.services.impl.util.RequestValidationUtil; -import org.wso2.carbon.device.mgt.common.PaginationRequest; -import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException; import org.wso2.carbon.device.application.mgt.core.exception.ForbiddenException; import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; @@ -44,6 +42,9 @@ import org.wso2.carbon.device.application.mgt.core.util.APIUtil; import org.wso2.carbon.device.application.mgt.store.api.services.SubscriptionManagementAPI; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.MDMAppConstants; +import org.wso2.carbon.device.mgt.common.PaginationRequest; +import org.wso2.carbon.device.mgt.common.PaginationResult; import javax.validation.Valid; import javax.ws.rs.Path; @@ -55,9 +56,8 @@ import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.core.Response; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; import java.util.List; +import java.util.Properties; /** * Implementation of Subscription Management related APIs. @@ -75,16 +75,22 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ @PathParam("uuid") String uuid, @PathParam("action") String action, @Valid List deviceIdentifiers, - @QueryParam("timestamp") long timestamp) { + @QueryParam("timestamp") long timestamp, + @QueryParam("block-uninstall") Boolean isUninstallBlocked + ) { + Properties properties = new Properties(); + if(isUninstallBlocked != null) { + properties.put(MDMAppConstants.AndroidConstants.IS_BLOCK_UNINSTALL, isUninstallBlocked); + } try { if (0 == timestamp) { SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); ApplicationInstallResponse response = subscriptionManager - .performBulkAppOperation(uuid, deviceIdentifiers, SubscriptionType.DEVICE.toString(), action); + .performBulkAppOperation(uuid, deviceIdentifiers, SubscriptionType.DEVICE.toString(), action, properties); return Response.status(Response.Status.OK).entity(response).build(); } else { return scheduleApplicationOperationTask(uuid, deviceIdentifiers, SubscriptionType.DEVICE, - SubAction.valueOf(action.toUpperCase()), timestamp); + SubAction.valueOf(action.toUpperCase()), timestamp, properties); } } catch (NotFoundException e) { String msg = "Couldn't found an application release for UUI: " + uuid; @@ -116,17 +122,23 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ @PathParam("subType") String subType, @PathParam("action") String action, @Valid List subscribers, - @QueryParam("timestamp") long timestamp) { + @QueryParam("timestamp") long timestamp, + @QueryParam("block-uninstall") Boolean isUninstallBlocked + ) { + Properties properties = new Properties(); + if(isUninstallBlocked != null) { + properties.put(MDMAppConstants.AndroidConstants.IS_BLOCK_UNINSTALL, isUninstallBlocked); + } try { if (0 == timestamp) { SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); ApplicationInstallResponse response = subscriptionManager - .performBulkAppOperation(uuid, subscribers, subType, action); + .performBulkAppOperation(uuid, subscribers, subType, action, properties); return Response.status(Response.Status.OK).entity(response).build(); } else { return scheduleApplicationOperationTask(uuid, subscribers, SubscriptionType.valueOf(subType.toUpperCase()), SubAction.valueOf(action.toUpperCase()), - timestamp); + timestamp, properties); } } catch (NotFoundException e) { String msg = "Couldn't found an application release for UUID: " + uuid + ". Hence, verify the payload"; @@ -170,7 +182,7 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ return Response.status(Response.Status.OK).entity(msg).build(); } else { return scheduleApplicationOperationTask(uuid, deviceIdentifiers, SubscriptionType.DEVICE, - SubAction.valueOf(SubAction.INSTALL.toString().toUpperCase()), timestamp); + SubAction.valueOf(SubAction.INSTALL.toString().toUpperCase()), timestamp, null); } } catch (NotFoundException e) { String msg = "Couldn't found an application release for UUI: " + uuid + " to perform ent app installation " @@ -216,7 +228,7 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ } else { return scheduleApplicationOperationTask(uuid, subscribers, SubscriptionType.valueOf(subType.toUpperCase()), - SubAction.valueOf(SubAction.INSTALL.toString().toUpperCase()), timestamp); + SubAction.valueOf(SubAction.INSTALL.toString().toUpperCase()), timestamp, null); } } catch (NotFoundException e) { String msg = "Couldn't found an application release for UUID: " + uuid + ". Hence, verify the payload"; @@ -255,11 +267,11 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ * @return {@link Response} of the operation */ private Response scheduleApplicationOperationTask(String applicationUUID, List subscribers, - SubscriptionType subType, SubAction subAction, long timestamp) { + SubscriptionType subType, SubAction subAction, long timestamp, Properties payload) { try { ScheduledAppSubscriptionTaskManager subscriptionTaskManager = new ScheduledAppSubscriptionTaskManager(); subscriptionTaskManager.scheduleAppSubscriptionTask(applicationUUID, subscribers, subType, subAction, - timestamp); + timestamp, payload); } catch (ApplicationOperationTaskException e) { String msg = "Error occurred while scheduling the application install operation"; log.error(msg, e); @@ -330,7 +342,7 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ return Response.status(Response.Status.FORBIDDEN).entity(msg).build(); } catch (ApplicationManagementException e) { String msg = "Error occurred while getting application with the application release uuid: " - + uuid; + + uuid; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } @@ -385,17 +397,17 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); } catch (BadRequestException e) { String msg = "Found invalid payload for getting application which has UUID: " + uuid - + ". Hence verify the payload"; + + ". Hence verify the payload"; log.error(msg, e); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } catch (ForbiddenException e) { String msg = "Application release is not in the installable state." - + "Hence you are not permitted to get the devices details."; + + "Hence you are not permitted to get the devices details."; log.error(msg, e); return Response.status(Response.Status.FORBIDDEN).entity(msg).build(); } catch (ApplicationManagementException e) { String msg = "Error occurred while getting application with the application " + - "release uuid: " + uuid; + "release uuid: " + uuid; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 7648a102b8..c0a5a4a6b4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -136,6 +136,7 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.ArrayList; +import java.util.Properties; @Path("/devices") public class DeviceManagementServiceImpl implements DeviceManagementService { @@ -915,7 +916,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { if (UUID != null) { ApplicationInstallResponse response = subscriptionManager .performBulkAppOperation(UUID, deviceIdentifiers, SubscriptionType.DEVICE.toString(), - "uninstall"); + "uninstall", new Properties()); return Response.status(Response.Status.OK).entity(response).build(); //if the applications not installed via entgra store } else { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/MDMAppConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/MDMAppConstants.java index e7f8f17df7..40b647d6ea 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/MDMAppConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/MDMAppConstants.java @@ -45,6 +45,7 @@ public class MDMAppConstants { private AndroidConstants() { throw new AssertionError(); } + public static final String IS_BLOCK_UNINSTALL = "isBlockUninstall"; public static final String OPCODE_INSTALL_APPLICATION = "INSTALL_APPLICATION"; public static final String OPCODE_UNINSTALL_APPLICATION = "UNINSTALL_APPLICATION"; public static final String UNMANAGED_APP_UNINSTALL= "UNMANAGED_APP_UNINSTALL"; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/android/AppStoreApplication.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/android/AppStoreApplication.java index 50126814ca..71464070b7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/android/AppStoreApplication.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/android/AppStoreApplication.java @@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.common.app.mgt.android; import com.google.gson.Gson; import java.io.Serializable; +import java.util.Properties; /** * This class represents the Appstore AuthenticationImpl information. @@ -29,6 +30,7 @@ public class AppStoreApplication implements Serializable { private String type; private String appIdentifier; + private Properties properties; public String getType() { return type; @@ -51,4 +53,11 @@ public class AppStoreApplication implements Serializable { return gson.toJson(this); } + public Properties getProperties() { + return properties; + } + + public void setProperties(Properties properties) { + this.properties = properties; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/android/EnterpriseApplication.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/android/EnterpriseApplication.java index f087013a18..0e9bdb47da 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/android/EnterpriseApplication.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/android/EnterpriseApplication.java @@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.common.app.mgt.android; import com.google.gson.Gson; import java.io.Serializable; +import java.util.Properties; /** * This class represents the Enterprise AuthenticationImpl information. @@ -30,6 +31,7 @@ public class EnterpriseApplication implements Serializable { private String type; private String url; private String appIdentifier; + private Properties properties; public String getAppIdentifier() { return appIdentifier; @@ -60,4 +62,11 @@ public class EnterpriseApplication implements Serializable { return gson.toJson(this); } + public Properties getProperties() { + return properties; + } + + public void setProperties(Properties properties) { + this.properties = properties; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/android/WebApplication.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/android/WebApplication.java index 3257429269..70797dc8d9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/android/WebApplication.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/app/mgt/android/WebApplication.java @@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.common.app.mgt.android; import com.google.gson.Gson; import java.io.Serializable; +import java.util.Properties; /** * This class represents the Web AuthenticationImpl information. @@ -30,6 +31,7 @@ public class WebApplication implements Serializable { private String name; private String url; private String type; + private Properties properties; public String getName() { return name; @@ -60,4 +62,11 @@ public class WebApplication implements Serializable { return gson.toJson(this); } + public Properties getProperties() { + return properties; + } + + public void setProperties(Properties properties) { + this.properties = properties; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/MDMAndroidOperationUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/MDMAndroidOperationUtil.java index 14dbaea9ed..99cacfd950 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/MDMAndroidOperationUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/MDMAndroidOperationUtil.java @@ -52,6 +52,7 @@ public class MDMAndroidOperationUtil { enterpriseApplication.setType(application.getType().toString()); enterpriseApplication.setUrl(application.getLocation()); enterpriseApplication.setAppIdentifier(application.getIdentifier()); + enterpriseApplication.setProperties(application.getProperties()); operation.setPayLoad(enterpriseApplication.toJSON()); break; case PUBLIC: @@ -59,6 +60,7 @@ public class MDMAndroidOperationUtil { new AppStoreApplication(); appStoreApplication.setType(application.getType().toString()); appStoreApplication.setAppIdentifier(application.getIdentifier()); + appStoreApplication.setProperties(application.getProperties()); operation.setPayLoad(appStoreApplication.toJSON()); break; case WEBAPP: @@ -67,6 +69,7 @@ public class MDMAndroidOperationUtil { webApplication.setUrl(application.getLocation()); webApplication.setName(application.getName()); webApplication.setType(application.getType().toString()); + webApplication.setProperties(application.getProperties()); operation.setPayLoad(webApplication.toJSON()); break; default: @@ -93,6 +96,7 @@ public class MDMAndroidOperationUtil { new EnterpriseApplication(); enterpriseApplication.setType(application.getType().toString()); enterpriseApplication.setAppIdentifier(application.getIdentifier()); + enterpriseApplication.setProperties(application.getProperties()); operation.setPayLoad(enterpriseApplication.toJSON()); break; case PUBLIC: