From e6ee539239ed9d625120d3fe8af358b72896758b Mon Sep 17 00:00:00 2001 From: osh Date: Wed, 9 Aug 2023 00:27:33 +0530 Subject: [PATCH] Add api changes for getting app details of operation --- .../common/services/SubscriptionManager.java | 10 ++++ .../mgt/core/dao/SubscriptionDAO.java | 12 +++++ .../GenericSubscriptionDAOImpl.java | 46 +++++++++++++++++++ .../core/impl/SubscriptionManagerImpl.java | 26 +++++++++++ .../api/ActivityInfoProviderService.java | 12 ++++- .../impl/ActivityProviderServiceImpl.java | 29 ++++++++++-- .../impl/ActivityProviderServiceImplTest.java | 2 +- .../mgt/common/operation/mgt/Activity.java | 46 +++++++++++++++++++ 8 files changed, 178 insertions(+), 5 deletions(-) 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 f2bd759fd7..e06a17cb9b 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 @@ -27,6 +27,7 @@ import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.PaginationResult; import io.entgra.device.mgt.core.device.mgt.common.app.mgt.App; +import io.entgra.device.mgt.core.device.mgt.common.operation.mgt.Activity; import java.util.List; import java.util.Properties; @@ -207,4 +208,13 @@ public interface SubscriptionManager { */ PaginationResult getAppInstalledSubscribeDevices(PaginationRequest request, String appUUID, String subType, String subTypeName) throws ApplicationManagementException; + + + /*** + * This method is responsible for retrieving application details of the passed operation id. + * @param id ID of the related operation + * @return {@link Activity} Activity result of the app information. + * @throws {@link SubscriptionManagementException} Exception of the subscription management + */ + Activity getOperationAppDetails(String id) throws SubscriptionManagementException; } 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 8b5caec6ac..804a4a29e2 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 @@ -23,6 +23,7 @@ import io.entgra.device.mgt.core.application.mgt.common.dto.DeviceSubscriptionDT import io.entgra.device.mgt.core.application.mgt.common.dto.ScheduledSubscriptionDTO; import io.entgra.device.mgt.core.application.mgt.common.exception.SubscriptionManagementException; import io.entgra.device.mgt.core.application.mgt.core.exception.ApplicationManagementDAOException; +import io.entgra.device.mgt.core.device.mgt.common.operation.mgt.Activity; import java.util.List; import java.util.Map; @@ -252,4 +253,15 @@ public interface SubscriptionDAO { * @throws {@link ApplicationManagementDAOException} if connections establishment fails. */ Map getCurrentInstalledAppVersion(int appId, List deviceIdList, String installedVersion) throws ApplicationManagementDAOException; + + + /** + * Retrieves app details by operation id. + * + * @param operationId ID of the operation which app details needs to be retrieved + * @param tenantId ID of tenant + * @return {@link Activity} + * @throws ApplicationManagementDAOException if error occurred while retrieving the app details + */ + Activity getOperationAppDetails(int operationId, int tenantId) 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 353340e3d3..373ade8595 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 @@ -21,6 +21,7 @@ import io.entgra.device.mgt.core.application.mgt.core.dao.SubscriptionDAO; import io.entgra.device.mgt.core.application.mgt.core.dao.impl.AbstractDAOImpl; import io.entgra.device.mgt.core.application.mgt.core.exception.UnexpectedServerErrorException; import io.entgra.device.mgt.core.application.mgt.core.util.DAOUtil; +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 io.entgra.device.mgt.core.application.mgt.common.ExecutionStatus; @@ -1435,4 +1436,49 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc throw new ApplicationManagementDAOException(msg, e); } } + + public Activity getOperationAppDetails(int operationId, int tenantId) throws ApplicationManagementDAOException { + Activity activity = null; + try { + String sql = "SELECT " + + "AP.NAME, " + + "AR.PACKAGE_NAME, " + + "DS.SUBSCRIBED_BY, " + + "DS.STATUS " + + "FROM AP_APP_SUB_OP_MAPPING SOP " + + "JOIN AP_DEVICE_SUBSCRIPTION DS ON SOP.AP_DEVICE_SUBSCRIPTION_ID = DS.ID " + + "JOIN AP_APP_RELEASE AR ON DS.AP_APP_RELEASE_ID = AR.ID " + + "JOIN AP_APP AP ON AP.ID = AR.AP_APP_ID " + + " WHERE SOP.OPERATION_ID = ? AND SOP.TENANT_ID = ?"; + + Connection conn = this.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + 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; + } + } + } catch (DBConnectionException e) { + String msg = + "Error occurred while getting the app details from the database related to operation " + operationId; + log.error(msg, e); + throw new ApplicationManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred when processing SQL to retrieve app details of 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/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 d89039dee5..0829906682 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 @@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.application.mgt.core.impl; import com.google.gson.Gson; import io.entgra.device.mgt.core.application.mgt.core.exception.BadRequestException; +import io.entgra.device.mgt.core.device.mgt.core.DeviceManagementConstants; import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger; import io.entgra.device.mgt.core.notification.logger.AppInstallLogContext; import io.entgra.device.mgt.core.notification.logger.impl.EntgraAppInstallLoggerImpl; @@ -1568,4 +1569,29 @@ public class SubscriptionManagerImpl implements SubscriptionManager { ConnectionManagerUtil.closeDBConnection(); } } + + @Override + public Activity getOperationAppDetails(String id) throws SubscriptionManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + int operationId = Integer.parseInt( + id.replace(DeviceManagementConstants.OperationAttributes.ACTIVITY, "")); + if (operationId == 0) { + throw new IllegalArgumentException("Operation ID cannot be null or zero (0)."); + } + try { + ConnectionManagerUtil.openDBConnection(); + return subscriptionDAO.getOperationAppDetails(operationId, tenantId); + } catch (ApplicationManagementDAOException e) { + String msg = "Error occurred while retrieving app details of operation: " + operationId; + log.error(msg, e); + throw new SubscriptionManagementException(msg, e); + } catch (DBConnectionException e) { + String msg = "Error occurred while retrieving the database connection"; + log.error(msg, e); + throw new SubscriptionManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + } 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/api/ActivityInfoProviderService.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/api/ActivityInfoProviderService.java index 99b56d9fbe..23076328e4 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/api/ActivityInfoProviderService.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/api/ActivityInfoProviderService.java @@ -308,7 +308,17 @@ public interface ActivityInfoProviderService { "Provide the value in the Java Date Format: EEE, d MMM yyyy HH:mm:ss Z\n." + "Example: Mon, 05 Jan 2014 15:10:00 +0200", required = false) - @HeaderParam("If-Modified-Since") String ifModifiedSince); + @HeaderParam("If-Modified-Since") String ifModifiedSince, + @ApiParam( + name = "response", + value = "The starting pagination index for the complete list of qualified items.", + required = false) + @QueryParam("response") Boolean response, + @ApiParam( + name = "appInstall", + value = "The starting pagination index for the complete list of qualified items.", + required = false) + @QueryParam("appInstall") Boolean appInstall); @GET @Path("/type/{operationCode}") 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 db2a8eab31..d4860d3dc3 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 @@ -18,6 +18,8 @@ package io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl; import com.google.gson.Gson; +import io.entgra.device.mgt.core.application.mgt.common.exception.SubscriptionManagementException; +import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionManager; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import io.entgra.device.mgt.core.device.mgt.common.ActivityPaginationRequest; @@ -146,8 +148,11 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService @Size(max = 45) String devicetype, @PathParam("deviceid") @Size(max = 45) String deviceid, - @HeaderParam("If-Modified-Since") String ifModifiedSince) { - Activity activity; + @HeaderParam("If-Modified-Since") String ifModifiedSince, + @QueryParam("response") Boolean response, + @QueryParam("appInstall") Boolean appInstall) { + Activity activity = new Activity(); + Activity appActivity = null; DeviceManagementProviderService dmService; try { RequestValidationUtil.validateActivityId(id); @@ -157,7 +162,20 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService deviceIdentifier.setType(devicetype); dmService = DeviceMgtAPIUtils.getDeviceManagementService(); - activity = dmService.getOperationByActivityIdAndDevice(id, deviceIdentifier); + + if (appInstall != null && appInstall) { + if (response != null && response) { + activity = dmService.getOperationByActivityIdAndDevice(id, deviceIdentifier); + } + SubscriptionManager subscriptionManager = DeviceMgtAPIUtils.getSubscriptionManager(); + appActivity = subscriptionManager.getOperationAppDetails(id); + activity.setUsername(appActivity.getUsername()); + activity.setPackageName(appActivity.getPackageName()); + activity.setAppName(appActivity.getAppName()); + activity.setStatus(appActivity.getStatus()); + } else { + activity = dmService.getOperationByActivityIdAndDevice(id, deviceIdentifier); + } if (activity == null) { String msg = "No activity can be " + "found upon the provided activity id '" + id + "'"; @@ -169,6 +187,11 @@ public class ActivityProviderServiceImpl implements ActivityInfoProviderService log.error(msg, e); return Response.serverError().entity( new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } catch (SubscriptionManagementException e) { + String msg = "ErrorResponse occurred while fetching the app details for the supplied id."; + log.error(msg, e); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/ActivityProviderServiceImplTest.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/ActivityProviderServiceImplTest.java index f56c673495..0e3d23d705 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/ActivityProviderServiceImplTest.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/src/test/java/io/entgra/device/mgt/core/device/mgt/api/jaxrs/service/impl/ActivityProviderServiceImplTest.java @@ -206,7 +206,7 @@ public class ActivityProviderServiceImplTest { Mockito.when(this.deviceManagementProviderService .getOperationByActivityIdAndDevice(TEST_ACTIVITY_ID, deviceIdentifier)).thenReturn(activity); Response response = this.activityInfoProviderService.getActivityByDevice(TEST_ACTIVITY_ID, - DEVICE_TYPE, DEVICE_ID, IF_MODIFIED_SINCE); + DEVICE_TYPE, DEVICE_ID, IF_MODIFIED_SINCE, false, false); Assert.assertNotNull(response); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); Mockito.reset(this.deviceManagementProviderService); 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/operation/mgt/Activity.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/operation/mgt/Activity.java index 5a04185e0a..e9874778d9 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/operation/mgt/Activity.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/operation/mgt/Activity.java @@ -80,6 +80,20 @@ public class Activity { @JsonProperty("initiatedBy") private String initiatedBy; + @ApiModelProperty(name = "appName", value = "App Name.") + private String appName; + + @ApiModelProperty(name = "packageName", + value = "package name of the application") + private String packageName; + @ApiModelProperty(name = "username", + value = "username of subscribed person") + private String username; + @ApiModelProperty(name = "status", + value = "Status of app install") + private String status; + + public String getActivityId() { return activityId; } @@ -127,5 +141,37 @@ public class Activity { public void setInitiatedBy(String initiatedBy) { this.initiatedBy = initiatedBy; } + + public String getAppName() { + return appName; + } + + public void setAppName(String appName) { + this.appName = appName; + } + + public String getPackageName() { + return packageName; + } + + public void setPackageName(String packageName) { + this.packageName = packageName; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } }