From df65b685976cba80c2dd8668f201e0104be2caec Mon Sep 17 00:00:00 2001 From: Inosh Perara Date: Mon, 24 Sep 2018 02:15:28 +0000 Subject: [PATCH] app get by ID --- .../common/services/ApplicationManager.java | 7 ++- .../mgt/core/dao/ApplicationDAO.java | 11 +++++ .../GenericApplicationDAOImpl.java | 44 ++++++++++++++++++- .../mgt/core/impl/ApplicationManagerImpl.java | 43 +++++++++++++++++- .../services/ApplicationManagementAPI.java | 11 ++--- .../impl/ApplicationManagementAPIImpl.java | 12 ++--- 6 files changed, 107 insertions(+), 21 deletions(-) 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 28203e646e0..7d5af468758 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 @@ -87,14 +87,13 @@ public interface ApplicationManager { String getUuidOfLatestRelease(int appId) throws ApplicationManagementException; /** - * To get Application with the given UUID. + * To get Application with the given Id. * - * @param appType type of the Application - * @param appName name of the Application + * @param id id of the Application * @return the Application identified by the UUID * @throws ApplicationManagementException Application Management Exception. */ - Application getApplication(String appType, String appName) throws ApplicationManagementException; + Application getApplicationById(int id) throws ApplicationManagementException; /** * To get an application associated with the release. 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 b65a266c32d..2556238e616 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 @@ -84,6 +84,17 @@ public interface ApplicationDAO { */ Application getApplication(String appName, String appType, int tenantId) throws ApplicationManagementDAOException; + /** + * To get the application with the given id + * + * @param id ID of the application. + * @param tenantId ID of the tenant. + * @return the application + * @throws ApplicationManagementDAOException Application Management DAO Exception. + */ + Application getApplicationById(String id, int tenantId) throws + ApplicationManagementDAOException; + /** * To get the application with the given uuid * 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 70248a15ad6..ab294dc15ff 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 @@ -337,6 +337,48 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } } + @Override + public Application getApplicationById(String id, int tenantId) throws + ApplicationManagementDAOException { + if (log.isDebugEnabled()) { + log.debug("Getting application with the id:" + id); + } + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + try { + conn = this.getDBConnection(); + String sql = + "SELECT AP_APP.ID AS APP_ID, AP_APP.NAME AS APP_NAME, AP_APP.TYPE AS APP_TYPE, AP_APP.APP_CATEGORY " + + "AS APP_CATEGORY, AP_APP.SUB_TYPE AS SUB_TYPE ,AP_APP.CURRENCY AS CURRENCY," + + " AP_APP.RESTRICTED AS RESTRICTED, AP_APP_TAG.TAG AS APP_TAG, AP_UNRESTRICTED_ROLE.ROLE " + + "AS ROLE FROM AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLE WHERE AP_APP.NAME=? AND " + + "AP_APP.APP_ID= ? AND AP_APP.TENANT_ID=?;"; + + stmt = conn.prepareStatement(sql); + stmt.setString(1, id); + stmt.setInt(2, tenantId); + rs = stmt.executeQuery(); + + if (log.isDebugEnabled()) { + log.debug("Successfully retrieved basic details of the application with the id:" + id); + } + + return Util.loadApplication(rs); + + } catch (SQLException e) { + throw new ApplicationManagementDAOException( + "Error occurred while getting application details with app id " + id + + " While executing query ", e); + } catch (JSONException e) { + throw new ApplicationManagementDAOException("Error occurred while parsing JSON", e); + } catch (DBConnectionException e) { + throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); + } finally { + Util.cleanupResources(stmt, rs); + } + } + @Override public Application getApplicationById(int applicationId, int tenantId) throws ApplicationManagementDAOException { @@ -420,7 +462,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic int paramIndex = 1; Connection conn; PreparedStatement stmt = null; - Application existingApplication = this.getApplication(application.getName(), application.getType(), tenantId); + Application existingApplication = this.getApplicationById(application.getId(), tenantId); if (existingApplication == null) { throw new ApplicationManagementException("There doesn't have an application for updating"); 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 58099ba0717..d8c7706972b 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 @@ -276,6 +276,46 @@ public class ApplicationManagerImpl implements ApplicationManager { } + @Override + public Application getApplicationById(int id) throws ApplicationManagementException { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + Application application; + boolean isAppAllowed = false; + List applicationReleases; + try { + ConnectionManagerUtil.openDBConnection(); + application = ApplicationManagementDAOFactory.getApplicationDAO() + .getApplicationById(id, tenantId); + if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { + applicationReleases = getReleases(application.getId()); + application.setApplicationReleases(applicationReleases); + return application; + } + + if (!application.getUnrestrictedRoles().isEmpty()) { + if (isRoleExists(application.getUnrestrictedRoles(), userName)) { + isAppAllowed = true; + } + } else { + isAppAllowed = true; + } + + if (!isAppAllowed) { + return null; + } + + applicationReleases = getReleases(application.getId()); + application.setApplicationReleases(applicationReleases); + return application; + } catch (UserStoreException e) { + throw new ApplicationManagementException( + "User-store exception while getting application with the application id " + id); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + private boolean isRoleExists(Collection unrestrictedRoleList, String userName) throws UserStoreException { String[] roleList; @@ -301,7 +341,6 @@ public class ApplicationManagerImpl implements ApplicationManager { return roleList; } - @Override public Application getApplication(String appType, String appName) throws ApplicationManagementException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String userName = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); @@ -311,7 +350,7 @@ public class ApplicationManagerImpl implements ApplicationManager { try { ConnectionManagerUtil.openDBConnection(); application = ApplicationManagementDAOFactory.getApplicationDAO() - .getApplication(appType, appName, tenantId); + .getApplication(appName, appType, tenantId); if (isAdminUser(userName, tenantId, CarbonConstants.UI_ADMIN_PERMISSION_COLLECTION)) { applicationReleases = getReleases(application.getId()); application.setApplicationReleases(applicationReleases); 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/ApplicationManagementAPI.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/ApplicationManagementAPI.java index 1dec91b98b8..35b70eeba57 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/ApplicationManagementAPI.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/ApplicationManagementAPI.java @@ -174,15 +174,10 @@ public interface ApplicationManagementAPI { }) Response getApplication( @ApiParam( - name = "appType", - value = "Type of the application", + name = "appId", + value = "application Id", required = true) - @PathParam("appType") String appType, - @ApiParam( - name = "appName", - value = "Application name", - required = true) - @QueryParam("isWithImages") String appName + @PathParam("appId") int appId ); @PUT 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 9e27fa77a62..6f16e1dc0fe 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 @@ -85,23 +85,23 @@ public class ApplicationManagementAPIImpl implements ApplicationManagementAPI { } } + @GET @Consumes("application/json") - @Path("/{appType}") + @Path("/{appId}") public Response getApplication( - @PathParam("appType") String appType, - @QueryParam("appName") String appName) { + @PathParam("appId") int appId) { ApplicationManager applicationManager = APIUtil.getApplicationManager(); try { - Application application = applicationManager.getApplication(appType, appName); + Application application = applicationManager.getApplicationById(appId); if (application == null) { return Response.status(Response.Status.NOT_FOUND).entity - ("Application with application type: " + appType + " not found").build(); + ("Application with application id: " + appId + " not found").build(); } return Response.status(Response.Status.OK).entity(application).build(); } catch (ApplicationManagementException e) { - log.error("Error occurred while getting application with the uuid " + appType, e); + log.error("Error occurred while getting application with the id " + appId, e); return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); } }