From c071b9de01870bf42a9aaa26cfa0dec0612bd67e Mon Sep 17 00:00:00 2001 From: Thameera Date: Sun, 21 Jan 2024 17:13:03 +0530 Subject: [PATCH] Modify operation template --- .../cache/OperationTemplateCacheLoader.java | 2 +- .../OperationTemplateCodesCacheLoader.java | 93 +++++++++++ .../template/dao/OperationTemplateDAO.java | 10 +- .../dao/impl/OperationTemplateDAOImpl.java | 96 ++++++++++-- .../util/OperationTemplateManagementUtil.java | 36 ++++- .../impl/OperationTemplateServiceImpl.java | 145 +++++++++++++++--- .../spi/OperationTemplateService.java | 10 +- .../core/operation/template/util/DAOUtil.java | 1 - .../mgt/core/operation/template/DAOTest.java | 12 +- .../core/operation/template/ServiceTest.java | 39 +++-- 10 files changed, 389 insertions(+), 55 deletions(-) create mode 100644 components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/cache/OperationTemplateCodesCacheLoader.java diff --git a/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/cache/OperationTemplateCacheLoader.java b/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/cache/OperationTemplateCacheLoader.java index f151d95d13..69783415a8 100644 --- a/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/cache/OperationTemplateCacheLoader.java +++ b/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/cache/OperationTemplateCacheLoader.java @@ -65,7 +65,7 @@ public class OperationTemplateCacheLoader extends CacheLoader> { + + private static final Log log = LogFactory.getLog(OperationTemplateCodesCacheLoader.class); + + private final OperationTemplateDAO operationTemplateDAO; + + public OperationTemplateCodesCacheLoader() { + this.operationTemplateDAO = OperationTemplateDAOFactory.getOperationTemplateDAO(); + } + + /** + * + * @param key the non-null key whose value should be loaded + * @return + * @throws OperationTemplateMgtPluginException + */ + @Override + public Set load(String key) throws OperationTemplateMgtPluginException { + OperationTemplateCacheKey operationTemplateCacheKey = OperationTemplateManagementUtil.getOperationTemplateCodeCacheKey( + key); + String subTypeId = operationTemplateCacheKey.getSubTypeId(); + String deviceType = operationTemplateCacheKey.getDeviceType(); + + if (log.isTraceEnabled()) { + log.trace( + "Loading operation template for subtype Id : " + subTypeId + " & deviceType : " + deviceType); + } + try { + ConnectionManagerUtils.openDBConnection(); + return operationTemplateDAO.getOperationTemplateCodesByDeviceTypeAndSubTypeId(deviceType, subTypeId); + } catch (DBConnectionException e) { + String msg = + "Error occurred while obtaining the database connection to retrieve operation template codes for " + + + "subtype Id : " + subTypeId + " & device type : " + deviceType; + log.error(msg); + throw new OperationTemplateMgtPluginException(msg, e); + } catch (InvalidCacheLoadException e) { + String msg = + "CacheLoader returned null for operation template codes for subtype Id : " + subTypeId + + " & device type : " + deviceType; + log.error(msg, e); + return null; + } catch (OperationTemplateManagementDAOException e) { + String msg = + "Error occurred in the database level while retrieving operation template codes for subtype Id : " + + subTypeId + " & device type : " + deviceType; + log.error(msg); + throw new OperationTemplateMgtPluginException(msg, e); + } finally { + ConnectionManagerUtils.closeDBConnection(); + } + } + +} diff --git a/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/dao/OperationTemplateDAO.java b/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/dao/OperationTemplateDAO.java index 5a0c60947b..2e3bba443d 100644 --- a/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/dao/OperationTemplateDAO.java +++ b/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/dao/OperationTemplateDAO.java @@ -21,6 +21,9 @@ package io.entgra.device.mgt.core.operation.template.dao; import io.entgra.device.mgt.core.operation.template.dto.OperationTemplate; import io.entgra.device.mgt.core.operation.template.exception.OperationTemplateManagementDAOException; +import java.util.List; +import java.util.Set; + /** * This class represents the key operations associated with persisting mobile-device related * information. @@ -31,8 +34,11 @@ public interface OperationTemplateDAO { OperationTemplate updateOperationTemplate(OperationTemplate operationTemplate) throws OperationTemplateManagementDAOException; - OperationTemplate getOperationTemplate(String subTypeId, String deviceType, String operationCode) throws OperationTemplateManagementDAOException; + OperationTemplate getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(String deviceType, String subTypeId, String operationCode) throws OperationTemplateManagementDAOException; + + List getAllOperationTemplatesByDeviceType(String deviceType) throws OperationTemplateManagementDAOException; - void deleteOperationTemplate(String subTypeId, String deviceCode, String operationCode) throws OperationTemplateManagementDAOException; + int deleteOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(String deviceType, String subTypeId, String operationCode) throws OperationTemplateManagementDAOException; + Set getOperationTemplateCodesByDeviceTypeAndSubTypeId(String deviceType, String subTypeId) throws OperationTemplateManagementDAOException; } diff --git a/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/dao/impl/OperationTemplateDAOImpl.java b/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/dao/impl/OperationTemplateDAOImpl.java index 67a356bca9..17ee2d0bcd 100644 --- a/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/dao/impl/OperationTemplateDAOImpl.java +++ b/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/dao/impl/OperationTemplateDAOImpl.java @@ -31,6 +31,10 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -102,7 +106,7 @@ public class OperationTemplateDAOImpl implements OperationTemplateDAO { stmt.setString(5, operationTemplate.getCode()); stmt.executeUpdate(); - return getOperationTemplate(operationTemplate.getSubTypeId(), operationTemplate.getDeviceType(), operationTemplate.getCode()); + return getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(operationTemplate.getDeviceType(), operationTemplate.getSubTypeId(), operationTemplate.getCode()); } catch (SQLException e) { String msg = "Error occurred while processing update operation template."; log.error(msg); @@ -122,14 +126,14 @@ public class OperationTemplateDAOImpl implements OperationTemplateDAO { * @throws OperationTemplateManagementDAOException */ @Override - public OperationTemplate getOperationTemplate(String subTypeId, String deviceType, String operationCode) + public OperationTemplate getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(String deviceType, String subTypeId, String operationCode) throws OperationTemplateManagementDAOException { try { - String sql = "SELECT * FROM SUB_OPERATION_TEMPLATE WHERE SUB_TYPE_ID = ? AND DEVICE_TYPE = ? AND OPERATION_CODE = ?"; + String sql = "SELECT * FROM SUB_OPERATION_TEMPLATE WHERE DEVICE_TYPE = ? AND SUB_TYPE_ID = ? AND OPERATION_CODE = ?"; Connection conn = ConnectionManagerUtils.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { - stmt.setString(1, subTypeId); - stmt.setString(2, deviceType); + stmt.setString(1, deviceType); + stmt.setString(2, subTypeId); stmt.setString(3, operationCode); try (ResultSet rs = stmt.executeQuery()) { @@ -151,22 +155,94 @@ public class OperationTemplateDAOImpl implements OperationTemplateDAO { } /** - * @param subTypeId * @param deviceType - * @param operationCode + * @param subTypeId + * @return * @throws OperationTemplateManagementDAOException */ @Override - public void deleteOperationTemplate(String subTypeId, String deviceType, String operationCode) + public Set getOperationTemplateCodesByDeviceTypeAndSubTypeId(String deviceType, String subTypeId) throws OperationTemplateManagementDAOException { - String sql = "DELETE FROM SUB_OPERATION_TEMPLATE WHERE SUB_TYPE_ID = ? AND DEVICE_TYPE = ? AND OPERATION_CODE = ?"; + try { + String sql = "SELECT OPERATION_CODE FROM SUB_OPERATION_TEMPLATE WHERE SUB_TYPE_ID = ? AND DEVICE_TYPE = ?"; Connection conn = ConnectionManagerUtils.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setString(1, subTypeId); stmt.setString(2, deviceType); + + Set deviceTypes = new HashSet<>(); + try (ResultSet rs = stmt.executeQuery()) { + while (rs.next()) { + deviceTypes.add(rs.getString("OPERATION_CODE")); + } + return deviceTypes; + } + } catch (SQLException e) { + String msg = "Error occurred while getting operation template codes for sub type id : " + subTypeId + + " and device type : " + deviceType; + log.error(msg, e); + throw new OperationTemplateManagementDAOException(msg, e); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining DB connection to getting operation template codes."; + log.error(msg); + throw new OperationTemplateManagementDAOException(msg, e); + } + } + + + /** + * @param deviceType + * @return + * @throws OperationTemplateManagementDAOException + */ + @Override + public List getAllOperationTemplatesByDeviceType(String deviceType) + throws OperationTemplateManagementDAOException { + + try { + String sql = "SELECT * FROM SUB_OPERATION_TEMPLATE WHERE DEVICE_TYPE = ?"; + List operationTemplates = new ArrayList<>(); + + Connection conn = ConnectionManagerUtils.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, deviceType); + try (ResultSet rs = stmt.executeQuery()) { + while (rs.next()) { + operationTemplates.add(DAOUtil.loadOperationTemplate(rs)); + } + return operationTemplates; + } + } catch (SQLException e) { + String msg = "Error occurred while loading operation template list."; + log.error(e.getMessage()); + throw new OperationTemplateManagementDAOException(msg, e); + } + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining DB connection to loading operation template list."; + log.error(msg); + throw new OperationTemplateManagementDAOException(msg, e); + } + } + + /** + * @param subTypeId + * @param deviceType + * @param operationCode + * @throws OperationTemplateManagementDAOException + */ + @Override + public int deleteOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(String deviceType, String subTypeId, String operationCode) + throws OperationTemplateManagementDAOException { + String sql = "DELETE FROM SUB_OPERATION_TEMPLATE WHERE DEVICE_TYPE = ? AND SUB_TYPE_ID = ? AND OPERATION_CODE = ?"; + try { + Connection conn = ConnectionManagerUtils.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, deviceType); + stmt.setString(2, subTypeId); stmt.setString(3, operationCode); - stmt.executeUpdate(); + return stmt.executeUpdate(); } catch (SQLException e) { String msg = "Error occurred while deleting operation template for sub type id : " + subTypeId + " and operation code : " + operationCode; diff --git a/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/dao/impl/util/OperationTemplateManagementUtil.java b/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/dao/impl/util/OperationTemplateManagementUtil.java index 917288eb6c..b4ba139f6e 100644 --- a/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/dao/impl/util/OperationTemplateManagementUtil.java +++ b/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/dao/impl/util/OperationTemplateManagementUtil.java @@ -59,8 +59,18 @@ public class OperationTemplateManagementUtil { * @param operationCode * @return */ - public static String setOperationTemplateCacheKey(String subTypeId, String deviceType, String operationCode) { - return subTypeId + "|" + deviceType + "|" + operationCode; + public static String setOperationTemplateCacheKey(String deviceType, String subTypeId, String operationCode) { + return deviceType + "|" + subTypeId + "|" + operationCode; + } + + /** + * + * @param subTypeId + * @param deviceType + * @return + */ + public static String setOperationTemplateCacheKey(String deviceType, String subTypeId) { + return deviceType + "|" + subTypeId; } /** @@ -70,8 +80,8 @@ public class OperationTemplateManagementUtil { */ public static OperationTemplateCacheKey getOperationTemplateCacheKey(String key) { String[] keys = key.split("\\|"); - String subTypeId = keys[0];; - String deviceType = keys[1]; + String deviceType = keys[0];; + String subTypeId = keys[1]; String operationCode = keys[2]; OperationTemplateCacheKey operationTemplateCacheKey = new OperationTemplateCacheKey(); @@ -82,4 +92,22 @@ public class OperationTemplateManagementUtil { return operationTemplateCacheKey; } + /** + * + * @param key + * @return + */ + public static OperationTemplateCacheKey getOperationTemplateCodeCacheKey(String key) { + + String[] keys = key.split("\\|"); + String deviceType = keys[0];; + String subTypeId = keys[1]; + + OperationTemplateCacheKey operationTemplateCacheKey = new OperationTemplateCacheKey(); + operationTemplateCacheKey.setSubTypeId(subTypeId); + operationTemplateCacheKey.setDeviceType(deviceType); + + return operationTemplateCacheKey; + } + } diff --git a/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/impl/OperationTemplateServiceImpl.java b/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/impl/OperationTemplateServiceImpl.java index 80fa1ea139..f70330da49 100644 --- a/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/impl/OperationTemplateServiceImpl.java +++ b/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/impl/OperationTemplateServiceImpl.java @@ -22,6 +22,7 @@ import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; import io.entgra.device.mgt.core.operation.template.cache.OperationTemplateCacheLoader; +import io.entgra.device.mgt.core.operation.template.cache.OperationTemplateCodesCacheLoader; import io.entgra.device.mgt.core.operation.template.dao.OperationTemplateDAO; import io.entgra.device.mgt.core.operation.template.dao.OperationTemplateDAOFactory; import io.entgra.device.mgt.core.operation.template.dao.impl.util.OperationTemplateManagementUtil; @@ -35,6 +36,10 @@ import io.entgra.device.mgt.core.operation.template.util.AssertUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -46,6 +51,9 @@ public class OperationTemplateServiceImpl implements OperationTemplateService { private static final Log log = LogFactory.getLog(OperationTemplateServiceImpl.class); private static final LoadingCache operationTemplateCache = CacheBuilder.newBuilder() .expireAfterWrite(15, TimeUnit.MINUTES).build(new OperationTemplateCacheLoader()); + + private static final LoadingCache> operationTemplateCodeCache = CacheBuilder.newBuilder() + .expireAfterWrite(15, TimeUnit.MINUTES).build(new OperationTemplateCodesCacheLoader()); private final OperationTemplateDAO operationTemplateDAO; public OperationTemplateServiceImpl() { @@ -67,10 +75,6 @@ public class OperationTemplateServiceImpl implements OperationTemplateService { operationTemplateDAO.addOperationTemplate(operationTemplate); ConnectionManagerUtils.commitDBTransaction(); - String key = OperationTemplateManagementUtil.setOperationTemplateCacheKey( - operationTemplate.getSubTypeId(), operationTemplate.getDeviceType(), operationTemplate.getCode()); - operationTemplateCache.put(key, operationTemplate); - if (log.isDebugEnabled()) { String msg = "Operation Template added successfully,for subtype id " + operationTemplate.getSubTypeId() + " and operation code " @@ -82,6 +86,61 @@ public class OperationTemplateServiceImpl implements OperationTemplateService { throw new OperationTemplateMgtPluginException(e.getMessage(), e); } finally { ConnectionManagerUtils.closeDBConnection(); + addOperationTemplateDetailsForCacheLoader(operationTemplate); + } + } + + public void addOperationTemplateDetailsForCacheLoader(OperationTemplate operationTemplate) { + try { + String operationTemplateKey = OperationTemplateManagementUtil.setOperationTemplateCacheKey( + operationTemplate.getDeviceType(), operationTemplate.getSubTypeId(), operationTemplate.getCode()); + String operationTemplateCodeKey = OperationTemplateManagementUtil.setOperationTemplateCacheKey( + operationTemplate.getDeviceType(), operationTemplate.getSubTypeId()); + + operationTemplateCache.put(operationTemplateKey, operationTemplate); + + Set operationCodeList = operationTemplateCodeCache.get(operationTemplateCodeKey); + if (operationCodeList == null) { + operationCodeList = new HashSet<>(); + } + operationCodeList.add(operationTemplate.getCode()); + operationTemplateCodeCache.put(operationTemplateCodeKey, operationCodeList); + } catch (Exception e) { + log.error("Error occurred while adding operation template details for the cache loader", e); + } + } + + public void deleteOperationTemplateDetailsForCacheLoader(String deviceType, String subTypeId, String code) { + try { + String operationTemplateKey = OperationTemplateManagementUtil.setOperationTemplateCacheKey( + deviceType, subTypeId, code); + String operationTemplateCodeKey = OperationTemplateManagementUtil.setOperationTemplateCacheKey( + deviceType, subTypeId); + + operationTemplateCache.invalidate(operationTemplateKey); + operationTemplateCodeCache.invalidate(operationTemplateCodeKey); + } catch (Exception e) { + log.error("Error occurred removing operation template details for the cache loader", e); + } + } + + public void refreshOperationTemplateDetailsForCacheLoader(OperationTemplate operationTemplate) { + try { + if (operationTemplate != null) { + String operationTemplateKey = OperationTemplateManagementUtil.setOperationTemplateCacheKey(operationTemplate.getDeviceType(), + operationTemplate.getSubTypeId(), operationTemplate.getCode()); + String operationTemplateCodeKey = OperationTemplateManagementUtil.setOperationTemplateCacheKey( + operationTemplate.getDeviceType(), operationTemplate.getSubTypeId()); + + operationTemplateCache.put(operationTemplateKey, operationTemplate); + Set codeList = operationTemplateCodeCache.get(operationTemplateCodeKey); + codeList.remove(operationTemplate.getCode()); + operationTemplateCodeCache.put(operationTemplateCodeKey, codeList); + + + } + } catch (ExecutionException e) { + log.error("Error occurred while updating operation template cache loader"); } } @@ -102,6 +161,7 @@ public class OperationTemplateServiceImpl implements OperationTemplateService { updatedOperationTemplate = operationTemplateDAO.updateOperationTemplate( operationTemplate); ConnectionManagerUtils.commitDBTransaction(); + if (log.isDebugEnabled()) { String msg = "Operation Template updated successfully,for subtype id " + operationTemplate.getSubTypeId() + " and operation code " + operationTemplate.getCode() @@ -114,12 +174,7 @@ public class OperationTemplateServiceImpl implements OperationTemplateService { throw new OperationTemplateMgtPluginException(e.getMessage(), e); } finally { ConnectionManagerUtils.closeDBConnection(); - - if (updatedOperationTemplate != null) { - String key = OperationTemplateManagementUtil.setOperationTemplateCacheKey( - operationTemplate.getSubTypeId(), operationTemplate.getDeviceType(), operationTemplate.getCode()); - operationTemplateCache.refresh(key); - } + refreshOperationTemplateDetailsForCacheLoader(updatedOperationTemplate); } } @@ -131,12 +186,12 @@ public class OperationTemplateServiceImpl implements OperationTemplateService { * @throws OperationTemplateMgtPluginException */ @Override - public OperationTemplate getOperationTemplate(String subTypeId, String deviceType, String operationCode) + public OperationTemplate getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(String deviceType, String subTypeId, String operationCode) throws OperationTemplateMgtPluginException { try { validateGetOperationTemplate(subTypeId, deviceType, operationCode); - String key = OperationTemplateManagementUtil.setOperationTemplateCacheKey(subTypeId, deviceType, + String key = OperationTemplateManagementUtil.setOperationTemplateCacheKey(deviceType, subTypeId, operationCode); return operationTemplateCache.get(key); } catch (ExecutionException e) { @@ -150,6 +205,27 @@ public class OperationTemplateServiceImpl implements OperationTemplateService { } } + /** + * @param deviceType + * @return + * @throws OperationTemplateMgtPluginException + */ + @Override + public List getAllOperationTemplatesByDeviceType(String deviceType) + throws OperationTemplateMgtPluginException { + AssertUtils.hasText(deviceType, "Invalid device type."); + try { + ConnectionManagerUtils.openDBConnection(); + return operationTemplateDAO.getAllOperationTemplatesByDeviceType(deviceType); + } catch (DBConnectionException | OperationTemplateManagementDAOException e) { + log.error(e.getMessage()); + throw new OperationTemplateMgtPluginException(e.getMessage(), e); + } finally { + ConnectionManagerUtils.closeDBConnection(); + + } + } + /** * @param subTypeId * @param deviceType @@ -157,17 +233,18 @@ public class OperationTemplateServiceImpl implements OperationTemplateService { * @throws OperationTemplateMgtPluginException */ @Override - public void deleteOperationTemplate(String subTypeId, String deviceType, String operationCode) + public void deleteOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(String deviceType, String subTypeId, String operationCode) throws OperationTemplateMgtPluginException { String msg = "Operation Template does not exist for subtype id : " + subTypeId + " and device type : " + deviceType + " and operation code : " + operationCode; - AssertUtils.isNull(getOperationTemplate(subTypeId, deviceType, operationCode), msg); + AssertUtils.isNull(getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(deviceType, subTypeId, operationCode), msg); + int deleted = 0; try { ConnectionManagerUtils.beginDBTransaction(); - operationTemplateDAO.deleteOperationTemplate(subTypeId, deviceType, operationCode); + deleted = operationTemplateDAO.deleteOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(deviceType, subTypeId, operationCode); ConnectionManagerUtils.commitDBTransaction(); if (log.isDebugEnabled()) { String debugMsg = "Operation Template deleted successfully,for subtype id " @@ -175,15 +252,43 @@ public class OperationTemplateServiceImpl implements OperationTemplateService { + operationCode + ""; log.debug(debugMsg); } - String key = OperationTemplateManagementUtil.setOperationTemplateCacheKey( - subTypeId, deviceType, operationCode); - operationTemplateCache.invalidate(key); } catch (DBConnectionException | OperationTemplateManagementDAOException e) { log.error(e.getMessage()); throw new OperationTemplateMgtPluginException(e.getMessage(), e); } finally { ConnectionManagerUtils.closeDBConnection(); + if (deleted == 1) { + deleteOperationTemplateDetailsForCacheLoader(deviceType, subTypeId, operationCode); + } + + } + } + /** + * @param deviceType + * @param subTypeId + * @return + * @throws OperationTemplateMgtPluginException + */ + @Override + public Set getOperationTemplateCodesByDeviceTypeAndSubTypeId(String deviceType, String subTypeId) + throws OperationTemplateMgtPluginException { + + try { + AssertUtils.hasText(subTypeId, "Invalid meter device subtype id: " + subTypeId); + AssertUtils.isTrue(Integer.valueOf(subTypeId)>0, "Invalid meter device subtype id: " + subTypeId); + AssertUtils.hasText(deviceType, "Invalid device type."); + + String key = OperationTemplateManagementUtil.setOperationTemplateCacheKey(deviceType, subTypeId); + return operationTemplateCodeCache.get(key); + } catch (ExecutionException e) { + log.error(e.getMessage()); + throw new OperationTemplateMgtPluginException(e.getMessage(), e); + } catch (CacheLoader.InvalidCacheLoadException e) { + String msg = "Operation Template codes doesn't exist for subtype id : " + subTypeId + " and device type : " + + deviceType; + log.error(msg, e); + return null; } } @@ -216,7 +321,7 @@ public class OperationTemplateServiceImpl implements OperationTemplateService { String msg = "Operation Template already exist for subtype id : " + operationTemplate.getSubTypeId() + " and device type : " + operationTemplate.getDeviceType() + " and operation code : " + operationTemplate.getCode(); - AssertUtils.notNull(getOperationTemplate(operationTemplate.getSubTypeId(), operationTemplate.getDeviceType(), + AssertUtils.notNull(getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(operationTemplate.getDeviceType(), operationTemplate.getSubTypeId(), operationTemplate.getCode()), msg); } @@ -232,7 +337,7 @@ public class OperationTemplateServiceImpl implements OperationTemplateService { String msg = "Operation Template does not exist for subtype id : " + operationTemplate.getSubTypeId() + " and device type : " + operationTemplate.getDeviceType() + " and operation code : " + operationTemplate.getCode(); - AssertUtils.isNull(getOperationTemplate(operationTemplate.getSubTypeId(), operationTemplate.getDeviceType(), + AssertUtils.isNull(getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(operationTemplate.getDeviceType(), operationTemplate.getSubTypeId(), operationTemplate.getCode()), msg); } diff --git a/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/spi/OperationTemplateService.java b/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/spi/OperationTemplateService.java index 659509b34b..332bc585d6 100644 --- a/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/spi/OperationTemplateService.java +++ b/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/spi/OperationTemplateService.java @@ -21,6 +21,9 @@ package io.entgra.device.mgt.core.operation.template.spi; import io.entgra.device.mgt.core.operation.template.dto.OperationTemplate; import io.entgra.device.mgt.core.operation.template.exception.OperationTemplateMgtPluginException; +import java.util.List; +import java.util.Set; + /** * Operation Template service interface. */ @@ -30,8 +33,11 @@ public interface OperationTemplateService { OperationTemplate updateOperationTemplate(OperationTemplate operationTemplate) throws OperationTemplateMgtPluginException; - OperationTemplate getOperationTemplate(String subTypeId, String deviceType, String operationCode) throws OperationTemplateMgtPluginException; + OperationTemplate getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(String deviceType, String subTypeId, String operationCode) throws OperationTemplateMgtPluginException; + + List getAllOperationTemplatesByDeviceType(String deviceType) throws OperationTemplateMgtPluginException; - void deleteOperationTemplate(String subTypeId, String deviceType, String operationCode) throws OperationTemplateMgtPluginException; + void deleteOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(String deviceType, String subTypeId, String operationCode) throws OperationTemplateMgtPluginException; + Set getOperationTemplateCodesByDeviceTypeAndSubTypeId(String deviceType, String subTypeId) throws OperationTemplateMgtPluginException; } diff --git a/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/util/DAOUtil.java b/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/util/DAOUtil.java index 18edcf7c8c..9953fad0b0 100644 --- a/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/util/DAOUtil.java +++ b/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/main/java/io/entgra/device/mgt/core/operation/template/util/DAOUtil.java @@ -44,7 +44,6 @@ public class DAOUtil { public static OperationTemplate loadOperationTemplate(ResultSet rs) throws SQLException, JsonSyntaxException { OperationTemplate operationTemplate = new OperationTemplate(); - Gson g = new Gson(); operationTemplate.setSubTypeId(rs.getString("SUB_TYPE_ID")); operationTemplate.setCode(rs.getString("OPERATION_CODE")); operationTemplate.setDeviceType(rs.getString("DEVICE_TYPE")); diff --git a/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/test/java/io/entgra/device/mgt/core/operation/template/DAOTest.java b/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/test/java/io/entgra/device/mgt/core/operation/template/DAOTest.java index c993cc435e..95594ad92d 100644 --- a/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/test/java/io/entgra/device/mgt/core/operation/template/DAOTest.java +++ b/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/test/java/io/entgra/device/mgt/core/operation/template/DAOTest.java @@ -48,8 +48,8 @@ public class DAOTest extends BaseOperationTemplatePluginTest { throws DBConnectionException, OperationTemplateManagementDAOException { ConnectionManagerUtils.openDBConnection(); - OperationTemplate operationTemplateActual = operationTemplateDAO.getOperationTemplate( - "4", TestUtils.deviceType, TestUtils.operationCode); + OperationTemplate operationTemplateActual = operationTemplateDAO.getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode( + TestUtils.deviceType, "4", TestUtils.operationCode); ConnectionManagerUtils.closeDBConnection(); Assert.assertNotNull(operationTemplateActual, "Cannot be null"); Assert.assertEquals(operationTemplateActual.getSubTypeId(), "4"); @@ -72,8 +72,8 @@ public class DAOTest extends BaseOperationTemplatePluginTest { operationTemplateDAO.addOperationTemplate(operationTemplate); ConnectionManagerUtils.commitDBTransaction(); - OperationTemplate operationTemplateActual = operationTemplateDAO.getOperationTemplate( - "4", TestUtils.deviceType, TestUtils.operationCode); + OperationTemplate operationTemplateActual = operationTemplateDAO.getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode( + TestUtils.deviceType, "4", TestUtils.operationCode); ConnectionManagerUtils.closeDBConnection(); Assert.assertNotNull(operationTemplateActual, "Cannot be null"); Assert.assertEquals(operationTemplateActual.getSubTypeId(), "4"); @@ -86,8 +86,8 @@ public class DAOTest extends BaseOperationTemplatePluginTest { throws DBConnectionException, OperationTemplateManagementDAOException { ConnectionManagerUtils.beginDBTransaction(); - OperationTemplate operationTemplate = operationTemplateDAO.getOperationTemplate( - "4", TestUtils.deviceType, TestUtils.operationCode); + OperationTemplate operationTemplate = operationTemplateDAO.getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode( + TestUtils.deviceType, "4", TestUtils.operationCode); OperationTemplate operationTemplateActual = operationTemplateDAO.updateOperationTemplate( operationTemplate); ConnectionManagerUtils.commitDBTransaction(); diff --git a/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/test/java/io/entgra/device/mgt/core/operation/template/ServiceTest.java b/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/test/java/io/entgra/device/mgt/core/operation/template/ServiceTest.java index e47fd7c1dd..96872a2407 100644 --- a/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/test/java/io/entgra/device/mgt/core/operation/template/ServiceTest.java +++ b/components/operation-template-mgt/io.entgra.device.mgt.core.operation.template/src/test/java/io/entgra/device/mgt/core/operation/template/ServiceTest.java @@ -29,6 +29,9 @@ import org.testng.Assert; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; +import java.util.List; +import java.util.Set; + public class ServiceTest extends BaseOperationTemplatePluginTest { private static final Log log = LogFactory.getLog(ServiceTest.class); @@ -43,7 +46,8 @@ public class ServiceTest extends BaseOperationTemplatePluginTest { @Test(dependsOnMethods = "testAddOperationTemplate") public void testGetOperationTemplate() throws OperationTemplateMgtPluginException { - OperationTemplate operationTemplateActual = operationTemplateService.getOperationTemplate(TestUtils.subtypeId, TestUtils.deviceType, TestUtils.operationCode); + OperationTemplate operationTemplateActual = operationTemplateService.getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode( + TestUtils.deviceType, TestUtils.subtypeId, TestUtils.operationCode); Assert.assertEquals(operationTemplateActual.getSubTypeId(), operationTemplateActual.getSubTypeId()); Assert.assertEquals(operationTemplateActual.getCode(), TestUtils.operationCode); Assert.assertEquals(operationTemplateActual.getDeviceType(), TestUtils.deviceType); @@ -59,7 +63,7 @@ public class ServiceTest extends BaseOperationTemplatePluginTest { operationTemplate.setOperationDefinition(TestUtils.getOperationDefinition(TestUtils.subtypeId, TestUtils.operationCode)); operationTemplateService.addOperationTemplate(operationTemplate); - OperationTemplate operationTemplateActual = operationTemplateService.getOperationTemplate(TestUtils.subtypeId, TestUtils.deviceType, TestUtils.operationCode); + OperationTemplate operationTemplateActual = operationTemplateService.getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(TestUtils.deviceType, TestUtils.subtypeId, TestUtils.operationCode); Assert.assertNotNull(operationTemplateActual, "Cannot be null"); Assert.assertEquals(operationTemplateActual.getOperationDefinition(), TestUtils.getOperationDefinition(TestUtils.subtypeId, TestUtils.operationCode)); Assert.assertEquals(operationTemplateActual.getSubTypeId(), operationTemplateActual.getSubTypeId()); @@ -70,7 +74,7 @@ public class ServiceTest extends BaseOperationTemplatePluginTest { @Test(dependsOnMethods = "testAddOperationTemplate") public void testUpdateOperationTemplate() throws OperationTemplateMgtPluginException { - OperationTemplate operationTemplate = operationTemplateService.getOperationTemplate(TestUtils.subtypeId, TestUtils.deviceType, TestUtils.operationCode); + OperationTemplate operationTemplate = operationTemplateService.getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(TestUtils.deviceType, TestUtils.subtypeId, TestUtils.operationCode); operationTemplate.setOperationDefinition("{}"); OperationTemplate operationTemplateActual = operationTemplateService.updateOperationTemplate(operationTemplate); @@ -81,13 +85,30 @@ public class ServiceTest extends BaseOperationTemplatePluginTest { Assert.assertEquals(operationTemplateActual.getDeviceType(), TestUtils.deviceType); } - @Test(dependsOnMethods = {"testAddOperationTemplate", "testGetOperationTemplate", "testUpdateOperationTemplate"}) - public void testDeleteOperationTemplate() throws OperationTemplateMgtPluginException { - operationTemplateService.deleteOperationTemplate(TestUtils.subtypeId, TestUtils.deviceType, TestUtils.operationCode); - Assert.assertNull(getOperationTemplateBySubtypeIdAndDeviceTypeAndOperationCode(TestUtils.subtypeId, TestUtils.deviceType, TestUtils.operationCode)); + + public OperationTemplate getOperationTemplateBySubtypeIdAndDeviceTypeAndOperationCode(String deviceType, String subtypeId, String operationCode) throws OperationTemplateMgtPluginException { + return operationTemplateService.getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(deviceType, subtypeId, operationCode); + } + + @Test(dependsOnMethods = "testAddOperationTemplate") + public void testGetOperationTemplateCodesByDeviceTypeAndSubTypeId() throws OperationTemplateMgtPluginException { + + Set operationCodes = operationTemplateService.getOperationTemplateCodesByDeviceTypeAndSubTypeId(TestUtils.deviceType, TestUtils.subtypeId); + Assert.assertNotNull(operationCodes, "Cannot be null"); + } + + @Test(dependsOnMethods = "testAddOperationTemplate") + public void testGetAllOperationTemplatesByDeviceType() throws OperationTemplateMgtPluginException { + + List operationTemplates = operationTemplateService.getAllOperationTemplatesByDeviceType(TestUtils.deviceType); + Assert.assertNotNull(operationTemplates, "Cannot be null"); + Assert.assertFalse(operationTemplates.isEmpty(), "operationTemplates is empty"); } - public OperationTemplate getOperationTemplateBySubtypeIdAndDeviceTypeAndOperationCode(String subtypeId, String deviceType, String operationCode) throws OperationTemplateMgtPluginException { - return operationTemplateService.getOperationTemplate(subtypeId, deviceType, operationCode); + @Test(dependsOnMethods = {"testAddOperationTemplate", "testGetOperationTemplate", "testUpdateOperationTemplate", + "testGetOperationTemplateCodesByDeviceTypeAndSubTypeId", "testGetAllOperationTemplatesByDeviceType"}) + public void testDeleteOperationTemplate() throws OperationTemplateMgtPluginException { + operationTemplateService.deleteOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(TestUtils.deviceType, TestUtils.subtypeId, TestUtils.operationCode); + Assert.assertNull(getOperationTemplateBySubtypeIdAndDeviceTypeAndOperationCode(TestUtils.deviceType, TestUtils.subtypeId, TestUtils.operationCode)); } }