From d8ff31b628835e7fd40a0a4b3c1eb4fffed840f7 Mon Sep 17 00:00:00 2001 From: megala21 Date: Mon, 9 Oct 2017 21:09:50 +0530 Subject: [PATCH] Adding remaining changes --- ...pplicationCategoryManagementException.java | 6 - .../mgt/core/dao/ApplicationDAO.java | 106 ++++++++++-- .../application/mgt/core/dao/CategoryDAO.java | 39 +++++ .../mgt/core/dao/common/DAOFactory.java | 25 ++- .../GenericApplicationDAOImpl.java | 135 ++-------------- .../impl/category/GenericCategoryDAOImpl.java | 151 ++++++++++++++++++ .../mgt/core/impl/CategoryManagerImpl.java | 18 +-- 7 files changed, 316 insertions(+), 164 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/category/GenericCategoryDAOImpl.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/ApplicationCategoryManagementException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/ApplicationCategoryManagementException.java index 522d08086d..5cf73b868d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/ApplicationCategoryManagementException.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/ApplicationCategoryManagementException.java @@ -23,14 +23,8 @@ package org.wso2.carbon.device.application.mgt.common.exception; * Exception that will be thrown during Application Category Management. */ public class ApplicationCategoryManagementException extends ApplicationManagementException { - public ApplicationCategoryManagementException(String message, Throwable throwable) { - super(message, throwable); - setMessage(message); - } - public ApplicationCategoryManagementException(String message) { super(message); setMessage(message); } - } 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 4d5c09b031..1e4874dd6f 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 @@ -20,7 +20,6 @@ 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.Category; 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; @@ -51,36 +50,111 @@ public interface ApplicationDAO { */ ApplicationList getApplications(Filter filter, int tenantId) throws ApplicationManagementDAOException; + /** + * To get the application with the given uuid + * + * @param uuid UUID of the application to be retrieved. + * @param tenantId ID of the tenant. + * @param userName Name of the user. + * @return the application + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ Application getApplication(String uuid, int tenantId, String userName) throws ApplicationManagementDAOException; + /** + * To get the application id of the application specified by the UUID + * + * @param uuid UUID of the application. + * @param tenantId ID of the tenant. + * @return ID of the Application. + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ int getApplicationId(String uuid, int tenantId) throws ApplicationManagementDAOException; + /** + * To edit the given application. + * + * @param application Application that need to be edited. + * @param tenantId Tenant ID of the Application. + * @return Updated Application. + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ Application editApplication(Application application, int tenantId) throws ApplicationManagementDAOException; + /** + * To delete the application identified by the UUID + * + * @param uuid UUID of the application. + * @param tenantId ID of tenant which the Application belongs to. + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ void deleteApplication(String uuid, int tenantId) throws ApplicationManagementDAOException; + /** + * To get the application count that satisfies gives search query. + * + * @param filter Application Filter. + * @return count of the applications + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ int getApplicationCount(Filter filter) throws ApplicationManagementDAOException; + /** + * To delete the properties of a application. + * + * @param applicationId ID of the application to delete the properties. + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ void deleteProperties(int applicationId) throws ApplicationManagementDAOException; + /** + * To delete the tags of a application. + * + * @param applicationId ID of the application to delete the tags. + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ void deleteTags(int applicationId) throws ApplicationManagementDAOException; - 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; - - Category addCategory(Category category) throws ApplicationManagementDAOException; - - List getCategories() throws ApplicationManagementDAOException; + /** + * To change the lifecycle state of the application. + * + * @param applicationUUID UUID of the application. + * @param lifecycleIdentifier New lifecycle state. + * @param username Name of the user. + * @param tenantId ID of the tenant. + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ + void changeLifecycle(String applicationUUID, String lifecycleIdentifier, String username, int tenantId) + throws ApplicationManagementDAOException; - Category getCategory(String name) throws ApplicationManagementDAOException; + /** + * To get the next possible lifecycle states for the application. + * + * @param applicationUUID UUID of the application. + * @param tenantId ID of the tenant. + * @return Next possible lifecycle states. + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ + List getNextLifeCycleStates(String applicationUUID, int tenantId) + throws ApplicationManagementDAOException; - boolean isApplicationExistForCategory(String name) throws ApplicationManagementDAOException; + /** + * To update the screen-shot count of a application. + * + * @param applicationUUID UUID of the application. + * @param tenantId ID of the tenant. + * @param count New count of the screen-shots. + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ + void updateScreenShotCount(String applicationUUID, int tenantId, int count) + throws ApplicationManagementDAOException; - void deleteCategory(String name) throws ApplicationManagementDAOException; + /** + * To check whether atleast one application exist under category. + * + * @param categoryName Name of the category. + * @return true if atleast one application exist under the given category, otherwise false. + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ + boolean isApplicationExist(String categoryName) 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/CategoryDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/CategoryDAO.java index 856a3f9628..d003552af3 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/CategoryDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/CategoryDAO.java @@ -18,8 +18,47 @@ */ package org.wso2.carbon.device.application.mgt.core.dao; +import org.wso2.carbon.device.application.mgt.common.Category; +import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; + +import java.util.List; + /** * This is responsible for Application Category related DAO operations. */ public interface CategoryDAO { + + /** + * To add a new category. + * + * @param category Category that need to be added. + * @return Newly added category. + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ + Category addCategory(Category category) throws ApplicationManagementDAOException; + + /** + * To get the existing categories. + * + * @return Existing categories. + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ + List getCategories() throws ApplicationManagementDAOException; + + /** + * To get the category with the given name. + * + * @param name Name of the Application category. + * @return Application Category. + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ + Category getCategory(String name) throws ApplicationManagementDAOException; + + /** + * To delete a particular category. + * + * @param name Name of the category that need to be deleted. + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ + 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/common/DAOFactory.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/DAOFactory.java index 4016192d14..fa2e94b45d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/DAOFactory.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/DAOFactory.java @@ -22,15 +22,11 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.application.mgt.common.exception.UnsupportedDatabaseEngineException; import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; -import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO; -import org.wso2.carbon.device.application.mgt.core.dao.ApplicationReleaseDAO; -import org.wso2.carbon.device.application.mgt.core.dao.LifecycleStateDAO; -import org.wso2.carbon.device.application.mgt.core.dao.PlatformDAO; -import org.wso2.carbon.device.application.mgt.core.dao.SubscriptionDAO; -import org.wso2.carbon.device.application.mgt.core.dao.VisibilityDAO; +import org.wso2.carbon.device.application.mgt.core.dao.*; import org.wso2.carbon.device.application.mgt.core.dao.impl.application.GenericApplicationDAOImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.application.release.GenericApplicationReleaseDAOImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.application.release.OracleApplicationDAOImpl; +import org.wso2.carbon.device.application.mgt.core.dao.impl.category.GenericCategoryDAOImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.lifecyclestate.GenericLifecycleStateImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.platform.GenericPlatformDAOImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.platform.OracleMsSQLPlatformDAOImpl; @@ -164,6 +160,23 @@ public class DAOFactory { throw new IllegalStateException("Database engine has not initialized properly."); } + /** + * To get the instance of CategoryDAOImplementation of the particular database engine. + * @return {@link org.wso2.carbon.device.application.mgt.core.dao.impl.category.GenericCategoryDAOImpl} + */ + public static CategoryDAO getCategoryDAO() { + if (databaseEngine != null) { + switch (databaseEngine) { + case Constants.DataBaseTypes.DB_TYPE_H2: + case Constants.DataBaseTypes.DB_TYPE_MYSQL: + return new GenericCategoryDAOImpl(); + default: + throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine); + } + } + throw new IllegalStateException("Database engine has not initialized properly."); + } + /** * This method initializes the databases by creating the database. * 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 8b04eeaa78..1ce1444d2f 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 @@ -23,7 +23,6 @@ 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.Category; 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; @@ -58,7 +57,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic log.debug("UUID : " + application.getUuid() + " Name : " + application.getName() + " User name : " + application.getUser().getUserName()); } - Connection conn = null; + Connection conn; PreparedStatement stmt = null; ResultSet rs = null; String sql = ""; @@ -114,7 +113,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic log.debug(String.format("Filter: limit=%s, offset=%", filter.getLimit(), filter.getOffset())); } - Connection conn = null; + Connection conn; PreparedStatement stmt = null; ResultSet rs = null; String sql = ""; @@ -384,7 +383,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic @Override public List getNextLifeCycleStates(String applicationUUID, int tenantId) throws ApplicationManagementDAOException { - Connection connection = null; + Connection connection; PreparedStatement preparedStatement = null; ResultSet resultSet = null; @@ -446,147 +445,31 @@ 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(), e); - } 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 { + public boolean isApplicationExist(String categoryName) throws ApplicationManagementDAOException { Connection conn; PreparedStatement stmt = null; ResultSet rs = null; String sql = "SELECT * FROM APPM_APPLICATION WHERE APPLICATION_CATEGORY_ID = (SELECT ID FROM " + "APPM_APPLICATION_CATEGORY WHERE NAME = ?)"; - try { conn = this.getDBConnection(); stmt = conn.prepareStatement(sql); - stmt.setString(1, name); + stmt.setString(1, categoryName); 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); + + categoryName, e); } catch (SQLException e) { throw new ApplicationManagementDAOException( - "SQL Exception while trying to get the application related with categories, while executing " - + sql, e); + "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; @@ -778,7 +661,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic @Override public int getApplicationId(String uuid, int tenantId) throws ApplicationManagementDAOException { - Connection conn = null; + Connection conn; PreparedStatement stmt = null; ResultSet rs = null; String sql; 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/category/GenericCategoryDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/category/GenericCategoryDAOImpl.java new file mode 100644 index 0000000000..75b3766cb3 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/category/GenericCategoryDAOImpl.java @@ -0,0 +1,151 @@ +/* + * Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +package org.wso2.carbon.device.application.mgt.core.dao.impl.category; + +import org.wso2.carbon.device.application.mgt.common.Category; +import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; +import org.wso2.carbon.device.application.mgt.core.dao.CategoryDAO; +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 java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +/** + * This is the concrete implementation of {@link CategoryDAO}. + */ +public class GenericCategoryDAOImpl extends AbstractDAOImpl implements CategoryDAO { + @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(), e); + } 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 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); + } + } + +} 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 6bd1b20199..632ac600e6 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 @@ -44,12 +44,13 @@ public class CategoryManagerImpl implements CategoryManager { "Application category name cannot be null. Application category creation failed."); } if (getCategory(category.getName()) != null) { - throw new ApplicationCategoryManagementException("Application category wth the name " + category.getName() + " " - + "exists already. Please select a different name"); + throw new ApplicationCategoryManagementException( + "Application category wth the name " + category.getName() + " " + + "exists already. Please select a different name"); } try { ConnectionManagerUtil.beginDBTransaction(); - Category createdCategory = DAOFactory.getApplicationDAO().addCategory(category); + Category createdCategory = DAOFactory.getCategoryDAO().addCategory(category); ConnectionManagerUtil.commitDBTransaction(); return createdCategory; } catch (ApplicationManagementDAOException e) { @@ -64,7 +65,7 @@ public class CategoryManagerImpl implements CategoryManager { public List getCategories() throws ApplicationManagementException { try { ConnectionManagerUtil.openDBConnection(); - return DAOFactory.getApplicationDAO().getCategories(); + return DAOFactory.getCategoryDAO().getCategories(); } finally { ConnectionManagerUtil.closeDBConnection(); } @@ -77,7 +78,7 @@ public class CategoryManagerImpl implements CategoryManager { } try { ConnectionManagerUtil.openDBConnection(); - return DAOFactory.getApplicationDAO().getCategory(name); + return DAOFactory.getCategoryDAO().getCategory(name); } finally { ConnectionManagerUtil.closeDBConnection(); } @@ -92,15 +93,13 @@ public class CategoryManagerImpl implements CategoryManager { } try { ConnectionManagerUtil.beginDBTransaction(); - boolean isApplicationExistForCategory = DAOFactory.getApplicationDAO().isApplicationExistForCategory(name); - + boolean isApplicationExistForCategory = DAOFactory.getApplicationDAO().isApplicationExist(name); if (isApplicationExistForCategory) { ConnectionManagerUtil.rollbackDBTransaction(); throw new ApplicationCategoryManagementException( "Cannot delete the the category " + name + ". Applications " + "exists for this category"); } - - DAOFactory.getApplicationDAO().deleteCategory(name); + DAOFactory.getCategoryDAO().deleteCategory(name); ConnectionManagerUtil.commitDBTransaction(); } catch (ApplicationManagementDAOException e) { ConnectionManagerUtil.rollbackDBTransaction(); @@ -109,5 +108,4 @@ public class CategoryManagerImpl implements CategoryManager { ConnectionManagerUtil.closeDBConnection(); } } - }