Merge pull request 'Fix filtering issues' (#460) from rajitha/device-mgt-core:local-appm-imp into APPM_Imp

Reviewed-on: community/device-mgt-core#460
APPM_Imp
Rajitha Kumara 4 months ago
commit 99b88d6da8

@ -22,6 +22,7 @@ package io.entgra.device.mgt.core.application.mgt.common;
import java.sql.Timestamp;
public class DeviceSubscription {
private int deviceId;
private String deviceName;
private String deviceIdentifier;
private String deviceStatus;
@ -31,6 +32,14 @@ public class DeviceSubscription {
private Timestamp dateOfLastUpdate;
private SubscriptionData subscriptionData;
public int getDeviceId() {
return deviceId;
}
public void setDeviceId(int deviceId) {
this.deviceId = deviceId;
}
public String getDeviceName() {
return deviceName;
}

@ -17,8 +17,20 @@
*
*/
package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.bean;
package io.entgra.device.mgt.core.application.mgt.common;
public enum DeviceSubscriptionStatus {
COMPLETED, ERROR, NEW, SUBSCRIBED
public class SubscriptionMetadata {
public static final class DeviceSubscriptionStatus {
public static final String NEW = "NEW";
public static final String PENDING = "PENDING";
public static final String COMPLETED = "COMPLETED";
public static final String FAILED = "FAILED";
}
public static final class SubscriptionTypes {
public static final String ROLE = "role";
public static final String DEVICE = "device";
public static final String GROUP = "group";
public static final String USER = "user";
}
}

@ -17,45 +17,47 @@
*
*/
package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.bean;
package io.entgra.device.mgt.core.application.mgt.common;
public class RoleBasedSubscriptionInfo {
import java.util.List;
public class SubscriptionResponse {
private String applicationUUID;
private String roleName;
private String subscriptionStatus;
public static String TRIGGERED_FROM_VALUE = "role";
private DeviceSubscriptionStatus deviceSubscriptionStatus;
private int count;
private List<?> data;
public String getApplicationUUID() {
return applicationUUID;
public SubscriptionResponse(String applicationUUID, int count, List<?> data) {
this.applicationUUID = applicationUUID;
this.count = count;
this.data = data;
}
public void setApplicationUUID(String applicationUUID) {
public SubscriptionResponse(String applicationUUID, List<?> data) {
this.applicationUUID = applicationUUID;
this.data = data;
}
public String getRoleName() {
return roleName;
public String getApplicationUUID() {
return applicationUUID;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
public void setApplicationUUID(String applicationUUID) {
this.applicationUUID = applicationUUID;
}
public String getSubscriptionStatus() {
return subscriptionStatus;
public int getCount() {
return count;
}
public void setSubscriptionStatus(String subscriptionStatus) {
this.subscriptionStatus = subscriptionStatus;
public void setCount(int count) {
this.count = count;
}
public DeviceSubscriptionStatus getDeviceSubscriptionStatus() {
return deviceSubscriptionStatus;
public List<?> getData() {
return data;
}
public void setDeviceSubscriptionStatus(DeviceSubscriptionStatus deviceSubscriptionStatus) {
this.deviceSubscriptionStatus = deviceSubscriptionStatus;
public void setData(List<?> data) {
this.data = data;
}
}

@ -0,0 +1,68 @@
/*
* 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 SubscriptionStatistics {
private float completedPercentage;
private float failedPercentage;
private float pendingPercentage;
private float newDevicesPercentage;
public SubscriptionStatistics() {}
public SubscriptionStatistics(float completedPercentage, float failedPercentage, float pendingPercentage,
float newDevicesPercentage) {
this.completedPercentage = completedPercentage;
this.failedPercentage = failedPercentage;
this.pendingPercentage = pendingPercentage;
this.newDevicesPercentage = newDevicesPercentage;
}
public float getCompletedPercentage() {
return completedPercentage;
}
public void setCompletedPercentage(float completedPercentage) {
this.completedPercentage = completedPercentage;
}
public float getFailedPercentage() {
return failedPercentage;
}
public void setFailedPercentage(float failedPercentage) {
this.failedPercentage = failedPercentage;
}
public float getPendingPercentage() {
return pendingPercentage;
}
public void setPendingPercentage(float pendingPercentage) {
this.pendingPercentage = pendingPercentage;
}
public float getNewDevicesPercentage() {
return newDevicesPercentage;
}
public void setNewDevicesPercentage(float newDevicesPercentage) {
this.newDevicesPercentage = newDevicesPercentage;
}
}

@ -0,0 +1,50 @@
/*
* 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.dto;
public class SubscriptionStatisticDTO {
private int completedDeviceCount;
private int pendingDevicesCount;
private int failedDevicesCount;
public int getCompletedDeviceCount() {
return completedDeviceCount;
}
public void setCompletedDeviceCount(int completedDeviceCount) {
this.completedDeviceCount = completedDeviceCount;
}
public int getPendingDevicesCount() {
return pendingDevicesCount;
}
public void setPendingDevicesCount(int pendingDevicesCount) {
this.pendingDevicesCount = pendingDevicesCount;
}
public int getFailedDevicesCount() {
return failedDevicesCount;
}
public void setFailedDevicesCount(int failedDevicesCount) {
this.failedDevicesCount = failedDevicesCount;
}
}

@ -24,6 +24,8 @@ 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;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionResponse;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionType;
import io.entgra.device.mgt.core.application.mgt.common.dto.CategorizedSubscriptionCountsDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO;
@ -229,77 +231,34 @@ public interface SubscriptionManager {
Activity getOperationAppDetails(String id) throws SubscriptionManagementException;
/**
* Retrieves the group details associated with a given app release UUID.
*
* @param uuid the UUID of the app release
* @param subscriptionStatus the status of the subscription (subscribed or unsubscribed)
* @param offset the offset for the data set
* @param limit the limit for the data set
* @return {@link SubscriptionsDTO} which contains the details of subscriptions.
* @throws ApplicationManagementException if an error occurs while fetching the group details
* Get subscription data describes by {@link SubscriptionInfo} entity
* @param subscriptionInfo {@link SubscriptionInfo}
* @param limit Limit value
* @param offset Offset value
* @return {@link SubscriptionResponse}
* @throws ApplicationManagementException Throws when error encountered while getting subscription data
*/
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.
*
* @param uuid the UUID of the app release
* @param subscriptionStatus the status of the subscription (subscribed or unsubscribed)
* @param offset the offset for the data set
* @param limit the limit for the data set
* @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, PaginationRequest request,
int offset, int limit) throws ApplicationManagementException;
/**
* Retrieves the Role details associated with a given app release UUID.
*
* @param uuid the UUID of the app release
* @param subscriptionStatus the status of the subscription (subscribed or unsubscribed)
* @param offset the offset for the data set
* @param limit the limit for the data set
* @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, PaginationRequest request,
// int offset, int limit) throws ApplicationManagementException;
List<SubscriptionEntity> getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException;
List<DeviceSubscription> getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException;
/**
* Retrieves the Device Subscription details associated with a given app release UUID.
*
* @param uuid the UUID of the app release
* @param subscriptionStatus the status of the subscription (subscribed or unsubscribed)
* @param offset the offset for the data set
* @param limit the limit for the data set
* @return {@link DeviceSubscriptionResponseDTO} which contains the details of device subscriptions.
* @throws ApplicationManagementException if an error occurs while fetching the device subscription details
* Get status based subscription data describes by {@link SubscriptionInfo} entity
* @param subscriptionInfo {@link SubscriptionInfo}
* @param limit Limit value
* @param offset Offset value
* @return {@link SubscriptionResponse}
* @throws ApplicationManagementException Throws when error encountered while getting subscription data
*/
DeviceSubscriptionResponseDTO getDeviceSubscriptionsDetailsByUUID(String uuid, String subscriptionStatus,
PaginationRequest request, int offset,
int limit) throws ApplicationManagementException;
SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException;
/**
* Retrieves the All Device details associated with a given app release UUID.
*
* @param uuid the UUID of the app release
* @param subscriptionStatus the status of the subscription (subscribed or unsubscribed)
* @param offset the offset for the data set
* @param limit the limit for the data set
* @return {@link DeviceSubscriptionResponseDTO} which contains the details of device subscriptions.
* @throws ApplicationManagementException if an error occurs while fetching the subscription details
* Get subscription statistics related data describes by the {@link SubscriptionInfo}
* @param subscriptionInfo {@link SubscriptionInfo}
* @return {@link SubscriptionStatistics}
* @throws ApplicationManagementException Throws when error encountered while getting statistics
*/
DeviceSubscriptionResponseDTO getAllSubscriptionDetailsByUUID(String uuid, String subscriptionStatus,
PaginationRequest request, int offset,
int limit) throws ApplicationManagementException;
SubscriptionStatistics getStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException;
/**
* This method is responsible for retrieving device subscription details related to the given UUID.

@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.application.mgt.core.dao;
import io.entgra.device.mgt.core.application.mgt.common.ExecutionStatus;
import io.entgra.device.mgt.core.application.mgt.common.dto.GroupSubscriptionDTO;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity;
import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatisticDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO;
@ -400,8 +401,10 @@ public interface SubscriptionDAO {
*/
List<DeviceSubscriptionDTO> getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId,
List<Integer> deviceIds, String actionStatus, String actionType,
String actionTriggeredBy, String tabActionStatus,
int limit, int offset) throws ApplicationManagementDAOException;
String actionTriggeredBy, int limit, int offset) throws ApplicationManagementDAOException;
int getDeviceSubscriptionCount(int appReleaseId, boolean unsubscribe, int tenantId,
List<Integer> deviceIds, String actionStatus, String actionType,
String actionTriggeredBy) throws ApplicationManagementDAOException;
/**
* This method is used to get the details of device subscriptions related to a UUID.
@ -420,6 +423,10 @@ public interface SubscriptionDAO {
List<DeviceSubscriptionDTO> getAllSubscriptionsDetails(int appReleaseId, boolean unsubscribe, int tenantId, String actionStatus, String actionType,
String actionTriggeredBy, int offset, int limit) throws ApplicationManagementDAOException;
int getAllSubscriptionsCount(int appReleaseId, boolean unsubscribe, int tenantId,
String actionStatus, String actionType, String actionTriggeredBy)
throws ApplicationManagementDAOException;
/**
* This method is used to get the counts of all subscription types related to a UUID.
*
@ -519,4 +526,8 @@ public interface SubscriptionDAO {
* @throws ApplicationManagementDAOException if connection establishment or SQL execution fails.
*/
int getUserUnsubscriptionCount(int appReleaseId, int tenantId) throws ApplicationManagementDAOException;
SubscriptionStatisticDTO getSubscriptionStatistic(List<Integer> deviceIds, String subscriptionType, boolean isUnsubscribed,
int tenantId) throws ApplicationManagementDAOException;
}

@ -17,9 +17,11 @@
*/
package io.entgra.device.mgt.core.application.mgt.core.dao.impl.subscription;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionMetadata;
import io.entgra.device.mgt.core.application.mgt.common.dto.GroupSubscriptionDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceOperationDTO;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity;
import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatisticDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO;
import io.entgra.device.mgt.core.application.mgt.core.dao.SubscriptionDAO;
import io.entgra.device.mgt.core.application.mgt.core.dao.impl.AbstractDAOImpl;
@ -47,6 +49,7 @@ import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.stream.Collectors;
@ -1914,8 +1917,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
@Override
public List<DeviceSubscriptionDTO> getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId,
List<Integer> deviceIds, String actionStatus, String actionType,
String actionTriggeredBy, String tabActionStatus,
int offset, int limit) throws ApplicationManagementDAOException {
String actionTriggeredBy, int limit, int offset) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Getting device subscriptions for the application release id " + appReleaseId
+ " and device ids " + deviceIds + " from the database");
@ -2014,6 +2016,71 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
}
@Override
public int getDeviceSubscriptionCount(int appReleaseId, boolean unsubscribe, int tenantId,
List<Integer> deviceIds, String actionStatus, String actionType,
String actionTriggeredBy) throws ApplicationManagementDAOException {
int deviceCount = 0;
try {
Connection conn = this.getDBConnection();
StringBuilder sql = new StringBuilder("SELECT COUNT(DISTINCT DS.DM_DEVICE_ID) AS COUNT "
+ "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(",")) + ") ");
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 ?");
}
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(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 "
+ appReleaseId + " and device ids " + deviceIds);
}
if (rs.next()) {
deviceCount = rs.getInt("COUNT");
}
return deviceCount;
}
} catch (SQLException e) {
String msg = "Error occurred while running SQL to get device subscription data for application ID: " + appReleaseId
+ " and device ids: " + deviceIds + ".";
log.error(msg, e);
throw new ApplicationManagementDAOException(msg, e);
}
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the DB connection for getting device subscriptions for "
+ "application Id: " + appReleaseId + " and device ids: " + deviceIds + ".";
log.error(msg, e);
throw new ApplicationManagementDAOException(msg, e);
}
}
// @Override
// public List<DeviceSubscriptionDTO> getSubscriptionDetailsByDeviceIds(int appReleaseId, boolean unsubscribe, int tenantId,
// List<Integer> deviceIds, String actionStatus, String actionType,
@ -2205,6 +2272,72 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
}
}
@Override
public int getAllSubscriptionsCount(int appReleaseId, boolean unsubscribe, int tenantId,
String actionStatus, String actionType, String actionTriggeredBy)
throws ApplicationManagementDAOException {
int deviceCount = 0;
if (log.isDebugEnabled()) {
log.debug("Getting device subscriptions for the application release id " + appReleaseId
+ " from the database");
}
String actionTriggeredColumn = unsubscribe ? "DS.UNSUBSCRIBED_BY" : "DS.SUBSCRIBED_BY";
StringBuilder sql = new StringBuilder("SELECT COUNT(DISTINCT DS.DM_DEVICE_ID) AS COUNT "
+ "FROM AP_DEVICE_SUBSCRIPTION DS "
+ "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 ?");
}
try {
Connection conn = this.getDBConnection();
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 + "%");
}
try (ResultSet rs = ps.executeQuery()) {
if (log.isDebugEnabled()) {
log.debug("Successfully retrieved device subscriptions for application release id "
+ appReleaseId);
}
if (rs.next()) {
deviceCount = rs.getInt("COUNT");
}
}
return deviceCount;
}
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the DB connection for getting device subscription for "
+ "application Id: " + appReleaseId + ".";
log.error(msg, e);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) {
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);
}
}
@Override
public int getAllSubscriptionCount(int appReleaseId, int tenantId)
throws ApplicationManagementDAOException {
@ -2596,4 +2729,56 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
throw new ApplicationManagementDAOException(msg, e);
}
}
@Override
public SubscriptionStatisticDTO getSubscriptionStatistic(List<Integer> deviceIds, String subscriptionType,
boolean isUnsubscribed, int tenantId)
throws ApplicationManagementDAOException {
SubscriptionStatisticDTO subscriptionStatisticDTO = new SubscriptionStatisticDTO();
String deviceIdsString = deviceIds.stream().map(String::valueOf).collect(Collectors.joining(","));
boolean doesAllEntriesRequired = true;
try {
Connection connection = getDBConnection();
String sql = "SELECT COUNT(DISTINCT ID) AS COUNT, " +
"STATUS FROM AP_DEVICE_SUBSCRIPTION WHERE " +
"TENANT_ID = ? AND UNSUBSCRIBED = ? AND DM_DEVICE_ID IN ("+ deviceIdsString + ")";
if (!Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.DEVICE)) {
sql = sql + " AND ACTION_TRIGGERED_FROM = ?";
doesAllEntriesRequired = false;
}
sql = sql + " GROUP BY (STATUS)";
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
int idx = 1;
preparedStatement.setInt(idx++, tenantId);
preparedStatement.setBoolean(idx++, isUnsubscribed);
if (!doesAllEntriesRequired) {
preparedStatement.setString(idx, subscriptionType);
}
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
int count = resultSet.getInt("COUNT");
String status = resultSet.getString("STATUS");
if (Objects.equals(status, "COMPLETED")) {
subscriptionStatisticDTO.setCompletedDeviceCount(count);
} else if (Objects.equals(status, "PENDING")) {
subscriptionStatisticDTO.setPendingDevicesCount(count);
} else {
subscriptionStatisticDTO.setFailedDevicesCount(count);
}
}
}
}
return subscriptionStatisticDTO;
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the DB connection for getting subscription statistics";
log.error(msg, e);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while running SQL for getting subscription statistics";
log.error(msg, e);
throw new ApplicationManagementDAOException(msg, e);
}
}
}

