Fixing issues for mysql database and adding CRUD operations to application release

feature/appm-store/pbac
megala21 7 years ago
parent 7fd7fbb30e
commit 884511fd68

@ -493,6 +493,45 @@ public interface ApplicationManagementAPI {
@Multipart(value = "applicationRelease", type = "application/json") ApplicationRelease applicationRelease, @Multipart(value = "applicationRelease", type = "application/json") ApplicationRelease applicationRelease,
@Multipart(value = "binaryFile") Attachment binaryFile); @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 @GET
@Path("/release-artifacts/{uuid}/{version}") @Path("/release-artifacts/{uuid}/{version}")
@Produces(MediaType.APPLICATION_OCTET_STREAM) @Produces(MediaType.APPLICATION_OCTET_STREAM)
@ -500,9 +539,9 @@ public interface ApplicationManagementAPI {
@ApiOperation( @ApiOperation(
consumes = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_OCTET_STREAM, produces = MediaType.APPLICATION_OCTET_STREAM,
httpMethod = "POST", httpMethod = "GET",
value = "Create an application release", value = "Get an application release",
notes = "This will create a new application release", notes = "This will return the application release indicated by Application UUID and version",
tags = "Application Management", tags = "Application Management",
extensions = { extensions = {
@Extension(properties = { @Extension(properties = {
@ -571,4 +610,43 @@ public interface ApplicationManagementAPI {
value = "Version of the application", value = "Version of the application",
required = false) required = false)
@QueryParam("version") String version); @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);
} }

@ -279,15 +279,19 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
@Path("/{appuuid}") @Path("/{appuuid}")
public Response deleteApplication(@PathParam("appuuid") String uuid) { public Response deleteApplication(@PathParam("appuuid") String uuid) {
ApplicationManager applicationManager = APIUtil.getApplicationManager(); ApplicationManager applicationManager = APIUtil.getApplicationManager();
ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager();
ApplicationReleaseManager applicationReleaseManager = APIUtil.getApplicationReleaseManager();
try { try {
applicationReleaseManager.deleteApplicationReleases(uuid);
applicationStorageManager.deleteApplicationArtifacts(uuid);
applicationManager.deleteApplication(uuid); applicationManager.deleteApplication(uuid);
String responseMsg = "Successfully deleted the application: " + uuid;
return Response.status(Response.Status.OK).entity(responseMsg).build();
} catch (ApplicationManagementException e) { } catch (ApplicationManagementException e) {
String msg = "Error occurred while deleting the application: " + uuid; String msg = "Error occurred while deleting the application: " + uuid;
log.error(msg, e); 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 @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 @Override
@GET @GET
@Produces(MediaType.APPLICATION_OCTET_STREAM) @Produces(MediaType.APPLICATION_OCTET_STREAM)
@ -334,7 +372,7 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI {
response.header("Content-Disposition", "attachment; filename=\"" + version + "\""); response.header("Content-Disposition", "attachment; filename=\"" + version + "\"");
return response.build(); return response.build();
} catch (ApplicationStorageManagementException e) { } 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); applicationUUID + " and version " + version, e);
if (e.getMessage().contains("Binary file does not exist")) { if (e.getMessage().contains("Binary file does not exist")) {
return APIUtil.getResponse(e, Response.Status.NOT_FOUND); 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); 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);
}
}
} }

@ -68,8 +68,28 @@ public interface ApplicationReleaseManager {
/** /**
* To update with a new release for an Application. * To update with a new release for an Application.
* *
* @param applicationUuid UUID of the Application
* @param applicationRelease ApplicationRelease * @param applicationRelease ApplicationRelease
* @return Updated Application Release.
* @throws ApplicationManagementException Application Management Exception. * @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;
} }

@ -58,4 +58,29 @@ public interface ApplicationStorageManager {
*/ */
public InputStream getReleasedArtifacts(String applicationUUID, String versionName) throws public InputStream getReleasedArtifacts(String applicationUUID, String versionName) throws
ApplicationStorageManagementException; 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;
} }

@ -56,4 +56,28 @@ public interface ApplicationReleaseDAO {
*/ */
List<ApplicationRelease> getApplicationReleases(String applicationUUID) throws ApplicationManagementDAOException; List<ApplicationRelease> 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;
} }

