DAO and Services for APIs to populate the installation details static UI.

Nipuni Kavindya 6 months ago
parent a784e93ad5
commit 17a594d848

@ -1,59 +0,0 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package io.entgra.device.mgt.core.application.mgt.common.dto;
public class DeviceCountsDTO {
private int group;
private int role;
private int user;
private int device;
public int getGroup() {
return group;
}
public void setGroup(int group) {
this.group = group;
}
public int getRole() {
return role;
}
public void setRole(int role) {
this.role = role;
}
public int getUser() {
return user;
}
public void setUser(int user) {
this.user = user;
}
public int getDevice() {
return device;
}
public void setDevice(int device) {
this.device = device;
}
}

@ -19,27 +19,37 @@
package io.entgra.device.mgt.core.application.mgt.common.dto;
public class SubscriptionStatusDeviceCountsDTO {
private String status;
private DeviceCountsDTO deviceCounts;
private String subscriptionType;
private int subscriptionCount;
private int unsubscriptionCount;
public SubscriptionStatusDeviceCountsDTO(String status, DeviceCountsDTO deviceCounts) {
this.status = status;
this.deviceCounts = deviceCounts;
public SubscriptionStatusDeviceCountsDTO(String subscriptionType, int subscriptionCount, int unsubscriptionCount) {
this.subscriptionType = subscriptionType;
this.subscriptionCount = subscriptionCount;
this.unsubscriptionCount = unsubscriptionCount;
}
public String getStatus() {
return status;
public String getSubscriptionType() {
return subscriptionType;
}
public void setStatus(String status) {
this.status = status;
public void setSubscriptionType(String subscriptionType) {
this.subscriptionType = subscriptionType;
}
public DeviceCountsDTO getDeviceCounts() {
return deviceCounts;
public int getSubscriptionCount() {
return subscriptionCount;
}
public void setDeviceCounts(DeviceCountsDTO deviceCounts) {
this.deviceCounts = deviceCounts;
public void setSubscriptionCount(int subscriptionCount) {
this.subscriptionCount = subscriptionCount;
}
public int getUnsubscriptionCount() {
return unsubscriptionCount;
}
public void setUnsubscriptionCount(int unsubscriptionCount) {
this.unsubscriptionCount = unsubscriptionCount;
}
}

@ -271,6 +271,7 @@ public interface SubscriptionManager {
* @return List of device subscription details.
* @throws SubscriptionManagementException if there is an error while fetching the details.
*/
List<SubscriptionStatusDeviceCountsDTO> getDeviceCountsForActionTypesByUUID(String uuid) throws ApplicationManagementException;
List<SubscriptionStatusDeviceCountsDTO> getSubscriptionCountsForActionTypesByUUID(String uuid)
throws ApplicationManagementException;
}

@ -382,6 +382,6 @@ public interface SubscriptionDAO {
* @param tenantId id of the current tenant.
* @throws ApplicationManagementDAOException if connection establishment or SQL execution fails.
*/
List<DeviceSubscriptionDTO> getAppReleaseDevicesByActionType(String uuid, int tenantId)
List<DeviceSubscriptionDTO> getSubscriptionDetailsForActionTypes(String uuid, int tenantId)
throws ApplicationManagementDAOException;
}

@ -1893,12 +1893,12 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
}
@Override
public List<DeviceSubscriptionDTO> getAppReleaseDevicesByActionType(String uuid, int tenantId)
public List<DeviceSubscriptionDTO> getSubscriptionDetailsForActionTypes(String uuid, int tenantId)
throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Getting device details for app UUID: " + uuid + " for tenant ID: " + tenantId);
log.debug("Getting subscription count details for app UUID: " + uuid + " for tenant ID: " + tenantId);
}
String sql = "SELECT "
String allDeviceSql = "SELECT "
+ "DS.ACTION_TRIGGERED_FROM AS ACTION_TRIGGERED_FROM, "
+ "DS.DM_DEVICE_ID AS DEVICE_ID, "
+ "DS.UNSUBSCRIBED AS UNSUBSCRIBED "
@ -1906,34 +1906,117 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
+ "INNER JOIN AP_DEVICE_SUBSCRIPTION DS ON AR.ID = DS.AP_APP_RELEASE_ID "
+ "WHERE AR.UUID = ? AND DS.TENANT_ID = ?";
String deviceSql = "SELECT "
+ "DS.ACTION_TRIGGERED_FROM AS ACTION_TRIGGERED_FROM, "
+ "DS.DM_DEVICE_ID AS DEVICE_ID, "
+ "DS.UNSUBSCRIBED AS UNSUBSCRIBED "
+ "FROM AP_APP_RELEASE AR "
+ "INNER JOIN AP_DEVICE_SUBSCRIPTION DS ON AR.ID = DS.AP_APP_RELEASE_ID "
+ "WHERE AR.UUID = ? AND DS.TENANT_ID = ? AND DS.ACTION_TRIGGERED_FROM = 'DEVICE'";
String groupSql = "SELECT "
+ "DS.UNSUBSCRIBED AS UNSUBSCRIBED "
+ "FROM AP_APP_RELEASE AR "
+ "INNER JOIN AP_GROUP_SUBSCRIPTION DS ON AR.ID = DS.AP_APP_RELEASE_ID "
+ "WHERE AR.UUID = ? AND DS.TENANT_ID = ?";
String roleSql = "SELECT "
+ "DS.UNSUBSCRIBED AS UNSUBSCRIBED "
+ "FROM AP_APP_RELEASE AR "
+ "INNER JOIN AP_ROLE_SUBSCRIPTION DS ON AR.ID = DS.AP_APP_RELEASE_ID "
+ "WHERE AR.UUID = ? AND DS.TENANT_ID = ?";
String userSql = "SELECT "
+ "DS.UNSUBSCRIBED AS UNSUBSCRIBED "
+ "FROM AP_APP_RELEASE AR "
+ "INNER JOIN AP_USER_SUBSCRIPTION DS ON AR.ID = DS.AP_APP_RELEASE_ID "
+ "WHERE AR.UUID = ? AND DS.TENANT_ID = ?";
try (Connection conn = this.getDBConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {
PreparedStatement allDevicePs = conn.prepareStatement(allDeviceSql);
PreparedStatement devicePs = conn.prepareStatement(deviceSql);
PreparedStatement groupPs = conn.prepareStatement(groupSql);
PreparedStatement rolePs = conn.prepareStatement(roleSql);
PreparedStatement userPs = conn.prepareStatement(userSql)) {
allDevicePs.setString(1, uuid);
allDevicePs.setInt(2, tenantId);
devicePs.setString(1, uuid);
devicePs.setInt(2, tenantId);
groupPs.setString(1, uuid);
groupPs.setInt(2, tenantId);
rolePs.setString(1, uuid);
rolePs.setInt(2, tenantId);
userPs.setString(1, uuid);
userPs.setInt(2, tenantId);
List<DeviceSubscriptionDTO> allDeviceSubscriptions = new ArrayList<>();
List<DeviceSubscriptionDTO> deviceSubscriptions = new ArrayList<>();
List<DeviceSubscriptionDTO> groupSubscriptions = new ArrayList<>();
List<DeviceSubscriptionDTO> roleSubscriptions = new ArrayList<>();
List<DeviceSubscriptionDTO> userSubscriptions = new ArrayList<>();
try (ResultSet allDeviceRs = allDevicePs.executeQuery()) {
while (allDeviceRs.next()) {
DeviceSubscriptionDTO details = new DeviceSubscriptionDTO();
details.setActionTriggeredFrom("ALL");
details.setUnsubscribed(allDeviceRs.getBoolean("UNSUBSCRIBED"));
allDeviceSubscriptions.add(details);
}
}
ps.setString(1, uuid);
ps.setInt(2, tenantId);
try (ResultSet deviceRs = devicePs.executeQuery()) {
while (deviceRs.next()) {
DeviceSubscriptionDTO details = new DeviceSubscriptionDTO();
details.setActionTriggeredFrom("DEVICE");
details.setUnsubscribed(deviceRs.getBoolean("UNSUBSCRIBED"));
deviceSubscriptions.add(details);
}
}
try (ResultSet rs = ps.executeQuery()) {
if (log.isDebugEnabled()) {
log.debug("Successfully retrieved device details for app UUID: " + uuid + " for tenant ID: " + tenantId);
try (ResultSet groupRs = groupPs.executeQuery()) {
while (groupRs.next()) {
DeviceSubscriptionDTO details = new DeviceSubscriptionDTO();
details.setActionTriggeredFrom("GROUP");
details.setUnsubscribed(groupRs.getBoolean("UNSUBSCRIBED"));
groupSubscriptions.add(details);
}
List<DeviceSubscriptionDTO> deviceSubscriptionList = new ArrayList<>();
}
try (ResultSet roleRs = rolePs.executeQuery()) {
while (roleRs.next()) {
DeviceSubscriptionDTO details = new DeviceSubscriptionDTO();
details.setActionTriggeredFrom("ROLE");
details.setUnsubscribed(roleRs.getBoolean("UNSUBSCRIBED"));
roleSubscriptions.add(details);
}
}
while (rs.next()) {
try (ResultSet userRs = userPs.executeQuery()) {
while (userRs.next()) {
DeviceSubscriptionDTO details = new DeviceSubscriptionDTO();
details.setActionTriggeredFrom(rs.getString("ACTION_TRIGGERED_FROM"));
details.setDeviceId(rs.getInt("DEVICE_ID"));
details.setUnsubscribed(rs.getBoolean("UNSUBSCRIBED"));
deviceSubscriptionList.add(details);
details.setActionTriggeredFrom("USER");
details.setUnsubscribed(userRs.getBoolean("UNSUBSCRIBED"));
userSubscriptions.add(details);
}
return deviceSubscriptionList;
}
List<DeviceSubscriptionDTO> allSubscriptions = new ArrayList<>();
allSubscriptions.addAll(allDeviceSubscriptions);
allSubscriptions.addAll(deviceSubscriptions);
allSubscriptions.addAll(groupSubscriptions);
allSubscriptions.addAll(roleSubscriptions);
allSubscriptions.addAll(userSubscriptions);
return allSubscriptions;
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the DB connection for getting device details for app UUID: "
String msg = "Error occurred while obtaining the DB connection for getting subscription count details for app UUID: "
+ uuid + ".";
log.error(msg, e);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while executing SQL to get device details for app UUID: " + uuid;
String msg = "Error occurred while executing SQL to get subscription count details for app UUID: " + uuid;
log.error(msg, e);
throw new ApplicationManagementDAOException(msg, e);
}

@ -24,7 +24,6 @@ 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.dto.DeviceCountsDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.GroupSubscriptionDetailDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatusDeviceCountsDTO;
@ -2136,7 +2135,8 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
}
@Override
public List<SubscriptionStatusDeviceCountsDTO> getDeviceCountsForActionTypesByUUID(String uuid) throws ApplicationManagementException {
public List<SubscriptionStatusDeviceCountsDTO> getSubscriptionCountsForActionTypesByUUID(String uuid)
throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
if (uuid == null || uuid.isEmpty()) {
throw new IllegalArgumentException("UUID cannot be null or empty.");
@ -2144,58 +2144,78 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
try {
ConnectionManagerUtil.openDBConnection();
List<DeviceSubscriptionDTO> deviceSubscriptions = subscriptionDAO.getAppReleaseDevicesByActionType(uuid, tenantId);
List<DeviceSubscriptionDTO> deviceSubscriptions = subscriptionDAO.getSubscriptionDetailsForActionTypes(uuid, tenantId);
if (deviceSubscriptions == null || deviceSubscriptions.isEmpty()) {
throw new ApplicationManagementException("No device subscriptions found for app UUID: " + uuid);
throw new ApplicationManagementException("No subscriptions found for app UUID: " + uuid);
}
DeviceCountsDTO subscribedCounts = new DeviceCountsDTO();
DeviceCountsDTO unsubscribedCounts = new DeviceCountsDTO();
int deviceSubscriptionCount = 0;
int deviceUnsubscriptionCount = 0;
int groupSubscriptionCount = 0;
int groupUnsubscriptionCount = 0;
int roleSubscriptionCount = 0;
int roleUnsubscriptionCount = 0;
int userSubscriptionCount = 0;
int userUnsubscriptionCount = 0;
int allSubscriptionCount = 0;
int allUnsubscriptionCount = 0;
for (DeviceSubscriptionDTO subscription : deviceSubscriptions) {
String actionTriggeredFrom = subscription.getActionTriggeredFrom().toLowerCase();
if (subscription.isUnsubscribed()) {
switch (actionTriggeredFrom) {
case "group":
unsubscribedCounts.setGroup(unsubscribedCounts.getGroup() + 1);
break;
case "role":
unsubscribedCounts.setRole(unsubscribedCounts.getRole() + 1);
break;
case "user":
unsubscribedCounts.setUser(unsubscribedCounts.getUser() + 1);
break;
case "device":
unsubscribedCounts.setDevice(unsubscribedCounts.getDevice() + 1);
break;
}
} else {
switch (actionTriggeredFrom) {
case "group":
subscribedCounts.setGroup(subscribedCounts.getGroup() + 1);
break;
case "role":
subscribedCounts.setRole(subscribedCounts.getRole() + 1);
break;
case "user":
subscribedCounts.setUser(subscribedCounts.getUser() + 1);
break;
case "device":
subscribedCounts.setDevice(subscribedCounts.getDevice() + 1);
break;
}
switch (subscription.getActionTriggeredFrom()) {
case "ALL":
if (subscription.isUnsubscribed()) {
allUnsubscriptionCount++;
} else {
allSubscriptionCount++;
}
break;
case "DEVICE":
if (subscription.isUnsubscribed()) {
deviceUnsubscriptionCount++;
} else {
deviceSubscriptionCount++;
}
break;
case "GROUP":
if (subscription.isUnsubscribed()) {
groupUnsubscriptionCount++;
} else {
groupSubscriptionCount++;
}
break;
case "ROLE":
if (subscription.isUnsubscribed()) {
roleUnsubscriptionCount++;
} else {
roleSubscriptionCount++;
}
break;
case "USER":
if (subscription.isUnsubscribed()) {
userUnsubscriptionCount++;
} else {
userSubscriptionCount++;
}
break;
}
}
List<SubscriptionStatusDeviceCountsDTO> deviceCountsList = new ArrayList<>();
deviceCountsList.add(new SubscriptionStatusDeviceCountsDTO("subscribed", subscribedCounts));
deviceCountsList.add(new SubscriptionStatusDeviceCountsDTO("unsubscribed", unsubscribedCounts));
List<SubscriptionStatusDeviceCountsDTO> subscriptionCounts = new ArrayList<>();
subscriptionCounts.add(new SubscriptionStatusDeviceCountsDTO(
"ALL", allSubscriptionCount, allUnsubscriptionCount));
subscriptionCounts.add(new SubscriptionStatusDeviceCountsDTO(
"DEVICE", deviceSubscriptionCount, deviceUnsubscriptionCount));
subscriptionCounts.add(new SubscriptionStatusDeviceCountsDTO(
"GROUP", groupSubscriptionCount, groupUnsubscriptionCount));
subscriptionCounts.add(new SubscriptionStatusDeviceCountsDTO(
"ROLE", roleSubscriptionCount, roleUnsubscriptionCount));
subscriptionCounts.add(new SubscriptionStatusDeviceCountsDTO(
"USER", userSubscriptionCount, userUnsubscriptionCount));
return deviceCountsList;
return subscriptionCounts;
} catch (ApplicationManagementDAOException e) {
String msg = "Error occurred while retrieving device count for UUID: " + uuid;
String msg = "Error occurred while retrieving subscription count for UUID: " + uuid;
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (DBConnectionException e) {

Loading…
Cancel
Save