From 4a239887cb717c996b3506ad49c2668adf586ddb Mon Sep 17 00:00:00 2001 From: ThilinaPremachandra Date: Fri, 16 Jun 2023 01:58:42 +0530 Subject: [PATCH] add try with resources --- .../core/dao/impl/AbstractGroupDAOImpl.java | 70 +++++++++---------- .../impl/group/PostgreSQLGroupDAOImpl.java | 46 ++++++------ .../GroupManagementProviderService.java | 7 ++ .../GroupManagementProviderServiceImpl.java | 15 ++-- 4 files changed, 71 insertions(+), 67 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 db38b8eb90..37a10f9c23 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 @@ -208,9 +208,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 { @@ -224,29 +223,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); } } @@ -281,27 +280,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 eec480abd7..6a786a0bfd 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 io.entgra.device.mgt.core.device.mgt.common.Device; import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroup; import io.entgra.device.mgt.core.device.mgt.core.dao.GroupManagementDAOException; @@ -40,6 +42,7 @@ 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 +83,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 +98,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()); + 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; + } } - 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); + 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 ab4e7aa926..b263ea1234 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 @@ -24,6 +24,13 @@ 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.DeviceNotFoundException; 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 44dea24fe4..2dd39081a3 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 @@ -49,6 +49,7 @@ import io.entgra.device.mgt.core.device.mgt.core.util.DeviceManagerUtil; 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(); }