Adding support for application category

feature/appm-store/pbac
megala21 7 years ago
parent 747fe5d8c2
commit 179e34aa92

@ -22,13 +22,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.application.mgt.api.beans.ErrorResponse; 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.*;
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 javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
@ -47,6 +41,7 @@ public class APIUtil {
private static ApplicationStorageManager applicationStorageManager; private static ApplicationStorageManager applicationStorageManager;
private static SubscriptionManager subscriptionManager; private static SubscriptionManager subscriptionManager;
private static PlatformStorageManager platformStorageManager; private static PlatformStorageManager platformStorageManager;
private static CategoryManager categoryManager;
public static ApplicationManager getApplicationManager() { public static ApplicationManager getApplicationManager() {
if (applicationManager == null) { if (applicationManager == null) {
@ -171,6 +166,30 @@ public class APIUtil {
return platformStorageManager; 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) { public static Response getResponse(Exception ex, Response.Status status) {
return getResponse(ex.getMessage(), status); return getResponse(ex.getMessage(), status);
} }

@ -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.Application;
import org.wso2.carbon.device.application.mgt.common.ApplicationList; 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.ApplicationRelease;
import org.wso2.carbon.device.application.mgt.common.Category;
import java.util.List; import java.util.List;
import javax.validation.Valid; import javax.validation.Valid;
@ -101,8 +102,21 @@ import javax.ws.rs.core.Response;
description = "Delete an application", description = "Delete an application",
key = "perm:application:delete", key = "perm:application:delete",
permissions = {"/device-mgt/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") @Path("/applications")
@ -747,4 +761,114 @@ public interface ApplicationManagementAPI {
value = "Whether to make it default or not", value = "Whether to make it default or not",
required = false) required = false)
@QueryParam("isDefault") boolean isDefault); @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);
} }

@ -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.APIUtil;
import org.wso2.carbon.device.application.mgt.api.FileStreamingOutput; 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.api.services.ApplicationManagementAPI;
import org.wso2.carbon.device.application.mgt.common.Application; import org.wso2.carbon.device.application.mgt.common.*;
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.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException;
import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException; 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); 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<Category> 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);
}
}
} }

@ -18,11 +18,14 @@
*/ */
package org.wso2.carbon.device.application.mgt.common; 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. * Represents the category a particular {@link Application} belongs to.
*/ */
public class Category { public class Category {
@Exclude
private int id; private int id;
private String name; private String name;

@ -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.Filter;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; 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} * 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 createCategory(Category application) throws ApplicationManagementException;
public Category editCategory(int applicationId, Category category) throws ApplicationManagementException; public List<Category> 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;
} }

@ -18,11 +18,7 @@
*/ */
package org.wso2.carbon.device.application.mgt.core.dao; 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.*;
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.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException;
import java.util.List; import java.util.List;
@ -47,16 +43,10 @@ public interface ApplicationDAO {
int getApplicationCount(Filter filter) throws ApplicationManagementDAOException; int getApplicationCount(Filter filter) throws ApplicationManagementDAOException;
void addProperties(Map<String, String> properties) throws ApplicationManagementDAOException;
void editProperties(Map<String, String> properties) throws ApplicationManagementDAOException;
void deleteProperties(int applicationId) throws ApplicationManagementDAOException; void deleteProperties(int applicationId) throws ApplicationManagementDAOException;
void deleteTags(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 void changeLifecycle(String applicationUUID, String lifecycleIdentifier, String username, int tenantId) throws
ApplicationManagementDAOException; ApplicationManagementDAOException;
@ -66,4 +56,13 @@ public interface ApplicationDAO {
void updateScreenShotCount(String applicationUUID, int tenantId, int count) throws void updateScreenShotCount(String applicationUUID, int tenantId, int count) throws
ApplicationManagementDAOException; ApplicationManagementDAOException;
Category addCategory(Category category) throws ApplicationManagementDAOException;
List<Category> getCategories() throws ApplicationManagementDAOException;
Category getCategory(String name) throws ApplicationManagementDAOException;
boolean isApplicationExistForCategory(String name) throws ApplicationManagementDAOException;
void deleteCategory(String name) throws ApplicationManagementDAOException;
} }

@ -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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.json.JSONException; import org.json.JSONException;
import org.wso2.carbon.device.application.mgt.common.Application; import org.wso2.carbon.device.application.mgt.common.*;
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.exception.DBConnectionException; 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.ApplicationDAO;
import org.wso2.carbon.device.application.mgt.core.dao.common.Util; 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<Category> getCategories() throws ApplicationManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
String sql = "SELECT * FROM APPM_APPLICATION_CATEGORY";
List<Category> 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 @Override
public Application editApplication(Application application, int tenantId) throws ApplicationManagementDAOException { public Application editApplication(Application application, int tenantId) throws ApplicationManagementDAOException {
Connection conn; Connection conn;
@ -661,21 +798,4 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
return id; return id;
} }
@Override
public void addProperties(Map<String, String> properties) throws ApplicationManagementDAOException {
}
@Override
public void editProperties(Map<String, String> properties) throws ApplicationManagementDAOException {
}
@Override
public void addRelease(ApplicationRelease release) throws ApplicationManagementDAOException {
}
} }

