Search in sources :

Example 31 with Subscription

use of org.wso2.carbon.apimgt.core.models.Subscription in project carbon-apimgt by wso2.

the class SubscriptionMappingUtil method fromSubscriptionListToDTO.

/**
 * Converts a List object of SubscribedAPIs into a DTO
 *
 * @param subscriptions a list of SubscribedAPI objects
 * @param limit max number of objects returned
 * @param offset starting index
 * @return SubscriptionListDTO object containing SubscriptionDTOs
 */
public static SubscriptionListDTO fromSubscriptionListToDTO(List<Subscription> subscriptions, Integer limit, Integer offset) {
    SubscriptionListDTO subscriptionListDTO = new SubscriptionListDTO();
    List<SubscriptionDTO> subscriptionDTOs = subscriptionListDTO.getList();
    if (subscriptionDTOs == null) {
        subscriptionDTOs = new ArrayList<>();
        subscriptionListDTO.setList(subscriptionDTOs);
    }
    // identifying the proper start and end indexes
    int size = subscriptions.size();
    int start = offset < size && offset >= 0 ? offset : Integer.MAX_VALUE;
    int end = offset + limit - 1 <= size - 1 ? offset + limit - 1 : size - 1;
    for (int i = start; i <= end; i++) {
        Subscription subscription = subscriptions.get(i);
        subscriptionDTOs.add(fromSubscriptionToDTO(subscription));
    }
    subscriptionListDTO.setCount(subscriptionDTOs.size());
    return subscriptionListDTO;
}
Also used : Subscription(org.wso2.carbon.apimgt.core.models.Subscription) SubscriptionDTO(org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionDTO) SubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionListDTO)

Example 32 with Subscription

use of org.wso2.carbon.apimgt.core.models.Subscription in project carbon-apimgt by wso2.

the class SubscriptionMappingUtil method fromSubscriptionToDTO.

/**
 * Converts a SubscribedAPI object into SubscriptionDTO
 *
 * @param subscription SubscribedAPI object
 * @return SubscriptionDTO corresponds to SubscribedAPI object
 */
public static SubscriptionDTO fromSubscriptionToDTO(Subscription subscription) {
    SubscriptionDTO subscriptionDTO = new SubscriptionDTO();
    subscriptionDTO.setSubscriptionId(subscription.getId());
    if (subscription.getApi() != null) {
        subscriptionDTO.setApiIdentifier(subscription.getApi().getId());
        subscriptionDTO.setApiName(subscription.getApi().getName());
        subscriptionDTO.setApiVersion(subscription.getApi().getVersion());
    }
    if (subscription.getApplication() != null) {
        subscriptionDTO.setApplicationId(subscription.getApplication().getId());
    }
    subscriptionDTO.setPolicy(subscription.getPolicy().getPolicyName());
    subscriptionDTO.setLifeCycleStatus(SubscriptionDTO.LifeCycleStatusEnum.valueOf(subscription.getStatus().toString()));
    return subscriptionDTO;
}
Also used : SubscriptionDTO(org.wso2.carbon.apimgt.rest.api.store.dto.SubscriptionDTO)

Example 33 with Subscription

use of org.wso2.carbon.apimgt.core.models.Subscription in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createSubscription.

public static Subscription createSubscription(String uuid) {
    if (StringUtils.isEmpty(uuid)) {
        uuid = UUID.randomUUID().toString();
    }
    Subscription subscription = new Subscription(uuid, createDefaultApplication(), createDefaultAPI().build(), goldApiPolicy);
    subscription.setStatus(APIMgtConstants.SubscriptionStatus.ACTIVE);
    return subscription;
}
Also used : Subscription(org.wso2.carbon.apimgt.core.models.Subscription)

Example 34 with Subscription

use of org.wso2.carbon.apimgt.core.models.Subscription in project carbon-apimgt by wso2.

the class APISubscriptionDAOImpl method deleteAPISubscription.

/**
 * Remove an existing API Subscription
 *
 * @param subscriptionId The UUID of the API Subscription that needs to be deleted
 * @throws APIMgtDAOException If failed to delete subscription.
 */
@Override
public void deleteAPISubscription(String subscriptionId) throws APIMgtDAOException {
    final String deleteSubscriptionSql = "DELETE FROM AM_SUBSCRIPTION WHERE UUID = ? ";
    try (Connection conn = DAOUtil.getConnection()) {
        boolean originalAutoCommitState = conn.getAutoCommit();
        conn.setAutoCommit(false);
        try (PreparedStatement ps = conn.prepareStatement(deleteSubscriptionSql)) {
            ps.setString(1, subscriptionId);
            ps.execute();
            conn.commit();
        } catch (SQLException ex) {
            conn.rollback();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "deleting api subscription(subscriptionId: " + subscriptionId + ")", ex);
        } finally {
            conn.setAutoCommit(originalAutoCommitState);
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "deleting api subscription(subscriptionId: " + subscriptionId + ")", e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 35 with Subscription

use of org.wso2.carbon.apimgt.core.models.Subscription in project carbon-apimgt by wso2.

the class APISubscriptionDAOImpl method updateSubscriptionStatus.

/**
 * Update Subscription Status
 *
 * @param subId     ID of the Subscription
 * @param subStatus New Subscription Status
 * @throws APIMgtDAOException If failed to update subscriptions.
 */
@Override
public void updateSubscriptionStatus(String subId, APIMgtConstants.SubscriptionStatus subStatus) throws APIMgtDAOException {
    final String updateSubscriptionSql = "UPDATE AM_SUBSCRIPTION SET SUB_STATUS = ?, LAST_UPDATED_TIME = ? " + "WHERE UUID = ?";
    try (Connection conn = DAOUtil.getConnection()) {
        conn.setAutoCommit(false);
        try (PreparedStatement preparedStatement = conn.prepareStatement(updateSubscriptionSql)) {
            preparedStatement.setString(1, subStatus.toString());
            preparedStatement.setTimestamp(2, Timestamp.valueOf(LocalDateTime.now()));
            preparedStatement.setString(3, subId);
            preparedStatement.execute();
            conn.commit();
        } catch (SQLException ex) {
            conn.rollback();
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "updating subscription status(subscriptionId: " + subId + ", status: " + subStatus + ")", ex);
        } finally {
            conn.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "updating subscription status(subscriptionId: " + subId + ", status: " + subStatus + ")", e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Aggregations

Test (org.testng.annotations.Test)58 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)58 SubscriptionPolicy (org.wso2.carbon.apimgt.core.models.policy.SubscriptionPolicy)37 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)35 APISubscriptionDAO (org.wso2.carbon.apimgt.core.dao.APISubscriptionDAO)34 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)34 Application (org.wso2.carbon.apimgt.core.models.Application)30 API (org.wso2.carbon.apimgt.core.models.API)28 ArrayList (java.util.ArrayList)27 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)22 Response (javax.ws.rs.core.Response)21 Test (org.junit.Test)21 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)21 SQLException (java.sql.SQLException)20 PolicyDAO (org.wso2.carbon.apimgt.core.dao.PolicyDAO)18 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)17 ApplicationDAO (org.wso2.carbon.apimgt.core.dao.ApplicationDAO)15 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)15 Connection (java.sql.Connection)14 PreparedStatement (java.sql.PreparedStatement)14