Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue of getting 0 results when filtering with 2+ meta attributes #145

Merged
merged 3 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public class OrganizationManagementConstants {
public static final String AND = "and";
public static final String FILTER_PLACEHOLDER_PREFIX = "FILTER_ID_";
public static final String PARENT_ID_FILTER_PLACEHOLDER_PREFIX = "FILTER_PARENT_ID_";
public static final String META_ATTRIBUTE_PLACEHOLDER_PREFIX = "UM_ORG_ATTRIBUTE_";
private static final String ORGANIZATION_MANAGEMENT_ERROR_CODE_PREFIX = "ORG-";
private static final Map<String, String> attributeColumnMap = new HashMap<>();
public static final Map<String, String> ATTRIBUTE_COLUMN_MAP = Collections.unmodifiableMap(attributeColumnMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ public class SQLConstants {
"FETCH NEXT :" + SQLPlaceholders.DB_SCHEMA_LIMIT + "; ROWS ONLY";

public static final String INNER_JOIN_UM_ORG_ATTRIBUTE =
"INNER JOIN UM_ORG_ATTRIBUTE ON UM_ORG.UM_ID = UM_ORG_ATTRIBUTE.UM_ORG_ID WHERE";
"INNER JOIN UM_ORG_ATTRIBUTE %s ON UM_ORG.UM_ID = %s.UM_ORG_ID WHERE";

public static final String GET_ALL_UM_ORG_ATTRIBUTES = "SELECT UM_ORG.UM_ID, UM_ORG.UM_ORG_NAME, " +
"UM_ORG.UM_CREATED_TIME, UM_ORG.UM_STATUS, UM_ORG_ATTRIBUTE.UM_ATTRIBUTE_KEY, " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.VIEW_ID_COLUMN;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.VIEW_LAST_MODIFIED_COLUMN;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.VIEW_NAME_COLUMN;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.VIEW_ORGANIZATION_ATTRIBUTES_TABLE;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.VIEW_PARENT_ID_COLUMN;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.VIEW_STATUS_COLUMN;
import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.VIEW_TENANT_UUID_COLUMN;
Expand Down Expand Up @@ -731,8 +730,9 @@ private List<Organization> getOrganizationsList(boolean authorizedSubOrgsOnly, b
String userID = getUserId();
String sqlStmt = prepareGetOrganizationQuery(authorizedSubOrgsOnly, recursive, sortOrder, applicationAudience,
filterQueryBuilder, parentIdFilterQueryBuilder, userID);
boolean isFilteringMetaAttributes = filterQueryBuilder.getMetaAttributeCount() > 1;

if (filterQueryBuilder.getHasSubAttribute()) {
if (isFilteringMetaAttributes) {
sqlStmt = String.format(GET_ALL_UM_ORG_ATTRIBUTES, sqlStmt);
}
List<Organization> organizations;
Expand All @@ -741,7 +741,7 @@ private List<Organization> getOrganizationsList(boolean authorizedSubOrgsOnly, b
try {
organizations = namedJdbcTemplate.executeQuery(sqlStmt,
(resultSet, rowNumber) -> {
if (filterQueryBuilder.getHasSubAttribute()) {
if (isFilteringMetaAttributes) {
String orgId = resultSet.getString(1);
Organization organization = organizationMap.get(orgId);
if (organization == null) {
Expand All @@ -761,7 +761,7 @@ private List<Organization> getOrganizationsList(boolean authorizedSubOrgsOnly, b
} catch (DataAccessException e) {
throw handleServerException(ERROR_CODE_ERROR_RETRIEVING_ORGANIZATIONS, e);
}
if (filterQueryBuilder.getHasSubAttribute()) {
if (isFilteringMetaAttributes) {
return new ArrayList<>(organizationMap.values());
}
return organizations;
Expand Down Expand Up @@ -1509,8 +1509,12 @@ private String prepareGetOrganizationQuery(boolean authorizedSubOrgsOnly, boolea

String sqlStmt = getOrgSqlStatement(authorizedSubOrgsOnly, applicationAudience);
String getOrgSqlStmtTail = getOrgSqlStmtTail(authorizedSubOrgsOnly, applicationAudience);
if (filterQueryBuilder.getHasSubAttribute()) {
sqlStmt = sqlStmt.replace("WHERE", INNER_JOIN_UM_ORG_ATTRIBUTE);

if (filterQueryBuilder.getMetaAttributeCount() > 1) {
for (String placeholder : filterQueryBuilder.getMetaAttributePlaceholders()) {
sqlStmt = sqlStmt.replace("WHERE",
String.format(INNER_JOIN_UM_ORG_ATTRIBUTE, placeholder, placeholder));
}
}
sqlStmt = appendFilterQueries(sqlStmt, filterQueryBuilder, parentIdFilterQueryBuilder, getOrgSqlStmtTail,
recursive, sortOrder);
Expand Down Expand Up @@ -1614,11 +1618,11 @@ private void setFilterAttributes(NamedPreparedStatement namedPreparedStatement,

private String handleViewAttrKeyColumn(ExpressionNode expressionNode, FilterQueryBuilder filterQueryBuilder) {

filterQueryBuilder.setHasSubAttribute(true);
String placeholder = filterQueryBuilder.generateMetaAttributePlaceholder();
String attributeValue = expressionNode.getAttributeValue();
String subAttributeName = StringUtils.substringAfter(attributeValue,
ORGANIZATION_ATTRIBUTES_FIELD + ".");
return VIEW_ORGANIZATION_ATTRIBUTES_TABLE + "." + VIEW_ATTR_KEY_COLUMN + " = '" + subAttributeName + "' AND " +
VIEW_ORGANIZATION_ATTRIBUTES_TABLE + "." + VIEW_ATTR_VALUE_COLUMN;
String subAttributeName = StringUtils.substringAfter(attributeValue, ORGANIZATION_ATTRIBUTES_FIELD + ".");

return placeholder + "." + VIEW_ATTR_KEY_COLUMN + " = '" + subAttributeName + "' AND " + placeholder + "."
+ VIEW_ATTR_VALUE_COLUMN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.List;
import java.util.Map;

import static org.wso2.carbon.identity.organization.management.service.constant.OrganizationManagementConstants.META_ATTRIBUTE_PLACEHOLDER_PREFIX;

/**
* Create filter query builder.
*/
Expand All @@ -32,7 +34,8 @@ public class FilterQueryBuilder {
private List<String> timestampParameters = new ArrayList<>();
private int count = 1;
private String filter;
private boolean hasSubAttribute;
private List<String> metaAttributePlaceholders = new ArrayList<>();
private int metaAttributeCount = 1;
HasiniSama marked this conversation as resolved.
Show resolved Hide resolved

/**
* Get filter query builder attributes.
Expand Down Expand Up @@ -97,22 +100,35 @@ public String getFilterQuery() {
}

/**
* Get the value of hasSubAttribute.
* Get filter query builder meta attribute placeholders.
*
* @return List of filter query builder meta attribute placeholders.
*/
public List<String> getMetaAttributePlaceholders() {

return metaAttributePlaceholders;
}

/**
* Generate and add a filter query builder meta attribute placeholder.
*
* @return boolean value indicating if the filter contains a sub attribute.
* @return Next meta attribute placeholder.
*/
public boolean getHasSubAttribute() {
HasiniSama marked this conversation as resolved.
Show resolved Hide resolved
public String generateMetaAttributePlaceholder() {

return hasSubAttribute;
String placeholder = META_ATTRIBUTE_PLACEHOLDER_PREFIX + metaAttributeCount;
metaAttributePlaceholders.add(placeholder);
metaAttributeCount++;
return placeholder;
}

/**
* Set the value of hasSubAttribute.
* Get the current count of meta attribute placeholders.
*
* @param hasSubAttribute boolean value indicating if the filter contains a sub attributes.
* @return Meta attribute count.
*/
public void setHasSubAttribute(boolean hasSubAttribute) {
public int getMetaAttributeCount() {

this.hasSubAttribute = hasSubAttribute;
return metaAttributeCount;
}
}
Loading