@ -23,7 +23,9 @@ import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription;
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.SubscriptionStatistics;
import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatisticDTO;
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;
@ -36,23 +38,27 @@ import java.util.stream.Collectors;
public class SubscriptionManagementHelperUtil {
public static List<DeviceSubscription> getDeviceSubscriptionData(List<DeviceSubscriptionDTO> deviceSubscriptionDTOS,
DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria) throws DeviceManagementException {
DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria,
boolean isUnsubscribed)
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);
return populateDeviceData(deviceSubscriptionDTOS, devices, isUnsubscribed);
}
private static List<DeviceSubscription> populateDeviceData(List<DeviceSubscriptionDTO> deviceSubscriptionDTOS, List<Device> devices) {
private static List<DeviceSubscription> populateDeviceData(List<DeviceSubscriptionDTO> deviceSubscriptionDTOS,
List<Device> devices, boolean isUnsubscribed) {
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.setDeviceId(device.getId());
deviceSubscription.setDeviceIdentifier(device.getDeviceIdentifier());
deviceSubscription.setDeviceOwner(device.getEnrolmentInfo().getOwner());
deviceSubscription.setDeviceType(device.getType());
@ -60,10 +66,7 @@ public class SubscriptionManagementHelperUtil {
deviceSubscription.setDeviceStatus(device.getEnrolmentInfo().getStatus().name());
deviceSubscription.setOwnershipType(device.getEnrolmentInfo().getOwnership().name());
deviceSubscription.setDateOfLastUpdate(new Timestamp(device.getEnrolmentInfo().getDateOfLastUpdate()));
SubscriptionData subscriptionData = new SubscriptionData();
subscriptionData.setTriggeredBy(deviceSubscriptionDTO.getActionTriggeredFrom());
subscriptionData.setTriggeredAt(deviceSubscriptionDTO.getSubscribedTimestamp());
subscriptionData.setSubscriptionType(deviceSubscriptionDTO.getStatus());
SubscriptionData subscriptionData = getSubscriptionData(isUnsubscribed, deviceSubscriptionDTO);
deviceSubscription.setSubscriptionData(subscriptionData);
deviceSubscriptions.add(deviceSubscription);
}
@ -71,13 +74,45 @@ public class SubscriptionManagementHelperUtil {
return deviceSubscriptions;
}
private static SubscriptionData getSubscriptionData(boolean isUnsubscribed, DeviceSubscriptionDTO deviceSubscriptionDTO) {
SubscriptionData subscriptionData = new SubscriptionData();
subscriptionData.setTriggeredBy(isUnsubscribed ? deviceSubscriptionDTO.getUnsubscribedBy() :
deviceSubscriptionDTO.getSubscribedBy());
subscriptionData.setTriggeredAt(deviceSubscriptionDTO.getSubscribedTimestamp());
subscriptionData.setDeviceSubscriptionStatus(deviceSubscriptionDTO.getStatus());
subscriptionData.setSubscriptionType(deviceSubscriptionDTO.getActionTriggeredFrom());
return subscriptionData;
}
public static String getDeviceSubscriptionStatus(SubscriptionInfo subscriptionInfo) {
return getDeviceSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionFilterCriteria().
getFilteringDeviceSubscriptionStatus(), subscriptionInfo.getDeviceSubscriptionStatus());
getFilteringDeviceSubscriptionStatus(), subscriptionInfo.getDeviceSubscriptionStatus());
}
public static String getDeviceSubscriptionStatus(String deviceSubscriptionStatusFilter, String deviceSubscriptionStatus) {
return (deviceSubscriptionStatusFilter != null && !deviceSubscriptionStatusFilter.isEmpty()) ?
deviceSubscriptionStatusFilter : deviceSubscriptionStatus;
}
public static SubscriptionStatistics getSubscriptionStatistics(SubscriptionStatisticDTO subscriptionStatisticDTO, int allDeviceCount) {
SubscriptionStatistics subscriptionStatistics = new SubscriptionStatistics();
subscriptionStatistics.setCompletedPercentage(
getPercentage(subscriptionStatisticDTO.getCompletedDeviceCount(), allDeviceCount));
subscriptionStatistics.setPendingPercentage(
getPercentage(subscriptionStatisticDTO.getPendingDevicesCount(), allDeviceCount));
subscriptionStatistics.setFailedPercentage(
getPercentage(subscriptionStatisticDTO.getFailedDevicesCount(), allDeviceCount));
subscriptionStatistics.setNewDevicesPercentage(getPercentage((allDeviceCount -
subscriptionStatisticDTO.getCompletedDeviceCount() -
subscriptionStatisticDTO.getPendingDevicesCount() -
subscriptionStatisticDTO.getFailedDevicesCount()), allDeviceCount));
return subscriptionStatistics;
}
public static float getPercentage(int numerator, int denominator) {
if (denominator <= 0) {
return 0.0f;
}
return ((float) numerator / (float) denominator) * 100;
}
}

