From f44a3b986b64aca62c39299eb950d32f9a72728e Mon Sep 17 00:00:00 2001 From: dewniMW Date: Sun, 22 Oct 2023 19:07:22 +0530 Subject: [PATCH 1/2] Add filter by org name for get orgs discovery attributes --- .../pom.xml | 3 + .../service/OrganizationDiscoveryManager.java | 4 +- .../OrganizationDiscoveryManagerImpl.java | 70 ++++++++++- .../service/constant/DiscoveryConstants.java | 27 +++++ .../service/constant/SQLConstants.java | 6 +- .../service/dao/OrganizationDiscoveryDAO.java | 6 +- .../dao/OrganizationDiscoveryDAOImpl.java | 109 +++++++++++++++++- 7 files changed, 214 insertions(+), 11 deletions(-) diff --git a/components/org.wso2.carbon.identity.organization.discovery.service/pom.xml b/components/org.wso2.carbon.identity.organization.discovery.service/pom.xml index ce441220c..67f392fcb 100644 --- a/components/org.wso2.carbon.identity.organization.discovery.service/pom.xml +++ b/components/org.wso2.carbon.identity.organization.discovery.service/pom.xml @@ -79,11 +79,14 @@ org.wso2.carbon.identity.organization.config.service.constant;version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}", org.wso2.carbon.identity.organization.config.service.exception;version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}", org.wso2.carbon.identity.organization.config.service.model;version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}", + org.wso2.carbon.identity.organization.discovery.service.constant;version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}", org.wso2.carbon.identity.organization.discovery.service.dao;version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}", org.wso2.carbon.identity.organization.discovery.service.model;version="${org.wso2.identity.organization.mgt.imp.pkg.version.range}", org.wso2.carbon.identity.organization.management.service;version="${org.wso2.identity.organization.mgt.core.imp.pkg.version.range}", org.wso2.carbon.identity.organization.management.service.constant;version="${org.wso2.identity.organization.mgt.core.imp.pkg.version.range}", org.wso2.carbon.identity.organization.management.service.exception;version="${org.wso2.identity.organization.mgt.core.imp.pkg.version.range}", + org.wso2.carbon.identity.organization.management.service.filter;version="${org.wso2.identity.organization.mgt.core.imp.pkg.version.range}", + org.wso2.carbon.identity.organization.management.service.model;version="${org.wso2.identity.organization.mgt.core.imp.pkg.version.range}", org.wso2.carbon.identity.organization.management.service.util;version="${org.wso2.identity.organization.mgt.core.imp.pkg.version.range}", org.wso2.carbon.identity.core;version="${carbon.identity.package.import.version.range}", org.wso2.carbon.identity.core.util;version="${carbon.identity.package.import.version.range}", diff --git a/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/OrganizationDiscoveryManager.java b/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/OrganizationDiscoveryManager.java index e0b212095..d03dfcfac 100644 --- a/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/OrganizationDiscoveryManager.java +++ b/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/OrganizationDiscoveryManager.java @@ -106,11 +106,13 @@ boolean isDiscoveryAttributeValueAvailable(String organizationId, String type, S /** * List the discovery attributes of all the organizations under the root organization. * + * @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 getOrganizationsDiscoveryAttributes() throws OrganizationManagementException; + List getOrganizationsDiscoveryAttributes(String filter) + throws OrganizationManagementException; /** * Get attribute based organization discovery handlers. diff --git a/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/OrganizationDiscoveryManagerImpl.java b/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/OrganizationDiscoveryManagerImpl.java index b6a3c7879..3c84ca419 100644 --- a/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/OrganizationDiscoveryManagerImpl.java +++ b/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/OrganizationDiscoveryManagerImpl.java @@ -28,7 +28,13 @@ 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; +import org.wso2.carbon.identity.organization.management.service.filter.ExpressionNode; +import org.wso2.carbon.identity.organization.management.service.filter.FilterTreeBuilder; +import org.wso2.carbon.identity.organization.management.service.filter.Node; +import org.wso2.carbon.identity.organization.management.service.filter.OperationNode; +import java.io.IOException; +import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; @@ -36,15 +42,22 @@ import java.util.Set; import java.util.stream.Collectors; +import static org.wso2.carbon.identity.organization.discovery.service.constant.DiscoveryConstants.ORGANIZATION_NAME; +import static org.wso2.carbon.identity.organization.discovery.service.constant.DiscoveryConstants.SUPPORTED_OPERATIONS; +import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.AND; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_DISCOVERY_ATTRIBUTE_ALREADY_ADDED_FOR_ORGANIZATION; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_DISCOVERY_ATTRIBUTE_TAKEN; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_DISCOVERY_CONFIG_DISABLED; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_DUPLICATE_DISCOVERY_ATTRIBUTE_TYPES; 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_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; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_UNSUPPORTED_DISCOVERY_ATTRIBUTE; +import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_UNSUPPORTED_FILTER_ATTRIBUTE; +import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_UNSUPPORTED_FILTER_OPERATION_FOR_ATTRIBUTE; import static org.wso2.carbon.identity.organization.management.service.util.Utils.getOrganizationId; import static org.wso2.carbon.identity.organization.management.service.util.Utils.handleClientException; @@ -134,9 +147,11 @@ public boolean isDiscoveryAttributeValueAvailable(String organizationId, String } @Override - public List getOrganizationsDiscoveryAttributes() throws OrganizationManagementException { + public List getOrganizationsDiscoveryAttributes(String filter) + throws OrganizationManagementException { - return organizationDiscoveryDAO.getOrganizationsDiscoveryAttributes(getOrganizationId()); + List expressionNodes = getExpressionNodes(filter); + return organizationDiscoveryDAO.getOrganizationsDiscoveryAttributes(getOrganizationId(), expressionNodes); } @Override @@ -211,4 +226,55 @@ private void validateOrganizationDiscoveryAttributes(boolean excludeCurrentOrgan } } } + + private List getExpressionNodes(String filter) throws OrganizationManagementClientException { + + List expressionNodes = new ArrayList<>(); + if (StringUtils.isBlank(filter)) { + filter = StringUtils.EMPTY; + } + try { + if (StringUtils.isNotBlank(filter)) { + FilterTreeBuilder filterTreeBuilder = new FilterTreeBuilder(filter); + Node rootNode = filterTreeBuilder.buildTree(); + setExpressionNodeList(rootNode, expressionNodes); + } + } catch (IOException e) { + throw handleClientException(ERROR_CODE_INVALID_FILTER_FORMAT); + } + return expressionNodes; + } + + private void setExpressionNodeList(Node node, List expression) throws + OrganizationManagementClientException { + + if (node instanceof ExpressionNode) { + ExpressionNode expressionNode = (ExpressionNode) node; + String attributeValue = expressionNode.getAttributeValue(); + String operation = expressionNode.getOperation(); + + if (StringUtils.isNotBlank(attributeValue)) { + if (isFilteringAttributeNotSupported(attributeValue)) { + throw handleClientException(ERROR_CODE_UNSUPPORTED_FILTER_ATTRIBUTE, attributeValue); + } + if (!SUPPORTED_OPERATIONS.contains(operation)) { + throw handleClientException(ERROR_CODE_UNSUPPORTED_FILTER_OPERATION_FOR_ATTRIBUTE, + operation, attributeValue); + } + expression.add(expressionNode); + } + } else if (node instanceof OperationNode) { + String operation = ((OperationNode) node).getOperation(); + if (!StringUtils.equalsIgnoreCase(AND, operation)) { + throw handleClientException(ERROR_CODE_UNSUPPORTED_COMPLEX_QUERY_IN_FILTER); + } + setExpressionNodeList(node.getLeftNode(), expression); + setExpressionNodeList(node.getRightNode(), expression); + } + } + + private boolean isFilteringAttributeNotSupported(String attributeValue) { + + return !attributeValue.equalsIgnoreCase(ORGANIZATION_NAME); + } } diff --git a/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/constant/DiscoveryConstants.java b/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/constant/DiscoveryConstants.java index 23ec99ca9..489f2921d 100644 --- a/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/constant/DiscoveryConstants.java +++ b/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/constant/DiscoveryConstants.java @@ -18,6 +18,19 @@ package org.wso2.carbon.identity.organization.discovery.service.constant; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.CO; +import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.EQ; +import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.EW; +import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.SW; +import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.VIEW_NAME_COLUMN; + /** * This class holds the constants related to organization discovery service. */ @@ -25,4 +38,18 @@ public class DiscoveryConstants { public static final String ENABLE_CONFIG = ".enable"; public static final String PRE_ADD_USER_EMAIL_DOMAIN_VALIDATE = "PRE_ADD_USER_EMAIL_DOMAIN_VALIDATE"; + public static final String ORGANIZATION_NAME = "organizationName"; + public static final Set SUPPORTED_OPERATIONS; + public static final Map ATTRIBUTE_COLUMN_MAP; + + static { + Set operations = new HashSet<>(Arrays.asList(SW, EW, CO, EQ)); + SUPPORTED_OPERATIONS = Collections.unmodifiableSet(operations); + } + + static { + Map attributeMap = new HashMap<>(); + attributeMap.put(ORGANIZATION_NAME, VIEW_NAME_COLUMN); + ATTRIBUTE_COLUMN_MAP = Collections.unmodifiableMap(attributeMap); + } } diff --git a/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/constant/SQLConstants.java b/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/constant/SQLConstants.java index cb2bd5f66..d90bb5f7a 100644 --- a/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/constant/SQLConstants.java +++ b/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/constant/SQLConstants.java @@ -49,8 +49,10 @@ public class SQLConstants { "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 UM_ROOT_ORG_ID = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROOT_ID + ";"; + "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_ORGANIZATIONS_DISCOVERY_ATTRIBUTES_TAIL = "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 " + "WHERE UM_DISCOVERY_TYPE = :" + SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TYPE + "; AND UM_DISCOVERY_VALUE = :" diff --git a/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/dao/OrganizationDiscoveryDAO.java b/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/dao/OrganizationDiscoveryDAO.java index 3cd117e35..368d13f94 100644 --- a/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/dao/OrganizationDiscoveryDAO.java +++ b/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/dao/OrganizationDiscoveryDAO.java @@ -21,6 +21,7 @@ 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; import java.util.List; @@ -102,12 +103,13 @@ void updateOrganizationDiscoveryAttributes(String organizationId, List getOrganizationsDiscoveryAttributes(String rootOrganizationId) throws - OrganizationManagementServerException; + List getOrganizationsDiscoveryAttributes(String rootOrganizationId, List + expressionNodes) throws OrganizationManagementServerException; /** * Get the organization ID by discovery attribute in the hierarchy. diff --git a/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/dao/OrganizationDiscoveryDAOImpl.java b/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/dao/OrganizationDiscoveryDAOImpl.java index 51367c689..0772c73af 100644 --- a/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/dao/OrganizationDiscoveryDAOImpl.java +++ b/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/dao/OrganizationDiscoveryDAOImpl.java @@ -18,6 +18,7 @@ package org.wso2.carbon.identity.organization.discovery.service.dao; +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.StringUtils; import org.wso2.carbon.database.utils.jdbc.NamedJdbcTemplate; import org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException; @@ -25,6 +26,8 @@ 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; +import org.wso2.carbon.identity.organization.management.service.model.FilterQueryBuilder; import org.wso2.carbon.identity.organization.management.service.util.Utils; import java.util.ArrayList; @@ -33,12 +36,14 @@ import java.util.List; import java.util.Map; +import static org.wso2.carbon.identity.organization.discovery.service.constant.DiscoveryConstants.ATTRIBUTE_COLUMN_MAP; import static org.wso2.carbon.identity.organization.discovery.service.constant.SQLConstants.CHECK_DISCOVERY_ATTRIBUTE_ADDED_IN_ORGANIZATION; import static org.wso2.carbon.identity.organization.discovery.service.constant.SQLConstants.CHECK_DISCOVERY_ATTRIBUTE_EXIST_IN_HIERARCHY; import static org.wso2.carbon.identity.organization.discovery.service.constant.SQLConstants.DELETE_ORGANIZATION_DISCOVERY_ATTRIBUTES; import static org.wso2.carbon.identity.organization.discovery.service.constant.SQLConstants.DISCOVERY_ATTRIBUTE_VALUE_LIST_PLACEHOLDER; import static org.wso2.carbon.identity.organization.discovery.service.constant.SQLConstants.EXCLUDE_CURRENT_ORGANIZATION_FROM_CHECK_DISCOVERY_ATTRIBUTE_EXIST; import static org.wso2.carbon.identity.organization.discovery.service.constant.SQLConstants.GET_ORGANIZATIONS_DISCOVERY_ATTRIBUTES; +import static org.wso2.carbon.identity.organization.discovery.service.constant.SQLConstants.GET_ORGANIZATIONS_DISCOVERY_ATTRIBUTES_TAIL; import static org.wso2.carbon.identity.organization.discovery.service.constant.SQLConstants.GET_ORGANIZATION_DISCOVERY_ATTRIBUTES; import static org.wso2.carbon.identity.organization.discovery.service.constant.SQLConstants.GET_ORGANIZATION_ID_BY_DISCOVERY_ATTRIBUTE; import static org.wso2.carbon.identity.organization.discovery.service.constant.SQLConstants.INSERT_ORGANIZATION_DISCOVERY_ATTRIBUTES; @@ -46,6 +51,9 @@ import static org.wso2.carbon.identity.organization.discovery.service.constant.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_ROOT_ID; import static org.wso2.carbon.identity.organization.discovery.service.constant.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_TYPE; import static org.wso2.carbon.identity.organization.discovery.service.constant.SQLConstants.SQLPlaceholders.DB_SCHEMA_COLUMN_NAME_VALUE; +import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.CO; +import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.EQ; +import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.EW; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_ADDING_ORGANIZATION_DISCOVERY_ATTRIBUTE; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_CHECKING_DISCOVERY_ATTRIBUTE_ADDED_IN_ORGANIZATION; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_CHECKING_ORGANIZATION_DISCOVERY_ATTRIBUTE_EXIST_IN_HIERARCHY; @@ -54,6 +62,8 @@ import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_LISTING_ORGANIZATIONS_DISCOVERY_ATTRIBUTES; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_RETRIEVING_ORGANIZATION_DISCOVERY_ATTRIBUTE; import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.ErrorMessages.ERROR_CODE_ERROR_UPDATING_ORGANIZATION_DISCOVERY_ATTRIBUTE; +import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.FILTER_PLACEHOLDER_PREFIX; +import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.SW; import static org.wso2.carbon.identity.organization.management.service.util.Utils.getOrganizationId; import static org.wso2.carbon.identity.organization.management.service.util.Utils.handleServerException; @@ -227,13 +237,20 @@ public void updateOrganizationDiscoveryAttributes(String organizationId, } @Override - public List getOrganizationsDiscoveryAttributes(String rootOrganizationId) + public List getOrganizationsDiscoveryAttributes(String rootOrganizationId, + List expressionNodes) throws OrganizationManagementServerException { + FilterQueryBuilder filterQueryBuilder = new FilterQueryBuilder(); + appendFilterQuery(expressionNodes, filterQueryBuilder); + Map filterAttributeValue = filterQueryBuilder.getFilterAttributeValue(); + NamedJdbcTemplate namedJdbcTemplate = Utils.getNewTemplate(); List rowDataCollectors; + String sqlStmt = GET_ORGANIZATIONS_DISCOVERY_ATTRIBUTES + filterQueryBuilder.getFilterQuery() + + GET_ORGANIZATIONS_DISCOVERY_ATTRIBUTES_TAIL; try { - rowDataCollectors = namedJdbcTemplate.executeQuery(GET_ORGANIZATIONS_DISCOVERY_ATTRIBUTES, + rowDataCollectors = namedJdbcTemplate.executeQuery(sqlStmt, (resultSet, rowNumber) -> { OrganizationDiscoveryRowDataCollector collector = new OrganizationDiscoveryRowDataCollector(); collector.setId(resultSet.getString(1)); @@ -242,8 +259,12 @@ public List getOrganizationsDiscoveryAttributes(String ro collector.setOrganizationName(resultSet.getString(4)); return collector; }, - namedPreparedStatement -> - namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROOT_ID, rootOrganizationId)); + namedPreparedStatement -> { + namedPreparedStatement.setString(DB_SCHEMA_COLUMN_NAME_ROOT_ID, rootOrganizationId); + for (Map.Entry entry : filterAttributeValue.entrySet()) { + namedPreparedStatement.setString(entry.getKey(), entry.getValue()); + } + }); } catch (DataAccessException e) { throw handleServerException(ERROR_CODE_ERROR_LISTING_ORGANIZATIONS_DISCOVERY_ATTRIBUTES, e, rootOrganizationId); @@ -339,4 +360,84 @@ private List buildOrganizationsDiscoveryFromRawData( } return discoveryList; } + + private void appendFilterQuery(List expressionNodes, FilterQueryBuilder filterQueryBuilder) { + + int count = 1; + StringBuilder filter = new StringBuilder(); + if (CollectionUtils.isEmpty(expressionNodes)) { + filterQueryBuilder.setFilterQuery(StringUtils.EMPTY); + } else { + for (ExpressionNode expressionNode : expressionNodes) { + String operation = expressionNode.getOperation(); + String value = expressionNode.getValue(); + String attributeName = ATTRIBUTE_COLUMN_MAP.get(expressionNode.getAttributeValue()); + if (StringUtils.isNotBlank(attributeName) && StringUtils.isNotBlank(value) && StringUtils + .isNotBlank(operation)) { + switch (operation) { + case EQ: { + equalFilterBuilder(count, value, attributeName, filter, filterQueryBuilder); + count++; + break; + } + case SW: { + startWithFilterBuilder(count, value, attributeName, filter, filterQueryBuilder); + count++; + break; + } + case EW: { + endWithFilterBuilder(count, value, attributeName, filter, filterQueryBuilder); + count++; + break; + } + case CO: { + containsFilterBuilder(count, value, attributeName, filter, filterQueryBuilder); + count++; + break; + } + default: { + break; + } + } + } + } + if (StringUtils.isBlank(filter.toString())) { + filterQueryBuilder.setFilterQuery(StringUtils.EMPTY); + } else { + filterQueryBuilder.setFilterQuery(filter.toString()); + } + } + } + + private void equalFilterBuilder(int count, String value, String attributeName, StringBuilder filter, + FilterQueryBuilder filterQueryBuilder) { + + String filterString = String.format(" = :%s%s; AND ", FILTER_PLACEHOLDER_PREFIX, count); + filter.append(attributeName).append(filterString); + filterQueryBuilder.setFilterAttributeValue(FILTER_PLACEHOLDER_PREFIX, value); + } + + private void startWithFilterBuilder(int count, String value, String attributeName, StringBuilder filter, + FilterQueryBuilder filterQueryBuilder) { + + String filterString = String.format(" like :%s%s; AND ", FILTER_PLACEHOLDER_PREFIX, count); + filter.append(attributeName).append(filterString); + filterQueryBuilder.setFilterAttributeValue(FILTER_PLACEHOLDER_PREFIX, value + "%"); + } + + private void endWithFilterBuilder(int count, String value, String attributeName, StringBuilder filter, + FilterQueryBuilder filterQueryBuilder) { + + String filterString = String.format(" like :%s%s; AND ", FILTER_PLACEHOLDER_PREFIX, count); + filter.append(attributeName).append(filterString); + filterQueryBuilder.setFilterAttributeValue(FILTER_PLACEHOLDER_PREFIX, "%" + value); + } + + private void containsFilterBuilder(int count, String value, String attributeName, StringBuilder filter, + FilterQueryBuilder filterQueryBuilder) { + + String filterString = String.format(" like :%s%s; AND ", FILTER_PLACEHOLDER_PREFIX, count); + filter.append(attributeName).append(filterString); + filterQueryBuilder.setFilterAttributeValue(FILTER_PLACEHOLDER_PREFIX, "%" + value + "%"); + } } From 0bc17e1ec150dee1849748ac7af9bdc3208bb621 Mon Sep 17 00:00:00 2001 From: dewniMW Date: Sun, 22 Oct 2023 19:43:46 +0530 Subject: [PATCH 2/2] Address review comment --- .../discovery/service/OrganizationDiscoveryManagerImpl.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/OrganizationDiscoveryManagerImpl.java b/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/OrganizationDiscoveryManagerImpl.java index 3c84ca419..683eae41f 100644 --- a/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/OrganizationDiscoveryManagerImpl.java +++ b/components/org.wso2.carbon.identity.organization.discovery.service/src/main/java/org/wso2/carbon/identity/organization/discovery/service/OrganizationDiscoveryManagerImpl.java @@ -254,7 +254,7 @@ private void setExpressionNodeList(Node node, List expression) t String operation = expressionNode.getOperation(); if (StringUtils.isNotBlank(attributeValue)) { - if (isFilteringAttributeNotSupported(attributeValue)) { + if (!isFilteringAttributeSupported(attributeValue)) { throw handleClientException(ERROR_CODE_UNSUPPORTED_FILTER_ATTRIBUTE, attributeValue); } if (!SUPPORTED_OPERATIONS.contains(operation)) { @@ -273,8 +273,8 @@ private void setExpressionNodeList(Node node, List expression) t } } - private boolean isFilteringAttributeNotSupported(String attributeValue) { + private boolean isFilteringAttributeSupported(String attributeValue) { - return !attributeValue.equalsIgnoreCase(ORGANIZATION_NAME); + return ORGANIZATION_NAME.equalsIgnoreCase(attributeValue); } }