From 6d6aa50b2a94c7fcc21158188c27462916aba282 Mon Sep 17 00:00:00 2001 From: Chatura Dilan Date: Wed, 14 Jun 2017 17:27:01 +0530 Subject: [PATCH] Added functionailty to update app --- .../impl/ApplicationManagementAPIImpl.java | 23 +++++++ .../common/services/ApplicationManager.java | 2 +- .../application/MySQLApplicationDAOImpl.java | 68 ++++++++++++++++++- .../mgt/core/impl/ApplicationManagerImpl.java | 2 +- 4 files changed, 92 insertions(+), 3 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 ba6dda0099..c6dde79ab1 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 @@ -86,4 +86,27 @@ public class ApplicationManagementAPIImpl { return Response.status(Response.Status.OK).entity(application).build(); } + + @PUT + @Consumes("application/json") + @Path("applications") + public Response editApplication(@Valid Application application) { + + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + + //TODO : Get username and tenantId + User user = new User("admin", -1234); + application.setUser(user); + + try { + application = applicationManager.editApplication(application); + + } catch (ApplicationManagementException e) { + String msg = "Error occurred while creating the application"; + log.error(msg, e); + return Response.status(Response.Status.BAD_REQUEST).build(); + } + return Response.status(Response.Status.OK).entity(application).build(); + } + } 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 5a3e913cf5..7fbcb9a1c3 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 @@ -27,7 +27,7 @@ public interface ApplicationManager{ public Application createApplication(Application application) throws ApplicationManagementException; - public Application editApplication(int uuid, Application application) throws ApplicationManagementException; + public Application editApplication(Application application) throws ApplicationManagementException; public void deleteApplication(int uuid) 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/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 b7821504fe..a0185ed40f 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 @@ -139,7 +139,73 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl { @Override public Application editApplication(Application application) throws ApplicationManagementDAOException { - return null; + + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; + String sql = ""; + + try { + conn = this.getConnection(); + sql += "UPDATE APPM_APPLICATION SET "; + sql += "NAME = IFNULL (?, NAME), "; + sql += "SHORT_DESCRIPTION = IFNULL (?, SHORT_DESCRIPTION), DESCRIPTION = IFNULL (?, DESCRIPTION), "; + sql += "ICON_NAME = IFNULL (?, ICON_NAME), BANNER_NAME = IFNULL (?, BANNER_NAME) "; + sql += " WHERE UUID = ?"; + + + stmt = conn.prepareStatement(sql); + stmt.setString(1, application.getName()); + stmt.setString(2, application.getShortDescription()); + stmt.setString(3, application.getDescription()); + stmt.setString(4, application.getIconName()); + stmt.setString(5, application.getBannerName()); + stmt.setString(6, application.getUuid()); +// 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.executeUpdate(); + +// if (application.getTags() != null && application.getTags().size() > 0) { +// sql = "INSERT INTO APPM_APPLICATION_TAG (NAME, APPLICATION_ID) VALUES (?, ?); "; +// stmt = conn.prepareStatement(sql); +// for (String tag : application.getTags()) { +// stmt.setString(1, tag); +// stmt.setInt(2, application.getId()); +// stmt.addBatch(); +// } +// stmt.executeBatch(); +// } +// +// if (application.getProperties() != null && application.getProperties().size() > 0) { +// sql = "INSERT INTO APPM_APPLICATION_PROPERTY (PROP_KEY, PROP_VAL, APPLICATION_ID) VALUES (?, ?, ?); "; +// stmt = conn.prepareStatement(sql); +// Iterator it = application.getProperties().entrySet().iterator(); +// while (it.hasNext()) { +// Map.Entry property = (Map.Entry)it.next(); +// stmt.setString(1, property.getKey()); +// stmt.setString(2, property.getValue()); +// 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) { + throw new ApplicationManagementDAOException("Error occurred while adding the application", e); + } + + return application; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/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 14f86dfa70..9201dc5e35 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 @@ -81,7 +81,7 @@ public class ApplicationManagerImpl implements ApplicationManager { @Override - public Application editApplication(int uuid, Application application) throws ApplicationManagementException { + public Application editApplication(Application application) throws ApplicationManagementException { validateApplication(application, true);