|
|
|
@ -19,17 +19,29 @@
|
|
|
|
|
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.core.dao.DeviceManagementDAOException;
|
|
|
|
|
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 +57,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;
|
|
|
|
@ -99,6 +102,7 @@ import io.entgra.device.mgt.core.device.mgt.core.util.MDMIOSOperationUtil;
|
|
|
|
|
import io.entgra.device.mgt.core.device.mgt.core.util.MDMWindowsOperationUtil;
|
|
|
|
|
import io.entgra.device.mgt.core.identity.jwt.client.extension.dto.AccessTokenInfo;
|
|
|
|
|
import org.wso2.carbon.user.api.UserStoreException;
|
|
|
|
|
import org.wso2.carbon.user.api.UserStoreManager;
|
|
|
|
|
|
|
|
|
|
import javax.ws.rs.core.MediaType;
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
@ -1505,16 +1509,19 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public PaginationResult getAppSubscriptionDetails(PaginationRequest request, String appUUID, String actionStatus,
|
|
|
|
|
String action, String installedVersion) throws ApplicationManagementException {
|
|
|
|
|
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 +1539,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 +1566,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 {
|
|
|
|
@ -1675,4 +1688,419 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<Map<String, Object>> getGroupsSubDetailsByUUID(String uuid, String subscriptionStatus) throws ApplicationManagementException {
|
|
|
|
|
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
|
|
|
|
|
boolean unsubscribe = subscriptionStatus.equals("unsubscribed");
|
|
|
|
|
try {
|
|
|
|
|
ConnectionManagerUtil.openDBConnection();
|
|
|
|
|
List<Map<String, Object>> groupDetailsWithDevices = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
List<Map<String, Object>> groupDetails = subscriptionDAO.getGroupsSubByUUID(uuid, unsubscribe, tenantId);
|
|
|
|
|
|
|
|
|
|
GroupManagementProviderService groupManagementProviderService = HelperUtil.getGroupManagementProviderService();
|
|
|
|
|
|
|
|
|
|
for (Map<String, Object> groupDetail : groupDetails) {
|
|
|
|
|
String groupName = (String) groupDetail.get("GroupName");
|
|
|
|
|
|
|
|
|
|
// Retrieve group details and device IDs for the group using the service layer
|
|
|
|
|
Map<String, Object> groupDetailWithDevices = groupManagementProviderService.getGroupDetailsWithDeviceIds(groupName);
|
|
|
|
|
|
|
|
|
|
// map to hold the response data
|
|
|
|
|
Map<String, Object> groupDetailMap = new HashMap<>();
|
|
|
|
|
groupDetailMap.put("GroupID", groupDetailWithDevices.get("GroupID"));
|
|
|
|
|
groupDetailMap.put("GroupName", groupDetail.get("GroupName"));
|
|
|
|
|
groupDetailMap.put("GroupOwner", groupDetailWithDevices.get("GroupOwner"));
|
|
|
|
|
groupDetailMap.put("SubscribedBy", groupDetail.get("SubscribedBy"));
|
|
|
|
|
groupDetailMap.put("SubscribedTimestamp", groupDetail.get("SubscribedTimestamp"));
|
|
|
|
|
groupDetailMap.put("Unsubscribed", groupDetail.get("Unsubscribed"));
|
|
|
|
|
groupDetailMap.put("UnsubscribedBy", groupDetail.get("UnsubscribedBy"));
|
|
|
|
|
groupDetailMap.put("UnsubscribedTimestamp", groupDetail.get("UnsubscribedTimestamp"));
|
|
|
|
|
groupDetailMap.put("AppReleaseId", groupDetail.get("AppReleaseId"));
|
|
|
|
|
|
|
|
|
|
groupDetailMap.put("DeviceCount", groupDetailWithDevices.get("DeviceCount"));
|
|
|
|
|
|
|
|
|
|
// Fetch device subscriptions for each device ID in the group
|
|
|
|
|
List<Map<String, Object>> pendingDevices = new ArrayList<>();
|
|
|
|
|
List<Map<String, Object>> installedDevices = new ArrayList<>();
|
|
|
|
|
List<Map<String, Object>> errorDevices = new ArrayList<>();
|
|
|
|
|
List<Map<String, Object>> newDevices = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
List<Integer> deviceIds = (List<Integer>) groupDetailWithDevices.get("DeviceIds");
|
|
|
|
|
Map<String, Integer> statusCounts = new HashMap<>();
|
|
|
|
|
statusCounts.put("PENDING", 0);
|
|
|
|
|
statusCounts.put("COMPLETED", 0);
|
|
|
|
|
statusCounts.put("ERROR", 0);
|
|
|
|
|
statusCounts.put("NEW", 0);
|
|
|
|
|
|
|
|
|
|
for (Integer deviceId : deviceIds) {
|
|
|
|
|
List<DeviceSubscriptionDTO> deviceSubscriptions = subscriptionDAO.getDeviceSubscriptions(
|
|
|
|
|
(Integer) groupDetail.get("AppReleaseId"), tenantId, null, null);
|
|
|
|
|
boolean isNewDevice = true;
|
|
|
|
|
for (DeviceSubscriptionDTO subscription : deviceSubscriptions) {
|
|
|
|
|
if (subscription.getDeviceId() == deviceId) {
|
|
|
|
|
Map<String, Object> deviceDetailMap = new HashMap<>();
|
|
|
|
|
deviceDetailMap.put("DeviceID", subscription.getDeviceId());
|
|
|
|
|
// deviceDetailMap.put("Status", subscription.getStatus()); // if need to get the real status
|
|
|
|
|
|
|
|
|
|
String status = subscription.getStatus();
|
|
|
|
|
switch (status) {
|
|
|
|
|
case "COMPLETED":
|
|
|
|
|
installedDevices.add(deviceDetailMap);
|
|
|
|
|
statusCounts.put("COMPLETED", statusCounts.get("COMPLETED") + 1);
|
|
|
|
|
break;
|
|
|
|
|
case "ERROR":
|
|
|
|
|
case "INVALID":
|
|
|
|
|
case "UNAUTHORIZED":
|
|
|
|
|
errorDevices.add(deviceDetailMap);
|
|
|
|
|
statusCounts.put("ERROR", statusCounts.get("ERROR") + 1);
|
|
|
|
|
break;
|
|
|
|
|
case "IN_PROGRESS":
|
|
|
|
|
case "PENDING":
|
|
|
|
|
case "REPEATED":
|
|
|
|
|
pendingDevices.add(deviceDetailMap);
|
|
|
|
|
statusCounts.put("PENDING", statusCounts.get("PENDING") + 1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
isNewDevice = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (isNewDevice) {
|
|
|
|
|
Map<String, Object> newDeviceDetailMap = new HashMap<>();
|
|
|
|
|
newDeviceDetailMap.put("DeviceID", deviceId);
|
|
|
|
|
newDevices.add(newDeviceDetailMap);
|
|
|
|
|
statusCounts.put("NEW", statusCounts.get("NEW") + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int totalDevices = deviceIds.size();
|
|
|
|
|
Map<String, Double> statusPercentages = new HashMap<>();
|
|
|
|
|
for (Map.Entry<String, Integer> entry : statusCounts.entrySet()) {
|
|
|
|
|
double percentage = ((double) entry.getValue() / totalDevices) * 100;
|
|
|
|
|
statusPercentages.put(entry.getKey(), percentage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<String, Object> devicesMap = new HashMap<>();
|
|
|
|
|
devicesMap.put("PendingDevices", pendingDevices);
|
|
|
|
|
devicesMap.put("InstalledDevices", installedDevices);
|
|
|
|
|
devicesMap.put("ErrorDevices", errorDevices);
|
|
|
|
|
devicesMap.put("NewDevices", newDevices);
|
|
|
|
|
|
|
|
|
|
groupDetailMap.put("Devices", devicesMap);
|
|
|
|
|
groupDetailMap.put("StatusPercentages", statusPercentages);
|
|
|
|
|
|
|
|
|
|
groupDetailsWithDevices.add(groupDetailMap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return groupDetailsWithDevices;
|
|
|
|
|
} catch (ApplicationManagementDAOException e) {
|
|
|
|
|
String msg = "Error occurred while fetching groups and devices for UUID: " + uuid;
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new ApplicationManagementException(msg, e);
|
|
|
|
|
} catch (DBConnectionException e) {
|
|
|
|
|
String msg = "DB Connection error occurred while fetching groups and devices for UUID: " + uuid;
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new ApplicationManagementException(msg, e);
|
|
|
|
|
} catch (GroupManagementException e) {
|
|
|
|
|
String msg = "Error occurred while fetching group details and device IDs: " + e.getMessage();
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new ApplicationManagementException(msg, e);
|
|
|
|
|
} finally {
|
|
|
|
|
ConnectionManagerUtil.closeDBConnection();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<Map<String, Object>> getUserSubscriptionsByUUID(String uuid, String subscriptionStatus) throws ApplicationManagementException {
|
|
|
|
|
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
|
|
|
|
|
boolean unsubscribe = subscriptionStatus.equals("unsubscribed");
|
|
|
|
|
try {
|
|
|
|
|
ConnectionManagerUtil.openDBConnection();
|
|
|
|
|
List<Map<String, Object>> userSubscriptions = subscriptionDAO.getUserSubscriptionsByUUID(uuid, unsubscribe, tenantId);
|
|
|
|
|
List<Map<String, Object>> userSubscriptionsWithDevices = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService();
|
|
|
|
|
|
|
|
|
|
for (Map<String, Object> userSubscription : userSubscriptions) {
|
|
|
|
|
String userName = (String) userSubscription.get("UserName");
|
|
|
|
|
|
|
|
|
|
// Retrieve owner details and device IDs for the user using the service layer
|
|
|
|
|
Map<String, Object> ownerDetailsWithDevices = deviceManagementProviderService.getOwnersWithDeviceIds(userName);
|
|
|
|
|
|
|
|
|
|
Map<String, Object> userSubscriptionMap = new HashMap<>();
|
|
|
|
|
userSubscriptionMap.put("UserName", userSubscription.get("UserName"));
|
|
|
|
|
userSubscriptionMap.put("SubscribedBy", userSubscription.get("SubscribedBy"));
|
|
|
|
|
userSubscriptionMap.put("SubscribedTimestamp", userSubscription.get("SubscribedTimestamp"));
|
|
|
|
|
userSubscriptionMap.put("Unsubscribed", userSubscription.get("Unsubscribed"));
|
|
|
|
|
userSubscriptionMap.put("UnsubscribedBy", userSubscription.get("UnsubscribedBy"));
|
|
|
|
|
userSubscriptionMap.put("UnsubscribedTimestamp", userSubscription.get("UnsubscribedTimestamp"));
|
|
|
|
|
userSubscriptionMap.put("AppReleaseId", userSubscription.get("AppReleaseId"));
|
|
|
|
|
|
|
|
|
|
userSubscriptionMap.put("DeviceCount", ownerDetailsWithDevices.get("DeviceCount"));
|
|
|
|
|
|
|
|
|
|
// Fetch device subscriptions for each device ID associated with the user
|
|
|
|
|
List<Map<String, Object>> pendingDevices = new ArrayList<>();
|
|
|
|
|
List<Map<String, Object>> installedDevices = new ArrayList<>();
|
|
|
|
|
List<Map<String, Object>> errorDevices = new ArrayList<>();
|
|
|
|
|
List<Map<String, Object>> newDevices = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
List<Integer> deviceIds = (List<Integer>) ownerDetailsWithDevices.get("DeviceIds");
|
|
|
|
|
Map<String, Integer> statusCounts = new HashMap<>();
|
|
|
|
|
statusCounts.put("PENDING", 0);
|
|
|
|
|
statusCounts.put("COMPLETED", 0);
|
|
|
|
|
statusCounts.put("ERROR", 0);
|
|
|
|
|
statusCounts.put("NEW", 0);
|
|
|
|
|
|
|
|
|
|
for (Integer deviceId : deviceIds) {
|
|
|
|
|
List<DeviceSubscriptionDTO> deviceSubscriptions = subscriptionDAO.getDeviceSubscriptions(
|
|
|
|
|
(Integer) userSubscription.get("AppReleaseId"), tenantId, null, null);
|
|
|
|
|
boolean isNewDevice = true;
|
|
|
|
|
for (DeviceSubscriptionDTO subscription : deviceSubscriptions) {
|
|
|
|
|
if (subscription.getDeviceId() == deviceId) {
|
|
|
|
|
Map<String, Object> deviceDetailMap = new HashMap<>();
|
|
|
|
|
deviceDetailMap.put("DeviceID", subscription.getDeviceId());
|
|
|
|
|
// deviceDetailMap.put("Status", subscription.getStatus());
|
|
|
|
|
|
|
|
|
|
String status = subscription.getStatus();
|
|
|
|
|
switch (status) {
|
|
|
|
|
case "COMPLETED":
|
|
|
|
|
installedDevices.add(deviceDetailMap);
|
|
|
|
|
statusCounts.put("COMPLETED", statusCounts.get("COMPLETED") + 1);
|
|
|
|
|
break;
|
|
|
|
|
case "ERROR":
|
|
|
|
|
case "INVALID":
|
|
|
|
|
case "UNAUTHORIZED":
|
|
|
|
|
errorDevices.add(deviceDetailMap);
|
|
|
|
|
statusCounts.put("ERROR", statusCounts.get("ERROR") + 1);
|
|
|
|
|
break;
|
|
|
|
|
case "IN_PROGRESS":
|
|
|
|
|
case "PENDING":
|
|
|
|
|
case "REPEATED":
|
|
|
|
|
pendingDevices.add(deviceDetailMap);
|
|
|
|
|
statusCounts.put("PENDING", statusCounts.get("PENDING") + 1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
isNewDevice = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (isNewDevice) {
|
|
|
|
|
Map<String, Object> newDeviceDetailMap = new HashMap<>();
|
|
|
|
|
newDeviceDetailMap.put("DeviceID", deviceId);
|
|
|
|
|
newDevices.add(newDeviceDetailMap);
|
|
|
|
|
statusCounts.put("NEW", statusCounts.get("NEW") + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int totalDevices = deviceIds.size();
|
|
|
|
|
Map<String, Double> statusPercentages = new HashMap<>();
|
|
|
|
|
for (Map.Entry<String, Integer> entry : statusCounts.entrySet()) {
|
|
|
|
|
double percentage = ((double) entry.getValue() / totalDevices) * 100;
|
|
|
|
|
statusPercentages.put(entry.getKey(), percentage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<String, Object> devicesMap = new HashMap<>();
|
|
|
|
|
devicesMap.put("PendingDevices", pendingDevices);
|
|
|
|
|
devicesMap.put("InstalledDevices", installedDevices);
|
|
|
|
|
devicesMap.put("ErrorDevices", errorDevices);
|
|
|
|
|
devicesMap.put("NewDevices", newDevices);
|
|
|
|
|
|
|
|
|
|
userSubscriptionMap.put("Devices", devicesMap);
|
|
|
|
|
userSubscriptionMap.put("StatusPercentages", statusPercentages);
|
|
|
|
|
|
|
|
|
|
userSubscriptionsWithDevices.add(userSubscriptionMap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return userSubscriptionsWithDevices;
|
|
|
|
|
} catch (ApplicationManagementDAOException e) {
|
|
|
|
|
String msg = "Error occurred while getting user subscriptions for the application release UUID: " + uuid;
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new ApplicationManagementException(msg, e);
|
|
|
|
|
} catch (DBConnectionException e) {
|
|
|
|
|
String msg = "DB Connection error occurred while getting user subscriptions for UUID: " + uuid;
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new ApplicationManagementException(msg, e);
|
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
} finally {
|
|
|
|
|
ConnectionManagerUtil.closeDBConnection();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<Map<String, Object>> getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus) throws ApplicationManagementException {
|
|
|
|
|
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
|
|
|
|
|
boolean unsubscribe = subscriptionStatus.equals("unsubscribed");
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
ConnectionManagerUtil.openDBConnection();
|
|
|
|
|
List<Map<String, Object>> roleSubscriptions = subscriptionDAO.getRoleSubscriptionsByUUID(uuid, unsubscribe, tenantId);
|
|
|
|
|
List<Map<String, Object>> roleSubscriptionsWithDevices = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService();
|
|
|
|
|
|
|
|
|
|
for (Map<String, Object> roleSubscription : roleSubscriptions) {
|
|
|
|
|
String roleName = (String) roleSubscription.get("RoleName");
|
|
|
|
|
Map<String, Object> roleSubscriptionMap = new HashMap<>();
|
|
|
|
|
roleSubscriptionMap.put("RoleName", roleName);
|
|
|
|
|
roleSubscriptionMap.put("SubscribedBy", roleSubscription.get("SubscribedBy"));
|
|
|
|
|
roleSubscriptionMap.put("SubscribedTimestamp", roleSubscription.get("SubscribedTimestamp"));
|
|
|
|
|
roleSubscriptionMap.put("Unsubscribed", roleSubscription.get("Unsubscribed"));
|
|
|
|
|
roleSubscriptionMap.put("UnsubscribedBy", roleSubscription.get("UnsubscribedBy"));
|
|
|
|
|
roleSubscriptionMap.put("UnsubscribedTimestamp", roleSubscription.get("UnsubscribedTimestamp"));
|
|
|
|
|
roleSubscriptionMap.put("AppReleaseId", roleSubscription.get("AppReleaseId"));
|
|
|
|
|
|
|
|
|
|
List<Map<String, Object>> pendingDevices = new ArrayList<>();
|
|
|
|
|
List<Map<String, Object>> installedDevices = new ArrayList<>();
|
|
|
|
|
List<Map<String, Object>> errorDevices = new ArrayList<>();
|
|
|
|
|
List<Map<String, Object>> newDevices = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
Map<String, Integer> statusCounts = new HashMap<>();
|
|
|
|
|
statusCounts.put("PENDING", 0);
|
|
|
|
|
statusCounts.put("COMPLETED", 0);
|
|
|
|
|
statusCounts.put("ERROR", 0);
|
|
|
|
|
statusCounts.put("NEW", 0);
|
|
|
|
|
|
|
|
|
|
List<String> users = this.getUsersForRole(roleName);
|
|
|
|
|
|
|
|
|
|
for (String user : users) {
|
|
|
|
|
// Retrieve owner details and device IDs for the user using the service layer
|
|
|
|
|
Map<String, Object> ownerDetailsWithDevices;
|
|
|
|
|
try {
|
|
|
|
|
ownerDetailsWithDevices = deviceManagementProviderService.getOwnersWithDeviceIds(user);
|
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
|
throw new ApplicationManagementException("Error retrieving owner details with devices for user: " + user, e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fetch device subscriptions for each device ID associated with the user
|
|
|
|
|
List<Integer> deviceIds = (List<Integer>) ownerDetailsWithDevices.get("DeviceIds");
|
|
|
|
|
for (Integer deviceId : deviceIds) {
|
|
|
|
|
List<DeviceSubscriptionDTO> deviceSubscriptions;
|
|
|
|
|
try {
|
|
|
|
|
deviceSubscriptions = subscriptionDAO.getDeviceSubscriptions(
|
|
|
|
|
(Integer) roleSubscription.get("AppReleaseId"), tenantId, null, null);
|
|
|
|
|
} catch (ApplicationManagementDAOException e) {
|
|
|
|
|
throw new ApplicationManagementException("Error retrieving device subscriptions", e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boolean isNewDevice = true;
|
|
|
|
|
for (DeviceSubscriptionDTO deviceSubscription : deviceSubscriptions) {
|
|
|
|
|
if (deviceSubscription.getDeviceId() == deviceId) {
|
|
|
|
|
Map<String, Object> deviceDetailMap = new HashMap<>();
|
|
|
|
|
deviceDetailMap.put("DeviceID", deviceSubscription.getDeviceId());
|
|
|
|
|
// deviceDetailMap.put("Status", deviceSubscription.getStatus());
|
|
|
|
|
|
|
|
|
|
String status = deviceSubscription.getStatus();
|
|
|
|
|
switch (status) {
|
|
|
|
|
case "COMPLETED":
|
|
|
|
|
installedDevices.add(deviceDetailMap);
|
|
|
|
|
statusCounts.put("COMPLETED", statusCounts.get("COMPLETED") + 1);
|
|
|
|
|
break;
|
|
|
|
|
case "ERROR":
|
|
|
|
|
case "INVALID":
|
|
|
|
|
case "UNAUTHORIZED":
|
|
|
|
|
errorDevices.add(deviceDetailMap);
|
|
|
|
|
statusCounts.put("ERROR", statusCounts.get("ERROR") + 1);
|
|
|
|
|
break;
|
|
|
|
|
case "IN_PROGRESS":
|
|
|
|
|
case "PENDING":
|
|
|
|
|
case "REPEATED":
|
|
|
|
|
pendingDevices.add(deviceDetailMap);
|
|
|
|
|
statusCounts.put("PENDING", statusCounts.get("PENDING") + 1);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
isNewDevice = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (isNewDevice) {
|
|
|
|
|
Map<String, Object> newDeviceDetailMap = new HashMap<>();
|
|
|
|
|
newDeviceDetailMap.put("DeviceID", deviceId);
|
|
|
|
|
newDevices.add(newDeviceDetailMap);
|
|
|
|
|
statusCounts.put("NEW", statusCounts.get("NEW") + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int totalDevices = pendingDevices.size() + installedDevices.size() + errorDevices.size() + newDevices.size();
|
|
|
|
|
Map<String, Double> statusPercentages = new HashMap<>();
|
|
|
|
|
for (Map.Entry<String, Integer> entry : statusCounts.entrySet()) {
|
|
|
|
|
double percentage = totalDevices == 0 ? 0.0 : ((double) entry.getValue() / totalDevices) * 100;
|
|
|
|
|
statusPercentages.put(entry.getKey(), percentage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Map<String, Object> devicesMap = new HashMap<>();
|
|
|
|
|
devicesMap.put("PendingDevices", pendingDevices);
|
|
|
|
|
devicesMap.put("InstalledDevices", installedDevices);
|
|
|
|
|
devicesMap.put("ErrorDevices", errorDevices);
|
|
|
|
|
devicesMap.put("NewDevices", newDevices);
|
|
|
|
|
|
|
|
|
|
roleSubscriptionMap.put("Devices", devicesMap);
|
|
|
|
|
roleSubscriptionMap.put("StatusPercentages", statusPercentages);
|
|
|
|
|
roleSubscriptionMap.put("DeviceCount", totalDevices);
|
|
|
|
|
|
|
|
|
|
roleSubscriptionsWithDevices.add(roleSubscriptionMap);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return roleSubscriptionsWithDevices;
|
|
|
|
|
} catch (ApplicationManagementDAOException e) {
|
|
|
|
|
String msg = "Error occurred in retrieving role subscriptions with devices";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new ApplicationManagementException(msg, e);
|
|
|
|
|
} catch (DBConnectionException e) {
|
|
|
|
|
String msg = "Error occurred while retrieving the database connection";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new ApplicationManagementException(msg, e);
|
|
|
|
|
} catch (UserStoreException e) {
|
|
|
|
|
String msg = "Error occurred while retrieving users for role";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new ApplicationManagementException(msg, e);
|
|
|
|
|
} finally {
|
|
|
|
|
ConnectionManagerUtil.closeDBConnection();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get user list for each role
|
|
|
|
|
public List<String> getUsersForRole(String roleName) throws UserStoreException {
|
|
|
|
|
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
|
|
|
|
int tenantId = ctx.getTenantId();
|
|
|
|
|
UserStoreManager userStoreManager = DataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId).getUserStoreManager();
|
|
|
|
|
String[] users = userStoreManager.getUserListOfRole(roleName);
|
|
|
|
|
return Arrays.asList(users);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<Map<String, Object>> getDeviceSubscriptionsOperationsByUUID(String uuid) throws ApplicationManagementException {
|
|
|
|
|
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
|
|
|
|
|
if (uuid == null || uuid.isEmpty()) {
|
|
|
|
|
throw new IllegalArgumentException("UUID cannot be null or empty.");
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
ConnectionManagerUtil.openDBConnection();
|
|
|
|
|
DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService();
|
|
|
|
|
List<Map<String, Object>> deviceSubscriptions = subscriptionDAO.getDeviceSubscriptionsOperationsByUUID(uuid, tenantId);
|
|
|
|
|
for (Map<String, Object> deviceSubscription : deviceSubscriptions) {
|
|
|
|
|
Integer operationId = (Integer) deviceSubscription.get("OperationId");
|
|
|
|
|
if (operationId != null) {
|
|
|
|
|
Map<String, Object> operationDetails = deviceManagementProviderService.getOperationDetailsById(operationId);
|
|
|
|
|
if (operationDetails != null) {
|
|
|
|
|
deviceSubscription.put("OperationCode", operationDetails.get("OperationCode"));
|
|
|
|
|
deviceSubscription.put("OperationDetails", operationDetails.get("OperationDetails"));
|
|
|
|
|
deviceSubscription.put("OperationProperties", operationDetails.get("OperationProperties"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return deviceSubscriptions;
|
|
|
|
|
} catch (ApplicationManagementDAOException e) {
|
|
|
|
|
String msg = "Error occurred while retrieving device subscriptions for UUID: " + uuid;
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new ApplicationManagementException(msg, e);
|
|
|
|
|
} catch (DBConnectionException e) {
|
|
|
|
|
String msg = "Error occurred while retrieving the database connection";
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
throw new ApplicationManagementException(msg, e);
|
|
|
|
|
} catch (OperationManagementException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
} finally {
|
|
|
|
|
ConnectionManagerUtil.closeDBConnection();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|