@ -22,15 +22,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.application.mgt.common.Application; import org.wso2.carbon.device.application.mgt.common.*;
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.exception.ApplicationManagementException; 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.common.services.ApplicationManager;
import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO; import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO;
@ -75,8 +67,17 @@ public class ApplicationManagerImpl implements ApplicationManager {
.getTenantId(), application.getPlatform().getIdentifier()); .getTenantId(), application.getPlatform().getIdentifier());
if (platform == null) { 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(); ConnectionManagerUtil.beginDBTransaction();
// Validating the platform // Validating the platform
@ -131,6 +132,22 @@ public class ApplicationManagerImpl implements ApplicationManager {
"Platform specified by identifier " + application.getPlatform().getIdentifier() "Platform specified by identifier " + application.getPlatform().getIdentifier()
+ " is not found. Please give a valid platform identifier."); + " 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); application.setPlatform(platform);
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO(); ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO();
@ -388,8 +405,9 @@ public class ApplicationManagerImpl implements ApplicationManager {
throw new ValidationException("Username and tenant Id cannot be empty"); throw new ValidationException("Username and tenant Id cannot be empty");
} }
if (application.getCategory() == null || application.getCategory().getId() == 0) { if (application.getCategory() == null || application.getCategory().getName() == null || application
throw new ValidationException("Category id cannot be empty"); .getCategory().getName().isEmpty()) {
throw new ValidationException("Category name cannot be empty");
} }
if (application.getPlatform() == null || application.getPlatform().getIdentifier() == null) { if (application.getPlatform() == null || application.getPlatform().getIdentifier() == null) {

@ -14,13 +14,18 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
* *
*/ s*/
package org.wso2.carbon.device.application.mgt.core.impl; 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.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.exception.ApplicationManagementException;
import org.wso2.carbon.device.application.mgt.common.services.CategoryManager; 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. * 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 { public class CategoryManagerImpl implements CategoryManager {
@Override @Override
public Category createCategory(Category application) throws ApplicationManagementException { public Category createCategory(Category category) throws ApplicationManagementException {
return null; 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 @Override
public Category editCategory(int applicationId, Category category) throws ApplicationManagementException { public List<Category> getCategories() throws ApplicationManagementException {
return null; try {
ConnectionManagerUtil.openDBConnection();
return DAOFactory.getApplicationDAO().getCategories();
} finally {
ConnectionManagerUtil.closeDBConnection();
}
} }
@Override @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 @Override
public Category getCategory(Filter filter) throws ApplicationManagementException { public void deleteCategory(String name) throws ApplicationManagementException {
return null; 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();
}
}
} }

@ -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 ( CREATE TABLE IF NOT EXISTS APPM_APPLICATION_CATEGORY (
ID INT NOT NULL AUTO_INCREMENT, ID INT NOT NULL AUTO_INCREMENT,
NAME VARCHAR(100) NOT NULL, NAME VARCHAR(100) NOT NULL UNIQUE,
DESCRIPTION TEXT NULL, DESCRIPTION TEXT NULL,
PUBLISHED TINYINT NULL,
PRIMARY KEY (ID)); PRIMARY KEY (ID));
INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION, PUBLISHED) VALUES ('Enterprise', 'Enterprise level INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION) VALUES ('Sports', 'Applications that involve sports.');
applications which the artifacts need to be provided', 1); INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION) VALUES ('Education', 'Application related with education');
INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION, PUBLISHED) VALUES ('Public', 'Public category in which the INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION) VALUES ('News', 'Applications involving news');
application need to be downloaded from the public application store', 1);
-- ----------------------------------------------------- -- -----------------------------------------------------
-- Table `APPM_LIFECYCLE_STATE` -- Table `APPM_LIFECYCLE_STATE`

@ -60,17 +60,15 @@ CREATE TABLE IF NOT EXISTS `APPM_PLATFORM_TENANT_MAPPING` (
-- ----------------------------------------------------- -- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `APPM_APPLICATION_CATEGORY` ( CREATE TABLE IF NOT EXISTS `APPM_APPLICATION_CATEGORY` (
`ID` INT NOT NULL AUTO_INCREMENT, `ID` INT NOT NULL AUTO_INCREMENT,
`NAME` VARCHAR(100) NOT NULL, `NAME` VARCHAR(100) NOT NULL UNIQUE,
`DESCRIPTION` TEXT NULL, `DESCRIPTION` TEXT NULL,
`PUBLISHED` TINYINT(1) NULL,
PRIMARY KEY (`ID`)) PRIMARY KEY (`ID`))
ENGINE = InnoDB ENGINE = InnoDB
COMMENT = 'This table contains the data related to the application category'; COMMENT = 'This table contains the data related to the application category';
INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION, PUBLISHED) VALUES ('Enterprise', INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION) VALUES ('Sports', 'Applications that involve sports.');
'Enterprise level applications which the artifacts need to be provided', 1); INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION) VALUES ('Education', 'Application related with education');
INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION, PUBLISHED) VALUES ('Public', INSERT INTO APPM_APPLICATION_CATEGORY (NAME, DESCRIPTION) VALUES ('News', 'Applications involving news');
'Public category in which the application need to be downloaded from the public application store', 1);
-- ----------------------------------------------------- -- -----------------------------------------------------
-- Table `APPM_LIFECYCLE_STATE` -- Table `APPM_LIFECYCLE_STATE`

Loading…
Cancel
Save