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 ca883183cc..9c524570ba 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 @@ -232,7 +232,6 @@ public interface ApplicationManagementAPI { required = true) @Valid Application application); - @POST @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @@ -649,4 +648,98 @@ public interface ApplicationManagementAPI { name = "version", value = "Version of the application") @QueryParam("version") String version); + + @GET + @Path("/image-artifacts/{uuid}") + @Produces(MediaType.APPLICATION_OCTET_STREAM) + @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:get") + }) + } + ) + @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 getApplicationImageArtifacts( + @ApiParam( + name = "UUID", + value = "Unique identifier of the Application", + required = true) + @PathParam("uuid") String applicationUUID, + @ApiParam( + name = "name", + value = "Name of the artifact to be retrieved", + required = true) + @QueryParam("name") String name, + @ApiParam( + name = "count", + value = "Count of the screen-shot artifact to be retrieved", + required = false) + @QueryParam("count") int count); + + @PUT + @Consumes("application/json") + @Path("/{uuid}/{version}/{channel}") + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "PUT", + value = "Make the particular application release as default or not", + notes = "Make the particular application release as default or not", + tags = "Application Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:application-mgt:login") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully retrieved the lifecycle states.", + response = List.class), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while getting the life-cycle states.", + response = ErrorResponse.class) + }) + Response updateDefaultVersion( + @ApiParam( + name = "UUID", + value = "Unique identifier of the Application", + required = true) + @PathParam("uuid") String applicationUUID, + @ApiParam( + name = "Version", + value = "Version of the Application Release", + required = true) + @PathParam("version") String version, + @ApiParam( + name = "Release Channel", + value = "Release Channel", + required = true) + @PathParam("channel") String channel, + @ApiParam( + name = "isDefault", + value = "Whether to make it default or not", + required = false) + @QueryParam("isDefault") boolean isDefault); } 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 e488db3fb5..ad968282b4 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 @@ -425,7 +425,51 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { + applicationUUID).build(); } } catch (ApplicationManagementException e) { - log.error("Error while deleting application release with the applicaion UUID " + applicationUUID, e); + log.error("Error while deleting application release with the application UUID " + applicationUUID, e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } + } + + @Override + @GET + @Path("/image-artifacts/{uuid}") + @Produces(MediaType.APPLICATION_OCTET_STREAM) + public Response getApplicationImageArtifacts(@PathParam("uuid") String applicationUUID, + @QueryParam("name") String name, @QueryParam("count") int count) { + if (name == null || name.isEmpty()) { + return Response.status(Response.Status.BAD_REQUEST).entity("Name should not be null. Name is mandatory to" + + " retrieve the particular image artifact of the release").build(); + } + ApplicationStorageManager applicationStorageManager = APIUtil.getApplicationStorageManager(); + try { + InputStream imageArtifact = applicationStorageManager.getImageArtifact(applicationUUID, name, count); + FileStreamingOutput fileStreamingOutput = new FileStreamingOutput(imageArtifact); + Response.ResponseBuilder response = Response.status(Response.Status.OK).entity(fileStreamingOutput); + response.header("Content-Disposition", "attachment; filename=\"" + name + "\""); + return response.build(); + } catch (ApplicationStorageManagementException e) { + log.error("Application Storage Management Exception while getting the image artifact " + name + " of " + + "the application with UUID " + applicationUUID, e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } + } + + @Override + @PUT + @Consumes("application/json") + @Path("/{uuid}/{version}/{channel}") + public Response updateDefaultVersion(@PathParam("uuid") String applicationUUID, @PathParam("version") String + version, @PathParam("channel") String channel, @QueryParam("isDefault") boolean isDefault) { + ApplicationReleaseManager applicationReleaseManager = APIUtil.getApplicationReleaseManager(); + try { + applicationReleaseManager.changeDefaultRelease(applicationUUID, version, isDefault, channel); + return Response.status(Response.Status.OK) + .entity("Successfully changed the default version for the " + "release channel " + channel + + " for the application UUID " + applicationUUID).build(); + } catch (ApplicationManagementException e) { + log.error("Application Release Management Exception while changing the default release for the release " + + "channel " + channel + " for the application with UUID " + applicationUUID + " for the version " + + version); 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/Application.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java index 4fe9a25c38..77c84bdf6a 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java @@ -43,14 +43,8 @@ public class Application { private String description; - private String iconName; - - private String bannerName; - private String videoName; - private List screenshots; - private List tags; private Platform platform; @@ -73,6 +67,8 @@ public class Application { private Visibility visibility; + private int screenShotCount; + private User user; public int getId() { @@ -155,22 +151,6 @@ public class Application { this.description = description; } - public String getIconName() { - return iconName; - } - - public void setIconName(String iconName) { - this.iconName = iconName; - } - - public String getBannerName() { - return bannerName; - } - - public void setBannerName(String bannerName) { - this.bannerName = bannerName; - } - public String getVideoName() { return videoName; } @@ -179,14 +159,6 @@ public class Application { this.videoName = videoName; } - public List getScreenshots() { - return screenshots; - } - - public void setScreenshots(List screenshots) { - this.screenshots = screenshots; - } - public List getTags() { return tags; } @@ -251,6 +223,14 @@ public class Application { this.user = user; } + public void setScreenShotCount (int screenShotCount) { + this.screenShotCount = screenShotCount; + } + + public int getScreenShotCount() { + return screenShotCount; + } + @Override public String toString() { return "UUID : " + uuid + "\tIdentifier : " + identifier + "\tName : " + name + "\tShort Description : " 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 d0b8d546d0..a97050d19f 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 @@ -25,7 +25,7 @@ import java.util.List; /** * ApplicationReleaseManager is responsible for handling all the operations related with - * {@link org.wso2.carbon.device.application.mgt.common.ApplicationRelease} which involving addition, updation , + * {@link org.wso2.carbon.device.application.mgt.common.ApplicationRelease} which involving addition, updating , * deletion and viewing. * */ @@ -59,11 +59,15 @@ public interface ApplicationReleaseManager { public List getReleases(String applicationUuid) throws ApplicationManagementException; /** - * To make a release as the default one for an application. - * - * @param id ID of the ApplicationRelease, that need to be made default. + * To make a particular application release as the default / not default-one + * @param uuid UUID of the application + * @param version Version of the application + * @param isDefault is default or not. + * @param releaseChannel Release channel to make the + * @throws ApplicationManagementException Application Management Exception. */ - public void makeDefaultRelease(int id) throws ApplicationManagementException; + public void changeDefaultRelease(String uuid, String version, boolean isDefault, String releaseChannel) + throws ApplicationManagementException; /** * To update with a new release for an Application. 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 8d1f073f10..860812244a 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 @@ -83,4 +83,16 @@ public interface ApplicationStorageManager { */ public void deleteAllApplicationReleaseArtifacts(String applicationUUID) throws ApplicationStorageManagementException; + + /** + * To get particular image artifact of the application. + * + * @param applicationUUID UUID of the application, to retrieve the image artifact. + * @param name Name of the artifact - icon/banner/screenshot + * @param count Position of a parameter to get the image artifact. + * @return the relevant image artifact. + * @throws ApplicationStorageManagementException Application Storage Management Exception. + */ + public InputStream getImageArtifact(String applicationUUID, String name, int count) 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/ApplicationDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java index 3fbd9ea56b..6c556757c3 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationDAO.java @@ -35,15 +35,15 @@ public interface ApplicationDAO { Application createApplication(Application application) throws ApplicationManagementDAOException; - ApplicationList getApplications(Filter filter) throws ApplicationManagementDAOException; + ApplicationList getApplications(Filter filter, int tenantId) throws ApplicationManagementDAOException; - Application getApplication(String uuid) throws ApplicationManagementDAOException; + Application getApplication(String uuid, int tenantId) throws ApplicationManagementDAOException; - int getApplicationId(String uuid) throws ApplicationManagementDAOException; + int getApplicationId(String uuid, int tenantId) throws ApplicationManagementDAOException; Application editApplication(Application application, int tenantId) throws ApplicationManagementDAOException; - void deleteApplication(String uuid) throws ApplicationManagementDAOException; + void deleteApplication(String uuid, int tenantId) throws ApplicationManagementDAOException; int getApplicationCount(Filter filter) throws ApplicationManagementDAOException; @@ -57,10 +57,13 @@ public interface ApplicationDAO { void addRelease(ApplicationRelease release) throws ApplicationManagementDAOException; - void changeLifecycle(String applicationUUID, String lifecycleIdentifier, String username) throws + void changeLifecycle(String applicationUUID, String lifecycleIdentifier, String username, int tenantId) throws ApplicationManagementDAOException; List getNextLifeCycleStates(String applicationUUID, int tenantId) throws ApplicationManagementDAOException; + void updateScreenShotCount(String applicationUUID, int tenantId, int count) 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/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 1cb2becfd1..2fce310266 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 @@ -45,7 +45,8 @@ public interface ApplicationReleaseDAO { * @return ApplicationRelease for the particular version of the given application * @throws ApplicationManagementDAOException Application Management DAO Exception. */ - ApplicationRelease getRelease(String applicationUuid, String versionName) throws ApplicationManagementDAOException; + ApplicationRelease getRelease(String applicationUuid, String versionName, int tenantId) throws + ApplicationManagementDAOException; /** * To get all the releases of a particular application. @@ -54,7 +55,8 @@ public interface ApplicationReleaseDAO { * @return list of the application releases * @throws ApplicationManagementDAOException Application Management DAO Exception. */ - List getApplicationReleases(String applicationUUID) throws ApplicationManagementDAOException; + List getApplicationReleases(String applicationUUID, int tenantId) throws + ApplicationManagementDAOException; /** * To update an Application release. @@ -80,4 +82,16 @@ public interface ApplicationReleaseDAO { * @throws ApplicationManagementDAOException Application Management DAO Exception. */ void deleteReleaseProperties(int id) throws ApplicationManagementDAOException; + + /** + * To change the default version of a particular release channel. + * @param uuid UUID of the application + * @param version Version of the application + * @param isDefault true if the request is to make the application as default one unless false + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ + void changeReleaseDefault(String uuid, String version, boolean isDefault, String releaseChannel, int tenantId) + 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/common/Util.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java index bb4284b00c..769e8a50ed 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java @@ -27,7 +27,6 @@ import org.wso2.carbon.device.application.mgt.common.Lifecycle; import org.wso2.carbon.device.application.mgt.common.LifecycleState; import org.wso2.carbon.device.application.mgt.common.Platform; import org.wso2.carbon.device.application.mgt.common.User; -import org.wso2.carbon.device.application.mgt.core.util.JSONUtil; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -63,10 +62,8 @@ public class Util { application.setIdentifier(rs.getString("IDENTIFIER")); application.setShortDescription(rs.getString("SHORT_DESCRIPTION")); application.setDescription(rs.getString("DESCRIPTION")); - application.setIconName(rs.getString("ICON_NAME")); - application.setBannerName(rs.getString("BANNER_NAME")); + application.setScreenShotCount(rs.getInt("SCREEN_SHOT_COUNT")); application.setVideoName(rs.getString("VIDEO_NAME")); - application.setScreenshots(JSONUtil.jsonArrayStringToList(rs.getString("SCREENSHOTS"))); application.setCreatedAt(rs.getDate("CREATED_AT")); application.setModifiedAt(rs.getDate("MODIFIED_AT")); application.setUser(new User(rs.getString("CREATED_BY"), rs.getInt("TENANT_ID"))); 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/GenericApplicationDAOImpl.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/GenericApplicationDAOImpl.java index b513fd1289..6f921a104b 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/GenericApplicationDAOImpl.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/GenericApplicationDAOImpl.java @@ -33,7 +33,6 @@ import org.wso2.carbon.device.application.mgt.core.dao.common.Util; 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 org.wso2.carbon.device.application.mgt.core.util.JSONUtil; import java.sql.Connection; import java.sql.Date; @@ -65,34 +64,32 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic ResultSet rs = null; String sql = ""; boolean isBatchExecutionSupported = ConnectionManagerUtil.isBatchQuerySupported(); - + int index = 0; try { conn = this.getDBConnection(); - sql += "INSERT INTO APPM_APPLICATION (UUID, IDENTIFIER, NAME, SHORT_DESCRIPTION, DESCRIPTION, ICON_NAME, " - + "BANNER_NAME, VIDEO_NAME, SCREENSHOTS, CREATED_BY, CREATED_AT, MODIFIED_AT, " + sql += "INSERT INTO APPM_APPLICATION (UUID, IDENTIFIER, NAME, SHORT_DESCRIPTION, DESCRIPTION, " + + "VIDEO_NAME, SCREEN_SHOT_COUNT, CREATED_BY, CREATED_AT, MODIFIED_AT, " + "APPLICATION_CATEGORY_ID, PLATFORM_ID, TENANT_ID, LIFECYCLE_STATE_ID, " + "LIFECYCLE_STATE_MODIFIED_AT, LIFECYCLE_STATE_MODIFIED_BY) VALUES " - + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); - stmt.setString(1, application.getUuid()); - stmt.setString(2, application.getIdentifier()); - stmt.setString(3, application.getName()); - stmt.setString(4, application.getShortDescription()); - stmt.setString(5, application.getDescription()); - stmt.setString(6, application.getIconName()); - stmt.setString(7, application.getBannerName()); - stmt.setString(8, application.getVideoName()); - stmt.setString(9, JSONUtil.listToJsonArrayString(application.getScreenshots())); - stmt.setString(10, application.getUser().getUserName()); - stmt.setDate(11, new Date(application.getCreatedAt().getTime())); - stmt.setDate(12, new Date(application.getModifiedAt().getTime())); - stmt.setInt(13, application.getCategory().getId()); - stmt.setInt(14, application.getPlatform().getId()); - stmt.setInt(15, application.getUser().getTenantId()); - stmt.setInt(16, application.getCurrentLifecycle().getLifecycleState().getId()); - stmt.setDate(17, new Date(application.getCurrentLifecycle().getLifecycleStateModifiedAt().getTime())); - stmt.setString(18, application.getCurrentLifecycle().getGetLifecycleStateModifiedBy()); + stmt.setString(++index, application.getUuid()); + stmt.setString(++index, application.getIdentifier()); + stmt.setString(++index, application.getName()); + stmt.setString(++index, application.getShortDescription()); + stmt.setString(++index, application.getDescription()); + stmt.setString(++index, application.getVideoName()); + stmt.setInt(++index, application.getScreenShotCount()); + stmt.setString(++index, application.getUser().getUserName()); + stmt.setDate(++index, new Date(application.getCreatedAt().getTime())); + stmt.setDate(++index, new Date(application.getModifiedAt().getTime())); + stmt.setInt(++index, application.getCategory().getId()); + stmt.setInt(++index, application.getPlatform().getId()); + stmt.setInt(++index, application.getUser().getTenantId()); + stmt.setInt(++index, application.getCurrentLifecycle().getLifecycleState().getId()); + stmt.setDate(++index, new Date(application.getCurrentLifecycle().getLifecycleStateModifiedAt().getTime())); + stmt.setString(++index, application.getCurrentLifecycle().getGetLifecycleStateModifiedBy()); stmt.executeUpdate(); rs = stmt.getGeneratedKeys(); @@ -112,7 +109,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic @Override - public ApplicationList getApplications(Filter filter) throws ApplicationManagementDAOException { + public ApplicationList getApplications(Filter filter, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Getting application data from the database"); log.debug(String.format("Filter: limit=%s, offset=%", filter.getLimit(), filter.getOffset())); @@ -125,6 +122,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic ApplicationList applicationList = new ApplicationList(); List applications = new ArrayList<>(); Pagination pagination = new Pagination(); + int index = 0; if (filter == null) { throw new ApplicationManagementDAOException("Filter need to be instantiated"); @@ -140,15 +138,15 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic + "LS.DESCRIPTION AS LS_DESCRIPTION FROM APPM_APPLICATION AS APP INNER JOIN APPM_PLATFORM AS " + "APL ON APP.PLATFORM_ID = APL.ID INNER JOIN APPM_APPLICATION_CATEGORY AS CAT ON " + "APP.APPLICATION_CATEGORY_ID = CAT.ID INNER JOIN APPM_LIFECYCLE_STATE AS " - + "LS ON APP.LIFECYCLE_STATE_ID = LS.ID "; + + "LS ON APP.LIFECYCLE_STATE_ID = LS.ID WHERE APP.TENANT_ID = ? "; if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) { - sql += "WHERE APP.NAME LIKE ? "; + sql += "AND APP.NAME LIKE ? "; } sql += "LIMIT ?,?;"; stmt = conn.prepareStatement(sql); - int index = 0; + stmt.setInt(++index, tenantId); if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) { stmt.setString(++index, "%" + filter.getSearchQuery() + "%"); } @@ -183,11 +181,14 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic applicationList.setApplications(applications); applicationList.setPagination(pagination); } catch (SQLException e) { - throw new ApplicationManagementDAOException("Error occurred while getting application List", e); + throw new ApplicationManagementDAOException("Error occurred while getting application list for the tenant" + + " " + tenantId + ". While executing " + sql, e); } catch (JSONException e) { - throw new ApplicationManagementDAOException("Error occurred while parsing JSON", e); + throw new ApplicationManagementDAOException("Error occurred while parsing JSON, while getting application" + + " list for the tenant " + tenantId, e); } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); + throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection while " + + "getting application list for the tenant " + tenantId, e); } finally { Util.cleanupResources(stmt, rs); } @@ -243,11 +244,11 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } @Override - public Application getApplication(String uuid) throws ApplicationManagementDAOException { + public Application getApplication(String uuid, int tenantId) throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Getting application with the UUID(" + uuid + ") from the database"); } - Connection conn = null; + Connection conn; PreparedStatement stmt = null; ResultSet rs = null; String sql = ""; @@ -260,10 +261,11 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic + "LS.DESCRIPTION AS LS_DESCRIPTION FROM APPM_APPLICATION AS APP INNER JOIN APPM_PLATFORM AS " + "APL ON APP.PLATFORM_ID = APL.ID INNER JOIN APPM_APPLICATION_CATEGORY AS CAT ON " + "APP.APPLICATION_CATEGORY_ID = CAT.ID INNER JOIN APPM_LIFECYCLE_STATE AS " - + "LS ON APP.LIFECYCLE_STATE_ID = LS.ID WHERE UUID = ?"; + + "LS ON APP.LIFECYCLE_STATE_ID = LS.ID WHERE UUID = ? AND APP.TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, uuid); + stmt.setInt(2, tenantId); rs = stmt.executeQuery(); if (log.isDebugEnabled()) { @@ -301,8 +303,8 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } @Override - public void changeLifecycle(String applicationUUID, String lifecycleIdentifier, String userName) throws - ApplicationManagementDAOException { + public void changeLifecycle(String applicationUUID, String lifecycleIdentifier, String userName, int tenantId) + throws ApplicationManagementDAOException { if (log.isDebugEnabled()) { log.debug("Change Life cycle status change " + lifecycleIdentifier + "request received to the DAO " + "level for the application with " + "the UUID '" + applicationUUID + "' from the user " @@ -314,12 +316,14 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic conn = this.getDBConnection(); String sql = "UPDATE APPM_APPLICATION SET " + "LIFECYCLE_STATE_ID = (SELECT ID FROM APPM_LIFECYCLE_STATE WHERE IDENTIFIER = ?), " - + "LIFECYCLE_STATE_MODIFIED_BY = ?, LIFECYCLE_STATE_MODIFIED_AT = ? WHERE UUID = ?"; + + "LIFECYCLE_STATE_MODIFIED_BY = ?, LIFECYCLE_STATE_MODIFIED_AT = ? WHERE UUID = ? AND TENANT_ID " + + "= ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, lifecycleIdentifier); stmt.setString(2, userName); stmt.setDate(3, new Date(System.currentTimeMillis())); stmt.setString(4, applicationUUID); + stmt.setInt(5, tenantId); stmt.executeUpdate(); } catch (DBConnectionException e) { throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); @@ -341,13 +345,14 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic String sql = "SELECT STATE.NAME, TRANSITION.DESCRIPTION, TRANSITION.PERMISSION FROM ( SELECT * FROM " + "APPM_LIFECYCLE_STATE ) STATE RIGHT JOIN (SELECT * FROM APPM_LIFECYCLE_STATE_TRANSITION WHERE " - + "INITIAL_STATE = (SELECT LIFECYCLE_STATE_ID FROM APPM_APPLICATION WHERE UUID = ?)) " + + "INITIAL_STATE = (SELECT LIFECYCLE_STATE_ID FROM APPM_APPLICATION WHERE UUID = ? AND TENANT_ID = ?)) " + "TRANSITION ON TRANSITION.NEXT_STATE = STATE.ID"; try { connection = this.getDBConnection(); preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, applicationUUID); + preparedStatement.setInt(2, tenantId); resultSet = preparedStatement.executeQuery(); List lifecycleStateTransitions = new ArrayList<>(); @@ -370,6 +375,31 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } } + @Override + public void updateScreenShotCount(String applicationUUID, int tenantId, int count) + throws ApplicationManagementDAOException { + Connection connection; + PreparedStatement statement = null; + String sql = "UPDATE APPM_APPLICATION SET SCREEN_SHOT_COUNT = ? where UUID = ? and TENANT_ID = ?"; + + try { + connection = this.getDBConnection(); + statement = connection.prepareStatement(sql); + statement.setInt(1, count); + statement.setString(2, applicationUUID); + statement.setInt(3, tenantId); + statement.executeUpdate(); + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException("Database connection while trying to update the screen-shot " + + "count for the application with UUID " + applicationUUID + " for the tenant " + tenantId); + } catch (SQLException e) { + throw new ApplicationManagementDAOException("SQL exception while executing the query '" + sql + "' .", e); + } finally { + Util.cleanupResources(statement, null); + } + + } + @Override public Application editApplication(Application application, int tenantId) throws ApplicationManagementDAOException { Connection conn; @@ -380,9 +410,9 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic conn = this.getDBConnection(); int index = 0; sql += "UPDATE APPM_APPLICATION SET NAME = IFNULL (?, NAME), SHORT_DESCRIPTION = IFNULL " - + "(?, SHORT_DESCRIPTION), DESCRIPTION = IFNULL (?, DESCRIPTION), ICON_NAME = IFNULL (?, ICON_NAME)" - + ", BANNER_NAME = IFNULL (?, BANNER_NAME), VIDEO_NAME = IFNULL (?, VIDEO_NAME), " - + "SCREENSHOTS = IFNULL (?, SCREENSHOTS), MODIFIED_AT = IFNULL (?, MODIFIED_AT), "; + + "(?, SHORT_DESCRIPTION), DESCRIPTION = IFNULL (?, DESCRIPTION), SCREEN_SHOT_COUNT = IFNULL (?, " + + "SCREEN_SHOT_COUNT), VIDEO_NAME = IFNULL (?, VIDEO_NAME), " + + "MODIFIED_AT = IFNULL (?, MODIFIED_AT), "; if (application.getPayment() != null) { sql += " IS_FREE = IFNULL (?, IS_FREE), "; @@ -404,10 +434,8 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic stmt.setString(++index, application.getName()); stmt.setString(++index, application.getShortDescription()); stmt.setString(++index, application.getDescription()); - stmt.setString(++index, application.getIconName()); - stmt.setString(++index, application.getBannerName()); + stmt.setInt(++index, application.getScreenShotCount()); stmt.setString(++index, application.getVideoName()); - stmt.setString(++index, JSONUtil.listToJsonArrayString(application.getScreenshots())); stmt.setDate(++index, new Date(application.getModifiedAt().getTime())); if (application.getPayment() != null) { stmt.setBoolean(++index, application.getPayment().isFreeApp()); @@ -427,7 +455,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic stmt.setString(++index, application.getUuid()); stmt.executeUpdate(); - application.setId(getApplicationId(application.getUuid())); + application.setId(getApplicationId(application.getUuid(), tenantId)); sql = "DELETE FROM APPM_APPLICATION_TAG WHERE APPLICATION_ID = ?"; stmt = conn.prepareStatement(sql); @@ -499,14 +527,15 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } @Override - public void deleteApplication(String uuid) throws ApplicationManagementDAOException { + public void deleteApplication(String uuid, int tenantId) throws ApplicationManagementDAOException { Connection conn; PreparedStatement stmt = null; try { conn = this.getDBConnection(); - String sql = "DELETE FROM APPM_APPLICATION WHERE UUID = ?"; + String sql = "DELETE FROM APPM_APPLICATION WHERE UUID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, uuid); + stmt.setInt(2, tenantId); stmt.executeUpdate(); } catch (DBConnectionException e) { @@ -561,7 +590,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } @Override - public int getApplicationId(String uuid) throws ApplicationManagementDAOException { + public int getApplicationId(String uuid, int tenantId) throws ApplicationManagementDAOException { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; @@ -569,9 +598,10 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic int id = 0; try { conn = this.getDBConnection(); - sql = "SELECT ID FROM APPM_APPLICATION WHERE UUID = ?"; + sql = "SELECT ID FROM APPM_APPLICATION WHERE UUID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, uuid); + stmt.setInt(2, tenantId); rs = stmt.executeQuery(); if (rs.next()) { id = rs.getInt(1); 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 50d39a780d..163326f075 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,7 +27,12 @@ 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.*; +import java.sql.Connection; +import java.sql.Date; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -44,6 +49,10 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements Connection connection; PreparedStatement statement = null; ResultSet resultSet = null; + + if (applicationRelease.isDefault()) { + + } 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; @@ -79,13 +88,13 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements } @Override - public ApplicationRelease getRelease(String applicationUuid, String versionName) + public ApplicationRelease getRelease(String applicationUuid, String versionName, int tenantId) throws ApplicationManagementDAOException { Connection connection; PreparedStatement statement = null; ResultSet resultSet = null; String sql = "SELECT * FROM APPM_APPLICATION_RELEASE WHERE VERSION_NAME = ? AND APPM_APPLICATION_ID = " - + "(SELECT ID FROM APPM_APPLICATION WHERE UUID = ?)"; + + "(SELECT ID FROM APPM_APPLICATION WHERE UUID = ? and TENANT_ID = ?)"; ApplicationRelease applicationRelease = null; ResultSet rsProperties = null; @@ -94,6 +103,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements statement = connection.prepareStatement(sql); statement.setString(1, versionName); statement.setString(2, applicationUuid); + statement.setInt(3, tenantId); resultSet = statement.executeQuery(); if (resultSet.next()) { @@ -132,13 +142,13 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements } @Override - public List getApplicationReleases(String applicationUUID) + public List getApplicationReleases(String applicationUUID, int tenantId) throws ApplicationManagementDAOException { Connection connection; PreparedStatement statement = null; ResultSet resultSet = null; String sql = "SELECT * FROM APPM_APPLICATION_RELEASE WHERE APPM_APPLICATION_ID = (SELECT ID FROM " - + "APPM_APPLICATION WHERE UUID = ?)"; + + "APPM_APPLICATION WHERE UUID = ? AND TENANT_ID = ?)"; List applicationReleases = new ArrayList<>(); ResultSet rsProperties = null; @@ -146,6 +156,7 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements connection = this.getDBConnection(); statement = connection.prepareStatement(sql); statement.setString(1, applicationUUID); + statement.setInt(2, tenantId); resultSet = statement.executeQuery(); while (resultSet.next()) { @@ -264,6 +275,40 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements } } + @Override + public void changeReleaseDefault(String uuid, String version, boolean isDefault, String releaseChannel, + int tenantId) throws ApplicationManagementDAOException { + Connection connection; + PreparedStatement statement = null; + String sql = "UPDATE APPM_APPLICATION_RELEASE SET IS_DEFAULT = ? AND RELEASE_CHANNEL = ? WHERE " + + "APPM_APPLICATION_ID = (SELECT ID from APPM_APPLICATION WHERE UUID = ? AND TENANT_ID = ?) " + + "AND VERSION_NAME = ?"; + + try { + if (isDefault) { + removeDefaultReleases(uuid, releaseChannel, tenantId); + } + connection = this.getDBConnection(); + statement = connection.prepareStatement(sql); + statement.setBoolean(1, isDefault); + statement.setString(2, releaseChannel.toUpperCase()); + statement.setString(3, uuid); + statement.setInt(4, tenantId); + statement.setString(5, version); + statement.executeUpdate(); + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException( + "Database Connection exception while try to change the " + "default release of the release channel " + + releaseChannel + " for the application " + uuid, e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException( + "SQL Exception while trying to change the default release of " + "the release channel " + + releaseChannel + " for the application " + uuid, e); + } finally { + Util.cleanupResources(statement, null); + } + } + /** * To insert the application release properties. * @param connection Database Connection @@ -294,4 +339,30 @@ public class GenericApplicationReleaseDAOImpl extends AbstractDAOImpl implements } } } + + /** + * To make all the releases of particular release channel as non-default ones. + * + * @param uuid UUID of the Application. + * @param releaseChannel ReleaseChannel for which we need to make all the releases as non-default ones. + * @param tenantId ID of the tenant. + * @throws DBConnectionException Database Connection Exception. + * @throws SQLException SQL Exception. + */ + private void removeDefaultReleases(String uuid, String releaseChannel, int tenantId) + throws DBConnectionException, SQLException { + PreparedStatement statement = null; + try { + Connection connection = this.getDBConnection(); + String sql = "UPDATE APPM_APPLICATION_RELEASE SET IS_DEFAULT = FALSE WHERE APPM_APPLICATION_ID = (SELECT " + + "ID from APPM_APPLICATION WHERE UUID = ? AND TENANT_ID = ?) AND RELEASE_CHANNEL = ?"; + statement = connection.prepareStatement(sql); + statement.setString(1, uuid); + statement.setInt(2, tenantId); + statement.setString(3, releaseChannel.toUpperCase()); + statement.executeUpdate(); + } finally { + Util.cleanupResources(statement, null); + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index 837ba1ea7c..f9f8dae680 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -46,7 +46,6 @@ import org.wso2.carbon.user.api.UserRealm; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.utils.multitenancy.MultitenantUtils; -import java.io.InputStream; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -158,10 +157,10 @@ public class ApplicationManagerImpl implements ApplicationManager { try { ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO(); ConnectionManagerUtil.beginDBTransaction(); - int appId = applicationDAO.getApplicationId(uuid); + int appId = applicationDAO.getApplicationId(uuid, tenantId); applicationDAO.deleteTags(appId); applicationDAO.deleteProperties(appId); - applicationDAO.deleteApplication(uuid); + applicationDAO.deleteApplication(uuid, tenantId); ConnectionManagerUtil.commitDBTransaction(); } catch (ApplicationManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); @@ -174,15 +173,14 @@ public class ApplicationManagerImpl implements ApplicationManager { @Override public ApplicationList getApplications(Filter filter) throws ApplicationManagementException { - + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { ConnectionManagerUtil.openDBConnection(); ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO(); - return applicationDAO.getApplications(filter); + return applicationDAO.getApplications(filter, tenantId); } finally { ConnectionManagerUtil.closeDBConnection(); } - } @Override @@ -190,6 +188,7 @@ public class ApplicationManagerImpl implements ApplicationManager { ApplicationManagementException { boolean isAvailableNextState = false; String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); List nextLifeCycles = getLifeCycleStates(applicationUuid); for (LifecycleStateTransition lifecycleStateTransition : nextLifeCycles) { @@ -210,7 +209,7 @@ public class ApplicationManagerImpl implements ApplicationManager { try { ConnectionManagerUtil.beginDBTransaction(); ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO(); - applicationDAO.changeLifecycle(applicationUuid, lifecycleIdentifier, userName); + applicationDAO.changeLifecycle(applicationUuid, lifecycleIdentifier, userName, tenantId); ConnectionManagerUtil.commitDBTransaction(); } catch (ApplicationManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); @@ -278,20 +277,15 @@ public class ApplicationManagerImpl implements ApplicationManager { @Override public Application getApplication(String uuid) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { ConnectionManagerUtil.openDBConnection(); - return DAOFactory.getApplicationDAO().getApplication(uuid); + return DAOFactory.getApplicationDAO().getApplication(uuid, tenantId); } finally { ConnectionManagerUtil.closeDBConnection(); } } - public void uploadArtifacts(String applicationUUID, InputStream iconFileStream, InputStream bannerFileStream, - List screenShotStreams) - throws ApplicationManagementException { - - } - /** * To check whether current user is application owner or admin. * @@ -310,7 +304,7 @@ public class ApplicationManagerImpl implements ApplicationManager { } try { ConnectionManagerUtil.openDBConnection(); - Application application = DAOFactory.getApplicationDAO().getApplication(applicationUUID); + Application application = DAOFactory.getApplicationDAO().getApplication(applicationUUID, tenantId); return application.getUser().getUserName().equals(userName) && application.getUser().getTenantId() == tenantId; } finally { 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 39eb043c82..b405f8f3db 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 @@ -20,6 +20,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.context.PrivilegedCarbonContext; 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; @@ -64,6 +65,7 @@ public class ApplicationReleaseManagerImpl implements ApplicationReleaseManager @Override public ApplicationRelease getRelease(String applicationUuid, String version) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); Application application = validateApplication(applicationUuid); if (log.isDebugEnabled()) { log.debug("Application release retrieval request is received for the application " + @@ -71,7 +73,7 @@ public class ApplicationReleaseManagerImpl implements ApplicationReleaseManager } try { ConnectionManagerUtil.openDBConnection(); - return DAOFactory.getApplicationReleaseDAO().getRelease(applicationUuid, version); + return DAOFactory.getApplicationReleaseDAO().getRelease(applicationUuid, version, tenantId); } finally { ConnectionManagerUtil.closeDBConnection(); } @@ -79,6 +81,7 @@ public class ApplicationReleaseManagerImpl implements ApplicationReleaseManager @Override public List getReleases(String applicationUuid) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); Application application = validateApplication(applicationUuid); if (log.isDebugEnabled()) { log.debug("Request is received to retrieve all the releases related with the application " + @@ -86,15 +89,33 @@ public class ApplicationReleaseManagerImpl implements ApplicationReleaseManager } try { ConnectionManagerUtil.openDBConnection(); - return DAOFactory.getApplicationReleaseDAO().getApplicationReleases(applicationUuid); + return DAOFactory.getApplicationReleaseDAO().getApplicationReleases(applicationUuid, tenantId); } finally { ConnectionManagerUtil.closeDBConnection(); } } @Override - public void makeDefaultRelease(int id) throws ApplicationManagementException { + public void changeDefaultRelease(String uuid, String version, boolean isDefault, String releaseChannel) throws + ApplicationManagementException { + Application application = validateApplication(uuid); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + if (log.isDebugEnabled()) { + log.debug("Request received to change the default release for the release channel " + releaseChannel + + "for the application " + application.toString()); + } + try { + ConnectionManagerUtil.beginDBTransaction(); + DAOFactory.getApplicationReleaseDAO() + .changeReleaseDefault(uuid, version, isDefault, releaseChannel, tenantId); + ConnectionManagerUtil.commitDBTransaction(); + } catch (ApplicationManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw e; + } finally { + ConnectionManagerUtil.closeDBConnection(); + } } @Override 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 c5882ef1d8..287cd5370b 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 @@ -21,12 +21,18 @@ 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.context.PrivilegedCarbonContext; 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.exception.DBConnectionException; +import org.wso2.carbon.device.application.mgt.common.exception.TransactionManagementException; import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; +import org.wso2.carbon.device.application.mgt.core.dao.common.DAOFactory; +import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; +import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.application.mgt.core.util.Constants; import java.io.File; @@ -36,6 +42,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.Arrays; import java.util.List; /** @@ -47,18 +54,8 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager @Override public void uploadImageArtifacts(String applicationUUID, InputStream iconFileStream, InputStream bannerFileStream, List screenShotStreams) 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 upload the artifacts to non-existing application."); - } + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + Application application = validateApplication(applicationUUID); String artifactDirectoryPath = Constants.artifactPath + application.getId(); if (log.isDebugEnabled()) { log.debug("Artifact Directory Path for saving the artifacts related with application " + applicationUUID @@ -66,10 +63,8 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager } createArtifactDirectory(artifactDirectoryPath); if (iconFileStream != null) { - String iconName = application.getIconName(); - iconName = (iconName == null) ? "icon" : iconName; try { - saveFile(iconFileStream, artifactDirectoryPath + File.separator + iconName); + saveFile(iconFileStream, artifactDirectoryPath + File.separator + Constants.IMAGE_ARTIFACTS[0]); } catch (IOException e) { throw new ApplicationStorageManagementException( "IO Exception while saving the icon file in the server for " + "the application " @@ -77,10 +72,8 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager } } if (bannerFileStream != null) { - String bannerName = application.getBannerName(); - bannerName = (bannerName == null) ? "banner" : bannerName; try { - saveFile(bannerFileStream, artifactDirectoryPath + File.separator + bannerName); + saveFile(bannerFileStream, artifactDirectoryPath + File.separator + Constants.IMAGE_ARTIFACTS[1]); } catch (IOException e) { throw new ApplicationStorageManagementException( "IO Exception while saving the banner file in the server for" + " the application " @@ -88,18 +81,11 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager } } if (screenShotStreams != null) { - int count = 1; + int count = application.getScreenShotCount() + 1; String screenshotName; - List screenShotNames = application.getScreenshots(); - boolean isScreenShotNameExist = (screenShotNames == null || screenShotNames.isEmpty()); - int screenShotNameLength = isScreenShotNameExist ? screenShotNames.size() : 0; for (InputStream screenshotStream : screenShotStreams) { try { - if (isScreenShotNameExist && count <= screenShotNameLength) { - screenshotName = screenShotNames.get(count); - } else { - screenshotName = "screenshot_" + count; - } + screenshotName = Constants.IMAGE_ARTIFACTS[2] + count; saveFile(screenshotStream, artifactDirectoryPath + File.separator + screenshotName); count++; } catch (IOException e) { @@ -108,24 +94,35 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager e); } } + try { + ConnectionManagerUtil.beginDBTransaction(); + DAOFactory.getApplicationDAO().updateScreenShotCount(applicationUUID, tenantId, count - 1); + ConnectionManagerUtil.commitDBTransaction(); + } catch (TransactionManagementException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new ApplicationStorageManagementException("Transaction Management exception while trying to " + + "update the screen-shot count of the application " + applicationUUID + " for the tenant " + + tenantId, e); + } catch (DBConnectionException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new ApplicationStorageManagementException("Database connection management exception while " + + "trying to update the screen-shot count for the application " + applicationUUID + " for the" + + " tenant " + tenantId, e); + } catch (ApplicationManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new ApplicationStorageManagementException("Application Management DAO exception while trying to" + + " update the screen-shot count for the application " + applicationUUID + " for the tenant " + + tenantId, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } } } @Override public void uploadReleaseArtifacts(String applicationUUID, String versionName, InputStream binaryFile) 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 upload release artifacts for not existing application."); - } + Application application = validateApplication(applicationUUID); String artifactDirectoryPath = Constants.artifactPath + application.getId(); if (log.isDebugEnabled()) { log.debug("Artifact Directory Path for saving the application release related artifacts related with " @@ -148,18 +145,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager @Override public InputStream getReleasedArtifacts(String applicationUUID, String versionName) 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 retrieve release artifacts for not existing application."); - } + Application application = validateApplication(applicationUUID); String artifactPath = Constants.artifactPath + application.getId() + File.separator + versionName; if (log.isDebugEnabled()) { @@ -182,18 +168,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager @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."); - } + Application application = validateApplication(applicationUUID); String artifactDirectoryPath = Constants.artifactPath + application.getId(); File artifactDirectory = new File(artifactDirectoryPath); @@ -205,18 +180,7 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager @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."); - } + Application application = validateApplication(applicationUUID); String artifactPath = Constants.artifactPath + application.getId() + File.separator + version; File artifact = new File(artifactPath); @@ -225,8 +189,10 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager } } - @Override public void deleteAllApplicationReleaseArtifacts(String applicationUUID) - throws ApplicationStorageManagementException { + @Override + public void deleteAllApplicationReleaseArtifacts(String applicationUUID) throws + ApplicationStorageManagementException { + Application application = validateApplication(applicationUUID); try { List applicationReleases = DataHolder.getInstance().getReleaseManager() .getReleases(applicationUUID); @@ -240,6 +206,31 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager } } + @Override + public InputStream getImageArtifact(String applicationUUID, String name, int count) throws + ApplicationStorageManagementException { + Application application = validateApplication(applicationUUID); + validateImageArtifactNames(name); + String imageArtifactPath = Constants.artifactPath + application.getId() + File.separator + name.toLowerCase(); + + if (name.equalsIgnoreCase(Constants.IMAGE_ARTIFACTS[2])) { + imageArtifactPath += count; + } + File imageFile = new File(imageArtifactPath); + if (!imageFile.exists()) { + throw new ApplicationStorageManagementException( + "Image artifact " + name + " does not exist for the " + "application with UUID " + applicationUUID); + } else { + try { + return new FileInputStream(imageArtifactPath); + } catch (FileNotFoundException e) { + throw new ApplicationStorageManagementException( + "File not found exception while trying to get the image artifact " + name + " for the " + + "application " + applicationUUID, e); + } + } + } + /** * To save a file in a given location. * @@ -294,4 +285,44 @@ public class ApplicationStorageManagerImpl implements ApplicationStorageManager } artifactDirectory.delete(); } + + /** + * To validate the image artifact names. + * @param name Name of the image artifact. + * @throws ApplicationStorageManagementException Application Storage Management Exception + */ + private void validateImageArtifactNames(String name) throws ApplicationStorageManagementException { + if (name == null || name.isEmpty()) { + throw new ApplicationStorageManagementException("Image artifact name cannot be null or empty. It is a " + + "required parameter"); + } + if (!Arrays.asList(Constants.IMAGE_ARTIFACTS).contains(name.toLowerCase())) { + throw new ApplicationStorageManagementException("Provide artifact name is not valid. Please provide the " + + "name among " + Arrays.toString(Constants.IMAGE_ARTIFACTS)); + } + } + + /** + * To validate the Application before storing and retrieving the artifacts of a particular application. + * + * @param uuid UUID of the Application + * @return {@link Application} if it is validated + * @throws ApplicationStorageManagementException Application Storage Management Exception will be thrown if a + * valid application related with the specific UUID + * could not be found. + */ + private Application validateApplication(String uuid) throws ApplicationStorageManagementException { + Application application = null; + try { + application = DataHolder.getInstance().getApplicationManager().getApplication(uuid); + } catch (ApplicationManagementException e) { + throw new ApplicationStorageManagementException( + "Exception while retrieving the application details for the application with UUID " + + uuid); + } + if (application == null) { + throw new ApplicationStorageManagementException("Application with UUID " + uuid + " does not exist."); + } + 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/util/Constants.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/Constants.java index 467be38824..9c6992b204 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/Constants.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/Constants.java @@ -58,4 +58,9 @@ public class Constants { * Path to save the Application related artifacts. */ public static String artifactPath = ""; + + /** + * Name of the image artifacts that are saved in the file system. + */ + public static final String[] IMAGE_ARTIFACTS = {"icon", "banner", "screenshot"}; } diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml index 1fb22c2b4a..cd8091d261 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml @@ -61,7 +61,6 @@ - ${carbon.home}/repository/resources/mobileapps/images/ - ${carbon.home}/repository/resources/mobileapps/binary/ + repository/resources/mobileapps/ \ No newline at end of file diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql index f344747a4a..809efada34 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql @@ -125,10 +125,8 @@ CREATE TABLE IF NOT EXISTS `APPM_APPLICATION` ( `NAME` VARCHAR(100) NOT NULL, `SHORT_DESCRIPTION` VARCHAR(255) NULL, `DESCRIPTION` TEXT NULL, - `ICON_NAME` VARCHAR(100) NULL, - `BANNER_NAME` VARCHAR(100) NULL, + `SCREEN_SHOT_COUNT` INT DEFAULT 0, `VIDEO_NAME` VARCHAR(100) NULL, - `SCREENSHOTS` TEXT NULL, `CREATED_BY` VARCHAR(255) NULL, `CREATED_AT` DATETIME NOT NULL, `MODIFIED_AT` DATETIME NULL, @@ -183,7 +181,7 @@ CREATE TABLE IF NOT EXISTS APPM_APPLICATION_RELEASE ( ID INT NOT NULL AUTO_INCREMENT, VERSION_NAME VARCHAR(100) NOT NULL, RESOURCE TEXT NULL, - RELEASE_CHANNEL VARCHAR(50) NULL, + RELEASE_CHANNEL VARCHAR(50) DEFAULT 'ALPHA', RELEASE_DETAILS TEXT NULL, CREATED_AT DATETIME NOT NULL, APPM_APPLICATION_ID INT NOT NULL, 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 379b6de3da..4845697026 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 @@ -126,10 +126,8 @@ CREATE TABLE IF NOT EXISTS `APPM_APPLICATION` ( `NAME` VARCHAR(100) NOT NULL, `SHORT_DESCRIPTION` VARCHAR(255) NULL, `DESCRIPTION` TEXT NULL, - `ICON_NAME` VARCHAR(100) NULL, - `BANNER_NAME` VARCHAR(100) NULL, `VIDEO_NAME` VARCHAR(100) NULL, - `SCREENSHOTS` TEXT NULL, + `SCREEN_SHOT_COUNT` INT DEFAULT 0, `CREATED_BY` VARCHAR(255) NULL, `CREATED_AT` DATETIME NOT NULL, `MODIFIED_AT` DATETIME NULL, @@ -191,7 +189,7 @@ CREATE TABLE IF NOT EXISTS `APPM_APPLICATION_RELEASE` ( `ID` INT NOT NULL AUTO_INCREMENT UNIQUE , `VERSION_NAME` VARCHAR(100) NOT NULL, `RESOURCE` TEXT NULL, - `RELEASE_CHANNEL` VARCHAR(50) NULL, + `RELEASE_CHANNEL` VARCHAR(50) DEFAULT 'ALPHA', `RELEASE_DETAILS` TEXT NULL, `CREATED_AT` DATETIME NOT NULL, `APPM_APPLICATION_ID` INT NOT NULL, @@ -307,8 +305,8 @@ CREATE TABLE IF NOT EXISTS `APPM_PLATFORM_TENANT_MAPPING` ( CONSTRAINT `fk_APPM_PLATFORM_TENANT_MAPPING_APPM_SUPPORTED_PLATFORM1` FOREIGN KEY (`PLATFORM_ID`) REFERENCES `APPM_PLATFORM` (`ID`) - ON DELETE NO ACTION - ON UPDATE NO ACTION) + ON DELETE CASCADE + ON UPDATE CASCADE ) ENGINE = InnoDB COMMENT = 'This table contains the data related relationship between application platofrm and appication mappings';