@ -20,6 +20,7 @@
package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionMetadata;
import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl.DeviceBasedSubscriptionManagementHelperServiceImpl;
import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl.GroupBasedSubscriptionManagementHelperServiceImpl;
import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl.RoleBasedSubscriptionManagementHelperServiceImpl;
@ -29,10 +30,7 @@ import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.serv
import java.util.Objects;
public class SubscriptionManagementServiceProvider {
private SubscriptionManagementServiceProvider() {}
private static class SubscriptionManagementProviderServiceHolder {
private static final SubscriptionManagementServiceProvider INSTANCE = new SubscriptionManagementServiceProvider();
private SubscriptionManagementServiceProvider() {
}
public static SubscriptionManagementServiceProvider getInstance() {
@ -44,10 +42,18 @@ public class SubscriptionManagementServiceProvider {
}
private SubscriptionManagementHelperService getSubscriptionManagementHelperService(String subscriptionType) {
if (Objects.equals(subscriptionType, "role")) return RoleBasedSubscriptionManagementHelperServiceImpl.getInstance();
if (Objects.equals(subscriptionType, "group")) return GroupBasedSubscriptionManagementHelperServiceImpl.getInstance();
if (Objects.equals(subscriptionType, "user")) return UserBasedSubscriptionManagementHelperServiceImpl.getInstance();
if (Objects.equals(subscriptionType, "device")) return DeviceBasedSubscriptionManagementHelperServiceImpl.getInstance();
if (Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.ROLE))
return RoleBasedSubscriptionManagementHelperServiceImpl.getInstance();
if (Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.GROUP))
return GroupBasedSubscriptionManagementHelperServiceImpl.getInstance();
if (Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.USER))
return UserBasedSubscriptionManagementHelperServiceImpl.getInstance();
if (Objects.equals(subscriptionType, SubscriptionMetadata.SubscriptionTypes.DEVICE))
return DeviceBasedSubscriptionManagementHelperServiceImpl.getInstance();
throw new UnsupportedOperationException("Subscription type: " + subscriptionType + " not supports");
}
private static class SubscriptionManagementProviderServiceHolder {
private static final SubscriptionManagementServiceProvider INSTANCE = new SubscriptionManagementServiceProvider();
}
}

