From 61ac576d6cad3d8c5462a3c10b40d56f743b947d Mon Sep 17 00:00:00 2001 From: sinthuja Date: Fri, 1 Sep 2017 15:34:34 +0530 Subject: [PATCH] Fixing https://github.com/wso2/product-iots/issues/1376. --- .../impl/GroupManagementServiceImpl.java | 7 +-- .../group/mgt/GroupNotExistException.java | 55 +++++++++++++++++++ .../GroupManagementProviderService.java | 7 +-- .../GroupManagementProviderServiceImpl.java | 22 ++++---- 4 files changed, 69 insertions(+), 22 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/GroupNotExistException.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java index 8bab08463b3..d0619e65205 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java @@ -29,10 +29,7 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceNotFoundException; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; -import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; -import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; -import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; -import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException; +import org.wso2.carbon.device.mgt.common.group.mgt.*; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; @@ -144,7 +141,7 @@ public class GroupManagementServiceImpl implements GroupManagementService { 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) { + } catch (GroupNotExistException e) { String msg = "There is another group already exists with name '" + deviceGroup.getName() + "'."; log.warn(msg); return Response.status(Response.Status.CONFLICT).entity(msg).build(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/GroupNotExistException.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/GroupNotExistException.java new file mode 100644 index 00000000000..a4abc968537 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/GroupNotExistException.java @@ -0,0 +1,55 @@ +/* +* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* WSO2 Inc. licenses this file to you under the Apache License, +* Version 2.0 (the "License"); you may not use this file except +* in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +* +*/ +package org.wso2.carbon.device.mgt.common.group.mgt; + +public class GroupNotExistException extends Exception{ + + private String errorMessage; + + public GroupNotExistException(String msg, Exception nestedEx) { + super(msg, nestedEx); + setErrorMessage(msg); + } + + public GroupNotExistException(String message, Throwable cause) { + super(message, cause); + setErrorMessage(message); + } + + public GroupNotExistException(String msg) { + super(msg); + setErrorMessage(msg); + } + + public GroupNotExistException() { + super(); + } + + public GroupNotExistException(Throwable cause) { + super(cause); + } + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java index 054a923c437..01368523a83 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderService.java @@ -23,10 +23,7 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceNotFoundException; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; -import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; -import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; -import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; -import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException; +import org.wso2.carbon.device.mgt.common.group.mgt.*; import java.util.List; @@ -53,7 +50,7 @@ public interface GroupManagementProviderService { * @param groupId of the group. * @throws GroupManagementException */ - void updateGroup(DeviceGroup deviceGroup, int groupId) throws GroupManagementException, GroupAlreadyExistException; + void updateGroup(DeviceGroup deviceGroup, int groupId) throws GroupManagementException, GroupNotExistException; /** * Delete existing device group. 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 b5a2a4f635e..484d22def90 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 @@ -73,9 +73,6 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid if (log.isDebugEnabled()) { log.debug("Creating group '" + deviceGroup.getName() + "'"); } - if (deviceGroup == null) { - throw new GroupManagementException("DeviceGroup cannot be null.", new NullPointerException()); - } int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); try { GroupManagementDAOFactory.beginTransaction(); @@ -95,6 +92,8 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid 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 '" + deviceGroup.getName() + "'"; log.error(msg, e); @@ -113,7 +112,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid */ @Override public void updateGroup(DeviceGroup deviceGroup, int groupId) - throws GroupManagementException, GroupAlreadyExistException { + throws GroupManagementException, GroupNotExistException { if (deviceGroup == null) { String msg = "Received incomplete data for updateGroup"; log.error(msg); @@ -122,30 +121,29 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid if (log.isDebugEnabled()) { log.debug("update group '" + deviceGroup.getName() + "'"); } - if (deviceGroup == null) { - throw new GroupManagementException("DeviceGroup cannot be null.", new NullPointerException()); - } try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.beginTransaction(); - DeviceGroup existingGroup = this.groupDAO.getGroup(deviceGroup.getName(), tenantId); - if (existingGroup == null || existingGroup.getGroupId() == groupId) { + DeviceGroup existingGroup = this.groupDAO.getGroup(groupId, tenantId); + if (existingGroup != null) { this.groupDAO.updateGroup(deviceGroup, groupId, tenantId); GroupManagementDAOFactory.commitTransaction(); } else { - throw new GroupAlreadyExistException("Group exist with name " + deviceGroup.getName()); + throw new GroupNotExistException("Group with ID - '" + groupId + "' doesn't exists!"); } } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); - String msg = "Error occurred while modifying deviceGroup '" + deviceGroup.getName() + "'."; + String msg = "Error occurred while modifying device group with ID - '" + groupId + "'."; 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 (GroupNotExistException ex) { + throw ex; } catch (Exception e) { - String msg = "Error occurred in updating group '" + deviceGroup.getName() + "'"; + String msg = "Error occurred in updating the device group with ID - '" + groupId + "'."; log.error(msg, e); throw new GroupManagementException(msg, e); } finally {