use of org.wso2.carbon.apimgt.api.model.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.api.model.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.api.model.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;
}
use of org.wso2.carbon.apimgt.api.model.Label in project carbon-apimgt by wso2.
the class LabelDAOImpl method insertAccessUrlMappings.
/**
* Add access url mappings
*
* @param labelId Id of the label
* @param accessUrls List of access urls
* @throws APIMgtDAOException if error occurs while adding access url mappings
*/
private static void insertAccessUrlMappings(String labelId, List<String> accessUrls) throws APIMgtDAOException {
if (!accessUrls.isEmpty()) {
List<String> existingAccessUrls = getLabelAccessUrls(labelId);
if (!existingAccessUrls.isEmpty()) {
accessUrls.removeAll(existingAccessUrls);
}
final String query = "INSERT INTO AM_LABEL_ACCESS_URL_MAPPING (LABEL_ID, ACCESS_URL) " + "VALUES (?,?)";
try (Connection connection = DAOUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
for (String accessUrl : accessUrls) {
statement.setString(1, labelId);
statement.setString(2, accessUrl);
statement.addBatch();
}
statement.executeBatch();
} catch (SQLException e) {
String message = DAOUtil.DAO_ERROR_PREFIX + "adding access url mappings for [label id] " + labelId;
throw new APIMgtDAOException(message, e);
}
}
}
use of org.wso2.carbon.apimgt.api.model.Label in project carbon-apimgt by wso2.
the class LabelDAOImpl method getLabels.
/**
* @see LabelDAO#getLabels()
*/
@Override
public List<Label> getLabels() throws APIMgtDAOException {
final String query = "SELECT LABEL_ID, NAME, TYPE_NAME FROM AM_LABELS";
List<Label> labels = new ArrayList<>();
try (Connection connection = DAOUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
try (ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
Label label = new Label.Builder().id(rs.getString("LABEL_ID")).name(rs.getString("NAME")).type(rs.getString("TYPE_NAME")).accessUrls(getLabelAccessUrls(rs.getString("LABEL_ID"))).build();
labels.add(label);
}
}
} catch (SQLException e) {
String message = DAOUtil.DAO_ERROR_PREFIX + "retrieving labels";
throw new APIMgtDAOException(message, e);
}
return labels;
}
Aggregations