Merge pull request 'Retrieve app details of Operation' (#203) from osh.silva/device-mgt-core:app-install-9211 into master

Reviewed-on: community/device-mgt-core#203
remotes/1712966534876109980/master
Inosh Perara 1 year ago
commit 8907903e26

@ -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;
}

@ -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<Integer,String> getCurrentInstalledAppVersion(int appId, List<Integer> 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;
}

@ -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,44 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
throw new ApplicationManagementDAOException(msg, e);
}
}
public Activity getOperationAppDetails(int operationId, int tenantId) throws ApplicationManagementDAOException {
try {
String sql = "SELECT "
+ "AP.NAME, "
+ "AP.TYPE, "
+ "AR.PACKAGE_NAME, "
+ "AR.VERSION, "
+ "DS.SUBSCRIBED_BY, "
+ "DS.STATUS, "
+ "DS.ACTION_TRIGGERED_FROM "
+ "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()) {
return DAOUtil.loadOperationActivity(rs);
}
}
} 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);
} catch (UnexpectedServerErrorException e) {
String msg = "More than one app for operation " + operationId;
log.error(msg, e);
throw new ApplicationManagementDAOException(msg, e);
}
}
}

@ -24,6 +24,7 @@ import io.entgra.device.mgt.core.application.mgt.common.dto.VppUserDTO;
import io.entgra.device.mgt.core.application.mgt.common.services.VPPApplicationManager;
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.device.mgt.core.DeviceManagementConstants;
import io.entgra.device.mgt.core.application.mgt.core.exception.UnexpectedServerErrorException;
import io.entgra.device.mgt.core.application.mgt.core.util.VppHttpUtil;
import io.entgra.device.mgt.core.device.mgt.extensions.logger.spi.EntgraLogger;
@ -1622,4 +1623,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();
}
}
}

@ -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,35 @@ public class DAOUtil {
return subscriptionDTOS;
}
public static Activity loadOperationActivity(ResultSet rs) throws SQLException, UnexpectedServerErrorException {
List<Activity> 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<Activity> loadOperationActivities (ResultSet rs) throws SQLException {
List<Activity> activities = new ArrayList<>();
while (rs.next()) {
Activity activity = new Activity();
activity.setAppName(rs.getString("NAME"));
activity.setAppType(rs.getString("TYPE"));
activity.setUsername(rs.getString("SUBSCRIBED_BY"));
activity.setPackageName(rs.getString("PACKAGE_NAME"));
activity.setStatus(rs.getString("STATUS"));
activity.setVersion(rs.getString("VERSION"));
activity.setTriggeredBy(rs.getString("ACTION_TRIGGERED_FROM"));
activities.add(activity);
}
return activities;
}
public static VppUserDTO loadVppUser(ResultSet rs) throws SQLException, UnexpectedServerErrorException {
List<VppUserDTO> vppUserDTOS = loadVppUsers(rs);
if (vppUserDTOS.isEmpty()) {

@ -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}")

@ -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,29 @@ 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);
if (appActivity != null) {
activity.setUsername(appActivity.getUsername());
activity.setPackageName(appActivity.getPackageName());
activity.setAppName(appActivity.getAppName());
activity.setStatus(appActivity.getStatus());
activity.setAppType(appActivity.getAppType());
activity.setVersion(appActivity.getVersion());
activity.setTriggeredBy(appActivity.getTriggeredBy());
} 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);
}
if (activity == null) {
String msg = "No activity can be " +
"found upon the provided activity id '" + id + "'";
@ -169,6 +196,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();
}
}

@ -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);

@ -80,6 +80,32 @@ 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;
@ApiModelProperty(name = "version",
value = "Version of app")
private String version;
@ApiModelProperty(name = "triggeredBy",
value = "Operation triggered by what")
private String triggeredBy;
@ApiModelProperty(name = "appType",
value = "Type of application")
private String appType;
public String getActivityId() {
return activityId;
}
@ -127,5 +153,61 @@ 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;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getTriggeredBy() {
return triggeredBy;
}
public void setTriggeredBy(String triggeredBy) {
this.triggeredBy = triggeredBy;
}
public String getAppType() {
return appType;
}
public void setAppType(String appType) {
this.appType = appType;
}
}

@ -241,6 +241,10 @@
<Scope>perm:ios:app-configurations</Scope>
<Scope>perm:mac-os:restart</Scope>
<Scope>perm:mac-os:shut-down</Scope>
<Scope>perm:app:vpp:user:modify</Scope>
<Scope>perm:app:vpp:user:view</Scope>
<Scope>perm:app:vpp:asset:modify</Scope>
<Scope>perm:app:vpp:asset:view</Scope>
</Scopes>
<SSOConfiguration>
<Issuer>device-mgt</Issuer>

Loading…
Cancel
Save