Merge branch 'application-mgt-new' of https://gitlab.com/tcdlpds/carbon-device-mgt into application-mgt-new

feature/appm-store/pbac
Jayasanka 6 years ago
commit 07e7d96174

@ -59,14 +59,18 @@ public interface ApplicationDAO {
void deleteTagMapping (List<Integer> tagIds, int applicationId, int tenantId) throws ApplicationManagementDAOException; void deleteTagMapping (List<Integer> tagIds, int applicationId, int tenantId) throws ApplicationManagementDAOException;
void deleteTagMapping (int applicationId, int tenantId) throws ApplicationManagementDAOException;
List<String> getAppCategories (int appId, int tenantId) throws ApplicationManagementDAOException; List<String> getAppCategories (int appId, int tenantId) throws ApplicationManagementDAOException;
List<CategoryDTO> getAllCategories(int tenantId) throws ApplicationManagementDAOException; List<CategoryDTO> getAllCategories(int tenantId) throws ApplicationManagementDAOException;
void addCategories(List<String> categories, int tenantId) throws ApplicationManagementDAOException; void addCategories(List<String> categories, int tenantId) throws ApplicationManagementDAOException;
void addCategoryMapping (List<Integer> categoryIds, int applicationId, int tenantId) throws ApplicationManagementDAOException; void addCategoryMapping(List<Integer> categoryIds, int applicationId, int tenantId)
throws ApplicationManagementDAOException;
void deleteCategoryMapping (int applicationId, int tenantId) throws ApplicationManagementDAOException;
/** /**
* To check application existence. * To check application existence.
@ -209,5 +213,8 @@ public interface ApplicationDAO {
ApplicationDTO getApplicationByRelease(String appReleaseUUID, int tenantId) throws ApplicationManagementDAOException; ApplicationDTO getApplicationByRelease(String appReleaseUUID, int tenantId) throws ApplicationManagementDAOException;
String getApplicationSubTypeByUUID(String uuid, int tenantId) throws ApplicationManagementDAOException; String getApplicationSubTypeByUUID(String uuid, int tenantId) throws ApplicationManagementDAOException;
void deleteApplication(int appId, int tenantId) throws ApplicationManagementDAOException;
} }

@ -115,6 +115,9 @@ public interface ApplicationReleaseDAO {
*/ */
void deleteRelease(int id) throws ApplicationManagementDAOException; void deleteRelease(int id) throws ApplicationManagementDAOException;
void deleteReleases(List<Integer> applicationReleaseIds) throws ApplicationManagementDAOException;
/** /**
* To get release details of a specific application. * To get release details of a specific application.
* *

@ -76,6 +76,9 @@ public interface LifecycleStateDAO {
*/ */
void deleteLifecycleStateByReleaseId(int releaseId) throws LifeCycleManagementDAOException; void deleteLifecycleStateByReleaseId(int releaseId) throws LifeCycleManagementDAOException;
void deleteLifecycleStates(List<Integer> appReleaseIds) throws LifeCycleManagementDAOException;
/*** /***
* *
* @param appId ID of the application * @param appId ID of the application

@ -808,6 +808,34 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
} }
} }
@Override
public void deleteCategoryMapping (int applicationId, int tenantId) throws ApplicationManagementDAOException{
if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to delete Category mappings.");
}
Connection conn;
String sql = "DELETE FROM "
+ "AP_APP_CATEGORY_MAPPING cm "
+ "WHERE "
+ "cm.AP_APP_ID = ? AND "
+ "cm.TENANT_ID = ?";
try {
conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)){
stmt.setInt(1, applicationId);
stmt.setInt(2, tenantId);
stmt.executeUpdate();
}
} catch (DBConnectionException e) {
throw new ApplicationManagementDAOException(
"Error occurred while obtaining the DB connection when deleting category mapping of application ID: "
+ applicationId , e);
} catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred when deleting category mapping of application ID: "
+ applicationId, e);
}
}
@Override @Override
public List<Integer> getTagIdsForTagNames(List<String> tagNames, int tenantId) public List<Integer> getTagIdsForTagNames(List<String> tagNames, int tenantId)
throws ApplicationManagementDAOException { throws ApplicationManagementDAOException {
@ -931,12 +959,40 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
} }
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException( throw new ApplicationManagementDAOException(
"Error occurred while obtaining the DB connection when deleting tag mapppig", e); "Error occurred while obtaining the DB connection when deleting tag mapping", e);
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred when deleting tag mapping", e); throw new ApplicationManagementDAOException("Error occurred when deleting tag mapping", e);
} }
} }
@Override
public void deleteTagMapping (int applicationId, int tenantId) throws ApplicationManagementDAOException{
if (log.isDebugEnabled()) {
log.debug("Request received in DAO Layer to delete Tag mappings.");
}
Connection conn;
String sql = "DELETE FROM "
+ "AP_APP_TAG_MAPPING tm "
+ "WHERE "
+ "tm.AP_APP_ID = ? AND "
+ "tm.TENANT_ID = ?";
try {
conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)){
stmt.setInt(1, applicationId);
stmt.setInt(2, tenantId);
stmt.executeUpdate();
}
} catch (DBConnectionException e) {
throw new ApplicationManagementDAOException(
"Error occurred while obtaining the DB connection when deleting tag mapping of application ID: "
+ applicationId , e);
} catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred when deleting tag mapping of application ID: "
+ applicationId, e);
}
}
@Override @Override
public List<String> getAppCategories(int appId, int tenantId) throws ApplicationManagementDAOException { public List<String> getAppCategories(int appId, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
@ -1132,4 +1188,31 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
} }
} }
@Override
public void deleteApplication(int appId, int tenantId) throws ApplicationManagementDAOException {
Connection conn;
String sql;
try {
conn = this.getDBConnection();
sql = "DELETE AP_APP ap "
+ "WHERE ap.ID = ? AND "
+ "ap.TENANT_ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)){
stmt.setInt(1, appId);
stmt.setInt(2, tenantId);
stmt.executeUpdate();
}
} catch (DBConnectionException e) {
String msg = "Error occurred while obtaining the DB connection to delete application for application id::."
+ appId;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred while deleting application for application ID: " + appId;
log.error(msg);
throw new ApplicationManagementDAOException(msg, e);
}
}
} }

@ -514,6 +514,31 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
} }
} }
@Override
public void deleteReleases(List<Integer> applicationReleaseIds) throws ApplicationManagementDAOException{
Connection connection;
String sql = "DELETE "
+ "FROM AP_APP_RELEASE "
+ "WHERE ID = ?";
try {
connection = this.getDBConnection();
try (PreparedStatement statement = connection.prepareStatement(sql)){
for (Integer releaseId : applicationReleaseIds){
statement.setInt(1, releaseId);
statement.addBatch();
}
statement.executeBatch();
}
} catch (DBConnectionException e) {
throw new ApplicationManagementDAOException(
"Database connection exception occurred while trying to delete given application release", e);
} catch (SQLException e) {
throw new ApplicationManagementDAOException(
"SQL exception occurred while execute delete query for deleting given application releases.", e);
}
}
@Override @Override
public boolean verifyReleaseExistenceByHash(String hashVal, int tenantId) throws ApplicationManagementDAOException { public boolean verifyReleaseExistenceByHash(String hashVal, int tenantId) throws ApplicationManagementDAOException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {

@ -212,6 +212,31 @@ public class GenericLifecycleStateDAOImpl extends AbstractDAOImpl implements Lif
} }
} }
@Override
public void deleteLifecycleStates(List<Integer> appReleaseIds) throws LifeCycleManagementDAOException{
Connection conn;
try {
conn = this.getDBConnection();
String sql = "DELETE FROM " +
"AP_APP_LIFECYCLE_STATE " +
"WHERE AP_APP_RELEASE_ID = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
for (Integer releaseId : appReleaseIds) {
stmt.setInt(1, releaseId);
stmt.addBatch();
}
stmt.executeBatch();
}
} catch (DBConnectionException e) {
throw new LifeCycleManagementDAOException("Error occurred while obtaining the DB connection for deleting "
+ "application life-cycle states for given application Ids.", e);
} catch (SQLException e) {
throw new LifeCycleManagementDAOException("Error occurred while deleting life-cycle states for given "
+ "application releases.", e);
}
}
private LifecycleStateDTO constructLifecycle(ResultSet rs) throws LifeCycleManagementDAOException { private LifecycleStateDTO constructLifecycle(ResultSet rs) throws LifeCycleManagementDAOException {
LifecycleStateDTO lifecycleState = null; LifecycleStateDTO lifecycleState = null;
try { try {

@ -737,8 +737,9 @@ public class ApplicationManagerImpl implements ApplicationManager {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("ApplicationDTO release request is received for the application id: " + applicationId); log.debug("ApplicationDTO release request is received for the application id: " + applicationId);
} }
ApplicationDTO applicationDTO = getApplication(applicationId);
try { try {
ApplicationDTO applicationDTO = getApplication(applicationId);
ApplicationReleaseDTO applicationReleaseDTO = uploadReleaseArtifacts(applicationReleaseWrapper, ApplicationReleaseDTO applicationReleaseDTO = uploadReleaseArtifacts(applicationReleaseWrapper,
applicationDTO, applicationArtifact); applicationDTO, applicationArtifact);
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
@ -819,11 +820,10 @@ public class ApplicationManagerImpl implements ApplicationManager {
public Application getApplicationById(int appId, String state) throws ApplicationManagementException { public Application getApplicationById(int appId, String state) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
ApplicationDTO applicationDTO;
boolean isVisibleApp = false; boolean isVisibleApp = false;
try { ApplicationDTO applicationDTO = getApplication(appId);
applicationDTO = getApplication(appId);
try {
ConnectionManagerUtil.openDBConnection(); ConnectionManagerUtil.openDBConnection();
List<ApplicationReleaseDTO> filteredApplicationReleaseDTOs = new ArrayList<>(); List<ApplicationReleaseDTO> filteredApplicationReleaseDTOs = new ArrayList<>();
for (ApplicationReleaseDTO applicationReleaseDTO : applicationDTO.getApplicationReleaseDTOs()) { for (ApplicationReleaseDTO applicationReleaseDTO : applicationDTO.getApplicationReleaseDTOs()) {
@ -1162,39 +1162,49 @@ public class ApplicationManagerImpl implements ApplicationManager {
@Override @Override
public void deleteApplication(int applicationId) throws ApplicationManagementException { public void deleteApplication(int applicationId) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager();
List<String> storedLocations = new ArrayList<>();
ApplicationDTO applicationDTO;
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request is received to delete applications which are related with the application id " log.debug("Request is received to delete applications which are related with the application id "
+ applicationId); + applicationId);
} }
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
ApplicationStorageManager applicationStorageManager = Util.getApplicationStorageManager();
ApplicationDTO applicationDTO = getApplication(applicationId);
List<ApplicationReleaseDTO> applicationReleaseDTOs = applicationDTO.getApplicationReleaseDTOs();
for (ApplicationReleaseDTO applicationReleaseDTO : applicationReleaseDTOs) {
if (!lifecycleStateManager.isDeletableState(applicationReleaseDTO.getCurrentState())){
String msg = "Application release which has application release UUID: " +
applicationReleaseDTO.getUuid() + " is not in a deletable state. Therefore Application "
+ "deletion is not permitted. In order to delete the application, all application releases "
+ "of the application has to be in a deletable state.";
log.error(msg);
throw new ForbiddenException(msg);
}
}
try { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
applicationDTO = this.applicationDAO.getApplicationById(applicationId, tenantId); List<Integer> deletingAppReleaseIds = new ArrayList<>();
if (applicationDTO == null) {
throw new NotFoundException("Couldn't found an application for Application ID: " + applicationId);
}
List<ApplicationReleaseDTO> applicationReleaseDTOs = applicationDTO.getApplicationReleaseDTOs();
List<ApplicationReleaseDTO> activeApplicationReleaseDTOs = new ArrayList<>();
for (ApplicationReleaseDTO applicationReleaseDTO : applicationReleaseDTOs) { for (ApplicationReleaseDTO applicationReleaseDTO : applicationReleaseDTOs) {
if (!applicationReleaseDTO.getCurrentState().equals(lifecycleStateManager.getEndState())){ List<DeviceSubscriptionDTO> deviceSubscriptionDTOS = subscriptionDAO
activeApplicationReleaseDTOs.add(applicationReleaseDTO); .getDeviceSubscriptions(applicationReleaseDTO.getId(), tenantId);
if (!deviceSubscriptionDTOS.isEmpty()){
String msg = "Application release which has UUID: " + applicationReleaseDTO.getUuid() +
" either subscribed to device/s or it had subscribed to device/s. Therefore you are not "
+ "permitted to delete the application release.";
log.error(msg);
throw new ForbiddenException(msg);
} }
storedLocations.add(applicationReleaseDTO.getAppHashValue()); applicationStorageManager.deleteApplicationReleaseArtifacts(applicationReleaseDTO.getAppHashValue());
} deletingAppReleaseIds.add(applicationReleaseDTO.getId());
if (!activeApplicationReleaseDTOs.isEmpty()) { }
String msg = "There are application releases which are not in the state " + lifecycleStateManager this.lifecycleStateDAO.deleteLifecycleStates(deletingAppReleaseIds);
.getEndState() + ". Hence you are not allowed to delete the application"; this.applicationReleaseDAO.deleteReleases(deletingAppReleaseIds);
log.error(msg); this.applicationDAO.deleteTagMapping(applicationId, tenantId);
throw new ForbiddenException(msg); this.applicationDAO.deleteCategoryMapping(applicationId, tenantId);
} this.applicationDAO.deleteApplication(applicationId, tenantId);
this.applicationDAO.retireApplication(applicationId);
ConnectionManagerUtil.commitDBTransaction(); ConnectionManagerUtil.commitDBTransaction();
applicationStorageManager.deleteAllApplicationReleaseArtifacts(storedLocations);
} catch (ApplicationManagementDAOException e) { } catch (ApplicationManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occurred when getting application data for application id: " + applicationId; String msg = "Error occurred when getting application data for application id: " + applicationId;
log.error(msg); log.error(msg);
throw new ApplicationManagementException(msg, e); throw new ApplicationManagementException(msg, e);
@ -1203,6 +1213,12 @@ public class ApplicationManagerImpl implements ApplicationManager {
+ applicationId; + applicationId;
log.error(msg); log.error(msg);
throw new ApplicationManagementException(msg); throw new ApplicationManagementException(msg);
} catch (LifeCycleManagementDAOException e) {
ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Error occured while deleting life-cycle state data of application releases of the application"
+ " which has application ID: " + applicationId;
log.error(msg);
throw new ApplicationManagementException(msg);
} finally { } finally {
ConnectionManagerUtil.closeDBConnection(); ConnectionManagerUtil.closeDBConnection();
} }
@ -1210,19 +1226,13 @@ public class ApplicationManagerImpl implements ApplicationManager {
@Override @Override
public void retireApplication(int applicationId) throws ApplicationManagementException { public void retireApplication(int applicationId) throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
ApplicationDTO applicationDTO;
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request is received to delete applications which are related with the application id " log.debug("Request is received to delete applications which are related with the application id "
+ applicationId); + applicationId);
} }
ApplicationDTO applicationDTO = getApplication(applicationId);
try { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
applicationDTO = this.applicationDAO.getApplicationById(applicationId, tenantId);
if (applicationDTO == null) {
throw new NotFoundException("Couldn't found an application for Application ID: " + applicationId);
}
List<ApplicationReleaseDTO> applicationReleaseDTOs = applicationDTO.getApplicationReleaseDTOs(); List<ApplicationReleaseDTO> applicationReleaseDTOs = applicationDTO.getApplicationReleaseDTOs();
List<ApplicationReleaseDTO> activeApplicationReleaseDTOs = new ArrayList<>(); List<ApplicationReleaseDTO> activeApplicationReleaseDTOs = new ArrayList<>();
for (ApplicationReleaseDTO applicationReleaseDTO : applicationReleaseDTOs) { for (ApplicationReleaseDTO applicationReleaseDTO : applicationReleaseDTOs) {
@ -1655,19 +1665,9 @@ public class ApplicationManagerImpl implements ApplicationManager {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
ApplicationDTO applicationDTO; ApplicationDTO applicationDTO = getApplication(applicationId);
try { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
applicationDTO = this.applicationDAO.getApplicationById(applicationId, tenantId);
if (applicationDTO == null) {
ConnectionManagerUtil.rollbackDBTransaction();
String msg = "Tried to update Application which is not in the publisher. Please verify "
+ "application details";
log.error(msg);
throw new NotFoundException(msg);
}
if (!StringUtils.isEmpty(applicationUpdateWrapper.getName())){ if (!StringUtils.isEmpty(applicationUpdateWrapper.getName())){
Filter filter = new Filter(); Filter filter = new Filter();
filter.setFullMatch(true); filter.setFullMatch(true);

@ -113,4 +113,42 @@ public interface ApplicationManagementPublisherAdminAPI {
value = "application release UUID", value = "application release UUID",
required = true) required = true)
@PathParam("uuid") String releaseUuid); @PathParam("uuid") String releaseUuid);
@DELETE
@Path("/{appId}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(
consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON,
httpMethod = "DELETE",
value = "Delete application release.",
notes = "This will delete application release for given UUID",
tags = "Application Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = SCOPE, value = "perm:admin:app:publisher:update")
})
}
)
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "OK. \n Successfully delete application release.",
response = ApplicationList.class),
@ApiResponse(
code = 404,
message = "Not Found. There doesn't have an application release for UUID" +
"query."),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Error occurred while deleting application release.",
response = ErrorResponse.class)
}) Response deleteApplication(
@ApiParam(
name = "appId",
value = "application ID",
required = true)
@PathParam("appId") int applicatioId);
} }

@ -100,4 +100,29 @@ public class ApplicationManagementPublisherAdminAPIImpl implements ApplicationMa
} }
} }
@DELETE
@Path("/{appId}")
public Response deleteApplication(
@PathParam("appId") int applicatioId) {
ApplicationManager applicationManager = APIUtil.getApplicationManager();
try {
applicationManager.deleteApplication(applicatioId);
String responseMsg = "Successfully deleted the application which has ID: " + applicatioId + "";
return Response.status(Response.Status.OK).entity(responseMsg).build();
} catch (NotFoundException e) {
String msg =
"Couldn't found application release which is having the ID:" + applicatioId;
log.error(msg, e);
return Response.status(Response.Status.NOT_FOUND).entity(msg).build();
} catch (ForbiddenException e) {
String msg = "You don't have require permission to delete the application which has ID: " + applicatioId;
log.error(msg, e);
return Response.status(Response.Status.FORBIDDEN).entity(msg).build();
} catch (ApplicationManagementException e) {
String msg = "Error occurred while deleting the application which has application ID:: " + applicatioId;
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
}
} }

Loading…
Cancel
Save