Search in sources :

Example 6 with UserApplicationAPIUsage

use of org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage 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 7 with UserApplicationAPIUsage

use of org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage in project carbon-apimgt by wso2.

the class APIProviderImpl method getAPIUsageByAPIId.

/**
 * Returns usage details of a particular API
 *
 * @param uuid API uuid
 * @param organization identifier of the organization
 * @return UserApplicationAPIUsages for given provider
 * @throws org.wso2.carbon.apimgt.api.APIManagementException If failed to get UserApplicationAPIUsage
 */
@Override
public List<SubscribedAPI> getAPIUsageByAPIId(String uuid, String organization) throws APIManagementException {
    APIIdentifier identifier = apiMgtDAO.getAPIIdentifierFromUUID(uuid);
    List<SubscribedAPI> subscribedAPIs = new ArrayList<>();
    if (identifier != null) {
        APIIdentifier apiIdEmailReplaced = new APIIdentifier(APIUtil.replaceEmailDomain(identifier.getProviderName()), identifier.getApiName(), identifier.getVersion());
        UserApplicationAPIUsage[] allApiResult = apiMgtDAO.getAllAPIUsageByProviderAndApiId(uuid, organization);
        for (UserApplicationAPIUsage usage : allApiResult) {
            for (SubscribedAPI apiSubscription : usage.getApiSubscriptions()) {
                APIIdentifier subsApiId = apiSubscription.getApiId();
                APIIdentifier subsApiIdEmailReplaced = new APIIdentifier(APIUtil.replaceEmailDomain(subsApiId.getProviderName()), subsApiId.getApiName(), subsApiId.getVersion());
                if (subsApiIdEmailReplaced.equals(apiIdEmailReplaced)) {
                    subscribedAPIs.add(apiSubscription);
                }
            }
        }
    }
    return subscribedAPIs;
}
Also used : UserApplicationAPIUsage(org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage) ArrayList(java.util.ArrayList) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier)

Aggregations

UserApplicationAPIUsage (org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage)7 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)7 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)4 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)4 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 TreeMap (java.util.TreeMap)3 Application (org.wso2.carbon.apimgt.api.model.Application)3 Tier (org.wso2.carbon.apimgt.api.model.Tier)3 ArrayList (java.util.ArrayList)2 APIProductIdentifier (org.wso2.carbon.apimgt.api.model.APIProductIdentifier)2 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)1 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)1 APIRevision (org.wso2.carbon.apimgt.api.model.APIRevision)1 SubscriptionListDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.SubscriptionListDTO)1