add update & delete operations

pull/111/head
Thashmi-nil 2 years ago
parent e6d268c9c2
commit 003ddfc56a

@ -524,4 +524,12 @@ public interface ApplicationManager {
String getPlistArtifact(String uuid) throws ApplicationManagementException;
List<ApplicationReleaseDTO> getReleaseByPackageNames(List<String> packageIds) throws ApplicationManagementException;
/**
* @param applicationRelease {@link ApplicationRelease}
* @param oldPackageName Old package name of the application
* @throws ApplicationManagementException Application management exception
*/
void updateAppIconInfo(ApplicationRelease applicationRelease, String oldPackageName)
throws ApplicationManagementException;
}

@ -1318,9 +1318,10 @@ public class ApplicationManagerImpl implements ApplicationManager {
private void persistAppIconInfo(ApplicationReleaseDTO applicationReleaseDTO)
throws ApplicationManagementException {
try{
String iconPath = APIUtil.createAppIconPath(applicationReleaseDTO);
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String iconPath = APIUtil.createAppIconPath(applicationReleaseDTO, tenantId);
DataHolder.getInstance().getDeviceManagementService().saveApplicationIcon(iconPath,
String.valueOf(applicationReleaseDTO.getPackageName()), applicationReleaseDTO.getVersion());
String.valueOf(applicationReleaseDTO.getPackageName()), applicationReleaseDTO.getVersion(), tenantId);
} catch (ApplicationManagementException e) {
String msg = "Error occurred while creating iconPath";
log.error(msg, e);
@ -1378,8 +1379,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
}
ApplicationRelease applicationRelease = APIUtil.releaseDtoToRelease(applicationReleaseDTO);
DataHolder.getInstance().getDeviceManagementService().saveApplicationIcon(applicationReleaseDTO.getIconName(),
String.valueOf(applicationReleaseDTO.getPackageName()), applicationReleaseDTO.getVersion());
ConnectionManagerUtil.commitDBTransaction();
return applicationRelease;
} catch (TransactionManagementException e) {
@ -1403,8 +1402,6 @@ public class ApplicationManagerImpl implements ApplicationManager {
String msg = "Error occurred while adding new application release for application " + applicationDTO.getId();
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
} catch (DeviceManagementException e) {
throw new RuntimeException(e);
} finally {
ConnectionManagerUtil.closeDBConnection();
}
@ -1941,6 +1938,13 @@ public class ApplicationManagerImpl implements ApplicationManager {
break;
}
}
try {
deleteAppIconInfo(applicationDTO);
} catch (ApplicationManagementException e) {
String msg = "Error occurred while deleting application icon info";
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
}
}
@Override
@ -4045,4 +4049,26 @@ public class ApplicationManagerImpl implements ApplicationManager {
ConnectionManagerUtil.closeDBConnection();
}
}
@Override
public void updateAppIconInfo(ApplicationRelease applicationRelease, String oldPackageName) throws ApplicationManagementException {
try {
DataHolder.getInstance().getDeviceManagementService().updateApplicationIcon(applicationRelease.getIconPath(),
oldPackageName, applicationRelease.getPackageName(), applicationRelease.getVersion());
} catch (DeviceManagementException e) {
String msg = "Error occurred while updating application icon info";
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
}
}
private void deleteAppIconInfo(ApplicationDTO applicationDTO) throws ApplicationManagementException{
try {
DataHolder.getInstance().getDeviceManagementService().deleteApplicationIcon(applicationDTO.getPackageName());
} catch (DeviceManagementException e) {
String msg = "Error occurred while deleting application icon info";
log.error(msg, e);
throw new ApplicationManagementException(msg, e);
}
}
}