@ -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.exception.ApplicationManagementDAOException;
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
import java.sql.Connection; import java.sql.*;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; 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 ," String sql = "insert into APPM_APPLICATION_RELEASE(VERSION_NAME, RESOURCE, RELEASE_CHANNEL ,"
+ "RELEASE_DETAILS, CREATED_AT, APPM_APPLICATION_ID, IS_DEFAULT) values (?, ?, ?, ?, ?, ?, ?)"; + "RELEASE_DETAILS, CREATED_AT, APPM_APPLICATION_ID, IS_DEFAULT) values (?, ?, ?, ?, ?, ?, ?)";
int index = 0; int index = 0;
boolean isBatchExecutionSupported = ConnectionManagerUtil.isBatchQuerySupported();
try { try {
connection = this.getDBConnection(); connection = this.getDBConnection();
statement = connection.prepareStatement(sql); statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
statement.setString(++index, applicationRelease.getVersionName()); statement.setString(++index, applicationRelease.getVersionName());
statement.setString(++index, applicationRelease.getResource()); statement.setString(++index, applicationRelease.getResource());
statement.setString(++index, String.valueOf(applicationRelease.getReleaseChannel())); statement.setString(++index, String.valueOf(applicationRelease.getReleaseChannel()));
@ -68,25 +63,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
if (resultSet.next()) { if (resultSet.next()) {
applicationRelease.setId(resultSet.getInt(1)); applicationRelease.setId(resultSet.getInt(1));
} }
insertApplicationReleaseProperties(connection, applicationRelease);
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<String, String> 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();
}
}
return applicationRelease; return applicationRelease;
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException( throw new ApplicationManagementDAOException(
@ -142,7 +119,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
} }
return applicationRelease; return applicationRelease;
} catch (DBConnectionException e) { } 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 " + + "release details of the application with UUID " + applicationUuid + " and version " +
versionName, e); versionName, e);
} catch (SQLException e) { } catch (SQLException e) {
@ -205,4 +182,116 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements
Util.cleanupResources(null, rsProperties); 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<String, String> 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();
}
}
}
} }

