[200~Refactored the code in subscription management.

subscription
Ruwin Dissanayake 3 months ago
parent 426fa14bfd
commit 0ee4412770

@ -252,12 +252,6 @@ public interface SubscriptionManager {
SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException;
// SubscriptionResponse getStatusBaseSubscriptionsGroup(SubscriptionInfo subscriptionInfo, int limit, int offset)
// throws ApplicationManagementException;
// SubscriptionResponse getStatusBaseSubscriptionsAll(SubscriptionInfo subscriptionInfo, int limit, int offset)
// throws ApplicationManagementException;
/**
* Get subscription statistics related data describes by the {@link SubscriptionInfo}

@ -1696,11 +1696,27 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
log.error(msg);
throw new NotFoundException(msg);
}
if (Objects.equals(subscriptionInfo.getSubscriptionType(), SubscriptionMetadata.SubscriptionTypes.GROUP)) {
List<SubscriptionEntity> subscriptionEntities = subscriptionDAO.
getGroupsSubscriptionDetailsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit);
int subscriptionCount = isUnsubscribe ? subscriptionDAO.getGroupUnsubscriptionCount(applicationReleaseDTO.getId(), tenantId) :
subscriptionDAO.getGroupSubscriptionCount(applicationReleaseDTO.getId(), tenantId);
return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionCount, subscriptionEntities);
} if (Objects.equals(subscriptionInfo.getSubscriptionType(), SubscriptionMetadata.SubscriptionTypes.ROLE)){
List<SubscriptionEntity> subscriptionEntities = subscriptionDAO.
getRoleSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit);
int subscriptionCount = isUnsubscribe ? subscriptionDAO.getRoleUnsubscriptionCount(applicationReleaseDTO.getId(), tenantId) :
subscriptionDAO.getRoleSubscriptionCount(applicationReleaseDTO.getId(), tenantId);
return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionCount, subscriptionEntities);
} if (Objects.equals(subscriptionInfo.getSubscriptionType(), SubscriptionMetadata.SubscriptionTypes.USER)){
List<SubscriptionEntity> subscriptionEntities = subscriptionDAO.
getUserSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit);
int subscriptionCount = isUnsubscribe ? subscriptionDAO.getUserUnsubscriptionCount(applicationReleaseDTO.getId(), tenantId) :
subscriptionDAO.getUserSubscriptionCount(applicationReleaseDTO.getId(), tenantId);
return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionCount, subscriptionEntities);
} else {
return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), Collections.emptyList());
}
} catch (DBConnectionException | ApplicationManagementDAOException e) {
String msg = "Error encountered while connecting to the database";
log.error(msg, e);
@ -1710,6 +1726,12 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
}
}
/**
* Retrieves subscription statistics described by the {@link SubscriptionInfo} entity.
* @param subscriptionInfo The {@link SubscriptionInfo} containing details about the subscription type and identifier.
* @return {@link SubscriptionStatistics} The subscription statistics for the specified subscription type and identifier.
* @throws ApplicationManagementException Throws when an error is encountered while getting subscription statistics.
*/
public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo)
throws ApplicationManagementException {
@ -1728,6 +1750,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
log.error(msg);
throw new NotFoundException(msg);
}
if (Objects.equals(subscriptionInfo.getSubscriptionType(), SubscriptionMetadata.SubscriptionTypes.GROUP)) {
List<Device> devices = HelperUtil.getGroupManagementProviderService().
getAllDevicesOfGroup(subscriptionInfo.getIdentifier(), false);
List<Integer> deviceIdsOwnByGroup = devices.stream().map(Device::getId).collect(Collectors.toList());
@ -1735,6 +1758,21 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
getSubscriptionStatistic(deviceIdsOwnByGroup, isUnsubscribe, tenantId, applicationReleaseDTO.getId());
int allDeviceCount = HelperUtil.getGroupManagementProviderService().getDeviceCount(subscriptionInfo.getIdentifier());
return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount);
} if (Objects.equals(subscriptionInfo.getSubscriptionType(), SubscriptionMetadata.SubscriptionTypes.ROLE)){
List<Integer> deviceIdsOwnByRole = getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(), tenantId);
SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO.
getSubscriptionStatistic(deviceIdsOwnByRole, isUnsubscribe, tenantId, applicationReleaseDTO.getId());
int allDeviceCount = deviceIdsOwnByRole.size();
return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount);
} if (Objects.equals(subscriptionInfo.getSubscriptionType(), SubscriptionMetadata.SubscriptionTypes.USER)){
List<Integer> deviceIdsOwnByUser = getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier());
SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO.
getSubscriptionStatistic(deviceIdsOwnByUser, isUnsubscribe, tenantId, applicationReleaseDTO.getId());
int allDeviceCount = deviceIdsOwnByUser.size();
return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount);
} else {
return null;
}
} catch (ApplicationManagementDAOException e) {
String msg = "Error encountered while getting subscription statistics for group: " + subscriptionInfo.getIdentifier();
log.error(msg, e);
@ -1743,6 +1781,14 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
String msg = "Error encountered while getting device subscription for group: " + subscriptionInfo.getIdentifier();
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (UserStoreException e) {
String msg = "Error encountered while getting subscription statistics for role: " + subscriptionInfo.getIdentifier();
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (DeviceManagementException e) {
String msg = "Error encountered while getting subscription statistics for user: " + subscriptionInfo.getIdentifier();
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
}
@ -1767,175 +1813,10 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
return getStatusBaseSubscriptionsRoleUser(subscriptionInfo, limit, offset);
}
}
//
// final boolean isUnsubscribe = Objects.equals(SubscriptionMetadata.SUBSCRIPTION_STATUS_UNSUBSCRIBED, subscriptionInfo.getSubscriptionStatus());
// List<DeviceSubscriptionDTO> deviceSubscriptionDTOS;
// int deviceCount = 0;
// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
//
// try {
// ConnectionManagerUtil.openDBConnection();
// ApplicationReleaseDTO applicationReleaseDTO = getApplicationRelease(subscriptionInfo.getApplicationUUID(), tenantId);
// ApplicationDTO applicationDTO = getApplication(subscriptionInfo.getApplicationUUID(), tenantId);
//
// String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo);
// DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria();
//
// if (Objects.equals(subscriptionInfo.getSubscriptionType(), SubscriptionMetadata.SubscriptionTypes.GROUP)){
// deviceSubscriptionDTOS = handleGroupSubscription(subscriptionInfo, limit, offset, isUnsubscribe, applicationReleaseDTO, applicationDTO, deviceSubscriptionStatus, deviceSubscriptionFilterCriteria);
// } if (Objects.equals(subscriptionInfo.getSubscriptionType(), SubscriptionMetadata.SubscriptionTypes.DEVICE)) {
// deviceSubscriptionDTOS = handleDeviceSubscription(subscriptionInfo, isUnsubscribe, applicationReleaseDTO, deviceSubscriptionStatus, deviceSubscriptionFilterCriteria);
// } else {
// getStatusBaseSubscriptionsRoleUser(subscriptionInfo, limit, offset);
// }
//
// List<DeviceSubscription> deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS,
// subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe, applicationDTO.getDeviceTypeId(), limit, offset);
// return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions);
// } catch (Exception e) {
// handleException(e, subscriptionInfo.getIdentifier());
// return null; // This line will not be reached due to exception handling, but is required for compilation.
// } finally {
// ConnectionManagerUtil.closeDBConnection();
// }
// }
//
// private ApplicationReleaseDTO getApplicationRelease(String applicationUUID, int tenantId) throws ApplicationManagementException {
// ApplicationReleaseDTO applicationReleaseDTO = null;
// try {
// applicationReleaseDTO = applicationReleaseDAO.getReleaseByUUID(applicationUUID, tenantId);
// } catch (ApplicationManagementDAOException e) {
// throw new RuntimeException(e);
// }
// if (applicationReleaseDTO == null) {
// String msg = "Couldn't find an application release for application release UUID: " + applicationUUID;
// log.error(msg);
// throw new NotFoundException(msg);
// }
// return applicationReleaseDTO;
// }
//
// private ApplicationDTO getApplication(String applicationUUID, int tenantId) throws ApplicationManagementException {
// ApplicationDTO applicationDTO = null;
// try {
// applicationDTO = this.applicationDAO.getAppWithRelatedRelease(applicationUUID, tenantId);
// } catch (ApplicationManagementDAOException e) {
// throw new RuntimeException(e);
// }
// if (applicationDTO == null) {
// String msg = "Application not found for the release UUID: " + applicationUUID;
// log.error(msg);
// throw new NotFoundException(msg);
// }
// return applicationDTO;
// }
//
// private List<DeviceSubscriptionDTO> handleDeviceSubscription(SubscriptionInfo subscriptionInfo, boolean isUnsubscribe, ApplicationReleaseDTO applicationReleaseDTO,
// String deviceSubscriptionStatus, DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria) throws DeviceManagementException {
// DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService();
// List<DeviceSubscriptionDTO> deviceSubscriptionDTOS;
// List<String> dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus());
//
// if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) {
// try {
// deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO.getId(),
// isUnsubscribe, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(), null, null,
// deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1);
// } catch (ApplicationManagementDAOException e) {
// throw new RuntimeException(e);
// }
//
// List<Integer> deviceIdsOfSubscription = deviceSubscriptionDTOS.stream()
// .map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList());
//
// List<Integer> newDeviceIds = deviceManagementProviderService.getDevicesNotInGivenIdList(deviceIdsOfSubscription);
//
// deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList());
// } else {
// try {
// deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO.getId(),
// isUnsubscribe, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(), dbSubscriptionStatus, null,
// deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1);
// } catch (ApplicationManagementDAOException e) {
// throw new RuntimeException(e);
// }
// }
// return deviceSubscriptionDTOS;
// }
//
// private List<DeviceSubscriptionDTO> handleGroupSubscription(SubscriptionInfo subscriptionInfo, int limit, int offset, boolean isUnsubscribe,
// ApplicationReleaseDTO applicationReleaseDTO, ApplicationDTO applicationDTO, String deviceSubscriptionStatus, DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria)
// throws GroupManagementException, DeviceManagementException {
// GroupManagementProviderService groupManagementProviderService = HelperUtil.getGroupManagementProviderService();
// DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService();
// List<DeviceSubscriptionDTO> deviceSubscriptionDTOS;
// List<String> dbSubscriptionStatus = SubscriptionManagementHelperUtil.getDBSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionStatus());
//
// if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) {
// List<Integer> allDeviceIdsOwnByGroup = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(),
// applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(),
// deviceSubscriptionFilterCriteria.getDeviceStatus(), -1, -1).getDeviceIds();
//
// try {
// deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
// isUnsubscribe, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(), allDeviceIdsOwnByGroup, null,
// null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1);
// } catch (ApplicationManagementDAOException e) {
// throw new RuntimeException(e);
// }
//
// List<Integer> deviceIdsOfSubscription = deviceSubscriptionDTOS.stream()
// .map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList());
//
// for (Integer deviceId : deviceIdsOfSubscription) {
// allDeviceIdsOwnByGroup.remove(deviceId);
// }
//
// List<Integer> newDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(allDeviceIdsOwnByGroup);
// deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList());
// } else {
// GroupDetailsDTO groupDetailsDTO = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(),
// applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(),
// deviceSubscriptionFilterCriteria.getDeviceStatus(), offset, limit);
// List<Integer> paginatedDeviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds();
//
// try {
// deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
// isUnsubscribe, PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(), paginatedDeviceIdsOwnByGroup, dbSubscriptionStatus,
// null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1);
// } catch (ApplicationManagementDAOException e) {
// throw new RuntimeException(e);
// }
// }
// return deviceSubscriptionDTOS;
// }
//
// private void handleException(Exception e, String identifier) throws ApplicationManagementException {
// if (e instanceof GroupManagementException) {
// String msg = "Error encountered while retrieving group details for group: " + identifier;
// log.error(msg, e);
// throw new ApplicationManagementException(msg, e);
// } else if (e instanceof ApplicationManagementDAOException || e instanceof DBConnectionException) {
// String msg = "Error encountered while connecting to the database";
// log.error(msg, e);
// throw new ApplicationManagementException(msg, e);
// } else if (e instanceof DeviceManagementException) {
// throw new RuntimeException(e);
// } else {
// String msg = "Unexpected error occurred";
// log.error(msg, e);
// throw new ApplicationManagementException(msg, e);
// }
// }
//
public SubscriptionResponse getStatusBaseSubscriptionsDevice(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException {
// if (Objects.equals(subscriptionInfo.getSubscriptionType(), SubscriptionMetadata.SubscriptionTypes.DEVICE)){
// return null;
// }
final boolean isUnsubscribe = Objects.equals(SubscriptionMetadata.SUBSCRIPTION_STATUS_UNSUBSCRIBED, subscriptionInfo.getSubscriptionStatus());
List<DeviceSubscriptionDTO> deviceSubscriptionDTOS;
int deviceCount = 0;
@ -2093,7 +1974,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
try {
ConnectionManagerUtil.openDBConnection();
List<Integer> deviceIdsOwnByRole = getAllDeviceIds(subscriptionInfo);
List<Integer> deviceIdsOwnByRoleUser = getAllDeviceIds(subscriptionInfo);
ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO.
getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId);
@ -2118,21 +1999,21 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) {
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByRole, null,
isUnsubscribe, tenantId, deviceIdsOwnByRoleUser, null,
null, deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1);
List<Integer> deviceIdsOfSubscription = deviceSubscriptionDTOS.stream().
map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList());
for (Integer deviceId : deviceIdsOfSubscription) {
deviceIdsOwnByRole.remove(deviceId);
deviceIdsOwnByRoleUser.remove(deviceId);
}
List<Integer> newDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(deviceIdsOwnByRole);
List<Integer> newDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(deviceIdsOwnByRoleUser);
deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList());
} else {
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByRole, dbSubscriptionStatus,
isUnsubscribe, tenantId, deviceIdsOwnByRoleUser, dbSubscriptionStatus,
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1);
}
deviceCount = SubscriptionManagementHelperUtil.getTotalDeviceSubscriptionCount(deviceSubscriptionDTOS,
@ -2181,25 +2062,6 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
return deviceListOwnByRole.stream().map(Device::getId).collect(Collectors.toList());
}
private List<Integer> getAllDeviceIds (SubscriptionInfo subscriptionInfo) throws DeviceManagementException, UserStoreException {
String type = subscriptionInfo.getSubscriptionType();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
if (Objects.equals(type, SubscriptionMetadata.SubscriptionTypes.ROLE)){
return getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(),tenantId);
}
// if (Objects.equals(type, SubscriptionMetadata.SubscriptionTypes.GROUP)){
// return getDeviceIdsOwnByGroup(subscriptionInfo.getIdentifier(),tenantId);
// }
if (Objects.equals(type,SubscriptionMetadata.SubscriptionTypes.USER)){
return getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier());
}else
return null;
}
@SuppressWarnings("unchecked")
private List<Integer> getDeviceIdsOwnByUser(String username) throws DeviceManagementException {
List<Device> deviceListOwnByUser = new ArrayList<>();
@ -2214,6 +2076,18 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
return deviceListOwnByUser.stream().map(Device::getId).collect(Collectors.toList());
}
private List<Integer> getAllDeviceIds (SubscriptionInfo subscriptionInfo) throws DeviceManagementException, UserStoreException {
String type = subscriptionInfo.getSubscriptionType();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
if (Objects.equals(type, SubscriptionMetadata.SubscriptionTypes.ROLE)){
return getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(),tenantId);
}
if (Objects.equals(type,SubscriptionMetadata.SubscriptionTypes.USER)){
return getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier());
}else
return null;
}
/**
* Get subscription statistics related data describes by the {@link SubscriptionInfo}

Loading…
Cancel
Save