Search in sources :

Example 51 with APIMgtDAOException

use of org.wso2.carbon.apimgt.api.APIMgtDAOException in project carbon-apimgt by wso2.

the class FunctionDAOImpl method deleteEventFunctionMapping.

/**
 * {@inheritDoc}
 */
@Override
public void deleteEventFunctionMapping(String userName, Event event, String functionName) throws APIMgtDAOException {
    if (userName == null) {
        throw new IllegalArgumentException("Username must not be null");
    }
    if (event == null) {
        throw new IllegalArgumentException("Event must not be null");
    }
    if (functionName == null) {
        throw new IllegalArgumentException("Function name must not be null");
    }
    final String sqlQuery = "DELETE FROM AM_EVENT_FUNCTION_MAPPING " + "WHERE EVENT = ? AND FUNCTION_ID = " + "(SELECT FUNCTION_ID " + "FROM AM_LAMBDA_FUNCTION " + "WHERE USER_NAME = ? AND FUNCTION_NAME = ?)";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
        preparedStatement.setString(1, event.getEventAsString());
        preparedStatement.setString(2, userName);
        preparedStatement.setString(3, functionName);
        preparedStatement.executeUpdate();
    } catch (SQLException e) {
        throw new APIMgtDAOException("Error deleting event function mapping -event: " + event + " -function: " + functionName + " -user: " + userName, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 52 with APIMgtDAOException

use of org.wso2.carbon.apimgt.api.APIMgtDAOException in project carbon-apimgt by wso2.

the class FunctionDAOImpl method getTriggersForUserFunction.

/**
 * {@inheritDoc}
 */
@Override
public List<Event> getTriggersForUserFunction(String userName, String functionName) throws APIMgtDAOException {
    if (userName == null) {
        throw new IllegalArgumentException("Username must not be null");
    }
    if (functionName == null) {
        throw new IllegalArgumentException("Function name must not be null");
    }
    List<Event> events = new ArrayList<>();
    final String sqlQuery = "SELECT EVENT " + "FROM AM_LAMBDA_FUNCTION JOIN AM_EVENT_FUNCTION_MAPPING " + "ON AM_LAMBDA_FUNCTION.FUNCTION_ID = AM_EVENT_FUNCTION_MAPPING.FUNCTION_ID " + "WHERE USER_NAME = ? AND FUNCTION_NAME = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
        preparedStatement.setString(1, userName);
        preparedStatement.setString(2, functionName);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                events.add(Event.valueOf(resultSet.getString("EVENT")));
            }
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException("Error retrieving triggers for function: " + functionName + " of user: " + userName, e);
    }
    return events;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Event(org.wso2.carbon.apimgt.core.models.Event) PreparedStatement(java.sql.PreparedStatement)

Example 53 with APIMgtDAOException

use of org.wso2.carbon.apimgt.api.APIMgtDAOException in project carbon-apimgt by wso2.

the class ApplicationDAOImpl method updateApplication.

/**
 * Update an existing Application
 *
 * @param appID      The UUID of the Application that needs to be updated
 * @param updatedApp Substitute {@link Application} object that will replace the existing Application
 * @throws APIMgtDAOException   If failed to update applications.
 */
@Override
public void updateApplication(String appID, Application updatedApp) throws APIMgtDAOException {
    final String updateAppQuery = "UPDATE AM_APPLICATION SET NAME=?, APPLICATION_POLICY_ID=" + "?, DESCRIPTION=?, APPLICATION_STATUS=?, UPDATED_BY=?, LAST_UPDATED_TIME=? WHERE UUID=?";
    try (Connection conn = DAOUtil.getConnection()) {
        conn.setAutoCommit(false);
        try (PreparedStatement ps = conn.prepareStatement(updateAppQuery)) {
            ps.setString(1, updatedApp.getName());
            ps.setString(2, updatedApp.getPolicy().getUuid());
            ps.setString(3, updatedApp.getDescription());
            ps.setString(4, updatedApp.getStatus());
            ps.setString(5, updatedApp.getUpdatedUser());
            ps.setTimestamp(6, Timestamp.valueOf(updatedApp.getUpdatedTime()));
            ps.setString(7, appID);
            ps.executeUpdate();
            updateApplicationPermission(conn, updatedApp.getPermissionMap(), updatedApp.getId());
            conn.commit();
        } catch (SQLException ex) {
            conn.rollback();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "updating application: " + appID, ex);
        } finally {
            conn.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException ex) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "updating application: " + appID, ex);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 54 with APIMgtDAOException

use of org.wso2.carbon.apimgt.api.APIMgtDAOException in project carbon-apimgt by wso2.

the class ApplicationDAOImpl method deleteApplication.

/**
 * Remove an existing Application
 *
 * @param appID The UUID of the Application that needs to be deleted
 * @throws APIMgtDAOException   If failed to delete application.
 */
@Override
public void deleteApplication(String appID) throws APIMgtDAOException {
    final String appDeleteQuery = "DELETE FROM AM_APPLICATION WHERE UUID = ?";
    try (Connection conn = DAOUtil.getConnection()) {
        boolean originalAutoCommitState = conn.getAutoCommit();
        conn.setAutoCommit(false);
        try (PreparedStatement ps = conn.prepareStatement(appDeleteQuery)) {
            ps.setString(1, appID);
            ps.execute();
            conn.commit();
        } catch (SQLException ex) {
            conn.rollback();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "deleting application: " + appID, ex);
        } finally {
            conn.setAutoCommit(originalAutoCommitState);
        }
    } catch (SQLException ex) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "deleting application: " + appID, ex);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 55 with APIMgtDAOException

use of org.wso2.carbon.apimgt.api.APIMgtDAOException in project carbon-apimgt by wso2.

the class DAOFactory method getAPISubscriptionDAO.

public static APISubscriptionDAO getAPISubscriptionDAO() throws APIMgtDAOException {
    APISubscriptionDAO apiSubscriptionDAO = null;
    try (Connection connection = DAOUtil.getConnection()) {
        String driverName = connection.getMetaData().getDriverName();
        if (driverName.contains(MYSQL) || driverName.contains(H2)) {
            apiSubscriptionDAO = new APISubscriptionDAOImpl();
        } else if (driverName.contains(DB2)) {
        } else if (driverName.contains(MS_SQL) || driverName.contains(MICROSOFT)) {
            apiSubscriptionDAO = new APISubscriptionDAOImpl();
        } else if (driverName.contains(POSTGRE)) {
            apiSubscriptionDAO = new APISubscriptionDAOImpl();
        } else if (driverName.contains(ORACLE)) {
            apiSubscriptionDAO = new APISubscriptionDAOImpl();
        } else {
            throw new APIMgtDAOException("Unhandled DB driver: " + driverName + " detected", ExceptionCodes.APIM_DAO_EXCEPTION);
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting APISubscriptionDAO", e);
    }
    setup();
    return apiSubscriptionDAO;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APISubscriptionDAO(org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Aggregations

APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)333 SQLException (java.sql.SQLException)192 Connection (java.sql.Connection)146 PreparedStatement (java.sql.PreparedStatement)129 Test (org.testng.annotations.Test)84 ResultSet (java.sql.ResultSet)72 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)72 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)57 API (org.wso2.carbon.apimgt.core.models.API)57 ArrayList (java.util.ArrayList)50 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)35 Application (org.wso2.carbon.apimgt.core.models.Application)24 HashMap (java.util.HashMap)23 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)22 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)21 BeforeTest (org.testng.annotations.BeforeTest)21 APIGateway (org.wso2.carbon.apimgt.core.api.APIGateway)20 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)20 IOException (java.io.IOException)19 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)17