From 2cbf78d4648a8f421780de6c9445d82014a3eeff Mon Sep 17 00:00:00 2001 From: lasantha Date: Thu, 15 Feb 2018 14:22:57 +0530 Subject: [PATCH] Fixed issues in Generic DAO Impl --- .../GenericApplicationDAOImpl.java | 69 +++--- .../impl/LifecycleManagementAPIImpl.java | 208 ------------------ 2 files changed, 34 insertions(+), 243 deletions(-) delete mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/LifecycleManagementAPIImpl.java 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 ca4a69fbf3d..2d68b040e2e 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 @@ -102,7 +102,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic ResultSet rs = null; int index = 0; String sql = "INSERT INTO AP_APP_TAG (TAG, TENANT_ID, AP_APP_ID) VALUES (?, ?, ?)"; - try{ + try { conn = this.getDBConnection(); conn.setAutoCommit(false); stmt = conn.prepareStatement(sql); @@ -114,9 +114,9 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } stmt.executeBatch(); - }catch (DBConnectionException e) { + } catch (DBConnectionException e) { throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection when adding tags", e); - }catch (SQLException e) { + } catch (SQLException e) { throw new ApplicationManagementDAOException("Error occurred while adding tags", e); } finally { Util.cleanupResources(stmt, rs); @@ -138,19 +138,19 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic conn = this.getDBConnection(); conn.setAutoCommit(false); stmt = conn.prepareStatement(sql); - stmt.setString(++index , appName); - stmt.setString(++index , type); + stmt.setString(++index, appName); + stmt.setString(++index, type); stmt.setInt(++index, tenantId); rs = stmt.executeQuery(); if (rs.next()) { isExist = 1; - } + } - return isExist; + return isExist; - }catch (DBConnectionException e) { + } catch (DBConnectionException e) { throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection when verifying application existence", e); - }catch (SQLException e) { + } catch (SQLException e) { throw new ApplicationManagementDAOException("Error occurred while adding unrestricted roles", e); } finally { Util.cleanupResources(stmt, rs); @@ -219,8 +219,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } catch (SQLException e) { throw new ApplicationManagementDAOException("Error occurred while getting application list for the tenant" + " " + tenantId + ". While executing " + sql, e); - } - catch (DBConnectionException e) { + } catch (DBConnectionException e) { throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection while " + "getting application list for the tenant " + tenantId, e); } catch (JSONException e) { @@ -256,7 +255,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic if (rs.next()) { uuId = rs.getString("UUID"); } - return uuId; + return uuId; } catch (SQLException e) { throw new ApplicationManagementDAOException("Error occurred while getting uuid of latest app release", e); } catch (DBConnectionException e) { @@ -315,7 +314,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic @Override public Application getApplication(String appName, String appType, int tenantId) throws ApplicationManagementDAOException { - if (log.isDebugEnabled()){ + if (log.isDebugEnabled()) { log.debug("Getting application with the type(" + appType + " and Name " + appName + " ) from the database"); } @@ -325,9 +324,9 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic 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.IS_FREE, AP_APP_TAG.TAG, AP_UNRESTRICTED_ROLES.ROLE AS RELESE_ID FROM " - + "AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLES WHERE AP_APP.NAME=? AND AP_APP.TYPE= ? " - + "AND AP_APP.TENANT_ID=?;"; + + "AS APP_CATEGORY, AP_APP.IS_FREE AS IS_FREE, AP_APP.RESTRICTED AS RESTRICTED, AP_APP_TAG.TAG AS " + + "APP_TAG, AP_UNRESTRICTED_ROLES.ROLE AS AS ROLE FROM AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLES " + + "WHERE AP_APP.NAME=? AND AP_APP.TYPE= ? AND AP_APP.TENANT_ID=?;"; stmt = conn.prepareStatement(sql); stmt.setString(1, appName); @@ -337,7 +336,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic if (log.isDebugEnabled()) { log.debug("Successfully retrieved basic details of the application with the type " - + appType +"and app name "+ appName); + + appType + "and app name " + appName); } return Util.loadApplication(rs); @@ -357,7 +356,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic @Override public Application getApplicationById(int applicationId, int tenantId) throws ApplicationManagementDAOException { - if (log.isDebugEnabled()){ + if (log.isDebugEnabled()) { log.debug("Getting application with the id (" + applicationId + ") from the database"); } Connection conn; @@ -365,9 +364,10 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic 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 \n" - + "AS APP_CATEGORY, AP_APP.IS_FREE, AP_APP_TAG.TAG, AP_UNRESTRICTED_ROLES.ROLE AS RELESE_ID FROM \n" - + "AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLES WHERE AP_APP.ID=? AND AP_APP.TENANT_ID=?;"; + 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.IS_FREE AS IS_FREE, AP_APP.RESTRICTED AS RESTRICTED, AP_APP_TAG.TAG AS APP_TAG, " + + "AP_UNRESTRICTED_ROLES.ROLE AS ROLE FROM AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLES WHERE AP_APP.ID=?" + + " AND AP_APP.TENANT_ID=?;"; stmt = conn.prepareStatement(sql); stmt.setInt(1, applicationId); @@ -395,7 +395,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic @Override public Boolean verifyApplicationExistenceById(int appId) throws ApplicationManagementDAOException { - if (log.isDebugEnabled()){ + if (log.isDebugEnabled()) { log.debug("Getting application with the application ID(" + appId + " ) from the database"); } Connection conn; @@ -416,7 +416,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic log.debug("Successfully retrieved basic details of the application with the application ID " + appId); } - if (rs.next()){ + if (rs.next()) { isAppExist = true; } @@ -425,8 +425,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } catch (SQLException e) { throw new ApplicationManagementDAOException( "Error occurred while getting application details with app ID " + appId + " While executing query ", e); - } - catch (DBConnectionException e) { + } catch (DBConnectionException e) { throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); } finally { Util.cleanupResources(stmt, rs); @@ -439,7 +438,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic PreparedStatement stmt = null; Application existingApplication = this.getApplication(application.getName(), application.getType(), tenantId); - if (existingApplication == null){ + if (existingApplication == null) { throw new ApplicationManagementException("There doesn't have an application for updating"); } try { @@ -451,16 +450,16 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic if (application.getName() != null && !application.getName().equals(existingApplication.getName())) { sql += "NAME = ?, "; } - if (application.getType() != null && !application.getType().equals(existingApplication.getType())){ + if (application.getType() != null && !application.getType().equals(existingApplication.getType())) { sql += "TYPE = ?, "; } - if (application.getAppCategory() != null && !application.getAppCategory().equals(existingApplication.getAppCategory())){ + if (application.getAppCategory() != null && !application.getAppCategory().equals(existingApplication.getAppCategory())) { sql += "APP_CATEGORY = ?, "; } - if (application.getIsRestricted() != existingApplication.getIsRestricted()){ + if (application.getIsRestricted() != existingApplication.getIsRestricted()) { sql += "RESTRICTED = ? "; } - if (application.getIsFree() != existingApplication.getIsFree()){ + if (application.getIsFree() != existingApplication.getIsFree()) { sql += "IS_FREE = ? "; } @@ -470,16 +469,16 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic if (application.getName() != null && !application.getName().equals(existingApplication.getName())) { stmt.setString(++index, application.getName()); } - if (application.getType() != null && !application.getType().equals(existingApplication.getType())){ + if (application.getType() != null && !application.getType().equals(existingApplication.getType())) { stmt.setString(++index, application.getType()); } - if (application.getAppCategory() != null && !application.getAppCategory().equals(existingApplication.getAppCategory())){ + if (application.getAppCategory() != null && !application.getAppCategory().equals(existingApplication.getAppCategory())) { stmt.setString(++index, application.getAppCategory()); } - if (application.getIsRestricted() != existingApplication.getIsRestricted()){ + if (application.getIsRestricted() != existingApplication.getIsRestricted()) { stmt.setInt(++index, application.getIsRestricted()); } - if (application.getIsFree() != existingApplication.getIsFree()){ + if (application.getIsFree() != existingApplication.getIsFree()) { stmt.setInt(++index, application.getIsFree()); } @@ -509,7 +508,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic } catch (DBConnectionException e) { throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); } catch (SQLException e) { - throw new ApplicationManagementDAOException("Error occurred while deleting the application: " , e); + throw new ApplicationManagementDAOException("Error occurred while deleting the application: ", e); } finally { Util.cleanupResources(stmt, null); } 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/LifecycleManagementAPIImpl.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/LifecycleManagementAPIImpl.java deleted file mode 100644 index 7b27467770d..00000000000 --- 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/LifecycleManagementAPIImpl.java +++ /dev/null @@ -1,208 +0,0 @@ -/* - * Copyright (c) 2016, 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.publisher.api.services.impl; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -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.exception.NotFoundException; -import org.wso2.carbon.device.application.mgt.core.util.Constants; -import org.wso2.carbon.device.application.mgt.publisher.api.APIUtil; -import org.wso2.carbon.device.application.mgt.publisher.api.services.LifecycleManagementAPI; -import org.wso2.carbon.device.application.mgt.common.LifecycleState; -import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; - -import java.util.Arrays; -import javax.ws.rs.*; -import javax.ws.rs.core.Response; - -/** - * Lifecycle Management related jax-rs APIs. - */ -@Path("/lifecycle") -public class LifecycleManagementAPIImpl implements LifecycleManagementAPI { - - private static Log log = LogFactory.getLog(LifecycleManagementAPIImpl.class); - - @GET - @Path("/{appId}/{uuid}") - public Response getLifecycleState( - @PathParam("appId") int applicationId, - @PathParam("uuid") String applicationUuid) { - LifecycleState lifecycleState; - ApplicationManager applicationManager = APIUtil.getApplicationManager(); - try { - lifecycleState = applicationManager.getLifecycleState(applicationId, applicationUuid); - } catch (LifecycleManagementException e) { - String msg = "Error occurred while getting lifecycle state."; - log.error(msg, e); - return Response.status(Response.Status.BAD_REQUEST).build(); - } - return Response.status(Response.Status.OK).entity(lifecycleState).build(); - } - - - @POST - @Path("/{appId}/{uuid}") - public Response addLifecycleState( - @PathParam("appId") int applicationId, - @PathParam("uuid") String applicationUuid, - LifecycleState state) { - ApplicationManager applicationManager = APIUtil.getApplicationManager(); - try { - applicationManager.addLifecycleState(applicationId, applicationUuid, state); - } catch (LifecycleManagementException e) { - String msg = "Error occurred while adding lifecycle state."; - log.error(msg, e); - return Response.status(Response.Status.BAD_REQUEST).build(); - } - return Response.status(Response.Status.CREATED).entity("Lifecycle state added successfully.").build(); - } - - - //todo remove below part - - @DELETE - @Path("/{identifier}") - public Response deleteLifecycleState(@PathParam("identifier") String identifier) { - return null; -// LifecycleStateManager lifecycleStateManager = APIUtil.getLifecycleStateManager(); -// try { -// lifecycleStateManager.deleteLifecycleState(identifier); -// } catch (LifecycleManagementException e) { -// String msg = "Error occurred while deleting lifecycle state."; -// log.error(msg, e); -// return Response.status(Response.Status.BAD_REQUEST).build(); -// } -// return Response.status(Response.Status.OK).entity("Lifecycle state deleted successfully.").build(); - } - -// @PUT -// @Consumes("application/json") -// @Path("/{uuid}/lifecycle") -// public Response changeLifecycleState(@PathParam("uuid") String applicationUUID, @QueryParam("state") String state) { -// ApplicationManager applicationManager = APIUtil.getApplicationManager(); -// -// if (!Arrays.asList(Constants.LIFE_CYCLES).contains(state)) { -// log.warn("Provided lifecycle state " + state + " is not valid. Please select one from" -// + Arrays.toString(Constants.LIFE_CYCLES)); -// return Response.status(Response.Status.BAD_REQUEST) -// .entity("Provided lifecycle state " + state + " is not valid. Please select one from " -// + Arrays.toString(Constants.LIFE_CYCLES)).build(); -// } -// try { -// applicationManager.changeLifecycle(applicationUUID, state); -// return Response.status(Response.Status.OK) -// .entity("Successfully changed the lifecycle state of the application: " + applicationUUID).build(); -// } catch (org.wso2.carbon.device.application.mgt.core.exception.NotFoundException e) { -// return Response.status(Response.Status.NOT_FOUND).build(); -// } catch (ApplicationManagementException e) { -// String msg = "Error occurred while changing the lifecycle of application: " + applicationUUID; -// log.error(msg, e); -// return APIUtil.getResponse(e, Response.Status.BAD_REQUEST); -// } -// } -// -// @GET -// @Path("/{uuid}/lifecycle") -// @Override -// public Response getNextLifeCycleStates(@PathParam("uuid") String applicationUUID) { -// ApplicationManager applicationManager = APIUtil.getApplicationManager(); -// try { -// if (applicationManager.getApplication(applicationUUID) == null) { -// if (log.isDebugEnabled()) { -// log.debug("Application with the UUID '" + applicationUUID + "' is not found."); -// } -// return Response.status(Response.Status.NOT_FOUND).entity("Application with the UUID '" + -// applicationUUID + "' is not found.").build(); -// } -// -// if (log.isDebugEnabled()) { -// log.debug("Application with UUID '" + applicationUUID + "' is found. Request received for getting " -// + "next life-cycle states for the particular application."); -// } -// return Response.status(Response.Status.OK).entity(applicationManager.getLifeCycleStates(applicationUUID)) -// .build(); -// } catch (NotFoundException e) { -// return Response.status(Response.Status.NOT_FOUND).build(); -// } catch (ApplicationManagementException e) { -// log.error("Application Management Exception while trying to get next states for the applications with " -// + "the application ID", e); -// return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); -// } -// } - - // ToDo - - @PUT - @Consumes("application/json") - @Path("/{uuid}/lifecycle") - public Response changeLifecycleState(@PathParam("uuid") String applicationUUID, @QueryParam("state") String state) { - ApplicationManager applicationManager = APIUtil.getApplicationManager(); - - if (!Arrays.asList(Constants.LIFE_CYCLES).contains(state)) { - log.warn("Provided lifecycle state " + state + " is not valid. Please select one from" - + Arrays.toString(Constants.LIFE_CYCLES)); - return Response.status(Response.Status.BAD_REQUEST) - .entity("Provided lifecycle state " + state + " is not valid. Please select one from " - + Arrays.toString(Constants.LIFE_CYCLES)).build(); - } - try { - applicationManager.changeLifecycle(applicationUUID, state); - return Response.status(Response.Status.OK) - .entity("Successfully changed the lifecycle state of the application: " + applicationUUID).build(); - } catch (NotFoundException e) { - return Response.status(Response.Status.NOT_FOUND).build(); - } catch (ApplicationManagementException e) { - String msg = "Error occurred while changing the lifecycle of application: " + applicationUUID; - log.error(msg, e); - return APIUtil.getResponse(e, Response.Status.BAD_REQUEST); - } - } - - @GET - @Path("/{uuid}/lifecycle") - @Override - public Response getNextLifeCycleStates(@PathParam("uuid") String applicationUUID) { - ApplicationManager applicationManager = APIUtil.getApplicationManager(); - try { -// if (applicationManager.getApplication(applicationUUID) == null) { -// if (log.isDebugEnabled()) { -// log.debug("Application with the UUID '" + applicationUUID + "' is not found."); -// } -// return Response.status(Response.Status.NOT_FOUND).entity("Application with the UUID '" + -// applicationUUID + "' is not found.").build(); -// } - - if (log.isDebugEnabled()) { - log.debug("Application with UUID '" + applicationUUID + "' is found. Request received for getting " - + "next life-cycle states for the particular application."); - } - return Response.status(Response.Status.OK).entity(applicationManager.getLifeCycleStates(applicationUUID)) - .build(); - } catch (NotFoundException e) { - return Response.status(Response.Status.NOT_FOUND).build(); - } catch (ApplicationManagementException e) { - log.error("Application Management Exception while trying to get next states for the applications with " - + "the application ID", e); - return APIUtil.getResponse(e, Response.Status.INTERNAL_SERVER_ERROR); - } - } - -}