Search in sources :

Example 41 with APIMgtDAOException

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

the class ApiDAOImpl method constructRatingFromResultSet.

/**
 * Constructs a Rating object from a result set
 *
 * @param rs the result set retrieved from db
 * @return Rating constructed from result set
 * @throws APIMgtDAOException if result set access fails
 */
private Rating constructRatingFromResultSet(ResultSet rs) throws APIMgtDAOException {
    Rating rating = new Rating();
    try {
        rating.setUuid(rs.getString("UUID"));
        rating.setRating((int) Double.parseDouble(rs.getString("RATING")));
        rating.setApiId(rs.getString("API_ID"));
        rating.setUsername(rs.getString("USER_IDENTIFIER"));
        rating.setCreatedUser(rs.getString("CREATED_BY"));
        rating.setCreatedTime(rs.getTimestamp("CREATED_TIME").toLocalDateTime());
        rating.setLastUpdatedUser(rs.getString("UPDATED_BY"));
        rating.setLastUpdatedTime(rs.getTimestamp("LAST_UPDATED_TIME").toLocalDateTime());
    } catch (SQLException e) {
        String errorMessage = "constructing Rating from ResultSet";
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + errorMessage, e);
    }
    return rating;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Rating(org.wso2.carbon.apimgt.core.models.Rating)

Example 42 with APIMgtDAOException

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

the class EntityDAO method getLastUpdatedTimeOfResourceByName.

/**
 * Returns the last access time of the given entity identified by the NAME field.
 *
 * @param resourceTableName Table name of the entity
 * @param name value in the NAME field of the entity
 * @return Last access time of the requested resource
 * @throws APIMgtDAOException
 */
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
static String getLastUpdatedTimeOfResourceByName(String resourceTableName, String name) throws APIMgtDAOException {
    final String query = "SELECT LAST_UPDATED_TIME FROM " + resourceTableName + " WHERE NAME = ?";
    String lastUpdatedTime = null;
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setString(1, name);
        try (ResultSet rs = statement.executeQuery()) {
            if (rs.next()) {
                lastUpdatedTime = rs.getString("LAST_UPDATED_TIME");
            }
        }
        return lastUpdatedTime;
    } catch (SQLException e) {
        throw new APIMgtDAOException("Error while retrieving last access time from table : " + resourceTableName + " and entity " + name, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 43 with APIMgtDAOException

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

the class FunctionDAOImpl method getUserDeployedFunctions.

/**
 * {@inheritDoc}
 */
@Override
public List<Function> getUserDeployedFunctions(String userName) throws APIMgtDAOException {
    if (userName == null) {
        throw new IllegalArgumentException("Username must not be null");
    }
    List<Function> functions = new ArrayList<>();
    final String sqlQuery = "SELECT FUNCTION_NAME, FUNCTION_URI " + "FROM AM_LAMBDA_FUNCTION " + "WHERE USER_NAME = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
        preparedStatement.setString(1, userName);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                String functionName = resultSet.getString("FUNCTION_NAME");
                URI endpointURI = new URI(resultSet.getString("FUNCTION_URI"));
                functions.add(new Function(functionName, endpointURI));
            }
        } catch (URISyntaxException e) {
            throw new APIMgtDAOException("Not a URI", e);
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException("Error retrieving functions of user: " + userName, e);
    }
    return functions;
}
Also used : Function(org.wso2.carbon.apimgt.core.models.Function) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 44 with APIMgtDAOException

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

the class ApplicationDAOImpl method addApplicationKeys.

@Override
public void addApplicationKeys(String appId, String keyType, String consumerKey) throws APIMgtDAOException {
    final String addApplicationKeysQuery = "INSERT INTO AM_APP_KEY_MAPPING (APPLICATION_ID, CLIENT_ID, KEY_TYPE) " + "VALUES (?, ?, ?)";
    try (Connection conn = DAOUtil.getConnection()) {
        conn.setAutoCommit(false);
        try (PreparedStatement ps = conn.prepareStatement(addApplicationKeysQuery)) {
            ps.setString(1, appId);
            ps.setString(2, consumerKey);
            ps.setString(3, keyType);
            ps.executeUpdate();
            conn.commit();
        } catch (SQLException ex) {
            conn.rollback();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "adding application keys(appId: " + appId + ")", ex);
        } finally {
            conn.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException ex) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "adding application keys(appId: " + 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 45 with APIMgtDAOException

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

the class ApplicationDAOImpl method updateApplicationState.

@Override
public void updateApplicationState(String appID, String state) throws APIMgtDAOException {
    final String updateAppQuery = "UPDATE AM_APPLICATION SET APPLICATION_STATUS=?, LAST_UPDATED_TIME=?  " + "WHERE UUID = ?";
    try (Connection conn = DAOUtil.getConnection()) {
        conn.setAutoCommit(false);
        try (PreparedStatement ps = conn.prepareStatement(updateAppQuery)) {
            ps.setString(1, state);
            ps.setTimestamp(2, Timestamp.valueOf(LocalDateTime.now()));
            ps.setString(3, appID);
            ps.executeUpdate();
            conn.commit();
        } catch (SQLException ex) {
            conn.rollback();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "updating application state(appId: " + appID + ", state: " + state + ")", ex);
        } finally {
            conn.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException ex) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "updating application state(appId: " + appID + ", state: " + state + ")", ex);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

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