Improvements to subscription helper services #459

Merged
tcdlpds merged 2 commits from rajitha/device-mgt-core:local-appm-imp into APPM_Imp 4 months ago

@ -19,14 +19,26 @@
package io.entgra.device.mgt.core.application.mgt.common;
import java.sql.Timestamp;
public class DeviceSubscription {
private String deviceName;
private String deviceIdentifier;
private String deviceStatus;
private String deviceOwner;
private String deviceType;
private String ownershipType;
private Timestamp dateOfLastUpdate;
private SubscriptionData subscriptionData;
public String getDeviceName() {
return deviceName;
}
public void setDeviceName(String deviceName) {
this.deviceName = deviceName;
}
public String getDeviceIdentifier() {
return deviceIdentifier;
}
@ -67,6 +79,14 @@ public class DeviceSubscription {
this.ownershipType = ownershipType;
}
public Timestamp getDateOfLastUpdate() {
return dateOfLastUpdate;
}
public void setDateOfLastUpdate(Timestamp dateOfLastUpdate) {
this.dateOfLastUpdate = dateOfLastUpdate;
}
public SubscriptionData getSubscriptionData() {
return subscriptionData;
}

@ -342,7 +342,7 @@ public interface SubscriptionDAO {
* @return {@link SubscriptionsDTO} which contains the details of subscriptions.
* @throws ApplicationManagementDAOException if connection establishment or SQL execution fails.
*/
List<SubscriptionsDTO> getUserSubscriptionsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId,
List<SubscriptionEntity> getUserSubscriptionsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId,
int offset, int limit) throws ApplicationManagementDAOException;
/**

@ -1694,14 +1694,14 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
}
@Override
public List<SubscriptionsDTO> getUserSubscriptionsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId,
public List<SubscriptionEntity> getUserSubscriptionsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId,
int offset, int limit) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to get user subscriptions related to the given UUID.");
}
try {
Connection conn = this.getDBConnection();
List<SubscriptionsDTO> userSubscriptions = new ArrayList<>();
List<SubscriptionEntity> subscriptionEntities = new ArrayList<>();
String subscriptionStatusTime = unsubscribe ? "US.UNSUBSCRIBED_TIMESTAMP" : "US.SUBSCRIBED_TIMESTAMP";
String sql = "SELECT US.USER_NAME, US.SUBSCRIBED_BY, US.SUBSCRIBED_TIMESTAMP, US.UNSUBSCRIBED, " +
@ -1717,21 +1717,21 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
ps.setInt(4, limit);
ps.setInt(5, offset);
try (ResultSet rs = ps.executeQuery()) {
SubscriptionEntity subscriptionEntity;
while (rs.next()) {
SubscriptionsDTO userSubscription;
userSubscription = new SubscriptionsDTO();
userSubscription.setName(rs.getString("USER_NAME"));
userSubscription.setSubscribedBy(rs.getString("SUBSCRIBED_BY"));
userSubscription.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_TIMESTAMP"));
userSubscription.setUnsubscribed(rs.getBoolean("UNSUBSCRIBED"));
userSubscription.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY"));
userSubscription.setUnsubscribedTimestamp(rs.getTimestamp("UNSUBSCRIBED_TIMESTAMP"));
userSubscription.setAppReleaseId(rs.getInt("AP_APP_RELEASE_ID"));
userSubscriptions.add(userSubscription);
subscriptionEntity = new SubscriptionEntity();
subscriptionEntity.setIdentity(rs.getString("USER_NAME"));
subscriptionEntity.setSubscribedBy(rs.getString("SUBSCRIBED_BY"));
subscriptionEntity.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_TIMESTAMP"));
subscriptionEntity.setUnsubscribed(rs.getBoolean("UNSUBSCRIBED"));
subscriptionEntity.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY"));
subscriptionEntity.setUnsubscribedTimestamp(rs.getTimestamp("UNSUBSCRIBED_TIMESTAMP"));
subscriptionEntity.setApplicationReleaseId(rs.getInt("AP_APP_RELEASE_ID"));
subscriptionEntities.add(subscriptionEntity);
}
}
return userSubscriptions;
return subscriptionEntities;
}
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the DB connection to get user subscriptions for the given UUID.";
@ -1948,7 +1948,11 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
}
sql.append("ORDER BY ").append(subscriptionStatusTime).
append(" DESC ").append("LIMIT ? OFFSET ?");
append(" DESC ");
if (offset >= 0 && limit >= 0) {
sql.append("LIMIT ? OFFSET ?");
}
try (PreparedStatement ps = conn.prepareStatement(sql.toString())) {
int paramIdx = 1;
@ -1969,8 +1973,10 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
ps.setString(paramIdx++, "%" + actionTriggeredBy + "%");
}
ps.setInt(paramIdx++, limit);
ps.setInt(paramIdx++, offset);
if (offset >= 0 && limit >= 0) {
ps.setInt(paramIdx++, limit);
ps.setInt(paramIdx, offset);
}
try (ResultSet rs = ps.executeQuery()) {
if (log.isDebugEnabled()) {
@ -2134,8 +2140,11 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
sql.append(" AND ").append(actionTriggeredColumn).append(" LIKE ? ");
}
sql.append("ORDER BY ").append(subscriptionStatusTime).append(" DESC ")
.append("LIMIT ? OFFSET ?");
sql.append("ORDER BY ").append(subscriptionStatusTime).append(" DESC ");
if (limit >= 0 && offset >= 0) {
sql.append("LIMIT ? OFFSET ?");
}
try {
Connection conn = this.getDBConnection();
@ -2155,8 +2164,10 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
ps.setString(paramIdx++, "%" + actionTriggeredBy + "%");
}
ps.setInt(paramIdx++, limit);
ps.setInt(paramIdx++, offset);
if (limit >= 0 && offset >= 0) {
ps.setInt(paramIdx++, limit);
ps.setInt(paramIdx, offset);
}
try (ResultSet rs = ps.executeQuery()) {
if (log.isDebugEnabled()) {

@ -20,7 +20,6 @@
package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt;
import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscription;
import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData;
import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionFilterCriteria;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionData;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo;
@ -30,6 +29,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.exceptions.DeviceManagementException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@ -56,6 +56,10 @@ public class SubscriptionManagementHelperUtil {
deviceSubscription.setDeviceIdentifier(device.getDeviceIdentifier());
deviceSubscription.setDeviceOwner(device.getEnrolmentInfo().getOwner());
deviceSubscription.setDeviceType(device.getType());
deviceSubscription.setDeviceName(device.getName());
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());

@ -20,8 +20,10 @@
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.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;
import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl.UserBasedSubscriptionManagementHelperServiceImpl;
import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service.SubscriptionManagementHelperService;
import java.util.Objects;
@ -44,6 +46,8 @@ 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();
throw new UnsupportedOperationException("Subscription type: " + subscriptionType + " not supports");
}
}

@ -21,28 +21,111 @@ package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.imp
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.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 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;
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();
}
public static DeviceBasedSubscriptionManagementHelperServiceImpl getInstance() {
return DeviceBasedSubscriptionManagementHelperServiceImpl.DeviceBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE;
}
@Override
public List<DeviceSubscription> getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException {
return null;
public List<DeviceSubscription> getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException {
final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus());
List<DeviceSubscriptionDTO> deviceSubscriptionDTOS;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
ConnectionManagerUtil.openDBConnection();
ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO.
getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId);
if (applicationReleaseDTO == null) {
String msg = "Couldn't find an application release for application release UUID: " +
subscriptionInfo.getApplicationUUID();
log.error(msg);
throw new NotFoundException(msg);
}
String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo);
DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria();
if (Objects.equals("NEW", deviceSubscriptionStatus)) {
deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO.
getId(),isUnsubscribe, tenantId, 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());
} else {
deviceSubscriptionDTOS = subscriptionDAO.getAllSubscriptionsDetails(applicationReleaseDTO.
getId(),isUnsubscribe, tenantId, null, subscriptionInfo.getSubscriptionType(),
deviceSubscriptionFilterCriteria.getTriggeredBy(), limit, offset);
}
return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria());
} catch (DeviceManagementException e) {
String msg = "Error encountered while getting device details";
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (ApplicationManagementDAOException | DBConnectionException e) {
String msg = "Error encountered while connecting to the database";
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
}
}
@Override
public List<SubscriptionEntity> getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException {
return null;
public List<SubscriptionEntity> getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException {
return Collections.emptyList();
}
@Override
public void getSubscriptionStatistics() throws ApplicationManagementException {
// todo: analytics engine
}
}

@ -35,6 +35,7 @@ import io.entgra.device.mgt.core.application.mgt.core.util.ConnectionManagerUtil
import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil;
import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.SubscriptionManagementHelperUtil;
import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service.SubscriptionManagementHelperService;
import io.entgra.device.mgt.core.device.mgt.common.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;
@ -45,6 +46,7 @@ import org.wso2.carbon.context.PrivilegedCarbonContext;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
public class GroupBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService {
private static final Log log = LogFactory.getLog(GroupBasedSubscriptionManagementHelperServiceImpl.class);
@ -65,7 +67,7 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr
final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus());
List<DeviceSubscriptionDTO> deviceSubscriptionDTOS;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
// todo: check and refactor
try {
ConnectionManagerUtil.openDBConnection();
ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO.
@ -89,28 +91,40 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr
DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria();
GroupManagementProviderService groupManagementProviderService = HelperUtil.getGroupManagementProviderService();
GroupDetailsDTO groupDetailsDTO = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(),
applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(),
deviceSubscriptionFilterCriteria.getDeviceStatus(), offset, limit);
List<Integer> deviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds();
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();
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByGroup, null,
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(),
null, -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());
} else {
groupDetailsDTO = groupManagementProviderService.getGroupDetailsWithDevices(subscriptionInfo.getIdentifier(),
applicationDTO.getDeviceTypeId(), deviceSubscriptionFilterCriteria.getOwner(), deviceSubscriptionFilterCriteria.getName(),
deviceSubscriptionFilterCriteria.getDeviceStatus(), offset, limit);
deviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds();
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByGroup, null,
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(),
null, limit, offset);
for (DeviceSubscriptionDTO deviceSubscriptionDTO: deviceSubscriptionDTOS) {
deviceIdsOwnByGroup.remove(deviceSubscriptionDTO.getDeviceId());
}
}
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByGroup, deviceSubscriptionStatus, subscriptionInfo.getSubscriptionType(),
deviceSubscriptionFilterCriteria.getTriggeredBy(), deviceSubscriptionStatus, limit, offset);
return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria());
return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria());
} catch (GroupManagementException e) {
String msg = "Error encountered while retrieving group details for group: " + subscriptionInfo.getIdentifier();
@ -156,6 +170,6 @@ public class GroupBasedSubscriptionManagementHelperServiceImpl implements Subscr
@Override
public void getSubscriptionStatistics() throws ApplicationManagementException {
// todo: analytics engine
}
}

@ -103,12 +103,11 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri
String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo);
DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria();
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByRole, null,
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(),
null, limit, offset);
if (Objects.equals("NEW", deviceSubscriptionStatus)) {
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByRole, null,
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(),
null, -1, -1);
List<Integer> deviceIdsOfSubscription = deviceSubscriptionDTOS.stream().
map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList());
@ -116,6 +115,11 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri
List<Integer> newDeviceIds = HelperUtil.getDeviceManagementProviderService().
getDevicesNotInGivenIdList(deviceIdsOfSubscription, new PaginationRequest(offset, limit));
deviceSubscriptionDTOS = newDeviceIds.stream().map(DeviceSubscriptionDTO::new).collect(Collectors.toList());
} else {
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByRole, null,
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(),
null, limit, offset);
}
return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS,
@ -166,6 +170,6 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri
@Override
public void getSubscriptionStatistics() throws ApplicationManagementException {
// todo: analytics engine
}
}

@ -20,254 +20,142 @@
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.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.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 org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/*
// @Override
// public List<SubscriptionsDTO> getUserSubscriptionsByUUID(String uuid, String subscriptionStatus,
// PaginationRequest request, int offset, int limit)
// throws ApplicationManagementException {
// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
// boolean unsubscribe = subscriptionStatus.equals("unsubscribed");
// String status;
//
// try {
// ConnectionManagerUtil.openDBConnection();
//
// ApplicationReleaseDTO applicationReleaseDTO = this.applicationReleaseDAO.getReleaseByUUID(uuid, tenantId);
// if (applicationReleaseDTO == null) {
// String msg = "Couldn't find an application release for application release UUID: " + uuid;
// log.error(msg);
// throw new NotFoundException(msg);
// }
// ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(uuid, tenantId);
// int appReleaseId = applicationReleaseDTO.getId();
// List<SubscriptionsDTO> userSubscriptionsWithDevices = new ArrayList<>();
//
// List<SubscriptionsDTO> userSubscriptions =
// subscriptionDAO.getUserSubscriptionsByAppReleaseID(appReleaseId, unsubscribe, tenantId, offset, limit);
// if (userSubscriptions == null) {
// throw new ApplicationManagementException("User details not found for appReleaseId: " + appReleaseId);
// }
//
// DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService();
//
// for (SubscriptionsDTO userSubscription : userSubscriptions) {
//
// if (StringUtils.isNotBlank(request.getUserName()) && !request.getUserName().equals(userSubscription.getName())) {
// continue;
// }
//
// String userName = StringUtils.isNotBlank(request.getUserName()) ? request.getUserName() : userSubscription.getName();
//
// // Retrieve owner details and device IDs for the user using the service layer
// OwnerWithDeviceDTO ownerDetailsWithDevices =
// deviceManagementProviderService.getOwnersWithDeviceIds(userName, applicationDTO.getDeviceTypeId(),
// request.getOwner(), request.getDeviceName(), request.getDeviceStatus());
//
// SubscriptionsDTO userSubscriptionDTO = new SubscriptionsDTO();
// userSubscriptionDTO.setName(userSubscription.getName());
// userSubscriptionDTO.setSubscribedBy(userSubscription.getSubscribedBy());
// userSubscriptionDTO.setSubscribedTimestamp(userSubscription.getSubscribedTimestamp());
// userSubscriptionDTO.setUnsubscribed(userSubscription.getUnsubscribed());
// userSubscriptionDTO.setUnsubscribedBy(userSubscription.getUnsubscribedBy());
// userSubscriptionDTO.setUnsubscribedTimestamp(userSubscription.getUnsubscribedTimestamp());
// userSubscriptionDTO.setAppReleaseId(userSubscription.getAppReleaseId());
//
// userSubscriptionDTO.setDeviceCount(ownerDetailsWithDevices.getDeviceCount());
//
// // Fetch device subscriptions for each device ID associated with the user
// List<DeviceSubscriptionData> pendingDevices = new ArrayList<>();
// List<DeviceSubscriptionData> installedDevices = new ArrayList<>();
// List<DeviceSubscriptionData> errorDevices = new ArrayList<>();
// List<DeviceSubscriptionData> newDevices = new ArrayList<>();
// List<DeviceSubscriptionData> subscribedDevices = new ArrayList<>();
//
// List<Integer> deviceIds = ownerDetailsWithDevices.getDeviceIds();
// Map<String, Integer> statusCounts = new HashMap<>();
// statusCounts.put("PENDING", 0);
// statusCounts.put("COMPLETED", 0);
// statusCounts.put("ERROR", 0);
// statusCounts.put("NEW", 0);
// statusCounts.put("SUBSCRIBED", 0);
//
// List<DeviceSubscriptionDTO> subscribedDeviceSubscriptions = new ArrayList<>();
// if (unsubscribe) {
// subscribedDeviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds(
// appReleaseId, !unsubscribe, tenantId, deviceIds, request.getActionStatus(), request.getActionType(),
// request.getActionTriggeredBy(), request.getTabActionStatus());
// }
//
// for (Integer deviceId : deviceIds) {
// List<DeviceSubscriptionDTO> deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds(
// userSubscription.getAppReleaseId(), unsubscribe, tenantId, deviceIds, request.getActionStatus(), request.getActionType(),
// request.getActionTriggeredBy(), request.getTabActionStatus());
// OwnerWithDeviceDTO ownerWithDeviceByDeviceId =
// deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId, request.getOwner(), request.getDeviceName(),
// request.getDeviceStatus());
// if (ownerWithDeviceByDeviceId == null) {
// continue;
// }
// boolean isNewDevice = true;
// for (DeviceSubscriptionDTO subscription : deviceSubscriptions) {
// if (subscription.getDeviceId() == deviceId) {
// DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData();
// deviceDetail.setDeviceId(subscription.getDeviceId());
// deviceDetail.setSubId(subscription.getId());
// deviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName());
// deviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus());
// deviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames());
// deviceDetail.setActionType(subscription.getActionTriggeredFrom());
// deviceDetail.setStatus(subscription.getStatus());
// deviceDetail.setActionType(subscription.getActionTriggeredFrom());
// deviceDetail.setActionTriggeredBy(subscription.getSubscribedBy());
// deviceDetail.setActionTriggeredTimestamp(subscription.getSubscribedTimestamp());
// deviceDetail.setUnsubscribed(subscription.isUnsubscribed());
// deviceDetail.setUnsubscribedBy(subscription.getUnsubscribedBy());
// deviceDetail.setUnsubscribedTimestamp(subscription.getUnsubscribedTimestamp());
// deviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes());
// deviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers());
//
// status = subscription.getStatus();
// switch (status) {
// case "COMPLETED":
// installedDevices.add(deviceDetail);
// statusCounts.put("COMPLETED", statusCounts.get("COMPLETED") + 1);
// break;
// case "ERROR":
// case "INVALID":
// case "UNAUTHORIZED":
// errorDevices.add(deviceDetail);
// statusCounts.put("ERROR", statusCounts.get("ERROR") + 1);
// break;
// case "IN_PROGRESS":
// case "PENDING":
// case "REPEATED":
// pendingDevices.add(deviceDetail);
// statusCounts.put("PENDING", statusCounts.get("PENDING") + 1);
// break;
// }
// isNewDevice = false;
// }
// }
// if (isNewDevice) {
// boolean isSubscribedDevice = false;
// for (DeviceSubscriptionDTO subscribedDevice : subscribedDeviceSubscriptions) {
// if (subscribedDevice.getDeviceId() == deviceId) {
// DeviceSubscriptionData subscribedDeviceDetail = new DeviceSubscriptionData();
// subscribedDeviceDetail.setDeviceId(subscribedDevice.getDeviceId());
// subscribedDeviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames());
// subscribedDeviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName());
// subscribedDeviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus());
// subscribedDeviceDetail.setSubId(subscribedDevice.getId());
// subscribedDeviceDetail.setActionTriggeredBy(subscribedDevice.getSubscribedBy());
// subscribedDeviceDetail.setActionTriggeredTimestamp(subscribedDevice.getSubscribedTimestamp());
// subscribedDeviceDetail.setActionType(subscribedDevice.getActionTriggeredFrom());
// subscribedDeviceDetail.setStatus(subscribedDevice.getStatus());
// subscribedDeviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes());
// subscribedDeviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers());
// subscribedDevices.add(subscribedDeviceDetail);
// statusCounts.put("SUBSCRIBED", statusCounts.get("SUBSCRIBED") + 1);
// isSubscribedDevice = true;
// break;
// }
// }
// if (!isSubscribedDevice) {
// DeviceSubscriptionData newDeviceDetail = new DeviceSubscriptionData();
// newDeviceDetail.setDeviceId(deviceId);
// newDeviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName());
// newDeviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus());
// newDeviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames());
// newDeviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes());
// newDeviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers());
// newDevices.add(newDeviceDetail);
// statusCounts.put("NEW", statusCounts.get("NEW") + 1);
// }
// }
// }
//
// int totalDevices = deviceIds.size();
// Map<String, Double> statusPercentages = new HashMap<>();
// for (Map.Entry<String, Integer> entry : statusCounts.entrySet()) {
// double percentage = ((double) entry.getValue() / totalDevices) * 100;
// String formattedPercentage = String.format("%.2f", percentage);
// statusPercentages.put(entry.getKey(), Double.valueOf(formattedPercentage));
// }
//
// List<DeviceSubscriptionData> requestedDevices = new ArrayList<>();
// if (StringUtils.isNotBlank(request.getTabActionStatus())) {
// switch (request.getTabActionStatus()) {
// case "COMPLETED":
// requestedDevices = installedDevices;
// break;
// case "PENDING":
// requestedDevices = pendingDevices;
// break;
// case "ERROR":
// requestedDevices = errorDevices;
// break;
// case "NEW":
// requestedDevices = newDevices;
// break;
// case "SUBSCRIBED":
// requestedDevices = subscribedDevices;
// break;
// }
// userSubscriptionDTO.setDevices(new CategorizedSubscriptionResult(requestedDevices, request.getTabActionStatus()));
// } else {
// CategorizedSubscriptionResult categorizedSubscriptionResult;
// if (subscribedDevices.isEmpty()) {
// categorizedSubscriptionResult =
// new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices);
// } else {
// categorizedSubscriptionResult =
// new CategorizedSubscriptionResult(installedDevices, pendingDevices, errorDevices, newDevices,
// subscribedDevices);
// }
// userSubscriptionDTO.setDevices(categorizedSubscriptionResult);
// userSubscriptionDTO.setStatusPercentages(statusPercentages);
//
// }
// userSubscriptionsWithDevices.add(userSubscriptionDTO);
// }
// return userSubscriptionsWithDevices;
// } catch (ApplicationManagementDAOException e) {
// String msg = "Error occurred while getting user subscriptions for the application release UUID: " + uuid;
// log.error(msg, e);
// throw new ApplicationManagementException(msg, e);
// } catch (DBConnectionException e) {
// String msg = "DB Connection error occurred while getting user subscriptions for UUID: " + uuid;
// log.error(msg, e);
// throw new ApplicationManagementException(msg, e);
// } catch (DeviceManagementDAOException e) {
// throw new RuntimeException(e);
// } finally {
// ConnectionManagerUtil.closeDBConnection();
// }
// }
*/
public class UserBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService {
private static final Log log = LogFactory.getLog(UserBasedSubscriptionManagementHelperServiceImpl.class);
private UserBasedSubscriptionManagementHelperServiceImpl() {}
private static class UserBasedSubscriptionManagementHelperServiceImplHolder {
private static final UserBasedSubscriptionManagementHelperServiceImpl INSTANCE
= new UserBasedSubscriptionManagementHelperServiceImpl();
}
public static UserBasedSubscriptionManagementHelperServiceImpl getInstance() {
return UserBasedSubscriptionManagementHelperServiceImpl.UserBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE;
}
@SuppressWarnings("unchecked")
@Override
public List<DeviceSubscription> getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException {
return null;
final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus());
List<DeviceSubscriptionDTO> deviceSubscriptionDTOS;
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());
ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO.
getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId);
if (applicationReleaseDTO == null) {
String msg = "Couldn't find an application release for application release UUID: " +
subscriptionInfo.getApplicationUUID();
log.error(msg);
throw new NotFoundException(msg);
}
String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo);
DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria();
if (Objects.equals("NEW", deviceSubscriptionStatus)) {
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByUser, null,
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(),
null, -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());
} else {
deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(),
isUnsubscribe, tenantId, deviceIdsOwnByUser, null,
subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(),
null, limit, offset);
}
return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS,
subscriptionInfo.getDeviceSubscriptionFilterCriteria());
} catch (DeviceManagementException e) {
String msg = "Error encountered while getting device details";
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (ApplicationManagementDAOException | DBConnectionException e) {
String msg = "Error encountered while connecting to the database";
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
}
}
@Override
public List<SubscriptionEntity> getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException {
return null;
public List<SubscriptionEntity> getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset)
throws ApplicationManagementException {
final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus());
final int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
ConnectionManagerUtil.openDBConnection();
ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO.
getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId);
if (applicationReleaseDTO == null) {
String msg = "Couldn't find an application release for application release UUID: " +
subscriptionInfo.getApplicationUUID();
log.error(msg);
throw new NotFoundException(msg);
}
return subscriptionDAO.
getUserSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit);
} catch (DBConnectionException | ApplicationManagementDAOException e) {
String msg = "Error encountered while connecting to the database";
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} finally {
ConnectionManagerUtil.closeDBConnection();
}
}
@Override
public void getSubscriptionStatistics() throws ApplicationManagementException {
// todo: analytics engine
}
}

