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/DeviceSubscriptionFilterCriteria.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/DeviceSubscriptionFilterCriteria.java new file mode 100644 index 0000000000..298a5221eb --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/DeviceSubscriptionFilterCriteria.java @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.common; + +public class DeviceSubscriptionFilterCriteria { + private String name; + private String owner; + private String deviceStatus; + private String filteringDeviceSubscriptionStatus; // COMPLETE, PENDING ... + private String triggeredBy; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getOwner() { + return owner; + } + + public void setOwner(String owner) { + this.owner = owner; + } + + public String getDeviceStatus() { + return deviceStatus; + } + + public void setDeviceStatus(String deviceStatus) { + this.deviceStatus = deviceStatus; + } + + public String getFilteringDeviceSubscriptionStatus() { + return filteringDeviceSubscriptionStatus; + } + + public void setFilteringDeviceSubscriptionStatus(String filteringDeviceSubscriptionStatus) { + this.filteringDeviceSubscriptionStatus = filteringDeviceSubscriptionStatus; + } + + public String getTriggeredBy() { + return triggeredBy; + } + + public void setTriggeredBy(String triggeredBy) { + this.triggeredBy = triggeredBy; + } +} 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/SubscriptionEntity.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionEntity.java new file mode 100644 index 0000000000..40c1b2f4a7 --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionEntity.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.common; + +import java.sql.Timestamp; + +public class SubscriptionEntity { + private String identity; + private String subscribedBy; + private Timestamp subscribedTimestamp; + private boolean unsubscribed; + private String unsubscribedBy; + private Timestamp unsubscribedTimestamp; + private int applicationReleaseId; + + public String getIdentity() { + return identity; + } + + public void setIdentity(String identity) { + this.identity = identity; + } + + public String getSubscribedBy() { + return subscribedBy; + } + + public void setSubscribedBy(String subscribedBy) { + this.subscribedBy = subscribedBy; + } + + public Timestamp getSubscribedTimestamp() { + return subscribedTimestamp; + } + + public void setSubscribedTimestamp(Timestamp subscribedTimestamp) { + this.subscribedTimestamp = subscribedTimestamp; + } + + public boolean getUnsubscribed() { + return unsubscribed; + } + + public void setUnsubscribed(boolean unsubscribed) { + this.unsubscribed = unsubscribed; + } + + public String getUnsubscribedBy() { + return unsubscribedBy; + } + + public void setUnsubscribedBy(String unsubscribedBy) { + this.unsubscribedBy = unsubscribedBy; + } + + public Timestamp getUnsubscribedTimestamp() { + return unsubscribedTimestamp; + } + + public void setUnsubscribedTimestamp(Timestamp unsubscribedTimestamp) { + this.unsubscribedTimestamp = unsubscribedTimestamp; + } + + public int getApplicationReleaseId() { + return applicationReleaseId; + } + + public void setApplicationReleaseId(int applicationReleaseId) { + this.applicationReleaseId = applicationReleaseId; + } +} 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/SubscriptionInfo.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionInfo.java new file mode 100644 index 0000000000..6af03aa61a --- /dev/null +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/SubscriptionInfo.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.common; + +public class SubscriptionInfo { + private String applicationUUID; + private String subscriptionType; + private String deviceSubscriptionStatus; + private String identifier; + private String subscriptionStatus; + private DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria; + + public String getApplicationUUID() { + return applicationUUID; + } + + public void setApplicationUUID(String applicationUUID) { + this.applicationUUID = applicationUUID; + } + + public String getSubscriptionType() { + return subscriptionType; + } + + public void setSubscriptionType(String subscriptionType) { + this.subscriptionType = subscriptionType; + } + + public String getDeviceSubscriptionStatus() { + return deviceSubscriptionStatus; + } + + public void setDeviceSubscriptionStatus(String deviceSubscriptionStatus) { + this.deviceSubscriptionStatus = deviceSubscriptionStatus; + } + + public String getIdentifier() { + return identifier; + } + + public void setIdentifier(String identifier) { + this.identifier = identifier; + } + + public String getSubscriptionStatus() { + return subscriptionStatus; + } + + public void setSubscriptionStatus(String subscriptionStatus) { + this.subscriptionStatus = subscriptionStatus; + } + + public DeviceSubscriptionFilterCriteria getDeviceSubscriptionFilterCriteria() { + return deviceSubscriptionFilterCriteria; + } + + public void setDeviceSubscriptionFilterCriteria(DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria) { + this.deviceSubscriptionFilterCriteria = deviceSubscriptionFilterCriteria; + } +} 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/dto/DeviceSubscriptionDTO.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceSubscriptionDTO.java index 44885312dc..a475a75cd0 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceSubscriptionDTO.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/dto/DeviceSubscriptionDTO.java @@ -35,6 +35,14 @@ public class DeviceSubscriptionDTO { private int appReleaseId; private String appUuid; + public DeviceSubscriptionDTO() { + + } + public DeviceSubscriptionDTO(int deviceId, String status) { + this.deviceId = deviceId; + this.status = status; + } + public int getId() { return id; } 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/services/SubscriptionManager.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/SubscriptionManager.java index c8ca40813a..7803c10ccb 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/SubscriptionManager.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.common/src/main/java/io/entgra/device/mgt/core/application/mgt/common/services/SubscriptionManager.java @@ -19,9 +19,13 @@ package io.entgra.device.mgt.core.application.mgt.common.services; import io.entgra.device.mgt.core.application.mgt.common.ApplicationInstallResponse; import io.entgra.device.mgt.core.application.mgt.common.CategorizedSubscriptionResult; +import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData; import io.entgra.device.mgt.core.application.mgt.common.ExecutionStatus; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; import io.entgra.device.mgt.core.application.mgt.common.SubscriptionType; import io.entgra.device.mgt.core.application.mgt.common.dto.CategorizedSubscriptionCountsDTO; +import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ScheduledSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceOperationDTO; @@ -260,8 +264,13 @@ public interface SubscriptionManager { * @return {@link SubscriptionsDTO} which contains the details of subscriptions. * @throws ApplicationManagementException if an error occurs while fetching the role details */ - List getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus, PaginationRequest request, - int offset, int limit) throws ApplicationManagementException; +// List getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus, PaginationRequest request, +// int offset, int limit) throws ApplicationManagementException; + List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException; + + List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException; /** * Retrieves the Device Subscription details associated with a given app release UUID. 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 8308669743..c77267d26c 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 @@ -19,6 +19,7 @@ package io.entgra.device.mgt.core.application.mgt.core.dao; import io.entgra.device.mgt.core.application.mgt.common.ExecutionStatus; import io.entgra.device.mgt.core.application.mgt.common.dto.GroupSubscriptionDTO; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO; @@ -327,7 +328,7 @@ public interface SubscriptionDAO { * @return {@link GroupSubscriptionDTO} which contains the details of group subscriptions. * @throws ApplicationManagementDAOException if connection establishment fails. */ - List getGroupsSubscriptionDetailsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, int offset, int limit) + List getGroupsSubscriptionDetailsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, int offset, int limit) throws ApplicationManagementDAOException; /** @@ -355,7 +356,7 @@ public interface SubscriptionDAO { * @return {@link SubscriptionsDTO} which contains the details of subscriptions. * @throws ApplicationManagementDAOException if connection establishment or SQL execution fails. */ - List getRoleSubscriptionsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, int offset, int limit) + List getRoleSubscriptionsByAppReleaseID(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 c1ab28e092..45d4cff175 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 @@ -19,6 +19,7 @@ package io.entgra.device.mgt.core.application.mgt.core.dao.impl.subscription; import io.entgra.device.mgt.core.application.mgt.common.dto.GroupSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceOperationDTO; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; import io.entgra.device.mgt.core.application.mgt.core.dao.SubscriptionDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.impl.AbstractDAOImpl; @@ -1641,14 +1642,14 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } @Override - public List getGroupsSubscriptionDetailsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, int offset, int limit) + public List getGroupsSubscriptionDetailsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, int offset, int limit) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to get groups related to the given AppReleaseID."); } try { Connection conn = this.getDBConnection(); - List groupDetails = new ArrayList<>(); + List subscriptionEntities = new ArrayList<>(); String subscriptionStatusTime = unsubscribe ? "GS.UNSUBSCRIBED_TIMESTAMP" : "GS.SUBSCRIBED_TIMESTAMP"; String sql = "SELECT GS.GROUP_NAME, GS.SUBSCRIBED_BY, GS.SUBSCRIBED_TIMESTAMP, GS.UNSUBSCRIBED, " + @@ -1665,21 +1666,21 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc ps.setInt(5, offset); try (ResultSet rs = ps.executeQuery()) { - GroupSubscriptionDTO groupDetail; + SubscriptionEntity subscriptionEntity; while (rs.next()) { - groupDetail = new GroupSubscriptionDTO(); - groupDetail.setGroupName(rs.getString("GROUP_NAME")); - groupDetail.setSubscribedBy(rs.getString("SUBSCRIBED_BY")); - groupDetail.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_TIMESTAMP")); - groupDetail.setUnsubscribed(rs.getBoolean("UNSUBSCRIBED")); - groupDetail.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY")); - groupDetail.setUnsubscribedTimestamp(rs.getTimestamp("UNSUBSCRIBED_TIMESTAMP")); - groupDetail.setAppReleaseId(rs.getInt("AP_APP_RELEASE_ID")); - - groupDetails.add(groupDetail); + subscriptionEntity = new SubscriptionEntity(); + subscriptionEntity.setIdentity(rs.getString("GROUP_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 groupDetails; + return subscriptionEntities; } } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the DB connection to get groups for the given UUID."; @@ -1744,14 +1745,14 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } @Override - public List getRoleSubscriptionsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, int offset, - int limit) throws ApplicationManagementDAOException { + public List getRoleSubscriptionsByAppReleaseID(int appReleaseId, boolean unsubscribe, int tenantId, int offset, + int limit) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Request received in DAO Layer to get role subscriptions related to the given AppReleaseID."); } try { Connection conn = this.getDBConnection(); - List roleSubscriptions = new ArrayList<>(); + List subscriptionEntities = new ArrayList<>(); String subscriptionStatusTime = unsubscribe ? "ARS.UNSUBSCRIBED_TIMESTAMP" : "ARS.SUBSCRIBED_TIMESTAMP"; String sql = "SELECT ARS.ROLE_NAME, ARS.SUBSCRIBED_BY, ARS.SUBSCRIBED_TIMESTAMP, ARS.UNSUBSCRIBED, " + @@ -1767,21 +1768,21 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc ps.setInt(4, limit); ps.setInt(5, offset); try (ResultSet rs = ps.executeQuery()) { - SubscriptionsDTO roleSubscription; + SubscriptionEntity subscriptionEntity; while (rs.next()) { - roleSubscription = new SubscriptionsDTO(); - roleSubscription.setName(rs.getString("ROLE_NAME")); - roleSubscription.setSubscribedBy(rs.getString("SUBSCRIBED_BY")); - roleSubscription.setSubscribedTimestamp(rs.getTimestamp("SUBSCRIBED_TIMESTAMP")); - roleSubscription.setUnsubscribed(rs.getBoolean("UNSUBSCRIBED")); - roleSubscription.setUnsubscribedBy(rs.getString("UNSUBSCRIBED_BY")); - roleSubscription.setUnsubscribedTimestamp(rs.getTimestamp("UNSUBSCRIBED_TIMESTAMP")); - roleSubscription.setAppReleaseId(rs.getInt("AP_APP_RELEASE_ID")); - - roleSubscriptions.add(roleSubscription); + subscriptionEntity = new SubscriptionEntity(); + subscriptionEntity.setIdentity(rs.getString("ROLE_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 roleSubscriptions; + return subscriptionEntities; } } catch (DBConnectionException e) { String msg = "Error occurred while obtaining the DB connection to get role subscriptions for the given UUID."; 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/impl/SubscriptionManagerImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java index 64995450cc..0d01be1c1b 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -24,6 +24,8 @@ import io.entgra.device.mgt.core.application.mgt.common.ApplicationSubscriptionI import io.entgra.device.mgt.core.application.mgt.common.ApplicationType; import io.entgra.device.mgt.core.application.mgt.common.CategorizedSubscriptionResult; import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; import io.entgra.device.mgt.core.application.mgt.common.dto.CategorizedSubscriptionCountsDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; @@ -45,6 +47,8 @@ import io.entgra.device.mgt.core.application.mgt.common.services.VPPApplicationM import io.entgra.device.mgt.core.application.mgt.core.dao.ApplicationReleaseDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.VppApplicationDAO; import io.entgra.device.mgt.core.application.mgt.core.exception.BadRequestException; +import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.SubscriptionManagementServiceProvider; +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.PaginationResult; import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceDetailsDTO; @@ -1707,698 +1711,727 @@ public class SubscriptionManagerImpl implements SubscriptionManager { public List getGroupsSubscriptionDetailsByUUID( String uuid, String subscriptionStatus, PaginationRequest request, int offset, int limit) throws ApplicationManagementException { - - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - boolean unsubscribe = subscriptionStatus.equals("unsubscribed"); - - 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 groupDetailsWithDevices = new ArrayList<>(); - - List groupDetails = - subscriptionDAO.getGroupsSubscriptionDetailsByAppReleaseID(appReleaseId, unsubscribe, tenantId, offset, limit); - if (groupDetails == null) { - throw new ApplicationManagementException("Group details not found for appReleaseId: " + appReleaseId); - } - - GroupManagementProviderService groupManagementProviderService = HelperUtil.getGroupManagementProviderService(); - - for (GroupSubscriptionDTO groupDetail : groupDetails) { - - if (StringUtils.isNotBlank(request.getGroupName()) && !request.getGroupName().equals(groupDetail.getGroupName())) { - continue; - } - - String groupName = StringUtils.isNotBlank(request.getGroupName()) ? request.getGroupName() : groupDetail.getGroupName(); - - // Retrieve group details and device IDs for the group using the service layer - GroupDetailsDTO groupDetailWithDevices = - groupManagementProviderService.getGroupDetailsWithDevices( - groupName, applicationDTO.getDeviceTypeId(), request.getOwner(), - request.getDeviceName(), request.getDeviceStatus(), offset, limit); - - SubscriptionsDTO groupDetailDTO = new SubscriptionsDTO(); - groupDetailDTO.setId(groupDetailWithDevices.getGroupId()); - groupDetailDTO.setName(groupDetail.getGroupName()); - groupDetailDTO.setOwner(groupDetailWithDevices.getGroupOwner()); - groupDetailDTO.setSubscribedBy(groupDetail.getSubscribedBy()); - groupDetailDTO.setSubscribedTimestamp(groupDetail.getSubscribedTimestamp()); - groupDetailDTO.setUnsubscribed(groupDetail.isUnsubscribed()); - groupDetailDTO.setUnsubscribedBy(groupDetail.getUnsubscribedBy()); - groupDetailDTO.setUnsubscribedTimestamp(groupDetail.getUnsubscribedTimestamp()); - groupDetailDTO.setAppReleaseId(groupDetail.getAppReleaseId()); - groupDetailDTO.setDeviceCount(groupDetailWithDevices.getDeviceCount()); - - // Fetch device subscriptions for each device ID in the group - List pendingDevices = new ArrayList<>(); - List installedDevices = new ArrayList<>(); - List errorDevices = new ArrayList<>(); - List newDevices = new ArrayList<>(); - List subscribedDevices = new ArrayList<>(); - - List deviceIds = groupDetailWithDevices.getDeviceIds(); - Map statusCounts = new HashMap<>(); - statusCounts.put("COMPLETED", 0); - statusCounts.put("ERROR", 0); - statusCounts.put("PENDING", 0); - statusCounts.put("NEW", 0); - statusCounts.put("SUBSCRIBED", 0); - - for (Integer deviceId : deviceIds) { - // Get subscribed devices if unsubscribed devices are requested - List deviceSubscriptions; - if (unsubscribe) { - deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( - appReleaseId, !unsubscribe, tenantId, deviceIds, - request.getActionStatus(), request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); - } else { - deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( - groupDetail.getAppReleaseId(), false, tenantId, deviceIds, - request.getActionStatus(), request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); - } - List filteredDeviceSubscriptions = deviceSubscriptions.stream() - .filter(subscription -> StringUtils.isBlank(request.getTabActionStatus()) || subscription.getStatus().equals(request.getTabActionStatus())) - .collect(Collectors.toList()); - boolean isNewDevice = true; - for (DeviceSubscriptionDTO subscription : filteredDeviceSubscriptions) { - if (subscription.getDeviceId() == deviceId) { - DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData(); - deviceDetail.setDeviceId(subscription.getDeviceId()); - deviceDetail.setStatus(subscription.getStatus()); - deviceDetail.setActionType(subscription.getActionTriggeredFrom()); - deviceDetail.setDeviceOwner(groupDetailWithDevices.getDeviceOwners().get(deviceId)); - deviceDetail.setDeviceStatus(groupDetailWithDevices.getDeviceStatuses().get(deviceId)); - deviceDetail.setDeviceName(groupDetailWithDevices.getDeviceNames().get(deviceId)); - deviceDetail.setSubId(subscription.getId()); - deviceDetail.setActionTriggeredBy(subscription.getSubscribedBy()); - deviceDetail.setActionTriggeredTimestamp(subscription.getSubscribedTimestamp()); - deviceDetail.setUnsubscribed(subscription.isUnsubscribed()); - deviceDetail.setUnsubscribedBy(subscription.getUnsubscribedBy()); - deviceDetail.setUnsubscribedTimestamp(subscription.getUnsubscribedTimestamp()); - deviceDetail.setType(groupDetailWithDevices.getDeviceTypes().get(deviceId)); - deviceDetail.setDeviceIdentifier(groupDetailWithDevices.getDeviceIdentifiers().get(deviceId)); - - String 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; - default: - newDevices.add(deviceDetail); - statusCounts.put("NEW", statusCounts.get("NEW") + 1); - break; - } - isNewDevice = false; - } - } - if (isNewDevice) { - boolean isSubscribedDevice = false; - for (DeviceSubscriptionDTO subscribedDevice : deviceSubscriptions) { - if (subscribedDevice.getDeviceId() == deviceId) { - DeviceSubscriptionData subscribedDeviceDetail = new DeviceSubscriptionData(); - subscribedDeviceDetail.setDeviceId(subscribedDevice.getDeviceId()); - subscribedDeviceDetail.setDeviceOwner(groupDetailWithDevices.getDeviceOwners().get(deviceId)); - subscribedDeviceDetail.setDeviceStatus(groupDetailWithDevices.getDeviceStatuses().get(deviceId)); - subscribedDeviceDetail.setDeviceName(groupDetailWithDevices.getDeviceNames().get(deviceId)); - subscribedDeviceDetail.setSubId(subscribedDevice.getId()); - subscribedDeviceDetail.setActionTriggeredBy(subscribedDevice.getSubscribedBy()); - subscribedDeviceDetail.setActionTriggeredTimestamp(subscribedDevice.getSubscribedTimestamp()); - subscribedDeviceDetail.setActionType(subscribedDevice.getActionTriggeredFrom()); - subscribedDeviceDetail.setStatus(subscribedDevice.getStatus()); - subscribedDeviceDetail.setType(groupDetailWithDevices.getDeviceTypes().get(deviceId)); - subscribedDeviceDetail.setDeviceIdentifier(groupDetailWithDevices.getDeviceIdentifiers().get(deviceId)); - subscribedDevices.add(subscribedDeviceDetail); - statusCounts.put("SUBSCRIBED", statusCounts.get("SUBSCRIBED") + 1); - isSubscribedDevice = true; - break; - } - } - if (!isSubscribedDevice) { - DeviceSubscriptionData newDeviceDetail = new DeviceSubscriptionData(); - newDeviceDetail.setDeviceId(deviceId); - newDeviceDetail.setDeviceOwner(groupDetailWithDevices.getDeviceOwners().get(deviceId)); - newDeviceDetail.setDeviceStatus(groupDetailWithDevices.getDeviceStatuses().get(deviceId)); - newDeviceDetail.setDeviceName(groupDetailWithDevices.getDeviceNames().get(deviceId)); - newDeviceDetail.setType(groupDetailWithDevices.getDeviceTypes().get(deviceId)); - newDeviceDetail.setDeviceIdentifier(groupDetailWithDevices.getDeviceIdentifiers().get(deviceId)); - 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; - } - groupDetailDTO.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); - } - groupDetailDTO.setDevices(categorizedSubscriptionResult); - } - groupDetailDTO.setStatusPercentages(statusPercentages); - groupDetailsWithDevices.add(groupDetailDTO); - } - - return groupDetailsWithDevices; - } catch (ApplicationManagementDAOException e) { - String msg = "Error occurred while fetching groups and devices for UUID: " + uuid; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (DBConnectionException e) { - String msg = "DB Connection error occurred while fetching groups and devices for UUID: " + uuid; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (GroupManagementException e) { - String msg = "Error occurred while fetching group details and device IDs: " + e.getMessage(); - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } + return null; } @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(); - } + PaginationRequest request, int offset, int limit) throws ApplicationManagementException { + return null; } - @Override - public List getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus, - PaginationRequest request, int offset, int limit) + public List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - boolean unsubscribe = subscriptionStatus.equals("unsubscribed"); - String roleName; - 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 roleSubscriptionsWithDevices = new ArrayList<>(); - - List roleSubscriptions = - subscriptionDAO.getRoleSubscriptionsByAppReleaseID(appReleaseId, unsubscribe, tenantId, offset, limit); - if (roleSubscriptions == null) { - throw new ApplicationManagementException("Role details not found for appReleaseId: " + appReleaseId); - } - - DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); - - for (SubscriptionsDTO roleSubscription : roleSubscriptions) { - - roleName = StringUtils.isNotBlank(request.getRoleName()) ? request.getRoleName() : roleSubscription.getName(); - - SubscriptionsDTO roleSubscriptionDTO = new SubscriptionsDTO(); - roleSubscriptionDTO.setName(roleSubscription.getName()); - roleSubscriptionDTO.setSubscribedBy(roleSubscription.getSubscribedBy()); - roleSubscriptionDTO.setSubscribedTimestamp(roleSubscription.getSubscribedTimestamp()); - roleSubscriptionDTO.setUnsubscribed(roleSubscription.getUnsubscribed()); - roleSubscriptionDTO.setUnsubscribedBy(roleSubscription.getUnsubscribedBy()); - roleSubscriptionDTO.setUnsubscribedTimestamp(roleSubscription.getUnsubscribedTimestamp()); - roleSubscriptionDTO.setAppReleaseId(roleSubscription.getAppReleaseId()); - - List pendingDevices = new ArrayList<>(); - List installedDevices = new ArrayList<>(); - List errorDevices = new ArrayList<>(); - List newDevices = new ArrayList<>(); - List subscribedDevices = new ArrayList<>(); - - Map statusCounts = new HashMap<>(); - statusCounts.put("PENDING", 0); - statusCounts.put("COMPLETED", 0); - statusCounts.put("ERROR", 0); - statusCounts.put("NEW", 0); - statusCounts.put("SUBSCRIBED", 0); - - // getting the user list for the role - List users = this.getUsersForRole(roleName); - - for (String user : users) { - - // for each user get the device info and device ids - OwnerWithDeviceDTO ownerDetailsWithDevices; - try { - ownerDetailsWithDevices = deviceManagementProviderService.getOwnersWithDeviceIds(user, applicationDTO.getDeviceTypeId(), - request.getOwner(), request.getDeviceName(), request.getDeviceStatus()); - } catch (DeviceManagementDAOException e) { - throw new ApplicationManagementException("Error retrieving owner details with devices for user: " + user, e); - } - - List deviceIds = ownerDetailsWithDevices.getDeviceIds(); - // now for each device id - for (Integer deviceId : deviceIds) { - - List subscribedDeviceSubscriptions = new ArrayList<>(); - if (unsubscribe) { - subscribedDeviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( - appReleaseId, !unsubscribe, tenantId, deviceIds, request.getActionStatus(), request.getActionType(), - request.getActionTriggeredBy(), request.getTabActionStatus()); - } - - // why the fuck is this here - OwnerWithDeviceDTO ownerWithDeviceByDeviceId = - deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId, request.getOwner(), request.getDeviceName(), - request.getDeviceStatus()); - - - if (ownerWithDeviceByDeviceId == null) { - continue; - } - - - List deviceSubscriptions; - try { - deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( - roleSubscription.getAppReleaseId(), unsubscribe, tenantId, deviceIds, request.getActionStatus(), - request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); - } catch (ApplicationManagementDAOException e) { - throw new ApplicationManagementException("Error retrieving device subscriptions", e); - } - - boolean isNewDevice = true; - for (DeviceSubscriptionDTO deviceSubscription : deviceSubscriptions) { - if (deviceSubscription.getDeviceId() == deviceId) { - DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData(); - deviceDetail.setDeviceId(deviceSubscription.getDeviceId()); - deviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); - deviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); - deviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); - deviceDetail.setActionType(deviceSubscription.getActionTriggeredFrom()); - deviceDetail.setStatus(deviceSubscription.getStatus()); - deviceDetail.setActionType(deviceSubscription.getActionTriggeredFrom()); - deviceDetail.setActionTriggeredBy(deviceSubscription.getSubscribedBy()); - deviceDetail.setSubId(deviceSubscription.getId()); - deviceDetail.setActionTriggeredTimestamp(deviceSubscription.getSubscribedTimestamp()); - deviceDetail.setUnsubscribed(deviceSubscription.isUnsubscribed()); - deviceDetail.setUnsubscribedBy(deviceSubscription.getUnsubscribedBy()); - deviceDetail.setUnsubscribedTimestamp(deviceSubscription.getUnsubscribedTimestamp()); - deviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); - deviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); - - status = deviceSubscription.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.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); - newDeviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); - newDeviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); - newDeviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); - newDeviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); - newDevices.add(newDeviceDetail); - statusCounts.put("NEW", statusCounts.get("NEW") + 1); - } - } - } - } - - int totalDevices = - pendingDevices.size() + installedDevices.size() + errorDevices.size() + newDevices.size() + subscribedDevices.size(); - Map statusPercentages = new HashMap<>(); - for (Map.Entry entry : statusCounts.entrySet()) { - double percentage = totalDevices == 0 ? 0.0 : ((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; - } - roleSubscriptionDTO.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); - } - roleSubscriptionDTO.setDevices(categorizedSubscriptionResult); - roleSubscriptionDTO.setStatusPercentages(statusPercentages); - roleSubscriptionDTO.setDeviceCount(totalDevices); - } - roleSubscriptionsWithDevices.add(roleSubscriptionDTO); - } + SubscriptionManagementHelperService subscriptionManagementHelperService = + SubscriptionManagementServiceProvider.getInstance().getSubscriptionManagementHelperService(subscriptionInfo); + return subscriptionManagementHelperService.getSubscriptions(subscriptionInfo, limit, offset); + } - return roleSubscriptionsWithDevices; - } catch (ApplicationManagementDAOException | DeviceManagementDAOException e) { - String msg = "Error occurred in retrieving role subscriptions with devices"; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (DBConnectionException e) { - String msg = "Error occurred while retrieving the database connection"; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } catch (UserStoreException e) { - String msg = "Error occurred while retrieving users for role"; - log.error(msg, e); - throw new ApplicationManagementException(msg, e); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } + @Override + public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException { + SubscriptionManagementHelperService subscriptionManagementHelperService = + SubscriptionManagementServiceProvider.getInstance().getSubscriptionManagementHelperService(subscriptionInfo); + return subscriptionManagementHelperService.getStatusBaseSubscriptions(subscriptionInfo, limit, offset); } +// @Override +// public List getGroupsSubscriptionDetailsByUUID( +// String uuid, String subscriptionStatus, PaginationRequest request, int offset, int limit) +// throws ApplicationManagementException { +// +// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); +// boolean unsubscribe = subscriptionStatus.equals("unsubscribed"); +// +// 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 groupDetailsWithDevices = new ArrayList<>(); +// +// List groupDetails = +// subscriptionDAO.getGroupsSubscriptionDetailsByAppReleaseID(appReleaseId, unsubscribe, tenantId, offset, limit); +// if (groupDetails == null) { +// throw new ApplicationManagementException("Group details not found for appReleaseId: " + appReleaseId); +// } +// +// GroupManagementProviderService groupManagementProviderService = HelperUtil.getGroupManagementProviderService(); +// +// for (GroupSubscriptionDTO groupDetail : groupDetails) { +// +// if (StringUtils.isNotBlank(request.getGroupName()) && !request.getGroupName().equals(groupDetail.getGroupName())) { +// continue; +// } +// +// String groupName = StringUtils.isNotBlank(request.getGroupName()) ? request.getGroupName() : groupDetail.getGroupName(); +// +// // Retrieve group details and device IDs for the group using the service layer +// GroupDetailsDTO groupDetailWithDevices = +// groupManagementProviderService.getGroupDetailsWithDevices( +// groupName, applicationDTO.getDeviceTypeId(), request.getOwner(), +// request.getDeviceName(), request.getDeviceStatus(), offset, limit); +// +// SubscriptionsDTO groupDetailDTO = new SubscriptionsDTO(); +// groupDetailDTO.setId(groupDetailWithDevices.getGroupId()); +// groupDetailDTO.setName(groupDetail.getGroupName()); +// groupDetailDTO.setOwner(groupDetailWithDevices.getGroupOwner()); +// groupDetailDTO.setSubscribedBy(groupDetail.getSubscribedBy()); +// groupDetailDTO.setSubscribedTimestamp(groupDetail.getSubscribedTimestamp()); +// groupDetailDTO.setUnsubscribed(groupDetail.isUnsubscribed()); +// groupDetailDTO.setUnsubscribedBy(groupDetail.getUnsubscribedBy()); +// groupDetailDTO.setUnsubscribedTimestamp(groupDetail.getUnsubscribedTimestamp()); +// groupDetailDTO.setAppReleaseId(groupDetail.getAppReleaseId()); +// groupDetailDTO.setDeviceCount(groupDetailWithDevices.getDeviceCount()); +// +// // Fetch device subscriptions for each device ID in the group +// List pendingDevices = new ArrayList<>(); +// List installedDevices = new ArrayList<>(); +// List errorDevices = new ArrayList<>(); +// List newDevices = new ArrayList<>(); +// List subscribedDevices = new ArrayList<>(); +// +// List deviceIds = groupDetailWithDevices.getDeviceIds(); +// Map statusCounts = new HashMap<>(); +// statusCounts.put("COMPLETED", 0); +// statusCounts.put("ERROR", 0); +// statusCounts.put("PENDING", 0); +// statusCounts.put("NEW", 0); +// statusCounts.put("SUBSCRIBED", 0); +// +// for (Integer deviceId : deviceIds) { +// // Get subscribed devices if unsubscribed devices are requested +// List deviceSubscriptions; +// if (unsubscribe) { +// deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( +// appReleaseId, !unsubscribe, tenantId, deviceIds, +// request.getActionStatus(), request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); +// } else { +// deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( +// groupDetail.getAppReleaseId(), false, tenantId, deviceIds, +// request.getActionStatus(), request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); +// } +// List filteredDeviceSubscriptions = deviceSubscriptions.stream() +// .filter(subscription -> StringUtils.isBlank(request.getTabActionStatus()) || subscription.getStatus().equals(request.getTabActionStatus())) +// .collect(Collectors.toList()); +// boolean isNewDevice = true; +// for (DeviceSubscriptionDTO subscription : filteredDeviceSubscriptions) { +// if (subscription.getDeviceId() == deviceId) { +// DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData(); +// deviceDetail.setDeviceId(subscription.getDeviceId()); +// deviceDetail.setStatus(subscription.getStatus()); +// deviceDetail.setActionType(subscription.getActionTriggeredFrom()); +// deviceDetail.setDeviceOwner(groupDetailWithDevices.getDeviceOwners().get(deviceId)); +// deviceDetail.setDeviceStatus(groupDetailWithDevices.getDeviceStatuses().get(deviceId)); +// deviceDetail.setDeviceName(groupDetailWithDevices.getDeviceNames().get(deviceId)); +// deviceDetail.setSubId(subscription.getId()); +// deviceDetail.setActionTriggeredBy(subscription.getSubscribedBy()); +// deviceDetail.setActionTriggeredTimestamp(subscription.getSubscribedTimestamp()); +// deviceDetail.setUnsubscribed(subscription.isUnsubscribed()); +// deviceDetail.setUnsubscribedBy(subscription.getUnsubscribedBy()); +// deviceDetail.setUnsubscribedTimestamp(subscription.getUnsubscribedTimestamp()); +// deviceDetail.setType(groupDetailWithDevices.getDeviceTypes().get(deviceId)); +// deviceDetail.setDeviceIdentifier(groupDetailWithDevices.getDeviceIdentifiers().get(deviceId)); +// +// String 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; +// default: +// newDevices.add(deviceDetail); +// statusCounts.put("NEW", statusCounts.get("NEW") + 1); +// break; +// } +// isNewDevice = false; +// } +// } +// if (isNewDevice) { +// boolean isSubscribedDevice = false; +// for (DeviceSubscriptionDTO subscribedDevice : deviceSubscriptions) { +// if (subscribedDevice.getDeviceId() == deviceId) { +// DeviceSubscriptionData subscribedDeviceDetail = new DeviceSubscriptionData(); +// subscribedDeviceDetail.setDeviceId(subscribedDevice.getDeviceId()); +// subscribedDeviceDetail.setDeviceOwner(groupDetailWithDevices.getDeviceOwners().get(deviceId)); +// subscribedDeviceDetail.setDeviceStatus(groupDetailWithDevices.getDeviceStatuses().get(deviceId)); +// subscribedDeviceDetail.setDeviceName(groupDetailWithDevices.getDeviceNames().get(deviceId)); +// subscribedDeviceDetail.setSubId(subscribedDevice.getId()); +// subscribedDeviceDetail.setActionTriggeredBy(subscribedDevice.getSubscribedBy()); +// subscribedDeviceDetail.setActionTriggeredTimestamp(subscribedDevice.getSubscribedTimestamp()); +// subscribedDeviceDetail.setActionType(subscribedDevice.getActionTriggeredFrom()); +// subscribedDeviceDetail.setStatus(subscribedDevice.getStatus()); +// subscribedDeviceDetail.setType(groupDetailWithDevices.getDeviceTypes().get(deviceId)); +// subscribedDeviceDetail.setDeviceIdentifier(groupDetailWithDevices.getDeviceIdentifiers().get(deviceId)); +// subscribedDevices.add(subscribedDeviceDetail); +// statusCounts.put("SUBSCRIBED", statusCounts.get("SUBSCRIBED") + 1); +// isSubscribedDevice = true; +// break; +// } +// } +// if (!isSubscribedDevice) { +// DeviceSubscriptionData newDeviceDetail = new DeviceSubscriptionData(); +// newDeviceDetail.setDeviceId(deviceId); +// newDeviceDetail.setDeviceOwner(groupDetailWithDevices.getDeviceOwners().get(deviceId)); +// newDeviceDetail.setDeviceStatus(groupDetailWithDevices.getDeviceStatuses().get(deviceId)); +// newDeviceDetail.setDeviceName(groupDetailWithDevices.getDeviceNames().get(deviceId)); +// newDeviceDetail.setType(groupDetailWithDevices.getDeviceTypes().get(deviceId)); +// newDeviceDetail.setDeviceIdentifier(groupDetailWithDevices.getDeviceIdentifiers().get(deviceId)); +// 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; +// } +// groupDetailDTO.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); +// } +// groupDetailDTO.setDevices(categorizedSubscriptionResult); +// } +// groupDetailDTO.setStatusPercentages(statusPercentages); +// groupDetailsWithDevices.add(groupDetailDTO); +// } +// +// return groupDetailsWithDevices; +// } catch (ApplicationManagementDAOException e) { +// String msg = "Error occurred while fetching groups and devices for UUID: " + uuid; +// log.error(msg, e); +// throw new ApplicationManagementException(msg, e); +// } catch (DBConnectionException e) { +// String msg = "DB Connection error occurred while fetching groups and devices for UUID: " + uuid; +// log.error(msg, e); +// throw new ApplicationManagementException(msg, e); +// } catch (GroupManagementException e) { +// String msg = "Error occurred while fetching group details and device IDs: " + e.getMessage(); +// log.error(msg, e); +// throw new ApplicationManagementException(msg, e); +// } finally { +// ConnectionManagerUtil.closeDBConnection(); +// } +// } +// +// @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(); +// } +// } +// +// +// @Override +// public List getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus, +// PaginationRequest request, int offset, int limit) +// throws ApplicationManagementException { +// int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); +// boolean unsubscribe = subscriptionStatus.equals("unsubscribed"); +// String roleName; +// 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 roleSubscriptionsWithDevices = new ArrayList<>(); +// +// List roleSubscriptions = +// subscriptionDAO.getRoleSubscriptionsByAppReleaseID(appReleaseId, unsubscribe, tenantId, offset, limit); +// if (roleSubscriptions == null) { +// throw new ApplicationManagementException("Role details not found for appReleaseId: " + appReleaseId); +// } +// + DeviceManagementProviderService deviceManagementProviderService = HelperUtil.getDeviceManagementProviderService(); +// +// for (SubscriptionsDTO roleSubscription : roleSubscriptions) { +// +// roleName = StringUtils.isNotBlank(request.getRoleName()) ? request.getRoleName() : roleSubscription.getName(); +// +// SubscriptionsDTO roleSubscriptionDTO = new SubscriptionsDTO(); +// roleSubscriptionDTO.setName(roleSubscription.getName()); +// roleSubscriptionDTO.setSubscribedBy(roleSubscription.getSubscribedBy()); +// roleSubscriptionDTO.setSubscribedTimestamp(roleSubscription.getSubscribedTimestamp()); +// roleSubscriptionDTO.setUnsubscribed(roleSubscription.getUnsubscribed()); +// roleSubscriptionDTO.setUnsubscribedBy(roleSubscription.getUnsubscribedBy()); +// roleSubscriptionDTO.setUnsubscribedTimestamp(roleSubscription.getUnsubscribedTimestamp()); +// roleSubscriptionDTO.setAppReleaseId(roleSubscription.getAppReleaseId()); +// +// List pendingDevices = new ArrayList<>(); +// List installedDevices = new ArrayList<>(); +// List errorDevices = new ArrayList<>(); +// List newDevices = new ArrayList<>(); +// List subscribedDevices = new ArrayList<>(); +// +// Map statusCounts = new HashMap<>(); +// statusCounts.put("PENDING", 0); +// statusCounts.put("COMPLETED", 0); +// statusCounts.put("ERROR", 0); +// statusCounts.put("NEW", 0); +// statusCounts.put("SUBSCRIBED", 0); +// +// // getting the user list for the role +// List users = this.getUsersForRole(roleName); +// +// for (String user : users) { +// +// // for each user get the device info and device ids +// OwnerWithDeviceDTO ownerDetailsWithDevices; +// try { +// ownerDetailsWithDevices = deviceManagementProviderService.getOwnersWithDeviceIds(user, applicationDTO.getDeviceTypeId(), +// request.getOwner(), request.getDeviceName(), request.getDeviceStatus()); +// } catch (DeviceManagementDAOException e) { +// throw new ApplicationManagementException("Error retrieving owner details with devices for user: " + user, e); +// } +// +// List deviceIds = ownerDetailsWithDevices.getDeviceIds(); +// // now for each device id +// for (Integer deviceId : deviceIds) { +// +// List subscribedDeviceSubscriptions = new ArrayList<>(); +// if (unsubscribe) { +// subscribedDeviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( +// appReleaseId, !unsubscribe, tenantId, deviceIds, request.getActionStatus(), request.getActionType(), +// request.getActionTriggeredBy(), request.getTabActionStatus()); +// } +// +// // why the fuck is this here +// OwnerWithDeviceDTO ownerWithDeviceByDeviceId = +// deviceManagementProviderService.getOwnerWithDeviceByDeviceId(deviceId, request.getOwner(), request.getDeviceName(), +// request.getDeviceStatus()); +// +// +// if (ownerWithDeviceByDeviceId == null) { +// continue; +// } +// +// +// List deviceSubscriptions; +// try { +// deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsByDeviceIds( +// roleSubscription.getAppReleaseId(), unsubscribe, tenantId, deviceIds, request.getActionStatus(), +// request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); +// } catch (ApplicationManagementDAOException e) { +// throw new ApplicationManagementException("Error retrieving device subscriptions", e); +// } +// +// boolean isNewDevice = true; +// for (DeviceSubscriptionDTO deviceSubscription : deviceSubscriptions) { +// if (deviceSubscription.getDeviceId() == deviceId) { +// DeviceSubscriptionData deviceDetail = new DeviceSubscriptionData(); +// deviceDetail.setDeviceId(deviceSubscription.getDeviceId()); +// deviceDetail.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); +// deviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); +// deviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); +// deviceDetail.setActionType(deviceSubscription.getActionTriggeredFrom()); +// deviceDetail.setStatus(deviceSubscription.getStatus()); +// deviceDetail.setActionType(deviceSubscription.getActionTriggeredFrom()); +// deviceDetail.setActionTriggeredBy(deviceSubscription.getSubscribedBy()); +// deviceDetail.setSubId(deviceSubscription.getId()); +// deviceDetail.setActionTriggeredTimestamp(deviceSubscription.getSubscribedTimestamp()); +// deviceDetail.setUnsubscribed(deviceSubscription.isUnsubscribed()); +// deviceDetail.setUnsubscribedBy(deviceSubscription.getUnsubscribedBy()); +// deviceDetail.setUnsubscribedTimestamp(deviceSubscription.getUnsubscribedTimestamp()); +// deviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); +// deviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); +// +// status = deviceSubscription.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.setDeviceName(ownerWithDeviceByDeviceId.getDeviceNames()); +// newDeviceDetail.setDeviceOwner(ownerWithDeviceByDeviceId.getUserName()); +// newDeviceDetail.setDeviceStatus(ownerWithDeviceByDeviceId.getDeviceStatus()); +// newDeviceDetail.setType(ownerWithDeviceByDeviceId.getDeviceTypes()); +// newDeviceDetail.setDeviceIdentifier(ownerWithDeviceByDeviceId.getDeviceIdentifiers()); +// newDevices.add(newDeviceDetail); +// statusCounts.put("NEW", statusCounts.get("NEW") + 1); +// } +// } +// } +// } +// +// int totalDevices = +// pendingDevices.size() + installedDevices.size() + errorDevices.size() + newDevices.size() + subscribedDevices.size(); +// Map statusPercentages = new HashMap<>(); +// for (Map.Entry entry : statusCounts.entrySet()) { +// double percentage = totalDevices == 0 ? 0.0 : ((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; +// } +// roleSubscriptionDTO.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); +// } +// roleSubscriptionDTO.setDevices(categorizedSubscriptionResult); +// roleSubscriptionDTO.setStatusPercentages(statusPercentages); +// roleSubscriptionDTO.setDeviceCount(totalDevices); +// } +// roleSubscriptionsWithDevices.add(roleSubscriptionDTO); +// } +// +// return roleSubscriptionsWithDevices; +// } catch (ApplicationManagementDAOException | DeviceManagementDAOException e) { +// String msg = "Error occurred in retrieving role subscriptions with devices"; +// log.error(msg, e); +// throw new ApplicationManagementException(msg, e); +// } catch (DBConnectionException e) { +// String msg = "Error occurred while retrieving the database connection"; +// log.error(msg, e); +// throw new ApplicationManagementException(msg, e); +// } catch (UserStoreException e) { +// String msg = "Error occurred while retrieving users for role"; +// log.error(msg, e); +// throw new ApplicationManagementException(msg, e); +// } finally { +// ConnectionManagerUtil.closeDBConnection(); +// } +// } + // @Override // public List getRoleSubscriptionsByUUID(String uuid, String subscriptionStatus, // PaginationRequest request, int offset, int limit) @@ -2699,12 +2732,12 @@ public class SubscriptionManagerImpl implements SubscriptionManager { Map allDevicesMap = allDevices.stream() .collect(Collectors.toMap(DeviceDetailsDTO::getDeviceId, Function.identity())); - List allSubscriptionsForUnSubscribed = - subscriptionDAO.getSubscriptionDetailsByDeviceIds(appReleaseId, !unsubscribe, tenantId, deviceIds, request.getActionStatus(), - request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); - List allSubscriptionsForSubscribed = - subscriptionDAO.getSubscriptionDetailsByDeviceIds(appReleaseId, unsubscribe, tenantId, deviceIds, request.getActionStatus(), - request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); + List allSubscriptionsForUnSubscribed = null; +// subscriptionDAO.getSubscriptionDetailsByDeviceIds(appReleaseId, !unsubscribe, tenantId, deviceIds, request.getActionStatus(), +// request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); + List allSubscriptionsForSubscribed = null; +// subscriptionDAO.getSubscriptionDetailsByDeviceIds(appReleaseId, unsubscribe, tenantId, deviceIds, request.getActionStatus(), +// request.getActionType(), request.getActionTriggeredBy(), request.getTabActionStatus()); Map allSubscriptionForUnSubscribedMap = allSubscriptionsForUnSubscribed.stream() .collect(Collectors.toMap(DeviceSubscriptionDTO::getDeviceId, Function.identity())); Map allSubscriptionForSubscribedMap = allSubscriptionsForSubscribed.stream() 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 new file mode 100644 index 0000000000..3a369060f1 --- /dev/null +++ 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 @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt; + +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.SubscriptionInfo; +import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; + +import java.util.List; + +public class SubscriptionManagementHelperUtil { + public static List getDeviceSubscriptionData(List deviceSubscriptionDTOS, + DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria) { + // todo: filtering + for (DeviceSubscriptionDTO deviceSubscriptionDTO : deviceSubscriptionDTOS) { + + } + return null; + } + + public static String getDeviceSubscriptionStatus(SubscriptionInfo subscriptionInfo) { + return getDeviceSubscriptionStatus(subscriptionInfo.getDeviceSubscriptionFilterCriteria(). + getFilteringDeviceSubscriptionStatus(), subscriptionInfo.getDeviceSubscriptionStatus()); + } + + public static String getDeviceSubscriptionStatus(String deviceSubscriptionStatusFilter, String deviceSubscriptionStatus) { + return (deviceSubscriptionStatusFilter != null && !deviceSubscriptionStatusFilter.isEmpty()) ? + deviceSubscriptionStatusFilter : deviceSubscriptionStatus; + } +} 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 new file mode 100644 index 0000000000..671f4fdeba --- /dev/null +++ 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 @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.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.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.service.SubscriptionManagementHelperService; + +import java.util.Objects; + +public class SubscriptionManagementServiceProvider { + private SubscriptionManagementServiceProvider() {} + + private static class SubscriptionManagementProviderServiceHolder { + private static final SubscriptionManagementServiceProvider INSTANCE = new SubscriptionManagementServiceProvider(); + } + + public static SubscriptionManagementServiceProvider getInstance() { + return SubscriptionManagementProviderServiceHolder.INSTANCE; + } + + public SubscriptionManagementHelperService getSubscriptionManagementHelperService(SubscriptionInfo subscriptionInfo) { + return getSubscriptionManagementHelperService(subscriptionInfo.getSubscriptionType()); + } + + private SubscriptionManagementHelperService getSubscriptionManagementHelperService(String subscriptionType) { + if (Objects.equals(subscriptionType, "role")) return RoleBasedSubscriptionManagementHelperServiceImpl.getInstance(); + if (Objects.equals(subscriptionType, "group")) return GroupBasedSubscriptionManagementHelperServiceImpl.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 new file mode 100644 index 0000000000..a130f78d7a --- /dev/null +++ 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 @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl; + +import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; +import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; +import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; +import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; +import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service.SubscriptionManagementHelperService; + +import java.util.List; + +public class DeviceBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { + @Override + public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { + return null; + } + + @Override + public List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { + return null; + } + + @Override + public void getSubscriptionStatistics() throws ApplicationManagementException { + + } +} 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 new file mode 100644 index 0000000000..d59cd455f2 --- /dev/null +++ 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 @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl; + +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.ApplicationDTO; +import io.entgra.device.mgt.core.application.mgt.common.dto.ApplicationReleaseDTO; +import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; +import io.entgra.device.mgt.core.application.mgt.common.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.group.mgt.GroupManagementException; +import io.entgra.device.mgt.core.device.mgt.core.dto.GroupDetailsDTO; +import io.entgra.device.mgt.core.device.mgt.core.service.GroupManagementProviderService; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; + +import java.util.List; +import java.util.Objects; + +public class GroupBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { + private static final Log log = LogFactory.getLog(GroupBasedSubscriptionManagementHelperServiceImpl.class); + private GroupBasedSubscriptionManagementHelperServiceImpl() {} + private static class GroupBasedSubscriptionManagementHelperServiceImplHolder { + private static final GroupBasedSubscriptionManagementHelperServiceImpl INSTANCE + = new GroupBasedSubscriptionManagementHelperServiceImpl(); + } + + public static GroupBasedSubscriptionManagementHelperServiceImpl getInstance() { + return GroupBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE; + } + + @Override + 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); + } + + ApplicationDTO applicationDTO = this.applicationDAO.getAppWithRelatedRelease(subscriptionInfo.getApplicationUUID(), tenantId); + if (applicationDTO == null) { + String msg = "Application not found for the release UUID: " + subscriptionInfo.getApplicationUUID(); + log.error(msg); + throw new NotFoundException(msg); + } + + String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); + 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(); + + if (Objects.equals("NEW", deviceSubscriptionStatus)) { + + 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); + + } catch (GroupManagementException e) { + String msg = "Error encountered while retrieving group details for group: " + subscriptionInfo.getIdentifier(); + 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(); + } + + return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria()); + } + + @Override + 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. + getGroupsSubscriptionDetailsByAppReleaseID(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 { + + } +} 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/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 similarity index 62% rename from 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/RoleBasedSubscriptionManagementHelperServiceImpl.java rename to 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 854a46fef5..0bcbc6628a 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/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 @@ -17,11 +17,14 @@ * */ -package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt; +package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl; +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; @@ -29,8 +32,7 @@ import io.entgra.device.mgt.core.application.mgt.core.exception.NotFoundExceptio 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.bean.DeviceSubscriptionStatus; -import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.bean.RoleBasedSubscriptionInfo; +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; @@ -46,26 +48,33 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; +import java.util.stream.Collectors; public class RoleBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { private static final Log log = LogFactory.getLog(RoleBasedSubscriptionManagementHelperServiceImpl.class); - private final RoleBasedSubscriptionInfo roleBasedSubscriptionInfo; - private final boolean isUnsubscribe; - public RoleBasedSubscriptionManagementHelperServiceImpl(RoleBasedSubscriptionInfo roleBasedSubscriptionInfo) { - this.roleBasedSubscriptionInfo = roleBasedSubscriptionInfo; - this.isUnsubscribe = "unsubscribed".equals(roleBasedSubscriptionInfo.getSubscriptionStatus()); + private RoleBasedSubscriptionManagementHelperServiceImpl() {} + private static class RoleBasedSubscriptionManagementHelperServiceImplHolder { + private static final RoleBasedSubscriptionManagementHelperServiceImpl INSTANCE + = new RoleBasedSubscriptionManagementHelperServiceImpl(); + } + + public static RoleBasedSubscriptionManagementHelperServiceImpl getInstance() { + return RoleBasedSubscriptionManagementHelperServiceImplHolder.INSTANCE; } @SuppressWarnings("unchecked") @Override - public List getStatusBaseSubscriptions(int limit, int offset) throws ApplicationManagementException { + 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 { UserStoreManager userStoreManager = DataHolder.getInstance().getRealmService(). getTenantUserRealm(tenantId).getUserStoreManager(); String[] usersWithRole = - userStoreManager.getUserListOfRole(roleBasedSubscriptionInfo.getRoleName()); + userStoreManager.getUserListOfRole(subscriptionInfo.getIdentifier()); List deviceListOwnByRole = new ArrayList<>(); for (String user : usersWithRole) { PaginationRequest paginationRequest = new PaginationRequest(offset, limit); @@ -78,36 +87,37 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri } } - List deviceIds = (List) deviceListOwnByRole.stream().map(Device::getId); + List deviceIdsOwnByRole = deviceListOwnByRole.stream().map(Device::getId).collect(Collectors.toList()); ConnectionManagerUtil.openDBConnection(); ApplicationReleaseDTO applicationReleaseDTO = applicationReleaseDAO. - getReleaseByUUID(roleBasedSubscriptionInfo.getApplicationUUID(), tenantId); + getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); if (applicationReleaseDTO == null) { String msg = "Couldn't find an application release for application release UUID: " + - roleBasedSubscriptionInfo.getApplicationUUID(); + subscriptionInfo.getApplicationUUID(); log.error(msg); throw new NotFoundException(msg); } - if (Objects.equals(DeviceSubscriptionStatus.NEW.toString(), - roleBasedSubscriptionInfo.getDeviceSubscriptionStatus().toString())) { + String deviceSubscriptionStatus = SubscriptionManagementHelperUtil.getDeviceSubscriptionStatus(subscriptionInfo); + DeviceSubscriptionFilterCriteria deviceSubscriptionFilterCriteria = subscriptionInfo.getDeviceSubscriptionFilterCriteria(); + + if (Objects.equals("NEW", deviceSubscriptionStatus)) { deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIds, null, RoleBasedSubscriptionInfo.TRIGGERED_FROM_VALUE, - null, null, limit, offset); - for (Integer deviceId : deviceIds) { - DeviceSubscriptionDTO deviceSubscriptionDTO = new DeviceSubscriptionDTO(); - deviceSubscriptionDTO.setDeviceId(deviceId); - deviceSubscriptionDTOS.remove(deviceSubscriptionDTO); + isUnsubscribe, tenantId, deviceIdsOwnByRole, null, + subscriptionInfo.getSubscriptionType(), deviceSubscriptionFilterCriteria.getTriggeredBy(), + null, limit, offset); + for (DeviceSubscriptionDTO deviceSubscriptionDTO: deviceSubscriptionDTOS) { + deviceListOwnByRole.remove(new Device(deviceSubscriptionDTO.getId())); } - return deviceSubscriptionDTOS; + deviceIdsOwnByRole = deviceListOwnByRole.stream().map(Device::getId).collect(Collectors.toList()); + } deviceSubscriptionDTOS = subscriptionDAO.getSubscriptionDetailsByDeviceIds(applicationReleaseDTO.getId(), - isUnsubscribe, tenantId, deviceIds, roleBasedSubscriptionInfo.getDeviceSubscriptionStatus(). - toString(), RoleBasedSubscriptionInfo.TRIGGERED_FROM_VALUE, - null, null, limit, offset); + isUnsubscribe, tenantId, deviceIdsOwnByRole, deviceSubscriptionStatus, subscriptionInfo.getSubscriptionType(), + deviceSubscriptionFilterCriteria.getTriggeredBy(), deviceSubscriptionStatus, limit, offset); } catch (UserStoreException e) { String msg = "Error encountered while getting the user management store for tenant id " + tenantId; @@ -121,27 +131,36 @@ public class RoleBasedSubscriptionManagementHelperServiceImpl implements Subscri String msg = "Error encountered while connecting to the database"; log.error(msg, e); throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); } - return deviceSubscriptionDTOS; + return SubscriptionManagementHelperUtil.getDeviceSubscriptionData(deviceSubscriptionDTOS, + subscriptionInfo.getDeviceSubscriptionFilterCriteria()); } @Override - public List getSubscriptions(int limit, int offset) throws ApplicationManagementException { + 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(roleBasedSubscriptionInfo.getApplicationUUID(), tenantId); + getReleaseByUUID(subscriptionInfo.getApplicationUUID(), tenantId); if (applicationReleaseDTO == null) { String msg = "Couldn't find an application release for application release UUID: " + - roleBasedSubscriptionInfo.getApplicationUUID(); + subscriptionInfo.getApplicationUUID(); log.error(msg); throw new NotFoundException(msg); } return subscriptionDAO. getRoleSubscriptionsByAppReleaseID(applicationReleaseDTO.getId(), isUnsubscribe, tenantId, offset, limit); } catch (DBConnectionException | ApplicationManagementDAOException e) { - throw new RuntimeException(e); + String msg = "Error encountered while connecting to the database"; + log.error(msg, e); + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); } } 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 new file mode 100644 index 0000000000..939b72101a --- /dev/null +++ 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 @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.impl; + +import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; +import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; +import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; +import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; +import io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service.SubscriptionManagementHelperService; + +import java.util.List; + +public class UserBasedSubscriptionManagementHelperServiceImpl implements SubscriptionManagementHelperService { + @Override + public List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { + return null; + } + + @Override + public List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) throws ApplicationManagementException { + return null; + } + + @Override + public void getSubscriptionStatistics() throws ApplicationManagementException { + + } +} 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/service/SubscriptionManagementHelperService.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/service/SubscriptionManagementHelperService.java index 256ed05b46..e205b0a58e 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/service/SubscriptionManagementHelperService.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/service/SubscriptionManagementHelperService.java @@ -19,6 +19,9 @@ package io.entgra.device.mgt.core.application.mgt.core.util.subscription.mgt.service; +import io.entgra.device.mgt.core.application.mgt.common.DeviceSubscriptionData; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionEntity; +import io.entgra.device.mgt.core.application.mgt.common.SubscriptionInfo; import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionsDTO; import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException; @@ -33,7 +36,9 @@ public interface SubscriptionManagementHelperService { SubscriptionDAO subscriptionDAO = ApplicationManagementDAOFactory.getSubscriptionDAO(); ApplicationDAO applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); ApplicationReleaseDAO applicationReleaseDAO = ApplicationManagementDAOFactory.getApplicationReleaseDAO(); - List getStatusBaseSubscriptions(int limit, int offset) throws ApplicationManagementException; - List getSubscriptions(int limit, int offset) throws ApplicationManagementException; + List getStatusBaseSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException; + List getSubscriptions(SubscriptionInfo subscriptionInfo, int limit, int offset) + throws ApplicationManagementException; void getSubscriptionStatistics() throws ApplicationManagementException; } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/Device.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/Device.java index b75f557077..2819364327 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/Device.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/Device.java @@ -97,6 +97,10 @@ public class Device implements Serializable { public Device() { } + public Device(int id) { + this.id = id; + } + public Device(String name, String type, String description, String deviceId, EnrolmentInfo enrolmentInfo, List features, List properties) { this.name = name; @@ -268,7 +272,9 @@ public class Device implements Serializable { Device device = (Device) o; - return getDeviceIdentifier().equals(device.getDeviceIdentifier()); + if (getDeviceIdentifier().equals(device.getDeviceIdentifier())) return true; + + return getId() == device.getId(); } 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/device/GenericDeviceDAOImpl.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/device/GenericDeviceDAOImpl.java index e2063761db..bf539199c9 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/device/GenericDeviceDAOImpl.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/device/GenericDeviceDAOImpl.java @@ -457,6 +457,7 @@ public class GenericDeviceDAOImpl extends AbstractDeviceDAOImpl { "e.ID AS ENROLMENT_ID " + "FROM DM_ENROLMENT e, " + "(SELECT d.ID, " + + "d.LAST_UPDATED_TIMESTAMP, " + "d.DEVICE_IDENTIFICATION " + "FROM DM_DEVICE d WHERE d.TENANT_ID = ?) d1 " + "WHERE d1.ID = e.DEVICE_ID AND e.TENANT_ID = ? ";