@ -20,34 +20,28 @@
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;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionMetadata;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionResponse;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics;
import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO;
import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException;
import io.entgra.device.mgt.core.application.mgt.common.exception.DBConnectionException;
import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManagementDAOException;
import io.entgra.device.mgt.core.application.mgt.core.exception.NotFoundException;
import io.entgra.device.mgt.core.application.mgt.core.internal.DataHolder;
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.Device;
import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest;
import io.entgra.device.mgt.core.device.mgt.common.PaginationResult;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException;
import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.api.UserStoreManager;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
@ -55,20 +49,20 @@ import java.util.stream.Collectors;
public class DeviceBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService {
private static final Log log = LogFactory.getLog(DeviceBasedSubscriptionManagementHelperServiceImpl.class);
private DeviceBasedSubscriptionManagementHelperServiceImpl() {}
private static class DeviceBasedSubscriptionManagementHelperServiceImplHolder {
private static final DeviceBasedSubscriptionManagementHelperServiceImpl INSTANCE
= new DeviceBasedSubscriptionManagementHelperServiceImpl();
private DeviceBasedSubscriptionManagementHelperServiceImpl() {
}
public static DeviceBasedSubscriptionManagementHelperServiceImpl getInstance() {
return DeviceBasedSubscriptionManagementHelperServiceImpl.DeviceBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE;
}
@Override
public List<DeviceSubscription> getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException {
final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus());
List<DeviceSubscriptionDTO> deviceSubscriptionDTOS;
int deviceCount = 0;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
@ -84,27 +78,34 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc
String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo);
DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria();
DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService();
if (Objects.equals("NEW", deviceSubscriptionStatus)) {
if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) {
deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO.
getId(),isUnsubscribe, tenantId, null, subscriptionInfo.getSubscriptionType(),
deviceSubscriptionFilterCriteria.getTriggeredBy(),-1, -1);
getId(), isUnsubscribe, tenantId, null, null,
deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1);
List<Integer> deviceIdsOfSubscription = deviceSubscriptionDTOS.stream().
map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList());
List<Integer> newDeviceIds = HelperUtil.getDeviceManagementProviderService().
getDevicesNotInGivenIdList(deviceIdsOfSubscription, new PaginationRequest(offset, limit));
List<Integer> newDeviceIds = deviceManagementProviderService.getDevicesNotInGivenIdList(deviceIdsOfSubscription,
new PaginationRequest(offset, limit));
deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList());
deviceCount = deviceManagementProviderService.getDeviceCountNotInGivenIdList(deviceIdsOfSubscription);
} else {
deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO.
getId(),isUnsubscribe, tenantId, null, subscriptionInfo.getSubscriptionType(),
deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset);
}
return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria());
getId(), isUnsubscribe, tenantId, subscriptionInfo.getDeviceSubscriptionStatus(), null,
deviceSubscriptionFilterCriteria.getTriggeredBy(), offset, limit);
deviceCount = subscriptionDAO.getAllSubscriptionsCount(applicationReleaseDTO.
getId(), isUnsubscribe, tenantId, subscriptionInfo.getDeviceSubscriptionStatus(), null,
deviceSubscriptionFilterCriteria.getTriggeredBy());
}
List<DeviceSubscription> deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe);
return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions);
} catch (DeviceManagementException e) {
String msg = "Error encountered while getting device details";
log.error(msg, e);
@ -119,13 +120,20 @@ public class DeviceBasedSubscriptionManagementHelperServiceImpl implements Subsc
}
@Override
public List<SubscriptionEntity> getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException {
return Collections.emptyList();
return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), Collections.emptyList());
}
@Override
public void getSubscriptionStatistics() throws ApplicationManagementException {
// todo: analytics engine
public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo)
throws ApplicationManagementException {
return null;
}
private static class DeviceBasedSubscriptionManagementHelperServiceImplHolder {
private static final DeviceBasedSubscriptionManagementHelperServiceImpl INSTANCE
= new DeviceBasedSubscriptionManagementHelperServiceImpl();
}
}

