From e2a467bc455c7b960a7f244bdffc0529b9628d1d Mon Sep 17 00:00:00 2001 From: osh Date: Wed, 9 Aug 2023 15:03:29 +0530 Subject: [PATCH] Update activity loading --- .../GenericSubscriptionDAOImpl.java | 18 ++++--------- .../application/mgt/core/util/DAOUtil.java | 27 +++++++++++++++++++ .../impl/ActivityProviderServiceImpl.java | 14 +++++++--- 3 files changed, 42 insertions(+), 17 deletions(-) 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 373ade85958..429ecc1e351 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 @@ -1438,7 +1438,6 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc } public Activity getOperationAppDetails(int operationId, int tenantId) throws ApplicationManagementDAOException { - Activity activity = null; try { String sql = "SELECT " + "AP.NAME, " @@ -1456,18 +1455,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc stmt.setInt(1, operationId); stmt.setInt(2,tenantId); try (ResultSet rs = stmt.executeQuery()) { - if (log.isDebugEnabled()) { - log.debug("Successfully retrieved app details for operation " - + operationId); - } - while (rs.next()) { - activity = new Activity(); - activity.setAppName(rs.getString("NAME")); - activity.setUsername(rs.getString("SUBSCRIBED_BY")); - activity.setPackageName(rs.getString("PACKAGE_NAME")); - activity.setStatus(rs.getString("STATUS")); - } - return activity; + return DAOUtil.loadOperationActivity(rs); } } } catch (DBConnectionException e) { @@ -1479,6 +1467,10 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc String msg = "Error occurred when processing SQL to retrieve app details of operation" + operationId; log.error(msg, e); throw new ApplicationManagementDAOException(msg, e); + } catch (UnexpectedServerErrorException e) { + String msg = "More than one app for operation " + operationId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); } } } 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/DAOUtil.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/DAOUtil.java index 08ebe3b60d3..ede8d7ff6d6 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/DAOUtil.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/DAOUtil.java @@ -21,6 +21,7 @@ import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import io.entgra.device.mgt.core.application.mgt.common.dto.*; import io.entgra.device.mgt.core.application.mgt.core.exception.UnexpectedServerErrorException; +import io.entgra.device.mgt.core.device.mgt.common.operation.mgt.Activity; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.json.JSONException; @@ -361,6 +362,32 @@ public class DAOUtil { return subscriptionDTOS; } + public static Activity loadOperationActivity(ResultSet rs) throws SQLException, UnexpectedServerErrorException { + List activity = loadOperationActivities(rs); + if (activity.isEmpty()) { + return null; + } + if (activity.size() > 1) { + String msg = "Internal server error. Found more than one app for operation"; + log.error(msg); + throw new UnexpectedServerErrorException(msg); + } + return activity.get(0); + } + + public static List loadOperationActivities (ResultSet rs) throws SQLException { + List activities = new ArrayList<>(); + while (rs.next()) { + Activity activity = new Activity(); + activity.setAppName(rs.getString("NAME")); + activity.setUsername(rs.getString("SUBSCRIBED_BY")); + activity.setPackageName(rs.getString("PACKAGE_NAME")); + activity.setStatus(rs.getString("STATUS")); + activities.add(activity); + } + return activities; + } + public static VppUserDTO loadVppUser(ResultSet rs) throws SQLException, UnexpectedServerErrorException { List vppUserDTOS = loadVppUsers(rs); if (vppUserDTOS.isEmpty()) { diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/ActivityProviderServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/ActivityProviderServiceImpl.java index d4860d3dc31..60272d589f7 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/ActivityProviderServiceImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/main/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/ActivityProviderServiceImpl.java @@ -169,10 +169,16 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService } SubscriptionManager subscriptionManager = DeviceMgtAPIUtils.getSubscriptionManager(); appActivity = subscriptionManager.getOperationAppDetails(id); - activity.setUsername(appActivity.getUsername()); - activity.setPackageName(appActivity.getPackageName()); - activity.setAppName(appActivity.getAppName()); - activity.setStatus(appActivity.getStatus()); + if (appActivity != null) { + activity.setUsername(appActivity.getUsername()); + activity.setPackageName(appActivity.getPackageName()); + activity.setAppName(appActivity.getAppName()); + activity.setStatus(appActivity.getStatus()); + } else { + String msg = "Cannot find the app details related to the operation "; + log.error(msg); + Response.status(404).entity(msg).build(); + } } else { activity = dmService.getOperationByActivityIdAndDevice(id, deviceIdentifier); }