From 884511fd68e3b3b743240d0587bc17b70b550c70 Mon Sep 17 00:00:00 2001 From: megala21 Date: Mon, 21 Aug 2017 17:39:27 +0530 Subject: [PATCH] Fixing issues for mysql database and adding CRUD operations to application release --- .../services/ApplicationManagementAPI.java | 84 +++++++++- .../impl/ApplicationManagementAPIImpl.java | 73 ++++++++- .../services/ApplicationReleaseManager.java | 22 ++- .../services/ApplicationStorageManager.java | 25 +++ .../mgt/core/dao/ApplicationReleaseDAO.java | 24 +++ .../GenericApplicationReleaseDAOImpl.java | 143 ++++++++++++++---- .../impl/ApplicationReleaseManagerImpl.java | 90 ++++++++--- .../impl/ApplicationStorageManagerImpl.java | 90 ++++++++++- .../dbscripts/cdm/application-mgt/mysql.sql | 14 +- 9 files changed, 496 insertions(+), 69 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementAPI.java index 69f2c8f3e32..ca883183cc4 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementAPI.java @@ -493,6 +493,45 @@ public interface ApplicationManagementAPI { @Multipart(value = "applicationRelease", type = "application/json") ApplicationRelease applicationRelease, @Multipart(value = "binaryFile") Attachment binaryFile); + @PUT + @Path("/release/{uuid}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.MULTIPART_FORM_DATA) + @ApiOperation( + consumes = MediaType.MULTIPART_FORM_DATA, + produces = MediaType.APPLICATION_JSON, + httpMethod = "PUT", + value = "Update an application release", + notes = "This will update a new application release", + tags = "Application Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:application:update") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 201, + message = "OK. \n Successfully created an application release.", + response = ApplicationRelease.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while releasing the application.", + response = ErrorResponse.class) + }) + + Response updateApplicationRelease( + @ApiParam( + name = "UUID", + value = "Unique identifier of the Application", + required = true) + @PathParam("uuid") String applicationUUID, + @Multipart(value = "applicationRelease", required = false, type = "application/json") + ApplicationRelease applicationRelease, + @Multipart(value = "binaryFile", required = false) Attachment binaryFile); + @GET @Path("/release-artifacts/{uuid}/{version}") @Produces(MediaType.APPLICATION_OCTET_STREAM) @@ -500,9 +539,9 @@ public interface ApplicationManagementAPI { @ApiOperation( consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_OCTET_STREAM, - httpMethod = "POST", - value = "Create an application release", - notes = "This will create a new application release", + httpMethod = "GET", + value = "Get an application release", + notes = "This will return the application release indicated by Application UUID and version", tags = "Application Management", extensions = { @Extension(properties = { @@ -571,4 +610,43 @@ public interface ApplicationManagementAPI { value = "Version of the application", required = false) @QueryParam("version") String version); + + @DELETE + @Path("/release/{uuid}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "DELETE", + value = "Delete the releases of a particular applicaion", + notes = "This will delete the releases or specific release of an application", + tags = "Application Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:application:delete") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully deleted the Application release."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while deleting the release of a" + + "particular application.", + response = ErrorResponse.class) + }) + Response deleteApplicationRelease( + @ApiParam( + name = "UUID", + value = "Unique identifier of the Application", + required = true) + @PathParam("uuid") String applicationUUID, + @ApiParam( + name = "version", + value = "Version of the application") + @QueryParam("version") String version); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementAPIImpl.java index 271520149ec..e488db3fb58 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementAPIImpl.java @@ -279,15 +279,19 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { @Path("/{appuuid}") public Response deleteApplication(@PathParam("appuuid") String uuid) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); + ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); + ApplicationReleaseManager applicationReleaseManager = APIUtil.getApplicationReleaseManager(); try { + applicationReleaseManager.deleteApplicationReleases(uuid); + applicationStorageManager.deleteApplicationArtifacts(uuid); applicationManager.deleteApplication(uuid); + String responseMsg = "Successfully deleted the application: " + uuid; + return Response.status(Response.Status.OK).entity(responseMsg).build(); } catch (ApplicationManagementException e) { String msg = "Error occurred while deleting the application: " + uuid; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } - String responseMsg = "Successfully deleted the application: " + uuid; - return Response.status(Response.Status.OK).entity(responseMsg).build(); } @Override @@ -320,6 +324,40 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { } } + @Override + @PUT + @Path("/release/{uuid}") + public Response updateApplicationRelease(@PathParam("uuid") String applicationUUID, @Multipart + ("applicationRelease") ApplicationRelease applicationRelease, @Multipart("binaryFile") Attachment + binaryFile) { + ApplicationReleaseManager applicationReleaseManager = APIUtil.getApplicationReleaseManager(); + ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); + try { + if (applicationRelease != null) { + applicationRelease = applicationReleaseManager.updateRelease(applicationUUID, applicationRelease); + } + if (binaryFile != null) { + String version = applicationRelease == null ? null : applicationRelease.getVersionName(); + + if (version == null) { + return Response.status(Response.Status.BAD_REQUEST).entity("Version cannot be null. Version is a " + + "mandatory parameter to update the release artifacts").build(); + } + applicationStorageManager + .uploadReleaseArtifacts(applicationUUID, version, binaryFile.getDataHandler().getInputStream()); + } + return Response.status(Response.Status.OK).entity(applicationRelease).build(); + } catch (ApplicationManagementException e) { + log.error("Error while updating the application release of the application with UUID " + applicationUUID); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } catch (IOException e) { + log.error("Error while updating the release artifacts of the application with UUID " + applicationUUID); + return APIUtil.getResponse(new ApplicationManagementException( + "Error while updating the release artifacts of the application with UUID " + + applicationUUID), Response.Status.INTERNAL_SERVER_ERROR); + } + } + @Override @GET @Produces(MediaType.APPLICATION_OCTET_STREAM) @@ -334,7 +372,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { response.header("Content-Disposition", "attachment; filename=\"" + version + "\""); return response.build(); } catch (ApplicationStorageManagementException e) { - log.error("Error while retrieving the binary file of the applcation release for the application UUID " + + log.error("Error while retrieving the binary file of the application release for the application UUID " + applicationUUID + " and version " + version, e); if (e.getMessage().contains("Binary file does not exist")) { return APIUtil.getResponse(e, Response.Status.NOT_FOUND); @@ -364,4 +402,31 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } + + @Override + @DELETE + @Path("/release/{uuid}") + public Response deleteApplicationRelease(@PathParam("uuid") String applicationUUID, + @QueryParam("version") String version) { + ApplicationReleaseManager applicationReleaseManager = APIUtil.getApplicationReleaseManager(); + ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); + try { + if (version != null && !version.isEmpty()) { + applicationStorageManager.deleteApplicationReleaseArtifacts(applicationUUID, version); + applicationReleaseManager.deleteApplicationRelease(applicationUUID, version); + return Response.status(Response.Status.OK) + .entity("Successfully deleted Application release with " + "version " + version + + " for the application with UUID " + applicationUUID).build(); + } else { + applicationStorageManager.deleteAllApplicationReleaseArtifacts(applicationUUID); + applicationReleaseManager.deleteApplicationReleases(applicationUUID); + return Response.status(Response.Status.OK) + .entity("Successfully deleted Application releases for the " + "application with UUID " + + applicationUUID).build(); + } + } catch (ApplicationManagementException e) { + log.error("Error while deleting application release with the applicaion UUID " + applicationUUID, e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationReleaseManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationReleaseManager.java index 05d293282fa..d0b8d546d0b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationReleaseManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationReleaseManager.java @@ -68,8 +68,28 @@ public interface ApplicationReleaseManager { /** * To update with a new release for an Application. * + * @param applicationUuid UUID of the Application * @param applicationRelease ApplicationRelease + * @return Updated Application Release. * @throws ApplicationManagementException Application Management Exception. */ - public void updateRelease(ApplicationRelease applicationRelease) throws ApplicationManagementException; + public ApplicationRelease updateRelease(String applicationUuid, ApplicationRelease applicationRelease) throws + ApplicationManagementException; + + /** + * To delete a particular release + * + * @param applicationUuid UUID of the Application, in which the ApplicationRelease need to be deleted. + * @param version Version of the ApplicationRelease that need to be deleted. + * @throws ApplicationManagementException Application Management Exception. + */ + public void deleteApplicationRelease(String applicationUuid, String version) throws ApplicationManagementException; + + /** + * To delete all the application releases related with the the particular application. + * + * @param applicationUuid UUID of the application. + * @throws ApplicationManagementException Application Management Exception. + */ + public void deleteApplicationReleases(String applicationUuid) throws ApplicationManagementException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java index 72da318963d..8d1f073f10a 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationStorageManager.java @@ -58,4 +58,29 @@ public interface ApplicationStorageManager { */ public InputStream getReleasedArtifacts(String applicationUUID, String versionName) throws ApplicationStorageManagementException; + + /** + * To delete all the artifacts related with a particular Application. + * @param applicationUUID UUID of the Application. + * @throws ApplicationStorageManagementException Application Storage Management Exception. + */ + public void deleteApplicationArtifacts(String applicationUUID) throws ApplicationStorageManagementException; + + /** + * To delete the artifacts related with particular Application Release. + * + * @param applicationUUID UUID of the Application. + * @param version Version of ApplicationRelease that need to be deleted. + * @throws ApplicationStorageManagementException Application Storage Management Exception. + */ + public void deleteApplicationReleaseArtifacts(String applicationUUID, String version) + throws ApplicationStorageManagementException; + + /** + * To delete all release artifacts related with particular Application Release. + * @param applicationUUID UUID of the Application. + * @throws ApplicationStorageManagementException Application Storage Management Exception + */ + public void deleteAllApplicationReleaseArtifacts(String applicationUUID) throws + ApplicationStorageManagementException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java index 8205a3250d8..1cb2becfd19 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationReleaseDAO.java @@ -56,4 +56,28 @@ public interface ApplicationReleaseDAO { */ List getApplicationReleases(String applicationUUID) throws ApplicationManagementDAOException; + /** + * To update an Application release. + * @param applicationRelease ApplicationRelease that need to be updated. + * @return the updated Application Release + * @throws ApplicationManagementDAOException Application Management DAO Exception + */ + ApplicationRelease updateRelease(ApplicationRelease applicationRelease) throws ApplicationManagementDAOException; + + /** + * To delete a particular release. + * + * @param id ID of the Application which the release need to be deleted. + * @param version Version of the Application Release + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ + void deleteRelease(int id, String version) throws ApplicationManagementDAOException; + + /** + * To delete the propertied of a particular Application Release. + * + * @param id ID of the ApplicationRelease in which properties need to be deleted. + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ + void deleteReleaseProperties(int id) throws ApplicationManagementDAOException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java index f35c63f0db5..50d39a780df 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/release/GenericApplicationReleaseDAOImpl.java @@ -27,11 +27,7 @@ import org.wso2.carbon.device.application.mgt.core.dao.impl.AbstractDAOImpl; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; -import java.sql.Connection; -import java.sql.Date; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.sql.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -51,11 +47,10 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements String sql = "insert into APPM_APPLICATION_RELEASE(VERSION_NAME, RESOURCE, RELEASE_CHANNEL ," + "RELEASE_DETAILS, CREATED_AT, APPM_APPLICATION_ID, IS_DEFAULT) values (?, ?, ?, ?, ?, ?, ?)"; int index = 0; - boolean isBatchExecutionSupported = ConnectionManagerUtil.isBatchQuerySupported(); try { connection = this.getDBConnection(); - statement = connection.prepareStatement(sql); + statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); statement.setString(++index, applicationRelease.getVersionName()); statement.setString(++index, applicationRelease.getResource()); statement.setString(++index, String.valueOf(applicationRelease.getReleaseChannel())); @@ -68,25 +63,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements if (resultSet.next()) { applicationRelease.setId(resultSet.getInt(1)); } - - if (applicationRelease.getProperties() != null && applicationRelease.getProperties().size() != 0) { - sql = "INSERT INTO APPM_RELEASE_PROPERTY (PROP_KEY, PROP_VALUE, APPLICATION_RELEASE_ID) VALUES (?,?,?)"; - statement = connection.prepareStatement(sql); - for (Object entry : applicationRelease.getProperties().entrySet()) { - Map.Entry property = (Map.Entry) entry; - statement.setString(1, property.getKey()); - statement.setString(2, property.getValue()); - statement.setInt(3, applicationRelease.getId()); - if (isBatchExecutionSupported) { - statement.addBatch(); - } else { - statement.execute(); - } - } - if (isBatchExecutionSupported) { - statement.executeBatch(); - } - } + insertApplicationReleaseProperties(connection, applicationRelease); return applicationRelease; } catch (SQLException e) { throw new ApplicationManagementDAOException( @@ -142,7 +119,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements } return applicationRelease; } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException("Database connection exception while trying to gett the " + throw new ApplicationManagementDAOException("Database connection exception while trying to get the " + "release details of the application with UUID " + applicationUuid + " and version " + versionName, e); } catch (SQLException e) { @@ -205,4 +182,116 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements Util.cleanupResources(null, rsProperties); } } + + @Override + public ApplicationRelease updateRelease(ApplicationRelease applicationRelease) + throws ApplicationManagementDAOException { + Connection connection; + PreparedStatement statement = null; + String sql = "UPDATE APPM_APPLICATION_RELEASE SET RESOURCE = IFNULL (?, RESOURCE), RELEASE_CHANNEL = IFNULL " + + "(?, RELEASE_CHANNEL), RELEASE_DETAILS = IFNULL (?, RELEASE_DETAILS), IS_DEFAULT = IFNULL " + + "(?, IS_DEFAULT) WHERE APPM_APPLICATION_ID = ? AND VERSION_NAME = ?"; + try { + connection = this.getDBConnection(); + statement = connection.prepareStatement(sql); + statement.setString(1, applicationRelease.getResource()); + statement.setString(2, String.valueOf(applicationRelease.getReleaseChannel())); + statement.setString(3, applicationRelease.getReleaseDetails()); + statement.setBoolean(4, applicationRelease.isDefault()); + statement.setInt(5, applicationRelease.getApplication().getId()); + statement.setString(6, applicationRelease.getVersionName()); + statement.executeUpdate(); + + sql = "DELETE FROM APPM_RELEASE_PROPERTY WHERE APPLICATION_RELEASE_ID = ?"; + statement = connection.prepareStatement(sql); + statement.setInt(1, applicationRelease.getId()); + statement.executeUpdate(); + insertApplicationReleaseProperties(connection, applicationRelease); + return applicationRelease; + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException("Database connection exception while trying to update the " + + "Application release for the application with UUID " + applicationRelease.getApplication() + .getUuid() + " for the version " + applicationRelease.getVersionName(), e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException( + "SQL exception while updating the release, while executing the query " + sql, e); + } finally { + Util.cleanupResources(statement, null); + } + } + + @Override + public void deleteRelease(int id, String version) throws ApplicationManagementDAOException { + Connection connection; + PreparedStatement statement = null; + String sql = "DELETE FROM APPM_APPLICATION_RELEASE WHERE APPM_APPLICATION_ID = ? AND VERSION_NAME = ?"; + try { + connection = this.getDBConnection(); + statement = connection.prepareStatement(sql); + statement.setInt(1, id); + statement.setString(2, version); + statement.executeUpdate(); + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException( + "Database connection exception while trying to delete the release with version " + version, e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException( + "SQL exception while deleting the release with version " + version + ",while executing the query " + + "sql", e); + } finally { + Util.cleanupResources(statement, null); + } + } + + @Override + public void deleteReleaseProperties(int id) throws ApplicationManagementDAOException { + Connection connection; + PreparedStatement statement = null; + String sql = "DELETE FROM APPM_RELEASE_PROPERTY WHERE APPLICATION_RELEASE_ID = ?"; + try { + connection = this.getDBConnection(); + statement = connection.prepareStatement(sql); + statement.setInt(1, id); + statement.executeUpdate(); + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException( + "Database connection exception while trying to delete the release properties ", e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException( + "SQL exception while deleting the properties of application release with the id " + id, e); + } finally { + Util.cleanupResources(statement, null); + } + } + + /** + * To insert the application release properties. + * @param connection Database Connection + * @param applicationRelease Application Release the properties of which that need to be inserted. + * @throws SQLException SQL Exception. + */ + private void insertApplicationReleaseProperties(Connection connection, ApplicationRelease applicationRelease) + throws SQLException { + String sql; + boolean isBatchExecutionSupported = ConnectionManagerUtil.isBatchQuerySupported(); + + if (applicationRelease.getProperties() != null && applicationRelease.getProperties().size() != 0) { + sql = "INSERT INTO APPM_RELEASE_PROPERTY (PROP_KEY, PROP_VALUE, APPLICATION_RELEASE_ID) VALUES (?,?,?)"; + PreparedStatement statement = connection.prepareStatement(sql); + for (Object entry : applicationRelease.getProperties().entrySet()) { + Map.Entry property = (Map.Entry) entry; + statement.setString(1, property.getKey()); + statement.setString(2, property.getValue()); + statement.setInt(3, applicationRelease.getId()); + if (isBatchExecutionSupported) { + statement.addBatch(); + } else { + statement.execute(); + } + } + if (isBatchExecutionSupported) { + statement.executeBatch(); + } + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationReleaseManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationReleaseManagerImpl.java index bc7cfa33a03..39eb043c826 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationReleaseManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationReleaseManagerImpl.java @@ -41,7 +41,8 @@ public class ApplicationReleaseManagerImpl implements ApplicationReleaseManager @Override public ApplicationRelease createRelease(String appicationUuid, ApplicationRelease applicationRelease) throws ApplicationManagementException { - Application application = validateReleaseCreateRequest(appicationUuid, applicationRelease); + Application application = validateApplication(appicationUuid); + validateReleaseCreateRequest(appicationUuid, applicationRelease); if (log.isDebugEnabled()) { log.debug("Application release request is received for the application " + application.toString()); } @@ -63,7 +64,7 @@ public class ApplicationReleaseManagerImpl implements ApplicationReleaseManager @Override public ApplicationRelease getRelease(String applicationUuid, String version) throws ApplicationManagementException { - Application application = validationGetReleaseRequest(applicationUuid); + Application application = validateApplication(applicationUuid); if (log.isDebugEnabled()) { log.debug("Application release retrieval request is received for the application " + application.toString() + " and version " + version); @@ -78,7 +79,7 @@ public class ApplicationReleaseManagerImpl implements ApplicationReleaseManager @Override public List getReleases(String applicationUuid) throws ApplicationManagementException { - Application application = validationGetReleaseRequest(applicationUuid); + Application application = validateApplication(applicationUuid); if (log.isDebugEnabled()) { log.debug("Request is received to retrieve all the releases related with the application " + application.toString()); @@ -97,8 +98,67 @@ public class ApplicationReleaseManagerImpl implements ApplicationReleaseManager } @Override - public void updateRelease(ApplicationRelease applicationRelease) throws ApplicationManagementException { + public ApplicationRelease updateRelease(String applicationUuid, ApplicationRelease applicationRelease) + throws ApplicationManagementException { + Application application = validateApplication(applicationUuid); + ApplicationRelease oldApplicationRelease = null; + if (applicationRelease == null || applicationRelease.getVersionName() != null) { + throw new ApplicationManagementException( + "Version is important to update the release of the application " + "with application UUID " + + applicationUuid); + } + oldApplicationRelease = getRelease(applicationUuid, applicationRelease.getVersionName()); + if (oldApplicationRelease == null) { + throw new ApplicationManagementException( + "Application release for the application " + applicationUuid + " with version " + applicationRelease + .getVersionName() + " does not exist. Cannot update the " + + "release that is not existing."); + } + applicationRelease.setApplication(application); + try { + ConnectionManagerUtil.beginDBTransaction(); + ApplicationRelease newApplicationRelease = DAOFactory.getApplicationReleaseDAO() + .updateRelease(applicationRelease); + ConnectionManagerUtil.commitDBTransaction(); + return newApplicationRelease; + } catch (ApplicationManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw e; + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @Override + public void deleteApplicationRelease(String applicationUuid, String version) + throws ApplicationManagementException { + Application application = validateApplication(applicationUuid); + ApplicationRelease applicationRelease = getRelease(applicationUuid, version); + if (applicationRelease == null) { + throw new ApplicationManagementException( + "Cannot delete a non-existing application release for the " + "application with UUID " + + applicationUuid); + } + try { + ConnectionManagerUtil.beginDBTransaction(); + DAOFactory.getApplicationReleaseDAO().deleteRelease(application.getId(), version); + DAOFactory.getApplicationReleaseDAO().deleteReleaseProperties(applicationRelease.getId()); + ConnectionManagerUtil.commitDBTransaction(); + } catch (ApplicationManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw e; + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + + @Override + public void deleteApplicationReleases(String applicationUuid) throws ApplicationManagementException { + List applicationReleases = getReleases(applicationUuid); + for (ApplicationRelease applicationRelease : applicationReleases) { + deleteApplicationRelease(applicationUuid, applicationRelease.getVersionName()); + } } /** @@ -107,16 +167,15 @@ public class ApplicationReleaseManagerImpl implements ApplicationReleaseManager * @param applicationUuid UUID of the Application. * @return Application related with the UUID */ - private Application validationGetReleaseRequest(String applicationUuid) throws ApplicationManagementException { + private Application validateApplication(String applicationUuid) throws ApplicationManagementException { if (applicationUuid == null) { throw new ApplicationManagementException("Application UUID is null. Application UUID is a required " - + "parameter to get the releases related to a particular application."); + + "parameter to get the relevant application."); } Application application = DataHolder.getInstance().getApplicationManager().getApplication(applicationUuid); if (application == null) { throw new ApplicationManagementException( - "Application with UUID " + applicationUuid + " does not exist. Cannot " - + "retrieve the releases for a non-existing application."); + "Application with UUID " + applicationUuid + " does not exist."); } return application; } @@ -126,21 +185,10 @@ public class ApplicationReleaseManagerImpl implements ApplicationReleaseManager * * @param applicationUuid UUID of the Application. * @param applicationRelease ApplicationRelease that need to be created. - * @return the Application related with the particular Application Release * @throws ApplicationManagementException Application Management Exception. */ - private Application validateReleaseCreateRequest(String applicationUuid, ApplicationRelease applicationRelease) + private void validateReleaseCreateRequest(String applicationUuid, ApplicationRelease applicationRelease) throws ApplicationManagementException { - if (applicationUuid == null) { - throw new ApplicationManagementException("Application UUID is null. Application UUID is a required " - + "parameter to do the application release"); - } - Application application = DataHolder.getInstance().getApplicationManager().getApplication(applicationUuid); - if (application == null) { - throw new ApplicationManagementException( - "Application with UUID " + applicationUuid + " does not exist. Cannot " - + "release an application that is not existing"); - } if (applicationRelease == null || applicationRelease.getVersionName() == null) { throw new ApplicationManagementException("ApplicationRelease version name is a mandatory parameter for " + "creating release. It cannot be found."); @@ -151,6 +199,6 @@ public class ApplicationReleaseManagerImpl implements ApplicationReleaseManager + applicationRelease.getVersionName() + " already exists. Cannot create an " + "application release with the same version."); } - return application; } + } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java index 30e9499dfa8..c5882ef1d8f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationStorageManagerImpl.java @@ -22,6 +22,7 @@ package org.wso2.carbon.device.application.mgt.core.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.application.mgt.common.Application; +import org.wso2.carbon.device.application.mgt.common.ApplicationRelease; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; @@ -117,8 +118,9 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager try { application = DataHolder.getInstance().getApplicationManager().getApplication(applicationUUID); } catch (ApplicationManagementException e) { - throw new ApplicationStorageManagementException("Exception while retrieving the application details for " - + "the application with UUID " + applicationUUID); + throw new ApplicationStorageManagementException( + "Exception while retrieving the application details for " + "the application with UUID " + + applicationUUID); } if (application == null) { throw new ApplicationStorageManagementException("Application with UUID " + applicationUUID + " does not " @@ -150,8 +152,9 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager try { application = DataHolder.getInstance().getApplicationManager().getApplication(applicationUUID); } catch (ApplicationManagementException e) { - throw new ApplicationStorageManagementException("Exception while retrieving the application details for " - + "the application with UUID " + applicationUUID); + throw new ApplicationStorageManagementException( + "Exception while retrieving the application details for " + "the application with UUID " + + applicationUUID); } if (application == null) { throw new ApplicationStorageManagementException("Application with UUID " + applicationUUID + " does not " @@ -171,12 +174,72 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager try { return new FileInputStream(artifactPath); } catch (FileNotFoundException e) { - throw new ApplicationStorageManagementException("Binary file does not exist for the version " + - versionName + " for the application ", e); + throw new ApplicationStorageManagementException( + "Binary file does not exist for the version " + versionName + " for the application ", e); } } } + @Override + public void deleteApplicationArtifacts(String applicationUUID) throws ApplicationStorageManagementException { + Application application; + try { + application = DataHolder.getInstance().getApplicationManager().getApplication(applicationUUID); + } catch (ApplicationManagementException e) { + throw new ApplicationStorageManagementException( + "Exception while retrieving the application details for " + "the application with UUID " + + applicationUUID); + } + if (application == null) { + throw new ApplicationStorageManagementException("Application with UUID " + applicationUUID + " does not " + + "exist. Cannot delete the artifacts of a non-existing application."); + } + String artifactDirectoryPath = Constants.artifactPath + application.getId(); + File artifactDirectory = new File(artifactDirectoryPath); + + if (artifactDirectory.exists()) { + deleteDir(artifactDirectory); + } + } + + @Override + public void deleteApplicationReleaseArtifacts(String applicationUUID, String version) + throws ApplicationStorageManagementException { + Application application; + try { + application = DataHolder.getInstance().getApplicationManager().getApplication(applicationUUID); + } catch (ApplicationManagementException e) { + throw new ApplicationStorageManagementException( + "Exception while retrieving the application details for " + "the application with UUID " + + applicationUUID); + } + if (application == null) { + throw new ApplicationStorageManagementException("Application with UUID " + applicationUUID + " does not " + + "exist. Cannot delete the artifacts of a non-existing application."); + } + String artifactPath = Constants.artifactPath + application.getId() + File.separator + version; + File artifact = new File(artifactPath); + + if (artifact.exists()) { + deleteDir(artifact); + } + } + + @Override public void deleteAllApplicationReleaseArtifacts(String applicationUUID) + throws ApplicationStorageManagementException { + try { + List applicationReleases = DataHolder.getInstance().getReleaseManager() + .getReleases(applicationUUID); + for (ApplicationRelease applicationRelease : applicationReleases) { + deleteApplicationReleaseArtifacts(applicationUUID, applicationRelease.getVersionName()); + } + } catch (ApplicationManagementException e) { + throw new ApplicationStorageManagementException( + "Application Management Exception while getting releases " + "for the application " + + applicationUUID, e); + } + } + /** * To save a file in a given location. * @@ -216,4 +279,19 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager } } } + + /** + * To delete a directory recursively + * + * @param artifactDirectory Artifact Directory that need to be deleted. + */ + private void deleteDir(File artifactDirectory) { + File[] contents = artifactDirectory.listFiles(); + if (contents != null) { + for (File file : contents) { + deleteDir(file); + } + } + artifactDirectory.delete(); + } } diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql index d0785ca9b03..379b6de3da1 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/mysql.sql @@ -49,10 +49,10 @@ CREATE TABLE IF NOT EXISTS `APPM_APPLICATION_CATEGORY` ( ENGINE = InnoDB COMMENT = 'This table contains the data related to the application category'; -INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION, PUBLISHED) VALUES ('Enterprise', 'Enterprise level -applications which the artifacts need to be provided', 1); -INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION, PUBLISHED) VALUES ('Public', 'Public category in which the -application need to be downloaded from the public application store', 1); +INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION, PUBLISHED) VALUES ('Enterprise', +'Enterprise level applications which the artifacts need to be provided', 1); +INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION, PUBLISHED) VALUES ('Public', +'Public category in which the application need to be downloaded from the public application store', 1); -- ----------------------------------------------------- -- Table `APPM_LIFECYCLE_STATE` @@ -65,8 +65,8 @@ CREATE TABLE IF NOT EXISTS APPM_LIFECYCLE_STATE ( PRIMARY KEY (ID), UNIQUE INDEX APPM_LIFECYCLE_STATE_IDENTIFIER_UNIQUE (IDENTIFIER ASC)); -INSERT INTO APPM_LIFECYCLE_STATE (NAME, IDENTIFIER, DESCRIPTION) VALUES ('CREATED', 'CREATED', 'Application creation -initial state'); +INSERT INTO APPM_LIFECYCLE_STATE (NAME, IDENTIFIER, DESCRIPTION) VALUES ('CREATED', 'CREATED', +'Application creation initial state'); INSERT INTO APPM_LIFECYCLE_STATE (NAME, IDENTIFIER, DESCRIPTION) VALUES ('IN REVIEW', 'IN REVIEW', 'Application is in in-review state'); INSERT INTO APPM_LIFECYCLE_STATE (NAME, IDENTIFIER, DESCRIPTION) @@ -188,7 +188,7 @@ CREATE TABLE IF NOT EXISTS `APPM_APPLICATION_PROPERTY` ( -- Table `APPM_APPLICATION_RELEASE` -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS `APPM_APPLICATION_RELEASE` ( - `ID` INT NOT NULL AUTO_INCREMENT, + `ID` INT NOT NULL AUTO_INCREMENT UNIQUE , `VERSION_NAME` VARCHAR(100) NOT NULL, `RESOURCE` TEXT NULL, `RELEASE_CHANNEL` VARCHAR(50) NULL,