From a45f4712ba585b85aafc90de778098f69d736f61 Mon Sep 17 00:00:00 2001 From: isuri Date: Tue, 19 Dec 2023 10:54:52 +0530 Subject: [PATCH] deviceOrg: refactor with best practices --- .../api/DeviceOrganizationMgtService.java | 3 ++- .../api/DeviceOrganizationMgtServiceImpl.java | 4 ++-- .../api/beans/GsonMessageBodyHandler.java | 6 +++--- .../api/beans/ValidationInterceptor.java | 4 +++- .../api/util/DeviceOrgConstants.java | 6 ++++++ .../dao/impl/DeviceOrganizationDAOImpl.java | 21 ++++++++++++------- 6 files changed, 30 insertions(+), 14 deletions(-) create mode 100644 components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/util/DeviceOrgConstants.java diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtService.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtService.java index 7f430c9d26..f02486432e 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtService.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtService.java @@ -17,6 +17,7 @@ */ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api.util.DeviceOrgConstants; import io.entgra.device.mgt.core.apimgt.annotations.Scope; import io.entgra.device.mgt.core.apimgt.annotations.Scopes; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api.beans.ErrorResponse; @@ -105,7 +106,7 @@ import javax.ws.rs.core.Response; ) public interface DeviceOrganizationMgtService { - String SCOPE = "scope"; + String SCOPE = DeviceOrgConstants.SCOPE; /** * Adds a new device organization. diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtServiceImpl.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtServiceImpl.java index cf690bab25..d47672944b 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtServiceImpl.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtServiceImpl.java @@ -49,7 +49,7 @@ import java.util.List; public class DeviceOrganizationMgtServiceImpl implements DeviceOrganizationMgtService { private static final Log log = LogFactory.getLog(DeviceOrganizationMgtServiceImpl.class); - Gson gson = new Gson(); + private static final Gson gson = new Gson(); @POST @Override @@ -72,7 +72,7 @@ public class DeviceOrganizationMgtServiceImpl implements DeviceOrganizationMgtSe boolean resp = deviceOrganizationService.addDeviceOrganization(deviceOrganizationRequest); SuccessResponse response = new SuccessResponse(); response.setSuccess(resp); - return Response.status(Response.Status.OK).entity(response).build(); + return Response.status(Response.Status.CREATED).entity(response).build(); } catch (DeviceOrganizationMgtPluginException e) { String errorMessage = "device organization failed to be created"; log.error(errorMessage); diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/beans/GsonMessageBodyHandler.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/beans/GsonMessageBodyHandler.java index 197c0b34e8..cd0ee361e7 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/beans/GsonMessageBodyHandler.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/beans/GsonMessageBodyHandler.java @@ -36,6 +36,7 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.lang.annotation.Annotation; import java.lang.reflect.Type; +import java.nio.charset.StandardCharsets; import static javax.ws.rs.core.MediaType.APPLICATION_JSON; @@ -46,7 +47,6 @@ public class GsonMessageBodyHandler implements MessageBodyWriter, Messag public static final String DATE_FORMAT = "EEE, d MMM yyyy HH:mm:ss Z"; private Gson gson; - private static final String UTF_8 = "UTF-8"; public boolean isReadable(Class aClass, Type type, Annotation[] annotations, MediaType mediaType) { return true; @@ -65,7 +65,7 @@ public class GsonMessageBodyHandler implements MessageBodyWriter, Messag InputStream entityStream) throws IOException, WebApplicationException { - InputStreamReader reader = new InputStreamReader(entityStream, "UTF-8"); + InputStreamReader reader = new InputStreamReader(entityStream, StandardCharsets.UTF_8); try { return getGson().fromJson(reader, type); @@ -87,7 +87,7 @@ public class GsonMessageBodyHandler implements MessageBodyWriter, Messag OutputStream entityStream) throws IOException, WebApplicationException { - OutputStreamWriter writer = new OutputStreamWriter(entityStream, UTF_8); + OutputStreamWriter writer = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8); try { getGson().toJson(object, type, writer); } finally { diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/beans/ValidationInterceptor.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/beans/ValidationInterceptor.java index 8d7224f4cc..ce98d50ee7 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/beans/ValidationInterceptor.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/beans/ValidationInterceptor.java @@ -49,7 +49,9 @@ public class ValidationInterceptor extends AbstractPhaseInterceptor { if (validator == null) { log.warn("Bean Validation provider could not be found, no validation will be performed"); } else { - log.debug("Validation In-Interceptor initialized successfully"); + if (log.isDebugEnabled()) { + log.debug("Validation In-Interceptor initialized successfully"); + } } } diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/util/DeviceOrgConstants.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/util/DeviceOrgConstants.java new file mode 100644 index 0000000000..7772e613bf --- /dev/null +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/util/DeviceOrgConstants.java @@ -0,0 +1,6 @@ +package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api.util; + +public class DeviceOrgConstants { + + public static final String SCOPE = "scope"; +} diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java index c3d79dace1..302c0351cf 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java @@ -132,8 +132,10 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { stmt.setInt(1, node.getDeviceId()); try (ResultSet rs = stmt.executeQuery()) { + DeviceNode child; + DeviceOrganization organization; while (rs.next()) { - DeviceNode child = getDeviceFromResultSet(rs); + child = getDeviceFromResultSet(rs); node.getChildren().add(child); hasChildren = true; if (includeDevice @@ -143,7 +145,7 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { parentAdded = true; // Set the flag to true after adding the parent device. } - DeviceOrganization organization = loadDeviceOrganization(rs); + organization = loadDeviceOrganization(rs); organizations.add(organization); getChildrenRecursive( @@ -244,15 +246,17 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, node.getDeviceId()); try (ResultSet rs = stmt.executeQuery()) { + DeviceNode parent; + DeviceOrganization organization; while (rs.next()) { - DeviceNode parent = getDeviceFromResultSet(rs); + parent = getDeviceFromResultSet(rs); node.getParents().add(parent); if (includeDevice && !childAdded) { parentNodes.add(node); childAdded = true; } - DeviceOrganization organization = loadDeviceOrganization(rs); + organization = loadDeviceOrganization(rs); organizations.add(organization); getParentsRecursive( @@ -298,8 +302,9 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { String sql = "SELECT * FROM DM_DEVICE_ORGANIZATION"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (ResultSet rs = stmt.executeQuery()) { + DeviceOrganization deviceOrganization; while (rs.next()) { - DeviceOrganization deviceOrganization = loadDeviceOrganization(rs); + deviceOrganization = loadDeviceOrganization(rs); deviceOrganizations.add(deviceOrganization); } } @@ -339,8 +344,9 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { stmt.setInt(1, request.getLimit()); stmt.setInt(2, request.getOffSet()); try (ResultSet rs = stmt.executeQuery()) { + DeviceOrganization deviceOrganization; while (rs.next()) { - DeviceOrganization deviceOrganization = loadDeviceOrganizationWithDeviceDetails(rs); + deviceOrganization = loadDeviceOrganizationWithDeviceDetails(rs); deviceOrganizations.add(deviceOrganization); } } @@ -373,8 +379,9 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { stmt.setInt(1, request.getLimit()); stmt.setInt(2, request.getOffSet()); try (ResultSet rs = stmt.executeQuery()) { + DeviceOrganization deviceOrganization; while (rs.next()) { - DeviceOrganization deviceOrganization = loadDeviceOrganization(rs); + deviceOrganization = loadDeviceOrganization(rs); deviceOrganizations.add(deviceOrganization); } }