Add device filtering capabilities

pull/455/head
Rajitha Kumara 4 months ago
parent 4fc14edf1e
commit 5f0e18f1bf

@ -0,0 +1,77 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package io.entgra.device.mgt.core.application.mgt.common;
public class DeviceSubscription {
private String deviceIdentifier;
private String deviceStatus;
private String deviceOwner;
private String deviceType;
private String ownershipType;
private SubscriptionData subscriptionData;
public String getDeviceIdentifier() {
return deviceIdentifier;
}
public void setDeviceIdentifier(String deviceIdentifier) {
this.deviceIdentifier = deviceIdentifier;
}
public String getDeviceStatus() {
return deviceStatus;
}
public void setDeviceStatus(String deviceStatus) {
this.deviceStatus = deviceStatus;
}
public String getDeviceOwner() {
return deviceOwner;
}
public void setDeviceOwner(String deviceOwner) {
this.deviceOwner = deviceOwner;
}
public String getDeviceType() {
return deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
public String getOwnershipType() {
return ownershipType;
}
public void setOwnershipType(String ownershipType) {
this.ownershipType = ownershipType;
}
public SubscriptionData getSubscriptionData() {
return subscriptionData;
}
public void setSubscriptionData(SubscriptionData subscriptionData) {
this.subscriptionData = subscriptionData;
}
}

@ -0,0 +1,61 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package io.entgra.device.mgt.core.application.mgt.common;
import java.sql.Timestamp;
public class SubscriptionData {
private String deviceSubscriptionStatus;
private String triggeredBy;
private String subscriptionType;
private Timestamp triggeredAt;
public String getDeviceSubscriptionStatus() {
return deviceSubscriptionStatus;
}
public void setDeviceSubscriptionStatus(String deviceSubscriptionStatus) {
this.deviceSubscriptionStatus = deviceSubscriptionStatus;
}
public String getTriggeredBy() {
return triggeredBy;
}
public void setTriggeredBy(String triggeredBy) {
this.triggeredBy = triggeredBy;
}
public String getSubscriptionType() {
return subscriptionType;
}
public void setSubscriptionType(String subscriptionType) {
this.subscriptionType = subscriptionType;
}
public Timestamp getTriggeredAt() {
return triggeredAt;
}
public void setTriggeredAt(Timestamp triggeredAt) {
this.triggeredAt = triggeredAt;
}
}

@ -38,6 +38,11 @@ public class DeviceSubscriptionDTO {
public DeviceSubscriptionDTO() {
}
public DeviceSubscriptionDTO(int deviceId) {
this.deviceId = deviceId;
}
public DeviceSubscriptionDTO(int deviceId, String status) {
this.deviceId = deviceId;
this.status = status;

@ -19,6 +19,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.DeviceSubscription;
import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData;
import io.entgra.device.mgt.core.application.mgt.common.ExecutionStatus;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity;
@ -269,7 +270,7 @@ public interface SubscriptionManager {
List<SubscriptionEntity> getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException;
List<DeviceSubscriptionData> getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
List<DeviceSubscription> getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException;
/**

@ -23,6 +23,7 @@ import io.entgra.device.mgt.core.application.mgt.common.ApplicationInstallRespon
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.DeviceSubscription;
import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo;
@ -1729,7 +1730,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
}
@Override
public List<DeviceSubscriptionData> getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
public List<DeviceSubscription> getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException {
SubscriptionManagementHelperService subscriptionManagementHelperService =
SubscriptionManagementServiceProvider.getInstance().getSubscriptionManagementHelperService(subscriptionInfo);

@ -19,21 +19,52 @@
package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt;
import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription;
import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData;
import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilterCriteria;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionData;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo;
import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO;
import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil;
import io.entgra.device.mgt.core.device.mgt.common.Device;
import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class SubscriptionManagementHelperUtil {
public static List<DeviceSubscriptionData> getDeviceSubscriptionData(List<DeviceSubscriptionDTO> deviceSubscriptionDTOS,
DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria) {
// todo: filtering
for (DeviceSubscriptionDTO deviceSubscriptionDTO : deviceSubscriptionDTOS) {
public static List<DeviceSubscription> getDeviceSubscriptionData(List<DeviceSubscriptionDTO> deviceSubscriptionDTOS,
DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria) throws DeviceManagementException {
List<Integer> deviceIds = deviceSubscriptionDTOS.stream().map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList());
PaginationRequest paginationRequest = new PaginationRequest(0, -1);
paginationRequest.setDeviceName(deviceSubscriptionFilterCriteria.getName());
paginationRequest.setDeviceStatus(deviceSubscriptionFilterCriteria.getDeviceStatus());
paginationRequest.setOwner(deviceSubscriptionFilterCriteria.getOwner());
List<Device> devices = HelperUtil.getDeviceManagementProviderService().getDevicesByDeviceIds(paginationRequest, deviceIds);
return populateDeviceData(deviceSubscriptionDTOS, devices);
}
private static List<DeviceSubscription> populateDeviceData(List<DeviceSubscriptionDTO> deviceSubscriptionDTOS, List<Device> devices) {
List<DeviceSubscription> deviceSubscriptions = new ArrayList<>();
for (Device device : devices) {
int idx = deviceSubscriptionDTOS.indexOf(new DeviceSubscriptionDTO(device.getId()));
if (idx >= 0) {
DeviceSubscriptionDTO deviceSubscriptionDTO = deviceSubscriptionDTOS.get(idx);
DeviceSubscription deviceSubscription = new DeviceSubscription();
deviceSubscription.setDeviceIdentifier(device.getDeviceIdentifier());
deviceSubscription.setDeviceOwner(device.getEnrolmentInfo().getOwner());
deviceSubscription.setDeviceType(device.getType());
SubscriptionData subscriptionData = new SubscriptionData();
subscriptionData.setTriggeredBy(deviceSubscriptionDTO.getActionTriggeredFrom());
subscriptionData.setTriggeredAt(deviceSubscriptionDTO.getSubscribedTimestamp());
subscriptionData.setSubscriptionType(deviceSubscriptionDTO.getStatus());
deviceSubscription.setSubscriptionData(subscriptionData);
deviceSubscriptions.add(deviceSubscription);
}
}
return null;
return deviceSubscriptions;
}
public static String getDeviceSubscriptionStatus(SubscriptionInfo subscriptionInfo) {

@ -19,6 +19,7 @@
package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl;
import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription;
import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo;
@ -31,7 +32,7 @@ import java.util.List;
public class DeviceBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService {
@Override
public List<DeviceSubscriptionData> getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException {
public List<DeviceSubscription> getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException {
return null;
}

@ -19,6 +19,7 @@
package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl;
import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription;
import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData;
import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilterCriteria;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity;
@ -34,6 +35,7 @@ import io.entgra.device.mgt.core.application.mgt.core.util.ConnectionManagerUtil
import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil;
import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.SubscriptionManagementHelperUtil;
import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service.SubscriptionManagementHelperService;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException;
import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException;
import io.entgra.device.mgt.core.device.mgt.core.dto.GroupDetailsDTO;
import io.entgra.device.mgt.core.device.mgt.core.service.GroupManagementProviderService;
@ -57,13 +59,13 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr
}
@Override
public List<DeviceSubscriptionData> getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
public List<DeviceSubscription> getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException {
final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus());
List<DeviceSubscriptionDTO> deviceSubscriptionDTOS;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
// todo: check and refactor
try {
ConnectionManagerUtil.openDBConnection();
ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO.
@ -107,6 +109,8 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByGroup, deviceSubscriptionStatus, subscriptionInfo.getSubscriptionType(),
deviceSubscriptionFilterCriteria.getTriggeredBy(), deviceSubscriptionStatus, limit, offset);
return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria());
} catch (GroupManagementException e) {
String msg = "Error encountered while retrieving group details for group: " + subscriptionInfo.getIdentifier();
@ -116,12 +120,12 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr
String msg = "Error encountered while connecting to the database";
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (DeviceManagementException e) {
throw new RuntimeException(e);
} finally {
ConnectionManagerUtil.closeDBConnection();
}
return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria());
}
@Override

@ -19,6 +19,7 @@
package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl;
import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription;
import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData;
import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilterCriteria;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity;
@ -64,13 +65,14 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri
@SuppressWarnings("unchecked")
@Override
public List<DeviceSubscriptionData> getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
public List<DeviceSubscription> getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException {
final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus());
List<DeviceSubscriptionDTO> deviceSubscriptionDTOS;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
ConnectionManagerUtil.openDBConnection();
UserStoreManager userStoreManager = DataHolder.getInstance().getRealmService().
getTenantUserRealm(tenantId).getUserStoreManager();
String[] usersWithRole =
@ -89,7 +91,6 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri
List<Integer> deviceIdsOwnByRole = deviceListOwnByRole.stream().map(Device::getId).collect(Collectors.toList());
ConnectionManagerUtil.openDBConnection();
ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO.
getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId);
if (applicationReleaseDTO == null) {
@ -102,22 +103,23 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri
String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo);
DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria();
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByRole, null,
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(),
null, limit, offset);
if (Objects.equals("NEW", deviceSubscriptionStatus)) {
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByRole, null,
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(),
null, limit, offset);
for (DeviceSubscriptionDTO deviceSubscriptionDTO: deviceSubscriptionDTOS) {
deviceListOwnByRole.remove(new Device(deviceSubscriptionDTO.getId()));
}
deviceIdsOwnByRole = deviceListOwnByRole.stream().map(Device::getId).collect(Collectors.toList());
List<Integer> deviceIdsOfSubscription = deviceSubscriptionDTOS.stream().
map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList());
List<Integer> newDeviceIds = HelperUtil.getDeviceManagementProviderService().
getDevicesNotInGivenIdList(deviceIdsOfSubscription, new PaginationRequest(offset, limit));
deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList());
}
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByRole, deviceSubscriptionStatus, subscriptionInfo.getSubscriptionType(),
deviceSubscriptionFilterCriteria.getTriggeredBy(), deviceSubscriptionStatus, limit, offset);
return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria());
} catch (UserStoreException e) {
String msg = "Error encountered while getting the user management store for tenant id " + tenantId;
@ -134,8 +136,6 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri
} finally {
ConnectionManagerUtil.closeDBConnection();
}
return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria());
}
@Override

@ -19,6 +19,7 @@
package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl;
import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription;
import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo;
@ -29,9 +30,234 @@ import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.serv
import java.util.List;
/*
// @Override
// public List<SubscriptionsDTO> getUserSubscriptionsByUUID(String uuid, String subscriptionStatus,
// PaginationRequest request, int offset, int limit)
// throws ApplicationManagementException {
// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
// boolean unsubscribe = subscriptionStatus.equals("unsubscribed");
// String status;
//
// try {
// ConnectionManagerUtil.openDBConnection();
//
// ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO.getReleaseByUUID(uuid, tenantId);
// if (applicationReleaseDTO == null) {
// String msg = "Couldn't find an application release for application release UUID: " + uuid;
// log.error(msg);
// throw new NotFoundException(msg);
// }
// ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(uuid, tenantId);
// int appReleaseId = applicationReleaseDTO.getId();
// List<SubscriptionsDTO> userSubscriptionsWithDevices = new ArrayList<>();
//
// List<SubscriptionsDTO> userSubscriptions =
// subscriptionDAO.getUserSubscriptionsByAppReleaseID(appReleaseId, unsubscribe, tenantId, offset, limit);
// if (userSubscriptions == null) {
// throw new ApplicationManagementException("User details not found for appReleaseId: " + appReleaseId);
// }
//
// DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService();
//
// for (SubscriptionsDTO userSubscription : userSubscriptions) {
//
// if (StringUtils.isNotBlank(request.getUserName()) && !request.getUserName().equals(userSubscription.getName())) {
// continue;
// }
//
// String userName = StringUtils.isNotBlank(request.getUserName()) ? request.getUserName() : userSubscription.getName();
//
// // Retrieve owner details and device IDs for the user using the service layer
// OwnerWithDeviceDTO ownerDetailsWithDevices =
// deviceManagementProviderService.getOwnersWithDeviceIds(userName, applicationDTO.getDeviceTypeId(),
// request.getOwner(), request.getDeviceName(), request.getDeviceStatus());
//
// SubscriptionsDTO userSubscriptionDTO = new SubscriptionsDTO();
// userSubscriptionDTO.setName(userSubscription.getName());
// userSubscriptionDTO.setSubscribedBy(userSubscription.getSubscribedBy());
// userSubscriptionDTO.setSubscribedTimestamp(userSubscription.getSubscribedTimestamp());
// userSubscriptionDTO.setUnsubscribed(userSubscription.getUnsubscribed());
// userSubscriptionDTO.setUnsubscribedBy(userSubscription.getUnsubscribedBy());
// userSubscriptionDTO.setUnsubscribedTimestamp(userSubscription.getUnsubscribedTimestamp());
// userSubscriptionDTO.setAppReleaseId(userSubscription.getAppReleaseId());
//
// userSubscriptionDTO.setDeviceCount(ownerDetailsWithDevices.getDeviceCount());
//
// // Fetch device subscriptions for each device ID associated with the user
// List<DeviceSubscriptionData> pendingDevices = new ArrayList<>();
// List<DeviceSubscriptionData> installedDevices = new ArrayList<>();
// List<DeviceSubscriptionData> errorDevices = new ArrayList<>();
// List<DeviceSubscriptionData> newDevices = new ArrayList<>();
// List<DeviceSubscriptionData> subscribedDevices = new ArrayList<>();
//
// List<Integer> deviceIds = ownerDetailsWithDevices.getDeviceIds();
// Map<String, Integer> statusCounts = new HashMap<>();
// statusCounts.put("PENDING", 0);
// statusCounts.put("COMPLETED", 0);
// statusCounts.put("ERROR", 0);
// statusCounts.put("NEW", 0);
// statusCounts.put("SUBSCRIBED", 0);
//
// List<DeviceSubscriptionDTO> subscribedDeviceSubscriptions = new ArrayList<>();
// if (unsubscribe) {
// subscribedDeviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds(
// appReleaseId, !unsubscribe, tenantId, deviceIds, request.getActionStatus(), request.getActionType(),
// request.getActionTriggeredBy(), request.getTabActionStatus());
// }
//
// for (Integer deviceId : deviceIds) {
// List<DeviceSubscriptionDTO> deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds(
// userSubscription.getAppReleaseId(), unsubscribe, tenantId, deviceIds, request.getActionStatus(), request.getActionType(),
// request.getActionTriggeredBy(), request.getTabActionStatus());
// OwnerWithDeviceDTO ownerWithDeviceByDeviceId =
// deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId, request.getOwner(), request.getDeviceName(),
// request.getDeviceStatus());
// if (ownerWithDeviceByDeviceId == null) {
// continue;
// }
// boolean isNewDevice = true;
// for (DeviceSubscriptionDTO subscription : deviceSubscriptions) {
// if (subscription.getDeviceId() == deviceId) {
// DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData();
// deviceDetail.setDeviceId(subscription.getDeviceId());
// deviceDetail.setSubId(subscription.getId());
// deviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName());
// deviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus());
// deviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames());
// deviceDetail.setActionType(subscription.getActionTriggeredFrom());
// deviceDetail.setStatus(subscription.getStatus());
// deviceDetail.setActionType(subscription.getActionTriggeredFrom());
// deviceDetail.setActionTriggeredBy(subscription.getSubscribedBy());
// deviceDetail.setActionTriggeredTimestamp(subscription.getSubscribedTimestamp());
// deviceDetail.setUnsubscribed(subscription.isUnsubscribed());
// deviceDetail.setUnsubscribedBy(subscription.getUnsubscribedBy());
// deviceDetail.setUnsubscribedTimestamp(subscription.getUnsubscribedTimestamp());
// deviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes());
// deviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers());
//
// status = subscription.getStatus();
// switch (status) {
// case "COMPLETED":
// installedDevices.add(deviceDetail);
// statusCounts.put("COMPLETED", statusCounts.get("COMPLETED") + 1);
// break;
// case "ERROR":
// case "INVALID":
// case "UNAUTHORIZED":
// errorDevices.add(deviceDetail);
// statusCounts.put("ERROR", statusCounts.get("ERROR") + 1);
// break;
// case "IN_PROGRESS":
// case "PENDING":
// case "REPEATED":
// pendingDevices.add(deviceDetail);
// statusCounts.put("PENDING", statusCounts.get("PENDING") + 1);
// break;
// }
// isNewDevice = false;
// }
// }
// if (isNewDevice) {
// boolean isSubscribedDevice = false;
// for (DeviceSubscriptionDTO subscribedDevice : subscribedDeviceSubscriptions) {
// if (subscribedDevice.getDeviceId() == deviceId) {
// DeviceSubscriptionData subscribedDeviceDetail = new DeviceSubscriptionData();
// subscribedDeviceDetail.setDeviceId(subscribedDevice.getDeviceId());
// subscribedDeviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames());
// subscribedDeviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName());
// subscribedDeviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus());
// subscribedDeviceDetail.setSubId(subscribedDevice.getId());
// subscribedDeviceDetail.setActionTriggeredBy(subscribedDevice.getSubscribedBy());
// subscribedDeviceDetail.setActionTriggeredTimestamp(subscribedDevice.getSubscribedTimestamp());
// subscribedDeviceDetail.setActionType(subscribedDevice.getActionTriggeredFrom());
// subscribedDeviceDetail.setStatus(subscribedDevice.getStatus());
// subscribedDeviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes());
// subscribedDeviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers());
// subscribedDevices.add(subscribedDeviceDetail);
// statusCounts.put("SUBSCRIBED", statusCounts.get("SUBSCRIBED") + 1);
// isSubscribedDevice = true;
// break;
// }
// }
// if (!isSubscribedDevice) {
// DeviceSubscriptionData newDeviceDetail = new DeviceSubscriptionData();
// newDeviceDetail.setDeviceId(deviceId);
// newDeviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName());
// newDeviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus());
// newDeviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames());
// newDeviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes());
// newDeviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers());
// newDevices.add(newDeviceDetail);
// 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;
// String formattedPercentage = String.format("%.2f", percentage);
// statusPercentages.put(entry.getKey(), Double.valueOf(formattedPercentage));
// }
//
// List<DeviceSubscriptionData> requestedDevices = new ArrayList<>();
// if (StringUtils.isNotBlank(request.getTabActionStatus())) {
// switch (request.getTabActionStatus()) {
// case "COMPLETED":
// requestedDevices = installedDevices;
// break;
// case "PENDING":
// requestedDevices = pendingDevices;
// break;
// case "ERROR":
// requestedDevices = errorDevices;
// break;
// case "NEW":
// requestedDevices = newDevices;
// break;
// case "SUBSCRIBED":
// requestedDevices = subscribedDevices;
// break;
// }
// userSubscriptionDTO.setDevices(new CategorizedSubscriptionResult(requestedDevices, request.getTabActionStatus()));
// } else {
// CategorizedSubscriptionResult categorizedSubscriptionResult;
// if (subscribedDevices.isEmpty()) {
// categorizedSubscriptionResult =
// new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices);
// } else {
// categorizedSubscriptionResult =
// new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices,
// subscribedDevices);
// }
// userSubscriptionDTO.setDevices(categorizedSubscriptionResult);
// userSubscriptionDTO.setStatusPercentages(statusPercentages);
//
// }
// userSubscriptionsWithDevices.add(userSubscriptionDTO);
// }
// 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();
// }
// }
*/
public class UserBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService {
@Override
public List<DeviceSubscriptionData> getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException {
public List<DeviceSubscription> getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException {
return null;
}

@ -19,6 +19,7 @@
package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service;
import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription;
import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo;
@ -36,7 +37,7 @@ public interface SubscriptionManagementHelperService {
SubscriptionDAO subscriptionDAO = ApplicationManagementDAOFactory.getSubscriptionDAO();
ApplicationDAO applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO();
ApplicationReleaseDAO applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO();
List<DeviceSubscriptionData> getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
List<DeviceSubscription> getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException;
List<SubscriptionEntity> getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException;

@ -864,4 +864,10 @@ public interface DeviceDAO {
* @throws DeviceManagementDAOException
*/
int getCountOfDevicesNotInGroup(PaginationRequest request, int tenantId) throws DeviceManagementDAOException;
List<Integer> getDevicesNotInGivenIdList(PaginationRequest request, List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException;
List<Device> getDevicesByDeviceIds(PaginationRequest paginationRequest, List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException;
}

@ -54,6 +54,7 @@ import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import java.util.Random;
import java.util.stream.Collectors;
public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
@ -3298,4 +3299,97 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
throw new DeviceManagementDAOException(msg, e);
}
}
@Override
public List<Integer> getDevicesNotInGivenIdList(PaginationRequest request, List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException {
List<Integer> filteredDeviceIds = new ArrayList<>();
String deviceIdStringList = deviceIds.stream().map(String::valueOf).collect(Collectors.joining(","));
try {
Connection connection = getConnection();
String sql = "SELECT ID AS DEVICE_ID FROM DM_DEVICE WHERE ID NOT IN " +
"(" + deviceIdStringList + ") LIMIT ? OFFSET ?";
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setInt(1, request.getRowCount());
preparedStatement.setInt(2, request.getStartIndex());
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
filteredDeviceIds.add(resultSet.getInt("DEVICE_ID"));
}
}
return filteredDeviceIds;
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving device ids not in: " + filteredDeviceIds;
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
@Override
public List<Device> getDevicesByDeviceIds(PaginationRequest paginationRequest, List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException {
List<Device> devices = new ArrayList<>();
String deviceIdStringList = deviceIds.stream().map(String::valueOf).collect(Collectors.joining(","));
boolean isOwnerProvided = false;
boolean isDeviceStatusProvided = false;
boolean isDeviceNameProvided = false;
try {
Connection connection = getConnection();
String sql = "SELECT e.DEVICE_ID, d.DEVICE_IDENTIFICATION, e.STATUS, e.OWNER, d.NAME AS DEVICE_NAME, " +
"e.DEVICE_TYPE FROM DM_DEVICE d INNER JOIN DM_ENROLMENT e " +
"WHERE d.ID = e.DEVICE_ID AND d.TENANT_ID = ? AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " +
"AND e.STATUS NOT IN ('DELETED', 'REMOVED')";
if (paginationRequest.getOwner() != null) {
sql = sql + " AND e.OWNER = ?";
isOwnerProvided = true;
}
if (paginationRequest.getDeviceStatus() != null) {
sql = sql + " AND e.STATUS = ?";
isDeviceStatusProvided = true;
}
if (paginationRequest.getDeviceName() != null) {
sql = sql + " AND d.DEVICE_NAME = ?";
isDeviceNameProvided = true;
}
int parameterIdx = 1;
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setInt(parameterIdx++, tenantId);
if (isOwnerProvided)
preparedStatement.setString(parameterIdx++, paginationRequest.getOwner());
if (isDeviceStatusProvided)
preparedStatement.setString(parameterIdx++, paginationRequest.getDeviceStatus());
if (isDeviceNameProvided)
preparedStatement.setString(parameterIdx, paginationRequest.getDeviceName());
try(ResultSet resultSet = preparedStatement.executeQuery()) {
Device device;
while(resultSet.next()) {
device = new Device();
device.setId(resultSet.getInt("DEVICE_ID"));
device.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFICATION"));
device.setName(resultSet.getString("DEVICE_NAME"));
device.setType(resultSet.getString("DEVICE_TYPE"));
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(resultSet.getString("STATUS")));
enrolmentInfo.setOwner(resultSet.getString("OWNER"));
device.setEnrolmentInfo(enrolmentInfo);
devices.add(device);
}
}
}
return devices;
} catch (SQLException e) {
String msg = "Error occurred while retrieving devices for device ids in: " + deviceIds;
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
}

@ -1152,4 +1152,10 @@ public interface DeviceManagementProviderService {
*/
Device updateDeviceName(Device device, String deviceType, String deviceId)
throws DeviceManagementException, DeviceNotFoundException, ConflictException;
List<Integer> getDevicesNotInGivenIdList(List<Integer> deviceIds, PaginationRequest paginationRequest)
throws DeviceManagementException;
List<Device> getDevicesByDeviceIds(PaginationRequest paginationRequest, List<Integer> deviceIds)
throws DeviceManagementException;
}

@ -5592,4 +5592,57 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
DeviceManagementDAOFactory.closeConnection();
}
}
@Override
public List<Integer> getDevicesNotInGivenIdList(List<Integer> deviceIds, PaginationRequest paginationRequest)
throws DeviceManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
if (paginationRequest == null) {
String msg = "Received null for pagination request";
log.error(msg);
throw new DeviceManagementException(msg);
}
try {
DeviceManagementDAOFactory.openConnection();
return deviceDAO.getDevicesNotInGivenIdList(paginationRequest, deviceIds, tenantId);
} catch (DeviceManagementDAOException e) {
String msg = "Error encountered while getting device ids";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (SQLException e) {
String msg = "Error encountered while getting the database connection";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Override
public List<Device> getDevicesByDeviceIds(PaginationRequest paginationRequest, List<Integer> deviceIds)
throws DeviceManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
if (paginationRequest == null) {
String msg = "Received null for pagination request";
log.error(msg);
throw new DeviceManagementException(msg);
}
try {
DeviceManagementDAOFactory.openConnection();
return deviceDAO.getDevicesByDeviceIds(paginationRequest, deviceIds, tenantId);
} catch (DeviceManagementDAOException e) {
String msg = "Error encountered while getting devices for device ids in " + deviceIds;
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (SQLException e) {
String msg = "Error encountered while getting the database connection";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
}

Loading…
Cancel
Save