Search in sources :

Example 61 with Label

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);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Label(org.wso2.carbon.apimgt.core.models.Label) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 62 with Label

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);
        }
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Label(org.wso2.carbon.apimgt.core.models.Label) ArrayList(java.util.ArrayList) List(java.util.List) PreparedStatement(java.sql.PreparedStatement) HashMap(java.util.HashMap) Map(java.util.Map)

Example 63 with Label

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;
}
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)

Example 64 with Label

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);
        }
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 65 with Label

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;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Label(org.wso2.carbon.apimgt.core.models.Label) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

Label (org.wso2.carbon.apimgt.core.models.Label)65 ArrayList (java.util.ArrayList)60 Test (org.testng.annotations.Test)45 LabelDAO (org.wso2.carbon.apimgt.core.dao.LabelDAO)32 API (org.wso2.carbon.apimgt.core.models.API)29 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)28 SQLException (java.sql.SQLException)26 PreparedStatement (java.sql.PreparedStatement)25 Connection (java.sql.Connection)20 HashMap (java.util.HashMap)18 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)14 Test (org.junit.Test)13 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)13 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)11 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)11 ResultSet (java.sql.ResultSet)10 IOException (java.io.IOException)9 Map (java.util.Map)9 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)9 BeforeTest (org.testng.annotations.BeforeTest)9