Skip to content

Commit

Permalink
Implement pagination for get discovery attributes of organizations
Browse files Browse the repository at this point in the history
  • Loading branch information
dewniMW committed Oct 27, 2023
1 parent 6933a5d commit 950b346
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

package org.wso2.carbon.identity.organization.discovery.service;

import org.wso2.carbon.identity.organization.discovery.service.model.DiscoveryOrganizationsResult;
import org.wso2.carbon.identity.organization.discovery.service.model.OrgDiscoveryAttribute;
import org.wso2.carbon.identity.organization.discovery.service.model.OrganizationDiscovery;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException;

import java.util.List;
Expand Down Expand Up @@ -106,12 +106,14 @@ boolean isDiscoveryAttributeValueAvailable(String organizationId, String type, S
/**
* List the discovery attributes of all the organizations under the root organization.
*
* @param limit The maximum number of records to be returned.
* @param offset The number of records to skip for pagination.
* @param filter The filter to be applied.
* @return The discovery attributes of the organizations.
* @throws OrganizationManagementException The exception thrown when listing discovery attributes of the
* organizations.
*/
List<OrganizationDiscovery> getOrganizationsDiscoveryAttributes(String filter)
DiscoveryOrganizationsResult getOrganizationsDiscoveryAttributes(Integer limit, Integer offset, String filter)
throws OrganizationManagementException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.wso2.carbon.identity.core.util.IdentityUtil;
import org.wso2.carbon.identity.organization.discovery.service.dao.OrganizationDiscoveryDAO;
import org.wso2.carbon.identity.organization.discovery.service.dao.OrganizationDiscoveryDAOImpl;
import org.wso2.carbon.identity.organization.discovery.service.internal.OrganizationDiscoveryServiceHolder;
import org.wso2.carbon.identity.organization.discovery.service.model.DiscoveryOrganizationsResult;
import org.wso2.carbon.identity.organization.discovery.service.model.OrgDiscoveryAttribute;
import org.wso2.carbon.identity.organization.discovery.service.model.OrganizationDiscovery;
import org.wso2.carbon.identity.organization.management.service.OrganizationManager;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementClientException;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementException;
Expand Down Expand Up @@ -52,6 +53,8 @@
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_EMPTY_DISCOVERY_ATTRIBUTES;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_INVALID_DISCOVERY_ATTRIBUTE_VALUE;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_INVALID_FILTER_FORMAT;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_INVALID_LIMIT;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_INVALID_OFFSET;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_INVALID_ORGANIZATION;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_UNAUTHORIZED_ORG_FOR_DISCOVERY_ATTRIBUTE_MANAGEMENT;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_UNSUPPORTED_COMPLEX_QUERY_IN_FILTER;
Expand Down Expand Up @@ -147,11 +150,15 @@ public boolean isDiscoveryAttributeValueAvailable(String organizationId, String
}

@Override
public List<OrganizationDiscovery> getOrganizationsDiscoveryAttributes(String filter)
public DiscoveryOrganizationsResult getOrganizationsDiscoveryAttributes(Integer limit, Integer offset,
String filter)
throws OrganizationManagementException {

limit = validateLimit(limit);
offset = validateOffset(offset);
List<ExpressionNode> expressionNodes = getExpressionNodes(filter);
return organizationDiscoveryDAO.getOrganizationsDiscoveryAttributes(getOrganizationId(), expressionNodes);
return organizationDiscoveryDAO.getOrganizationsDiscoveryAttributes(limit,
offset, getOrganizationId(), expressionNodes);
}

@Override
Expand Down Expand Up @@ -277,4 +284,44 @@ private boolean isFilteringAttributeSupported(String attributeValue) {

return ORGANIZATION_NAME.equalsIgnoreCase(attributeValue);
}

/**
* Validate limit.
*
* @param limit The given limit value.
* @return Validated limit.
* @throws OrganizationManagementClientException Exception thrown for invalid limit.
*/
private int validateLimit(Integer limit) throws OrganizationManagementClientException {

if (limit == null) {
limit = IdentityUtil.getDefaultItemsPerPage();
}
if (limit < 0) {
throw handleClientException(ERROR_CODE_INVALID_LIMIT);
}
int maximumItemsPerPage = IdentityUtil.getMaximumItemPerPage();
if (limit > maximumItemsPerPage) {
limit = maximumItemsPerPage;
}
return limit;
}

/**
* Validate offset.
*
* @param offset The given offset value.
* @return Validated offset value.
* @throws OrganizationManagementClientException Exception thrown for invalid offset.
*/
private int validateOffset(Integer offset) throws OrganizationManagementClientException {

if (offset == null) {
offset = 0;
}
if (offset < 0) {
throw handleClientException(ERROR_CODE_INVALID_OFFSET);
}
return offset;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,22 @@ public class SQLConstants {
public static final String DELETE_ORGANIZATION_DISCOVERY_ATTRIBUTES = "DELETE FROM UM_ORG_DISCOVERY WHERE " +
"UM_ORG_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ID + ";";

public static final String GET_ORGANIZATIONS_DISCOVERY_ATTRIBUTES = "SELECT UM_ORG_ID, UM_DISCOVERY_TYPE, " +
"UM_DISCOVERY_VALUE, UM_ORG_NAME FROM UM_ORG_DISCOVERY JOIN UM_ORG ON UM_ORG.UM_ID = UM_ORG_ID WHERE ";
public static final String GET_DISCOVERY_ORGANIZATION_IDS = "SELECT DISTINCT UM_ORG_ID FROM UM_ORG_DISCOVERY " +
"JOIN UM_ORG ON UM_ORG.UM_ID = UM_ORG_ID WHERE %s UM_ROOT_ORG_ID = :" +
SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROOT_ID + "; ORDER BY UM_ORG_ID LIMIT :" + SQLPlaceholders.DB_LIMIT +
"; OFFSET :" + SQLPlaceholders.DB_OFFSET + ";";

public static final String GET_ORGANIZATIONS_DISCOVERY_ATTRIBUTES_TAIL = "UM_ROOT_ORG_ID = :" +
public static final String GET_DISCOVERY_ORGANIZATION_IDS_MSSQL = "SELECT DISTINCT UM_ORG_ID FROM " +
"UM_ORG_DISCOVERY JOIN UM_ORG ON UM_ORG.UM_ID = UM_ORG_ID WHERE %s UM_ROOT_ORG_ID = :" +
SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROOT_ID + "; ORDER BY UM_ORG_ID OFFSET :" +
SQLPlaceholders.DB_OFFSET + "; ROWS FETCH NEXT :" + SQLPlaceholders.DB_LIMIT + "; ROWS ONLY";

public static final String GET_DISCOVERY_ORGANIZATIONS_ATTRIBUTES = "SELECT UM_ORG_ID, UM_DISCOVERY_TYPE, " +
"UM_DISCOVERY_VALUE, UM_ORG_NAME FROM UM_ORG_DISCOVERY JOIN UM_ORG ON UM_ORG.UM_ID = UM_ORG_ID WHERE " +
"UM_ORG_ID IN (" + SQLPlaceholders.ORGS_LIST_PLACEHOLDER + ")";

public static final String DISCOVERY_ORGANIZATIONS_TOTAL_COUNT = "SELECT COUNT(DISTINCT UM_ORG_ID) FROM " +
"UM_ORG_DISCOVERY JOIN UM_ORG ON UM_ORG.UM_ID = UM_ORG_ID WHERE %s UM_ROOT_ORG_ID = :" +
SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROOT_ID + ";";

public static final String GET_ORGANIZATION_ID_BY_DISCOVERY_ATTRIBUTE = "SELECT UM_ORG_ID FROM UM_ORG_DISCOVERY " +
Expand All @@ -68,5 +80,8 @@ public static final class SQLPlaceholders {
public static final String DB_SCHEMA_COLUMN_NAME_TYPE = "TYPE";
public static final String DB_SCHEMA_COLUMN_NAME_VALUE = "VALUE";
public static final String DB_SCHEMA_COLUMN_NAME_ROOT_ID = "ROOT_ID";
public static final String DB_LIMIT = "LIMIT";
public static final String DB_OFFSET = "OFFSET";
public static final String ORGS_LIST_PLACEHOLDER = "_ORGS_LIST_";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

package org.wso2.carbon.identity.organization.discovery.service.dao;

import org.wso2.carbon.identity.organization.discovery.service.model.DiscoveryOrganizationsResult;
import org.wso2.carbon.identity.organization.discovery.service.model.OrgDiscoveryAttribute;
import org.wso2.carbon.identity.organization.discovery.service.model.OrganizationDiscovery;
import org.wso2.carbon.identity.organization.management.service.exception.OrganizationManagementServerException;
import org.wso2.carbon.identity.organization.management.service.filter.ExpressionNode;

Expand Down Expand Up @@ -102,14 +102,17 @@ void updateOrganizationDiscoveryAttributes(String organizationId, List<OrgDiscov
/**
* List the discovery attributes of all the organizations under the given root organization.
*
* @param limit The maximum number of records to be returned.
* @param offset The number of records to skip for pagination.
* @param rootOrganizationId The root organization ID.
* @param expressionNodes The list of filters.
* @return The discovery attributes of the organizations.
* @throws OrganizationManagementServerException The server exception thrown when listing discovery attributes of
* the organizations.
*/
List<OrganizationDiscovery> getOrganizationsDiscoveryAttributes(String rootOrganizationId, List<ExpressionNode>
expressionNodes) throws OrganizationManagementServerException;
DiscoveryOrganizationsResult getOrganizationsDiscoveryAttributes(int limit, int offset, String rootOrganizationId,
List<ExpressionNode> expressionNodes)
throws OrganizationManagementServerException;

/**
* Get the organization ID by discovery attribute in the hierarchy.
Expand Down
Loading

0 comments on commit 950b346

Please sign in to comment.