Search in sources :

Example 16 with APIMgtDAOException

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

the class ApiDAOImpl method getAPI.

/**
 * {@inheritDoc}
 */
@Override
public API getAPI(String apiID) throws APIMgtDAOException {
    final String query = API_SELECT + " WHERE UUID = ? AND API_TYPE_ID = " + "(SELECT TYPE_ID FROM AM_API_TYPES WHERE TYPE_NAME = ?)";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setString(1, apiID);
        statement.setString(2, ApiType.STANDARD.toString());
        API api = constructAPIFromResultSet(connection, statement);
        if (api == null) {
            throw new APIMgtDAOException("API with ID " + apiID + " does not exist", ExceptionCodes.API_NOT_FOUND);
        }
        return api;
    } catch (SQLException | IOException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting API: " + apiID, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) IOException(java.io.IOException)

Example 17 with APIMgtDAOException

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

the class ApiDAOImpl method initApiTypes.

static void initApiTypes() throws APIMgtDAOException {
    try (Connection connection = DAOUtil.getConnection()) {
        try {
            if (!isApiTypesExist(connection)) {
                connection.setAutoCommit(false);
                addApiTypes(connection);
                connection.commit();
            }
        } catch (SQLException e) {
            connection.rollback();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "adding API types", e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "adding API types", e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 18 with APIMgtDAOException

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

the class ApiDAOImpl method updateComment.

@Override
public void updateComment(Comment comment, String commentId, String apiId) throws APIMgtDAOException {
    final String updateCommentQuery = "UPDATE AM_API_COMMENTS SET COMMENT_TEXT = ? " + ", UPDATED_BY = ? , LAST_UPDATED_TIME = ?" + " WHERE UUID = ? AND API_ID = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(updateCommentQuery)) {
        try {
            connection.setAutoCommit(false);
            statement.setString(1, comment.getCommentText());
            statement.setString(2, comment.getUpdatedUser());
            statement.setTimestamp(3, Timestamp.valueOf(LocalDateTime.now()));
            statement.setString(4, commentId);
            statement.setString(5, apiId);
            statement.execute();
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            String errorMessage = "updating comment for API " + apiId + ", Comment " + commentId;
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + errorMessage, e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        String errorMessage = "updating comment for API " + apiId + ", Comment " + commentId;
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + errorMessage, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 19 with APIMgtDAOException

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

the class ApiDAOImpl method getAPISummary.

/**
 * {@inheritDoc}
 */
@Override
public API getAPISummary(String apiID) throws APIMgtDAOException {
    final String query = API_SUMMARY_SELECT + " WHERE UUID = ? AND API_TYPE_ID = " + "(SELECT TYPE_ID FROM AM_API_TYPES WHERE TYPE_NAME = ?)";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setString(1, apiID);
        statement.setString(2, ApiType.STANDARD.toString());
        List<API> apiResults = constructAPISummaryList(connection, statement);
        if (apiResults.isEmpty()) {
            throw new APIMgtDAOException("API with ID " + apiID + " does not exist", ExceptionCodes.API_NOT_FOUND);
        }
        return apiResults.get(0);
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting API: " + apiID, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API)

Example 20 with APIMgtDAOException

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

the class AnalyticsDAOImpl method getApplicationCount.

/**
 * @see AnalyticsDAO#getApplicationCount(Instant, Instant, String)
 */
@Override
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
public List<ApplicationCount> getApplicationCount(Instant fromTimestamp, Instant toTimestamp, String createdBy) throws APIMgtDAOException {
    final String query;
    if (StringUtils.isNotEmpty(createdBy)) {
        query = "SELECT COUNT(UUID) AS count, CREATED_TIME AS time " + "FROM AM_APPLICATION " + "WHERE (CREATED_TIME BETWEEN ? AND ?) " + "AND CREATED_BY = ?" + "GROUP BY CREATED_TIME " + "ORDER BY CREATED_TIME ASC";
    } else {
        query = "SELECT COUNT(UUID) AS count, CREATED_TIME AS time " + "FROM AM_APPLICATION " + "WHERE (CREATED_TIME BETWEEN ? AND ?) " + "GROUP BY CREATED_TIME " + "ORDER BY CREATED_TIME ASC";
    }
    List<ApplicationCount> applicationCountList = new ArrayList<>();
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(query)) {
        statement.setTimestamp(1, Timestamp.from(fromTimestamp));
        statement.setTimestamp(2, Timestamp.from(toTimestamp));
        if (StringUtils.isNotEmpty(createdBy)) {
            statement.setString(3, createdBy);
        }
        log.debug("Executing query: {} ", query);
        statement.execute();
        try (ResultSet rs = statement.getResultSet()) {
            long count = 0;
            while (rs.next()) {
                ApplicationCount applicationCount = new ApplicationCount();
                count += rs.getLong("count");
                applicationCount.setTimestamp(rs.getTimestamp("time").getTime());
                applicationCount.setCount(count);
                applicationCountList.add(applicationCount);
            }
        }
    } catch (SQLException e) {
        String errorMsg = "Error while creating database connection/prepared-statement";
        throw new APIMgtDAOException(errorMsg, e);
    }
    return applicationCountList;
}
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) PreparedStatement(java.sql.PreparedStatement) ApplicationCount(org.wso2.carbon.apimgt.core.models.analytics.ApplicationCount) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

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