Fix searching issue in installation details in app store

group-11461
Lasantha Dharmakeerthi 4 months ago
commit 08e9625373

@ -60,6 +60,26 @@ public class CategorizedSubscriptionResult {
this.subscribedDevices = subscribedDevices;
}
public CategorizedSubscriptionResult(List<DeviceSubscriptionData> devices, String tabActionStatus) {
switch (tabActionStatus) {
case "COMPLETED":
this.installedDevices = devices;
break;
case "PENDING":
this.pendingDevices = devices;
break;
case "ERROR":
this.errorDevices = devices;
break;
case "NEW":
this.newDevices = devices;
break;
case "SUBSCRIBED":
this.subscribedDevices = devices;
break;
}
}
public List<DeviceSubscriptionData> getInstalledDevices() {
return installedDevices;
}
@ -100,4 +120,3 @@ public class CategorizedSubscriptionResult {
this.subscribedDevices = subscribedDevices;
}
}

@ -233,8 +233,9 @@ public interface SubscriptionManager {
* @return {@link SubscriptionsDTO} which contains the details of subscriptions.
* @throws ApplicationManagementException if an error occurs while fetching the group details
*/
List<SubscriptionsDTO> getGroupsSubscriptionDetailsByUUID(String uuid, String subscriptionStatus, int offset, int limit)
throws ApplicationManagementException;
public List<SubscriptionsDTO> getGroupsSubscriptionDetailsByUUID(String uuid, String subscriptionStatus,
PaginationRequest request, int offset,
int limit) throws ApplicationManagementException;
/**
* Retrieves the user details associated with a given app release UUID.
@ -246,8 +247,8 @@ public interface SubscriptionManager {
* @return {@link SubscriptionsDTO} which contains the details of subscriptions.
* @throws ApplicationManagementException if an error occurs while fetching the user details
*/
List<SubscriptionsDTO> getUserSubscriptionsByUUID(String uuid, String subscriptionStatus, int offset, int limit)
throws ApplicationManagementException;
List<SubscriptionsDTO> getUserSubscriptionsByUUID(String uuid, String subscriptionStatus, PaginationRequest request,
int offset, int limit) throws ApplicationManagementException;
/**
* Retrieves the Role details associated with a given app release UUID.
@ -259,8 +260,8 @@ public interface SubscriptionManager {
* @return {@link SubscriptionsDTO} which contains the details of subscriptions.
* @throws ApplicationManagementException if an error occurs while fetching the role details
*/
List<SubscriptionsDTO> getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus, int offset, int limit)
throws ApplicationManagementException;
List<SubscriptionsDTO> getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus, PaginationRequest request,
int offset, int limit) throws ApplicationManagementException;
/**
* Retrieves the Device Subscription details associated with a given app release UUID.
@ -272,8 +273,9 @@ public interface SubscriptionManager {
* @return {@link DeviceSubscriptionResponseDTO} which contains the details of device subscriptions.
* @throws ApplicationManagementException if an error occurs while fetching the device subscription details
*/
DeviceSubscriptionResponseDTO getDeviceSubscriptionsDetailsByUUID(String uuid, String subscriptionStatus, int offset, int limit)
throws ApplicationManagementException;
DeviceSubscriptionResponseDTO getDeviceSubscriptionsDetailsByUUID(String uuid, String subscriptionStatus,
PaginationRequest request, int offset,
int limit) throws ApplicationManagementException;
/**
* Retrieves the All Device details associated with a given app release UUID.
@ -285,8 +287,9 @@ public interface SubscriptionManager {
* @return {@link DeviceSubscriptionResponseDTO} which contains the details of device subscriptions.
* @throws ApplicationManagementException if an error occurs while fetching the subscription details
*/
DeviceSubscriptionResponseDTO getAllSubscriptionDetailsByUUID(String uuid, String subscriptionStatus, int offset, int limit)
throws ApplicationManagementException;
DeviceSubscriptionResponseDTO getAllSubscriptionDetailsByUUID(String uuid, String subscriptionStatus,
PaginationRequest request, int offset,
int limit) throws ApplicationManagementException;
/**
* This method is responsible for retrieving device subscription details related to the given UUID.

@ -390,12 +390,16 @@ public interface SubscriptionDAO {
* @param appReleaseId the appReleaseId of the application release.
* @param unsubscribe the Status of the subscription.
* @param tenantId id of the current tenant.
* @param actionStatus Status of the action
* @param actionType type of the action
* @param actionTriggeredBy subscribed by
* @param deviceIds deviceIds deviceIds to retrieve data.
* @return {@link DeviceOperationDTO} which contains the details of device subscriptions.
* @throws ApplicationManagementDAOException if connection establishment or SQL execution fails.
*/
List<DeviceSubscriptionDTO> getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, List<Integer> deviceIds)
throws ApplicationManagementDAOException;
List<DeviceSubscriptionDTO> getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId,
List<Integer> deviceIds, String actionStatus, String actionType,
String actionTriggeredBy, String tabActionStatus) throws ApplicationManagementDAOException;
/**
* This method is used to get the details of device subscriptions related to a UUID.
@ -403,13 +407,16 @@ public interface SubscriptionDAO {
* @param appReleaseId the appReleaseId of the application release.
* @param unsubscribe the Status of the subscription.
* @param tenantId id of the current tenant.
* @param actionStatus Status of the action
* @param actionType type of the action
* @param actionTriggeredBy subscribed by
* @param offset the offset for the data set
* @param limit the limit for the data set
* @return {@link DeviceOperationDTO} which contains the details of device subscriptions.
* @throws ApplicationManagementDAOException if connection establishment or SQL execution fails.
*/
List<DeviceSubscriptionDTO> getAllSubscriptionsDetails(int appReleaseId, boolean unsubscribe, int tenantId, int offset, int limit)
throws ApplicationManagementDAOException;
List<DeviceSubscriptionDTO> getAllSubscriptionsDetails(int appReleaseId, boolean unsubscribe, int tenantId, String actionStatus, String actionType,
String actionTriggeredBy, int offset, int limit) throws ApplicationManagementDAOException;
/**
* This method is used to get the counts of all subscription types related to a UUID.

@ -1911,8 +1911,9 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
}
@Override
public List<DeviceSubscriptionDTO> getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId, List<Integer> deviceIds)
throws ApplicationManagementDAOException {
public List<DeviceSubscriptionDTO> getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId,
List<Integer> deviceIds, String actionStatus, String actionType,
String actionTriggeredBy, String tabActionStatus) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Getting device subscriptions for the application release id " + appReleaseId
+ " and device ids " + deviceIds + " from the database");
@ -1920,7 +1921,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
try {
Connection conn = this.getDBConnection();
String subscriptionStatusTime = unsubscribe ? "DS.UNSUBSCRIBED_TIMESTAMP" : "DS.SUBSCRIBED_TIMESTAMP";
String sql = "SELECT "
StringBuilder sql = new StringBuilder("SELECT "
+ "DS.ID AS ID, "
+ "DS.SUBSCRIBED_BY AS SUBSCRIBED_BY, "
+ "DS.SUBSCRIBED_TIMESTAMP AS SUBSCRIBED_AT, "
@ -1932,16 +1933,39 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
+ "DS.DM_DEVICE_ID AS DEVICE_ID "
+ "FROM AP_DEVICE_SUBSCRIPTION DS "
+ "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID = ? AND DS.DM_DEVICE_ID IN (" +
deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ") "
+ "ORDER BY " + subscriptionStatusTime + " DESC";
deviceIds.stream().map(id -> "?").collect(Collectors.joining(",")) + ") ");
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, appReleaseId);
ps.setBoolean(2, unsubscribe);
ps.setInt(3, tenantId);
if (actionStatus != null && !actionStatus.isEmpty()) {
sql.append(" AND DS.STATUS = ? ");
}
if (actionType != null && !actionType.isEmpty()) {
sql.append(" AND DS.ACTION_TRIGGERED_FROM = ? ");
}
if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) {
sql.append(" AND DS.SUBSCRIBED_BY LIKE ? ");
}
sql.append("ORDER BY ").append(subscriptionStatusTime).append(" DESC");
try (PreparedStatement ps = conn.prepareStatement(sql.toString())) {
int paramIdx = 1;
ps.setInt(paramIdx++, appReleaseId);
ps.setBoolean(paramIdx++, unsubscribe);
ps.setInt(paramIdx++, tenantId);
for (int i = 0; i < deviceIds.size(); i++) {
ps.setInt(4 + i, deviceIds.get(i));
ps.setInt(paramIdx++, deviceIds.get(i));
}
if (actionStatus != null && !actionStatus.isEmpty()) {
ps.setString(paramIdx++, actionStatus);
}
if (actionType != null && !actionType.isEmpty()) {
ps.setString(paramIdx++, actionType);
}
if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) {
ps.setString(paramIdx++, "%" + actionTriggeredBy + "%");
}
try (ResultSet rs = ps.executeQuery()) {
if (log.isDebugEnabled()) {
log.debug("Successfully retrieved device subscriptions for application release id "
@ -1980,6 +2004,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
@Override
public List<DeviceSubscriptionDTO> getAllSubscriptionsDetails(int appReleaseId, boolean unsubscribe, int tenantId,
String actionStatus, String actionType, String actionTriggeredBy,
int offset, int limit) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Getting device subscriptions for the application release id " + appReleaseId
@ -1987,7 +2012,8 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
}
String subscriptionStatusTime = unsubscribe ? "DS.UNSUBSCRIBED_TIMESTAMP" : "DS.SUBSCRIBED_TIMESTAMP";
String sql = "SELECT "
String actionTriggeredColumn = unsubscribe ? "DS.UNSUBSCRIBED_BY" : "DS.SUBSCRIBED_BY";
StringBuilder sql = new StringBuilder("SELECT "
+ "DS.ID AS ID, "
+ "DS.SUBSCRIBED_BY AS SUBSCRIBED_BY, "
+ "DS.SUBSCRIBED_TIMESTAMP AS SUBSCRIBED_AT, "
@ -1995,21 +2021,45 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
+ "DS.UNSUBSCRIBED_BY AS UNSUBSCRIBED_BY, "
+ "DS.UNSUBSCRIBED_TIMESTAMP AS UNSUBSCRIBED_AT, "
+ "DS.ACTION_TRIGGERED_FROM AS ACTION_TRIGGERED_FROM, "
+ "DS.STATUS AS STATUS,"
+ "DS.STATUS AS STATUS, "
+ "DS.DM_DEVICE_ID AS DEVICE_ID "
+ "FROM AP_DEVICE_SUBSCRIPTION DS "
+ "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID=? "
+ "ORDER BY " + subscriptionStatusTime + " DESC "
+ "LIMIT ? OFFSET ?";
+ "WHERE DS.AP_APP_RELEASE_ID = ? AND DS.UNSUBSCRIBED = ? AND DS.TENANT_ID = ? ");
if (actionStatus != null && !actionStatus.isEmpty()) {
sql.append(" AND DS.STATUS = ? ");
}
if (actionType != null && !actionType.isEmpty()) {
sql.append(" AND DS.ACTION_TRIGGERED_FROM = ? ");
}
if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) {
sql.append(" AND ").append(actionTriggeredColumn).append(" LIKE ? ");
}
sql.append("ORDER BY ").append(subscriptionStatusTime).append(" DESC ")
.append("LIMIT ? OFFSET ?");
try {
Connection conn = this.getDBConnection();
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setInt(1, appReleaseId);
ps.setBoolean(2, unsubscribe);
ps.setInt(3, tenantId);
ps.setInt(4, limit);
ps.setInt(5, offset);
try (PreparedStatement ps = conn.prepareStatement(sql.toString())) {
int paramIdx = 1;
ps.setInt(paramIdx++, appReleaseId);
ps.setBoolean(paramIdx++, unsubscribe);
ps.setInt(paramIdx++, tenantId);
if (actionStatus != null && !actionStatus.isEmpty()) {
ps.setString(paramIdx++, actionStatus);
}
if (actionType != null && !actionType.isEmpty()) {
ps.setString(paramIdx++, actionType);
}
if (actionTriggeredBy != null && !actionTriggeredBy.isEmpty()) {
ps.setString(paramIdx++, "%" + actionTriggeredBy + "%");
}
ps.setInt(paramIdx++, limit);
ps.setInt(paramIdx++, offset);
try (ResultSet rs = ps.executeQuery()) {
if (log.isDebugEnabled()) {
log.debug("Successfully retrieved device subscriptions for application release id "
@ -2040,7 +2090,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
log.error(msg, e);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while while running SQL to get device subscription data for application ID: " + appReleaseId;
String msg = "Error occurred while running SQL to get device subscription data for application ID: " + appReleaseId;
log.error(msg, e);
throw new ApplicationManagementDAOException(msg, e);
}

@ -1704,12 +1704,12 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
}
@Override
public List<SubscriptionsDTO> getGroupsSubscriptionDetailsByUUID(String uuid, String subscriptionStatus, int offset,
int limit) throws ApplicationManagementException {
public List<SubscriptionsDTO> getGroupsSubscriptionDetailsByUUID(
String uuid, String subscriptionStatus, PaginationRequest request, int offset, int limit)
throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
boolean unsubscribe = subscriptionStatus.equals("unsubscribed");
String groupName;
String status;
try {
ConnectionManagerUtil.openDBConnection();
@ -1733,12 +1733,18 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
GroupManagementProviderService groupManagementProviderService = HelperUtil.getGroupManagementProviderService();
for (GroupSubscriptionDTO groupDetail : groupDetails) {
groupName = groupDetail.getGroupName();
if (StringUtils.isNotBlank(request.getGroupName()) && !request.getGroupName().equals(groupDetail.getGroupName())) {
continue;
}
String groupName = StringUtils.isNotBlank(request.getGroupName()) ? request.getGroupName() : groupDetail.getGroupName();
// Retrieve group details and device IDs for the group using the service layer
GroupDetailsDTO groupDetailWithDevices =
groupManagementProviderService.getGroupDetailsWithDevices(groupName, applicationDTO.getDeviceTypeId(),
offset, limit);
groupManagementProviderService.getGroupDetailsWithDevices(
groupName, applicationDTO.getDeviceTypeId(), request.getOwner(),
request.getDeviceName(), request.getDeviceStatus(), offset, limit);
SubscriptionsDTO groupDetailDTO = new SubscriptionsDTO();
groupDetailDTO.setId(groupDetailWithDevices.getGroupId());
@ -1761,24 +1767,29 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
List<Integer> deviceIds = groupDetailWithDevices.getDeviceIds();
Map<String, Integer> statusCounts = new HashMap<>();
statusCounts.put("PENDING", 0);
statusCounts.put("COMPLETED", 0);
statusCounts.put("ERROR", 0);
statusCounts.put("PENDING", 0);
statusCounts.put("NEW", 0);
statusCounts.put("SUBSCRIBED", 0);
// Get subscribed devices if unsubscribed devices are requested
List<DeviceSubscriptionDTO> subscribedDeviceSubscriptions = new ArrayList<>();
if (unsubscribe) {
subscribedDeviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds(
appReleaseId, !unsubscribe, tenantId, deviceIds);
}
for (Integer deviceId : deviceIds) {
List<DeviceSubscriptionDTO> deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds(
groupDetail.getAppReleaseId(), unsubscribe, tenantId, deviceIds);
// Get subscribed devices if unsubscribed devices are requested
List<DeviceSubscriptionDTO> deviceSubscriptions;
if (unsubscribe) {
deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds(
appReleaseId, !unsubscribe, tenantId, deviceIds,
request.getActionStatus(), request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus());
} else {
deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds(
groupDetail.getAppReleaseId(), false, tenantId, deviceIds,
request.getActionStatus(), request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus());
}
List<DeviceSubscriptionDTO> filteredDeviceSubscriptions = deviceSubscriptions.stream()
.filter(subscription -> StringUtils.isBlank(request.getTabActionStatus()) || subscription.getStatus().equals(request.getTabActionStatus()))
.collect(Collectors.toList());
boolean isNewDevice = true;
for (DeviceSubscriptionDTO subscription : deviceSubscriptions) {
for (DeviceSubscriptionDTO subscription : filteredDeviceSubscriptions) {
if (subscription.getDeviceId() == deviceId) {
DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData();
deviceDetail.setDeviceId(subscription.getDeviceId());
@ -1796,7 +1807,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
deviceDetail.setType(groupDetailWithDevices.getDeviceTypes().get(deviceId));
deviceDetail.setDeviceIdentifier(groupDetailWithDevices.getDeviceIdentifiers().get(deviceId));
status = subscription.getStatus();
String status = subscription.getStatus();
switch (status) {
case "COMPLETED":
installedDevices.add(deviceDetail);
@ -1814,13 +1825,17 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
pendingDevices.add(deviceDetail);
statusCounts.put("PENDING", statusCounts.get("PENDING") + 1);
break;
default:
newDevices.add(deviceDetail);
statusCounts.put("NEW", statusCounts.get("NEW") + 1);
break;
}
isNewDevice = false;
}
}
if (isNewDevice) {
boolean isSubscribedDevice = false;
for (DeviceSubscriptionDTO subscribedDevice : subscribedDeviceSubscriptions) {
for (DeviceSubscriptionDTO subscribedDevice : deviceSubscriptions) {
if (subscribedDevice.getDeviceId() == deviceId) {
DeviceSubscriptionData subscribedDeviceDetail = new DeviceSubscriptionData();
subscribedDeviceDetail.setDeviceId(subscribedDevice.getDeviceId());
@ -1862,17 +1877,38 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
statusPercentages.put(entry.getKey(), Double.valueOf(formattedPercentage));
}
CategorizedSubscriptionResult categorizedSubscriptionResult;
if (subscribedDevices.isEmpty()) {
categorizedSubscriptionResult =
new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices);
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;
}
groupDetailDTO.setDevices(new CategorizedSubscriptionResult(requestedDevices, request.getTabActionStatus()));
} else {
categorizedSubscriptionResult =
new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices, subscribedDevices);
CategorizedSubscriptionResult categorizedSubscriptionResult;
if (subscribedDevices.isEmpty()) {
categorizedSubscriptionResult =
new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices);
} else {
categorizedSubscriptionResult =
new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices, subscribedDevices);
}
groupDetailDTO.setDevices(categorizedSubscriptionResult);
}
groupDetailDTO.setDevices(categorizedSubscriptionResult);
groupDetailDTO.setStatusPercentages(statusPercentages);
groupDetailsWithDevices.add(groupDetailDTO);
}
@ -1895,11 +1931,11 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
}
@Override
public List<SubscriptionsDTO> getUserSubscriptionsByUUID(String uuid, String subscriptionStatus, int offset, int limit)
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 userName;
String status;
try {
@ -1924,11 +1960,17 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService();
for (SubscriptionsDTO userSubscription : userSubscriptions) {
userName = userSubscription.getName();
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());
deviceManagementProviderService.getOwnersWithDeviceIds(userName, applicationDTO.getDeviceTypeId(),
request.getOwner(), request.getDeviceName(), request.getDeviceStatus());
SubscriptionsDTO userSubscriptionDTO = new SubscriptionsDTO();
userSubscriptionDTO.setName(userSubscription.getName());
@ -1959,14 +2001,17 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
List<DeviceSubscriptionDTO> subscribedDeviceSubscriptions = new ArrayList<>();
if (unsubscribe) {
subscribedDeviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds(
appReleaseId, !unsubscribe, tenantId, deviceIds);
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);
userSubscription.getAppReleaseId(), unsubscribe, tenantId, deviceIds, request.getActionStatus(), request.getActionType(),
request.getActionTriggeredBy(), request.getTabActionStatus());
OwnerWithDeviceDTO ownerWithDeviceByDeviceId =
deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId);
deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId, request.getOwner(), request.getDeviceName(),
request.getDeviceStatus());
if (ownerWithDeviceByDeviceId == null) {
continue;
}
@ -2056,20 +2101,42 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
statusPercentages.put(entry.getKey(), Double.valueOf(formattedPercentage));
}
CategorizedSubscriptionResult categorizedSubscriptionResult;
if (subscribedDevices.isEmpty()) {
categorizedSubscriptionResult =
new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices);
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 =
new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices, subscribedDevices);
}
userSubscriptionDTO.setDevices(categorizedSubscriptionResult);
userSubscriptionDTO.setStatusPercentages(statusPercentages);
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;
@ -2087,7 +2154,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
}
@Override
public List<SubscriptionsDTO> getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus, int offset, int limit)
public List<SubscriptionsDTO> getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus,
PaginationRequest request, int offset, int limit)
throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
boolean unsubscribe = subscriptionStatus.equals("unsubscribed");
@ -2116,7 +2184,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService();
for (SubscriptionsDTO roleSubscription : roleSubscriptions) {
roleName = roleSubscription.getName();
roleName = StringUtils.isNotBlank(request.getRoleName()) ? request.getRoleName() : roleSubscription.getName();
SubscriptionsDTO roleSubscriptionDTO = new SubscriptionsDTO();
roleSubscriptionDTO.setName(roleSubscription.getName());
@ -2145,7 +2214,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
for (String user : users) {
OwnerWithDeviceDTO ownerDetailsWithDevices;
try {
ownerDetailsWithDevices = deviceManagementProviderService.getOwnersWithDeviceIds(user, applicationDTO.getDeviceTypeId());
ownerDetailsWithDevices = deviceManagementProviderService.getOwnersWithDeviceIds(user, applicationDTO.getDeviceTypeId(),
request.getOwner(), request.getDeviceName(), request.getDeviceStatus());
} catch (DeviceManagementDAOException e) {
throw new ApplicationManagementException("Error retrieving owner details with devices for user: " + user, e);
}
@ -2156,17 +2226,20 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
List<DeviceSubscriptionDTO> subscribedDeviceSubscriptions = new ArrayList<>();
if (unsubscribe) {
subscribedDeviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds(
appReleaseId, !unsubscribe, tenantId, deviceIds);
appReleaseId, !unsubscribe, tenantId, deviceIds, request.getActionStatus(), request.getActionType(),
request.getActionTriggeredBy(), request.getTabActionStatus());
}
OwnerWithDeviceDTO ownerWithDeviceByDeviceId =
deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId);
deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId, request.getOwner(), request.getDeviceName(),
request.getDeviceStatus());
if (ownerWithDeviceByDeviceId == null) {
continue;
}
List<DeviceSubscriptionDTO> deviceSubscriptions;
try {
deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds(
roleSubscription.getAppReleaseId(), unsubscribe, tenantId, deviceIds);
roleSubscription.getAppReleaseId(), unsubscribe, tenantId, deviceIds, request.getActionStatus(),
request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus());
} catch (ApplicationManagementDAOException e) {
throw new ApplicationManagementException("Error retrieving device subscriptions", e);
}
@ -2259,18 +2332,41 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
statusPercentages.put(entry.getKey(), Double.valueOf(formattedPercentage));
}
CategorizedSubscriptionResult categorizedSubscriptionResult;
if (subscribedDevices.isEmpty()) {
categorizedSubscriptionResult =
new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices);
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;
}
roleSubscriptionDTO.setDevices(new CategorizedSubscriptionResult(requestedDevices, request.getTabActionStatus()));
} else {
categorizedSubscriptionResult =
new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices, subscribedDevices);
CategorizedSubscriptionResult categorizedSubscriptionResult;
if (subscribedDevices.isEmpty()) {
categorizedSubscriptionResult =
new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices);
} else {
categorizedSubscriptionResult =
new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices,
subscribedDevices);
}
roleSubscriptionDTO.setDevices(categorizedSubscriptionResult);
roleSubscriptionDTO.setStatusPercentages(statusPercentages);
roleSubscriptionDTO.setDeviceCount(totalDevices);
}
roleSubscriptionDTO.setDevices(categorizedSubscriptionResult);
roleSubscriptionDTO.setStatusPercentages(statusPercentages);
roleSubscriptionDTO.setDeviceCount(totalDevices);
roleSubscriptionsWithDevices.add(roleSubscriptionDTO);
}
@ -2302,7 +2398,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
}
@Override
public DeviceSubscriptionResponseDTO getDeviceSubscriptionsDetailsByUUID(String uuid, String subscriptionStatus, int offset,
public DeviceSubscriptionResponseDTO getDeviceSubscriptionsDetailsByUUID(String uuid, String subscriptionStatus, PaginationRequest request, int offset,
int limit) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
@ -2332,7 +2428,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
}
List<DeviceDetailsDTO> allDevices =
deviceManagementProviderService.getDevicesByTenantId(tenantId, applicationDTO.getDeviceTypeId());
deviceManagementProviderService.getDevicesByTenantId(tenantId, applicationDTO.getDeviceTypeId(),
request.getOwner(), request.getDeviceStatus());
List<Integer> deviceIds = allDevices.stream()
.map(DeviceDetailsDTO::getDeviceId)
@ -2357,9 +2454,11 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
.collect(Collectors.toMap(DeviceDetailsDTO::getDeviceId, Function.identity()));
List<DeviceSubscriptionDTO> allSubscriptionsForUnSubscribed =
subscriptionDAO.getSubscriptionDetailsByDeviceIds(appReleaseId, !unsubscribe, tenantId, deviceIds);
subscriptionDAO.getSubscriptionDetailsByDeviceIds(appReleaseId, !unsubscribe, tenantId, deviceIds, request.getActionStatus(),
request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus());
List<DeviceSubscriptionDTO> allSubscriptionsForSubscribed =
subscriptionDAO.getSubscriptionDetailsByDeviceIds(appReleaseId, unsubscribe, tenantId, deviceIds);
subscriptionDAO.getSubscriptionDetailsByDeviceIds(appReleaseId, unsubscribe, tenantId, deviceIds, request.getActionStatus(),
request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus());
Map<Integer, DeviceSubscriptionDTO> allSubscriptionForUnSubscribedMap = allSubscriptionsForUnSubscribed.stream()
.collect(Collectors.toMap(DeviceSubscriptionDTO::getDeviceId, Function.identity()));
Map<Integer, DeviceSubscriptionDTO> allSubscriptionForSubscribedMap = allSubscriptionsForSubscribed.stream()
@ -2368,11 +2467,12 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
for (DeviceDetailsDTO device : allDevices) {
Integer deviceId = device.getDeviceId();
OwnerWithDeviceDTO ownerWithDevice =
deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId);
if (ownerWithDevice == null) {
deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId, request.getOwner(), request.getDeviceName(),
request.getDeviceStatus());
if (ownerWithDevice == null || (request.getDeviceName() != null && !request.getDeviceName().isEmpty() &&
(ownerWithDevice.getDeviceNames() == null || !ownerWithDevice.getDeviceNames().contains(request.getDeviceName())))) {
continue;
}
if (deviceSubscriptionMap.containsKey(deviceId)) {
DeviceSubscriptionDTO subscription = deviceSubscriptionMap.get(deviceId);
DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData();
@ -2461,14 +2561,35 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
statusPercentages.put(entry.getKey(), Double.valueOf(formattedPercentage));
}
CategorizedSubscriptionResult categorizedSubscriptionResult;
if (subscribedDevices.isEmpty()) {
categorizedSubscriptionResult =
new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices);
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;
}
} else {
categorizedSubscriptionResult =
new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices, subscribedDevices);
requestedDevices.addAll(installedDevices);
requestedDevices.addAll(pendingDevices);
requestedDevices.addAll(errorDevices);
requestedDevices.addAll(newDevices);
requestedDevices.addAll(subscribedDevices);
}
CategorizedSubscriptionResult categorizedSubscriptionResult =
new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices, subscribedDevices);
DeviceSubscriptionResponseDTO deviceSubscriptionResponse =
new DeviceSubscriptionResponseDTO(totalDevices, statusPercentages, categorizedSubscriptionResult);
@ -2490,8 +2611,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
}
@Override
public DeviceSubscriptionResponseDTO getAllSubscriptionDetailsByUUID(String uuid, String subscriptionStatus, int offset, int limit)
throws ApplicationManagementException {
public DeviceSubscriptionResponseDTO getAllSubscriptionDetailsByUUID(String uuid, String subscriptionStatus, PaginationRequest request,
int offset, int limit) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
boolean unsubscribe = subscriptionStatus.equals("unsubscribed");
@ -2508,7 +2629,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
int appReleaseId = applicationReleaseDTO.getId();
List<DeviceSubscriptionDTO> allSubscriptions =
subscriptionDAO.getAllSubscriptionsDetails(appReleaseId, unsubscribe, tenantId, offset, limit);
subscriptionDAO.getAllSubscriptionsDetails(appReleaseId, unsubscribe, tenantId, request.getActionStatus(),
request.getActionType(), request.getActionTriggeredBy(), offset, limit);
// empty response for no subscriptions
if (allSubscriptions.isEmpty()) {
@ -2534,16 +2656,18 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
statusCounts.put("NEW", 0);
List<DeviceDetailsDTO> allDevices =
deviceManagementProviderService.getDevicesByTenantId(tenantId, applicationDTO.getDeviceTypeId());
deviceManagementProviderService.getDevicesByTenantId(tenantId, applicationDTO.getDeviceTypeId(), request.getOwner(),
request.getDeviceStatus());
for (DeviceDetailsDTO device : allDevices) {
Integer deviceId = device.getDeviceId();
OwnerWithDeviceDTO ownerWithDevice =
deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId);
if (ownerWithDevice == null) {
deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId, request.getOwner(), request.getDeviceName(),
request.getDeviceStatus());
if (ownerWithDevice == null || (request.getDeviceName() != null && !request.getDeviceName().isEmpty() &&
(ownerWithDevice.getDeviceNames() == null || !ownerWithDevice.getDeviceNames().contains(request.getDeviceName())))) {
continue;
}
if (allSubscriptionMap.containsKey(deviceId)) {
DeviceSubscriptionDTO subscription = allSubscriptionMap.get(deviceId);
DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData();
@ -2602,6 +2726,29 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
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;
}
} else {
requestedDevices.addAll(installedDevices);
requestedDevices.addAll(pendingDevices);
requestedDevices.addAll(errorDevices);
requestedDevices.addAll(newDevices);
}
CategorizedSubscriptionResult categorizedSubscriptionResult =
new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices);
DeviceSubscriptionResponseDTO result =

@ -41,6 +41,14 @@ public class PaginationRequest {
private Date since;
private String filter;
private String serialNumber;
private String groupName;
private String roleName;
private String userName;
private String deviceStatus;
private String tabActionStatus;
private String actionStatus;
private String actionType;
private String actionTriggeredBy;
private Map<String, String> customProperty = new HashMap<>();
private Map<String, Object> property = new HashMap<>();
private List<String> statusList = new ArrayList<>();
@ -220,4 +228,68 @@ public class PaginationRequest {
+ this.ownership + "' Status '" + this.statusList + "' owner '" + this.owner + "' groupId: " + this.groupId
+ " start index: " + this.startIndex + ", SortColumns: " + this.sortColumn;
}
public String getDeviceStatus() {
return deviceStatus;
}
public void setDeviceStatus(String deviceStatus) {
this.deviceStatus = deviceStatus;
}
public String getActionStatus() {
return actionStatus;
}
public void setActionStatus(String actionStatus) {
this.actionStatus = actionStatus;
}
public String getActionType() {
return actionType;
}
public void setActionType(String actionType) {
this.actionType = actionType;
}
public String getActionTriggeredBy() {
return actionTriggeredBy;
}
public void setActionTriggeredBy(String actionTriggeredBy) {
this.actionTriggeredBy = actionTriggeredBy;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getTabActionStatus() {
return tabActionStatus;
}
public void setTabActionStatus(String tabActionStatus) {
this.tabActionStatus = tabActionStatus;
}
}

@ -103,21 +103,27 @@ public interface EnrollmentDAO {
* @param owner the owner whose device IDs need to be retrieved
* @param allowingDeviceStatuses statuses of devices need to be retrieved
* @param tenantId the ID of the tenant
* @param deviceOwner owner of the device
* @param deviceName name of the device
* @param deviceStatus status of the device
* @return {@link OwnerWithDeviceDTO} which contains a list of devices related to a user
* @throws DeviceManagementDAOException if an error occurs while fetching the data
*/
OwnerWithDeviceDTO getOwnersWithDevices(String owner, List<String> allowingDeviceStatuses, int tenantId, int deviceTypeId)
throws DeviceManagementDAOException;
OwnerWithDeviceDTO getOwnersWithDevices(String owner, List<String> allowingDeviceStatuses, int tenantId, int deviceTypeId,
String deviceOwner, String deviceName, String deviceStatus) throws DeviceManagementDAOException;
/**
* Retrieves a list of device IDs with owners and device status.
*
* @param deviceId the deviceId of the device which user need to be retrieved
* @param tenantId the ID of the tenant
* @param deviceOwner owner of the device
* @param deviceName name of the device
* @param deviceStatus status of the device
* @return {@link OwnerWithDeviceDTO} which contains a list of devices
* @throws DeviceManagementDAOException if an error occurs while fetching the data
*/
OwnerWithDeviceDTO getOwnerWithDeviceByDeviceId(int deviceId, int tenantId)
OwnerWithDeviceDTO getOwnerWithDeviceByDeviceId(int deviceId, int tenantId, String deviceOwner, String deviceName, String deviceStatus)
throws DeviceManagementDAOException;
/**
@ -126,9 +132,11 @@ public interface EnrollmentDAO {
* @param tenantId the ID of the tenant
* @param allowingDeviceStatuses the allowed device statuses of devices
* @param deviceTypeId the device type id
* @param deviceOwner owner of the device
* @param deviceStatus status of the device
* @return {@link OwnerWithDeviceDTO} which contains a list of devices related to a user
* @throws DeviceManagementDAOException if an error occurs while fetching the data
*/
List<DeviceDetailsDTO> getDevicesByTenantId(int tenantId, List<String> allowingDeviceStatuses, int deviceTypeId)
throws DeviceManagementDAOException;
List<DeviceDetailsDTO> getDevicesByTenantId(int tenantId, List<String> allowingDeviceStatuses, int deviceTypeId, String deviceOwner,
String deviceStatus) throws DeviceManagementDAOException;
}