@ -20,13 +20,16 @@
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;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionMetadata;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionResponse;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics;
import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatisticDTO;
import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException;
import io.entgra.device.mgt.core.application.mgt.common.exception.DBConnectionException;
import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManagementDAOException;
@ -35,10 +38,12 @@ 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.Device;
import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest;
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.DeviceManagementProviderService;
import io.entgra.device.mgt.core.device.mgt.core.service.GroupManagementProviderService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -50,10 +55,8 @@ import java.util.stream.Collectors;
public class GroupBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService {
private static final Log log = LogFactory.getLog(GroupBasedSubscriptionManagementHelperServiceImpl.class);
private GroupBasedSubscriptionManagementHelperServiceImpl() {}
private static class GroupBasedSubscriptionManagementHelperServiceImplHolder {
private static final GroupBasedSubscriptionManagementHelperServiceImpl INSTANCE
= new GroupBasedSubscriptionManagementHelperServiceImpl();
private GroupBasedSubscriptionManagementHelperServiceImpl() {
}
public static GroupBasedSubscriptionManagementHelperServiceImpl getInstance() {
@ -61,11 +64,12 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr
}
@Override
public List<DeviceSubscription> getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException {
final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus());
List<DeviceSubscriptionDTO> deviceSubscriptionDTOS;
int deviceCount = 0;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
@ -89,43 +93,49 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr
String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo);
DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria();
DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService();
GroupManagementProviderService groupManagementProviderService = HelperUtil.getGroupManagementProviderService();
GroupDetailsDTO groupDetailsDTO;
List<Integer> deviceIdsOwnByGroup;
if (Objects.equals("NEW", deviceSubscriptionStatus)) {
groupDetailsDTO = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(),
applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(),
deviceSubscriptionFilterCriteria.getDeviceStatus(), -1, -1);
deviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds();
List<Integer> allDeviceIdsOwnByGroup = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(),
applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(),
deviceSubscriptionFilterCriteria.getDeviceStatus(), -1, -1).getDeviceIds();
if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) {
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByGroup, null,
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(),
null, -1, -1);
isUnsubscribe, tenantId, allDeviceIdsOwnByGroup, null,
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1);
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());
for (Integer deviceId : deviceIdsOfSubscription) {
allDeviceIdsOwnByGroup.remove(deviceId);
}
List<Integer> paginatedNewDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(allDeviceIdsOwnByGroup,
new PaginationRequest(offset, limit));
deviceSubscriptionDTOS = paginatedNewDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList());
deviceCount = allDeviceIdsOwnByGroup.size();
} else {
groupDetailsDTO = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(),
applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(),
deviceSubscriptionFilterCriteria.getDeviceStatus(), offset, limit);
deviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds();
List<Integer> paginatedDeviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds();
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByGroup, null,
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(),
null, limit, offset);
}
return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria());
isUnsubscribe, tenantId, paginatedDeviceIdsOwnByGroup, subscriptionInfo.getDeviceSubscriptionStatus(),
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1);
deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, allDeviceIdsOwnByGroup, subscriptionInfo.getDeviceSubscriptionStatus(),
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy());
}
List<DeviceSubscription> deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe);
return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions);
} catch (GroupManagementException e) {
String msg = "Error encountered while retrieving group details for group: " + subscriptionInfo.getIdentifier();
log.error(msg, e);
@ -143,7 +153,7 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr
}
@Override
public List<SubscriptionEntity> getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException {
final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus());
final int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
@ -157,8 +167,11 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr
log.error(msg);
throw new NotFoundException(msg);
}
return subscriptionDAO.
List<SubscriptionEntity> subscriptionEntities = subscriptionDAO.
getGroupsSubscriptionDetailsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit);
int subscriptionCount = isUnsubscribe ? subscriptionDAO.getGroupUnsubscriptionCount(applicationReleaseDTO.getId(), tenantId) :
subscriptionDAO.getGroupSubscriptionCount(applicationReleaseDTO.getId(), tenantId);
return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionCount, subscriptionEntities);
} catch (DBConnectionException | ApplicationManagementDAOException e) {
String msg = "Error encountered while connecting to the database";
log.error(msg, e);
@ -169,7 +182,34 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr
}
@Override
public void getSubscriptionStatistics() throws ApplicationManagementException {
// todo: analytics engine
public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo)
throws ApplicationManagementException {
final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus());
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
ConnectionManagerUtil.openDBConnection();
List<Device> devices = HelperUtil.getGroupManagementProviderService().
getAllDevicesOfGroup(subscriptionInfo.getIdentifier(), false);
List<Integer> deviceIdsOwnByGroup = devices.stream().map(Device::getId).collect(Collectors.toList());
SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO.
getSubscriptionStatistic(deviceIdsOwnByGroup, subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId);
int allDeviceCount = HelperUtil.getGroupManagementProviderService().getDeviceCount(subscriptionInfo.getIdentifier());
return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount);
} catch (ApplicationManagementDAOException e) {
String msg = "Error encountered while getting subscription statistics for group: " + subscriptionInfo.getIdentifier();
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (GroupManagementException e) {
String msg = "Error encountered while getting device subscription for group: " + subscriptionInfo.getIdentifier();
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
}
}
private static class GroupBasedSubscriptionManagementHelperServiceImplHolder {
private static final GroupBasedSubscriptionManagementHelperServiceImpl INSTANCE
= new GroupBasedSubscriptionManagementHelperServiceImpl();
}
}

