From 43a19e6b7f5ba2c035cc94fa54873688b5ed985f Mon Sep 17 00:00:00 2001 From: nipuni Date: Fri, 27 Sep 2024 23:09:00 +0530 Subject: [PATCH] Add docUrl into metadata. --- .../common/metadata/mgt/WhiteLabelTheme.java | 9 ++ .../mgt/WhiteLabelThemeCreateRequest.java | 9 ++ .../metadata/mgt/MetaDataConfiguration.java | 11 --- .../mgt/documentation/DocConfiguration.java | 36 -------- .../whitelabel/WhiteLabelConfiguration.java | 10 ++ .../DeviceManagementServiceComponent.java | 12 +-- .../mgt/WhiteLabelManagementServiceImpl.java | 92 ++++++++++++++++++- .../mgt/core/util/DeviceManagerUtil.java | 6 +- .../src/main/resources/conf/cdm-config.xml | 4 +- .../repository/conf/cdm-config.xml.j2 | 6 +- 10 files changed, 130 insertions(+), 65 deletions(-) delete mode 100644 components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/metadata/mgt/documentation/DocConfiguration.java 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/WhiteLabelTheme.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/WhiteLabelTheme.java index 3bf6cc2e5b..62303e8c73 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/WhiteLabelTheme.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/WhiteLabelTheme.java @@ -24,6 +24,7 @@ public class WhiteLabelTheme { private WhiteLabelImage logoIconImage; private String footerText; private String appTitle; + private String docUrl; public String getFooterText() { return footerText; @@ -64,4 +65,12 @@ public class WhiteLabelTheme { public void setLogoIconImage(WhiteLabelImage logoIconImage) { this.logoIconImage = logoIconImage; } + + public String getDocUrl() { + return docUrl; + } + + public void setDocUrl(String docUrl) { + this.docUrl = docUrl; + } } 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/WhiteLabelThemeCreateRequest.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/WhiteLabelThemeCreateRequest.java index 0a48bf0144..95eeb6b01a 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/WhiteLabelThemeCreateRequest.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/WhiteLabelThemeCreateRequest.java @@ -24,6 +24,7 @@ public class WhiteLabelThemeCreateRequest { private WhiteLabelImageRequestPayload logoIcon; private String footerText; private String appTitle; + private String docUrl; public WhiteLabelImageRequestPayload getFavicon() { return favicon; @@ -64,4 +65,12 @@ public class WhiteLabelThemeCreateRequest { public void setLogoIcon(WhiteLabelImageRequestPayload logoIcon) { this.logoIcon = logoIcon; } + + public String getDocUrl() { + return docUrl; + } + + public void setDocUrl(String docUrl) { + this.docUrl = docUrl; + } } 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/config/metadata/mgt/MetaDataConfiguration.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/metadata/mgt/MetaDataConfiguration.java index 52cf65dbf7..1812862dd5 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/metadata/mgt/MetaDataConfiguration.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/metadata/mgt/MetaDataConfiguration.java @@ -18,7 +18,6 @@ package io.entgra.device.mgt.core.device.mgt.core.config.metadata.mgt; -import io.entgra.device.mgt.core.device.mgt.core.config.metadata.mgt.documentation.DocConfiguration; import io.entgra.device.mgt.core.device.mgt.core.config.metadata.mgt.whitelabel.WhiteLabelConfiguration; import javax.xml.bind.annotation.XmlElement; @@ -27,7 +26,6 @@ import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "MetaDataConfiguration") public class MetaDataConfiguration { private WhiteLabelConfiguration whiteLabelConfiguration; - private DocConfiguration docConfiguration; @XmlElement(name = "WhiteLabelConfiguration", required = true) public WhiteLabelConfiguration getWhiteLabelConfiguration() { @@ -37,13 +35,4 @@ public class MetaDataConfiguration { public void setWhiteLabelConfiguration(WhiteLabelConfiguration whiteLabelConfiguration) { this.whiteLabelConfiguration = whiteLabelConfiguration; } - - @XmlElement(name = "DocConfiguration", required = true) - public DocConfiguration getDocConfiguration() { - return docConfiguration; - } - - public void setDocConfiguration(DocConfiguration docConfiguration) { - this.docConfiguration = docConfiguration; - } } 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/config/metadata/mgt/documentation/DocConfiguration.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/metadata/mgt/documentation/DocConfiguration.java deleted file mode 100644 index 63a6981146..0000000000 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/metadata/mgt/documentation/DocConfiguration.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. - * - * Entgra (Pvt) Ltd. 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 io.entgra.device.mgt.core.device.mgt.core.config.metadata.mgt.documentation; - -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; - -@XmlRootElement(name = "DocConfiguration") -public class DocConfiguration { - private String docUrl; - - @XmlElement(name = "DocUrl", required = true) - public String getDocUrl() { - return docUrl; - } - - public void setDocUrl(String docUrl) { - this.docUrl = docUrl; - } -} 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/config/metadata/mgt/whitelabel/WhiteLabelConfiguration.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/metadata/mgt/whitelabel/WhiteLabelConfiguration.java index 0ff43eee28..b12c1b0b6d 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/metadata/mgt/whitelabel/WhiteLabelConfiguration.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/config/metadata/mgt/whitelabel/WhiteLabelConfiguration.java @@ -26,6 +26,16 @@ public class WhiteLabelConfiguration { private String footerText; private String appTitle; private WhiteLabelImages whiteLabelImages; + private String docUrl; + + @XmlElement(name = "DocUrl", required = true) + public String getDocUrl() { + return docUrl; + } + + public void setDocUrl(String docUrl) { + this.docUrl = docUrl; + } @XmlElement(name = "FooterText", required = true) public String getFooterText() { 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 c648965773..8d45e1ec91 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 @@ -18,6 +18,7 @@ package io.entgra.device.mgt.core.device.mgt.core.internal; import io.entgra.device.mgt.core.device.mgt.common.authorization.GroupAccessAuthorizationService; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; import io.entgra.device.mgt.core.device.mgt.core.authorization.GroupAccessAuthorizationServiceImpl; import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.DeviceStatusManagementServiceImpl; @@ -335,15 +336,14 @@ public class DeviceManagementServiceComponent { bundleContext.registerService(MetadataManagementService.class.getName(), metadataManagementService, null); /* Registering Whitelabel Service */ - WhiteLabelManagementService whiteLabelManagementService = new WhiteLabelManagementServiceImpl(); - DeviceManagementDataHolder.getInstance().setWhiteLabelManagementService(whiteLabelManagementService); try { + WhiteLabelManagementService whiteLabelManagementService = new WhiteLabelManagementServiceImpl(); + DeviceManagementDataHolder.getInstance().setWhiteLabelManagementService(whiteLabelManagementService); whiteLabelManagementService.addDefaultWhiteLabelThemeIfNotExist(tenantId); - } catch (Throwable e) { - log.error("Error occurred while adding default tenant white label theme", e); - + bundleContext.registerService(WhiteLabelManagementService.class.getName(), whiteLabelManagementService, null); + } catch (MetadataManagementException e) { + log.error("Error occurred while initializing the white label management service", e); } - bundleContext.registerService(WhiteLabelManagementService.class.getName(), whiteLabelManagementService, null); /* Registering DeviceState Filter Service */ DeviceStatusManagementService deviceStatusManagementService = new DeviceStatusManagementServiceImpl(); 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/WhiteLabelManagementServiceImpl.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/WhiteLabelManagementServiceImpl.java index 4ade3adc82..15a1e82cee 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/WhiteLabelManagementServiceImpl.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/WhiteLabelManagementServiceImpl.java @@ -60,8 +60,47 @@ public class WhiteLabelManagementServiceImpl implements WhiteLabelManagementServ private final MetadataDAO metadataDAO; - public WhiteLabelManagementServiceImpl() { + public WhiteLabelManagementServiceImpl() throws MetadataManagementException { this.metadataDAO = MetadataManagementDAOFactory.getMetadataDAO(); + initializeWhiteLabelThemes(); + } + + private void initializeWhiteLabelThemes() throws MetadataManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + WhiteLabelTheme defaultTheme = getDefaultWhiteLabelTheme(); + Metadata whiteLabelMetadata = getWhiteLabelMetaData(tenantId); + if (whiteLabelMetadata != null) { + WhiteLabelTheme whiteLabelTheme = new Gson().fromJson(whiteLabelMetadata.getMetaValue(), + WhiteLabelTheme.class); + if (whiteLabelTheme.getDocUrl() == null) { + whiteLabelTheme.setDocUrl(defaultTheme.getDocUrl()); + Metadata updatedMetadata = constructWhiteLabelThemeMetadata(whiteLabelTheme); + try { + MetadataManagementDAOFactory.beginTransaction(); + metadataDAO.updateMetadata(tenantId, updatedMetadata); + MetadataManagementDAOFactory.commitTransaction(); + if (log.isDebugEnabled()) { + log.debug("WhiteLabel theme's DocUrl was missing and has been updated to the default value " + + "for tenant: " + tenantId); + } + } + catch (MetadataManagementDAOException e) { + MetadataManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while fetching white label metadata for tenant: " + tenantId; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "Transaction failed while updating white label theme for tenant: " + + tenantId; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } finally { + MetadataManagementDAOFactory.closeConnection(); + } + } + } else { + addDefaultWhiteLabelThemeIfNotExist(tenantId); + } } @Override @@ -183,6 +222,16 @@ public class WhiteLabelManagementServiceImpl implements WhiteLabelManagementServ WhiteLabelStorageUtil.deleteWhiteLabelImageForTenantIfExists(tenantId); } + /** + * Get default metaDataConfiguration DocUrl from config + */ + private String getDefaultDocUrl() { + MetaDataConfiguration metaDataConfiguration = DeviceConfigurationManager.getInstance(). + getDeviceManagementConfig().getMetaDataConfiguration(); + WhiteLabelConfiguration whiteLabelConfiguration = metaDataConfiguration.getWhiteLabelConfiguration(); + return whiteLabelConfiguration.getDocUrl(); + } + /** * Construct and return default whitelabel detail bean {@link WhiteLabelImage} */ @@ -193,11 +242,13 @@ public class WhiteLabelManagementServiceImpl implements WhiteLabelManagementServ WhiteLabelImage logo = constructDefaultLogoImage(); WhiteLabelImage logoIcon = constructDefaultLogoIconImage(); WhiteLabelTheme defaultTheme = new WhiteLabelTheme(); + String docUrl = getDefaultDocUrl(); defaultTheme.setFooterText(footerText); defaultTheme.setAppTitle(appTitle); defaultTheme.setLogoImage(logo); defaultTheme.setLogoIconImage(logoIcon); defaultTheme.setFaviconImage(favicon); + defaultTheme.setDocUrl(docUrl); return defaultTheme; } @@ -386,6 +437,7 @@ public class WhiteLabelManagementServiceImpl implements WhiteLabelManagementServ whiteLabelTheme.setLogoIconImage(logoIconImage); whiteLabelTheme.setFooterText(whiteLabelThemeCreateRequest.getFooterText()); whiteLabelTheme.setAppTitle(whiteLabelThemeCreateRequest.getAppTitle()); + whiteLabelTheme.setDocUrl(whiteLabelThemeCreateRequest.getDocUrl()); return whiteLabelTheme; } @@ -418,6 +470,38 @@ public class WhiteLabelManagementServiceImpl implements WhiteLabelManagementServ return metadata; } + /** + * updates the given WhiteLabelTheme with default value for docUrl + * + * @param whiteLabelTheme the WhiteLabelTheme to be updated with defaults if necessary. + * @param tenantId the ID of the tenant whose metadata is being updated. + * @throws MetadataManagementException exception for an error occurs during the update or transaction commit. + */ + private void updateWhiteLabelThemeWithDefaults(WhiteLabelTheme whiteLabelTheme, int tenantId) + throws MetadataManagementException { + WhiteLabelTheme defaultTheme = getDefaultWhiteLabelTheme(); + if (whiteLabelTheme.getDocUrl() == null) { + whiteLabelTheme.setDocUrl(defaultTheme.getDocUrl()); + } + Metadata updatedMetadata = constructWhiteLabelThemeMetadata(whiteLabelTheme); + try { + MetadataManagementDAOFactory.beginTransaction(); + metadataDAO.updateMetadata(tenantId, updatedMetadata); + MetadataManagementDAOFactory.commitTransaction(); + } catch (MetadataManagementDAOException e) { + MetadataManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while updating metadata for tenant: " + tenantId; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred while committing the transaction for tenant: " + tenantId; + log.error(msg, e); + throw new MetadataManagementException(msg, e); + } finally { + MetadataManagementDAOFactory.closeConnection(); + } + } + @Override public WhiteLabelTheme getWhiteLabelTheme(String tenantDomain) throws MetadataManagementException, DeviceManagementException { int tenantId = DeviceManagerUtil.getTenantId(tenantDomain); @@ -435,7 +519,11 @@ public class WhiteLabelManagementServiceImpl implements WhiteLabelManagementServ throw new MetadataManagementException(msg); } } - return new Gson().fromJson(metadata.getMetaValue(), WhiteLabelTheme.class); + WhiteLabelTheme whiteLabelTheme = new Gson().fromJson(metadata.getMetaValue(), WhiteLabelTheme.class); + if (whiteLabelTheme.getDocUrl() == null) { + updateWhiteLabelThemeWithDefaults(whiteLabelTheme, tenantId); + } + return whiteLabelTheme; } /** 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/util/DeviceManagerUtil.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/util/DeviceManagerUtil.java index 5ff2c15b6d..3965b1cc77 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/util/DeviceManagerUtil.java @@ -20,7 +20,7 @@ package io.entgra.device.mgt.core.device.mgt.core.util; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.Gson; import io.entgra.device.mgt.core.device.mgt.core.config.metadata.mgt.MetaDataConfiguration; -import io.entgra.device.mgt.core.device.mgt.core.config.metadata.mgt.documentation.DocConfiguration; +import io.entgra.device.mgt.core.device.mgt.core.config.metadata.mgt.whitelabel.WhiteLabelConfiguration; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.HttpResponse; @@ -1289,7 +1289,7 @@ public final class DeviceManagerUtil { */ public static String getDocUrl() { DeviceManagementConfig deviceManagementConfig = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); - DocConfiguration docConfiguration = deviceManagementConfig.getMetaDataConfiguration().getDocConfiguration(); - return docConfiguration.getDocUrl(); + WhiteLabelConfiguration whiteLabelConfig = deviceManagementConfig.getMetaDataConfiguration().getWhiteLabelConfiguration(); + return whiteLabelConfig.getDocUrl(); } } diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml index f4b6d5266b..a8946af8eb 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml @@ -200,10 +200,8 @@ icon.png default - - https://docs.entgra.io/uem/6.0.0 - + diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/cdm-config.xml.j2 b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/cdm-config.xml.j2 index 2d5f7639f8..e8560353c3 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/cdm-config.xml.j2 +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/conf_templates/templates/repository/conf/cdm-config.xml.j2 @@ -348,12 +348,10 @@ icon.png default - - {% if product_conf is defined %} - https://docs.entgra.io/uem/{{product_conf.server_version}} + https://docs.entgra.io/uem/{{product_conf.server_version}} {% endif %} - +