Modify methods to categorize device subscriptions

## Purpose
* Fixes https://roadmap.entgra.net/issues/11162
Co-authored-by: Kavin Prathaban <kavin@entgra.io>
Co-committed-by: Kavin Prathaban <kavin@entgra.io>
pull/402/head
Kavin Prathaban 6 months ago committed by Lasantha Dharmakeerthi
parent 9517e80939
commit 1ea3a44859

@ -0,0 +1,41 @@
package io.entgra.device.mgt.core.application.mgt.common;
import java.util.List;
public class CategorizedSubscriptionResult {
private List<DeviceSubscriptionData> installedDevices;
private List<DeviceSubscriptionData> pendingDevices;
private List<DeviceSubscriptionData> errorDevices;
public CategorizedSubscriptionResult(List<DeviceSubscriptionData> installedDevices,
List<DeviceSubscriptionData> pendingDevices,
List<DeviceSubscriptionData> errorDevices) {
this.installedDevices = installedDevices;
this.pendingDevices = pendingDevices;
this.errorDevices = errorDevices;
}
public List<DeviceSubscriptionData> getInstalledDevices() {
return installedDevices;
}
public void setInstalledDevices(List<DeviceSubscriptionData> installedDevices) {
this.installedDevices = installedDevices;
}
public List<DeviceSubscriptionData> getPendingDevices() {
return pendingDevices;
}
public void setPendingDevices(List<DeviceSubscriptionData> pendingDevices) {
this.pendingDevices = pendingDevices;
}
public List<DeviceSubscriptionData> getErrorDevices() {
return errorDevices;
}
public void setErrorDevices(List<DeviceSubscriptionData> errorDevices) {
this.errorDevices = errorDevices;
}
}

