Search in sources :

Example 56 with SubscribedAPI

use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.

the class ApiMgtDAO method getSubscriptionsOfAPI.

/**
 * @param apiName    Name of the API
 * @param apiVersion Version of the API
 * @param provider   Name of API creator
 * @return All subscriptions of a given API
 * @throws org.wso2.carbon.apimgt.api.APIManagementException
 */
public List<SubscribedAPI> getSubscriptionsOfAPI(String apiName, String apiVersion, String provider) throws APIManagementException {
    Connection connection = null;
    PreparedStatement ps = null;
    ResultSet result = null;
    List<SubscribedAPI> subscriptions = new ArrayList<>();
    try {
        String sqlQuery = SQLConstants.GET_SUBSCRIPTIONS_OF_API_SQL;
        connection = APIMgtDBUtil.getConnection();
        ps = connection.prepareStatement(sqlQuery);
        ps.setString(1, apiName);
        ps.setString(2, apiVersion);
        ps.setString(3, provider);
        result = ps.executeQuery();
        while (result.next()) {
            APIIdentifier apiId = new APIIdentifier(result.getString("API_PROVIDER"), apiName, apiVersion);
            Subscriber subscriber = new Subscriber(result.getString("USER_ID"));
            SubscribedAPI subscription = new SubscribedAPI(subscriber, apiId);
            subscription.setUUID(result.getString("SUB_UUID"));
            subscription.setSubStatus(result.getString("SUB_STATUS"));
            subscription.setSubCreatedStatus(result.getString("SUBS_CREATE_STATE"));
            subscription.setTier(new Tier(result.getString("SUB_TIER_ID")));
            subscription.setCreatedTime(result.getString("SUB_CREATED_TIME"));
            Application application = new Application(result.getInt("APPLICATION_ID"));
            application.setName(result.getString("APPNAME"));
            subscription.setApplication(application);
            subscriptions.add(subscription);
        }
    } catch (SQLException e) {
        handleException("Error occurred while reading subscriptions of API: " + apiName + ':' + apiVersion, e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, connection, result);
    }
    return subscriptions;
}
Also used : Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) Tier(org.wso2.carbon.apimgt.api.model.Tier) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) ArrayList(java.util.ArrayList) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PreparedStatement(java.sql.PreparedStatement) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Application(org.wso2.carbon.apimgt.api.model.Application)

Example 57 with SubscribedAPI

use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.

the class ApiMgtDAO method getAllAPIProductUsageByProvider.

/**
 * @param providerName Name of the provider
 * @return UserApplicationAPIUsage of given provider
 * @throws org.wso2.carbon.apimgt.api.APIManagementException if failed to get
 *                                                           UserApplicationAPIUsage for given provider
 */
public UserApplicationAPIUsage[] getAllAPIProductUsageByProvider(String providerName) throws APIManagementException {
    try (Connection connection = APIMgtDBUtil.getConnection();
        PreparedStatement ps = connection.prepareStatement(SQLConstants.GET_APP_API_USAGE_BY_PROVIDER_SQL)) {
        ps.setString(1, APIUtil.replaceEmailDomainBack(providerName));
        try (ResultSet result = ps.executeQuery()) {
            Map<String, UserApplicationAPIUsage> userApplicationUsages = new TreeMap<String, UserApplicationAPIUsage>();
            while (result.next()) {
                int subId = result.getInt("SUBSCRIPTION_ID");
                String userId = result.getString("USER_ID");
                String application = result.getString("APPNAME");
                int appId = result.getInt("APPLICATION_ID");
                String subStatus = result.getString("SUB_STATUS");
                String subsCreateState = result.getString("SUBS_CREATE_STATE");
                String key = userId + "::" + application;
                UserApplicationAPIUsage usage = userApplicationUsages.get(key);
                if (usage == null) {
                    usage = new UserApplicationAPIUsage();
                    usage.setUserId(userId);
                    usage.setApplicationName(application);
                    usage.setAppId(appId);
                    userApplicationUsages.put(key, usage);
                }
                APIProductIdentifier apiProductId = new APIProductIdentifier(result.getString("API_PROVIDER"), result.getString("API_NAME"), result.getString("API_VERSION"));
                SubscribedAPI apiSubscription = new SubscribedAPI(new Subscriber(userId), apiProductId);
                apiSubscription.setSubStatus(subStatus);
                apiSubscription.setSubCreatedStatus(subsCreateState);
                apiSubscription.setUUID(result.getString("SUB_UUID"));
                apiSubscription.setTier(new Tier(result.getString("SUB_TIER_ID")));
                Application applicationObj = new Application(result.getString("APP_UUID"));
                apiSubscription.setApplication(applicationObj);
                usage.addApiSubscriptions(apiSubscription);
            }
            return userApplicationUsages.values().toArray(new UserApplicationAPIUsage[userApplicationUsages.size()]);
        }
    } catch (SQLException e) {
        handleException("Failed to find API Product Usage for :" + providerName, e);
    }
    return new UserApplicationAPIUsage[] {};
}
Also used : UserApplicationAPIUsage(org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage) Tier(org.wso2.carbon.apimgt.api.model.Tier) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) TreeMap(java.util.TreeMap) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) ResultSet(java.sql.ResultSet) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) Application(org.wso2.carbon.apimgt.api.model.Application)