@ -511,8 +511,7 @@ public class APIUtil {
+ artifactDownloadEndpoint + Constants.FORWARD_SLASH;
}
public static String createAppIconPath(ApplicationReleaseDTO applicationReleaseDTO) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
public static String createAppIconPath(ApplicationReleaseDTO applicationReleaseDTO, int tenantId) throws ApplicationManagementException {
String basePath = getArtifactDownloadBaseURL() + tenantId + Constants.FORWARD_SLASH + applicationReleaseDTO
.getAppHashValue() + Constants.FORWARD_SLASH;
String iconPath = basePath + Constants.ICON_ARTIFACT + Constants.FORWARD_SLASH + applicationReleaseDTO.getIconName();

@ -531,6 +531,8 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem
log.error(msg);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
String oldPackageName = applicationManager.getApplicationByUuid(applicationUUID).getPackageName();
applicationManager.updateAppIconInfo(applicationRelease, oldPackageName);
return Response.status(Response.Status.OK).entity(applicationRelease).build();
} catch (BadRequestException e) {
String msg = e.getMessage();
@ -571,6 +573,8 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem
log.error(msg);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
String oldPackageName = applicationManager.getApplicationByUuid(applicationUUID).getPackageName();
applicationManager.updateAppIconInfo(applicationRelease, oldPackageName);
return Response.status(Response.Status.OK).entity(applicationRelease).build();
} catch (BadRequestException e) {
String msg = e.getMessage();

@ -72,7 +72,7 @@ public interface ApplicationDAO {
* @param version version of the application
* @throws DeviceManagementDAOException If any database error occurred
*/
void saveApplicationIcon(String iconPath, String packageName, String version) throws DeviceManagementDAOException;
void saveApplicationIcon(String iconPath, String packageName, String version, int tenantId) throws DeviceManagementDAOException;
/**
* This method is used to check the package existence.
@ -80,4 +80,21 @@ public interface ApplicationDAO {
* @throws DeviceManagementDAOException If any database error occurred
*/
int getApplicationPackageCount(String packageName) throws DeviceManagementDAOException;
/**
* This method is used to update application icon information.
* @param iconPath Icon path of the application
* @param oldPackageName Old package name of the application
* @param newPackageName New package name of the application
* @param version Version of the application
* @throws DeviceManagementDAOException If any database error occurred
*/
void updateApplicationIcon(String iconPath, String oldPackageName, String newPackageName, String version) throws DeviceManagementDAOException;
/**
* This method is used to delete application icon information.
* @param packageName Package name of the application
* @throws DeviceManagementDAOException If any database error occurred
*/
void deleteApplicationIcon(String packageName) throws DeviceManagementDAOException;
}

@ -31,11 +31,9 @@ import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
@ -396,17 +394,19 @@ public class ApplicationDAOImpl implements ApplicationDAO {
}
@Override
public void saveApplicationIcon(String iconPath, String packageName, String version)
public void saveApplicationIcon(String iconPath, String packageName, String version, int tenantId)
throws DeviceManagementDAOException{
Connection conn;
PreparedStatement stmt = null;
try{
conn = this.getConnection();
stmt = conn.prepareStatement("INSERT INTO DM_APP_ICONS (ICON_PATH, PACKAGE_NAME, VERSION) " +
"VALUES (?, ?, ?)");
stmt = conn.prepareStatement("INSERT INTO DM_APP_ICONS (ICON_PATH, PACKAGE_NAME, VERSION, CREATED_TIMESTAMP, TENANT_ID) " +
"VALUES (?, ?, ?, ?, ?)");
stmt.setString(1,iconPath);
stmt.setString(2,packageName);
stmt.setString(3,version);
stmt.setTimestamp(4, new Timestamp(new Date().getTime()));
stmt.setInt(5, tenantId);
stmt.executeUpdate();
} catch(SQLException e){
throw new DeviceManagementDAOException("Error occurred while saving application icon details");
@ -435,4 +435,45 @@ public class ApplicationDAOImpl implements ApplicationDAO {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
}
@Override
public void updateApplicationIcon(String iconPath, String oldPackageName, String newPackageName, String version)
throws DeviceManagementDAOException{
Connection conn;
PreparedStatement stmt = null;
try{
conn = this.getConnection();
stmt = conn.prepareStatement("UPDATE DM_APP_ICONS " +
"SET " +
"ICON_PATH= ?, " +
"PACKAGE_NAME = ?, " +
"VERSION = ? " +
"WHERE PACKAGE_NAME = ?");
stmt.setString(1,iconPath);
stmt.setString(2,newPackageName);
stmt.setString(3,version);
stmt.setString(4,oldPackageName);
stmt.executeUpdate();
} catch(SQLException e){
throw new DeviceManagementDAOException("Error occurred while updating application icon details");
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
}
@Override
public void deleteApplicationIcon(String packageName) throws DeviceManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
try{
conn = this.getConnection();
stmt = conn.prepareStatement("DELETE FROM DM_APP_ICONS WHERE PACKAGE_NAME = ?" );
stmt.setString(1, packageName);
stmt.executeUpdate();
} catch (SQLException e) {
throw new DeviceManagementDAOException("Error occurred while deleting application icon details");
} finally {
DeviceManagementDAOUtil.cleanupResources(stmt, null);
}
}
}

@ -1040,8 +1040,27 @@ 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)
void saveApplicationIcon(String iconPath, String packageName, String version, int tenantId)
throws DeviceManagementException;
/**
* This method is for updating application icon info
* @param iconPath Icon path of the application
* @param oldPackageName Old package name of the application
* @param newPackageName New package name of the application
* @param version Version of the application
* @throws DeviceManagementException if any service level or DAO level error occurs
*/
void updateApplicationIcon(String iconPath, String oldPackageName, String newPackageName, String version)
throws DeviceManagementException;
/**
* This method is for deleting application icon info
* @param packageName Package name of the application
* @throws DeviceManagementException if any service level or DAO level error occurs
*/
void deleteApplicationIcon(String packageName) throws DeviceManagementException;
}

