From c6905b968e4acceed4d51f03f4be52c9c1561008 Mon Sep 17 00:00:00 2001 From: tcdlpds Date: Wed, 11 Sep 2024 23:18:21 +0530 Subject: [PATCH] Improve device status management service --- .../APIManagementProviderServiceImpl.java | 10 +- .../publisher/APIPublisherStartupHandler.java | 4 +- .../impl/DeviceStatusFilterServiceImpl.java | 22 ++-- .../service/impl/MetadataServiceImpl.java | 19 --- .../MetadataKeyAlreadyExistsException.java | 2 +- .../MetadataKeyNotFoundException.java | 2 +- .../mgt/DeviceStatusManagementService.java | 2 +- .../DeviceManagementServiceComponent.java | 8 +- .../DeviceStatusManagementServiceImpl.java | 122 ++++++++++++------ .../MetaRepositoryBasedLicenseManager.java | 10 +- 10 files changed, 115 insertions(+), 86 deletions(-) diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderServiceImpl.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderServiceImpl.java index d3f54d1247..3e612e5838 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderServiceImpl.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderServiceImpl.java @@ -257,14 +257,12 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe MetadataManagementService metadataManagementService = APIApplicationManagerExtensionDataHolder.getInstance().getMetadataManagementService(); metadataManagementService.createMetadata(metaData); return apiApplicationKey; - } catch (MetadataManagementException e) { - String msg = "Error occurred while creating meta data for meta key: " + applicationName; + } catch (MetadataKeyAlreadyExistsException e) { + String msg = "Since meta key:" + applicationName + " already exists, meta data creating process failed."; log.error(msg, e); throw new APIManagerException(msg, e); - } catch (MetadataKeyAlreadyExistsException e) { - String msg = - "Since meta key:" + applicationName + " already exists, meta data creating process " + - "failed."; + } catch (MetadataManagementException e) { + String msg = "Error occurred while creating meta data for meta key: " + applicationName; log.error(msg, e); throw new APIManagerException(msg, e); } catch (BadRequestException e) { diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherStartupHandler.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherStartupHandler.java index dcad2fa846..bd3451c4e3 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherStartupHandler.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherStartupHandler.java @@ -196,10 +196,10 @@ public class APIPublisherStartupHandler implements ServerStartupObserver { APIPublisherDataHolder.getInstance().setPermScopeMapping(permScopeMap); log.info(Constants.PERM_SCOPE_MAPPING_META_KEY + "entry updated successfully"); - } catch (MetadataManagementException e) { - log.error("Error encountered while updating permission scope mapping metadata with default scopes"); } catch (MetadataKeyAlreadyExistsException e) { log.error("Metadata entry already exists for " + Constants.PERM_SCOPE_MAPPING_META_KEY); + } catch (MetadataManagementException e) { + log.error("Error encountered while updating permission scope mapping metadata with default scopes"); } } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceStatusFilterServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceStatusFilterServiceImpl.java index 7e5fd01437..cc2a2c90e9 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceStatusFilterServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/DeviceStatusFilterServiceImpl.java @@ -20,13 +20,20 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.api.DeviceStatusFilterService; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.DeviceMgtAPIUtils; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataKeyNotFoundException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.CarbonContext; -import javax.ws.rs.*; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.util.List; @@ -42,15 +49,14 @@ public class DeviceStatusFilterServiceImpl implements DeviceStatusFilterService @GET @Path("/{deviceType}") public Response getDeviceStatusFilters(@PathParam("deviceType") String deviceType) { - List result; - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); try { DeviceStatusManagementService deviceManagementProviderService = DeviceMgtAPIUtils.getDeviceStatusManagmentService(); - result = deviceManagementProviderService.getDeviceStatusFilters(deviceType, tenantId); - if (result != null) { - return Response.status(Response.Status.OK).entity(result).build(); - } - return Response.status(Response.Status.NO_CONTENT).entity(false).build(); + return Response.status(Response.Status.OK).entity(deviceManagementProviderService + .getDeviceStatusFilters(deviceType, CarbonContext.getThreadLocalCarbonContext().getTenantId())).build(); + } catch (MetadataKeyNotFoundException e) { + String msg = "Couldn't find the device status filter details for device type: " + deviceType; + log.error(msg, e); + return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); } catch (MetadataManagementException e) { String msg = "Error occurred while getting device status filter of the tenant."; log.error(msg, e); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/MetadataServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/MetadataServiceImpl.java index 6ed5270334..2e2999bbda 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/MetadataServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/MetadataServiceImpl.java @@ -36,8 +36,6 @@ import org.apache.commons.logging.LogFactory; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; -import java.io.ByteArrayInputStream; -import java.io.IOException; import java.util.List; /** @@ -147,21 +145,4 @@ public class MetadataServiceImpl implements MetadataService { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } - - /** - * Useful to send files as application/octet-stream responses - */ - private Response sendFileStream(byte[] content) throws IOException { - try (ByteArrayInputStream binaryDuplicate = new ByteArrayInputStream(content)) { - Response.ResponseBuilder response = Response - .ok(binaryDuplicate, MediaType.APPLICATION_OCTET_STREAM); - response.status(Response.Status.OK); - response.header("Content-Length", content.length); - return response.build(); - } catch (IOException e) { - String msg = "Error occurred while creating input stream from buffer array. "; - log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } - } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/exceptions/MetadataKeyAlreadyExistsException.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/exceptions/MetadataKeyAlreadyExistsException.java index f138d5c12c..5feabe2708 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/exceptions/MetadataKeyAlreadyExistsException.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/exceptions/MetadataKeyAlreadyExistsException.java @@ -21,7 +21,7 @@ package io.entgra.device.mgt.core.device.mgt.common.exceptions; /** * Custom exception class to be used in MetadataMgmt related functionalities. */ -public class MetadataKeyAlreadyExistsException extends Exception { +public class MetadataKeyAlreadyExistsException extends MetadataManagementException { private static final long serialVersionUID = -1814347544027733436L; private String errorMessage; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/exceptions/MetadataKeyNotFoundException.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/exceptions/MetadataKeyNotFoundException.java index 965c1b757b..0f95bdaf05 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/exceptions/MetadataKeyNotFoundException.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/exceptions/MetadataKeyNotFoundException.java @@ -21,7 +21,7 @@ package io.entgra.device.mgt.core.device.mgt.common.exceptions; /** * Custom exception class to be used in MetadataMgmt related functionalities. */ -public class MetadataKeyNotFoundException extends Exception { +public class MetadataKeyNotFoundException extends MetadataManagementException { private static final long serialVersionUID = 5260831982626354815L; private String errorMessage; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/metadata/mgt/DeviceStatusManagementService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/metadata/mgt/DeviceStatusManagementService.java index 5ee7c8fce0..5abb9c7e88 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/metadata/mgt/DeviceStatusManagementService.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/metadata/mgt/DeviceStatusManagementService.java @@ -38,7 +38,7 @@ public interface DeviceStatusManagementService { * * @throws MetadataManagementException if error while resetting default device status */ - void resetToDefaultDeviceStausFilter() throws MetadataManagementException; + void resetToDefaultDeviceStatusFilter(int tenantId) throws MetadataManagementException; /** * This method is useful to update existing allowed device 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/internal/DeviceManagementServiceComponent.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementServiceComponent.java index 58e1d34ec8..c648965773 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -346,15 +346,15 @@ public class DeviceManagementServiceComponent { bundleContext.registerService(WhiteLabelManagementService.class.getName(), whiteLabelManagementService, null); /* Registering DeviceState Filter Service */ - DeviceStatusManagementService deviceStatusManagemntService = new DeviceStatusManagementServiceImpl(); - DeviceManagementDataHolder.getInstance().setDeviceStatusManagementService(deviceStatusManagemntService); + DeviceStatusManagementService deviceStatusManagementService = new DeviceStatusManagementServiceImpl(); + DeviceManagementDataHolder.getInstance().setDeviceStatusManagementService(deviceStatusManagementService); try { - deviceStatusManagemntService.addDefaultDeviceStatusFilterIfNotExist(tenantId); + deviceStatusManagementService.addDefaultDeviceStatusFilterIfNotExist(tenantId); } catch (Throwable e) { log.error("Error occurred while adding default tenant device status", e); } - bundleContext.registerService(DeviceStatusManagementService.class.getName(), deviceStatusManagemntService, null); + bundleContext.registerService(DeviceStatusManagementService.class.getName(), deviceStatusManagementService, null); /* Registering Event Configuration Service */ EventConfigurationProviderService eventConfigurationService = new EventConfigurationProviderServiceImpl(); 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/metadata/mgt/DeviceStatusManagementServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java index 1daea9fd24..42c0966da5 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/metadata/mgt/DeviceStatusManagementServiceImpl.java @@ -21,6 +21,7 @@ package io.entgra.device.mgt.core.device.mgt.core.metadata.mgt; import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataKeyNotFoundException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.TransactionManagementException; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.AllowedDeviceStatus; @@ -42,12 +43,12 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; - public class DeviceStatusManagementServiceImpl implements DeviceStatusManagementService { private static final Log log = LogFactory.getLog(DeviceStatusManagementServiceImpl.class); private final MetadataDAO metadataDAO; + private static final Gson gson = new Gson(); public DeviceStatusManagementServiceImpl() { this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO(); @@ -57,12 +58,11 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement public void addDefaultDeviceStatusFilterIfNotExist(int tenantId) throws MetadataManagementException { try { MetadataManagementDAOFactory.beginTransaction(); - if (!metadataDAO.isExist(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY) && !metadataDAO.isExist(tenantId, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY)) { - Metadata defaultDeviceStatusMetadata = constructDeviceStatusMetadata(getDefaultDeviceStatus()); - Metadata defaultDeviceStatusCheckMetadata = constructDeviceStatusCheckMetadata(getDefaultDeviceStatusCheck()); - // Add default device status and device status check metadata entries - addMetadataEntry(tenantId, defaultDeviceStatusMetadata, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); - addMetadataEntry(tenantId, defaultDeviceStatusCheckMetadata, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY); + if (!metadataDAO.isExist(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY)) { + metadataDAO.addMetadata(tenantId, constructDeviceStatusMetadata(getDefaultDeviceStatus())); + } + if (!metadataDAO.isExist(tenantId, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY)) { + metadataDAO.addMetadata(tenantId, constructDeviceStatusCheckMetadata(getDefaultDeviceStatusCheck())); } MetadataManagementDAOFactory.commitTransaction(); } catch (MetadataManagementDAOException e) { @@ -80,8 +80,27 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement } @Override - public void resetToDefaultDeviceStausFilter() throws MetadataManagementException { - + public void resetToDefaultDeviceStatusFilter(int tenantId) throws MetadataManagementException { + try { + MetadataManagementDAOFactory.beginTransaction(); + Metadata defaultDeviceStatusMetadata = constructDeviceStatusMetadata(getDefaultDeviceStatus()); + Metadata defaultDeviceStatusCheckMetadata = constructDeviceStatusCheckMetadata(getDefaultDeviceStatusCheck()); + // Add default device status and device status check metadata entries + metadataDAO.addMetadata(tenantId, defaultDeviceStatusMetadata); + metadataDAO.addMetadata(tenantId, defaultDeviceStatusCheckMetadata); + MetadataManagementDAOFactory.commitTransaction(); + } catch (MetadataManagementDAOException e) { + MetadataManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while inserting default device status metadata entries."; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while starting the transaction to reset default device status filters."; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } finally { + MetadataManagementDAOFactory.closeConnection(); + } } @Override @@ -91,7 +110,6 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement // Retrieve the current device status metadata Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); if (metadata != null) { - Gson gson = new Gson(); Type listType = new TypeToken>() { }.getType(); List currentStatusList = gson.fromJson(metadata.getMetaValue(), listType); @@ -105,7 +123,7 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement } } metadata.setMetaValue(gson.toJson(currentStatusList)); - updateMetadataEntry(tenantId, metadata, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); + metadataDAO.updateMetadata(tenantId, metadata); } MetadataManagementDAOFactory.commitTransaction(); } catch (MetadataManagementDAOException e) { @@ -124,14 +142,12 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement @Override public boolean updateDefaultDeviceStatusCheck(int tenantId, boolean isChecked) throws MetadataManagementException { - boolean success = false; try { MetadataManagementDAOFactory.beginTransaction(); if (metadataDAO.isExist(tenantId, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY)) { - Metadata isDeviceStatusChecked = constructDeviceStatusCheckMetadata(isChecked); // Add default device status check metadata entries - updateMetadataEntry(tenantId, isDeviceStatusChecked, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY); - success = true; + metadataDAO.updateMetadata(tenantId, constructDeviceStatusCheckMetadata(isChecked)); + return true; } MetadataManagementDAOFactory.commitTransaction(); } catch (MetadataManagementDAOException e) { @@ -146,7 +162,7 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement } finally { MetadataManagementDAOFactory.closeConnection(); } - return success; + return false; } @Override @@ -154,11 +170,15 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement try { MetadataManagementDAOFactory.openConnection(); Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); - Gson gson = new Gson(); - Type listType = new TypeToken>() {}.getType(); - List statusList = gson.fromJson(metadata.getMetaValue(), listType); - - return statusList; + if (metadata == null) { + String msg = + "Couldn't find the meta data value for meta key: " + MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY; + log.error(msg); + throw new MetadataKeyNotFoundException(msg); + } + Type listType = new TypeToken>() { + }.getType(); + return gson.fromJson(metadata.getMetaValue(), listType); } catch (MetadataManagementDAOException e) { String msg = "Error occurred while retrieving device status meta data for tenant:" + tenantId; log.error(msg, e); @@ -177,8 +197,14 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement try { MetadataManagementDAOFactory.openConnection(); Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); - Gson gson = new Gson(); - Type listType = new TypeToken>() {}.getType(); + if (metadata == null) { + String msg = "Couldn't find the meta details of meta Key: " + + MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY + " and tenant Id: " + tenantId; + log.error(msg); + throw new MetadataKeyNotFoundException(msg); + } + Type listType = new TypeToken>() { + }.getType(); List statusList = gson.fromJson(metadata.getMetaValue(), listType); for (AllowedDeviceStatus status : statusList) { @@ -207,6 +233,12 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement try { MetadataManagementDAOFactory.openConnection(); Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY); + if (metadata == null) { + String msg = "Couldn't find the meta data value for meta key: " + + MetadataConstants.IS_DEVICE_STATUS_CHECK_META_KEY + " and tenant Id: " + tenantId; + log.error(msg); + throw new MetadataKeyNotFoundException(msg); + } String metaValue = metadata.getMetaValue(); return Boolean.parseBoolean(metaValue); } catch (MetadataManagementDAOException e) { @@ -227,8 +259,13 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement try { MetadataManagementDAOFactory.openConnection(); Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY); + if (metadata == null) { + String msg = "Couldn't find the meta data value for meta key: " + + MetadataConstants.ALLOWED_DEVICE_STATUS_META_KEY + " and tenant Id: " + tenantId; + log.error(msg); + throw new MetadataKeyNotFoundException(msg); + } - Gson gson = new Gson(); Type listType = new TypeToken>() { }.getType(); List statusList = gson.fromJson(metadata.getMetaValue(), listType); @@ -254,22 +291,13 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement } } - private void addMetadataEntry(int tenantId, Metadata metadata, String key) throws MetadataManagementDAOException { - metadataDAO.addMetadata(tenantId, metadata); - if (log.isDebugEnabled()) { - log.debug(key + " metadata entry has been inserted successfully"); - } - } - - private void updateMetadataEntry(int tenantId, Metadata metadata, String key) throws MetadataManagementDAOException { - metadataDAO.updateMetadata(tenantId, metadata); - if (log.isDebugEnabled()) { - log.debug(key + " metadata entry has been updated successfully"); - } - } - + /** + * To construct device status Meta data by using received device status items + * + * @param deviceStatusItems {@link List} + * @return {@link Metadata} + */ private Metadata constructDeviceStatusMetadata(List deviceStatusItems) { - Gson gson = new Gson(); String deviceStatusItemsJsonString = gson.toJson(deviceStatusItems); Metadata metadata = new Metadata(); @@ -279,6 +307,12 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement return metadata; } + /** + * To construct device status check Meta data to either enable it or disable it. + * + * @param deviceStatusCheck True or False + * @return {@link Metadata} + */ private Metadata constructDeviceStatusCheckMetadata(boolean deviceStatusCheck) { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("deviceStatusCheck", String.valueOf(deviceStatusCheck)); @@ -289,6 +323,11 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement return metadata; } + /** + * Get default list of device status items from the configuration. + * + * @return List of device status items + */ private List getDefaultDeviceStatus() { DeviceStatusConfigurations deviceStatusConfigurations = UIConfigurationManager.getInstance().getUIConfig().getDeviceStatusConfigurations(); List deviceStatusItems = new ArrayList<>(); @@ -305,6 +344,11 @@ public class DeviceStatusManagementServiceImpl implements DeviceStatusManagement return deviceStatusItems; } + /** + * Get Default device status check from the configuration. + * + * @return default status check value, it will be either 'True' or 'False' + */ private boolean getDefaultDeviceStatusCheck() { DeviceStatusConfigurations deviceStatusConfigurations = UIConfigurationManager.getInstance().getUIConfig().getDeviceStatusConfigurations(); boolean deviceStatusCheck = false; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/license/mgt/meta/data/MetaRepositoryBasedLicenseManager.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/license/mgt/meta/data/MetaRepositoryBasedLicenseManager.java index d86c15c118..c38701e111 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/license/mgt/meta/data/MetaRepositoryBasedLicenseManager.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/license/mgt/meta/data/MetaRepositoryBasedLicenseManager.java @@ -107,13 +107,13 @@ public class MetaRepositoryBasedLicenseManager implements LicenseManager { } else { metadataManagementService.createMetadata(metadata); } - } catch (MetadataManagementException e) { - String msg = "Error occurred while saving the licence value in meta data repository"; + } catch (MetadataKeyAlreadyExistsException e) { + String msg = "Error occurred while saving the licence key and licence key exist.Licence Key: " + + licenceKey; log.error(msg, e); throw new LicenseManagementException(msg, e); - } catch (MetadataKeyAlreadyExistsException e) { - String msg = - "Error occurred while saving the licence key and licence key exist. Licence Key: " + licenceKey; + } catch (MetadataManagementException e) { + String msg = "Error occurred while saving the licence value in meta data repository"; log.error(msg, e); throw new LicenseManagementException(msg, e); }