Fix inconsistencies with subtypes and operation templates

master
Lasantha Dharmakeerthi 3 months ago
commit 8d685382e3

@ -216,8 +216,8 @@ public class OperationTemplateServiceImpl implements OperationTemplateService {
throws OperationTemplateMgtPluginException { throws OperationTemplateMgtPluginException {
AssertUtils.hasText(deviceType, "Invalid device type."); AssertUtils.hasText(deviceType, "Invalid device type.");
try { try {
ConnectionManagerUtils.openDBConnection(); ConnectionManagerUtils.openDBConnection();
return operationTemplateDAO.getAllOperationTemplates(deviceType); return operationTemplateDAO.getAllOperationTemplates(deviceType);
} 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);
@ -273,11 +273,12 @@ public class OperationTemplateServiceImpl implements OperationTemplateService {
*/ */
@Override @Override
public Set<String> getOperationTemplateCodes(String deviceType, String subTypeId) public Set<String> getOperationTemplateCodes(String deviceType, String subTypeId)
throws OperationTemplateMgtPluginException { throws OperationTemplateMgtPluginException {
try { try {
AssertUtils.hasText(subTypeId, "Invalid meter device subtype id: " + subTypeId); AssertUtils.hasText(subTypeId, "Invalid device subtype id: " + subTypeId);
AssertUtils.isTrue(Integer.valueOf(subTypeId)>0, "Invalid meter device subtype id: " + subTypeId); AssertUtils.isTrue(Integer.parseInt(subTypeId) > 0,
"Invalid device subtype id: " + subTypeId);
AssertUtils.hasText(deviceType, "Invalid device type."); AssertUtils.hasText(deviceType, "Invalid device type.");
String key = OperationTemplateManagementUtil.setOperationTemplateCacheKey(deviceType, subTypeId); String key = OperationTemplateManagementUtil.setOperationTemplateCacheKey(deviceType, subTypeId);
@ -294,7 +295,6 @@ public class OperationTemplateServiceImpl implements OperationTemplateService {
} }
/** /**
*
* @param subTypeId * @param subTypeId
* @param deviceType * @param deviceType
* @param operationCode * @param operationCode
@ -303,18 +303,18 @@ public class OperationTemplateServiceImpl implements OperationTemplateService {
private void validateGetOperationTemplate(String subTypeId, String deviceType, String operationCode) private void validateGetOperationTemplate(String subTypeId, String deviceType, String operationCode)
throws OperationTemplateMgtPluginException { throws OperationTemplateMgtPluginException {
AssertUtils.hasText(subTypeId, "Invalid meter device subtype id: " + subTypeId); AssertUtils.hasText(subTypeId, "Invalid device subtype id: " + subTypeId);
AssertUtils.isTrue(Integer.valueOf(subTypeId)>0, "Invalid meter device subtype id: " + subTypeId); AssertUtils.isTrue(Integer.parseInt(subTypeId) > 0, "Invalid device subtype id: " + subTypeId);
AssertUtils.hasText(operationCode, "Validation failed due to invalid operation code: " + operationCode); AssertUtils.hasText(operationCode, "Validation failed due to invalid operation code: " + operationCode);
AssertUtils.hasText(deviceType, "Invalid device type."); AssertUtils.hasText(deviceType, "Invalid device type.");
AssertUtils.isTrue(deviceType.equals("METER"), "Invalid device type. ");
} }
/** /**
* @param operationTemplate * @param operationTemplate
* @throws OperationTemplateMgtPluginException * @throws OperationTemplateMgtPluginException
*/ */
private void validateAddOperationTemplate(OperationTemplate operationTemplate) throws OperationTemplateMgtPluginException { private void validateAddOperationTemplate(OperationTemplate operationTemplate)
throws OperationTemplateMgtPluginException {
AssertUtils.isNull(operationTemplate, "Operation Template can not be null"); AssertUtils.isNull(operationTemplate, "Operation Template can not be null");
AssertUtils.hasText(operationTemplate.getOperationDefinition(), "Operation definition can not be null"); AssertUtils.hasText(operationTemplate.getOperationDefinition(), "Operation definition can not be null");

@ -49,7 +49,7 @@ public class ServiceNegativeTest extends BaseOperationTemplatePluginTest {
@Test(description = "This method tests Add Operation template under negative circumstances while missing " + @Test(description = "This method tests Add Operation template under negative circumstances while missing " +
"required fields", "required fields",
expectedExceptions = {OperationTemplateMgtPluginException.class}, expectedExceptions = {OperationTemplateMgtPluginException.class},
expectedExceptionsMessageRegExp = "Invalid meter device subtype id: 0") expectedExceptionsMessageRegExp = "Invalid device subtype id: 0")
public void testAddOperationTemplates() throws OperationTemplateMgtPluginException { public void testAddOperationTemplates() throws OperationTemplateMgtPluginException {
OperationTemplate operationTemplate = new OperationTemplate(); OperationTemplate operationTemplate = new OperationTemplate();

@ -31,13 +31,13 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
public class GetDeviceSubTypeCacheLoader extends CacheLoader<DeviceSubTypeCacheKey, DeviceSubType> { public class DeviceSubTypeCacheLoader extends CacheLoader<DeviceSubTypeCacheKey, DeviceSubType> {
private static final Log log = LogFactory.getLog(GetDeviceSubTypeCacheLoader.class); private static final Log log = LogFactory.getLog(DeviceSubTypeCacheLoader.class);
private final DeviceSubTypeDAO deviceSubTypeDAO; private final DeviceSubTypeDAO deviceSubTypeDAO;
public GetDeviceSubTypeCacheLoader() { public DeviceSubTypeCacheLoader() {
this.deviceSubTypeDAO = DeviceSubTypeDAOFactory.getDeviceSubTypeDAO(); this.deviceSubTypeDAO = DeviceSubTypeDAOFactory.getDeviceSubTypeDAO();
} }

@ -101,8 +101,9 @@ public class DeviceSubTypeDAOImpl implements DeviceSubTypeDAO {
public DeviceSubType getDeviceSubType(String subTypeId, int tenantId, String deviceType) public DeviceSubType getDeviceSubType(String subTypeId, int tenantId, String deviceType)
throws SubTypeMgtDAOException { throws SubTypeMgtDAOException {
try { try {
String sql = "SELECT s.*, o.OPERATION_CODE FROM DM_DEVICE_SUB_TYPE s " + String sql = "SELECT s.*, o.OPERATION_CODE FROM DM_DEVICE_SUB_TYPE s " +
"LEFT JOIN SUB_OPERATION_TEMPLATE o on s.SUB_TYPE_ID = o.SUB_TYPE_ID " + "LEFT JOIN SUB_OPERATION_TEMPLATE o ON s.SUB_TYPE_ID = o.SUB_TYPE_ID " +
"AND s.DEVICE_TYPE = o.DEVICE_TYPE " +
"WHERE s.SUB_TYPE_ID = ? AND s.TENANT_ID = ? AND s.DEVICE_TYPE = ?"; "WHERE s.SUB_TYPE_ID = ? AND s.TENANT_ID = ? AND s.DEVICE_TYPE = ?";
Connection conn = ConnectionManagerUtil.getDBConnection(); Connection conn = ConnectionManagerUtil.getDBConnection();
@ -143,8 +144,7 @@ public class DeviceSubTypeDAOImpl implements DeviceSubTypeDAO {
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
stmt.setString(2, deviceType); stmt.setString(2, deviceType);
try (ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
List<DeviceSubType> deviceSubTypes = DAOUtil.loadDeviceSubTypes(rs); return DAOUtil.loadDeviceSubTypes(rs);
return deviceSubTypes;
} }
} }
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
@ -163,14 +163,14 @@ public class DeviceSubTypeDAOImpl implements DeviceSubTypeDAO {
@Override @Override
public int getDeviceSubTypeCount(String deviceType) throws SubTypeMgtDAOException { public int getDeviceSubTypeCount(String deviceType) throws SubTypeMgtDAOException {
try { try {
String sql = "SELECT COUNT(*) as DEVICE_COUNT FROM DM_DEVICE_SUB_TYPE WHERE DEVICE_TYPE = ? "; String sql = "SELECT COUNT(*) as SUB_TYPE_COUNT FROM DM_DEVICE_SUB_TYPE WHERE DEVICE_TYPE = ? ";
Connection conn = ConnectionManagerUtil.getDBConnection(); Connection conn = ConnectionManagerUtil.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) { try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, deviceType); stmt.setString(1, deviceType);
try (ResultSet rs = stmt.executeQuery()) { try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) { if (rs.next()) {
return rs.getInt("DEVICE_COUNT"); return rs.getInt("SUB_TYPE_COUNT");
} }
return 0; return 0;
} }
@ -252,4 +252,5 @@ public class DeviceSubTypeDAOImpl implements DeviceSubTypeDAO {
throw new SubTypeMgtDAOException(msg, e); throw new SubTypeMgtDAOException(msg, e);
} }
} }
} }

@ -21,7 +21,7 @@ package io.entgra.device.mgt.core.subtype.mgt.impl;
import com.google.common.cache.CacheBuilder; 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.subtype.mgt.cache.GetDeviceSubTypeCacheLoader; import io.entgra.device.mgt.core.subtype.mgt.cache.DeviceSubTypeCacheLoader;
import io.entgra.device.mgt.core.subtype.mgt.dto.DeviceSubTypeCacheKey; import io.entgra.device.mgt.core.subtype.mgt.dto.DeviceSubTypeCacheKey;
import io.entgra.device.mgt.core.subtype.mgt.exception.BadRequestException; import io.entgra.device.mgt.core.subtype.mgt.exception.BadRequestException;
import io.entgra.device.mgt.core.subtype.mgt.exception.DBConnectionException; import io.entgra.device.mgt.core.subtype.mgt.exception.DBConnectionException;
@ -48,7 +48,7 @@ public class DeviceSubTypeServiceImpl implements DeviceSubTypeService {
private static final LoadingCache<DeviceSubTypeCacheKey, DeviceSubType> deviceSubTypeCache private static final LoadingCache<DeviceSubTypeCacheKey, DeviceSubType> deviceSubTypeCache
= CacheBuilder.newBuilder() = CacheBuilder.newBuilder()
.expireAfterWrite(15, TimeUnit.MINUTES) .expireAfterWrite(15, TimeUnit.MINUTES)
.build(new GetDeviceSubTypeCacheLoader()); .build(new DeviceSubTypeCacheLoader());
private final DeviceSubTypeDAO deviceSubTypeDAO; private final DeviceSubTypeDAO deviceSubTypeDAO;
public DeviceSubTypeServiceImpl() { public DeviceSubTypeServiceImpl() {
@ -166,7 +166,13 @@ public class DeviceSubTypeServiceImpl implements DeviceSubTypeService {
throws SubTypeMgtPluginException { throws SubTypeMgtPluginException {
try { try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
return deviceSubTypeDAO.getAllDeviceSubTypes(tenantId, deviceType); List<DeviceSubType> subtypes = deviceSubTypeDAO.getAllDeviceSubTypes(tenantId, deviceType);
DeviceSubTypeCacheKey key;
for (DeviceSubType dst: subtypes) {
key = DeviceSubTypeMgtUtil.getDeviceSubTypeCacheKey(tenantId, dst.getSubTypeId(), deviceType);
deviceSubTypeCache.put(key, dst);
}
return subtypes;
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the database connection to retrieve all device subtype for " + String msg = "Error occurred while obtaining the database connection to retrieve all device subtype for " +
deviceType + " subtypes"; deviceType + " subtypes";

Loading…
Cancel
Save