Fixed issues in Generic DAO Impl

feature/appm-store/pbac
lasantha 7 years ago
parent 8b4e3ec051
commit 2cbf78d464

@ -102,7 +102,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
ResultSet rs = null; ResultSet rs = null;
int index = 0; int index = 0;
String sql = "INSERT INTO AP_APP_TAG (TAG, TENANT_ID, AP_APP_ID) VALUES (?, ?, ?)"; String sql = "INSERT INTO AP_APP_TAG (TAG, TENANT_ID, AP_APP_ID) VALUES (?, ?, ?)";
try{ try {
conn = this.getDBConnection(); conn = this.getDBConnection();
conn.setAutoCommit(false); conn.setAutoCommit(false);
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
@ -114,9 +114,9 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
} }
stmt.executeBatch(); stmt.executeBatch();
}catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection when adding tags", 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); throw new ApplicationManagementDAOException("Error occurred while adding tags", e);
} finally { } finally {
Util.cleanupResources(stmt, rs); Util.cleanupResources(stmt, rs);
@ -138,8 +138,8 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
conn = this.getDBConnection(); conn = this.getDBConnection();
conn.setAutoCommit(false); conn.setAutoCommit(false);
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(++index , appName); stmt.setString(++index, appName);
stmt.setString(++index , type); stmt.setString(++index, type);
stmt.setInt(++index, tenantId); stmt.setInt(++index, tenantId);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
if (rs.next()) { if (rs.next()) {
@ -148,9 +148,9 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
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); 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); throw new ApplicationManagementDAOException("Error occurred while adding unrestricted roles", e);
} finally { } finally {
Util.cleanupResources(stmt, rs); Util.cleanupResources(stmt, rs);
@ -219,8 +219,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException("Error occurred while getting application list for the tenant" throw new ApplicationManagementDAOException("Error occurred while getting application list for the tenant"
+ " " + tenantId + ". While executing " + sql, e); + " " + tenantId + ". While executing " + sql, e);
} } catch (DBConnectionException e) {
catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection while " throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection while "
+ "getting application list for the tenant " + tenantId, e); + "getting application list for the tenant " + tenantId, e);
} catch (JSONException e) { } catch (JSONException e) {
@ -315,7 +314,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
@Override @Override
public Application getApplication(String appName, String appType, int tenantId) throws public Application getApplication(String appName, String appType, int tenantId) throws
ApplicationManagementDAOException { ApplicationManagementDAOException {
if (log.isDebugEnabled()){ if (log.isDebugEnabled()) {
log.debug("Getting application with the type(" + appType + " and Name " + appName + log.debug("Getting application with the type(" + appType + " and Name " + appName +
" ) from the database"); " ) from the database");
} }
@ -325,9 +324,9 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
try { try {
conn = this.getDBConnection(); 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 " 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 " + "AS APP_CATEGORY, AP_APP.IS_FREE AS IS_FREE, AP_APP.RESTRICTED AS RESTRICTED, AP_APP_TAG.TAG AS " +
+ "AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLES WHERE AP_APP.NAME=? AND AP_APP.TYPE= ? " "APP_TAG, AP_UNRESTRICTED_ROLES.ROLE AS AS ROLE FROM AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLES " +
+ "AND AP_APP.TENANT_ID=?;"; "WHERE AP_APP.NAME=? AND AP_APP.TYPE= ? AND AP_APP.TENANT_ID=?;";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, appName); stmt.setString(1, appName);
@ -337,7 +336,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Successfully retrieved basic details of the application with the type " 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); return Util.loadApplication(rs);
@ -357,7 +356,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
@Override @Override
public Application getApplicationById(int applicationId, int tenantId) throws public Application getApplicationById(int applicationId, int tenantId) throws
ApplicationManagementDAOException { ApplicationManagementDAOException {
if (log.isDebugEnabled()){ if (log.isDebugEnabled()) {
log.debug("Getting application with the id (" + applicationId + ") from the database"); log.debug("Getting application with the id (" + applicationId + ") from the database");
} }
Connection conn; Connection conn;
@ -365,9 +364,10 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
ResultSet rs = null; ResultSet rs = null;
try { try {
conn = this.getDBConnection(); 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" 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 \n" "AS APP_CATEGORY, AP_APP.IS_FREE AS IS_FREE, AP_APP.RESTRICTED AS RESTRICTED, AP_APP_TAG.TAG AS APP_TAG, " +
+ "AP_APP, AP_APP_TAG, AP_UNRESTRICTED_ROLES WHERE AP_APP.ID=? AND AP_APP.TENANT_ID=?;"; "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 = conn.prepareStatement(sql);
stmt.setInt(1, applicationId); stmt.setInt(1, applicationId);
@ -395,7 +395,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
@Override @Override
public Boolean verifyApplicationExistenceById(int appId) throws ApplicationManagementDAOException { 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"); log.debug("Getting application with the application ID(" + appId + " ) from the database");
} }
Connection conn; 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); log.debug("Successfully retrieved basic details of the application with the application ID " + appId);
} }
if (rs.next()){ if (rs.next()) {
isAppExist = true; isAppExist = true;
} }
@ -425,8 +425,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
} catch (SQLException e) { } catch (SQLException e) {
throw new ApplicationManagementDAOException( throw new ApplicationManagementDAOException(
"Error occurred while getting application details with app ID " + appId + " While executing query ", e); "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); throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
} finally { } finally {
Util.cleanupResources(stmt, rs); Util.cleanupResources(stmt, rs);
@ -439,7 +438,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
PreparedStatement stmt = null; PreparedStatement stmt = null;
Application existingApplication = this.getApplication(application.getName(), application.getType(), tenantId); 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"); throw new ApplicationManagementException("There doesn't have an application for updating");
} }
try { try {
@ -451,16 +450,16 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
if (application.getName() != null && !application.getName().equals(existingApplication.getName())) { if (application.getName() != null && !application.getName().equals(existingApplication.getName())) {
sql += "NAME = ?, "; sql += "NAME = ?, ";
} }
if (application.getType() != null && !application.getType().equals(existingApplication.getType())){ if (application.getType() != null && !application.getType().equals(existingApplication.getType())) {
sql += "TYPE = ?, "; sql += "TYPE = ?, ";
} }
if (application.getAppCategory() != null && !application.getAppCategory().equals(existingApplication.getAppCategory())){ if (application.getAppCategory() != null && !application.getAppCategory().equals(existingApplication.getAppCategory())) {
sql += "APP_CATEGORY = ?, "; sql += "APP_CATEGORY = ?, ";
} }
if (application.getIsRestricted() != existingApplication.getIsRestricted()){ if (application.getIsRestricted() != existingApplication.getIsRestricted()) {
sql += "RESTRICTED = ? "; sql += "RESTRICTED = ? ";
} }
if (application.getIsFree() != existingApplication.getIsFree()){ if (application.getIsFree() != existingApplication.getIsFree()) {
sql += "IS_FREE = ? "; sql += "IS_FREE = ? ";
} }
@ -470,16 +469,16 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
if (application.getName() != null && !application.getName().equals(existingApplication.getName())) { if (application.getName() != null && !application.getName().equals(existingApplication.getName())) {
stmt.setString(++index, application.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()); 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()); stmt.setString(++index, application.getAppCategory());
} }
if (application.getIsRestricted() != existingApplication.getIsRestricted()){ if (application.getIsRestricted() != existingApplication.getIsRestricted()) {
stmt.setInt(++index, application.getIsRestricted()); stmt.setInt(++index, application.getIsRestricted());
} }
if (application.getIsFree() != existingApplication.getIsFree()){ if (application.getIsFree() != existingApplication.getIsFree()) {
stmt.setInt(++index, application.getIsFree()); stmt.setInt(++index, application.getIsFree());
} }
@ -509,7 +508,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
} catch (DBConnectionException e) { } catch (DBConnectionException e) {
throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e); throw new ApplicationManagementDAOException("Error occurred while obtaining the DB connection.", e);
} catch (SQLException 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 { } finally {
Util.cleanupResources(stmt, null); Util.cleanupResources(stmt, 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);
}
}
}
Loading…
Cancel
Save