Modify operation template

remotes/1712966534876109980/master
Thameera 11 months ago
parent 4ba0be5510
commit c071b9de01

@ -65,7 +65,7 @@ public class OperationTemplateCacheLoader extends CacheLoader<String, OperationT
} }
try { try {
ConnectionManagerUtils.openDBConnection(); ConnectionManagerUtils.openDBConnection();
return operationTemplateDAO.getOperationTemplate(subTypeId, deviceType, operationCode); return operationTemplateDAO.getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(deviceType, subTypeId, operationCode);
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
String msg = String msg =
"Error occurred while obtaining the database connection to retrieve operation template for " "Error occurred while obtaining the database connection to retrieve operation template for "

@ -0,0 +1,93 @@
/*
* 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.operation.template.cache;
import com.google.common.cache.CacheLoader;
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;
import io.entgra.device.mgt.core.operation.template.dto.OperationTemplateCacheKey;
import io.entgra.device.mgt.core.operation.template.exception.DBConnectionException;
import io.entgra.device.mgt.core.operation.template.exception.OperationTemplateManagementDAOException;
import io.entgra.device.mgt.core.operation.template.exception.OperationTemplateMgtPluginException;
import io.entgra.device.mgt.core.operation.template.util.ConnectionManagerUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.List;
import java.util.Set;
/**
* Class for the Operation Template cache.
*/
public class OperationTemplateCodesCacheLoader extends CacheLoader<String, Set<String>> {
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<String> 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();
}
}
}

@ -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.dto.OperationTemplate;
import io.entgra.device.mgt.core.operation.template.exception.OperationTemplateManagementDAOException; 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 * This class represents the key operations associated with persisting mobile-device related
* information. * information.
@ -31,8 +34,11 @@ public interface OperationTemplateDAO {
OperationTemplate updateOperationTemplate(OperationTemplate operationTemplate) OperationTemplate updateOperationTemplate(OperationTemplate operationTemplate)
throws OperationTemplateManagementDAOException; throws OperationTemplateManagementDAOException;
OperationTemplate getOperationTemplate(String subTypeId, String deviceType, String operationCode) throws OperationTemplateManagementDAOException; OperationTemplate getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(String deviceType, String subTypeId, String operationCode) throws OperationTemplateManagementDAOException;
List<OperationTemplate> 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<String> getOperationTemplateCodesByDeviceTypeAndSubTypeId(String deviceType, String subTypeId) throws OperationTemplateManagementDAOException;
} }

