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

Nipuni Kavindya 8 months ago
parent 918c604d72
commit a784e93ad5

@ -0,0 +1,59 @@
/*
* 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;
}
}

@ -31,6 +31,8 @@ public class DeviceSubscriptionDTO {
private String actionTriggeredFrom;
private String status;
private int deviceId;
private int appReleaseId;
private String appUuid;
public int getId() { return id; }
@ -71,4 +73,20 @@ public class DeviceSubscriptionDTO {
public int getDeviceId() { return deviceId; }
public void setDeviceId(int deviceId) { this.deviceId = deviceId; }
public int getAppReleaseId() {
return appReleaseId;
}
public void setAppReleaseId(int appReleaseId) {
this.appReleaseId = appReleaseId;
}
public String getAppUuid() {
return appUuid;
}
public void setAppUuid(String appUuid) {
this.appUuid = appUuid;
}
}

@ -0,0 +1,45 @@
/*
* 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 SubscriptionStatusDeviceCountsDTO {
private String status;
private DeviceCountsDTO deviceCounts;
public SubscriptionStatusDeviceCountsDTO(String status, DeviceCountsDTO deviceCounts) {
this.status = status;
this.deviceCounts = deviceCounts;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public DeviceCountsDTO getDeviceCounts() {
return deviceCounts;
}
public void setDeviceCounts(DeviceCountsDTO deviceCounts) {
this.deviceCounts = deviceCounts;
}
}

@ -23,6 +23,7 @@ import io.entgra.device.mgt.core.application.mgt.common.ExecutionStatus;
import io.entgra.device.mgt.core.application.mgt.common.SubscriptionType;
import io.entgra.device.mgt.core.application.mgt.common.dto.GroupSubscriptionDetailDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.ScheduledSubscriptionDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.SubscriptionStatusDeviceCountsDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.UserSubscriptionDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.RoleSubscriptionDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceOperationDTO;
@ -35,7 +36,6 @@ import io.entgra.device.mgt.core.device.mgt.common.app.mgt.App;
import io.entgra.device.mgt.core.device.mgt.common.operation.mgt.Activity;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
@ -264,4 +264,13 @@ public interface SubscriptionManager {
List<DeviceOperationDTO> getDeviceSubscriptionsOperationsByUUID(String uuid)
throws ApplicationManagementException;
/**
* This method is responsible for retrieving device counts details related to the given UUID.
*
* @param uuid the UUID of the application release.
* @return List of device subscription details.
* @throws SubscriptionManagementException if there is an error while fetching the details.
*/
List<SubscriptionStatusDeviceCountsDTO> getDeviceCountsForActionTypesByUUID(String uuid) throws ApplicationManagementException;
}

@ -374,4 +374,14 @@ public interface SubscriptionDAO {
*/
List<DeviceSubscriptionDTO> getDeviceSubscriptionsDetails(int appReleaseId, int tenantId)
throws ApplicationManagementDAOException;
/**
* This method is used to get the counts of devices for subscription types related to a UUID.
*
* @param uuid the UUID of the application release.
* @param tenantId id of the current tenant.
* @throws ApplicationManagementDAOException if connection establishment or SQL execution fails.
*/
List<DeviceSubscriptionDTO> getAppReleaseDevicesByActionType(String uuid, int tenantId)
throws ApplicationManagementDAOException;
}

@ -1892,5 +1892,50 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
}
}
@Override
public List<DeviceSubscriptionDTO> getAppReleaseDevicesByActionType(String uuid, int tenantId)
throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) {
log.debug("Getting device details for app UUID: " + uuid + " for tenant ID: " + tenantId);
}
String sql = "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 = ?";
try (Connection conn = this.getDBConnection();
PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setString(1, uuid);
ps.setInt(2, tenantId);
try (ResultSet rs = ps.executeQuery()) {
if (log.isDebugEnabled()) {
log.debug("Successfully retrieved device details for app UUID: " + uuid + " for tenant ID: " + tenantId);
}
List<DeviceSubscriptionDTO> deviceSubscriptionList = new ArrayList<>();
while (rs.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);
}
return deviceSubscriptionList;
}
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the DB connection for getting device 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;
log.error(msg, e);
throw new ApplicationManagementDAOException(msg, e);
}
}
}

@ -24,8 +24,10 @@ 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;
import io.entgra.device.mgt.core.application.mgt.common.dto.UserSubscriptionDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.RoleSubscriptionDTO;
import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceOperationDTO;
@ -2132,4 +2134,78 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
ConnectionManagerUtil.closeDBConnection();
}
}
@Override
public List<SubscriptionStatusDeviceCountsDTO> getDeviceCountsForActionTypesByUUID(String uuid) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
if (uuid == null || uuid.isEmpty()) {
throw new IllegalArgumentException("UUID cannot be null or empty.");
}
try {
ConnectionManagerUtil.openDBConnection();
List<DeviceSubscriptionDTO> deviceSubscriptions = subscriptionDAO.getAppReleaseDevicesByActionType(uuid, tenantId);
if (deviceSubscriptions == null || deviceSubscriptions.isEmpty()) {
throw new ApplicationManagementException("No device subscriptions found for app UUID: " + uuid);
}
DeviceCountsDTO subscribedCounts = new DeviceCountsDTO();
DeviceCountsDTO unsubscribedCounts = new DeviceCountsDTO();
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;
}
}
}
List<SubscriptionStatusDeviceCountsDTO> deviceCountsList = new ArrayList<>();
deviceCountsList.add(new SubscriptionStatusDeviceCountsDTO("subscribed", subscribedCounts));
deviceCountsList.add(new SubscriptionStatusDeviceCountsDTO("unsubscribed", unsubscribedCounts));
return deviceCountsList;
} catch (ApplicationManagementDAOException e) {
String msg = "Error occurred while retrieving device count for UUID: " + uuid;
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 (ApplicationManagementException e) {
throw new RuntimeException(e);
} finally {
ConnectionManagerUtil.closeDBConnection();
}
}
}

Loading…
Cancel
Save