From acd3039c65c3827e9e8bbc4b26676e42cfc79651 Mon Sep 17 00:00:00 2001 From: ThilinaPremachandra Date: Tue, 4 Apr 2023 03:01:09 +0530 Subject: [PATCH 1/9] pom.xml updated by pull --- .../org.wso2.carbon.device.mgt.analytics.data.publisher/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/pom.xml index 3ac62b95001..d3196bf7e20 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/pom.xml @@ -47,7 +47,7 @@ testng - org.wso2.tomcat + org. wso2.tomcat tomcat From 145740fec6e08c3c71d90061e7fb1958aa396bff Mon Sep 17 00:00:00 2001 From: ThilinaPremachandra Date: Tue, 4 Apr 2023 13:52:03 +0530 Subject: [PATCH 2/9] Add hierarchical grouping feature --- .../service/api/GroupManagementService.java | 81 +++++++++ .../admin/GroupManagementAdminService.java | 86 +++++++++- .../impl/GroupManagementServiceImpl.java | 54 +++++- .../GroupManagementAdminServiceImpl.java | 38 ++++- .../group/mgt/DeviceGroupRoleWrapper.java | 155 ++++++++++++++++++ .../core/device/mgt/core/dao/GroupDAO.java | 30 ++++ .../core/dao/impl/AbstractGroupDAOImpl.java | 101 ++++++++++-- .../impl/group/PostgreSQLGroupDAOImpl.java | 42 ++++- .../core/dao/util/GroupManagementDAOUtil.java | 1 + .../GroupManagementProviderService.java | 18 +- .../GroupManagementProviderServiceImpl.java | 85 +++++++++- .../src/test/resources/sql/h2.sql | 1 + .../pom.xml | 2 +- .../src/test/resources/sql/CreateH2TestDB.sql | 1 + .../src/main/resources/dbscripts/cdm/h2.sql | 1 + .../main/resources/dbscripts/cdm/mssql.sql | 1 + .../main/resources/dbscripts/cdm/mysql.sql | 1 + .../main/resources/dbscripts/cdm/oracle.sql | 1 + .../resources/dbscripts/cdm/postgresql.sql | 1 + 19 files changed, 655 insertions(+), 45 deletions(-) create mode 100644 components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/group/mgt/DeviceGroupRoleWrapper.java diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/GroupManagementService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/GroupManagementService.java index fb348388d54..33614a73287 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/GroupManagementService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/GroupManagementService.java @@ -40,6 +40,7 @@ import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.DeviceToGroupsAssign import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.ErrorResponse; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.RoleList; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.Constants; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupRoleWrapper; import javax.validation.Valid; import javax.ws.rs.Consumes; @@ -182,6 +183,13 @@ import java.util.List; key = "perm:groups:devices-types", roles = {"Internal/devicemgt-user"}, permissions = {"/device-mgt/groups/devices/types"} + ), + @Scope( + name = "View whether the groups has relevant device types", + description = "View whether the groups has relevant device types", + key = "perm:groups:add", + roles = {"Internal/devicemgt-user"}, + permissions = {"/device-mgt/groups/device-types"} ) } ) @@ -1229,4 +1237,77 @@ public interface GroupManagementService { required = true) List identifiers); + + @POST + @Path("/roles/share") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_POST, + value = "Consolidated API for Creating a Device Group, Adding Devices, and Sharing", + notes = "This API can be used to create a new device group, add devices to the group, and share the group with user roles.", + tags = "Device Group Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:groups:add") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 201, + message = "Created. \n Device group has successfully been created.", + responseHeaders = { + @ResponseHeader( + name = "Content-Location", + description = "The URL of the created group."), + @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 = 303, + message = "See Other. \n Source can be retrieved from the URL specified at the Location " + + "header.", + responseHeaders = { + @ResponseHeader( + name = "Content-Location", + description = "The Source URL of the document.")}), + @ApiResponse( + code = 400, + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), + @ApiResponse( + code = 401, + message = "Unauthorized. \n Current logged in user is not authorized to perform the operation.", + response = ErrorResponse.class), + @ApiResponse( + code = 404, + message = "Group not found.", + response = ErrorResponse.class), + @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 creating the group or adding devices or sharing the group.", + response = ErrorResponse.class) + }) + Response createGroupWithRoles(@ApiParam( + name = "group", + value = "Define the group object with data.", + required = true) + @Valid DeviceGroupRoleWrapper group + ); + } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/admin/GroupManagementAdminService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/admin/GroupManagementAdminService.java index 2c8571efbea..373afe92726 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/admin/GroupManagementAdminService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/api/admin/GroupManagementAdminService.java @@ -36,9 +36,16 @@ import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroup; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.DeviceGroupList; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.ErrorResponse; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.Constants; - +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupRoleWrapper; import javax.validation.Valid; -import javax.ws.rs.*; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -368,4 +375,79 @@ public interface GroupManagementAdminService { required = true) @Valid DeviceGroup group); + + + @POST + @Path("/roles/share") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_POST, + value = "Consolidated API for Creating a Device Group, Adding Devices, and Sharing", + notes = "This API can be used to create a new device group, add devices to the group, and share the group with user roles.", + tags = "Device Group Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = Constants.SCOPE, value = "perm:groups:add") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 201, + message = "Created. \n Device group has successfully been created.", + responseHeaders = { + @ResponseHeader( + name = "Content-Location", + description = "The URL of the created group."), + @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 = 303, + message = "See Other. \n Source can be retrieved from the URL specified at the Location " + + "header.", + responseHeaders = { + @ResponseHeader( + name = "Content-Location", + description = "The Source URL of the document.")}), + @ApiResponse( + code = 400, + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), + @ApiResponse( + code = 401, + message = "Unauthorized. \n Current logged in user is not authorized to perform the operation.", + response = ErrorResponse.class), + @ApiResponse( + code = 404, + message = "Group not found.", + response = ErrorResponse.class), + @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 creating the group or adding devices or sharing the group.", + response = ErrorResponse.class) + }) + Response createGroupWithRoles(@ApiParam( + name = "group", + value = "Define the group object with data.", + required = true) + @Valid DeviceGroupRoleWrapper group + + ); + } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImpl.java index 1e6f525cb33..b68bbd0f3a4 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImpl.java @@ -18,6 +18,14 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroup; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupConstants; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupRoleWrapper; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceTypesOfGroups; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupAlreadyExistException; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupNotExistException; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.RoleDoesNotExistException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.CarbonConstants; @@ -29,13 +37,6 @@ import io.entgra.device.mgt.core.device.mgt.common.GroupPaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.PaginationResult; import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceNotFoundException; -import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroup; -import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupConstants; -import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceTypesOfGroups; -import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupAlreadyExistException; -import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException; -import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupNotExistException; -import io.entgra.device.mgt.core.device.mgt.common.group.mgt.RoleDoesNotExistException; import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; import io.entgra.device.mgt.core.device.mgt.core.service.GroupManagementProviderService; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.DeviceGroupList; @@ -48,6 +49,9 @@ import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.DeviceMgtAPIUtils; import io.entgra.device.mgt.core.policy.mgt.common.PolicyAdministratorPoint; import io.entgra.device.mgt.core.policy.mgt.common.PolicyManagementException; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.transaction.UserTransaction; import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.POST; @@ -430,4 +434,38 @@ public class GroupManagementServiceImpl implements GroupManagementService { } } -} + @POST + @Path("/roles/share") + @Override + public Response createGroupWithRoles(DeviceGroupRoleWrapper groups) { + String owner = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + if (groups == null) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } + groups.setOwner(owner); + groups.setStatus(DeviceGroupConstants.GroupStatus.ACTIVE); + try { + DeviceMgtAPIUtils.getGroupManagementProviderService().createGroupWithRoles(groups, DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); + DeviceGroup group = DeviceMgtAPIUtils.getGroupManagementProviderService().getGroup(groups.getName(), owner.isEmpty()); + if (group != null) { + DeviceMgtAPIUtils.getGroupManagementProviderService().manageGroupSharing(group.getGroupId(), groups.getUserRoles()); + return Response.status(Response.Status.CREATED).entity(group.getGroupId()).build(); + } else { + String msg = "Error occurred while retrieving newly created group."; + log.error(msg); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } + } catch (GroupManagementException e) { + String msg = "Error occurred while adding new group."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (GroupAlreadyExistException e) { + String msg = "Group already exists with name : " + groups.getName() + "."; + log.warn(msg); + return Response.status(Response.Status.CONFLICT).entity(msg).build(); + } catch (RoleDoesNotExistException e) { + return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build(); + } + } + +} \ No newline at end of file diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java index 20fa85d8558..30546f214ab 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java @@ -17,21 +17,25 @@ */ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.admin; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import io.entgra.device.mgt.core.device.mgt.common.GroupPaginationRequest; -import io.entgra.device.mgt.core.device.mgt.common.PaginationResult; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroup; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupConstants; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupRoleWrapper; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupAlreadyExistException; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.RoleDoesNotExistException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import io.entgra.device.mgt.core.device.mgt.common.GroupPaginationRequest; +import io.entgra.device.mgt.core.device.mgt.common.PaginationResult; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.DeviceGroupList; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.api.admin.GroupManagementAdminService; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.util.RequestValidationUtil; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.DeviceMgtAPIUtils; +import org.wso2.carbon.context.PrivilegedCarbonContext; import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; +import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; @@ -144,4 +148,30 @@ public class GroupManagementAdminServiceImpl implements GroupManagementAdminServ return Response.status(Response.Status.CONFLICT).entity(msg).build(); } } + @POST + @Path("/roles/share") + @Override + public Response createGroupWithRoles(DeviceGroupRoleWrapper group) { + String owner = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + if (group == null) { + return Response.status(Response.Status.BAD_REQUEST).build(); + } + group.setOwner(owner); + group.setStatus(DeviceGroupConstants.GroupStatus.ACTIVE); + try { + DeviceMgtAPIUtils.getGroupManagementProviderService().createGroupWithRoles(group, DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); + DeviceMgtAPIUtils.getGroupManagementProviderService().manageGroupSharing(group.getGroupId(), group.getUserRoles()); + return Response.status(Response.Status.CREATED).build(); + } catch (GroupManagementException e) { + String msg = "Error occurred while adding new group."; + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } catch (GroupAlreadyExistException e) { + String msg = "Group already exists with name : " + group.getName() + "."; + return Response.status(Response.Status.CONFLICT).entity(msg).build(); + } catch (RoleDoesNotExistException e) { + return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build(); + } + } + } + diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/group/mgt/DeviceGroupRoleWrapper.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/group/mgt/DeviceGroupRoleWrapper.java new file mode 100644 index 00000000000..132730e9158 --- /dev/null +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/group/mgt/DeviceGroupRoleWrapper.java @@ -0,0 +1,155 @@ +/* + * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.entgra.device.mgt.core.device.mgt.common.group.mgt; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import io.swagger.annotations.ApiParam; + +import java.io.Serializable; +import java.util.List; +import java.util.Map; + +/** + * Holds Device Group details and expose to external access + */ +@ApiModel(value = "DeviceGroupRoleWrapper", description = "This class carries all information related to a managed device group.") +public class DeviceGroupRoleWrapper implements Serializable { + + private static final long serialVersionUID = 1998121711L; + + @ApiModelProperty(name = "id", value = "ID of the device group in the device group information database.") + private int id; + + @ApiModelProperty(name = "description", value = "The device group description that can be set on the device group by the user.", + required = true) + private String description; + + @ApiModelProperty(name = "name", value = "The device group name that can be set on the device group by the user.", + required = true) + private String name; + + private String owner; + + @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; + + @ApiModelProperty(name = "userRoles", value = "User roles") + private List userRoles; + +// @ApiModelProperty(name = "DeviceGroupRoleWrapper", value = "Device group role wrapper") +// private List DeviceGroupRoleWrapper; + + public DeviceGroupRoleWrapper() {} + + public DeviceGroupRoleWrapper(String name) { + this.name = name; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + private Map groupProperties; + + public int getGroupId() { + return id; + } + + public void setGroupId(int id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getOwner() { + return owner; + } + + public void setOwner(String owner) { + this.owner = owner; + } + + public Map getGroupProperties() { + return groupProperties; + } + + public void setGroupProperties(Map groupProperties) { + 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; + } + + public List getUserRoles() { + return userRoles; + } + + public void setUserRoles(List userRoles) { + this.userRoles = userRoles; + } +} diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java index 8176e59f729..55f1da1f0d0 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java @@ -23,6 +23,7 @@ import io.entgra.device.mgt.core.device.mgt.common.GroupPaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.exceptions.ReportManagementException; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroup; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupRoleWrapper; import java.util.List; import java.util.Map; @@ -41,6 +42,21 @@ public interface GroupDAO { */ int addGroup(DeviceGroup deviceGroup, int tenantId) throws GroupManagementDAOException; + + /** + * Add properties for device group. + * Note that groupId parameter is considered seperately due to the groupId parameter passed with + * device group Payload is ignored in the add/update logic instead the internal groupId reference is used. + * + * @param groups to be added. + * @param tenantId of the group. + * @return sql execution result. + * @throws GroupManagementDAOException + */ + int addGroupWithRoles(DeviceGroupRoleWrapper groups, int tenantId) throws GroupManagementDAOException; + + + /** * Add properties for device group. * Note that groupId parameter is considered seperately due to the groupId parameter passed with @@ -51,8 +67,22 @@ public interface GroupDAO { * @return sql execution result. * @throws GroupManagementDAOException */ + boolean addGroupProperties(DeviceGroup deviceGroup, int groupId, int tenantId) throws GroupManagementDAOException; + /** + * Update properties for device group. + * Note that groupId parameter is considered seperately due to the groupId parameter passed with + * device group Payload is ignored in the add/update logic instead the internal groupId reference is used. + * + * @param groups to be updated. + * @param tenantId of the group. + * @return sql execution result. + * @throws GroupManagementDAOException + */ + + boolean addGroupPropertiesWithRoles(DeviceGroupRoleWrapper groups, int groupId, int tenantId) throws GroupManagementDAOException; + /** * Update properties for device group. * Note that groupId parameter is considered seperately due to the groupId parameter passed with diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java index b8196c6e035..7331ab6ad5f 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.core.dao.impl; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupRoleWrapper; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -53,7 +54,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { throws GroupManagementDAOException { try { Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP " + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH, PARENT_GROUP_ID FROM DM_GROUP " + "WHERE TENANT_ID = ?"; if (StringUtils.isNotBlank(request.getGroupName())) { sql += " AND GROUP_NAME LIKE ?"; @@ -115,7 +116,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { try { Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP WHERE TENANT_ID = ?"; + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH, PARENT_GROUP_ID FROM DM_GROUP WHERE TENANT_ID = ?"; if (StringUtils.isNotBlank(request.getGroupName())) { sql += " AND GROUP_NAME LIKE ?"; } @@ -179,11 +180,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, PARENT_PATH) " - + "VALUES (?, ?, ?, ?, ?)"; - } else { - sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID, PARENT_PATH, STATUS) " + sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID, PARENT_PATH, PARENT_GROUP_ID) " + "VALUES (?, ?, ?, ?, ?, ?)"; + } else { + sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID, PARENT_PATH, PARENT_GROUP_ID, STATUS) " + + "VALUES (?, ?, ?, ?, ?, ?, ?)"; hasStatus = true; } stmt = conn.prepareStatement(sql, new String[]{"ID"}); @@ -192,10 +193,12 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { stmt.setString(3, deviceGroup.getOwner()); stmt.setInt(4, tenantId); stmt.setString(5, deviceGroup.getParentPath()); + stmt.setInt(6, deviceGroup.getParentGroupId()); if (hasStatus) { - stmt.setString(6, deviceGroup.getStatus()); + stmt.setString(7, deviceGroup.getStatus()); } + stmt.executeUpdate(); rs = stmt.getGeneratedKeys(); if (rs.next()) { @@ -210,6 +213,48 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { } } + public int addGroupWithRoles(DeviceGroupRoleWrapper groups, int tenantId) throws GroupManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs; + int groupId = -1; + boolean hasStatus = false; + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + String sql; + if (groups.getStatus() == null || groups.getStatus().isEmpty()) { + sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID, PARENT_PATH, PARENT_GROUP_ID) " + + "VALUES (?, ?, ?, ?, ?, ?)"; + } else { + sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID, PARENT_PATH, PARENT_GROUP_ID, STATUS) " + + "VALUES (?, ?, ?, ?, ?, ?, ?)"; + hasStatus = true; + } + stmt = conn.prepareStatement(sql, new String[]{"ID"}); + stmt.setString(1, groups.getDescription()); + stmt.setString(2, groups.getName()); + stmt.setString(3, groups.getOwner()); + stmt.setInt(4, tenantId); + stmt.setString(5, groups.getParentPath()); + stmt.setInt(6, groups.getParentGroupId()); + if (hasStatus) { + stmt.setString(7, groups.getStatus()); + } + + + stmt.executeUpdate(); + rs = stmt.getGeneratedKeys(); + if (rs.next()) { + groupId = rs.getInt(1); + } + return groupId; + } catch (SQLException e) { + throw new GroupManagementDAOException("Error occurred while adding deviceGroup '" + + groups.getName() + "'", e); + } finally { + GroupManagementDAOUtil.cleanupResources(stmt, null); + } + } + public boolean addGroupProperties(DeviceGroup deviceGroup, int groupId, int tenantId) throws GroupManagementDAOException { boolean status; @@ -238,6 +283,34 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { return status; } + public boolean addGroupPropertiesWithRoles(DeviceGroupRoleWrapper groups, int groupId, int tenantId) + throws GroupManagementDAOException { + boolean status; + PreparedStatement stmt = null; + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + stmt = conn.prepareStatement( + "INSERT INTO GROUP_PROPERTIES(GROUP_ID, PROPERTY_NAME, " + + "PROPERTY_VALUE, TENANT_ID) VALUES (?, ?, ?, ?)"); + for (Map.Entry entry : groups.getGroupProperties().entrySet()) { + stmt.setInt(1, groupId); + stmt.setString(2, entry.getKey()); + stmt.setString(3, entry.getValue()); + stmt.setInt(4, tenantId); + stmt.addBatch(); + } + stmt.executeBatch(); + status = true; + } catch (SQLException e) { + String msg = "Error occurred while adding properties for group '" + + groups.getName() + "' values : " + groups.getGroupProperties(); + throw new GroupManagementDAOException(msg, e); + } finally { + GroupManagementDAOUtil.cleanupResources(stmt, null); + } + return status; + } + public boolean updateGroupProperties(DeviceGroup deviceGroup, int groupId, int tenantId) throws GroupManagementDAOException { boolean status; @@ -495,7 +568,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { ResultSet resultSet = null; try { Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP WHERE ID = ? " + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH, PARENT_GROUP_ID FROM DM_GROUP WHERE ID = ? " + "AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, groupId); @@ -518,7 +591,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { 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 " + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH, PARENT_GROUP_ID FROM DM_GROUP " + "WHERE PARENT_PATH LIKE ? AND TENANT_ID = ?"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, parentPath + "%"); @@ -543,7 +616,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { 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 " + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH, PARENT_GROUP_ID FROM DM_GROUP " + "WHERE PARENT_PATH LIKE ? AND TENANT_ID = ?"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, "/"); @@ -571,7 +644,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, G.PARENT_PATH FROM DM_GROUP G " + + String sql = "SELECT G.ID, G.GROUP_NAME, G.DESCRIPTION, G.OWNER, G.STATUS, G.PARENT_PATH, G.PARENT_GROUP_ID 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); @@ -709,7 +782,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { try { Connection conn = GroupManagementDAOFactory.getConnection(); String sql = - "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP " + "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH, PARENT_GROUP_ID FROM DM_GROUP " + "WHERE LOWER(GROUP_NAME) = LOWER(?) AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, groupName); @@ -880,7 +953,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { List deviceGroupList = null; try { Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP g, " + + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH, PARENT_GROUP_ID P FROM DM_GROUP g, " + "(SELECT GROUP_ID FROM DM_ROLE_GROUP_MAP WHERE ROLE IN ("; int index = 0; @@ -994,7 +1067,7 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { List deviceGroupList = null; try { Connection conn = GroupManagementDAOFactory.getConnection(); - String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH FROM DM_GROUP " + String sql = "SELECT ID, DESCRIPTION, GROUP_NAME, OWNER, STATUS, PARENT_PATH, PARENT_GROUP_ID FROM DM_GROUP " + "WHERE OWNER = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, username); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/group/PostgreSQLGroupDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/group/PostgreSQLGroupDAOImpl.java index 9dd23da5c0e..00b581c1a41 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/group/PostgreSQLGroupDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/group/PostgreSQLGroupDAOImpl.java @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.core.dao.impl.group; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupRoleWrapper; import org.apache.solr.common.StringUtils; import io.entgra.device.mgt.core.device.mgt.common.Device; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroup; @@ -49,7 +50,7 @@ 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, PARENT_PATH) " + + sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID, PARENT_PATH, PARENT_GROUP_ID) " + "VALUES (?, ?, ?, ?) RETURNING ID"; } else { sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID, PARENT_PATH, STATUS) " + @@ -79,6 +80,45 @@ public class PostgreSQLGroupDAOImpl extends AbstractGroupDAOImpl { } } + public int addGroupWithRoles(DeviceGroupRoleWrapper groups, int tenantId) throws GroupManagementDAOException { + PreparedStatement stmt = null; + ResultSet rs; + int groupId = -1; + boolean hasStatus = false; + try { + Connection conn = GroupManagementDAOFactory.getConnection(); + String sql; + if(StringUtils.isEmpty(groups.getStatus())) { + 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, PARENT_PATH, STATUS) " + + "VALUES (?, ?, ?, ?, ?) RETURNING ID"; + hasStatus = true; + } + stmt = conn.prepareStatement(sql); + stmt.setString(1, groups.getDescription()); + stmt.setString(2, groups.getName()); + stmt.setString(3, groups.getOwner()); + stmt.setInt(4, tenantId); + stmt.setString(5, groups.getParentPath()); + if(hasStatus) { + stmt.setString(6, groups.getStatus()); + } + stmt.execute(); + rs = stmt.getGeneratedKeys(); + if (rs.next()) { + groupId = rs.getInt(1); + } + return groupId; + } catch (SQLException e) { + throw new GroupManagementDAOException("Error occurred while adding deviceGroup '" + + groups.getName() + "'", e); + } finally { + GroupManagementDAOUtil.cleanupResources(stmt, null); + } + } + @Override public List getDevices(int groupId, int startIndex, int rowCount, int tenantId) throws GroupManagementDAOException { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/util/GroupManagementDAOUtil.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/util/GroupManagementDAOUtil.java index f5b8f4ce4a9..82f3c05cc6b 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/util/GroupManagementDAOUtil.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/util/GroupManagementDAOUtil.java @@ -87,6 +87,7 @@ public final class GroupManagementDAOUtil { group.setOwner(resultSet.getString("OWNER")); group.setStatus(resultSet.getString("STATUS")); group.setParentPath(resultSet.getString("PARENT_PATH")); + group.setParentGroupId(resultSet.getInt("PARENT_GROUP_ID")); return group; } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java index 5f7840c5e7d..f65244842c3 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java @@ -23,12 +23,7 @@ import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceNotFoundException; import io.entgra.device.mgt.core.device.mgt.common.GroupPaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.PaginationResult; -import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroup; -import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceTypesOfGroups; -import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupAlreadyExistException; -import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException; -import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupNotExistException; -import io.entgra.device.mgt.core.device.mgt.common.group.mgt.RoleDoesNotExistException; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.*; import java.util.List; @@ -48,6 +43,17 @@ public interface GroupManagementProviderService { void createGroup(DeviceGroup deviceGroup, String defaultRole, String[] defaultPermissions) throws GroupManagementException, GroupAlreadyExistException; + /** + * Add new device group and create default role with default permissions. + * + * @param groups to add + * @param defaultRole of the deviceGroup + * @param defaultPermissions of the default role + * @throws GroupManagementException + */ + void createGroupWithRoles(DeviceGroupRoleWrapper groups, String defaultRole, + String[] defaultPermissions )throws GroupManagementException, GroupAlreadyExistException, RoleDoesNotExistException; + /** * Update existing device group. * diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java index 23fb15646a7..40d45b09638 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -18,6 +18,20 @@ package io.entgra.device.mgt.core.device.mgt.core.service; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroup; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupConstants; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupRoleWrapper; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceTypesOfGroups; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupAlreadyExistException; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupNotExistException; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.RoleDoesNotExistException; +import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceDAO; +import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOException; +import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOFactory; +import io.entgra.device.mgt.core.device.mgt.core.dao.GroupDAO; +import io.entgra.device.mgt.core.device.mgt.core.dao.GroupManagementDAOException; +import io.entgra.device.mgt.core.device.mgt.core.dao.GroupManagementDAOFactory; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -33,14 +47,6 @@ import io.entgra.device.mgt.core.device.mgt.common.GroupPaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.PaginationResult; import io.entgra.device.mgt.core.device.mgt.common.exceptions.TrackerAlreadyExistException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.TransactionManagementException; -import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroup; -import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupConstants; -import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceTypesOfGroups; -import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupAlreadyExistException; -import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException; -import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupNotExistException; -import io.entgra.device.mgt.core.device.mgt.common.group.mgt.RoleDoesNotExistException; -import io.entgra.device.mgt.core.device.mgt.core.dao.*; import io.entgra.device.mgt.core.device.mgt.core.event.config.GroupAssignmentEventOperationExecutor; import io.entgra.device.mgt.core.device.mgt.core.geo.task.GeoFenceEventOperationManager; import io.entgra.device.mgt.core.device.mgt.core.internal.DeviceManagementDataHolder; @@ -57,7 +63,6 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.stream.Collectors; @@ -142,6 +147,68 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } } + + public void createGroupWithRoles(DeviceGroupRoleWrapper groups, String defaultRole, String[] defaultPermissions) + throws GroupManagementException, GroupAlreadyExistException { + if (groups == null) { + String msg = "Received incomplete data for createGroup"; + log.error(msg); + throw new GroupManagementException(msg); + } + if (log.isDebugEnabled()) { + log.debug("Creating group '" + groups.getName() + "'"); + } + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + GroupManagementDAOFactory.beginTransaction(); + DeviceGroup existingGroup = this.groupDAO.getGroup(groups.getName(), tenantId); + if (existingGroup == null) { + if (groups.getParentGroupId() == 0) { + groups.setParentPath(DeviceGroupConstants.HierarchicalGroup.SEPERATOR); + } else { + DeviceGroup immediateParentGroup = groupDAO.getGroup(groups.getParentGroupId(), tenantId); + if (immediateParentGroup == null) { + String msg = "Parent group with group ID '" + groups.getParentGroupId() + + "' does not exist. Hence creating of group '" + groups.getName() + + "' was not success"; + log.error(msg); + throw new GroupManagementException(msg); + } + String parentPath = DeviceManagerUtil.createParentPath(immediateParentGroup); + groups.setParentPath(parentPath); + } + int updatedGroupID = this.groupDAO.addGroupWithRoles(groups, tenantId); + if (groups.getGroupProperties() != null && groups.getGroupProperties().size() > 0) { + this.groupDAO.addGroupPropertiesWithRoles(groups, updatedGroupID, tenantId); + } + GroupManagementDAOFactory.commitTransaction(); + } else { + throw new GroupAlreadyExistException("Group exist with name " + groups.getName()); + } + } catch (GroupManagementDAOException e) { + GroupManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while adding deviceGroup '" + groups.getName() + "' to database."; + log.error(msg, e); + throw new GroupManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while initiating transaction."; + log.error(msg, e); + throw new GroupManagementException(msg, e); + } catch (GroupAlreadyExistException ex) { + throw ex; + } catch (Exception e) { + String msg = "Error occurred in creating group '" + groups.getName() + "'"; + log.error(msg, e); + throw new GroupManagementException(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); + } + + if (log.isDebugEnabled()) { + log.debug("DeviceGroup added: " + groups.getName()); + } + } + /** * {@inheritDoc} */ diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql index d22d8f312fc..4f83fa5b5ed 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/test/resources/sql/h2.sql @@ -16,6 +16,7 @@ CREATE TABLE IF NOT EXISTS DM_GROUP ( OWNER VARCHAR(45) DEFAULT NULL, PARENT_PATH VARCHAR(255) DEFAULT NULL, TENANT_ID INTEGER DEFAULT 0, + PARENT_GROUP_ID INTEGER DEFAULT 0, PRIMARY KEY (ID) ); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/pom.xml index d3196bf7e20..3ac62b95001 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.analytics.data.publisher/pom.xml @@ -47,7 +47,7 @@ testng - org. wso2.tomcat + org.wso2.tomcat tomcat diff --git a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql index defd753f334..f5a25e36a21 100644 --- a/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql +++ b/components/policy-mgt/io.entgra.device.mgt.core.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql @@ -20,6 +20,7 @@ CREATE TABLE IF NOT EXISTS DM_GROUP ( OWNER VARCHAR(45) DEFAULT NULL, PARENT_PATH VARCHAR(255) DEFAULT NULL, TENANT_ID INTEGER DEFAULT 0, + PARENT_GROUP_ID INTEGER DEFAULT 0, PRIMARY KEY (ID) ); diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index ad8d4e639e7..20786cfe2d4 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -25,6 +25,7 @@ CREATE TABLE IF NOT EXISTS DM_GROUP ( OWNER VARCHAR(255) DEFAULT NULL, PARENT_PATH VARCHAR(255) DEFAULT NULL, TENANT_ID INTEGER DEFAULT 0, + PARENT_GROUP_ID INTEGER DEFAULT 0, PRIMARY KEY (ID) ); diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index c162d41e060..5b6b3b93f14 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -41,6 +41,7 @@ IF NOT EXISTS(SELECT * OWNER VARCHAR(255) DEFAULT NULL, PARENT_PATH VARCHAR(255) DEFAULT NULL, TENANT_ID INTEGER DEFAULT 0, + PARENT_GROUP_ID INTEGER DEFAULT 0, PRIMARY KEY (ID) ); diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index 6dcc7bec443..86da197b12f 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -29,6 +29,7 @@ CREATE TABLE IF NOT EXISTS DM_GROUP ( OWNER VARCHAR(255) DEFAULT NULL, PARENT_PATH VARCHAR(255) DEFAULT NULL, TENANT_ID INTEGER DEFAULT 0, + PARENT_GROUP_ID INTEGER DEFAULT 0, PRIMARY KEY (ID) ) ENGINE = InnoDB; diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql index a6f87c9cb63..6cc55f7e741 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -52,6 +52,7 @@ CREATE TABLE DM_GROUP ( OWNER VARCHAR2(255) DEFAULT NULL, PARENT_PATH VARCHAR2(255) DEFAULT NULL, TENANT_ID NUMBER(10) DEFAULT 0, + PARENT_GROUP_ID NUMBER(10) DEFAULT 0, CONSTRAINT PK_DM_GROUP PRIMARY KEY (ID) ) / diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql index 8c601111b74..b1fc128cf91 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -23,6 +23,7 @@ CREATE TABLE IF NOT EXISTS DM_GROUP ( OWNER VARCHAR(45) DEFAULT NULL, PARENT_PATH VARCHAR(255) DEFAULT NULL, TENANT_ID INTEGER DEFAULT 0, + PARENT_GROUP_ID INTEGER DEFAULT 0, PRIMARY KEY (ID) ) ; From 67c529b0fae4c51142771dba1debb7eae074d29a Mon Sep 17 00:00:00 2001 From: ThilinaPremachandra Date: Wed, 14 Jun 2023 17:16:38 +0530 Subject: [PATCH 3/9] fixes line issues --- .../group/mgt/DeviceGroupRoleWrapper.java | 20 ++-- .../core/device/mgt/core/dao/GroupDAO.java | 92 +++++++++---------- 2 files changed, 52 insertions(+), 60 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/group/mgt/DeviceGroupRoleWrapper.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/group/mgt/DeviceGroupRoleWrapper.java index 132730e9158..768fe55524c 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/group/mgt/DeviceGroupRoleWrapper.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/group/mgt/DeviceGroupRoleWrapper.java @@ -37,12 +37,10 @@ public class DeviceGroupRoleWrapper implements Serializable { @ApiModelProperty(name = "id", value = "ID of the device group in the device group information database.") private int id; - @ApiModelProperty(name = "description", value = "The device group description that can be set on the device group by the user.", - required = true) + @ApiModelProperty(name = "description", value = "The device group description that can be set on the device group by the user.", required = true) private String description; - @ApiModelProperty(name = "name", value = "The device group name that can be set on the device group by the user.", - required = true) + @ApiModelProperty(name = "name", value = "The device group name that can be set on the device group by the user.", required = true) private String name; private String owner; @@ -62,10 +60,8 @@ public class DeviceGroupRoleWrapper implements Serializable { @ApiModelProperty(name = "userRoles", value = "User roles") private List userRoles; -// @ApiModelProperty(name = "DeviceGroupRoleWrapper", value = "Device group role wrapper") -// private List DeviceGroupRoleWrapper; - - public DeviceGroupRoleWrapper() {} + public DeviceGroupRoleWrapper() { + } public DeviceGroupRoleWrapper(String name) { this.name = name; @@ -79,7 +75,7 @@ public class DeviceGroupRoleWrapper implements Serializable { this.status = status; } - private Map groupProperties; + private Map groupProperties; public int getGroupId() { return id; @@ -113,11 +109,11 @@ public class DeviceGroupRoleWrapper implements Serializable { this.owner = owner; } - public Map getGroupProperties() { + public Map getGroupProperties() { return groupProperties; } - public void setGroupProperties(Map groupProperties) { + public void setGroupProperties(Map groupProperties) { this.groupProperties = groupProperties; } @@ -152,4 +148,4 @@ public class DeviceGroupRoleWrapper implements Serializable { public void setUserRoles(List userRoles) { this.userRoles = userRoles; } -} +} \ No newline at end of file diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java index 55f1da1f0d0..b7ef873c0de 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java @@ -32,6 +32,7 @@ import java.util.Map; * This interface represents the key operations associated with persisting group related information. */ public interface GroupDAO { + /** * Add new Device Group. * @@ -42,21 +43,18 @@ public interface GroupDAO { */ int addGroup(DeviceGroup deviceGroup, int tenantId) throws GroupManagementDAOException; - /** * Add properties for device group. * Note that groupId parameter is considered seperately due to the groupId parameter passed with * device group Payload is ignored in the add/update logic instead the internal groupId reference is used. * - * @param groups to be added. - * @param tenantId of the group. + * @param groups to be added. + * @param tenantId of the group. * @return sql execution result. * @throws GroupManagementDAOException */ int addGroupWithRoles(DeviceGroupRoleWrapper groups, int tenantId) throws GroupManagementDAOException; - - /** * Add properties for device group. * Note that groupId parameter is considered seperately due to the groupId parameter passed with @@ -67,7 +65,6 @@ public interface GroupDAO { * @return sql execution result. * @throws GroupManagementDAOException */ - boolean addGroupProperties(DeviceGroup deviceGroup, int groupId, int tenantId) throws GroupManagementDAOException; /** @@ -75,12 +72,11 @@ public interface GroupDAO { * Note that groupId parameter is considered seperately due to the groupId parameter passed with * device group Payload is ignored in the add/update logic instead the internal groupId reference is used. * - * @param groups to be updated. - * @param tenantId of the group. + * @param groups to be updated. + * @param tenantId of the group. * @return sql execution result. * @throws GroupManagementDAOException */ - boolean addGroupPropertiesWithRoles(DeviceGroupRoleWrapper groups, int groupId, int tenantId) throws GroupManagementDAOException; /** @@ -98,8 +94,8 @@ public interface GroupDAO { /** * Remove properties for device group. * - * @param groupId to be deleted. - * @param tenantId of the group. + * @param groupId to be deleted. + * @param tenantId of the group. * @throws GroupManagementDAOException */ void deleteAllGroupProperties(int groupId, int tenantId) throws GroupManagementDAOException; @@ -116,19 +112,19 @@ public interface GroupDAO { /** * Retrives all properties stored against a group. * - * @param groupId to be deleted. - * @param tenantId of the group. + * @param groupId to be deleted. + * @param tenantId of the group. * @return sql execution result. * @throws GroupManagementDAOException */ - Map getAllGroupProperties(int groupId, int tenantId) throws GroupManagementDAOException; + Map getAllGroupProperties(int groupId, int tenantId) throws GroupManagementDAOException; /** * Update an existing Device Group. * * @param deviceGroup group to update. - * @param groupId of Device Group. - * @param tenantId of the group. + * @param groupId of Device Group. + * @param tenantId of the group. * @throws GroupManagementDAOException */ void updateGroup(DeviceGroup deviceGroup, int groupId, int tenantId) @@ -138,7 +134,7 @@ public interface GroupDAO { * Update existing Device Groups. * * @param deviceGroups groups to update. - * @param tenantId of the group. + * @param tenantId of the group. * @throws GroupManagementDAOException on error during updating of groups */ void updateGroups(List deviceGroups, int tenantId) throws GroupManagementDAOException; @@ -146,8 +142,8 @@ public interface GroupDAO { /** * Delete an existing Device Group. * - * @param groupId of Device Group. - * @param tenantId of the group. + * @param groupId of Device Group. + * @param tenantId of the group. * @throws GroupManagementDAOException */ void deleteGroup(int groupId, int tenantId) throws GroupManagementDAOException; @@ -156,7 +152,7 @@ public interface GroupDAO { * Delete mappings of Device Groups. * * @param groupIds of Device Groups. - * @param tenantId of the group. + * @param tenantId of the group. * @throws GroupManagementDAOException on error during deletion of mappings of groups */ void deleteGroupsMapping(List groupIds, int tenantId) throws GroupManagementDAOException; @@ -165,7 +161,7 @@ public interface GroupDAO { * Delete existing Device Groups. * * @param groupIds of Device Groups. - * @param tenantId of the group. + * @param tenantId of the group. * @throws GroupManagementDAOException on error during deletion of groups */ void deleteGroups(List groupIds, int tenantId) throws GroupManagementDAOException; @@ -173,8 +169,8 @@ public interface GroupDAO { /** * Get device group by id. * - * @param groupId of Device Group. - * @param tenantId of the group. + * @param groupId of Device Group. + * @param tenantId of the group. * @return Device Group in tenant with specified name. * @throws GroupManagementDAOException */ @@ -184,7 +180,7 @@ public interface GroupDAO { * Get children groups by parent path. * * @param parentPath of parent group. - * @param tenantId of the group. + * @param tenantId of the group. * @return {@link List} list of children device groups * @throws GroupManagementDAOException on error during retrieval of children groups */ @@ -193,7 +189,7 @@ public interface GroupDAO { /** * Get root groups. * - * @param tenantId of the group. + * @param tenantId of the group. * @return {@link List} list of root device groups * @throws GroupManagementDAOException on error during retrieval of root groups */ @@ -201,6 +197,7 @@ public interface GroupDAO { /** * Get the groups of device with device id provided + * * @param deviceId * @return groups which has the device. * @throws GroupManagementDAOException @@ -211,7 +208,7 @@ public interface GroupDAO { * Get paginated list of Device Groups in tenant. * * @param paginationRequest to filter results. - * @param tenantId of user's tenant. + * @param tenantId of user's tenant. * @return List of all Device Groups in tenant. * @throws GroupManagementDAOException */ @@ -251,7 +248,7 @@ public interface GroupDAO { * Get paginated count of Device Groups in tenant. * * @param paginationRequest to filter results. - * @param tenantId of user's tenant. + * @param tenantId of user's tenant. * @return List of all Device Groups in tenant. * @throws GroupManagementDAOException */ @@ -261,7 +258,7 @@ public interface GroupDAO { * Check group already existed with given name. * * @param groupName of the Device Group. - * @param tenantId of user's tenant. + * @param tenantId of user's tenant. * @return existence of group with name * @throws GroupManagementDAOException */ @@ -270,7 +267,7 @@ public interface GroupDAO { /** * Add device to a given Device Group. * - * @param groupId of Device Group. + * @param groupId of Device Group. * @param deviceId of the device. * @param tenantId of user's tenant. * @throws GroupManagementDAOException @@ -280,7 +277,7 @@ public interface GroupDAO { /** * Remove device from the Device Group. * - * @param groupId of Device Group. + * @param groupId of Device Group. * @param deviceId of the device. * @param tenantId of user's tenant. * @throws GroupManagementDAOException @@ -290,7 +287,7 @@ public interface GroupDAO { /** * Check device is belonging to a Device Group. * - * @param groupId of Device Group. + * @param groupId of Device Group. * @param deviceId of the device. * @param tenantId of user's tenant. * @throws GroupManagementDAOException @@ -301,7 +298,7 @@ public interface GroupDAO { /** * Get count of devices in a Device Group. * - * @param groupId of Device Group. + * @param groupId of Device Group. * @param tenantId of user's tenant. * @return device count. * @throws GroupManagementDAOException @@ -311,10 +308,10 @@ public interface GroupDAO { /** * Get paginated result of devices of a given tenant and device group. * - * @param groupId of Device Group. + * @param groupId of Device Group. * @param startIndex for pagination. - * @param rowCount for pagination. - * @param tenantId of user's tenant. + * @param rowCount for pagination. + * @param tenantId of user's tenant. * @return list of device in group * @throws GroupManagementDAOException */ @@ -324,9 +321,9 @@ public interface GroupDAO { /** * Get All the devices that are in one of the given device status and belongs to given group * - * @param groupName Group name + * @param groupName Group name * @param deviceStatuses Device Statuses - * @param tenantId Tenant Id + * @param tenantId Tenant Id * @return List of devices * @throws GroupManagementDAOException if error occurred while retreving list of devices that are in one of the * given device status and belongs to the given group @@ -389,8 +386,8 @@ public interface GroupDAO { /** * Get count of all device groups which shared with a user role. * - * @param roles of the group. - * @param tenantId of user's tenant. + * @param roles of the group. + * @param tenantId of user's tenant. * @param parentPath of the group. * @return count of device groups. * @throws GroupManagementDAOException @@ -420,27 +417,26 @@ public interface GroupDAO { /** * Get count of device groups which owned by user. * - * @param username of the owner. - * @param tenantId of user's tenant. + * @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, String parentPath) throws GroupManagementDAOException; - /** - * Get device Ids of devices which are assigned to groups. + components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java * Get device Ids of devices which are assigned to groups. * * @param paginationRequest Request object with offset and limit. - * @param groupNames default group names that should be omitted when checking the device - * whether they have been assigned to groups - * @throws GroupManagementDAOException Might occur while retrieving information of group - * unassigned devices + * @param groupNames default group names that should be omitted when checking the device + * whether they have been assigned to groups * @return details of devices that are unassigned to groups. + * @throws GroupManagementDAOException Might occur while retrieving information of group + * unassigned devices */ List getGroupUnassignedDevices(PaginationRequest paginationRequest, List groupNames) throws GroupManagementDAOException; -} +} \ No newline at end of file From bb507173703f03f8565676ff18323e43aba6f506 Mon Sep 17 00:00:00 2001 From: ThilinaPremachandra Date: Wed, 14 Jun 2023 17:45:27 +0530 Subject: [PATCH 4/9] remove unnecessary imports, fixes line issues --- .../jaxrs/service/impl/GroupManagementServiceImpl.java | 4 ---- .../device/mgt/core/dao/impl/AbstractGroupDAOImpl.java | 4 ---- .../mgt/core/service/GroupManagementProviderService.java | 5 ++--- .../core/service/GroupManagementProviderServiceImpl.java | 8 ++------ 4 files changed, 4 insertions(+), 17 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImpl.java index b68bbd0f3a4..ef9928dc392 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImpl.java @@ -49,9 +49,6 @@ import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.DeviceMgtAPIUtils; import io.entgra.device.mgt.core.policy.mgt.common.PolicyAdministratorPoint; import io.entgra.device.mgt.core.policy.mgt.common.PolicyManagementException; -import javax.naming.InitialContext; -import javax.naming.NamingException; -import javax.transaction.UserTransaction; import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.POST; @@ -467,5 +464,4 @@ public class GroupManagementServiceImpl implements GroupManagementService { return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build(); } } - } \ No newline at end of file diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java index 7331ab6ad5f..5f29424336a 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java @@ -197,8 +197,6 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { if (hasStatus) { stmt.setString(7, deviceGroup.getStatus()); } - - stmt.executeUpdate(); rs = stmt.getGeneratedKeys(); if (rs.next()) { @@ -239,8 +237,6 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { if (hasStatus) { stmt.setString(7, groups.getStatus()); } - - stmt.executeUpdate(); rs = stmt.getGeneratedKeys(); if (rs.next()) { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java index f65244842c3..f52524db913 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java @@ -46,13 +46,12 @@ public interface GroupManagementProviderService { /** * Add new device group and create default role with default permissions. * - * @param groups to add + * @param groups to add * @param defaultRole of the deviceGroup * @param defaultPermissions of the default role * @throws GroupManagementException */ - void createGroupWithRoles(DeviceGroupRoleWrapper groups, String defaultRole, - String[] defaultPermissions )throws GroupManagementException, GroupAlreadyExistException, RoleDoesNotExistException; + void createGroupWithRoles(DeviceGroupRoleWrapper groups, String defaultRole, String[] defaultPermissions) throws GroupManagementException, GroupAlreadyExistException, RoleDoesNotExistException; /** * Update existing device group. diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java index 40d45b09638..2d0b82c1941 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -147,9 +147,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } } - - public void createGroupWithRoles(DeviceGroupRoleWrapper groups, String defaultRole, String[] defaultPermissions) - throws GroupManagementException, GroupAlreadyExistException { + public void createGroupWithRoles(DeviceGroupRoleWrapper groups, String defaultRole, String[] defaultPermissions) throws GroupManagementException, GroupAlreadyExistException { if (groups == null) { String msg = "Received incomplete data for createGroup"; log.error(msg); @@ -168,9 +166,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } else { DeviceGroup immediateParentGroup = groupDAO.getGroup(groups.getParentGroupId(), tenantId); if (immediateParentGroup == null) { - String msg = "Parent group with group ID '" + groups.getParentGroupId() - + "' does not exist. Hence creating of group '" + groups.getName() - + "' was not success"; + String msg = "Parent group with group ID '" + groups.getParentGroupId() + "' does not exist. Hence creating of group '" + groups.getName() + "' was not success"; log.error(msg); throw new GroupManagementException(msg); } From bc2bccac84c1971ebf5b560319054cbbcee60db8 Mon Sep 17 00:00:00 2001 From: ThilinaPremachandra Date: Wed, 14 Jun 2023 18:38:11 +0530 Subject: [PATCH 5/9] remove unnecessary code lines --- .../core/device/mgt/core/dao/GroupDAO.java | 85 +++++++++---------- 1 file changed, 42 insertions(+), 43 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java index b7ef873c0de..e1639e6262c 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java @@ -32,7 +32,6 @@ import java.util.Map; * This interface represents the key operations associated with persisting group related information. */ public interface GroupDAO { - /** * Add new Device Group. * @@ -48,8 +47,8 @@ public interface GroupDAO { * Note that groupId parameter is considered seperately due to the groupId parameter passed with * device group Payload is ignored in the add/update logic instead the internal groupId reference is used. * - * @param groups to be added. - * @param tenantId of the group. + * @param groups to be added. + * @param tenantId of the group. * @return sql execution result. * @throws GroupManagementDAOException */ @@ -72,8 +71,8 @@ public interface GroupDAO { * Note that groupId parameter is considered seperately due to the groupId parameter passed with * device group Payload is ignored in the add/update logic instead the internal groupId reference is used. * - * @param groups to be updated. - * @param tenantId of the group. + * @param groups to be updated. + * @param tenantId of the group. * @return sql execution result. * @throws GroupManagementDAOException */ @@ -94,8 +93,8 @@ public interface GroupDAO { /** * Remove properties for device group. * - * @param groupId to be deleted. - * @param tenantId of the group. + * @param groupId to be deleted. + * @param tenantId of the group. * @throws GroupManagementDAOException */ void deleteAllGroupProperties(int groupId, int tenantId) throws GroupManagementDAOException; @@ -112,19 +111,19 @@ public interface GroupDAO { /** * Retrives all properties stored against a group. * - * @param groupId to be deleted. - * @param tenantId of the group. + * @param groupId to be deleted. + * @param tenantId of the group. * @return sql execution result. * @throws GroupManagementDAOException */ - Map getAllGroupProperties(int groupId, int tenantId) throws GroupManagementDAOException; + Map getAllGroupProperties(int groupId, int tenantId) throws GroupManagementDAOException; /** * Update an existing Device Group. * * @param deviceGroup group to update. - * @param groupId of Device Group. - * @param tenantId of the group. + * @param groupId of Device Group. + * @param tenantId of the group. * @throws GroupManagementDAOException */ void updateGroup(DeviceGroup deviceGroup, int groupId, int tenantId) @@ -134,7 +133,7 @@ public interface GroupDAO { * Update existing Device Groups. * * @param deviceGroups groups to update. - * @param tenantId of the group. + * @param tenantId of the group. * @throws GroupManagementDAOException on error during updating of groups */ void updateGroups(List deviceGroups, int tenantId) throws GroupManagementDAOException; @@ -142,8 +141,8 @@ public interface GroupDAO { /** * Delete an existing Device Group. * - * @param groupId of Device Group. - * @param tenantId of the group. + * @param groupId of Device Group. + * @param tenantId of the group. * @throws GroupManagementDAOException */ void deleteGroup(int groupId, int tenantId) throws GroupManagementDAOException; @@ -152,7 +151,7 @@ public interface GroupDAO { * Delete mappings of Device Groups. * * @param groupIds of Device Groups. - * @param tenantId of the group. + * @param tenantId of the group. * @throws GroupManagementDAOException on error during deletion of mappings of groups */ void deleteGroupsMapping(List groupIds, int tenantId) throws GroupManagementDAOException; @@ -161,7 +160,7 @@ public interface GroupDAO { * Delete existing Device Groups. * * @param groupIds of Device Groups. - * @param tenantId of the group. + * @param tenantId of the group. * @throws GroupManagementDAOException on error during deletion of groups */ void deleteGroups(List groupIds, int tenantId) throws GroupManagementDAOException; @@ -169,8 +168,8 @@ public interface GroupDAO { /** * Get device group by id. * - * @param groupId of Device Group. - * @param tenantId of the group. + * @param groupId of Device Group. + * @param tenantId of the group. * @return Device Group in tenant with specified name. * @throws GroupManagementDAOException */ @@ -180,7 +179,7 @@ public interface GroupDAO { * Get children groups by parent path. * * @param parentPath of parent group. - * @param tenantId of the group. + * @param tenantId of the group. * @return {@link List} list of children device groups * @throws GroupManagementDAOException on error during retrieval of children groups */ @@ -189,7 +188,7 @@ public interface GroupDAO { /** * Get root groups. * - * @param tenantId of the group. + * @param tenantId of the group. * @return {@link List} list of root device groups * @throws GroupManagementDAOException on error during retrieval of root groups */ @@ -197,7 +196,6 @@ public interface GroupDAO { /** * Get the groups of device with device id provided - * * @param deviceId * @return groups which has the device. * @throws GroupManagementDAOException @@ -208,7 +206,7 @@ public interface GroupDAO { * Get paginated list of Device Groups in tenant. * * @param paginationRequest to filter results. - * @param tenantId of user's tenant. + * @param tenantId of user's tenant. * @return List of all Device Groups in tenant. * @throws GroupManagementDAOException */ @@ -248,7 +246,7 @@ public interface GroupDAO { * Get paginated count of Device Groups in tenant. * * @param paginationRequest to filter results. - * @param tenantId of user's tenant. + * @param tenantId of user's tenant. * @return List of all Device Groups in tenant. * @throws GroupManagementDAOException */ @@ -258,7 +256,7 @@ public interface GroupDAO { * Check group already existed with given name. * * @param groupName of the Device Group. - * @param tenantId of user's tenant. + * @param tenantId of user's tenant. * @return existence of group with name * @throws GroupManagementDAOException */ @@ -267,7 +265,7 @@ public interface GroupDAO { /** * Add device to a given Device Group. * - * @param groupId of Device Group. + * @param groupId of Device Group. * @param deviceId of the device. * @param tenantId of user's tenant. * @throws GroupManagementDAOException @@ -277,7 +275,7 @@ public interface GroupDAO { /** * Remove device from the Device Group. * - * @param groupId of Device Group. + * @param groupId of Device Group. * @param deviceId of the device. * @param tenantId of user's tenant. * @throws GroupManagementDAOException @@ -287,7 +285,7 @@ public interface GroupDAO { /** * Check device is belonging to a Device Group. * - * @param groupId of Device Group. + * @param groupId of Device Group. * @param deviceId of the device. * @param tenantId of user's tenant. * @throws GroupManagementDAOException @@ -298,7 +296,7 @@ public interface GroupDAO { /** * Get count of devices in a Device Group. * - * @param groupId of Device Group. + * @param groupId of Device Group. * @param tenantId of user's tenant. * @return device count. * @throws GroupManagementDAOException @@ -308,10 +306,10 @@ public interface GroupDAO { /** * Get paginated result of devices of a given tenant and device group. * - * @param groupId of Device Group. + * @param groupId of Device Group. * @param startIndex for pagination. - * @param rowCount for pagination. - * @param tenantId of user's tenant. + * @param rowCount for pagination. + * @param tenantId of user's tenant. * @return list of device in group * @throws GroupManagementDAOException */ @@ -321,9 +319,9 @@ public interface GroupDAO { /** * Get All the devices that are in one of the given device status and belongs to given group * - * @param groupName Group name + * @param groupName Group name * @param deviceStatuses Device Statuses - * @param tenantId Tenant Id + * @param tenantId Tenant Id * @return List of devices * @throws GroupManagementDAOException if error occurred while retreving list of devices that are in one of the * given device status and belongs to the given group @@ -386,8 +384,8 @@ public interface GroupDAO { /** * Get count of all device groups which shared with a user role. * - * @param roles of the group. - * @param tenantId of user's tenant. + * @param roles of the group. + * @param tenantId of user's tenant. * @param parentPath of the group. * @return count of device groups. * @throws GroupManagementDAOException @@ -417,22 +415,23 @@ public interface GroupDAO { /** * Get count of device groups which owned by user. * - * @param username of the owner. - * @param tenantId of user's tenant. + * @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, String parentPath) throws GroupManagementDAOException; + /** - components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/GroupDAO.java * Get device Ids of devices which are assigned to groups. + * Get device Ids of devices which are assigned to groups. * * @param paginationRequest Request object with offset and limit. - * @param groupNames default group names that should be omitted when checking the device - * whether they have been assigned to groups - * @return details of devices that are unassigned to groups. + * @param groupNames default group names that should be omitted when checking the device + * whether they have been assigned to groups * @throws GroupManagementDAOException Might occur while retrieving information of group - * unassigned devices + * unassigned devices + * @return details of devices that are unassigned to groups. */ List getGroupUnassignedDevices(PaginationRequest paginationRequest, From 00b107ee99d4493e8dc80e40776a7720ca02e89a Mon Sep 17 00:00:00 2001 From: ThilinaPremachandra Date: Thu, 15 Jun 2023 11:15:23 +0530 Subject: [PATCH 6/9] remove unnecessary variable, change error msg --- .../jaxrs/service/impl/GroupManagementServiceImpl.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImpl.java index ef9928dc392..1c077c00a51 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/GroupManagementServiceImpl.java @@ -435,15 +435,15 @@ public class GroupManagementServiceImpl implements GroupManagementService { @Path("/roles/share") @Override public Response createGroupWithRoles(DeviceGroupRoleWrapper groups) { - String owner = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); if (groups == null) { return Response.status(Response.Status.BAD_REQUEST).build(); } - groups.setOwner(owner); + groups.setOwner(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername()); groups.setStatus(DeviceGroupConstants.GroupStatus.ACTIVE); try { DeviceMgtAPIUtils.getGroupManagementProviderService().createGroupWithRoles(groups, DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); - DeviceGroup group = DeviceMgtAPIUtils.getGroupManagementProviderService().getGroup(groups.getName(), owner.isEmpty()); + DeviceGroup group = DeviceMgtAPIUtils.getGroupManagementProviderService().getGroup(groups.getName(), + PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername().isEmpty()); if (group != null) { DeviceMgtAPIUtils.getGroupManagementProviderService().manageGroupSharing(group.getGroupId(), groups.getUserRoles()); return Response.status(Response.Status.CREATED).entity(group.getGroupId()).build(); @@ -453,7 +453,7 @@ public class GroupManagementServiceImpl implements GroupManagementService { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } catch (GroupManagementException e) { - String msg = "Error occurred while adding new group."; + String msg = "Error occurred while adding " + groups.getName() + " group"; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } catch (GroupAlreadyExistException e) { From e66c3dc6f62fc8e22c0100c19395426fe0562401 Mon Sep 17 00:00:00 2001 From: ThilinaPremachandra Date: Thu, 15 Jun 2023 14:29:10 +0530 Subject: [PATCH 7/9] add error log --- .../impl/admin/GroupManagementAdminServiceImpl.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java index 30546f214ab..4495a3fe9ca 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java @@ -152,21 +152,22 @@ public class GroupManagementAdminServiceImpl implements GroupManagementAdminServ @Path("/roles/share") @Override public Response createGroupWithRoles(DeviceGroupRoleWrapper group) { - String owner = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); if (group == null) { return Response.status(Response.Status.BAD_REQUEST).build(); } - group.setOwner(owner); + group.setOwner(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername()); group.setStatus(DeviceGroupConstants.GroupStatus.ACTIVE); try { DeviceMgtAPIUtils.getGroupManagementProviderService().createGroupWithRoles(group, DEFAULT_ADMIN_ROLE, DEFAULT_ADMIN_PERMISSIONS); DeviceMgtAPIUtils.getGroupManagementProviderService().manageGroupSharing(group.getGroupId(), group.getUserRoles()); return Response.status(Response.Status.CREATED).build(); } catch (GroupManagementException e) { - String msg = "Error occurred while adding new group."; + String msg = "Error occurred while adding " + group.getName() + " group"; + log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } catch (GroupAlreadyExistException e) { String msg = "Group already exists with name : " + group.getName() + "."; + log.warn(msg); return Response.status(Response.Status.CONFLICT).entity(msg).build(); } catch (RoleDoesNotExistException e) { return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build(); From 3722bfc761dc927deb9ee5d392765b85fe200e5d Mon Sep 17 00:00:00 2001 From: ThilinaPremachandra Date: Fri, 16 Jun 2023 01:58:42 +0530 Subject: [PATCH 8/9] add try with resources --- .../core/dao/impl/AbstractGroupDAOImpl.java | 68 +++++++++---------- .../impl/group/PostgreSQLGroupDAOImpl.java | 47 +++++++------ .../GroupManagementProviderService.java | 8 ++- .../GroupManagementProviderServiceImpl.java | 15 ++-- 4 files changed, 72 insertions(+), 66 deletions(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java index 5f29424336a..937b9752598 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java @@ -211,9 +211,8 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { } } + @Override public int addGroupWithRoles(DeviceGroupRoleWrapper groups, int tenantId) throws GroupManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs; int groupId = -1; boolean hasStatus = false; try { @@ -227,27 +226,29 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { + "VALUES (?, ?, ?, ?, ?, ?, ?)"; hasStatus = true; } - stmt = conn.prepareStatement(sql, new String[]{"ID"}); - stmt.setString(1, groups.getDescription()); - stmt.setString(2, groups.getName()); - stmt.setString(3, groups.getOwner()); - stmt.setInt(4, tenantId); - stmt.setString(5, groups.getParentPath()); - stmt.setInt(6, groups.getParentGroupId()); - if (hasStatus) { - stmt.setString(7, groups.getStatus()); - } - stmt.executeUpdate(); - rs = stmt.getGeneratedKeys(); - if (rs.next()) { - groupId = rs.getInt(1); + try (PreparedStatement stmt = conn.prepareStatement(sql, new String[]{"ID"})) { + stmt.setString(1, groups.getDescription()); + stmt.setString(2, groups.getName()); + stmt.setString(3, groups.getOwner()); + stmt.setInt(4, tenantId); + stmt.setString(5, groups.getParentPath()); + stmt.setInt(6, groups.getParentGroupId()); + if (hasStatus) { + stmt.setString(7, groups.getStatus()); + } + stmt.executeUpdate(); + try (ResultSet rs = stmt.getGeneratedKeys();) { + if (rs.next()) { + groupId = rs.getInt(1); + } + return groupId; + } } - return groupId; } catch (SQLException e) { - throw new GroupManagementDAOException("Error occurred while adding deviceGroup '" + - groups.getName() + "'", e); - } finally { - GroupManagementDAOUtil.cleanupResources(stmt, null); + String msg = "Error occurred while adding deviceGroup '" + + groups.getName() + "'"; + log.error(msg); + throw new GroupManagementDAOException(msg, e); } } @@ -282,27 +283,26 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO { public boolean addGroupPropertiesWithRoles(DeviceGroupRoleWrapper groups, int groupId, int tenantId) throws GroupManagementDAOException { boolean status; - PreparedStatement stmt = null; try { Connection conn = GroupManagementDAOFactory.getConnection(); - stmt = conn.prepareStatement( + try (PreparedStatement stmt = conn.prepareStatement( "INSERT INTO GROUP_PROPERTIES(GROUP_ID, PROPERTY_NAME, " + - "PROPERTY_VALUE, TENANT_ID) VALUES (?, ?, ?, ?)"); - for (Map.Entry entry : groups.getGroupProperties().entrySet()) { - stmt.setInt(1, groupId); - stmt.setString(2, entry.getKey()); - stmt.setString(3, entry.getValue()); - stmt.setInt(4, tenantId); - stmt.addBatch(); + "PROPERTY_VALUE, TENANT_ID) VALUES (?, ?, ?, ?)")) { + for (Map.Entry entry : groups.getGroupProperties().entrySet()) { + stmt.setInt(1, groupId); + stmt.setString(2, entry.getKey()); + stmt.setString(3, entry.getValue()); + stmt.setInt(4, tenantId); + stmt.addBatch(); + } + stmt.executeBatch(); + status = true; } - stmt.executeBatch(); - status = true; } catch (SQLException e) { String msg = "Error occurred while adding properties for group '" + groups.getName() + "' values : " + groups.getGroupProperties(); + log.error(msg); throw new GroupManagementDAOException(msg, e); - } finally { - GroupManagementDAOUtil.cleanupResources(stmt, null); } return status; } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/group/PostgreSQLGroupDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/group/PostgreSQLGroupDAOImpl.java index 00b581c1a41..67994b51a67 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/group/PostgreSQLGroupDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/group/PostgreSQLGroupDAOImpl.java @@ -19,6 +19,8 @@ package io.entgra.device.mgt.core.device.mgt.core.dao.impl.group; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupRoleWrapper; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.solr.common.StringUtils; import io.entgra.device.mgt.core.device.mgt.common.Device; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroup; @@ -40,6 +42,8 @@ import java.util.List; */ public class PostgreSQLGroupDAOImpl extends AbstractGroupDAOImpl { + private static final Log log = LogFactory.getLog(PostgreSQLGroupDAOImpl.class); + @Override public int addGroup(DeviceGroup deviceGroup, int tenantId) throws GroupManagementDAOException { PreparedStatement stmt = null; @@ -80,15 +84,14 @@ public class PostgreSQLGroupDAOImpl extends AbstractGroupDAOImpl { } } + @Override public int addGroupWithRoles(DeviceGroupRoleWrapper groups, int tenantId) throws GroupManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs; int groupId = -1; boolean hasStatus = false; try { Connection conn = GroupManagementDAOFactory.getConnection(); String sql; - if(StringUtils.isEmpty(groups.getStatus())) { + if (StringUtils.isEmpty(groups.getStatus())) { sql = "INSERT INTO DM_GROUP(DESCRIPTION, GROUP_NAME, OWNER, TENANT_ID, PARENT_PATH) " + "VALUES (?, ?, ?, ?) RETURNING ID"; } else { @@ -96,26 +99,28 @@ public class PostgreSQLGroupDAOImpl extends AbstractGroupDAOImpl { "VALUES (?, ?, ?, ?, ?) RETURNING ID"; hasStatus = true; } - stmt = conn.prepareStatement(sql); - stmt.setString(1, groups.getDescription()); - stmt.setString(2, groups.getName()); - stmt.setString(3, groups.getOwner()); - stmt.setInt(4, tenantId); - stmt.setString(5, groups.getParentPath()); - if(hasStatus) { - stmt.setString(6, groups.getStatus()); - } - stmt.execute(); - rs = stmt.getGeneratedKeys(); - if (rs.next()) { - groupId = rs.getInt(1); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, groups.getDescription()); + stmt.setString(2, groups.getName()); + stmt.setString(3, groups.getOwner()); + stmt.setInt(4, tenantId); + stmt.setString(5, groups.getParentPath()); + if (hasStatus) { + stmt.setString(6, groups.getStatus()); + } + stmt.execute(); + try (ResultSet rs = stmt.getGeneratedKeys()) { + if (rs.next()) { + groupId = rs.getInt(1); + } + return groupId; + } } - return groupId; } catch (SQLException e) { - throw new GroupManagementDAOException("Error occurred while adding deviceGroup '" + - groups.getName() + "'", e); - } finally { - GroupManagementDAOUtil.cleanupResources(stmt, null); + String msg = "Error occurred while adding deviceGroup '" + + groups.getName() + "'"; + log.error(msg); + throw new GroupManagementDAOException(msg, e); } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java index f52524db913..153b97b5bfa 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderService.java @@ -23,7 +23,13 @@ import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceNotFoundException; import io.entgra.device.mgt.core.device.mgt.common.GroupPaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.PaginationResult; -import io.entgra.device.mgt.core.device.mgt.common.group.mgt.*; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroup; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupRoleWrapper; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceTypesOfGroups; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupAlreadyExistException; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupNotExistException; +import io.entgra.device.mgt.core.device.mgt.common.group.mgt.RoleDoesNotExistException; import java.util.List; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java index 2d0b82c1941..9540577805b 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -35,6 +35,7 @@ import io.entgra.device.mgt.core.device.mgt.core.dao.GroupManagementDAOFactory; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.netbeans.lib.cvsclient.commandLine.command.status; import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -147,7 +148,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } } - public void createGroupWithRoles(DeviceGroupRoleWrapper groups, String defaultRole, String[] defaultPermissions) throws GroupManagementException, GroupAlreadyExistException { + public void createGroupWithRoles(DeviceGroupRoleWrapper groups, String defaultRole, String[] defaultPermissions) throws GroupManagementException { if (groups == null) { String msg = "Received incomplete data for createGroup"; log.error(msg); @@ -179,23 +180,17 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } GroupManagementDAOFactory.commitTransaction(); } else { - throw new GroupAlreadyExistException("Group exist with name " + groups.getName()); + throw new GroupManagementException("Group exist with name " + groups.getName()); } - } catch (GroupManagementDAOException e) { + } catch (GroupManagementDAOException | GroupManagementException e) { GroupManagementDAOFactory.rollbackTransaction(); - String msg = "Error occurred while adding deviceGroup '" + groups.getName() + "' to database."; + String msg = e.getMessage(); log.error(msg, e); throw new GroupManagementException(msg, e); } catch (TransactionManagementException e) { String msg = "Error occurred while initiating transaction."; log.error(msg, e); throw new GroupManagementException(msg, e); - } catch (GroupAlreadyExistException ex) { - throw ex; - } catch (Exception e) { - String msg = "Error occurred in creating group '" + groups.getName() + "'"; - log.error(msg, e); - throw new GroupManagementException(msg, e); } finally { GroupManagementDAOFactory.closeConnection(); } From 647d4fdb78e7b315ab6a6172a15e09833e330168 Mon Sep 17 00:00:00 2001 From: ThilinaPremachandra Date: Fri, 16 Jun 2023 09:48:52 +0530 Subject: [PATCH 9/9] add rollback --- .../mgt/core/service/GroupManagementProviderServiceImpl.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java index 9540577805b..be1ad0d545e 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -167,6 +167,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } else { DeviceGroup immediateParentGroup = groupDAO.getGroup(groups.getParentGroupId(), tenantId); if (immediateParentGroup == null) { + GroupManagementDAOFactory.rollbackTransaction(); String msg = "Parent group with group ID '" + groups.getParentGroupId() + "' does not exist. Hence creating of group '" + groups.getName() + "' was not success"; log.error(msg); throw new GroupManagementException(msg); @@ -182,7 +183,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } else { throw new GroupManagementException("Group exist with name " + groups.getName()); } - } catch (GroupManagementDAOException | GroupManagementException e) { + } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); String msg = e.getMessage(); log.error(msg, e);