Merge pull request 'Add method for get installed applications using device ID' (#299) from pramilaniroshan/device-mgt-core:rm-10291 into master

Reviewed-on: community/device-mgt-core#299
rm-10583
Pahansith Gunathilake 9 months ago
commit dfcec7cf3e

@ -1333,7 +1333,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String iconPath = APIUtil.createAppIconPath(applicationReleaseDTO, tenantId);
DataHolder.getInstance().getDeviceManagementService().saveApplicationIcon(iconPath,
String.valueOf(applicationReleaseDTO.getPackageName()), applicationReleaseDTO.getVersion(), tenantId);
String.valueOf(applicationReleaseDTO.getPackageName()), applicationReleaseDTO.getVersion());
} catch (ApplicationManagementException e) {
String msg = "Error occurred while creating iconPath. Application package name : " + applicationReleaseDTO.getPackageName();
log.error(msg, e);

@ -114,4 +114,14 @@ public interface ApplicationDAO {
* @throws DeviceManagementDAOException
*/
String getIconPath(String applicationIdentifier) throws DeviceManagementDAOException;
/**
* This method is used to get the installed application list of a specific device
* @param deviceId ID of the device
* @param enrolmentId Enrolment ID of the device
* @param tenantId tenant ID
* @throws DeviceManagementDAOException If any database error occurred
*/
List<Application> getInstalledApplicationListOnDevice(int deviceId, int enrolmentId, int tenantId)
throws DeviceManagementDAOException;
}

@ -575,4 +575,50 @@ public class ApplicationDAOImpl implements ApplicationDAO {
}
return applicationList;
}
public List<Application> getInstalledApplicationListOnDevice(int deviceId, int enrolmentId, int tenantId)
throws DeviceManagementDAOException {
Connection conn;
List<Application> applicationList = new ArrayList<>();
Application application;
String sql = "SELECT " +
"ID, " +
"NAME, " +
"APP_IDENTIFIER, " +
"PLATFORM, " +
"CATEGORY, " +
"VERSION, " +
"TYPE, " +
"LOCATION_URL, " +
"IMAGE_URL, " +
"APP_PROPERTIES, " +
"MEMORY_USAGE, " +
"IS_ACTIVE, " +
"TENANT_ID " +
"FROM DM_APPLICATION " +
"WHERE DEVICE_ID = ? AND " +
"ENROLMENT_ID = ? AND " +
"TENANT_ID = ? ";
try {
conn = this.getConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, deviceId);
stmt.setInt(2, enrolmentId);
stmt.setInt(3, tenantId);
try (ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
application = loadApplication(rs);
applicationList.add(application);
}
}
}
} catch (SQLException e) {
String msg = "SQL Error occurred while retrieving the list of Applications " +
"installed in device id '" + deviceId;
log.error(msg, e);
throw new DeviceManagementDAOException(msg, e);
}
return applicationList;
}
}

@ -1030,10 +1030,9 @@ public interface DeviceManagementProviderService {
* @param iconPath Icon path of the application
* @param packageName Package name of the application
* @param version Version of the application
* @param tenantId Tenant ID of the application created user
* @throws DeviceManagementException if any service level or DAO level error occurs
*/
void saveApplicationIcon(String iconPath, String packageName, String version, int tenantId)
void saveApplicationIcon(String iconPath, String packageName, String version)
throws DeviceManagementException;
/**
@ -1062,4 +1061,12 @@ public interface DeviceManagementProviderService {
*/
List<Application> getInstalledApplicationsOnDevice(Device device, int offset, int limit)
throws DeviceManagementException;
/**
* This method is for getting the installed application list of a device
* @param device {@link Device}
* @return list of applications {@link Application}
* @throws DeviceManagementException if any service level or DAO level error occurs
*/
List<Application> getInstalledApplicationsOnDevice(Device device) throws DeviceManagementException;
}

@ -4995,9 +4995,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
}
@Override
public void saveApplicationIcon(String iconPath, String packageName, String version, int tenantId) throws DeviceManagementException{
public void saveApplicationIcon(String iconPath, String packageName, String version) throws DeviceManagementException{
int tenantId = 0;
try{
DeviceManagementDAOFactory.beginTransaction();
tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
if(applicationDAO.getApplicationPackageCount(packageName) == 0){
applicationDAO.saveApplicationIcon(iconPath, packageName, version, tenantId);
}
@ -5119,4 +5121,38 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
}
return newApplicationList;
}
public List<Application> getInstalledApplicationsOnDevice(Device device) throws DeviceManagementException {
List<Application> applications;
try {
DeviceManagementDAOFactory.openConnection();
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
applications = applicationDAO.getInstalledApplicationListOnDevice(device.getId(),
device.getEnrolmentInfo().getId(), tenantId);
if (applications == null) {
String msg = "Couldn't found applications for device identifier '" + device.getId() + "'";
log.error(msg);
throw new DeviceManagementException(msg);
}
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while retrieving the application list of android device, " +
"which carries the id '" + device.getId() + "'";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while opening a connection to the data source";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
List<Application> newApplicationList;
newApplicationList = this.getInstalledAppIconInfo(applications);
if (newApplicationList == null) {
String msg = "Error occurred while getting app icon info for device identifier '" + device.getId() + "'";
log.error(msg);
throw new DeviceManagementException(msg);
}
return newApplicationList;
}
}

Loading…
Cancel
Save