use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.
the class MssqlSQLStatements method prepareAttributeSearchStatementForStore.
/**
* @see ApiDAOVendorSpecificStatements#prepareAttributeSearchStatementForStore(Connection connection, List, List,
* Map, int, int)
*/
@Override
@SuppressFBWarnings({ "SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING", "OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE" })
public PreparedStatement prepareAttributeSearchStatementForStore(Connection connection, List<String> roles, List<String> labels, Map<String, String> attributeMap, int offset, int limit) throws APIMgtDAOException {
StringBuilder roleListBuilder = new StringBuilder();
roleListBuilder.append("?");
for (int i = 0; i < roles.size() - 1; i++) {
roleListBuilder.append(",?");
}
StringBuilder searchQuery = new StringBuilder();
Iterator<Map.Entry<String, String>> entries = attributeMap.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<String, String> entry = entries.next();
searchQuery.append("LOWER(");
if (APIMgtConstants.TAG_SEARCH_TYPE_PREFIX.equalsIgnoreCase(entry.getKey())) {
searchQuery.append(APIMgtConstants.TAG_NAME_COLUMN);
} else if (APIMgtConstants.SUBCONTEXT_SEARCH_TYPE_PREFIX.equalsIgnoreCase(entry.getKey())) {
searchQuery.append(APIMgtConstants.URL_PATTERN_COLUMN);
} else {
searchQuery.append(entry.getKey());
}
searchQuery.append(") LIKE ?");
if (entries.hasNext()) {
searchQuery.append(" AND ");
}
}
// retrieve the attribute applicable for the search
String searchAttribute = attributeMap.entrySet().iterator().next().getKey();
// get the corresponding implementation based on the attribute to be searched
String query = searchMap.get(searchAttribute).getStoreAttributeSearchQuery(roleListBuilder, searchQuery, offset, limit);
query = "Select * from ( " + query + " ) A" + getStoreAPIsByLabelJoinQuery(labels);
try {
int queryIndex = 1;
PreparedStatement statement = connection.prepareStatement(query);
// include the attribute in the query (for APIs with public visibility)
for (Map.Entry<String, String> entry : attributeMap.entrySet()) {
statement.setString(queryIndex, '%' + entry.getValue().toLowerCase(Locale.ENGLISH) + '%');
queryIndex++;
}
// include user roles in the query
for (String role : roles) {
statement.setString(queryIndex, role);
queryIndex++;
}
// include the attribute in the query (for APIs with restricted visibility)
for (Map.Entry<String, String> entry : attributeMap.entrySet()) {
statement.setString(queryIndex, '%' + entry.getValue().toLowerCase(Locale.ENGLISH) + '%');
queryIndex++;
}
for (String label : labels) {
statement.setString(queryIndex, label);
queryIndex++;
}
statement.setInt(queryIndex, limit);
// setting 0 as the default offset based on store-api.yaml and MSSQL specifications
statement.setInt(++queryIndex, (offset < 0) ? 0 : offset);
return statement;
} catch (SQLException e) {
String errorMsg = "Error occurred while searching APIs for attributes in the database.";
log.error(errorMsg, e);
throw new APIMgtDAOException(errorMsg, e);
}
}
use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.
the class MysqlSQLStatements method prepareAttributeSearchStatementForStore.
/**
* @see ApiDAOVendorSpecificStatements#prepareAttributeSearchStatementForStore(Connection connection, List, List,
* Map, int, int)
*/
@Override
@SuppressFBWarnings({ "SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING", "OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE" })
public PreparedStatement prepareAttributeSearchStatementForStore(Connection connection, List<String> roles, List<String> labels, Map<String, String> attributeMap, int offset, int limit) throws APIMgtDAOException {
StringBuilder roleListBuilder = new StringBuilder();
roleListBuilder.append("?");
for (int i = 0; i < roles.size() - 1; i++) {
roleListBuilder.append(",?");
}
StringBuilder searchQuery = new StringBuilder();
Iterator<Map.Entry<String, String>> entries = attributeMap.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<String, String> entry = entries.next();
searchQuery.append("LOWER(");
if (APIMgtConstants.TAG_SEARCH_TYPE_PREFIX.equalsIgnoreCase(entry.getKey())) {
searchQuery.append(APIMgtConstants.TAG_NAME_COLUMN);
} else if (APIMgtConstants.SUBCONTEXT_SEARCH_TYPE_PREFIX.equalsIgnoreCase(entry.getKey())) {
searchQuery.append(APIMgtConstants.URL_PATTERN_COLUMN);
} else {
searchQuery.append(entry.getKey());
}
searchQuery.append(") LIKE ?");
if (entries.hasNext()) {
searchQuery.append(" AND ");
}
}
// retrieve the attribute applicable for the search
String searchAttribute = attributeMap.entrySet().iterator().next().getKey();
// get the corresponding implementation based on the attribute to be searched
String query = searchMap.get(searchAttribute).getStoreAttributeSearchQuery(roleListBuilder, searchQuery, offset, limit);
query = "Select * from ( " + query + " ) A" + getStoreAPIsByLabelJoinQuery(labels);
try {
int queryIndex = 1;
PreparedStatement statement = connection.prepareStatement(query);
// include the attribute in the query (for APIs with public visibility)
for (Map.Entry<String, String> entry : attributeMap.entrySet()) {
statement.setString(queryIndex, '%' + entry.getValue().toLowerCase(Locale.ENGLISH) + '%');
queryIndex++;
}
// include user roles in the query
for (String role : roles) {
statement.setString(queryIndex, role);
queryIndex++;
}
// include the attribute in the query (for APIs with restricted visibility)
for (Map.Entry<String, String> entry : attributeMap.entrySet()) {
statement.setString(queryIndex, '%' + entry.getValue().toLowerCase(Locale.ENGLISH) + '%');
queryIndex++;
}
for (String label : labels) {
statement.setString(queryIndex, label);
queryIndex++;
}
// setting 0 as the default offset based on store-api.yaml and MySQL specifications
statement.setInt(queryIndex, (offset < 0) ? 0 : offset);
statement.setInt(++queryIndex, limit);
return statement;
} catch (SQLException e) {
String errorMsg = "Error occurred while searching APIs for attributes in the database.";
log.error(errorMsg, e);
throw new APIMgtDAOException(errorMsg, e);
}
}
use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.
the class LabelDAOImpl method getLabelsByType.
@Override
public List<Label> getLabelsByType(String type) throws APIMgtDAOException {
final String query = "SELECT LABEL_ID, NAME, TYPE_NAME FROM AM_LABELS WHERE TYPE_NAME = ?";
List<Label> labels = new ArrayList<>();
try (Connection connection = DAOUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, type.toUpperCase(Locale.ENGLISH));
try (ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
labels.add(new Label.Builder().id(rs.getString("LABEL_ID")).name(rs.getString("NAME")).type(rs.getString("TYPE_NAME")).accessUrls(getLabelAccessUrls(rs.getString("LABEL_ID"))).build());
}
}
return labels;
} catch (SQLException e) {
String message = "Error while retrieving label [label type] " + type;
throw new APIMgtDAOException(message, e, ExceptionCodes.LABEL_NOT_FOUND);
}
}
use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.
the class LabelDAOImpl method addLabels.
/**
* @see LabelDAO#addLabels(List)
*/
@Override
public void addLabels(List<Label> labels) throws APIMgtDAOException {
if (!labels.isEmpty()) {
final String query = "INSERT INTO AM_LABELS (LABEL_ID, NAME) VALUES (?,?)";
Map<String, List<String>> urlMap = new HashMap<>();
try (Connection connection = DAOUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
for (Label label : labels) {
statement.setString(1, label.getId());
statement.setString(2, label.getName());
statement.addBatch();
urlMap.put(label.getId(), label.getAccessUrls());
}
statement.executeBatch();
if (!urlMap.isEmpty()) {
for (Map.Entry<String, List<String>> entry : urlMap.entrySet()) {
if (entry.getValue() != null) {
insertAccessUrlMappings(entry.getKey(), entry.getValue());
}
}
}
} catch (SQLException e) {
String message = DAOUtil.DAO_ERROR_PREFIX + "adding label data";
throw new APIMgtDAOException(message, e);
}
}
}
use of org.wso2.carbon.apimgt.core.models.Label in project carbon-apimgt by wso2.
the class LabelDAOImpl method getLabelAccessUrls.
/**
* Retrieve access urls of a label by label Id
*
* @param labelId Id of the label
* @return List of access urls of the label
* @throws APIMgtDAOException if error occurs while retrieving access urls
*/
private static List<String> getLabelAccessUrls(String labelId) throws APIMgtDAOException {
final String query = "SELECT ACCESS_URL FROM AM_LABEL_ACCESS_URL_MAPPING WHERE LABEL_ID = ?";
List<String> accessUrls = new ArrayList<>();
try (Connection connection = DAOUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
statement.setString(1, labelId);
try (ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
accessUrls.add(rs.getString("ACCESS_URL"));
}
}
} catch (SQLException e) {
String message = DAOUtil.DAO_ERROR_PREFIX + "retrieving access url for [label id] " + labelId;
throw new APIMgtDAOException(message, e);
}
return accessUrls;
}
Aggregations