@ -20,12 +20,15 @@
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;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionMetadata;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionResponse;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics;
import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatisticDTO;
import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException;
import io.entgra.device.mgt.core.application.mgt.common.exception.DBConnectionException;
import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManagementDAOException;
@ -39,6 +42,7 @@ 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.PaginationResult;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException;
import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
@ -53,43 +57,25 @@ import java.util.stream.Collectors;
public class RoleBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService {
private static final Log log = LogFactory.getLog(RoleBasedSubscriptionManagementHelperServiceImpl.class);
private RoleBasedSubscriptionManagementHelperServiceImpl() {}
private static class RoleBasedSubscriptionManagementHelperServiceImplHolder {
private static final RoleBasedSubscriptionManagementHelperServiceImpl INSTANCE
= new RoleBasedSubscriptionManagementHelperServiceImpl();
private RoleBasedSubscriptionManagementHelperServiceImpl() {
}
public static RoleBasedSubscriptionManagementHelperServiceImpl getInstance() {
return RoleBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE;
}
@SuppressWarnings("unchecked")
@Override
public List<DeviceSubscription> getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException {
final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus());
List<DeviceSubscriptionDTO> deviceSubscriptionDTOS;
int deviceCount = 0;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
ConnectionManagerUtil.openDBConnection();
UserStoreManager userStoreManager = DataHolder.getInstance().getRealmService().
getTenantUserRealm(tenantId).getUserStoreManager();
String[] usersWithRole =
userStoreManager.getUserListOfRole(subscriptionInfo.getIdentifier());
List<Device> deviceListOwnByRole = new ArrayList<>();
for (String user : usersWithRole) {
PaginationRequest paginationRequest = new PaginationRequest(offset, limit);
paginationRequest.setOwner(user);
paginationRequest.setStatusList(Arrays.asList("ACTIVE", "INACTIVE", "UNREACHABLE"));
PaginationResult ownDeviceIds = HelperUtil.getDeviceManagementProviderService().
getAllDevicesIdList(paginationRequest);
if (ownDeviceIds.getData() != null) {
deviceListOwnByRole.addAll((List<Device>)ownDeviceIds.getData());
}
}
List<Integer> deviceIdsOwnByRole = deviceListOwnByRole.stream().map(Device::getId).collect(Collectors.toList());
List<Integer> deviceIdsOwnByRole = getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(), tenantId);
ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO.
getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId);
@ -102,28 +88,37 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri
String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo);
DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria();
DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService();
if (Objects.equals("NEW", deviceSubscriptionStatus)) {
if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) {
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByRole, null,
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(),
null, -1, -1);
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1);
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());
for (Integer deviceId : deviceIdsOfSubscription) {
deviceIdsOwnByRole.remove(deviceId);
}
List<Integer> paginatedNewDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(deviceIdsOwnByRole,
new PaginationRequest(offset, limit));
deviceSubscriptionDTOS = paginatedNewDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList());
deviceCount = deviceIdsOwnByRole.size();
} else {
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByRole, null,
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(),
null, limit, offset);
}
isUnsubscribe, tenantId, deviceIdsOwnByRole, subscriptionInfo.getDeviceSubscriptionStatus(),
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset);
return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria());
deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByRole, subscriptionInfo.getDeviceSubscriptionStatus(),
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy());
}
List<DeviceSubscription> deviceSubscriptions = SubscriptionManagementHelperUtil.
getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe);
return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions);
} catch (UserStoreException e) {
String msg = "Error encountered while getting the user management store for tenant id " + tenantId;
@ -143,7 +138,7 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri
}
@Override
public List<SubscriptionEntity> getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException {
final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus());
final int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
@ -157,8 +152,11 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri
log.error(msg);
throw new NotFoundException(msg);
}
return subscriptionDAO.
List<SubscriptionEntity> subscriptionEntities = subscriptionDAO.
getRoleSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit);
int subscriptionCount = isUnsubscribe ? subscriptionDAO.getRoleUnsubscriptionCount(applicationReleaseDTO.getId(), tenantId) :
subscriptionDAO.getRoleSubscriptionCount(applicationReleaseDTO.getId(), tenantId);
return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionCount, subscriptionEntities);
} catch (DBConnectionException | ApplicationManagementDAOException e) {
String msg = "Error encountered while connecting to the database";
log.error(msg, e);
@ -169,7 +167,47 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri
}
@Override
public void getSubscriptionStatistics() throws ApplicationManagementException {
// todo: analytics engine
public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException {
final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus());
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
ConnectionManagerUtil.openDBConnection();
List<Integer> deviceIdsOwnByRole = getDeviceIdsOwnByRole(subscriptionInfo.getIdentifier(), tenantId);
SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO.
getSubscriptionStatistic(deviceIdsOwnByRole, subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId);
int allDeviceCount = deviceIdsOwnByRole.size();
return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount);
} catch (DeviceManagementException | ApplicationManagementDAOException | UserStoreException e) {
String msg = "Error encountered while getting subscription statistics for role: " + subscriptionInfo.getIdentifier();
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
}
}
@SuppressWarnings("unchecked")
private List<Integer> getDeviceIdsOwnByRole(String roleName, int tenantId) throws UserStoreException, DeviceManagementException {
UserStoreManager userStoreManager = DataHolder.getInstance().getRealmService().
getTenantUserRealm(tenantId).getUserStoreManager();
String[] usersWithRole =
userStoreManager.getUserListOfRole(roleName);
List<Device> deviceListOwnByRole = new ArrayList<>();
for (String user : usersWithRole) {
PaginationRequest paginationRequest = new PaginationRequest(-1, -1);
paginationRequest.setOwner(user);
paginationRequest.setStatusList(Arrays.asList("ACTIVE", "INACTIVE", "UNREACHABLE"));
PaginationResult ownDeviceIds = HelperUtil.getDeviceManagementProviderService().
getAllDevicesIdList(paginationRequest);
if (ownDeviceIds.getData() != null) {
deviceListOwnByRole.addAll((List<Device>) ownDeviceIds.getData());
}
}
return deviceListOwnByRole.stream().map(Device::getId).collect(Collectors.toList());
}
private static class RoleBasedSubscriptionManagementHelperServiceImplHolder {
private static final RoleBasedSubscriptionManagementHelperServiceImpl INSTANCE
= new RoleBasedSubscriptionManagementHelperServiceImpl();
}
}