@ -4926,11 +4926,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
}
@Override
public void saveApplicationIcon(String iconPath, String packageName, String version) throws DeviceManagementException{
public void saveApplicationIcon(String iconPath, String packageName, String version, int tenantId) throws DeviceManagementException{
try{
DeviceManagementDAOFactory.beginTransaction();
if(applicationDAO.getApplicationPackageCount(packageName) == 0){
applicationDAO.saveApplicationIcon(iconPath, packageName, version);
applicationDAO.saveApplicationIcon(iconPath, packageName, version, tenantId);
}
DeviceManagementDAOFactory.commitTransaction();
} catch (TransactionManagementException e) {
@ -4947,6 +4947,47 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
}
}
@Override
public void updateApplicationIcon(String iconPath, String oldPackageName, String newPackageName, String version)
throws DeviceManagementException{
try {
DeviceManagementDAOFactory.beginTransaction();
applicationDAO.updateApplicationIcon(iconPath, oldPackageName, newPackageName, version);
DeviceManagementDAOFactory.commitTransaction();
} catch (TransactionManagementException e) {
String msg = "Error occurred while initiating transaction";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while updating app icon info";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
@Override
public void deleteApplicationIcon(String packageName)
throws DeviceManagementException {
try {
DeviceManagementDAOFactory.beginTransaction();
applicationDAO.deleteApplicationIcon(packageName);
DeviceManagementDAOFactory.commitTransaction();
} catch (TransactionManagementException e) {
String msg = "Error occurred while initiating transaction";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while deleting app icon info";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
}
private void getInstalledAppIconInfo(Device device) throws DeviceManagementException {
List<Application> applications = device.getApplications();
String iconPath;
@ -4956,11 +4997,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
iconPath = deviceDAO.getIconPath(app.getApplicationIdentifier());
app.setImageUrl(iconPath);
}
DeviceManagementDAOFactory.commitTransaction();
} catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while retrieving installed app icon info";
log.error(msg, e);
throw new DeviceManagementException(msg, e);
} catch (SQLException e) {
DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while opening a connection to the data source";
log.error(msg);
throw new DeviceManagementException(msg, e);

Loading…
Cancel
Save