use of org.wso2.carbon.apimgt.core.exception.APIMgtDAOException in project carbon-apimgt by wso2.
the class DAOFactory method getAnalyticsDAO.
/**
* To get the AnalyticsDao object. Depends on different vendors.
*
* @return AnalyticsDAO object
* @throws APIMgtDAOException if error during getting analytics database connection
*/
public static AnalyticsDAO getAnalyticsDAO() throws APIMgtDAOException {
AnalyticsDAO analyticsDAO;
boolean isAnalyticsEnabled = ServiceReferenceHolder.getInstance().getAPIMConfiguration().getAnalyticsConfigurations().isEnabled();
if (isAnalyticsEnabled) {
try (Connection connection = DAOUtil.getAnalyticsConnection()) {
analyticsDAO = getAnalyticsDaoImplForVendor(connection);
} catch (SQLException e) {
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting FunctionDAO", e);
}
} else {
// if analytics is not enabled create a normal AMDB data connection to check db driver
try (Connection connection = DAOUtil.getConnection()) {
analyticsDAO = getAnalyticsDaoImplForVendor(connection);
} catch (SQLException e) {
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting FunctionDAO", e);
}
}
return analyticsDAO;
}
use of org.wso2.carbon.apimgt.core.exception.APIMgtDAOException in project carbon-apimgt by wso2.
the class LabelDAOImpl method updateLabel.
/**
* @see LabelDAO#updateLabel(Label)
*/
@Override
public Label updateLabel(Label updatedLabel) throws APIMgtDAOException {
final String query = "UPDATE AM_LABELS SET NAME=? , TYPE_NAME=? WHERE LABEL_ID=?";
String labelId = updatedLabel.getId();
List<String> accessURLs = updatedLabel.getAccessUrls();
try (Connection connection = DAOUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
try {
connection.setAutoCommit(false);
statement.setString(1, updatedLabel.getName());
if (updatedLabel.getType() != null) {
statement.setString(2, updatedLabel.getType().toUpperCase(Locale.ENGLISH));
} else {
statement.setString(2, updatedLabel.getType());
}
statement.setString(3, updatedLabel.getId());
statement.executeUpdate();
connection.commit();
deleteLabelAccessUrlMappings(labelId);
if (APIMgtConstants.LABEL_TYPE_GATEWAY.equalsIgnoreCase(updatedLabel.getType())) {
insertAccessUrlMappings(labelId, accessURLs);
} else {
accessURLs = new ArrayList<>();
}
} catch (SQLException e) {
connection.rollback();
String message = "Error while updating the label" + " [label id] " + labelId;
throw new APIMgtDAOException(message, e, ExceptionCodes.LABEL_UPDATE_FAILED);
} finally {
connection.setAutoCommit(DAOUtil.isAutoCommit());
}
return new Label.Builder().id(labelId).name(updatedLabel.getName()).description(updatedLabel.getDescription()).accessUrls(accessURLs).type(updatedLabel.getType()).build();
} catch (SQLException e) {
String message = "Error while updating the label [label ID] " + labelId;
throw new APIMgtDAOException(message, e, ExceptionCodes.LABEL_UPDATE_FAILED);
}
}
use of org.wso2.carbon.apimgt.core.exception.APIMgtDAOException in project carbon-apimgt by wso2.
the class LabelDAOImpl method getLabelsByName.
/**
* @see LabelDAO#getLabelsByName(List)
*/
@SuppressFBWarnings("SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING")
@Override
public List<Label> getLabelsByName(List<String> labelNames) throws APIMgtDAOException {
List<Label> matchingLabels = new ArrayList<>();
if (!labelNames.isEmpty()) {
final String query = "SELECT LABEL_ID, NAME FROM AM_LABELS WHERE NAME IN (" + DAOUtil.getParameterString(labelNames.size()) + ")";
try (Connection connection = DAOUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
for (int i = 0; i < labelNames.size(); ++i) {
statement.setString(i + 1, labelNames.get(i));
}
try (ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
Label label = new Label.Builder().id(rs.getString("LABEL_ID")).name(rs.getString("NAME")).accessUrls(getLabelAccessUrls(rs.getString("LABEL_ID"))).build();
matchingLabels.add(label);
}
}
} catch (SQLException e) {
String message = DAOUtil.DAO_ERROR_PREFIX + "retrieving labels";
throw new APIMgtDAOException(message, e);
}
}
return matchingLabels;
}
use of org.wso2.carbon.apimgt.core.exception.APIMgtDAOException in project carbon-apimgt by wso2.
the class LabelDAOImpl method deleteLabelAccessUrlMappings.
/**
* Delete access urls of a label by label id
*
* @param labelId Id of the label
* @throws APIMgtDAOException if error occurs while deleting label access urls
*/
private void deleteLabelAccessUrlMappings(String labelId) throws APIMgtDAOException {
final String query = "DELETE FROM AM_LABEL_ACCESS_URL_MAPPING WHERE LABEL_ID = ?";
try (Connection connection = DAOUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
try {
connection.setAutoCommit(false);
statement.setString(1, labelId);
statement.execute();
connection.commit();
} catch (SQLException e) {
connection.rollback();
String message = DAOUtil.DAO_ERROR_PREFIX + "deleting the label access url mappings [label id] " + labelId;
throw new APIMgtDAOException(message, e);
} finally {
connection.setAutoCommit(DAOUtil.isAutoCommit());
}
} catch (SQLException e) {
String message = DAOUtil.DAO_ERROR_PREFIX + "deleting the label access url mappings [label id] " + labelId;
throw new APIMgtDAOException(message, e);
}
}
use of org.wso2.carbon.apimgt.core.exception.APIMgtDAOException in project carbon-apimgt by wso2.
the class FunctionDAOImpl method getUserFunctionsForEvent.
/**
* {@inheritDoc}
*/
@Override
public List<Function> getUserFunctionsForEvent(String userName, Event event) throws APIMgtDAOException {
if (userName == null) {
throw new IllegalArgumentException("Username must not be null");
}
if (event == null) {
throw new IllegalArgumentException("Event must not be null");
}
List<Function> functions = new ArrayList<>();
final String sqlQuery = "SELECT FUNCTION_NAME, FUNCTION_URI " + "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 EVENT = ?";
try (Connection connection = DAOUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sqlQuery)) {
preparedStatement.setString(1, userName);
preparedStatement.setString(2, event.getEventAsString());
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 for event: " + event + " of user: " + userName, e);
}
return functions;
}
Aggregations