@ -23,8 +23,12 @@ import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription;
import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilterCriteria;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionMetadata;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionResponse;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics;
import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatisticDTO;
import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException;
import io.entgra.device.mgt.core.application.mgt.common.exception.DBConnectionException;
import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManagementDAOException;
@ -37,6 +41,7 @@ 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.PaginationResult;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException;
import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
@ -49,36 +54,25 @@ import java.util.stream.Collectors;
public class UserBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService {
private static final Log log = LogFactory.getLog(UserBasedSubscriptionManagementHelperServiceImpl.class);
private UserBasedSubscriptionManagementHelperServiceImpl() {}
private static class UserBasedSubscriptionManagementHelperServiceImplHolder {
private static final UserBasedSubscriptionManagementHelperServiceImpl INSTANCE
= new UserBasedSubscriptionManagementHelperServiceImpl();
private UserBasedSubscriptionManagementHelperServiceImpl() {
}
public static UserBasedSubscriptionManagementHelperServiceImpl getInstance() {
return UserBasedSubscriptionManagementHelperServiceImpl.UserBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE;
}
@SuppressWarnings("unchecked")
@Override
public List<DeviceSubscription> getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
public SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException {
final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus());
List<DeviceSubscriptionDTO> deviceSubscriptionDTOS;
int deviceCount = 0;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
ConnectionManagerUtil.openDBConnection();
List<Device> deviceListOwnByUser = new ArrayList<>();
PaginationRequest paginationRequest = new PaginationRequest(offset, limit);
paginationRequest.setOwner(subscriptionInfo.getIdentifier());
paginationRequest.setStatusList(Arrays.asList("ACTIVE", "INACTIVE", "UNREACHABLE"));
PaginationResult ownDeviceIds = HelperUtil.getDeviceManagementProviderService().
getAllDevicesIdList(paginationRequest);
if (ownDeviceIds.getData() != null) {
deviceListOwnByUser.addAll((List<Device>)ownDeviceIds.getData());
}
List<Integer> deviceIdsOwnByUser = deviceListOwnByUser.stream().map(Device::getId).collect(Collectors.toList());
List<Integer> deviceIdsOwnByUser = getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier());
ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO.
getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId);
@ -91,30 +85,37 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri
String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo);
DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria();
DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService();
if (Objects.equals("NEW", deviceSubscriptionStatus)) {
if (Objects.equals(SubscriptionMetadata.DeviceSubscriptionStatus.NEW, deviceSubscriptionStatus)) {
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByUser, null,
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(),
null, -1, -1);
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), -1, -1);
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());
for (Integer deviceId : deviceIdsOfSubscription) {
deviceIdsOwnByUser.remove(deviceId);
}
List<Integer> paginatedNewDeviceIds = deviceManagementProviderService.getDevicesInGivenIdList(deviceIdsOwnByUser,
new PaginationRequest(offset, limit));
deviceSubscriptionDTOS = paginatedNewDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList());
deviceCount = deviceIdsOwnByUser.size();
} else {
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByUser, null,
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(),
null, limit, offset);
}
isUnsubscribe, tenantId, deviceIdsOwnByUser, subscriptionInfo.getDeviceSubscriptionStatus(),
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset);
return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria());
deviceCount = subscriptionDAO.getDeviceSubscriptionCount(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByUser, subscriptionInfo.getDeviceSubscriptionStatus(),
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy());
}
List<DeviceSubscription> deviceSubscriptions = SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria(), isUnsubscribe);
return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), deviceCount, deviceSubscriptions);
} catch (DeviceManagementException e) {
String msg = "Error encountered while getting device details";
log.error(msg, e);
@ -129,7 +130,7 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri
}
@Override
public List<SubscriptionEntity> getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
public SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException {
final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus());
final int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
@ -143,8 +144,11 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri
log.error(msg);
throw new NotFoundException(msg);
}
return subscriptionDAO.
List<SubscriptionEntity> subscriptionEntities = subscriptionDAO.
getUserSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit);
int subscriptionCount = isUnsubscribe ? subscriptionDAO.getUserUnsubscriptionCount(applicationReleaseDTO.getId(), tenantId) :
subscriptionDAO.getUserSubscriptionCount(applicationReleaseDTO.getId(), tenantId);
return new SubscriptionResponse(subscriptionInfo.getApplicationUUID(), subscriptionCount, subscriptionEntities);
} catch (DBConnectionException | ApplicationManagementDAOException e) {
String msg = "Error encountered while connecting to the database";
log.error(msg, e);
@ -155,7 +159,41 @@ public class UserBasedSubscriptionManagementHelperServiceImpl implements Subscri
}
@Override
public void getSubscriptionStatistics() throws ApplicationManagementException {
// todo: analytics engine
public SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo) throws ApplicationManagementException {
final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus());
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
ConnectionManagerUtil.openDBConnection();
List<Integer> deviceIdsOwnByUser = getDeviceIdsOwnByUser(subscriptionInfo.getIdentifier());
SubscriptionStatisticDTO subscriptionStatisticDTO = subscriptionDAO.
getSubscriptionStatistic(deviceIdsOwnByUser, subscriptionInfo.getSubscriptionType(), isUnsubscribe, tenantId);
int allDeviceCount = deviceIdsOwnByUser.size();
return SubscriptionManagementHelperUtil.getSubscriptionStatistics(subscriptionStatisticDTO, allDeviceCount);
} catch (DeviceManagementException | ApplicationManagementDAOException e) {
String msg = "Error encountered while getting subscription statistics for user: " + subscriptionInfo.getIdentifier();
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
}
}
@SuppressWarnings("unchecked")
private List<Integer> getDeviceIdsOwnByUser(String username) throws DeviceManagementException {
List<Device> deviceListOwnByUser = new ArrayList<>();
PaginationRequest paginationRequest = new PaginationRequest(-1, -1);
paginationRequest.setOwner(username);
paginationRequest.setStatusList(Arrays.asList("ACTIVE", "INACTIVE", "UNREACHABLE"));
PaginationResult ownDeviceIds = HelperUtil.getDeviceManagementProviderService().
getAllDevicesIdList(paginationRequest);
if (ownDeviceIds.getData() != null) {
deviceListOwnByUser.addAll((List<Device>) ownDeviceIds.getData());
}
return deviceListOwnByUser.stream().map(Device::getId).collect(Collectors.toList());
}
private static class UserBasedSubscriptionManagementHelperServiceImplHolder {
private static final UserBasedSubscriptionManagementHelperServiceImpl INSTANCE
= new UserBasedSubscriptionManagementHelperServiceImpl();
}
}

@ -19,27 +19,26 @@
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;
import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionResponse;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionStatistics;
import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException;
import io.entgra.device.mgt.core.application.mgt.core.dao.ApplicationDAO;
import io.entgra.device.mgt.core.application.mgt.core.dao.ApplicationReleaseDAO;
import io.entgra.device.mgt.core.application.mgt.core.dao.SubscriptionDAO;
import io.entgra.device.mgt.core.application.mgt.core.dao.common.ApplicationManagementDAOFactory;
import java.util.List;
public interface SubscriptionManagementHelperService {
SubscriptionDAO subscriptionDAO = ApplicationManagementDAOFactory.getSubscriptionDAO();
ApplicationDAO applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO();
ApplicationReleaseDAO applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO();
List<DeviceSubscription> getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
SubscriptionResponse getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException;
List<SubscriptionEntity> getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
SubscriptionResponse getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException;
SubscriptionStatistics getSubscriptionStatistics(SubscriptionInfo subscriptionInfo)
throws ApplicationManagementException;
void getSubscriptionStatistics() throws ApplicationManagementException;
}