@ -18,6 +18,7 @@
package io.entgra.device.mgt.core.application.mgt.common.services; package io.entgra.device.mgt.core.application.mgt.common.services;
import io.entgra.device.mgt.core.application.mgt.common.ApplicationInstallResponse; import io.entgra.device.mgt.core.application.mgt.common.ApplicationInstallResponse;
import io.entgra.device.mgt.core.application.mgt.common.CategorizedSubscriptionResult;
import io.entgra.device.mgt.core.application.mgt.common.ExecutionStatus; import io.entgra.device.mgt.core.application.mgt.common.ExecutionStatus;
import io.entgra.device.mgt.core.application.mgt.common.dto.ScheduledSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ScheduledSubscriptionDTO;
import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException;
@ -194,7 +195,7 @@ public interface SubscriptionManager {
* application release for given UUID, if an error occurred while getting device details of subscribed device ids, * application release for given UUID, if an error occurred while getting device details of subscribed device ids,
* if an error occurred while getting subscription details of given application release UUID. * if an error occurred while getting subscription details of given application release UUID.
*/ */
PaginationResult getAppSubscriptionDetails(PaginationRequest request, String appUUID, String actionStatus, String action, String installedVersion) CategorizedSubscriptionResult getAppSubscriptionDetails(PaginationRequest request, String appUUID, String actionStatus, String action, String installedVersion)
throws ApplicationManagementException; throws ApplicationManagementException;
/*** /***

@ -19,17 +19,28 @@
package io.entgra.device.mgt.core.application.mgt.core.impl; package io.entgra.device.mgt.core.application.mgt.core.impl;
import com.google.gson.Gson; import com.google.gson.Gson;
import io.entgra.device.mgt.core.application.mgt.common.ApplicationInstallResponse;
import io.entgra.device.mgt.core.application.mgt.common.ApplicationSubscriptionInfo;
import io.entgra.device.mgt.core.application.mgt.common.ApplicationType;
import io.entgra.device.mgt.core.application.mgt.common.CategorizedSubscriptionResult;
import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData;
import io.entgra.device.mgt.core.application.mgt.common.DeviceTypes;
import io.entgra.device.mgt.core.application.mgt.common.ExecutionStatus;
import io.entgra.device.mgt.core.application.mgt.common.SubAction;
import io.entgra.device.mgt.core.application.mgt.common.SubscribingDeviceIdHolder;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionType;
import io.entgra.device.mgt.core.application.mgt.common.dto.VppAssetDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.VppAssetDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.VppUserDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.VppUserDTO;
import io.entgra.device.mgt.core.application.mgt.common.services.VPPApplicationManager; import io.entgra.device.mgt.core.application.mgt.common.services.VPPApplicationManager;
import io.entgra.device.mgt.core.application.mgt.core.dao.VppApplicationDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.VppApplicationDAO;
import io.entgra.device.mgt.core.application.mgt.core.exception.BadRequestException; import io.entgra.device.mgt.core.application.mgt.core.exception.BadRequestException;
import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest;
import io.entgra.device.mgt.core.device.mgt.common.PaginationResult;
import io.entgra.device.mgt.core.device.mgt.core.DeviceManagementConstants; import io.entgra.device.mgt.core.device.mgt.core.DeviceManagementConstants;
import io.entgra.device.mgt.core.application.mgt.core.exception.UnexpectedServerErrorException; import io.entgra.device.mgt.core.application.mgt.core.exception.UnexpectedServerErrorException;
import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger;
import io.entgra.device.mgt.core.notification.logger.AppInstallLogContext; import io.entgra.device.mgt.core.notification.logger.AppInstallLogContext;
import io.entgra.device.mgt.core.notification.logger.impl.EntgraAppInstallLoggerImpl; import io.entgra.device.mgt.core.notification.logger.impl.EntgraAppInstallLoggerImpl;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
@ -45,15 +56,6 @@ import org.json.JSONObject;
import io.entgra.device.mgt.core.apimgt.application.extension.dto.ApiApplicationKey; import io.entgra.device.mgt.core.apimgt.application.extension.dto.ApiApplicationKey;
import io.entgra.device.mgt.core.apimgt.application.extension.exception.APIManagerException; import io.entgra.device.mgt.core.apimgt.application.extension.exception.APIManagerException;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import io.entgra.device.mgt.core.application.mgt.common.ApplicationInstallResponse;
import io.entgra.device.mgt.core.application.mgt.common.ApplicationSubscriptionInfo;
import io.entgra.device.mgt.core.application.mgt.common.ApplicationType;
import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData;
import io.entgra.device.mgt.core.application.mgt.common.DeviceTypes;
import io.entgra.device.mgt.core.application.mgt.common.ExecutionStatus;
import io.entgra.device.mgt.core.application.mgt.common.SubAction;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionType;
import io.entgra.device.mgt.core.application.mgt.common.SubscribingDeviceIdHolder;
import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationPolicyDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationPolicyDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO;
@ -1505,16 +1507,19 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
} }
@Override @Override
public PaginationResult getAppSubscriptionDetails(PaginationRequest request, String appUUID, String actionStatus, public CategorizedSubscriptionResult getAppSubscriptionDetails(PaginationRequest request, String appUUID, String actionStatus,
String action, String installedVersion) throws ApplicationManagementException { String action, String installedVersion) throws ApplicationManagementException {
int limitValue = request.getRowCount(); int limitValue = request.getRowCount();
int offsetValue = request.getStartIndex(); int offsetValue = request.getStartIndex();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
DeviceManagementProviderService deviceManagementProviderService = HelperUtil DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService();
.getDeviceManagementProviderService(); List<DeviceSubscriptionData> installedDevices = new ArrayList<>();
List<DeviceSubscriptionData> pendingDevices = new ArrayList<>();
List<DeviceSubscriptionData> errorDevices = new ArrayList<>();
if (offsetValue < 0 || limitValue <= 0) { if (offsetValue < 0 || limitValue <= 0) {
String msg = "Found incompatible values for offset and limit. Hence please check the request and resend. " String msg = "Found incompatible values for offset and limit. Hence please check the request and resend. " +
+ "Offset " + offsetValue + " limit " + limitValue; "Offset " + offsetValue + " limit " + limitValue;
log.error(msg); log.error(msg);
throw new BadRequestException(msg); throw new BadRequestException(msg);
} }
@ -1532,31 +1537,25 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
List<DeviceSubscriptionDTO> deviceSubscriptionDTOS = subscriptionDAO List<DeviceSubscriptionDTO> deviceSubscriptionDTOS = subscriptionDAO
.getDeviceSubscriptions(applicationReleaseId, tenantId, actionStatus, action); .getDeviceSubscriptions(applicationReleaseId, tenantId, actionStatus, action);
if (deviceSubscriptionDTOS.isEmpty()) { if (deviceSubscriptionDTOS.isEmpty()) {
PaginationResult paginationResult = new PaginationResult(); return new CategorizedSubscriptionResult(new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
paginationResult.setData(new ArrayList<>());
paginationResult.setRecordsFiltered(0);
paginationResult.setRecordsTotal(0);
return paginationResult;
} }
List<Integer> deviceIdList = deviceSubscriptionDTOS.stream().map(DeviceSubscriptionDTO::getDeviceId) List<Integer> deviceIdList = deviceSubscriptionDTOS.stream().map(DeviceSubscriptionDTO::getDeviceId)
.collect(Collectors.toList()); .collect(Collectors.toList());
Map<Integer,String> currentVersionsMap = subscriptionDAO.getCurrentInstalledAppVersion(applicationDTO.getId(),deviceIdList, installedVersion); Map<Integer, String> currentVersionsMap = subscriptionDAO.getCurrentInstalledAppVersion(applicationDTO.getId(), deviceIdList, installedVersion);
try { try {
//pass the device id list to device manager service method // Pass the device id list to device manager service method
PaginationResult paginationResult = deviceManagementProviderService.getAppSubscribedDevices PaginationResult paginationResult = deviceManagementProviderService.getAppSubscribedDevices(request, deviceIdList);
(request, deviceIdList);
List<DeviceSubscriptionData> deviceSubscriptionDataList = new ArrayList<>();
if (!paginationResult.getData().isEmpty()) { if (!paginationResult.getData().isEmpty()) {
List<Device> devices = (List<Device>) paginationResult.getData(); List<Device> devices = (List<Device>) paginationResult.getData();
for (Device device : devices) { for (Device device : devices) {
if(installedVersion != null && !installedVersion.isEmpty() && !currentVersionsMap.containsKey(device.getId())){ if (installedVersion != null && !installedVersion.isEmpty() && !currentVersionsMap.containsKey(device.getId())) {
continue; continue;
} }
DeviceSubscriptionData deviceSubscriptionData = new DeviceSubscriptionData(); DeviceSubscriptionData deviceSubscriptionData = new DeviceSubscriptionData();
if(currentVersionsMap.containsKey(device.getId())){ if (currentVersionsMap.containsKey(device.getId())) {
deviceSubscriptionData.setCurrentInstalledVersion(currentVersionsMap.get(device.getId())); deviceSubscriptionData.setCurrentInstalledVersion(currentVersionsMap.get(device.getId()));
}else{ } else {
deviceSubscriptionData.setCurrentInstalledVersion("-"); deviceSubscriptionData.setCurrentInstalledVersion("-");
} }
for (DeviceSubscriptionDTO subscription : deviceSubscriptionDTOS) { for (DeviceSubscriptionDTO subscription : deviceSubscriptionDTOS) {
@ -1565,39 +1564,51 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
if (subscription.isUnsubscribed()) { if (subscription.isUnsubscribed()) {
deviceSubscriptionData.setAction(Constants.UNSUBSCRIBED); deviceSubscriptionData.setAction(Constants.UNSUBSCRIBED);
deviceSubscriptionData.setActionTriggeredBy(subscription.getUnsubscribedBy()); deviceSubscriptionData.setActionTriggeredBy(subscription.getUnsubscribedBy());
deviceSubscriptionData deviceSubscriptionData.setActionTriggeredTimestamp(subscription.getUnsubscribedTimestamp().getTime() / 1000);
.setActionTriggeredTimestamp(subscription.getUnsubscribedTimestamp().getTime() / 1000);
} else { } else {
deviceSubscriptionData.setAction(Constants.SUBSCRIBED); deviceSubscriptionData.setAction(Constants.SUBSCRIBED);
deviceSubscriptionData.setActionTriggeredBy(subscription.getSubscribedBy()); deviceSubscriptionData.setActionTriggeredBy(subscription.getSubscribedBy());
deviceSubscriptionData deviceSubscriptionData.setActionTriggeredTimestamp(subscription.getSubscribedTimestamp().getTime() / 1000);
.setActionTriggeredTimestamp(subscription.getSubscribedTimestamp().getTime() / 1000);
} }
deviceSubscriptionData.setActionType(subscription.getActionTriggeredFrom()); deviceSubscriptionData.setActionType(subscription.getActionTriggeredFrom());
deviceSubscriptionData.setStatus(subscription.getStatus()); deviceSubscriptionData.setStatus(subscription.getStatus());
deviceSubscriptionData.setSubId(subscription.getId()); deviceSubscriptionData.setSubId(subscription.getId());
deviceSubscriptionDataList.add(deviceSubscriptionData);
// Categorize the subscription data based on its status
switch (subscription.getStatus()) {
case "COMPLETED":
installedDevices.add(deviceSubscriptionData);
break;
case "ERROR":
case "INVALID":
case "UNAUTHORIZED":
errorDevices.add(deviceSubscriptionData);
break;
case "IN_PROGRESS":
case "PENDING":
case "REPEATED":
pendingDevices.add(deviceSubscriptionData);
break;
}
break; break;
} }
} }
} }
} }
paginationResult.setData(deviceSubscriptionDataList); return new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices);
return paginationResult;
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "service error occurred while getting device data from the device management service. " String msg = "Service error occurred while getting device data from the device management service. " +
+ "Device ids " + deviceIdList; "Device ids " + deviceIdList;
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} }
} catch (ApplicationManagementDAOException e) { } catch (ApplicationManagementDAOException e) {
String msg = String msg = "Error occurred when getting application release data for application release UUID: " + appUUID;
"Error occurred when getting application release data for application release UUID: " + appUUID;
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
String msg = "DB Connection error occurred while trying to get subscription data of application which has " String msg = "DB Connection error occurred while trying to get subscription data of application which has " +
+ "application release UUID " + appUUID; "application release UUID " + appUUID;
log.error(msg, e); log.error(msg, e);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
} finally { } finally {

Loading…
Cancel
Save