@ -41,7 +41,8 @@ public class ApplicationReleaseManagerImpl implements ApplicationReleaseManager
@Override @Override
public ApplicationRelease createRelease(String appicationUuid, ApplicationRelease applicationRelease) throws public ApplicationRelease createRelease(String appicationUuid, ApplicationRelease applicationRelease) throws
ApplicationManagementException { ApplicationManagementException {
Application application = validateReleaseCreateRequest(appicationUuid, applicationRelease); Application application = validateApplication(appicationUuid);
validateReleaseCreateRequest(appicationUuid, applicationRelease);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Application release request is received for the application " + application.toString()); log.debug("Application release request is received for the application " + application.toString());
} }
@ -63,7 +64,7 @@ public class ApplicationReleaseManagerImpl implements ApplicationReleaseManager
@Override @Override
public ApplicationRelease getRelease(String applicationUuid, String version) throws public ApplicationRelease getRelease(String applicationUuid, String version) throws
ApplicationManagementException { ApplicationManagementException {
Application application = validationGetReleaseRequest(applicationUuid); Application application = validateApplication(applicationUuid);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Application release retrieval request is received for the application " + log.debug("Application release retrieval request is received for the application " +
application.toString() + " and version " + version); application.toString() + " and version " + version);
@ -78,7 +79,7 @@ public class ApplicationReleaseManagerImpl implements ApplicationReleaseManager
@Override @Override
public List<ApplicationRelease> getReleases(String applicationUuid) throws ApplicationManagementException { public List<ApplicationRelease> getReleases(String applicationUuid) throws ApplicationManagementException {
Application application = validationGetReleaseRequest(applicationUuid); Application application = validateApplication(applicationUuid);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request is received to retrieve all the releases related with the application " + log.debug("Request is received to retrieve all the releases related with the application " +
application.toString()); application.toString());
@ -97,8 +98,67 @@ public class ApplicationReleaseManagerImpl implements ApplicationReleaseManager
} }
@Override @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<ApplicationRelease> 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. * @param applicationUuid UUID of the Application.
* @return Application related with the UUID * @return Application related with the UUID
*/ */
private Application validationGetReleaseRequest(String applicationUuid) throws ApplicationManagementException { private Application validateApplication(String applicationUuid) throws ApplicationManagementException {
if (applicationUuid == null) { if (applicationUuid == null) {
throw new ApplicationManagementException("Application UUID is null. Application UUID is a required " 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); Application application = DataHolder.getInstance().getApplicationManager().getApplication(applicationUuid);
if (application == null) { if (application == null) {
throw new ApplicationManagementException( throw new ApplicationManagementException(
"Application with UUID " + applicationUuid + " does not exist. Cannot " "Application with UUID " + applicationUuid + " does not exist.");
+ "retrieve the releases for a non-existing application.");
} }
return application; return application;
} }
@ -126,21 +185,10 @@ public class ApplicationReleaseManagerImpl implements ApplicationReleaseManager
* *
* @param applicationUuid UUID of the Application. * @param applicationUuid UUID of the Application.
* @param applicationRelease ApplicationRelease that need to be created. * @param applicationRelease ApplicationRelease that need to be created.
* @return the Application related with the particular Application Release
* @throws ApplicationManagementException Application Management Exception. * @throws ApplicationManagementException Application Management Exception.
*/ */
private Application validateReleaseCreateRequest(String applicationUuid, ApplicationRelease applicationRelease) private void validateReleaseCreateRequest(String applicationUuid, ApplicationRelease applicationRelease)
throws ApplicationManagementException { 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) { if (applicationRelease == null || applicationRelease.getVersionName() == null) {
throw new ApplicationManagementException("ApplicationRelease version name is a mandatory parameter for " throw new ApplicationManagementException("ApplicationRelease version name is a mandatory parameter for "
+ "creating release. It cannot be found."); + "creating release. It cannot be found.");
@ -151,6 +199,6 @@ public class ApplicationReleaseManagerImpl implements ApplicationReleaseManager
+ applicationRelease.getVersionName() + " already exists. Cannot create an " + applicationRelease.getVersionName() + " already exists. Cannot create an "
+ "application release with the same version."); + "application release with the same version.");
} }
return application;
} }
} }

