use of org.wso2.carbon.apimgt.api.model.Subscriber in project carbon-apimgt by wso2.
the class ApiMgtDAO method getUserRatingInfo.
/**
* @param uuid API uuid
* @param userId User Id
* @param conn Database connection
* @throws APIManagementException if failed to get user API Ratings
*/
private JSONObject getUserRatingInfo(String uuid, String userId, Connection conn) throws APIManagementException, SQLException {
PreparedStatement ps = null;
ResultSet rs = null;
JSONObject ratingObj = new JSONObject();
int userRating = 0;
int id = -1;
String ratingId = null;
try {
int tenantId;
tenantId = APIUtil.getTenantId(userId);
// Get subscriber Id
Subscriber subscriber = getSubscriber(userId, tenantId, conn);
if (subscriber == null) {
String msg = "Could not load Subscriber records for: " + userId;
log.error(msg);
throw new APIManagementException(msg);
}
// Get API Id
id = getAPIID(uuid, conn);
String sqlQuery = SQLConstants.GET_API_RATING_INFO_SQL;
if (id == -1) {
String msg = "Could not load API record for API with UUID: " + uuid;
log.error(msg);
throw new APIManagementException(msg);
}
// This query to get rating information from the AM_API_RATINGS table
ps = conn.prepareStatement(sqlQuery);
ps.setInt(1, subscriber.getId());
ps.setInt(2, id);
rs = ps.executeQuery();
while (rs.next()) {
ratingId = rs.getString("RATING_ID");
userRating = rs.getInt("RATING");
}
if (ratingId != null) {
// A rating record exists
ratingObj.put(APIConstants.RATING_ID, ratingId);
ratingObj.put(APIConstants.USER_NAME, userId);
ratingObj.put(APIConstants.RATING, userRating);
}
} catch (SQLException e) {
handleException("Failed to retrieve API ratings ", e);
} finally {
APIMgtDBUtil.closeAllConnections(ps, null, rs);
}
return ratingObj;
}
use of org.wso2.carbon.apimgt.api.model.Subscriber in project carbon-apimgt by wso2.
the class ApiMgtDAO method initSubscribedAPIDetailed.
private void initSubscribedAPIDetailed(Connection connection, SubscribedAPI subscribedAPI, Subscriber subscriber, ResultSet result) throws SQLException, APIManagementException {
subscribedAPI.setSubscriptionId(result.getInt("SUBS_ID"));
subscribedAPI.setSubStatus(result.getString("SUB_STATUS"));
subscribedAPI.setSubCreatedStatus(result.getString("SUBS_CREATE_STATE"));
String tierName = result.getString(APIConstants.SUBSCRIPTION_FIELD_TIER_ID);
String requestedTierName = result.getString(APIConstants.SUBSCRIPTION_FIELD_TIER_ID_PENDING);
subscribedAPI.setTier(new Tier(tierName));
subscribedAPI.setRequestedTier(new Tier(requestedTierName));
subscribedAPI.setUUID(result.getString("SUB_UUID"));
// setting NULL for subscriber. If needed, Subscriber object should be constructed &
// passed in
int applicationId = result.getInt("APP_ID");
Application application = new Application(result.getString("APP_NAME"), subscriber);
application.setId(result.getInt("APP_ID"));
application.setTokenType(result.getString("APP_TOKEN_TYPE"));
application.setCallbackUrl(result.getString("CALLBACK_URL"));
application.setUUID(result.getString("APP_UUID"));
if (multiGroupAppSharingEnabled) {
application.setGroupId(getGroupId(connection, application.getId()));
application.setOwner(result.getString("OWNER"));
}
subscribedAPI.setApplication(application);
}
use of org.wso2.carbon.apimgt.api.model.Subscriber in project carbon-apimgt by wso2.
the class ApiMgtDAO method populateAppRegistrationWorkflowDTO.
public void populateAppRegistrationWorkflowDTO(ApplicationRegistrationWorkflowDTO workflowDTO) throws APIManagementException {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Application application = null;
Subscriber subscriber = null;
String registrationEntry = SQLConstants.GET_APPLICATION_REGISTRATION_ENTRY_BY_SUBSCRIBER_SQL;
try {
conn = APIMgtDBUtil.getConnection();
ps = conn.prepareStatement(registrationEntry);
ps.setString(1, workflowDTO.getExternalWorkflowReference());
rs = ps.executeQuery();
while (rs.next()) {
subscriber = new Subscriber(rs.getString("USER_ID"));
subscriber.setId(rs.getInt("SUBSCRIBER_ID"));
application = new Application(rs.getString("NAME"), subscriber);
application.setId(rs.getInt("APPLICATION_ID"));
application.setUUID(rs.getString("UUID"));
application.setTokenType(rs.getString("APP_TYPE"));
application.setApplicationWorkFlowStatus(rs.getString("APPLICATION_STATUS"));
application.setCallbackUrl(rs.getString("CALLBACK_URL"));
application.setDescription(rs.getString("DESCRIPTION"));
application.setTier(rs.getString("APPLICATION_TIER"));
workflowDTO.setApplication(application);
workflowDTO.setKeyType(rs.getString("TOKEN_TYPE"));
workflowDTO.setUserName(subscriber.getName());
workflowDTO.setDomainList(rs.getString("ALLOWED_DOMAINS"));
workflowDTO.setValidityTime(rs.getLong("VALIDITY_PERIOD"));
String tenantDomain = MultitenantUtils.getTenantDomain(subscriber.getName());
String keyManagerUUID = rs.getString("KEY_MANAGER");
workflowDTO.setKeyManager(keyManagerUUID);
KeyManagerConfigurationDTO keyManagerConfigurationByUUID = getKeyManagerConfigurationByUUID(conn, keyManagerUUID);
if (keyManagerConfigurationByUUID != null) {
OAuthAppRequest request = ApplicationUtils.createOauthAppRequest(application.getName(), null, application.getCallbackUrl(), rs.getString("TOKEN_SCOPE"), rs.getString("INPUTS"), application.getTokenType(), keyManagerConfigurationByUUID.getOrganization(), keyManagerConfigurationByUUID.getName());
request.setMappingId(workflowDTO.getWorkflowReference());
request.getOAuthApplicationInfo().setApplicationUUID(application.getUUID());
workflowDTO.setAppInfoDTO(request);
} else {
throw new APIManagementException("Error occured while finding the KeyManager from uuid " + keyManagerUUID + ".", ExceptionCodes.KEY_MANAGER_NOT_REGISTERED);
}
}
} catch (SQLException | IOException e) {
handleException("Error occurred while retrieving an " + "Application Registration Entry for Workflow : " + workflowDTO.getExternalWorkflowReference(), e);
} finally {
APIMgtDBUtil.closeAllConnections(ps, conn, rs);
}
}
use of org.wso2.carbon.apimgt.api.model.Subscriber in project carbon-apimgt by wso2.
the class ApiMgtDAO method getAllAPIUsageByProvider.
/**
* @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[] getAllAPIUsageByProvider(String providerName) throws APIManagementException {
Connection connection = null;
PreparedStatement ps = null;
ResultSet result = null;
try {
String sqlQuery = SQLConstants.GET_APP_API_USAGE_BY_PROVIDER_SQL;
connection = APIMgtDBUtil.getConnection();
ps = connection.prepareStatement(sqlQuery);
ps.setString(1, APIUtil.replaceEmailDomainBack(providerName));
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 :" + providerName, e);
return null;
} finally {
APIMgtDBUtil.closeAllConnections(ps, connection, result);
}
}
use of org.wso2.carbon.apimgt.api.model.Subscriber in project carbon-apimgt by wso2.
the class ApiMgtDAO method getApplicationByUUID.
/**
* Retrieves the Application which is corresponding to the given UUID String
*
* @param uuid UUID of Application
* @return
* @throws APIManagementException
*/
public Application getApplicationByUUID(String uuid) throws APIManagementException {
Connection connection = null;
PreparedStatement prepStmt = null;
ResultSet rs = null;
int applicationId = 0;
Application application = null;
try {
connection = APIMgtDBUtil.getConnection();
String query = SQLConstants.GET_APPLICATION_BY_UUID_SQL;
prepStmt = connection.prepareStatement(query);
prepStmt.setString(1, uuid);
rs = prepStmt.executeQuery();
if (rs.next()) {
String applicationName = rs.getString("NAME");
String subscriberId = rs.getString("SUBSCRIBER_ID");
String subscriberName = rs.getString("USER_ID");
Subscriber subscriber = new Subscriber(subscriberName);
subscriber.setId(Integer.parseInt(subscriberId));
application = new Application(applicationName, subscriber);
application.setDescription(rs.getString("DESCRIPTION"));
application.setStatus(rs.getString("APPLICATION_STATUS"));
application.setCallbackUrl(rs.getString("CALLBACK_URL"));
applicationId = rs.getInt("APPLICATION_ID");
application.setId(applicationId);
application.setGroupId(rs.getString("GROUP_ID"));
application.setUUID(rs.getString("UUID"));
application.setTier(rs.getString("APPLICATION_TIER"));
application.setTokenType(rs.getString("TOKEN_TYPE"));
application.setOwner(rs.getString("CREATED_BY"));
application.setOrganization(rs.getString("ORGANIZATION"));
subscriber.setId(rs.getInt("SUBSCRIBER_ID"));
if (multiGroupAppSharingEnabled) {
if (application.getGroupId() == null || application.getGroupId().isEmpty()) {
application.setGroupId(getGroupId(connection, application.getId()));
}
}
Timestamp createdTime = rs.getTimestamp("CREATED_TIME");
application.setCreatedTime(createdTime == null ? null : String.valueOf(createdTime.getTime()));
try {
Timestamp updated_time = rs.getTimestamp("UPDATED_TIME");
application.setLastUpdatedTime(updated_time == null ? null : String.valueOf(updated_time.getTime()));
} catch (SQLException e) {
// fixing Timestamp issue with default value '0000-00-00 00:00:00'for existing applications created
application.setLastUpdatedTime(application.getCreatedTime());
}
}
// Get custom attributes of application
if (application != null) {
Map<String, String> applicationAttributes = getApplicationAttributes(connection, applicationId);
application.setApplicationAttributes(applicationAttributes);
}
} catch (SQLException e) {
handleException("Error while obtaining details of the Application : " + uuid, e);
} finally {
APIMgtDBUtil.closeAllConnections(prepStmt, connection, rs);
}
return application;
}
Aggregations