@ -3337,7 +3337,7 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
try {
Connection connection = getConnection();
String sql = "SELECT e.DEVICE_ID, d.DEVICE_IDENTIFICATION, e.STATUS, e.OWNER, d.NAME AS DEVICE_NAME, " +
"e.DEVICE_TYPE FROM DM_DEVICE d INNER JOIN DM_ENROLMENT e " +
"e.DEVICE_TYPE, e.OWNERSHIP, e.DATE_OF_LAST_UPDATE FROM DM_DEVICE d INNER JOIN DM_ENROLMENT e " +
"WHERE d.ID = e.DEVICE_ID AND d.TENANT_ID = ? AND e.DEVICE_ID IN (" + deviceIdStringList+ ") " +
"AND e.STATUS NOT IN ('DELETED', 'REMOVED')";
@ -3379,6 +3379,8 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO {
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
enrolmentInfo.setStatus(EnrolmentInfo.Status.valueOf(resultSet.getString("STATUS")));
enrolmentInfo.setOwner(resultSet.getString("OWNER"));
enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.valueOf(resultSet.getString("OWNERSHIP")));
enrolmentInfo.setDateOfLastUpdate(resultSet.getTimestamp("DATE_OF_LAST_UPDATE").getTime());
device.setEnrolmentInfo(enrolmentInfo);
devices.add(device);
}

@ -1494,8 +1494,9 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
if (deviceStatus != null && !deviceStatus.isEmpty()) {
sql.append(" AND e.STATUS = ?");
}
sql.append(" LIMIT ? OFFSET ?");
if (limit >= 0 && offset >=0 ) {
sql.append(" LIMIT ? OFFSET ?");
}
Connection conn = null;
try {
@ -1519,8 +1520,10 @@ public abstract class AbstractGroupDAOImpl implements GroupDAO {
stmt.setString(index++, deviceStatus);
}
stmt.setInt(index++, limit);
stmt.setInt(index++, offset);
if (limit >= 0 && offset >=0 ) {
stmt.setInt(index++, limit);
stmt.setInt(index++, offset);
}
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {

Loading…
Cancel
Save