@ -476,13 +476,16 @@ public interface GroupDAO {
* @param allowingDeviceStatuses the statuses of devices
* @param deviceTypeId the device type id
* @param tenantId Tenant ID
* @param deviceOwner owner of the device
* @param deviceName name of the device
* @param deviceStatus status of the device
* @param offset the offset for the data set
* @param limit the limit for the data set
* @return {@link GroupDetailsDTO} which containing group details and a list of device IDs
* @throws GroupManagementDAOException if an error occurs while retrieving the group details and devices
*/
GroupDetailsDTO getGroupDetailsWithDevices(String groupName, List<String> allowingDeviceStatuses, int deviceTypeId,
int tenantId, int offset, int limit)
int tenantId, String deviceOwner, String deviceName, String deviceStatus, int offset, int limit)
throws GroupManagementDAOException;
}

@ -564,8 +564,9 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
}
@Override
public OwnerWithDeviceDTO getOwnersWithDevices(String owner, List<String> allowingDeviceStatuses, int tenantId, int deviceTypeId)
throws DeviceManagementDAOException {
public OwnerWithDeviceDTO getOwnersWithDevices(String owner, List<String> allowingDeviceStatuses, int tenantId,
int deviceTypeId, String deviceOwner, String deviceName,
String deviceStatus) throws DeviceManagementDAOException {
Connection conn = null;
OwnerWithDeviceDTO ownerDetails = new OwnerWithDeviceDTO();
List<Integer> deviceIds = new ArrayList<>();
@ -579,20 +580,48 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
}
}
String sql = "SELECT e.DEVICE_ID, e.OWNER, e.STATUS AS DEVICE_STATUS, d.NAME AS DEVICE_NAME, e.DEVICE_TYPE AS DEVICE_TYPE, e.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFICATION " +
StringBuilder sql = new StringBuilder(
"SELECT e.DEVICE_ID, " +
"e.OWNER, " +
"e.STATUS AS DEVICE_STATUS, " +
"d.NAME AS DEVICE_NAME, " +
"e.DEVICE_TYPE AS DEVICE_TYPE, " +
"e.DEVICE_IDENTIFICATION AS DEVICE_IDENTIFICATION " +
"FROM DM_ENROLMENT e " +
"JOIN DM_DEVICE d ON e.DEVICE_ID = d.ID " +
"WHERE e.OWNER = ? AND e.TENANT_ID = ? AND d.DEVICE_TYPE_ID = ? AND e.STATUS IN (" + deviceFilters.toString() + ")";
"WHERE e.OWNER = ? AND e.TENANT_ID = ? AND d.DEVICE_TYPE_ID = ? AND e.STATUS IN (" + deviceFilters + ")");
if (deviceOwner != null && !deviceOwner.isEmpty()) {
sql.append(" AND e.OWNER LIKE ?");
}
if (deviceName != null && !deviceName.isEmpty()) {
sql.append(" AND d.NAME LIKE ?");
}
if (deviceStatus != null && !deviceStatus.isEmpty()) {
sql.append(" AND e.STATUS = ?");
}
try {
conn = this.getConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, owner);
stmt.setInt(2, tenantId);
stmt.setInt(3, deviceTypeId);
for (int i = 0; i < allowingDeviceStatuses.size(); i++) {
stmt.setString(4 + i, allowingDeviceStatuses.get(i));
try (PreparedStatement stmt = conn.prepareStatement(sql.toString())) {
int index = 1;
stmt.setString(index++, owner);
stmt.setInt(index++, tenantId);
stmt.setInt(index++, deviceTypeId);
for (String status : allowingDeviceStatuses) {
stmt.setString(index++, status);
}
if (deviceOwner != null && !deviceOwner.isEmpty()) {
stmt.setString(index++, "%" + deviceOwner + "%");
}
if (deviceName != null && !deviceName.isEmpty()) {
stmt.setString(index++, "%" + deviceName + "%");
}
if (deviceStatus != null && !deviceStatus.isEmpty()) {
stmt.setString(index++, deviceStatus);
}
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
if (ownerDetails.getUserName() == null) {
@ -600,8 +629,8 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
}
ownerDetails.setDeviceStatus(rs.getString("DEVICE_STATUS"));
ownerDetails.setDeviceNames(rs.getString("DEVICE_NAME"));
ownerDetails.setDeviceTypes("DEVICE_TYPE");
ownerDetails.setDeviceIdentifiers("DEVICE_IDENTIFICATION");
ownerDetails.setDeviceTypes(rs.getString("DEVICE_TYPE"));
ownerDetails.setDeviceIdentifiers(rs.getString("DEVICE_IDENTIFICATION"));
deviceIds.add(rs.getInt("DEVICE_ID"));
deviceCount++;
}
@ -618,25 +647,55 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
}
@Override
public OwnerWithDeviceDTO getOwnerWithDeviceByDeviceId(int deviceId, int tenantId)
throws DeviceManagementDAOException {
public OwnerWithDeviceDTO getOwnerWithDeviceByDeviceId(int deviceId, int tenantId, String deviceOwner, String deviceName,
String deviceStatus) throws DeviceManagementDAOException {
OwnerWithDeviceDTO deviceOwnerWithStatus = new OwnerWithDeviceDTO();
Connection conn = null;
String sql = "SELECT e.DEVICE_ID, e.OWNER, e.STATUS AS DEVICE_STATUS, d.NAME AS DEVICE_NAME, e.DEVICE_TYPE, e.DEVICE_IDENTIFICATION " +
List<Integer> deviceIds = new ArrayList<>();
StringBuilder sql = new StringBuilder(
"SELECT e.DEVICE_ID, " +
"e.OWNER, " +
"e.STATUS AS DEVICE_STATUS, " +
"d.NAME AS DEVICE_NAME, " +
"e.DEVICE_TYPE, " +
"e.DEVICE_IDENTIFICATION " +
"FROM DM_ENROLMENT e " +
"JOIN DM_DEVICE d ON e.DEVICE_ID = d.ID " +
"WHERE e.DEVICE_ID = ? AND e.TENANT_ID = ?";
"WHERE e.DEVICE_ID = ? AND e.TENANT_ID = ?");
if (deviceOwner != null && !deviceOwner.isEmpty()) {
sql.append(" AND e.OWNER LIKE ?");
}
if (deviceName != null && !deviceName.isEmpty()) {
sql.append(" AND d.NAME LIKE ?");
}
if (deviceStatus != null && !deviceStatus.isEmpty()) {
sql.append(" AND e.STATUS = ?");
}
try {
conn = this.getConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, deviceId);
stmt.setInt(2, tenantId);
try (PreparedStatement stmt = conn.prepareStatement(sql.toString())) {
int paramIndex = 1;
stmt.setInt(paramIndex++, deviceId);
stmt.setInt(paramIndex++, tenantId);
// Set filter parameters if provided
if (deviceOwner != null && !deviceOwner.isEmpty()) {
stmt.setString(paramIndex++, "%" + deviceOwner + "%");
}
if (deviceName != null && !deviceName.isEmpty()) {
stmt.setString(paramIndex++, "%" + deviceName + "%");
}
if (deviceStatus != null && !deviceStatus.isEmpty()) {
stmt.setString(paramIndex++, deviceStatus);
}
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
deviceOwnerWithStatus.setUserName(rs.getString("OWNER"));
deviceOwnerWithStatus.setDeviceStatus(rs.getString("DEVICE_STATUS"));
List<Integer> deviceIds = new ArrayList<>();
deviceIds.add(rs.getInt("DEVICE_ID"));
deviceOwnerWithStatus.setDeviceIds(deviceIds);
deviceOwnerWithStatus.setDeviceNames(rs.getString("DEVICE_NAME"));
@ -654,8 +713,8 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
}
@Override
public List<DeviceDetailsDTO> getDevicesByTenantId(int tenantId, List<String> allowingDeviceStatuses, int deviceTypeId)
throws DeviceManagementDAOException {
public List<DeviceDetailsDTO> getDevicesByTenantId(int tenantId, List<String> allowingDeviceStatuses, int deviceTypeId,
String deviceOwner, String deviceStatus) throws DeviceManagementDAOException {
List<DeviceDetailsDTO> devices = new ArrayList<>();
if (allowingDeviceStatuses.isEmpty()) {
return devices;
@ -669,15 +728,23 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
}
}
String sql = "SELECT e.DEVICE_ID, e.OWNER, e.STATUS, e.DEVICE_TYPE, e.DEVICE_IDENTIFICATION " +
StringBuilder sql = new StringBuilder("SELECT e.DEVICE_ID, e.OWNER, e.STATUS, e.DEVICE_TYPE, e.DEVICE_IDENTIFICATION " +
"FROM DM_ENROLMENT e " +
"JOIN DM_DEVICE d ON e.DEVICE_ID = d.ID " +
"WHERE e.TENANT_ID = ? AND e.STATUS IN (" + deviceFilters.toString() + ") AND d.DEVICE_TYPE_ID = ?";
"WHERE e.TENANT_ID = ? AND e.STATUS IN (" + deviceFilters.toString() + ") AND d.DEVICE_TYPE_ID = ?");
if (deviceOwner != null && !deviceOwner.isEmpty()) {
sql.append(" AND e.OWNER LIKE ?");
}
if (deviceStatus != null && !deviceStatus.isEmpty()) {
sql.append(" AND e.STATUS = ?");
}
Connection conn = null;
try {
conn = this.getConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
try (PreparedStatement stmt = conn.prepareStatement(sql.toString())) {
int index = 1;
stmt.setInt(index++, tenantId);
for (String status : allowingDeviceStatuses) {
@ -685,6 +752,13 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
}
stmt.setInt(index++, deviceTypeId);
if (deviceOwner != null && !deviceOwner.isEmpty()) {
stmt.setString(index++, "%" + deviceOwner + "%");
}
if (deviceStatus != null && !deviceStatus.isEmpty()) {
stmt.setString(index++, deviceStatus);
}
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
DeviceDetailsDTO device = new DeviceDetailsDTO();
@ -704,4 +778,5 @@ public abstract class AbstractEnrollmentDAOImpl implements EnrollmentDAO {
}
return devices;
}
}

@ -1441,8 +1441,8 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
}
@Override
public GroupDetailsDTO getGroupDetailsWithDevices(String groupName, List<String> allowedStatuses, int deviceTypeId,
int tenantId, int offset, int limit)
public GroupDetailsDTO getGroupDetailsWithDevices(String groupName, List<String> allowedStatuses, int deviceTypeId, int tenantId,
String deviceOwner, String deviceName, String deviceStatus, int offset, int limit)
throws GroupManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to get group details and device IDs for group: " + groupName);
@ -1455,15 +1455,15 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
Map<Integer, String> deviceTypes = new HashMap<>();
Map<Integer, String> deviceIdentifiers = new HashMap<>();
StringBuilder deviceFilters = new StringBuilder();
StringBuilder statusPlaceholders = new StringBuilder();
for (int i = 0; i < allowedStatuses.size(); i++) {
deviceFilters.append("?");
statusPlaceholders.append("?");
if (i < allowedStatuses.size() - 1) {
deviceFilters.append(",");
statusPlaceholders.append(",");
}
}
String sql =
StringBuilder sql = new StringBuilder(
"SELECT " +
" g.ID AS GROUP_ID, " +
" g.GROUP_NAME, " +
@ -1483,13 +1483,24 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
" g.GROUP_NAME = ? " +
" AND g.TENANT_ID = ? " +
" AND d.DEVICE_TYPE_ID = ? " +
" AND e.STATUS IN (" + deviceFilters.toString() + ") " +
"LIMIT ? OFFSET ?";
" AND e.STATUS IN (" + statusPlaceholders + ")");
if (deviceOwner != null && !deviceOwner.isEmpty()) {
sql.append(" AND e.OWNER LIKE ?");
}
if (deviceName != null && !deviceName.isEmpty()) {
sql.append(" AND d.NAME LIKE ?");
}
if (deviceStatus != null && !deviceStatus.isEmpty()) {
sql.append(" AND e.STATUS = ?");
}
sql.append(" LIMIT ? OFFSET ?");
Connection conn = null;
try {
conn = GroupManagementDAOFactory.getConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
try (PreparedStatement stmt = conn.prepareStatement(sql.toString())) {
int index = 1;
stmt.setString(index++, groupName);
stmt.setInt(index++, tenantId);
@ -1497,6 +1508,17 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
for (String status : allowedStatuses) {
stmt.setString(index++, status);
}
if (deviceOwner != null && !deviceOwner.isEmpty()) {
stmt.setString(index++, "%" + deviceOwner + "%");
}
if (deviceName != null && !deviceName.isEmpty()) {
stmt.setString(index++, "%" + deviceName + "%");
}
if (deviceStatus != null && !deviceStatus.isEmpty()) {
stmt.setString(index++, deviceStatus);
}
stmt.setInt(index++, limit);
stmt.setInt(index++, offset);
@ -1516,15 +1538,15 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
deviceIdentifiers.put(deviceId, rs.getString("DEVICE_IDENTIFICATION"));
}
}
groupDetails.setDeviceIds(deviceIds);
groupDetails.setDeviceCount(deviceIds.size());
groupDetails.setDeviceOwners(deviceOwners);
groupDetails.setDeviceStatuses(deviceStatuses);
groupDetails.setDeviceNames(deviceNames);
groupDetails.setDeviceTypes(deviceTypes);
groupDetails.setDeviceIdentifiers(deviceIdentifiers);
return groupDetails;
}
groupDetails.setDeviceIds(deviceIds);
groupDetails.setDeviceCount(deviceIds.size());
groupDetails.setDeviceOwners(deviceOwners);
groupDetails.setDeviceStatuses(deviceStatuses);
groupDetails.setDeviceNames(deviceNames);
groupDetails.setDeviceTypes(deviceTypes);
groupDetails.setDeviceIdentifiers(deviceIdentifiers);
return groupDetails;
} catch (SQLException e) {
String msg = "Error occurred while retrieving group details and device IDs for group: " + groupName;
log.error(msg, e);

@ -1082,29 +1082,40 @@ public interface DeviceManagementProviderService {
* Get owner details and device IDs for a given owner and tenant.
*
* @param owner the name of the owner.
* @param deviceTypeId the device type id
* @param deviceTypeId the device type id]
* @param deviceOwner owner of the device
* @param deviceName name of the device
* @param deviceStatus status of the device
* @return {@link OwnerWithDeviceDTO} which contains a list of devices related to a user.
* @throws DeviceManagementException if an error occurs while fetching owner details.
*/
OwnerWithDeviceDTO getOwnersWithDeviceIds(String owner, int deviceTypeId) throws DeviceManagementDAOException;
OwnerWithDeviceDTO getOwnersWithDeviceIds(String owner, int deviceTypeId, String deviceOwner, String deviceName, String deviceStatus)
throws DeviceManagementDAOException;
/**
* Get owner details and device IDs for a given owner and tenant.
*
* @param deviceId the deviceId of the device.
* @param deviceOwner owner of the device
* @param deviceName name of the device
* @param deviceStatus status of the device
* @return {@link OwnerWithDeviceDTO} which contains a list of devices related to a user.
* @throws DeviceManagementException if an error occurs while fetching owner details.
*/
OwnerWithDeviceDTO getOwnerWithDeviceByDeviceId(int deviceId) throws DeviceManagementDAOException;
OwnerWithDeviceDTO getOwnerWithDeviceByDeviceId(int deviceId, String deviceOwner, String deviceName, String deviceStatus)
throws DeviceManagementDAOException;
/**
* Get owner details and device IDs for a given owner and tenant.
* @param tenantId the tenant id which devices need to be retried
* @param deviceTypeId the device type id
* @param deviceOwner owner of the device
* @param deviceStatus status of the device
* @return {@link DeviceDetailsDTO} which contains devices details.
* @throws DeviceManagementException if an error occurs while fetching owner details.
*/
List<DeviceDetailsDTO> getDevicesByTenantId(int tenantId, int deviceTypeId) throws DeviceManagementDAOException;
List<DeviceDetailsDTO> getDevicesByTenantId(int tenantId, int deviceTypeId, String deviceOwner, String deviceStatus)
throws DeviceManagementDAOException;
/**
* Get operation details by operation code.

@ -5354,7 +5354,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
}
@Override
public OwnerWithDeviceDTO getOwnersWithDeviceIds(String owner, int deviceTypeId) throws DeviceManagementDAOException {
public OwnerWithDeviceDTO getOwnersWithDeviceIds(String owner, int deviceTypeId, String deviceOwner, String deviceName, String deviceStatus)
throws DeviceManagementDAOException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
OwnerWithDeviceDTO ownerWithDeviceDTO;
@ -5365,7 +5366,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
try {
DeviceManagementDAOFactory.openConnection();
ownerWithDeviceDTO = this.enrollmentDAO.getOwnersWithDevices(owner, allowingDeviceStatuses, tenantId, deviceTypeId);
ownerWithDeviceDTO = this.enrollmentDAO.getOwnersWithDevices(owner, allowingDeviceStatuses, tenantId, deviceTypeId, deviceOwner, deviceName, deviceStatus);
if (ownerWithDeviceDTO == null) {
String msg = "No data found for owner: " + owner;
log.error(msg);
@ -5389,13 +5390,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
@Override
public OwnerWithDeviceDTO getOwnerWithDeviceByDeviceId(int deviceId) throws DeviceManagementDAOException {
public OwnerWithDeviceDTO getOwnerWithDeviceByDeviceId(int deviceId, String deviceOwner, String deviceName, String deviceStatus)
throws DeviceManagementDAOException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
OwnerWithDeviceDTO deviceOwnerWithStatus;
try {
DeviceManagementDAOFactory.openConnection();
deviceOwnerWithStatus = enrollmentDAO.getOwnerWithDeviceByDeviceId(deviceId, tenantId);
deviceOwnerWithStatus = enrollmentDAO.getOwnerWithDeviceByDeviceId(deviceId, tenantId, deviceOwner, deviceName, deviceStatus);
if (deviceOwnerWithStatus == null) {
throw new DeviceManagementDAOException("No data found for device ID: " + deviceId);
}
@ -5416,7 +5418,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
}
@Override
public List<DeviceDetailsDTO> getDevicesByTenantId(int tenantId, int deviceTypeId) throws DeviceManagementDAOException {
public List<DeviceDetailsDTO> getDevicesByTenantId(int tenantId, int deviceTypeId, String deviceOwner, String deviceStatus)
throws DeviceManagementDAOException {
List<DeviceDetailsDTO> devices;
List<String> allowingDeviceStatuses = new ArrayList<>();
allowingDeviceStatuses.add(EnrolmentInfo.Status.ACTIVE.toString());
@ -5424,7 +5427,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
allowingDeviceStatuses.add(EnrolmentInfo.Status.UNREACHABLE.toString());
try {
DeviceManagementDAOFactory.openConnection();
devices = enrollmentDAO.getDevicesByTenantId(tenantId, allowingDeviceStatuses, deviceTypeId);
devices = enrollmentDAO.getDevicesByTenantId(tenantId, allowingDeviceStatuses, deviceTypeId, deviceOwner, deviceStatus);
if (devices == null || devices.isEmpty()) {
String msg = "No devices found for tenant ID: " + tenantId;
log.error(msg);

@ -378,11 +378,15 @@ public interface GroupManagementProviderService {
*
* @param groupName the name of the group.
* @param deviceTypeId the device type id
* @param deviceOwner owner of the device
* @param deviceName name of the device
* @param deviceStatus status of the device
* @param offset the offset for the data set
* @param limit the limit for the data set
* @return {@link GroupDetailsDTO} which containing group details and a list of device IDs
* @throws GroupManagementException if an error occurs while fetching group details.
*/
GroupDetailsDTO getGroupDetailsWithDevices(String groupName, int deviceTypeId, int offset, int limit) throws GroupManagementException;
GroupDetailsDTO getGroupDetailsWithDevices(String groupName, int deviceTypeId, String deviceOwner, String deviceName, String deviceStatus,
int offset, int limit) throws GroupManagementException;
}

