From 48fbc40bd2cd840f606e94bc85611620f637bb78 Mon Sep 17 00:00:00 2001 From: megala21 Date: Tue, 3 Oct 2017 23:28:13 +0530 Subject: [PATCH] Adding platform retrieval based on tag and getting platform tags --- .../api/services/PlatformManagementAPI.java | 40 ++++++++++++++++++- .../impl/PlatformManagementAPIImpl.java | 35 +++++++++++++++- .../mgt/common/services/PlatformManager.java | 8 ++++ .../application/mgt/core/dao/PlatformDAO.java | 2 + .../impl/platform/GenericPlatformDAOImpl.java | 33 +++++++++++++++ .../mgt/core/impl/PlatformManagerImpl.java | 13 ++++++ .../dbscripts/cdm/application-mgt/h2.sql | 2 +- 7 files changed, 128 insertions(+), 5 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/PlatformManagementAPI.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/PlatformManagementAPI.java index ce7ecc88fd..8dd3da5e4a 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/PlatformManagementAPI.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/PlatformManagementAPI.java @@ -134,10 +134,12 @@ public interface PlatformManagementAPI { + "- ENABLED: The platforms that are currently enabled for the tenant\n" + "- DISABLED: The platforms that can be used by the tenant but disabled " + "to be used for tenant\n" - + "- ALL: All the list of platforms that can be used by the tenant", required = false) + + "- ALL: All the list of platforms that can be used by the tenant") @QueryParam("status") @Size(max = 45) - String status + String status, + @ApiParam(name = "tag", defaultValue = "Tag value that we need to search the platform for") + @QueryParam("tag") String tag ); @GET @@ -340,4 +342,38 @@ public interface PlatformManagementAPI { String status ); + @GET + @Path("tags") + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_JSON) + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "get platform tags that starts with the given character sequence", + notes = "This will get all platform tags that has the given character sequence ", + tags = "Platform Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "perm:platform:add") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully retrieved platform tags.", + response = Platform.class, + responseContainer = "List"), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n Error occurred while getting the platform tags.", + response = ErrorResponse.class) + }) + Response getPlatformTags( + @ApiParam(name = "name", value ="The initial part of the name of platform tags that we need to retrieve", + required = true) + @QueryParam("name") @Size(min = 3) String name + ); } 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/PlatformManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/PlatformManagementAPIImpl.java index de6d0829dd..7868733c33 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/PlatformManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/PlatformManagementAPIImpl.java @@ -52,7 +52,7 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { @GET @Override - public Response getPlatforms(@QueryParam("status") String status) { + public Response getPlatforms(@QueryParam("status") String status, @QueryParam("tag") String tag) { int tenantID = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); if (log.isDebugEnabled()) { @@ -61,6 +61,7 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { try { List platforms = APIUtil.getPlatformManager().getPlatforms(tenantID); List results; + List filteredPlatforms = new ArrayList<>(); if (status != null) { if (status.contentEquals(ALL_STATUS)) { results = platforms; @@ -84,10 +85,22 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { } else { results = platforms; } + + if (tag != null) { + if (results != null) { + for (Platform platform : results) { + if (platform.getTags() != null && platform.getTags().contains(tag)) { + filteredPlatforms.add(platform); + } + } + } + } else { + filteredPlatforms = results; + } if (log.isDebugEnabled()) { log.debug("Number of platforms with the status " + status + " : " + results.size()); } - return Response.status(Response.Status.OK).entity(results).build(); + return Response.status(Response.Status.OK).entity(filteredPlatforms).build(); } catch (PlatformManagementException e) { log.error("Error while getting the platforms for tenant - " + tenantID, e); return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); @@ -190,4 +203,22 @@ public class PlatformManagementAPIImpl implements PlatformManagementAPI { return APIUtil.getResponse(e, Response.Status.NOT_FOUND); } } + + @GET + @Path("tags") + @Override + public Response getPlatformTags(@QueryParam("name") String name) { + if (name == null || name.isEmpty() || name.length() < 3) { + return APIUtil.getResponse("In order to get platform tags, it is required to pass the first 3 " + + "characters of the platform tag name", Response.Status.INTERNAL_SERVER_ERROR); + } + try { + List platformTags = APIUtil.getPlatformManager().getPlatformTags(name); + return Response.status(Response.Status.OK).entity(platformTags).build(); + } catch (PlatformManagementException e) { + log.error("Platform Management Exception while trying to get the platform tags with starting character " + + "sequence " + name, e); + return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } + } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/PlatformManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/PlatformManager.java index b9c07f9130..467be42844 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/PlatformManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/PlatformManager.java @@ -133,4 +133,12 @@ public interface PlatformManager { */ public void removePlatforms(int tenantId) throws PlatformManagementException; + /** + * To get the platform tags. + * + * @param name Starting character sequence of the platform name. + * @return list of the platform tags that start with the character sequence. + * @throws PlatformManagementException PlatformManagement Exception + */ + public List getPlatformTags(String name) throws PlatformManagementException; } 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 c67971a7f1..05baa7fe6d 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 @@ -54,4 +54,6 @@ public interface PlatformDAO { int getMultiTenantPlatforms(String identifier) throws PlatformManagementDAOException; + List getPlatformTags(String name) throws PlatformManagementDAOException; + } 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/GenericPlatformDAOImpl.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/GenericPlatformDAOImpl.java index 360eb49cba..3625a90eaf 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/platform/GenericPlatformDAOImpl.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/GenericPlatformDAOImpl.java @@ -666,6 +666,39 @@ public class GenericPlatformDAOImpl extends AbstractDAOImpl implements PlatformD } catch (SQLException e) { throw new PlatformManagementDAOException("SQL exception while executing the query " + sql + " to get the" + " tenants which has the platform with the platform identifier : " + identifier, e); + } finally { + Util.cleanupResources(stmt, rs); + } + } + + @Override + public List getPlatformTags(String name) throws PlatformManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + String sql = ""; + List tagList = new ArrayList<>(); + + try { + conn = this.getDBConnection(); + sql = "SELECT NAME FROM APPM_PLATFORM_TAG WHERE NAME LIKE ?"; + + stmt = conn.prepareStatement(sql); + stmt.setString(1, name + "%"); + rs = stmt.executeQuery(); + + if (rs.next()) { + tagList.add(rs.getString("NAME")); + } + return tagList; + } catch (DBConnectionException e) { + throw new PlatformManagementDAOException("Database Connection exception while trying to get the platform " + + "tags that are starting with " + name, e); + } catch (SQLException e) { + throw new PlatformManagementDAOException("SQL exception while executing the query " + sql + " to get the" + + " platform tags that are starting with " + name, e); + } finally { + Util.cleanupResources(stmt, rs); } } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/PlatformManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/PlatformManagerImpl.java index 5daa733cd3..bff99724d1 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/PlatformManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/PlatformManagerImpl.java @@ -419,6 +419,19 @@ public class PlatformManagerImpl implements PlatformManager { } } + @Override + public List getPlatformTags(String name) throws PlatformManagementException { + try { + ConnectionManagerUtil.openDBConnection(); + return DAOFactory.getPlatformDAO().getPlatformTags(name); + } catch (DBConnectionException e) { + throw new PlatformManagementException("Database Connection Exception while getting the platform tags that" + + " are starting with the character sequence " + name, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + /** * To share the super-tenant platform with other tenants * @param platformIdentifier Identifier of the platform diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql index 3d5e3c4fc4..21855a963b 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/dbscripts/cdm/application-mgt/h2.sql @@ -286,7 +286,7 @@ CREATE INDEX FK_APPLICATION_COMMENTS_APPLICATION_RELEASE ON APPM_COMMENT(APPLICA -- Table APPM_PLATFORM_TAG -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS APPM_PLATFORM_TAG ( - name VARCHAR(100) NOT NULL, + NAME VARCHAR(100) NOT NULL, PLATFORM_ID INT NOT NULL, PRIMARY KEY (PLATFORM_ID, name), CONSTRAINT fk_APPM_SUPPORTED_PLATFORM_TAGS_APPM_SUPPORTED_PLATFORM1