From 489aaabc445087abcc9d972d759f68332dfe05de Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Fri, 16 Jul 2021 00:13:30 +0530 Subject: [PATCH] Move slash to a constant --- .../group/mgt/DeviceGroupConstants.java | 4 ++++ .../GroupManagementProviderServiceImpl.java | 21 ++++++++++--------- .../mgt/core/util/DeviceManagerUtil.java | 6 ++++-- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroupConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroupConstants.java index 039219bfe8..b1a185d98f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroupConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroupConstants.java @@ -84,4 +84,8 @@ public class DeviceGroupConstants { public static final String[] DEFAULT_VIEW_EVENTS_PERMISSIONS = {"/permission/device-mgt/user/groups/device_events"}; } + + public static final class HierarchicalGroup { + public static final String SEPERATOR = "/"; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java index 808a355cbe..6a678c1fca 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -113,7 +113,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid DeviceGroup existingGroup = this.groupDAO.getGroup(deviceGroup.getName(), tenantId); if (existingGroup == null) { if (deviceGroup.getParentGroupId() == 0) { - deviceGroup.setParentPath("/"); + deviceGroup.setParentPath(DeviceGroupConstants.HierarchicalGroup.SEPERATOR); } else { DeviceGroup immediateParentGroup = groupDAO.getGroup(deviceGroup.getParentGroupId(), tenantId); if (immediateParentGroup == null) { @@ -178,10 +178,10 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid DeviceGroup existingGroup = this.groupDAO.getGroup(groupId, tenantId); if (existingGroup != null) { List groupsToUpdate = new ArrayList<>(); - String immediateParentID = StringUtils.substringAfterLast(existingGroup.getParentPath(), "/"); + String immediateParentID = StringUtils.substringAfterLast(existingGroup.getParentPath(), DeviceGroupConstants.HierarchicalGroup.SEPERATOR); String parentPath = ""; if (deviceGroup.getParentGroupId() == 0) { - deviceGroup.setParentPath("/"); + deviceGroup.setParentPath(DeviceGroupConstants.HierarchicalGroup.SEPERATOR); } else { DeviceGroup immediateParentGroup = groupDAO.getGroup(deviceGroup.getParentGroupId(), tenantId); if (immediateParentGroup == null) { @@ -197,7 +197,8 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid deviceGroup.setGroupId(groupId); groupsToUpdate.add(deviceGroup); if (StringUtils.isNotBlank(immediateParentID)) { - List childrenGroups = groupDAO.getChildrenGroups(DeviceManagerUtil.createParentPath(existingGroup), tenantId); + List childrenGroups = groupDAO.getChildrenGroups( + DeviceManagerUtil.createParentPath(existingGroup), tenantId); for (DeviceGroup childrenGroup : childrenGroups) { childrenGroup.setParentPath(childrenGroup.getParentPath() .replace(existingGroup.getParentPath(), parentPath)); @@ -258,9 +259,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } else { for (DeviceGroup childrenGroup : childrenGroups) { String newParentPath = childrenGroup.getParentPath() - .replace("/" + deviceGroup.getGroupId(), ""); + .replace(DeviceGroupConstants.HierarchicalGroup.SEPERATOR + deviceGroup.getGroupId(), ""); if (StringUtils.isEmpty(newParentPath)) { - newParentPath = "/"; + newParentPath = DeviceGroupConstants.HierarchicalGroup.SEPERATOR; } childrenGroup.setParentPath(newParentPath); } @@ -437,7 +438,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.openConnection(); - request.setParentPath("/"); + request.setParentPath(DeviceGroupConstants.HierarchicalGroup.SEPERATOR); rootGroups = this.groupDAO.getGroups(request, tenantId); for (DeviceGroup rootGroup : rootGroups) { if (requireGroupProps) { @@ -480,7 +481,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid List rootGroups; try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - request.setParentPath("/"); + request.setParentPath(DeviceGroupConstants.HierarchicalGroup.SEPERATOR); if (StringUtils.isBlank(username)) { GroupManagementDAOFactory.openConnection(); rootGroups = groupDAO.getGroups(request, tenantId); @@ -616,7 +617,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.openConnection(); - request.setParentPath("/"); + request.setParentPath(DeviceGroupConstants.HierarchicalGroup.SEPERATOR); rootGroups = this.groupDAO.getGroups(request, allDeviceGroupIdsOfUser, tenantId); for (DeviceGroup rootGroup : rootGroups) { if (requireGroupProps) { @@ -1306,7 +1307,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid while (iterator.hasNext()) { DeviceGroup childGroup = iterator.next(); int immediateParentID = Integer.parseInt(StringUtils.substringAfterLast( - childGroup.getParentPath(), "/")); + childGroup.getParentPath(), DeviceGroupConstants.HierarchicalGroup.SEPERATOR)); if (immediateParentID == parentGroup.getGroupId()) { if (requireGroupProps) { populateGroupProperties(childGroup, tenantId); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index 334c9b848c..36315a1315 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -70,6 +70,7 @@ import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException; import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException; import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; +import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.common.exceptions.MetadataManagementException; import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementException; @@ -1169,10 +1170,11 @@ public final class DeviceManagerUtil { * @return created parent path */ public static String createParentPath(DeviceGroup deviceGroup) { - if ("/".equals(deviceGroup.getParentPath())) { + if (DeviceGroupConstants.HierarchicalGroup.SEPERATOR.equals(deviceGroup.getParentPath())) { return deviceGroup.getParentPath() + deviceGroup.getGroupId(); } else { - return deviceGroup.getParentPath() + "/" + deviceGroup.getGroupId(); + return deviceGroup.getParentPath() + DeviceGroupConstants.HierarchicalGroup.SEPERATOR + + deviceGroup.getGroupId(); } } }