@ -868,6 +868,12 @@ public interface DeviceDAO {
List<Integer> getDevicesNotInGivenIdList(PaginationRequest request, List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException;
List<Integer> getDevicesInGivenIdList(PaginationRequest request, List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException;
int getDeviceCountNotInGivenIdList(List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException;
List<Device> getDevicesByDeviceIds(PaginationRequest paginationRequest, List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException;
}

@ -488,4 +488,5 @@ public interface GroupDAO {
int tenantId, String deviceOwner, String deviceName, String deviceStatus, int offset, int limit)
throws GroupManagementDAOException;
int getDeviceCount(String groupName, int tenantId) throws GroupManagementDAOException;
}

@ -3308,10 +3308,11 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
try {
Connection connection = getConnection();
String sql = "SELECT ID AS DEVICE_ID FROM DM_DEVICE WHERE ID NOT IN " +
"(" + deviceIdStringList + ") LIMIT ? OFFSET ?";
"(" + deviceIdStringList + ") AND TENANT_ID = ? LIMIT ? OFFSET ?";
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setInt(1, request.getRowCount());
preparedStatement.setInt(2, request.getStartIndex());
preparedStatement.setInt(1, tenantId);
preparedStatement.setInt(2, request.getRowCount());
preparedStatement.setInt(3, request.getStartIndex());
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
filteredDeviceIds.add(resultSet.getInt("DEVICE_ID"));
@ -3326,6 +3327,58 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
}
}
@Override
public List<Integer> getDevicesInGivenIdList(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 IN " +
"(" + deviceIdStringList + ") AND TENANT_ID = ? LIMIT ? OFFSET ?";
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setInt(1, tenantId);
preparedStatement.setInt(2, request.getRowCount());
preparedStatement.setInt(3, 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 in: " + filteredDeviceIds;
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
@Override
public int getDeviceCountNotInGivenIdList(List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException {
int deviceCount = 0;
String deviceIdStringList = deviceIds.stream().map(String::valueOf).collect(Collectors.joining(","));
try {
Connection connection = getConnection();
String sql = "SELECT COUNT(ID) AS COUNT FROM DM_DEVICE WHERE ID NOT IN " +
"(" + deviceIdStringList + ") AND TENANT_ID = ?";
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setInt(1, tenantId);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
deviceCount = resultSet.getInt("COUNT");
}
}
return deviceCount;
}
} catch (SQLException e) {
String msg = "Error occurred while retrieving device count";
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
}
@Override
public List<Device> getDevicesByDeviceIds(PaginationRequest paginationRequest, List<Integer> deviceIds, int tenantId)
throws DeviceManagementDAOException {
@ -3352,7 +3405,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
}
if (paginationRequest.getDeviceName() != null) {
sql = sql + " AND d.DEVICE_NAME = ?";
sql = sql + " AND d.NAME = ?";
isDeviceNameProvided = true;
}

@ -1556,4 +1556,29 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
throw new GroupManagementDAOException(msg, e);
}
}
@Override
public int getDeviceCount(String groupName, int tenantId) throws GroupManagementDAOException {
int deviceCount = 0;
try {
Connection connection = GroupManagementDAOFactory.getConnection();
String sql = "SELECT COUNT(d.ID) AS COUNT FROM DM_GROUP d INNER JOIN " +
"DM_DEVICE_GROUP_MAP m ON " +
"d.ID = m.GROUP_ID WHERE d.TENANT_ID = ? AND d.GROUP_NAME = ?";
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setInt(1, tenantId);
preparedStatement.setString(2, groupName);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (resultSet.next()) {
deviceCount = resultSet.getInt("COUNT");
}
}
}
return deviceCount;
} catch (SQLException e) {
String msg = "Error occurred while retrieving device count for the group: " + groupName;
log.error(msg, e);
throw new GroupManagementDAOException(msg, e);
}
}
}

@ -1156,6 +1156,10 @@ public interface DeviceManagementProviderService {
List<Integer> getDevicesNotInGivenIdList(List<Integer> deviceIds, PaginationRequest paginationRequest)
throws DeviceManagementException;
List<Integer> getDevicesInGivenIdList(List<Integer> deviceIds, PaginationRequest paginationRequest)
throws DeviceManagementException;
int getDeviceCountNotInGivenIdList(List<Integer> deviceIds) throws DeviceManagementException;
List<Device> getDevicesByDeviceIds(PaginationRequest paginationRequest, List<Integer> deviceIds)
throws DeviceManagementException;
}

@ -5620,6 +5620,54 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
}
}
@Override
public List<Integer> getDevicesInGivenIdList(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.getDevicesInGivenIdList(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 int getDeviceCountNotInGivenIdList(List<Integer> deviceIds)
throws DeviceManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
DeviceManagementDAOFactory.openConnection();
return deviceDAO.getDeviceCountNotInGivenIdList(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 {

@ -30,6 +30,7 @@ import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupAlreadyExistEx
import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupManagementException;
import io.entgra.device.mgt.core.device.mgt.common.group.mgt.GroupNotExistException;
import io.entgra.device.mgt.core.device.mgt.common.group.mgt.RoleDoesNotExistException;
import io.entgra.device.mgt.core.device.mgt.core.dao.GroupManagementDAOException;
import io.entgra.device.mgt.core.device.mgt.core.dto.GroupDetailsDTO;
import org.wso2.carbon.user.api.AuthorizationManager;
import org.wso2.carbon.user.api.UserStoreManager;
@ -389,4 +390,6 @@ public interface GroupManagementProviderService {
GroupDetailsDTO getGroupDetailsWithDevices(String groupName, int deviceTypeId, String deviceOwner, String deviceName, String deviceStatus,
int offset, int limit) throws GroupManagementException;
int getDeviceCount(String groupName) throws GroupManagementException;
}

@ -1725,4 +1725,18 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
return groupDetailsWithDevices;
}
@Override
public int getDeviceCount(String groupName) throws GroupManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
GroupManagementDAOFactory.openConnection();
return groupDAO.getDeviceCount(groupName, tenantId);
} catch (SQLException | GroupManagementDAOException e) {
String msg = "Error occurred while retrieving device count.";
log.error(msg, e);
throw new GroupManagementException(msg, e);
} finally {
GroupManagementDAOFactory.closeConnection();
}
}
}

Loading…
Cancel
Save