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>
appm_improvement
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;
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.dto.ScheduledSubscriptionDTO;
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,
* 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;
/***

@ -19,17 +19,28 @@
package io.entgra.device.mgt.core.application.mgt.core.impl;
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.VppUserDTO;
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.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.application.mgt.core.exception.UnexpectedServerErrorException;
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.impl.EntgraAppInstallLoggerImpl;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.lang.StringUtils;
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.exception.APIManagerException;
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.ApplicationPolicyDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO;
@ -1505,16 +1507,19 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
}
@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 {
int limitValue = request.getRowCount();
int offsetValue = request.getStartIndex();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
DeviceManagementProviderService deviceManagementProviderService = HelperUtil
.getDeviceManagementProviderService();
DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService();
List<DeviceSubscriptionData> installedDevices = new ArrayList<>();
List<DeviceSubscriptionData> pendingDevices = new ArrayList<>();
List<DeviceSubscriptionData> errorDevices = new ArrayList<>();
if (offsetValue < 0 || limitValue <= 0) {
String msg = "Found incompatible values for offset and limit. Hence please check the request and resend. "
+ "Offset " + offsetValue + " limit " + limitValue;
String msg = "Found incompatible values for offset and limit. Hence please check the request and resend. " +
"Offset " + offsetValue + " limit " + limitValue;
log.error(msg);
throw new BadRequestException(msg);
}
@ -1532,31 +1537,25 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
List<DeviceSubscriptionDTO> deviceSubscriptionDTOS = subscriptionDAO
.getDeviceSubscriptions(applicationReleaseId, tenantId, actionStatus, action);
if (deviceSubscriptionDTOS.isEmpty()) {
PaginationResult paginationResult = new PaginationResult();
paginationResult.setData(new ArrayList<>());
paginationResult.setRecordsFiltered(0);
paginationResult.setRecordsTotal(0);
return paginationResult;
return new CategorizedSubscriptionResult(new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
}
List<Integer> deviceIdList = deviceSubscriptionDTOS.stream().map(DeviceSubscriptionDTO::getDeviceId)
.collect(Collectors.toList());
Map<Integer,String> currentVersionsMap = subscriptionDAO.getCurrentInstalledAppVersion(applicationDTO.getId(),deviceIdList, installedVersion);
Map<Integer, String> currentVersionsMap = subscriptionDAO.getCurrentInstalledAppVersion(applicationDTO.getId(), deviceIdList, installedVersion);
try {
//pass the device id list to device manager service method
PaginationResult paginationResult = deviceManagementProviderService.getAppSubscribedDevices
(request, deviceIdList);
List<DeviceSubscriptionData> deviceSubscriptionDataList = new ArrayList<>();
// Pass the device id list to device manager service method
PaginationResult paginationResult = deviceManagementProviderService.getAppSubscribedDevices(request, deviceIdList);
if (!paginationResult.getData().isEmpty()) {
List<Device> devices = (List<Device>) paginationResult.getData();
for (Device device : devices) {
if(installedVersion != null && !installedVersion.isEmpty() && !currentVersionsMap.containsKey(device.getId())){
if (installedVersion != null && !installedVersion.isEmpty() && !currentVersionsMap.containsKey(device.getId())) {
continue;
}
DeviceSubscriptionData deviceSubscriptionData = new DeviceSubscriptionData();
if(currentVersionsMap.containsKey(device.getId())){
if (currentVersionsMap.containsKey(device.getId())) {
deviceSubscriptionData.setCurrentInstalledVersion(currentVersionsMap.get(device.getId()));
}else{
} else {
deviceSubscriptionData.setCurrentInstalledVersion("-");
}
for (DeviceSubscriptionDTO subscription : deviceSubscriptionDTOS) {
@ -1565,39 +1564,51 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
if (subscription.isUnsubscribed()) {
deviceSubscriptionData.setAction(Constants.UNSUBSCRIBED);
deviceSubscriptionData.setActionTriggeredBy(subscription.getUnsubscribedBy());
deviceSubscriptionData
.setActionTriggeredTimestamp(subscription.getUnsubscribedTimestamp().getTime() / 1000);
deviceSubscriptionData.setActionTriggeredTimestamp(subscription.getUnsubscribedTimestamp().getTime() / 1000);
} else {
deviceSubscriptionData.setAction(Constants.SUBSCRIBED);
deviceSubscriptionData.setActionTriggeredBy(subscription.getSubscribedBy());
deviceSubscriptionData
.setActionTriggeredTimestamp(subscription.getSubscribedTimestamp().getTime() / 1000);
deviceSubscriptionData.setActionTriggeredTimestamp(subscription.getSubscribedTimestamp().getTime() / 1000);
}
deviceSubscriptionData.setActionType(subscription.getActionTriggeredFrom());
deviceSubscriptionData.setStatus(subscription.getStatus());
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;
}
}
}
paginationResult.setData(deviceSubscriptionDataList);
return paginationResult;
}
return new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices);
} catch (DeviceManagementException e) {
String msg = "service error occurred while getting device data from the device management service. "
+ "Device ids " + deviceIdList;
String msg = "Service error occurred while getting device data from the device management service. " +
"Device ids " + deviceIdList;
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
}
} catch (ApplicationManagementDAOException e) {
String msg =
"Error occurred when getting application release data for application release UUID: " + appUUID;
String msg = "Error occurred when getting application release data for application release UUID: " + appUUID;
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (DBConnectionException e) {
String msg = "DB Connection error occurred while trying to get subscription data of application which has "
+ "application release UUID " + appUUID;
String msg = "DB Connection error occurred while trying to get subscription data of application which has " +
"application release UUID " + appUUID;
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} finally {

Loading…
Cancel
Save