From 2b0ae8c5a442a322e64063d0474295b4fe41af21 Mon Sep 17 00:00:00 2001 From: Chatura Dilan Date: Wed, 14 Jun 2017 11:59:06 +0530 Subject: [PATCH] Added creating app functionality --- .../services/ApplicationManagementAPI.java | 1 - .../impl/ApplicationManagementAPIImpl.java | 29 +- .../src/main/webapp/META-INF/permissions.xml | 386 ------------------ .../application/mgt/common/Application.java | 21 +- .../application/mgt/common/Category.java | 1 - .../application/mgt/common/Lifecycle.java | 6 +- .../mgt/common/LifecycleState.java | 16 +- .../application/mgt/common/Platform.java | 20 +- .../device/application/mgt/common/User.java | 47 +++ .../ApplicationManagementException.java | 3 + .../common/services/ApplicationManager.java | 4 +- .../mgt/core/dao/LifecycleStateDAO.java | 5 + .../application/mgt/core/dao/PlatformDAO.java | 5 + .../mgt/core/dao/common/DAOFactory.java | 40 +- .../application/mgt/core/dao/common/Util.java | 17 +- .../mgt/core/dao/impl/AbstractDAOImpl.java | 31 ++ .../AbstractApplicationDAOImpl.java | 23 +- .../application/H2ApplicationDAOImpl.java | 13 +- .../application/MySQLApplicationDAOImpl.java | 96 ++++- .../AbstractLifecycleStateDAOImpl.java | 26 ++ .../H2LifecycleStateDAOImpl.java | 33 ++ .../MySQLLifecycleStateDAOImpl.java | 76 ++++ .../platform/AbstractPlatformDAOImpl.java | 39 ++ .../dao/impl/platform/H2PlatformDAOImpl.java | 31 ++ .../impl/platform/MySQLPlatformDAOImpl.java | 74 ++++ .../mgt/core/exception/NotFoundException.java | 33 ++ .../core/exception/ValidationException.java | 32 ++ .../mgt/core/impl/ApplicationManagerImpl.java | 95 ++++- .../mgt/core/util/ConnectionManagerUtil.java | 45 +- .../application/mgt/core/util/HelperUtil.java | 29 ++ .../application/mgt/core/util/JSONUtil.java | 50 +++ 31 files changed, 837 insertions(+), 490 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/User.java 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/AbstractDAOImpl.java rename 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 (80%) 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/lifecyclestate/AbstractLifecycleStateDAOImpl.java 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/lifecyclestate/H2LifecycleStateDAOImpl.java 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/lifecyclestate/MySQLLifecycleStateDAOImpl.java 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/platform/AbstractPlatformDAOImpl.java 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/platform/H2PlatformDAOImpl.java 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/platform/MySQLPlatformDAOImpl.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/NotFoundException.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ValidationException.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/HelperUtil.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/JSONUtil.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementAPI.java index 5ac7909929..41084eee7f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/ApplicationManagementAPI.java @@ -90,5 +90,4 @@ public interface ApplicationManagementAPI { ); - } 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 f45883c46e..184515a349 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 @@ -20,13 +20,15 @@ package org.wso2.carbon.device.application.mgt.api.services.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.application.mgt.common.Filter; -import org.wso2.carbon.device.application.mgt.common.ApplicationList; +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.services.ApplicationManager; import org.wso2.carbon.device.application.mgt.api.APIUtil; + +import javax.validation.Valid; import javax.ws.rs.*; import javax.ws.rs.core.Response; +import java.util.Date; @Produces({"application/json"}) @Consumes({"application/json"}) @@ -62,4 +64,27 @@ public class ApplicationManagementAPIImpl { return Response.status(Response.Status.NOT_FOUND).build(); } } + + @POST + @Consumes("application/json") + @Path("applications") + public Response createApplication(@Valid Application application) { + + ApplicationManager applicationManager = APIUtil.getApplicationManager(); + + //TODO : Get username and tenantId + User user = new User("admin", -1234); + application.setUser(user); + + try { + application = applicationManager.createApplication(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.api/src/main/webapp/META-INF/permissions.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/META-INF/permissions.xml index befabcd097..1b99f2dc8d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/META-INF/permissions.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/webapp/META-INF/permissions.xml @@ -30,391 +30,5 @@ - - Device Management - /device-mgt - / - GET - - - - Device Management Admin - /device-mgt/admin - / - GET - - - Devices - /device-mgt/devices - / - GET - - - - List devices - /device-mgt/devices/List - /devices - GET - - - Search devices - /device-mgt/devices/Search - /devices/search-devices - POST - - - View device - /device-mgt/devices/View - /devices/*/* - GET - - - View device info - /device-mgt/devices/View - /devices/*/*/info - GET - - - View device applications - /device-mgt/devices/View-Applications - /devices/*/*/applications - GET - - - View device effective-policy - /device-mgt/devices/View-Active-Policy - /devices/*/*/effective-policy - GET - - - View devices feature - /device-mgt/devices/View-Features - /devices/*/*/features - GET - - - View device operations - /device-mgt/devices/View-Operations - /devices/*/*/operations - GET - - - View Compliance Data - /device-mgt/devices/View-Compliance-Data - /devices/*/*/compliance-data - GET - - - List all devices - /device-mgt/devices/Admin-View - /admin/devices - GET - - - Verify device authorization - /device-mgt/devices - /admin/authorization - POST - - - View device types - /device-mgt/devices/Admin-DeviceType-View - /admin/device-types - GET - - - - Policies - /device-mgt/policies - / - GET - - - - List policies - /device-mgt/policies/List - /policies - GET - - - Add Policy - /device-mgt/policies/Add - /policies - POST - - - Activate policy - /device-mgt/policies/Activate-Policy - /policies/activate-policy - PUT - - - Deactivate Policy - /device-mgt/policies/Deactivate-Policy - /policies/deactivate-policy - PUT - - - Remove Policy - /device-mgt/policies/Remove - /policies/remove-policy - POST - - - View Policy - /device-mgt/policies/View - /policies/* - GET - - - Update Policy - /device-mgt/policies/Update - /policies/* - PUT - - - Update Policy - /device-mgt/policies/Update - /policies/apply-changes - PUT - - - Update Policy - /device-mgt/policies/Change-Priority - /policies/priorities - PUT - - - - Notifications - /device-mgt/notifications - / - GET - - - - View notifications - /device-mgt/notifications/View - /notifications - GET - - - Mark checked notifications - /device-mgt/notifications/View - /notifications/*/mark-checked - PUT - - - - Users - /device-mgt/users - / - GET - - - - List users - /device-mgt/users/List - /users - GET - - - Add user - /device-mgt/users/Add - /users - POST - - - List users - /device-mgt/users/Search - /users/search/usernames - GET - - - Remove user - /device-mgt/users/Remove - /users/* - DELETE - - - View user - /device-mgt/users/View - /users/* - GET - - - Update user - /device-mgt/users/Update - /users/* - PUT - - - Update user credentials - /device-mgt/users/Change-Password - /users/*/credentials - PUT - - - View assigned role - /device-mgt/roles/Assigned-Roles - /users/*/roles - GET - - - Change any user credentials - /device-mgt/users/Change-Password-Any - /admin/users/*/credentials - POST - - - Send invitation mail - /device-mgt/users/Send-invitations - /users/send-invitation - POST - - - - Roles - /device-mgt/roles - / - GET - - - - List roles - /device-mgt/roles/List - /roles - GET - - - Add role - /device-mgt/roles/Add - /roles - POST - - - Remove role - /device-mgt/roles/Remove - /roles/* - DELETE - - - View role - /device-mgt/roles/View - /roles/* - GET - - - Update role - /device-mgt/roles/Update - /roles/* - PUT - - - View role permissions - /device-mgt/roles/View-Permission - /roles/*/permissions - GET - - - Add Users to role - /device-mgt/roles/Add-Users - /roles/*/users - PUT - - - - Configurations - /device-mgt/general-configs - / - GET - - - - View configuration - /device-mgt/general-configuration/View - /configuration - GET - - - Update configuration - /device-mgt/general-configuration/Update - /configuration - PUT - - - - Activities - /device-mgt/activities - / - GET - - - - View Activities - /device-mgt/devices/owning/view - /activities - GET - - - View Activity Details - /device-mgt/devices/owning/view - /activities/* - GET - - - - Applications - /device-mgt/applications - / - GET - - - - Install Applications - /device-mgt/application/Install - /admin/applications/install-application - POST - - - Uninstall-Applications - /device-mgt/application/Uninstall - /admin/applications/uninstall-application - POST - - - - - - Device Management User - /device-mgt/user - / - GET - - - User Devices - /device-mgt/user/devices - / - GET - - - User Policies - /device-mgt/user/policies - / - GET - - - User Notifications - /device-mgt/user/notifications - / - GET - - - User Operations - /device-mgt/user/operations - / - GET - - diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java index 144f8231ed..71a304fb96 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Application.java @@ -58,8 +58,6 @@ public class Application { private Map properties; - private String createdBy; - private Date createdAt; private Date modifiedAt; @@ -72,6 +70,8 @@ public class Application { private Visibility visibility; + private User user; + public int getId() { return id; } @@ -84,7 +84,6 @@ public class Application { this.releases = releases; } - public List getComments() { return comments; } @@ -217,14 +216,6 @@ public class Application { this.properties = properties; } - public String getCreatedBy() { - return createdBy; - } - - public void setCreatedBy(String createdBy) { - this.createdBy = createdBy; - } - public Date getCreatedAt() { return createdAt; } @@ -248,4 +239,12 @@ public class Application { public void setVisibility(Visibility visibility) { this.visibility = visibility; } + + public User getUser() { + return user; + } + + public void setUser(User user) { + this.user = user; + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Category.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Category.java index 19f63c8ee8..b0c0fbdf32 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Category.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Category.java @@ -22,7 +22,6 @@ import org.wso2.carbon.device.application.mgt.common.jaxrs.Exclude; public class Category { - @Exclude private int id; private String name; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Lifecycle.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Lifecycle.java index c8901af5bc..3db7d20e88 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Lifecycle.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Lifecycle.java @@ -25,7 +25,7 @@ public class Lifecycle { private Date lifecycleStateModifiedAt; - private Date getLifecycleStateModifiedBy; + private String getLifecycleStateModifiedBy; public LifecycleState getLifecycleState() { return lifecycleState; @@ -43,11 +43,11 @@ public class Lifecycle { this.lifecycleStateModifiedAt = lifecycleStateModifiedAt; } - public Date getGetLifecycleStateModifiedBy() { + public String getGetLifecycleStateModifiedBy() { return getLifecycleStateModifiedBy; } - public void setGetLifecycleStateModifiedBy(Date getLifecycleStateModifiedBy) { + public void setGetLifecycleStateModifiedBy(String getLifecycleStateModifiedBy) { this.getLifecycleStateModifiedBy = getLifecycleStateModifiedBy; } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleState.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleState.java index 4037c99400..0f74a8f829 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleState.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/LifecycleState.java @@ -20,17 +20,19 @@ package org.wso2.carbon.device.application.mgt.common; public class LifecycleState { - private String id; + private int id; private String name; + private String identifier; + private String description; - public String getId() { + public int getId() { return id; } - public void setId(String id) { + public void setId(int id) { this.id = id; } @@ -42,6 +44,14 @@ public class LifecycleState { this.name = name; } + public String getIdentifier() { + return identifier; + } + + public void setIdentifier(String identifier) { + this.identifier = identifier; + } + public String getDescription() { return description; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Platform.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Platform.java index 27550b9ecc..20837b2dc1 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Platform.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/Platform.java @@ -32,7 +32,7 @@ public class Platform { private String description; - private String code; + private String identifier; private String iconName; @@ -40,6 +40,8 @@ public class Platform { private Map properties; + private boolean published; + public int getId() { return id; } @@ -64,12 +66,12 @@ public class Platform { this.description = description; } - public String getCode() { - return code; + public String getIdentifier() { + return identifier; } - public void setCode(String code) { - this.code = code; + public void setIdentifier(String identifier) { + this.identifier = identifier; } public String getIconName() { @@ -95,4 +97,12 @@ public class Platform { public void setTags(List tags) { this.tags = tags; } + + public boolean isPublished() { + return published; + } + + public void setPublished(boolean published) { + this.published = published; + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/User.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/User.java new file mode 100644 index 0000000000..2a8fd8224e --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/User.java @@ -0,0 +1,47 @@ +/* + * 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.common; + +public class User { + + private String userName; + + private int tenantId; + + public User(String userName, int tenantId) { + this.userName = userName; + this.tenantId = tenantId; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + public int getTenantId() { + return tenantId; + } + + public void setTenantId(int tenantId) { + this.tenantId = tenantId; + } +} 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 adde1770e0..723e3a07a2 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 @@ -32,6 +32,9 @@ public abstract class ApplicationManagementException extends Exception { setMessage(message); } + public ApplicationManagementException() { + + } @Override public String getMessage() { return 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 0642aef21c..5a3e913cf5 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,9 +27,9 @@ public interface ApplicationManager{ public Application createApplication(Application application) throws ApplicationManagementException; - public Application editApplication(int applicationId, Application application) throws ApplicationManagementException; + public Application editApplication(int uuid, Application application) throws ApplicationManagementException; - public void deleteApplication(int applicationId) throws ApplicationManagementException; + public void deleteApplication(int 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/LifecycleStateDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/LifecycleStateDAO.java index 9addac93d2..9f4674d069 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/LifecycleStateDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/LifecycleStateDAO.java @@ -18,6 +18,11 @@ */ package org.wso2.carbon.device.application.mgt.core.dao; +import org.wso2.carbon.device.application.mgt.common.LifecycleState; +import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; + public interface LifecycleStateDAO { + public LifecycleState getLifeCycleStateByIdentifier(String identifier) 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/PlatformDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/PlatformDAO.java index b1ff1bf97e..164fb849e6 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/PlatformDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/PlatformDAO.java @@ -18,6 +18,11 @@ */ package org.wso2.carbon.device.application.mgt.core.dao; +import org.wso2.carbon.device.application.mgt.common.Platform; +import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; + public interface PlatformDAO { + public Platform getPlatformByIdentifier(String identifier) 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 71c3e675fa..293a7ef3a0 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,13 +22,17 @@ 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.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.impl.application.H2ApplicationDAOImpl; import org.wso2.carbon.device.application.mgt.core.dao.impl.application.MySQLApplicationDAOImpl; +import org.wso2.carbon.device.application.mgt.core.dao.impl.lifecyclestate.H2LifecycleStateDAOImpl; +import org.wso2.carbon.device.application.mgt.core.dao.impl.lifecyclestate.MySQLLifecycleStateDAOImpl; +import org.wso2.carbon.device.application.mgt.core.dao.impl.platform.H2PlatformDAOImpl; +import org.wso2.carbon.device.application.mgt.core.dao.impl.platform.MySQLPlatformDAOImpl; import org.wso2.carbon.device.application.mgt.core.util.Constants; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; -import javax.sql.DataSource; - /** * This class intends to act as the primary entity that hides all DAO instantiation related complexities and logic so @@ -59,4 +63,34 @@ public class DAOFactory { } throw new IllegalStateException("Database engine has not initialized properly."); } -} \ No newline at end of file + + public static PlatformDAO getPlatformDAO(){ + if (databaseEngine != null) { + switch (databaseEngine) { + case Constants.DataBaseTypes.DB_TYPE_H2: + return new H2PlatformDAOImpl(); + case Constants.DataBaseTypes.DB_TYPE_MYSQL: + return new MySQLPlatformDAOImpl(); + default: + throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine); + } + } + throw new IllegalStateException("Database engine has not initialized properly."); + } + + public static LifecycleStateDAO getLifecycleStateDAO(){ + if (databaseEngine != null) { + switch (databaseEngine) { + case Constants.DataBaseTypes.DB_TYPE_H2: + return new H2LifecycleStateDAOImpl(); + case Constants.DataBaseTypes.DB_TYPE_MYSQL: + return new MySQLLifecycleStateDAOImpl(); + default: + throw new UnsupportedDatabaseEngineException("Unsupported database engine : " + databaseEngine); + } + } + throw new IllegalStateException("Database engine has not initialized properly."); + } +} + + 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 19cb7da13a..1a845010e8 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 @@ -25,6 +25,7 @@ import org.json.JSONException; import org.wso2.carbon.device.application.mgt.common.Application; import org.wso2.carbon.device.application.mgt.common.Platform; import org.wso2.carbon.device.application.mgt.common.Category; +import org.wso2.carbon.device.application.mgt.core.util.JSONUtil; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -46,13 +47,13 @@ public class Util { application.setIconName(rs.getString("ICON_NAME")); application.setBannerName(rs.getString("BANNER_NAME")); application.setVideoName(rs.getString("VIDEO_NAME")); - application.setScreenshots(jsonArrayStringToList(rs.getString("SCREENSHOTS"))); + application.setScreenshots(JSONUtil.jsonArrayStringToList(rs.getString("SCREENSHOTS"))); application.setCreatedAt(rs.getDate("CREATED_AT")); application.setModifiedAt(rs.getDate("MODIFIED_AT")); Platform platform = new Platform(); platform.setName(rs.getString("APL_NAME")); - platform.setCode(rs.getString("APL_IDENTIFIER")); + platform.setIdentifier(rs.getString("APL_IDENTIFIER")); application.setPlatform(platform); Map properties = new HashMap<>(); @@ -90,15 +91,5 @@ public class Util { } } - public static List jsonArrayStringToList(String value) throws JSONException { - JSONArray jsonArray = new JSONArray(value); - List list = new ArrayList<>(); - if (jsonArray != null) { - int len = jsonArray.length(); - for (int i = 0; i < len; i++) { - list.add(jsonArray.get(i).toString()); - } - } - return list; - } + } \ No newline at end of file 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/AbstractDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/AbstractDAOImpl.java new file mode 100644 index 0000000000..b302abb70c --- /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/AbstractDAOImpl.java @@ -0,0 +1,31 @@ +/* + * 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; + +import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; +import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; + +import java.sql.Connection; + +public abstract class AbstractDAOImpl { + + protected Connection getConnection() throws DBConnectionException { + return ConnectionManagerUtil.getConnection(); + } +} 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/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 similarity index 80% rename from components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/AbstractApplicationDAOImpl.java rename to 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 ebc06a5dae..405559ca6a 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/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 @@ -16,7 +16,7 @@ * under the License. * */ -package org.wso2.carbon.device.application.mgt.core.dao.impl; +package org.wso2.carbon.device.application.mgt.core.dao.impl.application; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -24,38 +24,24 @@ import org.wso2.carbon.device.application.mgt.common.Application; import org.wso2.carbon.device.application.mgt.common.Filter; 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.impl.AbstractDAOImpl; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.application.mgt.core.dao.common.Util; -import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -public abstract class AbstractApplicationDAOImpl implements ApplicationDAO { +public abstract class AbstractApplicationDAOImpl extends AbstractDAOImpl implements ApplicationDAO { private static final Log log = LogFactory.getLog(AbstractApplicationDAOImpl.class); - @Override - public Application createApplication(Application application) throws ApplicationManagementDAOException { - return null; - } - - @Override - public Application editApplication(Application application) throws ApplicationManagementDAOException { - return null; - } - @Override public void deleteApplication(Application application) throws ApplicationManagementDAOException { } - private Connection getConnection() throws DBConnectionException { - return ConnectionManagerUtil.getConnection(); - } - @Override public int getApplicationCount(Filter filter) throws ApplicationManagementDAOException { if(log.isDebugEnabled()){ @@ -77,8 +63,7 @@ public abstract class AbstractApplicationDAOImpl implements ApplicationDAO { conn = this.getConnection(); sql += "SELECT COUNT(APP.ID) AS APP_COUNT "; sql += "FROM APPM_APPLICATION AS APP "; - sql += "INNER JOIN APPM_PLATFORM_APPLICATION_MAPPING AS APM ON APP.PLATFORM_APPLICATION_MAPPING_ID = APM.ID "; - sql += "INNER JOIN APPM_PLATFORM AS APL ON APM.PLATFORM_ID = APL.ID "; + sql += "INNER JOIN APPM_PLATFORM AS APL ON APP.PLATFORM_ID = APL.ID "; sql += "INNER JOIN APPM_APPLICATION_CATEGORY AS CAT ON APP.APPLICATION_CATEGORY_ID = CAT.ID "; if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) { 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 9d9816f0cb..7e6827b5a6 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 @@ -28,7 +28,6 @@ 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.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.application.mgt.core.dao.common.Util; -import org.wso2.carbon.device.application.mgt.core.dao.impl.AbstractApplicationDAOImpl; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import java.sql.Connection; @@ -45,6 +44,11 @@ public class H2ApplicationDAOImpl extends AbstractApplicationDAOImpl { private static final Log log = LogFactory.getLog(H2ApplicationDAOImpl.class); + @Override + public Application createApplication(Application application) throws ApplicationManagementDAOException { + return null; + } + @Override public ApplicationList getApplications(Filter filter) throws ApplicationManagementDAOException { @@ -133,7 +137,10 @@ public class H2ApplicationDAOImpl extends AbstractApplicationDAOImpl { return applicationList; } - private Connection getConnection() throws DBConnectionException { - return ConnectionManagerUtil.getConnection(); + @Override + public Application editApplication(Application application) throws ApplicationManagementDAOException { + return null; } + + } 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 c0f5697f56..b7821504fe 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 @@ -28,15 +28,14 @@ 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.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.application.mgt.core.dao.common.Util; -import org.wso2.carbon.device.application.mgt.core.dao.impl.AbstractApplicationDAOImpl; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; +import org.wso2.carbon.device.application.mgt.core.util.JSONUtil; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.sql.*; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; +import java.util.Map; /** * This class holds the generic implementation of ApplicationDAO which can be used to support ANSI db syntax. @@ -48,7 +47,7 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl { @Override public ApplicationList getApplications(Filter filter) throws ApplicationManagementDAOException { - if(log.isDebugEnabled()){ + if (log.isDebugEnabled()) { log.debug("Getting application data from the database"); log.debug(String.format("Filter: limit=%s, offset=%", filter.getLimit(), filter.getOffset())); } @@ -75,8 +74,7 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl { sql += "SELECT SQL_CALC_FOUND_ROWS APP.*, APL.NAME AS APL_NAME, APL.IDENTIFIER AS APL_IDENTIFIER," + " CAT.NAME AS CAT_NAME "; sql += "FROM APPM_APPLICATION AS APP "; - sql += "INNER JOIN APPM_PLATFORM_APPLICATION_MAPPING AS APM ON APP.PLATFORM_APPLICATION_MAPPING_ID = APM.ID "; - sql += "INNER JOIN APPM_PLATFORM AS APL ON APM.PLATFORM_ID = APL.ID "; + sql += "INNER JOIN APPM_PLATFORM AS APL ON APP.PLATFORM_ID = APL.ID "; sql += "INNER JOIN APPM_APPLICATION_CATEGORY AS CAT ON APP.APPLICATION_CATEGORY_ID = CAT.ID "; if (filter.getSearchQuery() != null && !filter.getSearchQuery().isEmpty()) { @@ -139,7 +137,85 @@ public class MySQLApplicationDAOImpl extends AbstractApplicationDAOImpl { return applicationList; } - private Connection getConnection() throws DBConnectionException { - return ConnectionManagerUtil.getConnection(); + @Override + public Application editApplication(Application application) throws ApplicationManagementDAOException { + return null; + } + + + @Override + public Application createApplication(Application application) throws ApplicationManagementDAOException { + + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; + String sql = ""; + + + try { + conn = this.getConnection(); + sql += "INSERT INTO APPM_APPLICATION (UUID, 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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + + 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.executeUpdate(); + + rs = stmt.getGeneratedKeys(); + if (rs.next()) { + application.setId(rs.getInt(1)); + } + + 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/dao/impl/lifecyclestate/AbstractLifecycleStateDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/lifecyclestate/AbstractLifecycleStateDAOImpl.java new file mode 100644 index 0000000000..917558fda6 --- /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/lifecyclestate/AbstractLifecycleStateDAOImpl.java @@ -0,0 +1,26 @@ +/* + * 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.lifecyclestate; + +import org.wso2.carbon.device.application.mgt.core.dao.LifecycleStateDAO; +import org.wso2.carbon.device.application.mgt.core.dao.impl.AbstractDAOImpl; + +public abstract class AbstractLifecycleStateDAOImpl extends AbstractDAOImpl implements LifecycleStateDAO { + +} 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/lifecyclestate/H2LifecycleStateDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/lifecyclestate/H2LifecycleStateDAOImpl.java new file mode 100644 index 0000000000..9333c8b4b5 --- /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/lifecyclestate/H2LifecycleStateDAOImpl.java @@ -0,0 +1,33 @@ +/* + * 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.lifecyclestate; + +import org.wso2.carbon.device.application.mgt.common.LifecycleState; +import org.wso2.carbon.device.application.mgt.common.Platform; +import org.wso2.carbon.device.application.mgt.core.dao.impl.platform.AbstractPlatformDAOImpl; +import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; + +public class H2LifecycleStateDAOImpl extends AbstractLifecycleStateDAOImpl { + + + @Override + public LifecycleState getLifeCycleStateByIdentifier(String identifier) throws ApplicationManagementDAOException { + return null; + } +} 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/lifecyclestate/MySQLLifecycleStateDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/lifecyclestate/MySQLLifecycleStateDAOImpl.java new file mode 100644 index 0000000000..421fba789b --- /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/lifecyclestate/MySQLLifecycleStateDAOImpl.java @@ -0,0 +1,76 @@ +/* + * 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.lifecyclestate; + +import org.wso2.carbon.device.application.mgt.common.LifecycleState; +import org.wso2.carbon.device.application.mgt.common.Platform; +import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; +import org.wso2.carbon.device.application.mgt.core.dao.common.Util; +import org.wso2.carbon.device.application.mgt.core.dao.impl.platform.AbstractPlatformDAOImpl; +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; + +public class MySQLLifecycleStateDAOImpl extends AbstractLifecycleStateDAOImpl { + + @Override + public LifecycleState getLifeCycleStateByIdentifier(String identifier) throws ApplicationManagementDAOException { + + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; + String sql = ""; + + try { + conn = this.getConnection(); + sql += "SELECT * "; + sql += "FROM APPM_LIFECYCLE_STATE "; + sql += "WHERE IDENTIFIER = ? "; + + stmt = conn.prepareStatement(sql); + stmt.setString(1, identifier); + rs = stmt.executeQuery(); + + LifecycleState lifecycleState = null; + + if (rs.next()) { + lifecycleState = new LifecycleState(); + lifecycleState.setId(rs.getInt("ID")); + lifecycleState.setName(rs.getString("NAME")); + lifecycleState.setIdentifier(rs.getString("IDENTIFIER")); + lifecycleState.setDescription(rs.getString("DESCRIPTION")); + } + + return lifecycleState; + + } catch (SQLException e) { + throw new ApplicationManagementDAOException("Error occurred while getting application List", e); + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); + } finally { + Util.cleanupResources(stmt, rs); + } + + + } + +} \ No newline at end of file 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/platform/AbstractPlatformDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/platform/AbstractPlatformDAOImpl.java new file mode 100644 index 0000000000..d6331064d6 --- /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/platform/AbstractPlatformDAOImpl.java @@ -0,0 +1,39 @@ +/* + * 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.platform; + +import org.json.JSONException; +import org.wso2.carbon.device.application.mgt.common.Platform; +import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; +import org.wso2.carbon.device.application.mgt.core.dao.PlatformDAO; +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 org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +public abstract class AbstractPlatformDAOImpl extends AbstractDAOImpl implements PlatformDAO { + + + +} 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/platform/H2PlatformDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/platform/H2PlatformDAOImpl.java new file mode 100644 index 0000000000..d293d53f9c --- /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/platform/H2PlatformDAOImpl.java @@ -0,0 +1,31 @@ +/* + * 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.platform; + +import org.wso2.carbon.device.application.mgt.common.Platform; +import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; + +public class H2PlatformDAOImpl extends AbstractPlatformDAOImpl { + + + @Override + public Platform getPlatformByIdentifier(String identifier) throws ApplicationManagementDAOException { + return null; + } +} 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/platform/MySQLPlatformDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/platform/MySQLPlatformDAOImpl.java new file mode 100644 index 0000000000..d3914f87ee --- /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/platform/MySQLPlatformDAOImpl.java @@ -0,0 +1,74 @@ +/* + * 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.platform; + +import org.wso2.carbon.device.application.mgt.common.Platform; +import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; +import org.wso2.carbon.device.application.mgt.core.dao.common.Util; +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; + +public class MySQLPlatformDAOImpl extends AbstractPlatformDAOImpl { + + @Override + public Platform getPlatformByIdentifier(String identifier) throws ApplicationManagementDAOException { + + Connection conn = null; + PreparedStatement stmt = null; + ResultSet rs = null; + String sql = ""; + + try { + conn = this.getConnection(); + sql += "SELECT * "; + sql += "FROM APPM_PLATFORM "; + sql += "WHERE IDENTIFIER = ? "; + + stmt = conn.prepareStatement(sql); + stmt.setString(1, identifier); + rs = stmt.executeQuery(); + + Platform platform = null; + + if (rs.next()) { + platform = new Platform(); + platform.setId(rs.getInt("ID")); + platform.setName(rs.getString("NAME")); + platform.setIdentifier(rs.getString("IDENTIFIER")); + platform.setPublished(rs.getBoolean("PUBLISHED")); + } + + return platform; + + } catch (SQLException e) { + throw new ApplicationManagementDAOException("Error occurred while getting application List", e); + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); + } finally { + Util.cleanupResources(stmt, rs); + } + + + } + +} \ No newline at end of file diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/NotFoundException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/NotFoundException.java new file mode 100644 index 0000000000..5314dd06ac --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/NotFoundException.java @@ -0,0 +1,33 @@ +/* + * 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.exception; + +import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; + +public class NotFoundException extends ApplicationManagementException { + + public NotFoundException(String message, Throwable throwable) { + super(message, throwable); + } + + public NotFoundException(String 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/exception/ValidationException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ValidationException.java new file mode 100644 index 0000000000..7044f2ce5e --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ValidationException.java @@ -0,0 +1,32 @@ +/* + * 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.exception; + +import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; + +public class ValidationException extends ApplicationManagementException { + + public ValidationException(String message, Throwable throwable) { + super(message, throwable); + } + + public ValidationException(String 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/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 0e538b4d6c..f0b756d318 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,29 +18,87 @@ */ package org.wso2.carbon.device.application.mgt.core.impl; -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.Filter; +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.services.ApplicationManager; 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.NotFoundException; +import org.wso2.carbon.device.application.mgt.core.exception.ValidationException; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; +import org.wso2.carbon.device.application.mgt.core.util.HelperUtil; + +import java.util.Date; public class ApplicationManagerImpl implements ApplicationManager { + + public static final String CREATED = "created"; + @Override public Application createApplication(Application application) throws ApplicationManagementException { - return null; + + validateApplication(application, false); + + try { + ConnectionManagerUtil.openConnection(); + ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO(); + + application.setUuid(HelperUtil.generateApplicationUuid()); + + application.setCreatedAt(new Date()); + application.setModifiedAt(new Date()); + + LifecycleStateDAO lifecycleStateDAO = DAOFactory.getLifecycleStateDAO(); + LifecycleState lifecycleState = lifecycleStateDAO.getLifeCycleStateByIdentifier(CREATED); + if (lifecycleState == null) { + throw new NotFoundException("Invalid lifecycle state."); + } + + Lifecycle lifecycle = new Lifecycle(); + lifecycle.setLifecycleState(lifecycleState); + lifecycle.setLifecycleState(lifecycleState); + lifecycle.setLifecycleStateModifiedAt(new Date()); + lifecycle.setGetLifecycleStateModifiedBy(application.getUser().getUserName()); + application.setCurrentLifecycle(lifecycle); + + PlatformDAO platformDAO = DAOFactory.getPlatformDAO(); + Platform platform = platformDAO.getPlatformByIdentifier(application.getPlatform().getIdentifier()); + if (platform == null) { + throw new NotFoundException("Invalid platform"); + } + application.setPlatform(platform); + + return applicationDAO.createApplication(application); + } finally { + ConnectionManagerUtil.closeConnection(); + } + } + @Override - public Application editApplication(int applicationId, Application application) throws ApplicationManagementException { - return null; + public Application editApplication(int uuid, Application application) throws ApplicationManagementException { + + validateApplication(application, true); + + try { + ConnectionManagerUtil.openConnection(); + ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO(); + + application.setModifiedAt(new Date()); + + return applicationDAO.editApplication(application); + } finally { + ConnectionManagerUtil.closeConnection(); + } + } @Override - public void deleteApplication(int applicationId) throws ApplicationManagementException { + public void deleteApplication(int uuid) throws ApplicationManagementException { } @@ -56,4 +114,27 @@ public class ApplicationManagerImpl implements ApplicationManager { } } + + private void validateApplication(Application application, boolean isEdit) throws ValidationException { + + if (application.getName() == null) { + throw new ValidationException("Application name cannot be empty"); + } + + if (application.getUser() == null || application.getUser().getUserName() == null || + application.getUser().getTenantId() == 0) { + throw new ValidationException("Username and tenant Id cannot be empty"); + } + + if (!isEdit) { + if (application.getCategory() == null || application.getCategory().getId() == 0) { + throw new ValidationException("Category id cannot be empty"); + } + if (application.getPlatform() == null || application.getPlatform().getIdentifier() == null) { + throw new ValidationException("Platform identifier cannot be empty"); + } + } + + + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java index b7c7d4830b..68e136983f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java @@ -130,30 +130,33 @@ public class ConnectionManagerUtil { } public static void closeConnection() { - TxState txState = currentTxState.get(); - if (TxState.CONNECTION_NOT_BORROWED == txState) { - if (log.isDebugEnabled()) { - log.debug("No successful connection appears to have been borrowed to perform the underlying " + - "transaction even though the 'openConnection' method has been called. Therefore, " + - "'closeConnection' method is returning silently"); + if(currentTxState != null) { + TxState txState = currentTxState.get(); + + if (TxState.CONNECTION_NOT_BORROWED == txState) { + if (log.isDebugEnabled()) { + log.debug("No successful connection appears to have been borrowed to perform the underlying " + + "transaction even though the 'openConnection' method has been called. Therefore, " + + "'closeConnection' method is returning silently"); + } + currentTxState.remove(); + return; } - currentTxState.remove(); - return; - } - Connection conn = currentConnection.get(); - if (conn == null) { - throw new IllegalTransactionStateException("No connection is associated with the current transaction. " + - "This might have ideally been caused by not properly initiating the transaction via " + - "'beginTransaction'/'openConnection' methods"); - } - try { - conn.close(); - } catch (SQLException e) { - log.warn("Error occurred while close the connection", e); + Connection conn = currentConnection.get(); + if (conn == null) { + throw new IllegalTransactionStateException("No connection is associated with the current transaction. " + + "This might have ideally been caused by not properly initiating the transaction via " + + "'beginTransaction'/'openConnection' methods"); + } + try { + conn.close(); + } catch (SQLException e) { + log.warn("Error occurred while close the connection", e); + } + currentConnection.remove(); + currentTxState.remove(); } - currentConnection.remove(); - currentTxState.remove(); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/HelperUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/HelperUtil.java new file mode 100644 index 0000000000..6d4720cc72 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/HelperUtil.java @@ -0,0 +1,29 @@ +/* + * 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.util; + +import java.util.UUID; + +public class HelperUtil { + + public static String generateApplicationUuid(){ + return UUID.randomUUID().toString(); + } + +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/JSONUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/JSONUtil.java new file mode 100644 index 0000000000..cd443f1dc5 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/JSONUtil.java @@ -0,0 +1,50 @@ +/* + * 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.util; + +import org.json.JSONArray; +import org.json.JSONException; + +import java.util.ArrayList; +import java.util.List; + +public class JSONUtil { + + public static List jsonArrayStringToList(String value) throws JSONException { + JSONArray jsonArray = new JSONArray(value); + List list = new ArrayList<>(); + if (jsonArray != null) { + int len = jsonArray.length(); + for (int i = 0; i < len; i++) { + list.add(jsonArray.get(i).toString()); + } + } + return list; + } + + public static String listToJsonArrayString(List list) { + JSONArray jsonArray = new JSONArray(); + if (list != null) { + for (String listItem : list) { + jsonArray.put(listItem); + } + } + return jsonArray.toString(); + } +}