use of org.wso2.carbon.apimgt.core.exception.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);
}
}
use of org.wso2.carbon.apimgt.core.exception.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);
}
}
use of org.wso2.carbon.apimgt.core.exception.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);
}
}
use of org.wso2.carbon.apimgt.core.exception.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);
}
}
use of org.wso2.carbon.apimgt.core.exception.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;
}
Aggregations