From 28f1ecfb5b55b6d7dd00b46664d700f87e0c3109 Mon Sep 17 00:00:00 2001 From: Chathura Ekanayake Date: Thu, 27 Jul 2017 15:52:12 +0530 Subject: [PATCH] Implementing application operations for app management --- .../impl/ApplicationManagementAPIImpl.java | 16 +++ .../ApplicationManagementException.java | 2 +- .../common/services/ApplicationManager.java | 2 +- .../mgt/core/dao/ApplicationDAO.java | 6 +- .../application/mgt/core/dao/common/Util.java | 2 + .../AbstractApplicationDAOImpl.java | 5 - .../application/H2ApplicationDAOImpl.java | 20 ++- .../application/MySQLApplicationDAOImpl.java | 116 +++++++++++++++--- .../mgt/core/impl/ApplicationManagerImpl.java | 26 +++- 9 files changed, 159 insertions(+), 36 deletions(-) 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 c6dde79ab15..d1a9bd3249c 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 @@ -109,4 +109,20 @@ public class ApplicationManagementAPIImpl { return Response.status(Response.Status.OK).entity(application).build(); } + @DELETE + @Path("applications/{appuuid}") + public Response deleteApplication(@PathParam("appuuid") String uuid) { + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + try { + applicationManager.deleteApplication(uuid); + + } catch (ApplicationManagementException e) { + String msg = "Error occurred while deleting the application: " + uuid; + log.error(msg, e); + return Response.status(Response.Status.BAD_REQUEST).build(); + } + String responseMsg = "Successfully deleted the application: " + uuid; + return Response.status(Response.Status.OK).entity(responseMsg).build(); + } + } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/ApplicationManagementException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/ApplicationManagementException.java index 723e3a07a21..f437d6ac37f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/ApplicationManagementException.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/ApplicationManagementException.java @@ -18,7 +18,7 @@ */ package org.wso2.carbon.device.application.mgt.common.exception; -public abstract class ApplicationManagementException extends Exception { +public class ApplicationManagementException extends Exception { String message; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java index 7fbcb9a1c39..85d310c5096 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/ApplicationManager.java @@ -29,7 +29,7 @@ public interface ApplicationManager{ public Application editApplication(Application application) throws ApplicationManagementException; - public void deleteApplication(int uuid) throws ApplicationManagementException; + public void deleteApplication(String uuid) throws ApplicationManagementException; public ApplicationList getApplications(Filter filter) 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 d3072e9aff9..860ca00c896 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 @@ -36,7 +36,7 @@ public interface ApplicationDAO { Application editApplication(Application application) throws ApplicationManagementDAOException; - void deleteApplication(Application application) throws ApplicationManagementDAOException; + void deleteApplication(String uuid) throws ApplicationManagementDAOException; int getApplicationCount(Filter filter) throws ApplicationManagementDAOException; @@ -44,7 +44,9 @@ public interface ApplicationDAO { void editProperties(Map properties) throws ApplicationManagementDAOException; - void deleteProperties(List propertyKeys) throws ApplicationManagementDAOException; + void deleteProperties(int applicationId) throws ApplicationManagementDAOException; + + void deleteTags(int applicationId) throws ApplicationManagementDAOException; void changeLifeCycle(LifecycleState lifecycleState) throws ApplicationManagementDAOException; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java index e64719e8251..f9ce7d288d1 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/common/Util.java @@ -43,6 +43,8 @@ public class Util { application.setId(rs.getInt("ID")); application.setName(rs.getString("NAME")); application.setUuid(rs.getString("UUID")); + application.setIdentifier(rs.getString("IDENTIFIER")); + application.setShortDescription(rs.getString("SHORT_DESCRIPTION")); application.setDescription(rs.getString("DESCRIPTION")); application.setIconName(rs.getString("ICON_NAME")); application.setBannerName(rs.getString("BANNER_NAME")); 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/AbstractApplicationDAOImpl.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/AbstractApplicationDAOImpl.java index 405559ca6a8..3ad01179b05 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/AbstractApplicationDAOImpl.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/AbstractApplicationDAOImpl.java @@ -37,11 +37,6 @@ public abstract class AbstractApplicationDAOImpl extends AbstractDAOImpl impleme private static final Log log = LogFactory.getLog(AbstractApplicationDAOImpl.class); - @Override - public void deleteApplication(Application application) throws ApplicationManagementDAOException { - - } - @Override public int getApplicationCount(Filter filter) throws ApplicationManagementDAOException { if(log.isDebugEnabled()){ 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/H2ApplicationDAOImpl.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/H2ApplicationDAOImpl.java index a3e53feacf0..695ff6052da 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/H2ApplicationDAOImpl.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/H2ApplicationDAOImpl.java @@ -145,6 +145,21 @@ public class H2ApplicationDAOImpl extends AbstractApplicationDAOImpl { return 0; } + @Override + public void deleteApplication(String uuid) throws ApplicationManagementDAOException { + + } + + @Override + public void deleteProperties(int applicationId) throws ApplicationManagementDAOException { + + } + + @Override + public void deleteTags(int applicationId) throws ApplicationManagementDAOException { + + } + @Override public Application editApplication(Application application) throws ApplicationManagementDAOException { return null; @@ -160,11 +175,6 @@ public class H2ApplicationDAOImpl extends AbstractApplicationDAOImpl { } - @Override - public void deleteProperties(List propertyKeys) throws ApplicationManagementDAOException { - - } - @Override public void changeLifeCycle(LifecycleState lifecycleState) 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/MySQLApplicationDAOImpl.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/MySQLApplicationDAOImpl.java index 90cf14b8da6..f6b9b0087b9 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/MySQLApplicationDAOImpl.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/MySQLApplicationDAOImpl.java @@ -189,6 +189,27 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl { return application; } + @Override + public void deleteApplication(String uuid) throws ApplicationManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + conn = this.getConnection(); + String sql = "DELETE FROM APPM_APPLICATION WHERE UUID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setString(1, uuid); + stmt.executeUpdate(); + + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException("Error occurred while deleting the application: " + uuid, e); + } finally { + Util.cleanupResources(stmt, rs); + } + } + @Override public int getApplicationId(String uuid) throws ApplicationManagementDAOException { Connection conn = null; @@ -216,7 +237,6 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl { } return id; - } @Override @@ -298,6 +318,22 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl { stmt.executeBatch(); } + // delete existing properties and add new ones. if no properties are set, existing ones will be deleted. + sql = "DELETE FROM APPM_APPLICATION_PROPERTY WHERE APPLICATION_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, application.getId()); + stmt.executeUpdate(); + + sql = "INSERT INTO APPM_APPLICATION_PROPERTY (PROP_KEY, PROP_VAL, APPLICATION_ID) VALUES (?, ?, ?); "; + stmt = conn.prepareStatement(sql); + for (String propKey : application.getProperties().keySet()) { + stmt.setString(1, propKey); + stmt.setString(2, application.getProperties().get(propKey)); + stmt.setInt(3, application.getId()); + stmt.addBatch(); + } + stmt.executeBatch(); + } catch (DBConnectionException e) { throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); } catch (SQLException e) { @@ -318,8 +354,47 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl { } @Override - public void deleteProperties(List propertyKeys) throws ApplicationManagementDAOException { + public void deleteProperties(int applicationId) throws ApplicationManagementDAOException { + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + conn = this.getConnection(); + String sql = "DELETE FROM APPM_APPLICATION_PROPERTY WHERE APPLICATION_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, applicationId); + stmt.executeUpdate(); + + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException("Error occurred while deleting properties of application: " + applicationId, e); + } finally { + Util.cleanupResources(stmt, rs); + } + } + + @Override + public void deleteTags(int applicationId) throws ApplicationManagementDAOException { + + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + conn = this.getConnection(); + String sql = "DELETE FROM APPM_APPLICATION_TAG WHERE APPLICATION_ID = ?"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, applicationId); + stmt.executeUpdate(); + + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); + } catch (SQLException e) { + throw new ApplicationManagementDAOException("Error occurred while deleting tags of application: " + applicationId, e); + } finally { + Util.cleanupResources(stmt, rs); + } } @Override @@ -345,29 +420,30 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl { try { conn = this.getConnection(); - sql += "INSERT INTO APPM_APPLICATION (UUID, NAME, SHORT_DESCRIPTION, DESCRIPTION, ICON_NAME, BANNER_NAME, " + + sql += "INSERT INTO APPM_APPLICATION (UUID, IDENTIFIER, NAME, SHORT_DESCRIPTION, DESCRIPTION, ICON_NAME, BANNER_NAME, " + "VIDEO_NAME, SCREENSHOTS, CREATED_BY, CREATED_AT, MODIFIED_AT, APPLICATION_CATEGORY_ID, " + "" + "PLATFORM_ID, TENANT_ID, LIFECYCLE_STATE_ID, LIFECYCLE_STATE_MODIFIED_AT, " + - "LIFECYCLE_STATE_MODIFIED_BY) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + "LIFECYCLE_STATE_MODIFIED_BY) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); stmt.setString(1, application.getUuid()); - stmt.setString(2, application.getName()); - stmt.setString(3, application.getShortDescription()); - stmt.setString(4, application.getDescription()); - stmt.setString(5, application.getIconName()); - stmt.setString(6, application.getBannerName()); - stmt.setString(7, application.getVideoName()); - stmt.setString(8, JSONUtil.listToJsonArrayString(application.getScreenshots())); - stmt.setString(9, application.getUser().getUserName()); - stmt.setDate(10, new Date(application.getCreatedAt().getTime())); - stmt.setDate(11, new Date(application.getModifiedAt().getTime())); - stmt.setInt(12, application.getCategory().getId()); - stmt.setInt(13, application.getPlatform().getId()); - stmt.setInt(14, application.getUser().getTenantId()); - stmt.setInt(15, application.getCurrentLifecycle().getLifecycleState().getId()); - stmt.setDate(16, new Date(application.getCurrentLifecycle().getLifecycleStateModifiedAt().getTime())); - stmt.setString(17, application.getCurrentLifecycle().getGetLifecycleStateModifiedBy()); + stmt.setString(2, application.getIdentifier()); + stmt.setString(3, application.getName()); + stmt.setString(4, application.getShortDescription()); + stmt.setString(5, application.getDescription()); + stmt.setString(6, application.getIconName()); + stmt.setString(7, application.getBannerName()); + stmt.setString(8, application.getVideoName()); + stmt.setString(9, JSONUtil.listToJsonArrayString(application.getScreenshots())); + stmt.setString(10, application.getUser().getUserName()); + stmt.setDate(11, new Date(application.getCreatedAt().getTime())); + stmt.setDate(12, new Date(application.getModifiedAt().getTime())); + stmt.setInt(13, application.getCategory().getId()); + stmt.setInt(14, application.getPlatform().getId()); + stmt.setInt(15, application.getUser().getTenantId()); + stmt.setInt(16, application.getCurrentLifecycle().getLifecycleState().getId()); + stmt.setDate(17, new Date(application.getCurrentLifecycle().getLifecycleStateModifiedAt().getTime())); + stmt.setString(18, application.getCurrentLifecycle().getGetLifecycleStateModifiedBy()); stmt.executeUpdate(); rs = stmt.getGeneratedKeys(); 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 e75ebaae061..e260e1231ec 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 @@ -18,6 +18,9 @@ */ package org.wso2.carbon.device.application.mgt.core.impl; +import com.sun.corba.se.spi.legacy.connection.Connection; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; 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.DBConnectionException; @@ -26,6 +29,7 @@ import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO; 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.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.exception.ValidationException; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; @@ -35,7 +39,7 @@ import java.util.Date; public class ApplicationManagerImpl implements ApplicationManager { - + private static final Log log = LogFactory.getLog(ApplicationManagerImpl.class); public static final String CREATED = "created"; @Override @@ -111,8 +115,26 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public void deleteApplication(int uuid) throws ApplicationManagementException { + public void deleteApplication(String uuid) throws ApplicationManagementException { + + try { + ConnectionManagerUtil.openConnection(); + ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO(); + int appId = applicationDAO.getApplicationId(uuid); + ConnectionManagerUtil.beginTransaction(); + applicationDAO.deleteTags(appId); + applicationDAO.deleteProperties(appId); + applicationDAO.deleteApplication(uuid); + ConnectionManagerUtil.commitTransaction(); + + } catch (ApplicationManagementDAOException e) { + ConnectionManagerUtil.rollbackTransaction(); + String msg = "Failed to delete application: " + uuid; + throw new ApplicationManagementException(msg, e); + } finally { + ConnectionManagerUtil.closeConnection(); + } } @Override