Example 58 with SubscribedAPI

use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.

the class ApiMgtDAO method getAllAPIUsageByProviderAndApiId.

/**
 * @param uuid API uuid
 * @param organization Organization of the API
 * @return UserApplicationAPIUsage of given provider
 * @throws org.wso2.carbon.apimgt.api.APIManagementException if failed to get
 *                                                           UserApplicationAPIUsage for given provider
 */
public UserApplicationAPIUsage[] getAllAPIUsageByProviderAndApiId(String uuid, String organization) throws APIManagementException {
    Connection connection = null;
    PreparedStatement ps = null;
    ResultSet result = null;
    try {
        String sqlQuery = SQLConstants.GET_APP_API_USAGE_BY_UUID_SQL;
        connection = APIMgtDBUtil.getConnection();
        ps = connection.prepareStatement(sqlQuery);
        ps.setString(1, uuid);
        ps.setString(2, organization);
        result = ps.executeQuery();
        Map<String, UserApplicationAPIUsage> userApplicationUsages = new TreeMap<String, UserApplicationAPIUsage>();
        while (result.next()) {
            int subId = result.getInt("SUBSCRIPTION_ID");
            String userId = result.getString("USER_ID");
            String application = result.getString("APPNAME");
            int appId = result.getInt("APPLICATION_ID");
            String subStatus = result.getString("SUB_STATUS");
            String subsCreateState = result.getString("SUBS_CREATE_STATE");
            String key = userId + "::" + application;
            UserApplicationAPIUsage usage = userApplicationUsages.get(key);
            if (usage == null) {
                usage = new UserApplicationAPIUsage();
                usage.setUserId(userId);
                usage.setApplicationName(application);
                usage.setAppId(appId);
                userApplicationUsages.put(key, usage);
            }
            APIIdentifier apiId = new APIIdentifier(result.getString("API_PROVIDER"), result.getString("API_NAME"), result.getString("API_VERSION"));
            SubscribedAPI apiSubscription = new SubscribedAPI(new Subscriber(userId), apiId);
            apiSubscription.setSubStatus(subStatus);
            apiSubscription.setSubCreatedStatus(subsCreateState);
            apiSubscription.setUUID(result.getString("SUB_UUID"));
            apiSubscription.setTier(new Tier(result.getString("SUB_TIER_ID")));
            Application applicationObj = new Application(result.getString("APP_UUID"));
            apiSubscription.setApplication(applicationObj);
            usage.addApiSubscriptions(apiSubscription);
        }
        return userApplicationUsages.values().toArray(new UserApplicationAPIUsage[userApplicationUsages.size()]);
    } catch (SQLException e) {
        handleException("Failed to find API Usage for API with UUID :" + uuid, e);
        return null;
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, connection, result);
    }
}
Also used : UserApplicationAPIUsage(org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage) Tier(org.wso2.carbon.apimgt.api.model.Tier) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) TreeMap(java.util.TreeMap) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) ResultSet(java.sql.ResultSet) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Application(org.wso2.carbon.apimgt.api.model.Application)

Example 59 with SubscribedAPI

use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.

the class ApiMgtDAO method updateSubscriptionStatusAndTier.