@ -18,7 +18,7 @@
package io.entgra.device.mgt.core.device.mgt.core.service;
import io.entgra.device.mgt.core.device.mgt.common.*;
import io.entgra.device.mgt.core.device.mgt.common.EnrolmentInfo;
import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroup;
import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupConstants;
import io.entgra.device.mgt.core.device.mgt.common.group.mgt.DeviceGroupRoleWrapper;
@ -40,8 +40,13 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import io.entgra.device.mgt.core.device.mgt.common.Device;
import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier;
import io.entgra.device.mgt.core.device.mgt.common.DeviceManagementConstants;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceNotFoundException;
import io.entgra.device.mgt.core.device.mgt.common.GroupPaginationRequest;
import io.entgra.device.mgt.core.device.mgt.common.PaginationResult;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.TransactionManagementException;
import io.entgra.device.mgt.core.device.mgt.core.event.config.GroupAssignmentEventOperationExecutor;
import io.entgra.device.mgt.core.device.mgt.core.geo.task.GeoFenceEventOperationManager;
@ -1684,8 +1689,8 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
}
@Override
public GroupDetailsDTO getGroupDetailsWithDevices(String groupName, int deviceTypeId, int offset, int limit)
throws GroupManagementException {
public GroupDetailsDTO getGroupDetailsWithDevices(String groupName, int deviceTypeId, String deviceOwner, String deviceName, String deviceStatus,
int offset, int limit) throws GroupManagementException {
if (log.isDebugEnabled()) {
log.debug("Retrieving group details and device IDs for group: " + groupName);
}
@ -1699,7 +1704,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
try {
GroupManagementDAOFactory.openConnection();
groupDetailsWithDevices = this.groupDAO.getGroupDetailsWithDevices(groupName, allowingDeviceStatuses,
deviceTypeId, tenantId, offset, limit);
deviceTypeId, tenantId, deviceOwner, deviceName, deviceStatus, offset, limit);
} catch (GroupManagementDAOException | SQLException e) {
String msg = "Error occurred while retrieving group details and device IDs for group: " + groupName;
log.error(msg, e);

Loading…
Cancel
Save