From 7703d217f92e08d27a9bd43b5bd602004fb9be9c Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Fri, 26 Apr 2019 23:03:51 +0530 Subject: [PATCH] Add App updating API --- .../mgt/core/dao/ApplicationDAO.java | 2 +- .../GenericApplicationDAOImpl.java | 76 ++++++------------- .../mgt/core/impl/ApplicationManagerImpl.java | 7 +- .../impl/ApplicationManagementAPIImpl.java | 26 +++---- 4 files changed, 42 insertions(+), 69 deletions(-) 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 3bd7a484dd..efd1bdec20 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 @@ -168,7 +168,7 @@ public interface ApplicationDAO { * @return Updated ApplicationDTO. * @throws ApplicationManagementDAOException ApplicationDTO Management DAO Exception. */ - ApplicationDTO editApplication(ApplicationDTO application, int tenantId) throws ApplicationManagementDAOException; + boolean updateApplication(ApplicationDTO application, int tenantId) throws ApplicationManagementDAOException; /** * To delete the application 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 e0d06c2346..64fbb1e7fb 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 @@ -583,66 +583,36 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } @Override - public ApplicationDTO editApplication(ApplicationDTO application, int tenantId) + public boolean updateApplication(ApplicationDTO applicationDTO, int tenantId) throws ApplicationManagementDAOException { - int paramIndex = 1; Connection conn; - PreparedStatement stmt = null; - //todo this is wrong - ApplicationDTO existingApplication = this.getApplicationById(application.getId(), tenantId); - - if (existingApplication == null) { - throw new ApplicationManagementDAOException("There doesn't have an application for updating"); - } try { conn = this.getDBConnection(); - String sql = "UPDATE AP_APP SET "; - - if (application.getName() != null && !application.getName().equals(existingApplication.getName())) { - sql += "NAME = ?, "; - } - if (application.getType() != null && !application.getType().equals(existingApplication.getType())) { - sql += "TYPE = ?, "; + String sql = "UPDATE AP_APP AP " + + "SET " + + "AP.NAME = ?, " + + "AP.DESCRIPTION = ?, " + + "AP.SUB_TYPE = ?, " + + "AP.CURRENCY = ? " + + "WHERE AP.ID = ? AND AP.TENANT_ID = ?"; + + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setString(1, applicationDTO.getName()); + stmt.setString(2, applicationDTO.getDescription()); + stmt.setString(3, applicationDTO.getSubType()); + stmt.setString(4, applicationDTO.getPaymentCurrency()); + stmt.setInt(5, applicationDTO.getId()); + stmt.setInt(6, tenantId); + return stmt.executeUpdate() > 0; } - if (application.getAppCategory() != null && !application.getAppCategory().equals( - existingApplication.getAppCategory())) { - sql += "APP_CATEGORY = ?, "; - } -// if (application.getIsRestricted() != existingApplication.getIsRestricted()) { -// sql += "RESTRICTED = ? "; -// } - if (!application.getSubType().equals(existingApplication.getSubType())) { - sql += "SUB_TYPE = ? "; - } - - sql += "WHERE ID = ?"; - - stmt = conn.prepareStatement(sql); - if (application.getName() != null && !application.getName().equals(existingApplication.getName())) { - stmt.setString(paramIndex++, application.getName()); - } - if (application.getType() != null && !application.getType().equals(existingApplication.getType())) { - stmt.setString(paramIndex++, application.getType()); - } - if (application.getAppCategory() != null && !application.getAppCategory().equals( - existingApplication.getAppCategory())) { - stmt.setString(paramIndex++, application.getAppCategory()); - } -// if (application.getIsRestricted() != existingApplication.getIsRestricted()) { -// stmt.setBoolean(paramIndex++, application.getIsRestricted()); -// } - if (!application.getSubType().equals(existingApplication.getSubType())) { - stmt.setString(paramIndex++, application.getSubType()); - } - stmt.setInt(paramIndex, application.getId()); - stmt.executeUpdate(); - return application; } catch (DBConnectionException e) { - throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); + String msg = "Error occurred while obtaining the DB connection to update the application."; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } catch (SQLException e) { - throw new ApplicationManagementDAOException("Error occurred while adding the application", e); - } finally { - Util.cleanupResources(stmt, null); + String msg = "Error occurred when obtaining database connection for updating the application."; + log.error(msg); + throw new ApplicationManagementDAOException(msg, e); } } 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 370c770af1..e8dc3e72fd 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 @@ -1589,8 +1589,11 @@ public class ApplicationManagerImpl implements ApplicationManager { applicationDAO.deleteTags(removingTagList, applicationId, tenantId); } } - //todo - applicationDAO.editApplication(applicationDTO, tenantId); + if (!applicationDAO.updateApplication(applicationDTO, tenantId)){ + String msg = "Any application is not updated for the application ID: " + applicationId; + log.error(msg); + throw new ApplicationManagementException(msg); + } } catch (UserStoreException e) { ConnectionManagerUtil.rollbackDBTransaction(); throw new ApplicationManagementException( diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java index 3b99f1c50b..342e27b029 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementAPIImpl.java @@ -23,7 +23,6 @@ import org.apache.commons.logging.LogFactory; import org.apache.cxf.jaxrs.ext.multipart.Attachment; import org.apache.cxf.jaxrs.ext.multipart.Multipart; import org.wso2.carbon.device.application.mgt.common.*; -import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationReleaseDTO; import org.wso2.carbon.device.application.mgt.common.dto.LifecycleStateDTO; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationStorageManagementException; @@ -34,7 +33,6 @@ import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationReleaseW import org.wso2.carbon.device.application.mgt.common.wrapper.ApplicationWrapper; import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException; import org.wso2.carbon.device.application.mgt.core.exception.ForbiddenException; -import org.wso2.carbon.device.application.mgt.core.exception.ValidationException; import org.wso2.carbon.device.application.mgt.core.util.APIUtil; import org.wso2.carbon.device.application.mgt.publisher.api.services.ApplicationManagementAPI; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; @@ -316,10 +314,6 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { } } - /* - //todo ---------------------- - */ - @PUT @Consumes("application/json") @Path("/{appId}") @@ -328,23 +322,29 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { @Valid ApplicationWrapper applicationWrapper) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); try { - //todo wrong applicationManager.updateApplication(applicationId, applicationWrapper); return Response.status(Response.Status.OK) - .entity("Application was updated successfully. ApplicationID " + applicationId).build(); + .entity("Application was updated successfully for ApplicationID: " + applicationId).build(); } catch (NotFoundException e) { log.error(e.getMessage()); return Response.status(Response.Status.NOT_FOUND).entity(e.getMessage()).build(); - } catch (ForbiddenException e) { - log.error(e.getMessage()); - return Response.status(Response.Status.FORBIDDEN).entity(e.getMessage()).build(); - } catch (ApplicationManagementException e) { - String msg = "Error occurred while modifying the application"; + } catch (BadRequestException e) { + String msg = "Error occurred while modifying the application. Found bad request payload for updating the " + + "application"; log.error(msg, e); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } + catch (ApplicationManagementException e) { + String msg = "Internal Error occurred while modifying the application."; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); + } } + /* + //todo ---------------------- + */ + @Override @PUT @Path("/{deviceType}/{appId}/{uuid}")