public void updateSubscriptionStatusAndTier(int subscriptionId, String status) throws APIManagementException {
    Connection conn = null;
    PreparedStatement ps = null;
    SubscribedAPI subscribedAPI = getSubscriptionById(subscriptionId);
    try {
        conn = APIMgtDBUtil.getConnection();
        conn.setAutoCommit(false);
        // This query is to update the AM_SUBSCRIPTION table
        String sqlQuery = SQLConstants.UPDATE_SUBSCRIPTION_STATUS_AND_TIER_SQL;
        ps = conn.prepareStatement(sqlQuery);
        ps.setString(1, null);
        if (subscribedAPI.getRequestedTier().getName() == null) {
            ps.setString(2, subscribedAPI.getTier().getName());
        } else {
            ps.setString(2, subscribedAPI.getRequestedTier().getName());
        }
        ps.setString(3, status);
        ps.setInt(4, subscriptionId);
        ps.execute();
        // Commit transaction
        conn.commit();
    } catch (SQLException e) {
        if (conn != null) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                log.error("Failed to rollback subscription status update ", e1);
            }
        }
        handleException("Failed to update subscription status ", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, conn, null);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PreparedStatement(java.sql.PreparedStatement)

Example 60 with SubscribedAPI

use of org.wso2.carbon.apimgt.api.model.SubscribedAPI in project carbon-apimgt by wso2.

the class ApiMgtDAO method getPaginatedSubscribedAPIsByApplication.

public Set<SubscribedAPI> getPaginatedSubscribedAPIsByApplication(Application application, Integer offset, Integer limit, String organization) throws APIManagementException {
    Set<SubscribedAPI> subscribedAPIs = new LinkedHashSet<>();
    try (Connection connection = APIMgtDBUtil.getConnection();
        PreparedStatement ps = connection.prepareStatement(SQLConstants.GET_PAGINATED_SUBSCRIBED_APIS_BY_APP_ID_SQL)) {
        ps.setInt(1, application.getId());
        ps.setString(2, organization);
        try (ResultSet result = ps.executeQuery()) {
            int index = 0;
            while (result.next()) {
                if (index >= offset && index < limit) {
                    String apiType = result.getString("TYPE");
                    if (APIConstants.API_PRODUCT.toString().equals(apiType)) {
                        APIProductIdentifier identifier = new APIProductIdentifier(APIUtil.replaceEmailDomain(result.getString("API_PROVIDER")), result.getString("API_NAME"), result.getString("API_VERSION"));
                        identifier.setUUID(result.getString("API_UUID"));
                        SubscribedAPI subscribedAPI = new SubscribedAPI(application.getSubscriber(), identifier);
                        subscribedAPI.setApplication(application);
                        initSubscribedAPI(subscribedAPI, result);
                        subscribedAPIs.add(subscribedAPI);
                    } else {
                        APIIdentifier identifier = new APIIdentifier(APIUtil.replaceEmailDomain(result.getString("API_PROVIDER")), result.getString("API_NAME"), result.getString("API_VERSION"));
                        identifier.setUuid(result.getString("API_UUID"));
                        SubscribedAPI subscribedAPI = new SubscribedAPI(application.getSubscriber(), identifier);
                        subscribedAPI.setApplication(application);
                        initSubscribedAPI(subscribedAPI, result);
                        subscribedAPIs.add(subscribedAPI);
                    }
                    if (index == limit - 1) {
                        break;
                    }
                }
                index++;
            }
        }
    } catch (SQLException e) {
        handleException("Failed to get SubscribedAPI of application :" + application.getName(), e);
    }
    return subscribedAPIs;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) PreparedStatement(java.sql.PreparedStatement) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier)

Aggregations

SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)54 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)28 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)28 Application (org.wso2.carbon.apimgt.api.model.Application)28 Tier (org.wso2.carbon.apimgt.api.model.Tier)23 Test (org.junit.Test)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)18 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)16 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)14 ArrayList (java.util.ArrayList)12 ApiTypeWrapper (org.wso2.carbon.apimgt.api.model.ApiTypeWrapper)12 HashSet (java.util.HashSet)11 TreeMap (java.util.TreeMap)11 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)11 Connection (java.sql.Connection)10 PreparedStatement (java.sql.PreparedStatement)10 SQLException (java.sql.SQLException)10 ResultSet (java.sql.ResultSet)9 JSONObject (net.minidev.json.JSONObject)9 API (org.wso2.carbon.apimgt.api.model.API)9