diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/DeviceSubscription.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/DeviceSubscription.java index 98c76c192c..f3209c53b6 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/DeviceSubscription.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/DeviceSubscription.java @@ -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; } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java index c77267d26c..c68eb96e1d 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/SubscriptionDAO.java @@ -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 getUserSubscriptionsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, + List getUserSubscriptionsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, int offset, int limit) throws ApplicationManagementDAOException; /** diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index 45d4cff175..d645cf7ab1 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -1694,14 +1694,14 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } @Override - public List getUserSubscriptionsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, + public List 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 userSubscriptions = new ArrayList<>(); + List 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()) { diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java index 93a5d6f066..d20294535c 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementHelperUtil.java @@ -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()); diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementServiceProvider.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementServiceProvider.java index 671f4fdeba..2769278735 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementServiceProvider.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/SubscriptionManagementServiceProvider.java @@ -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"); } } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java index fad338b06f..30d91338c3 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/DeviceBasedSubscriptionManagementHelperServiceImpl.java @@ -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 getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { - return null; + public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException { + final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); + List 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 deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). + map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); + + List 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 getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { - return null; + public List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException { + return Collections.emptyList(); } @Override public void getSubscriptionStatistics() throws ApplicationManagementException { - + // todo: analytics engine } } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java index 6454fa8807..9e05b6fc2e 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/GroupBasedSubscriptionManagementHelperServiceImpl.java @@ -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 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 deviceIdsOwnByGroup = groupDetailsDTO.getDeviceIds(); + GroupDetailsDTO groupDetailsDTO; + List 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 deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). + map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); + + List 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 } } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java index 5bf4ffcb20..72796a9cd4 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/RoleBasedSubscriptionManagementHelperServiceImpl.java @@ -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 deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); @@ -116,6 +115,11 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri List 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 } } diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java index 0180b9953d..437bab449f 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/util/subscription/mgt/impl/UserBasedSubscriptionManagementHelperServiceImpl.java @@ -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 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 userSubscriptionsWithDevices = new ArrayList<>(); -// -// List 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 pendingDevices = new ArrayList<>(); -// List installedDevices = new ArrayList<>(); -// List errorDevices = new ArrayList<>(); -// List newDevices = new ArrayList<>(); -// List subscribedDevices = new ArrayList<>(); -// -// List deviceIds = ownerDetailsWithDevices.getDeviceIds(); -// Map statusCounts = new HashMap<>(); -// statusCounts.put("PENDING", 0); -// statusCounts.put("COMPLETED", 0); -// statusCounts.put("ERROR", 0); -// statusCounts.put("NEW", 0); -// statusCounts.put("SUBSCRIBED", 0); -// -// List 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 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 statusPercentages = new HashMap<>(); -// for (Map.Entry entry : statusCounts.entrySet()) { -// double percentage = ((double) entry.getValue() / totalDevices) * 100; -// String formattedPercentage = String.format("%.2f", percentage); -// statusPercentages.put(entry.getKey(), Double.valueOf(formattedPercentage)); -// } -// -// List 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 getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { - return null; + final boolean isUnsubscribe = Objects.equals("unsubscribe", subscriptionInfo.getSubscriptionStatus()); + List deviceSubscriptionDTOS; + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + + try { + ConnectionManagerUtil.openDBConnection(); + List 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)ownDeviceIds.getData()); + } + + List 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 deviceIdsOfSubscription = deviceSubscriptionDTOS.stream(). + map(DeviceSubscriptionDTO::getDeviceId).collect(Collectors.toList()); + + List 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 getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { - return null; + public List 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 } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 2034bcf33c..7f77d3d3e2 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -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); } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java index 1eba6c1a7e..416b6e6efd 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/AbstractGroupDAOImpl.java @@ -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()) {