diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java index 7755a77af9..aa0e74b229 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/APIUtil.java @@ -22,13 +22,7 @@ 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.api.beans.ErrorResponse; -import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; -import org.wso2.carbon.device.application.mgt.common.services.ApplicationReleaseManager; -import org.wso2.carbon.device.application.mgt.common.services.ApplicationStorageManager; -import org.wso2.carbon.device.application.mgt.common.services.LifecycleStateManager; -import org.wso2.carbon.device.application.mgt.common.services.PlatformManager; -import org.wso2.carbon.device.application.mgt.common.services.PlatformStorageManager; -import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager; +import org.wso2.carbon.device.application.mgt.common.services.*; import javax.ws.rs.core.Response; @@ -47,6 +41,7 @@ public class APIUtil { private static ApplicationStorageManager applicationStorageManager; private static SubscriptionManager subscriptionManager; private static PlatformStorageManager platformStorageManager; + private static CategoryManager categoryManager; public static ApplicationManager getApplicationManager() { if (applicationManager == null) { @@ -171,6 +166,30 @@ public class APIUtil { return platformStorageManager; } + + /** + * To get the Category Manager from the osgi context. + * + * @return CategoryManager instance in the current osgi context. + */ + public static CategoryManager getCategoryManager() { + if (categoryManager == null) { + synchronized (APIUtil.class) { + if (categoryManager == null) { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + categoryManager = (CategoryManager) ctx + .getOSGiService(CategoryManager.class, null); + if (categoryManager == null) { + String msg = "Category Manager service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + } + } + } + return categoryManager; + } + public static Response getResponse(Exception ex, Response.Status status) { return getResponse(ex.getMessage(), status); } 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 1a8cca69e7..65c1b7d468 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 @@ -36,6 +36,7 @@ import org.wso2.carbon.device.application.mgt.api.beans.ErrorResponse; import org.wso2.carbon.device.application.mgt.common.Application; import org.wso2.carbon.device.application.mgt.common.ApplicationList; import org.wso2.carbon.device.application.mgt.common.ApplicationRelease; +import org.wso2.carbon.device.application.mgt.common.Category; import java.util.List; import javax.validation.Valid; @@ -101,8 +102,21 @@ import javax.ws.rs.core.Response; description = "Delete an application", key = "perm:application:delete", permissions = {"/device-mgt/application/delete"} + ), + @Scope( + name = "Create an application category", + description = "Create an application category", + key = "perm:application-category:create", + permissions = {"/device-mgt/application/category/create"} + ), + @Scope( + name = "Delete an Application", + description = "Delete an application", + key = "perm:application-category:delete", + permissions = {"/device-mgt/application/category/delete"} ) + } ) @Path("/applications") @@ -747,4 +761,114 @@ public interface ApplicationManagementAPI { value = "Whether to make it default or not", required = false) @QueryParam("isDefault") boolean isDefault); + + + @POST + @Path("/category") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "POST", + value = "Create an application category", + notes = "This will create a new category", + tags = "Application Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:application-category:create") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 201, + message = "OK. \n Successfully created a new category.", + response = Category.class), + @ApiResponse( + code = 400, + message = "Bad request. Required parameters are not provided"), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while creating application category.", + response = ErrorResponse.class) + }) + Response createCategory( + @ApiParam( + name = "category", + value = "The category that need to be created.", + required = true) + @Valid Category category); + + @GET + @Path("/category") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Get existing application categories", + notes = "This will retrieve the existing categories", + tags = "Application Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:application:create") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully retrieved existing categories.", + response = Application.class), + @ApiResponse( + code = 400, + message = "Bad request. Required parameters are not provided"), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while getting the application list.", + response = ErrorResponse.class) + }) + Response getCategories(); + + @DELETE + @Path("/category/{name}") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "DELETE", + value = "Delete application category with the given name", + notes = "This will delete the application category with the given name", + tags = "Application Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:application-category:delete") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully retrieved existing categories.", + response = Application.class), + @ApiResponse( + code = 400, + message = "Bad request. Required parameters are not provided"), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while getting the application list.", + response = ErrorResponse.class) + }) + Response deleteCategory( + @ApiParam( + name = "Name", + value = "Name of the application category", + required = true) + @PathParam("name") String name); } 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 2043014fc0..aa16e889d1 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 @@ -25,11 +25,7 @@ import org.apache.cxf.jaxrs.ext.multipart.Multipart; import org.wso2.carbon.device.application.mgt.api.APIUtil; import org.wso2.carbon.device.application.mgt.api.FileStreamingOutput; import org.wso2.carbon.device.application.mgt.api.services.ApplicationManagementAPI; -import org.wso2.carbon.device.application.mgt.common.Application; -import org.wso2.carbon.device.application.mgt.common.ApplicationList; -import org.wso2.carbon.device.application.mgt.common.ApplicationRelease; -import org.wso2.carbon.device.application.mgt.common.Filter; -import org.wso2.carbon.device.application.mgt.common.ImageArtifact; +import org.wso2.carbon.device.application.mgt.common.*; 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.ResourceManagementException; @@ -542,4 +538,51 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } } + + @Override + @POST + @Path("/category") + public Response createCategory(@Valid Category category) { + if (category == null) { + return Response.status(Response.Status.BAD_REQUEST).entity("Category is null. cannot create the " + + "category").build(); + } + try { + Category createdCategory = APIUtil.getCategoryManager().createCategory(category); + return Response.status(Response.Status.CREATED).entity(createdCategory).build(); + } catch (ApplicationManagementException e) { + log.error("Application Management Exception while trying to create the application category", e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } + } + + @Override + @GET + @Path("/category") + public Response getCategories() { + List categories; + try { + categories = APIUtil.getCategoryManager().getCategories(); + return Response.status(Response.Status.OK).entity(categories).build(); + } catch (ApplicationManagementException e) { + log.error("Application Management Exception while trying to get application categories", e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } + } + + @Override + @DELETE + @Path("/category/{name}") + public Response deleteCategory(@PathParam("name") String name) { + if (name == null || name.isEmpty()) { + return Response.status(Response.Status.BAD_REQUEST).entity("Name cannot be null or empty.").build(); + } + try { + APIUtil.getCategoryManager().deleteCategory(name); + return Response.status(Response.Status.OK).entity("Successfully deleted the category.").build(); + } catch (ApplicationManagementException e) { + log.error("Application Management Exception while trying to delete category", e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Category.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Category.java index f4fc5a0721..84d8a4edd7 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Category.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Category.java @@ -18,11 +18,14 @@ */ package org.wso2.carbon.device.application.mgt.common; +import org.wso2.carbon.device.application.mgt.common.jaxrs.Exclude; + /** * Represents the category a particular {@link Application} belongs to. */ public class Category { + @Exclude private int id; private String name; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/CategoryManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/CategoryManager.java index 824ac9ca42..b5b4e20b6d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/CategoryManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/CategoryManager.java @@ -22,6 +22,8 @@ import org.wso2.carbon.device.application.mgt.common.Category; import org.wso2.carbon.device.application.mgt.common.Filter; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; +import java.util.List; + /** * CategoryManager is responsible for handling add, delete, update opertaions related with {@link Category} */ @@ -29,9 +31,9 @@ public interface CategoryManager { public Category createCategory(Category application) throws ApplicationManagementException; - public Category editCategory(int applicationId, Category category) throws ApplicationManagementException; + public List getCategories() throws ApplicationManagementException; - public void deleteCategory(int applicationId) throws ApplicationManagementException; + public Category getCategory(String name) throws ApplicationManagementException; - public Category getCategory(Filter filter) throws ApplicationManagementException; + public void deleteCategory(String name) throws ApplicationManagementException; } 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 f49a2d3f4b..80174015d6 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 @@ -18,11 +18,7 @@ */ package org.wso2.carbon.device.application.mgt.core.dao; -import org.wso2.carbon.device.application.mgt.common.Application; -import org.wso2.carbon.device.application.mgt.common.ApplicationList; -import org.wso2.carbon.device.application.mgt.common.ApplicationRelease; -import org.wso2.carbon.device.application.mgt.common.Filter; -import org.wso2.carbon.device.application.mgt.common.LifecycleStateTransition; +import org.wso2.carbon.device.application.mgt.common.*; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import java.util.List; @@ -47,16 +43,10 @@ public interface ApplicationDAO { int getApplicationCount(Filter filter) throws ApplicationManagementDAOException; - void addProperties(Map properties) throws ApplicationManagementDAOException; - - void editProperties(Map properties) throws ApplicationManagementDAOException; - void deleteProperties(int applicationId) throws ApplicationManagementDAOException; void deleteTags(int applicationId) throws ApplicationManagementDAOException; - void addRelease(ApplicationRelease release) throws ApplicationManagementDAOException; - void changeLifecycle(String applicationUUID, String lifecycleIdentifier, String username, int tenantId) throws ApplicationManagementDAOException; @@ -66,4 +56,13 @@ public interface ApplicationDAO { void updateScreenShotCount(String applicationUUID, int tenantId, int count) throws ApplicationManagementDAOException; + Category addCategory(Category category) throws ApplicationManagementDAOException; + + List getCategories() throws ApplicationManagementDAOException; + + Category getCategory(String name) throws ApplicationManagementDAOException; + + boolean isApplicationExistForCategory(String name) throws ApplicationManagementDAOException; + + void deleteCategory(String name) throws ApplicationManagementDAOException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/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 98d5bcf4d3..b79dca2017 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 @@ -21,12 +21,7 @@ package org.wso2.carbon.device.application.mgt.core.dao.impl.application; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.json.JSONException; -import org.wso2.carbon.device.application.mgt.common.Application; -import org.wso2.carbon.device.application.mgt.common.ApplicationList; -import org.wso2.carbon.device.application.mgt.common.ApplicationRelease; -import org.wso2.carbon.device.application.mgt.common.Filter; -import org.wso2.carbon.device.application.mgt.common.LifecycleStateTransition; -import org.wso2.carbon.device.application.mgt.common.Pagination; +import org.wso2.carbon.device.application.mgt.common.*; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.Util; @@ -445,6 +440,148 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } + @Override + public Category addCategory(Category category) throws ApplicationManagementDAOException { + Connection connection; + PreparedStatement statement = null; + String sql = "INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION) VALUES (?, ?)"; + String[] generatedColumns = { "ID" }; + ResultSet rs = null; + + try { + connection = this.getDBConnection(); + statement = connection.prepareStatement(sql, generatedColumns); + statement.setString(1, category.getName()); + statement.setString(2, category.getDescription()); + statement.executeUpdate(); + rs = statement.getGeneratedKeys(); + if (rs.next()) { + category.setId(rs.getInt(1)); + } + return category; + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException( + "Database connection while trying to update the categroy " + category.getName()); + } catch (SQLException e) { + throw new ApplicationManagementDAOException("SQL exception while executing the query '" + sql + "' .", e); + } finally { + Util.cleanupResources(statement, rs); + } + } + + @Override + public List getCategories() throws ApplicationManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + String sql = "SELECT * FROM APPM_APPLICATION_CATEGORY"; + List categories = new ArrayList<>(); + + try { + conn = this.getDBConnection(); + stmt = conn.prepareStatement(sql); + rs = stmt.executeQuery(); + while (rs.next()) { + Category category = new Category(); + category.setId(rs.getInt("ID")); + category.setName(rs.getString("NAME")); + category.setDescription(rs.getString("DESCRIPTION")); + categories.add(category); + } + return categories; + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException("Database Connection Exception while trying to get the " + + "application categories", e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException("SQL Exception while trying to get the application " + + "categories, while executing " + sql, e); + } finally { + Util.cleanupResources(stmt, rs); + } + + } + + @Override + public Category getCategory(String name) throws ApplicationManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + String sql = "SELECT * FROM APPM_APPLICATION_CATEGORY WHERE NAME = ?"; + + try { + conn = this.getDBConnection(); + stmt = conn.prepareStatement(sql); + stmt.setString(1, name); + rs = stmt.executeQuery(); + if (rs.next()) { + Category category = new Category(); + category.setId(rs.getInt("ID")); + category.setName(rs.getString("NAME")); + category.setDescription(rs.getString("DESCRIPTION")); + return category; + } + return null; + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException("Database Connection Exception while trying to get the " + + "application categories", e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException("SQL Exception while trying to get the application " + + "categories, while executing " + sql, e); + } finally { + Util.cleanupResources(stmt, rs); + } + } + + @Override + public boolean isApplicationExistForCategory(String name) throws ApplicationManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + String sql = "SELECT * FROM APPM_APPLICATION WHERE APPLICATION_CATEGORY_ID = (ID FROM " + + "APPM_APPLICATION_CATEGORY WHERE NAME = ?)"; + + try { + conn = this.getDBConnection(); + stmt = conn.prepareStatement(sql); + stmt.setString(1, name); + rs = stmt.executeQuery(); + return rs.next(); + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException( + "Database Connection Exception while trying to check the " + "applications for teh category " + + name, e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException( + "SQL Exception while trying to get the application related with categories, while executing " + + sql, e); + } finally { + Util.cleanupResources(stmt, rs); + } + } + + @Override + public void deleteCategory(String name) throws ApplicationManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + String sql = "DELETE FROM APPM_APPLICATION_CATEGORY WHERE NAME = ?"; + + try { + conn = this.getDBConnection(); + stmt = conn.prepareStatement(sql); + stmt.setString(1, name); + stmt.executeUpdate(); + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException( + "Database Connection Exception while trying to delete the category " + name, e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException( + "SQL Exception while trying to delete the category " + name + " while executing the query " + + sql, e); + } finally { + Util.cleanupResources(stmt, null); + } + } + @Override public Application editApplication(Application application, int tenantId) throws ApplicationManagementDAOException { Connection conn; @@ -661,21 +798,4 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic return id; } - - @Override - public void addProperties(Map properties) throws ApplicationManagementDAOException { - - } - - @Override - public void editProperties(Map properties) throws ApplicationManagementDAOException { - - } - - @Override - public void addRelease(ApplicationRelease release) 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/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 4d87356ce7..31da0f4ff5 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 @@ -22,15 +22,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.application.mgt.common.Application; -import org.wso2.carbon.device.application.mgt.common.ApplicationList; -import org.wso2.carbon.device.application.mgt.common.Filter; -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.LifecycleStateTransition; -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.common.Visibility; +import org.wso2.carbon.device.application.mgt.common.*; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO; @@ -75,8 +67,17 @@ public class ApplicationManagerImpl implements ApplicationManager { .getTenantId(), application.getPlatform().getIdentifier()); if (platform == null) { - throw new NotFoundException("Invalid platform"); + throw new NotFoundException( + "Invalid platform is provided for the application " + application.getUuid()); } + + Category category = DataHolder.getInstance().getCategoryManager() + .getCategory(application.getCategory().getName()); + if (category == null) { + throw new NotFoundException( + "Invalid Category is provided for the application " + application.getUuid()); + } + application.setCategory(category); ConnectionManagerUtil.beginDBTransaction(); // Validating the platform @@ -131,6 +132,22 @@ public class ApplicationManagerImpl implements ApplicationManager { "Platform specified by identifier " + application.getPlatform().getIdentifier() + " is not found. Please give a valid platform identifier."); } + + if (application.getCategory() != null) { + String applicationCategoryName = application.getCategory().getName(); + if (applicationCategoryName == null || applicationCategoryName.isEmpty()) { + throw new ApplicationManagementException("Application category name cannot be null or " + + "empty. Cannot edit the application."); + } + Category category = DataHolder.getInstance().getCategoryManager() + .getCategory(application.getCategory().getName()); + if (category == null) { + throw new NotFoundException( + "Invalid Category is provided for the application " + application.getUuid() + ". " + + "Cannot edit application"); + } + application.setCategory(category); + } application.setPlatform(platform); ConnectionManagerUtil.beginDBTransaction(); ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO(); @@ -388,8 +405,9 @@ public class ApplicationManagerImpl implements ApplicationManager { throw new ValidationException("Username and tenant Id cannot be empty"); } - if (application.getCategory() == null || application.getCategory().getId() == 0) { - throw new ValidationException("Category id cannot be empty"); + if (application.getCategory() == null || application.getCategory().getName() == null || application + .getCategory().getName().isEmpty()) { + throw new ValidationException("Category name cannot be empty"); } if (application.getPlatform() == null || application.getPlatform().getIdentifier() == 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/CategoryManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/CategoryManagerImpl.java index 58089fa300..064bd6ef2f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/CategoryManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/CategoryManagerImpl.java @@ -14,13 +14,18 @@ * specific language governing permissions and limitations * under the License. * -*/ +s*/ package org.wso2.carbon.device.application.mgt.core.impl; import org.wso2.carbon.device.application.mgt.common.Category; -import org.wso2.carbon.device.application.mgt.common.Filter; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.services.CategoryManager; +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.exception.NotFoundException; +import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; + +import java.util.List; /** * This class is the default implementation for the CategoryManager. @@ -29,22 +34,75 @@ import org.wso2.carbon.device.application.mgt.common.services.CategoryManager; public class CategoryManagerImpl implements CategoryManager { @Override - public Category createCategory(Category application) throws ApplicationManagementException { - return null; + public Category createCategory(Category category) throws ApplicationManagementException { + if (category == null) { + throw new ApplicationManagementException("Category is null. Cannot create a category."); + } + if (category.getName() == null) { + throw new ApplicationManagementException( + "Application category name cannot be null. Application " + "category creation failed"); + } + try { + ConnectionManagerUtil.beginDBTransaction(); + Category createdCategory = DAOFactory.getApplicationDAO().addCategory(category); + ConnectionManagerUtil.commitDBTransaction(); + return createdCategory; + } catch (ApplicationManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw e; + } finally { + ConnectionManagerUtil.closeDBConnection(); + } } @Override - public Category editCategory(int applicationId, Category category) throws ApplicationManagementException { - return null; + public List getCategories() throws ApplicationManagementException { + try { + ConnectionManagerUtil.openDBConnection(); + return DAOFactory.getApplicationDAO().getCategories(); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } } @Override - public void deleteCategory(int applicationId) throws ApplicationManagementException { - + public Category getCategory(String name) throws ApplicationManagementException { + if (name == null || name.isEmpty()) { + throw new ApplicationManagementException("Name cannot be empty or null. Cannot get category"); + } + try { + ConnectionManagerUtil.openDBConnection(); + return DAOFactory.getApplicationDAO().getCategory(name); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } } @Override - public Category getCategory(Filter filter) throws ApplicationManagementException { - return null; + public void deleteCategory(String name) throws ApplicationManagementException { + Category category = getCategory(name); + if (category == null) { + throw new NotFoundException( + "Category with the name '" + name + "' not found. Cannot delete the " + "non-existing category"); + } + try { + ConnectionManagerUtil.beginDBTransaction(); + boolean isApplicationExistForCategory = DAOFactory.getApplicationDAO().isApplicationExistForCategory(name); + + if (isApplicationExistForCategory) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw new ApplicationManagementException( + "Cannot delete the the category " + name + ". Applications " + "exists for this category"); + } + + DAOFactory.getApplicationDAO().deleteCategory(name); + ConnectionManagerUtil.commitDBTransaction(); + } catch (ApplicationManagementDAOException e) { + ConnectionManagerUtil.rollbackDBTransaction(); + throw e; + } finally { + ConnectionManagerUtil.closeDBConnection(); + } } + } 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 21855a963b..ad3d082962 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 @@ -44,15 +44,13 @@ CREATE INDEX IF NOT EXISTS FK_PLATFROM_TENANT_MAPPING_PLATFORM ON APPM_PLATFORM_ -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS APPM_APPLICATION_CATEGORY ( ID INT NOT NULL AUTO_INCREMENT, - NAME VARCHAR(100) NOT NULL, + NAME VARCHAR(100) NOT NULL UNIQUE, DESCRIPTION TEXT NULL, - PUBLISHED TINYINT NULL, PRIMARY KEY (ID)); -INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION, PUBLISHED) VALUES ('Enterprise', 'Enterprise level -applications which the artifacts need to be provided', 1); -INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION, PUBLISHED) VALUES ('Public', 'Public category in which the -application need to be downloaded from the public application store', 1); +INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION) VALUES ('Sports', 'Applications that involve sports.'); +INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION) VALUES ('Education', 'Application related with education'); +INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION) VALUES ('News', 'Applications involving news'); -- ----------------------------------------------------- -- Table `APPM_LIFECYCLE_STATE` 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 e680b9c65d..14b21ad7f8 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 @@ -60,17 +60,15 @@ CREATE TABLE IF NOT EXISTS `APPM_PLATFORM_TENANT_MAPPING` ( -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS `APPM_APPLICATION_CATEGORY` ( `ID` INT NOT NULL AUTO_INCREMENT, - `NAME` VARCHAR(100) NOT NULL, + `NAME` VARCHAR(100) NOT NULL UNIQUE, `DESCRIPTION` TEXT NULL, - `PUBLISHED` TINYINT(1) NULL, PRIMARY KEY (`ID`)) ENGINE = InnoDB COMMENT = 'This table contains the data related to the application category'; -INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION, PUBLISHED) VALUES ('Enterprise', -'Enterprise level applications which the artifacts need to be provided', 1); -INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION, PUBLISHED) VALUES ('Public', -'Public category in which the application need to be downloaded from the public application store', 1); +INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION) VALUES ('Sports', 'Applications that involve sports.'); +INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION) VALUES ('Education', 'Application related with education'); +INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION) VALUES ('News', 'Applications involving news'); -- ----------------------------------------------------- -- Table `APPM_LIFECYCLE_STATE`