use of org.wso2.carbon.apimgt.core.models.Subscription in project carbon-apimgt by wso2.
the class ApiDAOImpl method getCompositeAPIApplicationId.
private String getCompositeAPIApplicationId(Connection connection, String apiId) throws APIMgtDAOException {
APISubscriptionDAO apiSubscriptionDAO = DAOFactory.getAPISubscriptionDAO();
List<Subscription> subscriptions = apiSubscriptionDAO.getAPISubscriptionsByAPI(apiId);
if (!subscriptions.isEmpty()) {
return subscriptions.get(0).getApplication().getId();
}
throw new IllegalStateException("Composite API ID " + apiId + " has no associated Application subscription");
}
use of org.wso2.carbon.apimgt.core.models.Subscription in project carbon-apimgt by wso2.
the class APISubscriptionDAOImpl method getPendingAPISubscriptionsByApplication.
/**
* Retrieve the list of subscriptions of an Application which are in pending state
*
* @param applicationId The UUID of Application
* @return A list of {@link Subscription} objects which has pending status
* @throws APIMgtDAOException If failed to get subscriptions.
*/
@Override
public List<Subscription> getPendingAPISubscriptionsByApplication(String applicationId) throws APIMgtDAOException {
final String getSubscriptionsByAppSql = "SELECT SUBS.UUID AS SUBS_UUID, SUBS.TIER_ID AS SUBS_TIER, " + "SUBS.API_ID AS API_ID, SUBS.APPLICATION_ID AS APP_ID, SUBS.SUB_STATUS AS SUB_STATUS, " + "SUBS.SUB_TYPE AS SUB_TYPE, API.PROVIDER AS API_PROVIDER, API.NAME AS API_NAME, " + "API.CONTEXT AS API_CONTEXT, API.VERSION AS API_VERSION, POLICY.NAME AS SUBS_POLICY " + "FROM AM_SUBSCRIPTION SUBS, AM_API API, AM_SUBSCRIPTION_POLICY POLICY " + "WHERE SUBS.APPLICATION_ID = ? AND SUBS.API_ID = API.UUID AND SUBS.TIER_ID = POLICY.UUID " + "AND SUBS.SUB_STATUS=?";
try (Connection conn = DAOUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(getSubscriptionsByAppSql)) {
ps.setString(1, applicationId);
ps.setString(2, SubscriptionStatus.ON_HOLD.toString());
try (ResultSet rs = ps.executeQuery()) {
return createSubscriptionsWithApiInformationOnly(rs);
}
} catch (SQLException e) {
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting pending api subscriptions by application(appId: " + applicationId + ")", e);
}
}
use of org.wso2.carbon.apimgt.core.models.Subscription in project carbon-apimgt by wso2.
the class APISubscriptionDAOImpl method getAPISubscriptionsByApplication.
@Override
public List<Subscription> getAPISubscriptionsByApplication(String applicationId, ApiType apiType) throws APIMgtDAOException {
final String getSubscriptionsByAppSql = "SELECT SUBS.UUID AS SUBS_UUID, SUBS.TIER_ID AS SUBS_TIER, " + "SUBS.API_ID AS API_ID, SUBS.APPLICATION_ID AS APP_ID, SUBS.SUB_STATUS AS SUB_STATUS, " + "SUBS.SUB_TYPE AS SUB_TYPE, API.PROVIDER AS API_PROVIDER, API.NAME AS API_NAME, " + "API.CONTEXT AS API_CONTEXT, API.VERSION AS API_VERSION, POLICY.NAME AS SUBS_POLICY " + "FROM AM_SUBSCRIPTION SUBS, AM_API API, AM_SUBSCRIPTION_POLICY POLICY " + "WHERE SUBS.APPLICATION_ID = ? AND SUBS.API_ID = API.UUID AND SUBS.TIER_ID = POLICY.UUID " + "AND API.API_TYPE_ID = (SELECT TYPE_ID FROM AM_API_TYPES WHERE TYPE_NAME = ?)";
try (Connection conn = DAOUtil.getConnection();
PreparedStatement ps = conn.prepareStatement(getSubscriptionsByAppSql)) {
ps.setString(1, applicationId);
ps.setString(2, apiType.toString());
try (ResultSet rs = ps.executeQuery()) {
return createSubscriptionsWithApiInformationOnly(rs);
}
} catch (SQLException e) {
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "getting api subscriptions by application(appId: " + applicationId + ", apiType: " + apiType + ")", e);
}
}
use of org.wso2.carbon.apimgt.core.models.Subscription in project carbon-apimgt by wso2.
the class APISubscriptionDAOImpl method createSubscription.
void createSubscription(String apiId, String appId, String uuid, String policyId, APIMgtConstants.SubscriptionStatus status, Connection conn) throws APIMgtDAOException, SQLException {
// check for existing subscriptions
final String checkExistingSubscriptionSql = " SELECT UUID FROM AM_SUBSCRIPTION WHERE API_ID = ? " + "AND APPLICATION_ID = ?";
try (PreparedStatement ps = conn.prepareStatement(checkExistingSubscriptionSql)) {
ps.setString(1, apiId);
ps.setString(2, appId);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
throw new APIMgtDAOException("Subscription already exists for API " + DAOFactory.getApiDAO().getAPI(apiId).getName() + " in Application " + DAOFactory.getApplicationDAO().getApplication(appId).getName(), ExceptionCodes.SUBSCRIPTION_ALREADY_EXISTS);
}
}
}
// add new subscription
final String addSubscriptionSql = "INSERT INTO AM_SUBSCRIPTION (UUID, TIER_ID, API_ID, APPLICATION_ID," + "SUB_STATUS, CREATED_TIME) VALUES (?,?,?,?,?,?)";
try (PreparedStatement ps = conn.prepareStatement(addSubscriptionSql)) {
conn.setAutoCommit(false);
ps.setString(1, uuid);
ps.setString(2, policyId);
ps.setString(3, apiId);
ps.setString(4, appId);
ps.setString(5, status != null ? status.toString() : APIMgtConstants.SubscriptionStatus.ACTIVE.toString());
ps.setTimestamp(6, Timestamp.valueOf(LocalDateTime.now()));
ps.execute();
}
}
use of org.wso2.carbon.apimgt.core.models.Subscription in project carbon-apimgt by wso2.
the class APISubscriptionDAOImpl method createSubscriptionsWithApiInformationOnly.
private List<Subscription> createSubscriptionsWithApiInformationOnly(ResultSet rs) throws APIMgtDAOException {
List<Subscription> subscriptionList = new ArrayList<>();
try {
Subscription subscription;
while (rs.next()) {
String subscriptionId = rs.getString("SUBS_UUID");
String subscriptionTier = rs.getString("SUBS_POLICY");
API.APIBuilder apiBuilder = new API.APIBuilder(rs.getString("API_PROVIDER"), rs.getString("API_NAME"), rs.getString("API_VERSION"));
apiBuilder.id(rs.getString("API_ID"));
apiBuilder.context(rs.getString("API_CONTEXT"));
API api = apiBuilder.build();
subscription = new Subscription(subscriptionId, null, api, new SubscriptionPolicy(subscriptionTier));
subscription.setStatus(APIMgtConstants.SubscriptionStatus.valueOf(rs.getString("SUB_STATUS")));
subscriptionList.add(subscription);
}
} catch (SQLException e) {
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "creating subscriptions api information", e);
}
return subscriptionList;
}
Aggregations