@ -22,6 +22,7 @@ package org.wso2.carbon.device.application.mgt.core.impl;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.application.mgt.common.Application; 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.ApplicationManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException;
import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager;
@ -117,8 +118,9 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
try { try {
application = DataHolder.getInstance().getApplicationManager().getApplication(applicationUUID); application = DataHolder.getInstance().getApplicationManager().getApplication(applicationUUID);
} catch (ApplicationManagementException e) { } catch (ApplicationManagementException e) {
throw new ApplicationStorageManagementException("Exception while retrieving the application details for " throw new ApplicationStorageManagementException(
+ "the application with UUID " + applicationUUID); "Exception while retrieving the application details for " + "the application with UUID "
+ applicationUUID);
} }
if (application == null) { if (application == null) {
throw new ApplicationStorageManagementException("Application with UUID " + applicationUUID + " does not " throw new ApplicationStorageManagementException("Application with UUID " + applicationUUID + " does not "
@ -150,8 +152,9 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
try { try {
application = DataHolder.getInstance().getApplicationManager().getApplication(applicationUUID); application = DataHolder.getInstance().getApplicationManager().getApplication(applicationUUID);
} catch (ApplicationManagementException e) { } catch (ApplicationManagementException e) {
throw new ApplicationStorageManagementException("Exception while retrieving the application details for " throw new ApplicationStorageManagementException(
+ "the application with UUID " + applicationUUID); "Exception while retrieving the application details for " + "the application with UUID "
+ applicationUUID);
} }
if (application == null) { if (application == null) {
throw new ApplicationStorageManagementException("Application with UUID " + applicationUUID + " does not " throw new ApplicationStorageManagementException("Application with UUID " + applicationUUID + " does not "
@ -171,9 +174,69 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager
try { try {
return new FileInputStream(artifactPath); return new FileInputStream(artifactPath);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
throw new ApplicationStorageManagementException("Binary file does not exist for the version " + throw new ApplicationStorageManagementException(
versionName + " for the application ", e); "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<ApplicationRelease> 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);
} }
} }
@ -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();
}
} }

@ -49,10 +49,10 @@ CREATE TABLE IF NOT EXISTS `APPM_APPLICATION_CATEGORY` (
ENGINE = InnoDB ENGINE = InnoDB
COMMENT = 'This table contains the data related to the application category'; COMMENT = 'This table contains the data related to the application category';
INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION, PUBLISHED) VALUES ('Enterprise', 'Enterprise level INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION, PUBLISHED) VALUES ('Enterprise',
applications which the artifacts need to be provided', 1); '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 INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION, PUBLISHED) VALUES ('Public',
application need to be downloaded from the public application store', 1); 'Public category in which the application need to be downloaded from the public application store', 1);
-- ----------------------------------------------------- -- -----------------------------------------------------
-- Table `APPM_LIFECYCLE_STATE` -- Table `APPM_LIFECYCLE_STATE`
@ -65,8 +65,8 @@ CREATE TABLE IF NOT EXISTS APPM_LIFECYCLE_STATE (
PRIMARY KEY (ID), PRIMARY KEY (ID),
UNIQUE INDEX APPM_LIFECYCLE_STATE_IDENTIFIER_UNIQUE (IDENTIFIER ASC)); UNIQUE INDEX APPM_LIFECYCLE_STATE_IDENTIFIER_UNIQUE (IDENTIFIER ASC));
INSERT INTO APPM_LIFECYCLE_STATE (NAME, IDENTIFIER, DESCRIPTION) VALUES ('CREATED', 'CREATED', 'Application creation INSERT INTO APPM_LIFECYCLE_STATE (NAME, IDENTIFIER, DESCRIPTION) VALUES ('CREATED', 'CREATED',
initial state'); 'Application creation initial state');
INSERT INTO APPM_LIFECYCLE_STATE (NAME, IDENTIFIER, DESCRIPTION) INSERT INTO APPM_LIFECYCLE_STATE (NAME, IDENTIFIER, DESCRIPTION)
VALUES ('IN REVIEW', 'IN REVIEW', 'Application is in in-review state'); VALUES ('IN REVIEW', 'IN REVIEW', 'Application is in in-review state');
INSERT INTO APPM_LIFECYCLE_STATE (NAME, IDENTIFIER, DESCRIPTION) INSERT INTO APPM_LIFECYCLE_STATE (NAME, IDENTIFIER, DESCRIPTION)
@ -188,7 +188,7 @@ CREATE TABLE IF NOT EXISTS `APPM_APPLICATION_PROPERTY` (
-- Table `APPM_APPLICATION_RELEASE` -- Table `APPM_APPLICATION_RELEASE`
-- ----------------------------------------------------- -- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `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, `VERSION_NAME` VARCHAR(100) NOT NULL,
`RESOURCE` TEXT NULL, `RESOURCE` TEXT NULL,
`RELEASE_CHANNEL` VARCHAR(50) NULL, `RELEASE_CHANNEL` VARCHAR(50) NULL,

Loading…
Cancel
Save