@ -31,6 +31,10 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Timestamp; 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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -102,7 +106,7 @@ public class OperationTemplateDAOImpl implements OperationTemplateDAO {
stmt.setString(5, operationTemplate.getCode()); stmt.setString(5, operationTemplate.getCode());
stmt.executeUpdate(); stmt.executeUpdate();
return getOperationTemplate(operationTemplate.getSubTypeId(), operationTemplate.getDeviceType(), operationTemplate.getCode()); return getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(operationTemplate.getDeviceType(), operationTemplate.getSubTypeId(), operationTemplate.getCode());
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while processing update operation template."; String msg = "Error occurred while processing update operation template.";
log.error(msg); log.error(msg);
@ -122,14 +126,14 @@ public class OperationTemplateDAOImpl implements OperationTemplateDAO {
* @throws OperationTemplateManagementDAOException * @throws OperationTemplateManagementDAOException
*/ */
@Override @Override
public OperationTemplate getOperationTemplate(String subTypeId, String deviceType, String operationCode) public OperationTemplate getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(String deviceType, String subTypeId, String operationCode)
throws OperationTemplateManagementDAOException { throws OperationTemplateManagementDAOException {
try { 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(); Connection conn = ConnectionManagerUtils.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, subTypeId); stmt.setString(1, deviceType);
stmt.setString(2, deviceType); stmt.setString(2, subTypeId);
stmt.setString(3, operationCode); stmt.setString(3, operationCode);
try (ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
@ -151,22 +155,94 @@ public class OperationTemplateDAOImpl implements OperationTemplateDAO {
} }
/** /**
* @param subTypeId
* @param deviceType * @param deviceType
* @param operationCode * @param subTypeId
* @return
* @throws OperationTemplateManagementDAOException * @throws OperationTemplateManagementDAOException
*/ */
@Override @Override
public void deleteOperationTemplate(String subTypeId, String deviceType, String operationCode) public Set<String> getOperationTemplateCodesByDeviceTypeAndSubTypeId(String deviceType, String subTypeId)
throws OperationTemplateManagementDAOException { throws OperationTemplateManagementDAOException {
String sql = "DELETE FROM SUB_OPERATION_TEMPLATE WHERE SUB_TYPE_ID = ? AND DEVICE_TYPE = ? AND OPERATION_CODE = ?";
try { try {
String sql = "SELECT OPERATION_CODE FROM SUB_OPERATION_TEMPLATE WHERE SUB_TYPE_ID = ? AND DEVICE_TYPE = ?";
Connection conn = ConnectionManagerUtils.getDBConnection(); Connection conn = ConnectionManagerUtils.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, subTypeId); stmt.setString(1, subTypeId);
stmt.setString(2, deviceType); stmt.setString(2, deviceType);
Set<String> 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<OperationTemplate> getAllOperationTemplatesByDeviceType(String deviceType)
throws OperationTemplateManagementDAOException {
try {
String sql = "SELECT * FROM SUB_OPERATION_TEMPLATE WHERE DEVICE_TYPE = ?";
List<OperationTemplate> 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.setString(3, operationCode);
stmt.executeUpdate(); return stmt.executeUpdate();
} catch (SQLException e) { } catch (SQLException e) {
String msg = "Error occurred while deleting operation template for sub type id : " + subTypeId String msg = "Error occurred while deleting operation template for sub type id : " + subTypeId
+ " and operation code : " + operationCode; + " and operation code : " + operationCode;

@ -59,8 +59,18 @@ public class OperationTemplateManagementUtil {
* @param operationCode * @param operationCode
* @return * @return
*/ */
public static String setOperationTemplateCacheKey(String subTypeId, String deviceType, String operationCode) { public static String setOperationTemplateCacheKey(String deviceType, String subTypeId, String operationCode) {
return subTypeId + "|" + deviceType + "|" + 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) { public static OperationTemplateCacheKey getOperationTemplateCacheKey(String key) {
String[] keys = key.split("\\|"); String[] keys = key.split("\\|");
String subTypeId = keys[0];; String deviceType = keys[0];;
String deviceType = keys[1]; String subTypeId = keys[1];
String operationCode = keys[2]; String operationCode = keys[2];
OperationTemplateCacheKey operationTemplateCacheKey = new OperationTemplateCacheKey(); OperationTemplateCacheKey operationTemplateCacheKey = new OperationTemplateCacheKey();
@ -82,4 +92,22 @@ public class OperationTemplateManagementUtil {
return operationTemplateCacheKey; 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;
}
} }

@ -22,6 +22,7 @@ import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader; import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache; 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.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.OperationTemplateDAO;
import io.entgra.device.mgt.core.operation.template.dao.OperationTemplateDAOFactory; import io.entgra.device.mgt.core.operation.template.dao.OperationTemplateDAOFactory;
import io.entgra.device.mgt.core.operation.template.dao.impl.util.OperationTemplateManagementUtil; 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.Log;
import org.apache.commons.logging.LogFactory; 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.ExecutionException;
import java.util.concurrent.TimeUnit; 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 Log log = LogFactory.getLog(OperationTemplateServiceImpl.class);
private static final LoadingCache<String, OperationTemplate> operationTemplateCache = CacheBuilder.newBuilder() private static final LoadingCache<String, OperationTemplate> operationTemplateCache = CacheBuilder.newBuilder()
.expireAfterWrite(15, TimeUnit.MINUTES).build(new OperationTemplateCacheLoader()); .expireAfterWrite(15, TimeUnit.MINUTES).build(new OperationTemplateCacheLoader());
private static final LoadingCache<String, Set<String>> operationTemplateCodeCache = CacheBuilder.newBuilder()
.expireAfterWrite(15, TimeUnit.MINUTES).build(new OperationTemplateCodesCacheLoader());
private final OperationTemplateDAO operationTemplateDAO; private final OperationTemplateDAO operationTemplateDAO;
public OperationTemplateServiceImpl() { public OperationTemplateServiceImpl() {
@ -67,10 +75,6 @@ public class OperationTemplateServiceImpl implements OperationTemplateService {
operationTemplateDAO.addOperationTemplate(operationTemplate); operationTemplateDAO.addOperationTemplate(operationTemplate);
ConnectionManagerUtils.commitDBTransaction(); ConnectionManagerUtils.commitDBTransaction();
String key = OperationTemplateManagementUtil.setOperationTemplateCacheKey(
operationTemplate.getSubTypeId(), operationTemplate.getDeviceType(), operationTemplate.getCode());
operationTemplateCache.put(key, operationTemplate);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
String msg = "Operation Template added successfully,for subtype id " String msg = "Operation Template added successfully,for subtype id "
+ operationTemplate.getSubTypeId() + " and operation code " + operationTemplate.getSubTypeId() + " and operation code "
@ -82,6 +86,61 @@ public class OperationTemplateServiceImpl implements OperationTemplateService {
throw new OperationTemplateMgtPluginException(e.getMessage(), e); throw new OperationTemplateMgtPluginException(e.getMessage(), e);
} finally { } finally {
ConnectionManagerUtils.closeDBConnection(); 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<String> 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<String> 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( updatedOperationTemplate = operationTemplateDAO.updateOperationTemplate(
operationTemplate); operationTemplate);
ConnectionManagerUtils.commitDBTransaction(); ConnectionManagerUtils.commitDBTransaction();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
String msg = "Operation Template updated successfully,for subtype id " String msg = "Operation Template updated successfully,for subtype id "
+ operationTemplate.getSubTypeId() + " and operation code " + operationTemplate.getCode() + operationTemplate.getSubTypeId() + " and operation code " + operationTemplate.getCode()
@ -114,12 +174,7 @@ public class OperationTemplateServiceImpl implements OperationTemplateService {
throw new OperationTemplateMgtPluginException(e.getMessage(), e); throw new OperationTemplateMgtPluginException(e.getMessage(), e);
} finally { } finally {
ConnectionManagerUtils.closeDBConnection(); ConnectionManagerUtils.closeDBConnection();
refreshOperationTemplateDetailsForCacheLoader(updatedOperationTemplate);
if (updatedOperationTemplate != null) {
String key = OperationTemplateManagementUtil.setOperationTemplateCacheKey(
operationTemplate.getSubTypeId(), operationTemplate.getDeviceType(), operationTemplate.getCode());
operationTemplateCache.refresh(key);
}
} }
} }
@ -131,12 +186,12 @@ public class OperationTemplateServiceImpl implements OperationTemplateService {
* @throws OperationTemplateMgtPluginException * @throws OperationTemplateMgtPluginException
*/ */
@Override @Override
public OperationTemplate getOperationTemplate(String subTypeId, String deviceType, String operationCode) public OperationTemplate getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(String deviceType, String subTypeId, String operationCode)
throws OperationTemplateMgtPluginException { throws OperationTemplateMgtPluginException {
try { try {
validateGetOperationTemplate(subTypeId, deviceType, operationCode); validateGetOperationTemplate(subTypeId, deviceType, operationCode);
String key = OperationTemplateManagementUtil.setOperationTemplateCacheKey(subTypeId, deviceType, String key = OperationTemplateManagementUtil.setOperationTemplateCacheKey(deviceType, subTypeId,
operationCode); operationCode);
return operationTemplateCache.get(key); return operationTemplateCache.get(key);
} catch (ExecutionException e) { } catch (ExecutionException e) {
@ -150,6 +205,27 @@ public class OperationTemplateServiceImpl implements OperationTemplateService {
} }
} }
/**
* @param deviceType
* @return
* @throws OperationTemplateMgtPluginException
*/
@Override
public List<OperationTemplate> 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 subTypeId
* @param deviceType * @param deviceType
@ -157,17 +233,18 @@ public class OperationTemplateServiceImpl implements OperationTemplateService {
* @throws OperationTemplateMgtPluginException * @throws OperationTemplateMgtPluginException
*/ */
@Override @Override
public void deleteOperationTemplate(String subTypeId, String deviceType, String operationCode) public void deleteOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(String deviceType, String subTypeId, String operationCode)
throws OperationTemplateMgtPluginException { throws OperationTemplateMgtPluginException {
String msg = "Operation Template does not exist for subtype id : " + subTypeId String msg = "Operation Template does not exist for subtype id : " + subTypeId
+ " and device type : " + deviceType + " and operation code : " + " and device type : " + deviceType + " and operation code : "
+ operationCode; + operationCode;
AssertUtils.isNull(getOperationTemplate(subTypeId, deviceType, operationCode), msg); AssertUtils.isNull(getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(deviceType, subTypeId, operationCode), msg);
int deleted = 0;
try { try {
ConnectionManagerUtils.beginDBTransaction(); ConnectionManagerUtils.beginDBTransaction();
operationTemplateDAO.deleteOperationTemplate(subTypeId, deviceType, operationCode); deleted = operationTemplateDAO.deleteOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(deviceType, subTypeId, operationCode);
ConnectionManagerUtils.commitDBTransaction(); ConnectionManagerUtils.commitDBTransaction();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
String debugMsg = "Operation Template deleted successfully,for subtype id " String debugMsg = "Operation Template deleted successfully,for subtype id "
@ -175,15 +252,43 @@ public class OperationTemplateServiceImpl implements OperationTemplateService {
+ operationCode + ""; + operationCode + "";
log.debug(debugMsg); log.debug(debugMsg);
} }
String key = OperationTemplateManagementUtil.setOperationTemplateCacheKey(
subTypeId, deviceType, operationCode);
operationTemplateCache.invalidate(key);
} catch (DBConnectionException | OperationTemplateManagementDAOException e) { } catch (DBConnectionException | OperationTemplateManagementDAOException e) {
log.error(e.getMessage()); log.error(e.getMessage());
throw new OperationTemplateMgtPluginException(e.getMessage(), e); throw new OperationTemplateMgtPluginException(e.getMessage(), e);
} finally { } finally {
ConnectionManagerUtils.closeDBConnection(); ConnectionManagerUtils.closeDBConnection();
if (deleted == 1) {
deleteOperationTemplateDetailsForCacheLoader(deviceType, subTypeId, operationCode);
}
}
}
/**
* @param deviceType
* @param subTypeId
* @return
* @throws OperationTemplateMgtPluginException
*/
@Override
public Set<String> 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() String msg = "Operation Template already exist for subtype id : " + operationTemplate.getSubTypeId()
+ " and device type : " + operationTemplate.getDeviceType() + " and operation code : " + " and device type : " + operationTemplate.getDeviceType() + " and operation code : "
+ operationTemplate.getCode(); + operationTemplate.getCode();
AssertUtils.notNull(getOperationTemplate(operationTemplate.getSubTypeId(), operationTemplate.getDeviceType(), AssertUtils.notNull(getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(operationTemplate.getDeviceType(), operationTemplate.getSubTypeId(),
operationTemplate.getCode()), msg); operationTemplate.getCode()), msg);
} }
@ -232,7 +337,7 @@ public class OperationTemplateServiceImpl implements OperationTemplateService {
String msg = "Operation Template does not exist for subtype id : " + operationTemplate.getSubTypeId() String msg = "Operation Template does not exist for subtype id : " + operationTemplate.getSubTypeId()
+ " and device type : " + operationTemplate.getDeviceType() + " and operation code : " + " and device type : " + operationTemplate.getDeviceType() + " and operation code : "
+ operationTemplate.getCode(); + operationTemplate.getCode();
AssertUtils.isNull(getOperationTemplate(operationTemplate.getSubTypeId(), operationTemplate.getDeviceType(), AssertUtils.isNull(getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(operationTemplate.getDeviceType(), operationTemplate.getSubTypeId(),
operationTemplate.getCode()), msg); operationTemplate.getCode()), msg);
} }

@ -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.dto.OperationTemplate;
import io.entgra.device.mgt.core.operation.template.exception.OperationTemplateMgtPluginException; import io.entgra.device.mgt.core.operation.template.exception.OperationTemplateMgtPluginException;
import java.util.List;
import java.util.Set;
/** /**
* Operation Template service interface. * Operation Template service interface.
*/ */
@ -30,8 +33,11 @@ public interface OperationTemplateService {
OperationTemplate updateOperationTemplate(OperationTemplate operationTemplate) throws OperationTemplateMgtPluginException; 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<OperationTemplate> 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<String> getOperationTemplateCodesByDeviceTypeAndSubTypeId(String deviceType, String subTypeId) throws OperationTemplateMgtPluginException;
} }

@ -44,7 +44,6 @@ public class DAOUtil {
public static OperationTemplate loadOperationTemplate(ResultSet rs) public static OperationTemplate loadOperationTemplate(ResultSet rs)
throws SQLException, JsonSyntaxException { throws SQLException, JsonSyntaxException {
OperationTemplate operationTemplate = new OperationTemplate(); OperationTemplate operationTemplate = new OperationTemplate();
Gson g = new Gson();
operationTemplate.setSubTypeId(rs.getString("SUB_TYPE_ID")); operationTemplate.setSubTypeId(rs.getString("SUB_TYPE_ID"));
operationTemplate.setCode(rs.getString("OPERATION_CODE")); operationTemplate.setCode(rs.getString("OPERATION_CODE"));
operationTemplate.setDeviceType(rs.getString("DEVICE_TYPE")); operationTemplate.setDeviceType(rs.getString("DEVICE_TYPE"));

@ -48,8 +48,8 @@ public class DAOTest extends BaseOperationTemplatePluginTest {
throws DBConnectionException, OperationTemplateManagementDAOException { throws DBConnectionException, OperationTemplateManagementDAOException {
ConnectionManagerUtils.openDBConnection(); ConnectionManagerUtils.openDBConnection();
OperationTemplate operationTemplateActual = operationTemplateDAO.getOperationTemplate( OperationTemplate operationTemplateActual = operationTemplateDAO.getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(
"4", TestUtils.deviceType, TestUtils.operationCode); TestUtils.deviceType, "4", TestUtils.operationCode);
ConnectionManagerUtils.closeDBConnection(); ConnectionManagerUtils.closeDBConnection();
Assert.assertNotNull(operationTemplateActual, "Cannot be null"); Assert.assertNotNull(operationTemplateActual, "Cannot be null");
Assert.assertEquals(operationTemplateActual.getSubTypeId(), "4"); Assert.assertEquals(operationTemplateActual.getSubTypeId(), "4");
@ -72,8 +72,8 @@ public class DAOTest extends BaseOperationTemplatePluginTest {
operationTemplateDAO.addOperationTemplate(operationTemplate); operationTemplateDAO.addOperationTemplate(operationTemplate);
ConnectionManagerUtils.commitDBTransaction(); ConnectionManagerUtils.commitDBTransaction();
OperationTemplate operationTemplateActual = operationTemplateDAO.getOperationTemplate( OperationTemplate operationTemplateActual = operationTemplateDAO.getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(
"4", TestUtils.deviceType, TestUtils.operationCode); TestUtils.deviceType, "4", TestUtils.operationCode);
ConnectionManagerUtils.closeDBConnection(); ConnectionManagerUtils.closeDBConnection();
Assert.assertNotNull(operationTemplateActual, "Cannot be null"); Assert.assertNotNull(operationTemplateActual, "Cannot be null");
Assert.assertEquals(operationTemplateActual.getSubTypeId(), "4"); Assert.assertEquals(operationTemplateActual.getSubTypeId(), "4");
@ -86,8 +86,8 @@ public class DAOTest extends BaseOperationTemplatePluginTest {
throws DBConnectionException, OperationTemplateManagementDAOException { throws DBConnectionException, OperationTemplateManagementDAOException {
ConnectionManagerUtils.beginDBTransaction(); ConnectionManagerUtils.beginDBTransaction();
OperationTemplate operationTemplate = operationTemplateDAO.getOperationTemplate( OperationTemplate operationTemplate = operationTemplateDAO.getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(
"4", TestUtils.deviceType, TestUtils.operationCode); TestUtils.deviceType, "4", TestUtils.operationCode);
OperationTemplate operationTemplateActual = operationTemplateDAO.updateOperationTemplate( OperationTemplate operationTemplateActual = operationTemplateDAO.updateOperationTemplate(
operationTemplate); operationTemplate);
ConnectionManagerUtils.commitDBTransaction(); ConnectionManagerUtils.commitDBTransaction();

@ -29,6 +29,9 @@ import org.testng.Assert;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.util.List;
import java.util.Set;
public class ServiceTest extends BaseOperationTemplatePluginTest { public class ServiceTest extends BaseOperationTemplatePluginTest {
private static final Log log = LogFactory.getLog(ServiceTest.class); private static final Log log = LogFactory.getLog(ServiceTest.class);
@ -43,7 +46,8 @@ public class ServiceTest extends BaseOperationTemplatePluginTest {
@Test(dependsOnMethods = "testAddOperationTemplate") @Test(dependsOnMethods = "testAddOperationTemplate")
public void testGetOperationTemplate() throws OperationTemplateMgtPluginException { 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.getSubTypeId(), operationTemplateActual.getSubTypeId());
Assert.assertEquals(operationTemplateActual.getCode(), TestUtils.operationCode); Assert.assertEquals(operationTemplateActual.getCode(), TestUtils.operationCode);
Assert.assertEquals(operationTemplateActual.getDeviceType(), TestUtils.deviceType); Assert.assertEquals(operationTemplateActual.getDeviceType(), TestUtils.deviceType);
@ -59,7 +63,7 @@ public class ServiceTest extends BaseOperationTemplatePluginTest {
operationTemplate.setOperationDefinition(TestUtils.getOperationDefinition(TestUtils.subtypeId, TestUtils.operationCode)); operationTemplate.setOperationDefinition(TestUtils.getOperationDefinition(TestUtils.subtypeId, TestUtils.operationCode));
operationTemplateService.addOperationTemplate(operationTemplate); 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.assertNotNull(operationTemplateActual, "Cannot be null");
Assert.assertEquals(operationTemplateActual.getOperationDefinition(), TestUtils.getOperationDefinition(TestUtils.subtypeId, TestUtils.operationCode)); Assert.assertEquals(operationTemplateActual.getOperationDefinition(), TestUtils.getOperationDefinition(TestUtils.subtypeId, TestUtils.operationCode));
Assert.assertEquals(operationTemplateActual.getSubTypeId(), operationTemplateActual.getSubTypeId()); Assert.assertEquals(operationTemplateActual.getSubTypeId(), operationTemplateActual.getSubTypeId());
@ -70,7 +74,7 @@ public class ServiceTest extends BaseOperationTemplatePluginTest {
@Test(dependsOnMethods = "testAddOperationTemplate") @Test(dependsOnMethods = "testAddOperationTemplate")
public void testUpdateOperationTemplate() throws OperationTemplateMgtPluginException { 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.setOperationDefinition("{}");
OperationTemplate operationTemplateActual = operationTemplateService.updateOperationTemplate(operationTemplate); OperationTemplate operationTemplateActual = operationTemplateService.updateOperationTemplate(operationTemplate);
@ -81,13 +85,30 @@ public class ServiceTest extends BaseOperationTemplatePluginTest {
Assert.assertEquals(operationTemplateActual.getDeviceType(), TestUtils.deviceType); Assert.assertEquals(operationTemplateActual.getDeviceType(), TestUtils.deviceType);
} }
@Test(dependsOnMethods = {"testAddOperationTemplate", "testGetOperationTemplate", "testUpdateOperationTemplate"})
public void testDeleteOperationTemplate() throws OperationTemplateMgtPluginException { public OperationTemplate getOperationTemplateBySubtypeIdAndDeviceTypeAndOperationCode(String deviceType, String subtypeId, String operationCode) throws OperationTemplateMgtPluginException {
operationTemplateService.deleteOperationTemplate(TestUtils.subtypeId, TestUtils.deviceType, TestUtils.operationCode); return operationTemplateService.getOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(deviceType, subtypeId, operationCode);
Assert.assertNull(getOperationTemplateBySubtypeIdAndDeviceTypeAndOperationCode(TestUtils.subtypeId, TestUtils.deviceType, TestUtils.operationCode)); }
@Test(dependsOnMethods = "testAddOperationTemplate")
public void testGetOperationTemplateCodesByDeviceTypeAndSubTypeId() throws OperationTemplateMgtPluginException {
Set<String> operationCodes = operationTemplateService.getOperationTemplateCodesByDeviceTypeAndSubTypeId(TestUtils.deviceType, TestUtils.subtypeId);
Assert.assertNotNull(operationCodes, "Cannot be null");
}
@Test(dependsOnMethods = "testAddOperationTemplate")
public void testGetAllOperationTemplatesByDeviceType() throws OperationTemplateMgtPluginException {
List<OperationTemplate> 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 { @Test(dependsOnMethods = {"testAddOperationTemplate", "testGetOperationTemplate", "testUpdateOperationTemplate",
return operationTemplateService.getOperationTemplate(subtypeId, deviceType, operationCode); "testGetOperationTemplateCodesByDeviceTypeAndSubTypeId", "testGetAllOperationTemplatesByDeviceType"})
public void testDeleteOperationTemplate() throws OperationTemplateMgtPluginException {
operationTemplateService.deleteOperationTemplateByDeviceTypeAndSubTypeIdAndOperationCode(TestUtils.deviceType, TestUtils.subtypeId, TestUtils.operationCode);
Assert.assertNull(getOperationTemplateBySubtypeIdAndDeviceTypeAndOperationCode(TestUtils.deviceType, TestUtils.subtypeId, TestUtils.operationCode));
} }
} }

Loading…
Cancel
Save