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 8bab08463b..d0619e6520 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 0000000000..a4abc96853 --- /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 054a923c43..01368523a8 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 b5a2a4f635..484d22def9 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 {