From 63889f4e059ccff2a364d8e81076e40a194fc664 Mon Sep 17 00:00:00 2001 From: Lasantha Dharmakeerthi Date: Mon, 10 Apr 2023 06:23:40 +0000 Subject: [PATCH] Fix default theme loading issue (#98) Co-authored-by: Dharmakeerthi Lasantha Co-authored-by: Pahansith Gunathilake Reviewed-on: https://repository.entgra.net/community/device-mgt-core/pulls/98 Co-authored-by: Lasantha Dharmakeerthi Co-committed-by: Lasantha Dharmakeerthi --- .../service/impl/WhiteLabelServiceImpl.java | 6 +--- .../mgt/WhiteLabelManagementService.java | 2 +- .../mgt/WhiteLabelManagementServiceImpl.java | 35 ++++++++++++------- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/WhiteLabelServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/WhiteLabelServiceImpl.java index 97621667dc..9c11dcd6f0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/WhiteLabelServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/WhiteLabelServiceImpl.java @@ -127,13 +127,9 @@ public class WhiteLabelServiceImpl implements WhiteLabelService { WhiteLabelTheme whiteLabelTheme = DeviceMgtAPIUtils.getWhiteLabelManagementService().getWhiteLabelTheme(tenantDomain); return Response.status(Response.Status.CREATED).entity(whiteLabelTheme).build(); } catch (MetadataManagementException e) { - String msg = "Error occurred while deleting whitelabel for tenant"; + String msg = "Error occurred while getting whitelabel for tenant"; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } catch (NotFoundException e) { - String msg = "Not white label theme configured for this tenant"; - log.error(msg, e); - return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } catch (DeviceManagementException e) { String msg = "Error occurred while retrieving tenant details of whitelabel"; log.error(msg, e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/metadata/mgt/WhiteLabelManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/metadata/mgt/WhiteLabelManagementService.java index 6ccf82eab4..6fc34c52dd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/metadata/mgt/WhiteLabelManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/metadata/mgt/WhiteLabelManagementService.java @@ -79,5 +79,5 @@ public interface WhiteLabelManagementService { * This method is useful to get existing white label theme * @throws MetadataManagementException if error while getting existing white label theme */ - WhiteLabelTheme getWhiteLabelTheme(String tenantDomain) throws MetadataManagementException, NotFoundException, DeviceManagementException; + WhiteLabelTheme getWhiteLabelTheme(String tenantDomain) throws MetadataManagementException, DeviceManagementException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/metadata/mgt/WhiteLabelManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/metadata/mgt/WhiteLabelManagementServiceImpl.java index ec2bca767e..79e0c7ec01 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/metadata/mgt/WhiteLabelManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/metadata/mgt/WhiteLabelManagementServiceImpl.java @@ -26,7 +26,6 @@ import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; -import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Base64File; import org.wso2.carbon.device.mgt.common.FileResponse; @@ -329,10 +328,6 @@ public class WhiteLabelManagementServiceImpl implements WhiteLabelManagementServ } finally { MetadataManagementDAOFactory.closeConnection(); } - } catch (NotFoundException e) { - String msg = "Error occurred while retrieving existing white label theme"; - log.error(msg, e); - throw new MetadataManagementException(msg, e); } catch (DeviceManagementException e) { String msg = "Error occurred while getting tenant details of white label"; log.error(msg, e); @@ -429,20 +424,36 @@ public class WhiteLabelManagementServiceImpl implements WhiteLabelManagementServ } @Override - public WhiteLabelTheme getWhiteLabelTheme(String tenantDomain) throws MetadataManagementException, NotFoundException, DeviceManagementException { + public WhiteLabelTheme getWhiteLabelTheme(String tenantDomain) throws MetadataManagementException, DeviceManagementException { int tenantId = DeviceManagerUtil.getTenantId(tenantDomain); if (log.isDebugEnabled()) { log.debug("Retrieving whitelabel theme for tenant: " + tenantId); } - try { - MetadataManagementDAOFactory.openConnection(); - Metadata metadata = metadataDAO.getMetadata(tenantId, MetadataConstants.WHITELABEL_META_KEY); + Metadata metadata = getWhiteLabelMetaData(tenantId); + if (metadata == null) { + addDefaultWhiteLabelThemeIfNotExist(tenantId); + metadata = getWhiteLabelMetaData(tenantId); if (metadata == null) { - String msg = "Whitelabel theme not found for tenant: " + tenantId; + String msg = "Whitelabel theme not found for tenant: " + tenantId + ". Further, Default White Label " + + "Theming Adding step failed."; log.error(msg); - throw new NotFoundException(msg); + throw new MetadataManagementException(msg); } - return new Gson().fromJson(metadata.getMetaValue(), WhiteLabelTheme.class); + } + return new Gson().fromJson(metadata.getMetaValue(), WhiteLabelTheme.class); + } + + /** + * Load White label Meta Data for given tenant Id. + * @param tenantId Id of the tenant + * @return {@link Metadata} + * @throws MetadataManagementException if an error occurred while getting Meta-Data info from Database for a + * given tenant ID. + */ + private Metadata getWhiteLabelMetaData (int tenantId) throws MetadataManagementException { + try { + MetadataManagementDAOFactory.openConnection(); + return metadataDAO.getMetadata(tenantId, MetadataConstants.WHITELABEL_META_KEY); } catch (MetadataManagementDAOException e) { String msg = "Error occurred while retrieving white label theme for tenant:" + tenantId; log.error(msg, e);