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

Include organization name in list discovery attributes of organizations method #264

Merged
merged 2 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -19,6 +19,7 @@
package org.wso2.carbon.identity.organization.discovery.service;

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 @@ -109,8 +110,7 @@ boolean isDiscoveryAttributeValueAvailable(String organizationId, String type, S
* @throws OrganizationManagementException The exception thrown when listing discovery attributes of the
* organizations.
*/
Map<String, List<OrgDiscoveryAttribute>> getOrganizationsDiscoveryAttributes()
throws OrganizationManagementException;
List<OrganizationDiscovery> getOrganizationsDiscoveryAttributes() throws OrganizationManagementException;

/**
* Get attribute based organization discovery handlers.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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.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 @@ -132,8 +133,7 @@ public boolean isDiscoveryAttributeValueAvailable(String organizationId, String
}

@Override
public Map<String, List<OrgDiscoveryAttribute>> getOrganizationsDiscoveryAttributes()
throws OrganizationManagementException {
public List<OrganizationDiscovery> getOrganizationsDiscoveryAttributes() throws OrganizationManagementException {

return organizationDiscoveryDAO.getOrganizationsDiscoveryAttributes(getOrganizationId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ 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 FROM UM_ORG_DISCOVERY 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 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 = :"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
package org.wso2.carbon.identity.organization.discovery.service.dao;

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 java.util.List;
import java.util.Map;

/**
* This interface performs CRUD operations for organization discovery.
Expand Down Expand Up @@ -106,7 +106,7 @@ void updateOrganizationDiscoveryAttributes(String organizationId, List<OrgDiscov
* @throws OrganizationManagementServerException The server exception thrown when listing discovery attributes of
* the organizations.
*/
Map<String, List<OrgDiscoveryAttribute>> getOrganizationsDiscoveryAttributes(String rootOrganizationId) throws
List<OrganizationDiscovery> getOrganizationsDiscoveryAttributes(String rootOrganizationId) throws
OrganizationManagementServerException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.wso2.carbon.database.utils.jdbc.exceptions.DataAccessException;
import org.wso2.carbon.database.utils.jdbc.exceptions.TransactionException;
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.util.Utils;

Expand Down Expand Up @@ -226,7 +227,7 @@ public void updateOrganizationDiscoveryAttributes(String organizationId,
}

@Override
public Map<String, List<OrgDiscoveryAttribute>> getOrganizationsDiscoveryAttributes(String rootOrganizationId)
public List<OrganizationDiscovery> getOrganizationsDiscoveryAttributes(String rootOrganizationId)
throws OrganizationManagementServerException {

NamedJdbcTemplate namedJdbcTemplate = Utils.getNewTemplate();
Expand All @@ -238,6 +239,7 @@ public Map<String, List<OrgDiscoveryAttribute>> getOrganizationsDiscoveryAttribu
collector.setId(resultSet.getString(1));
collector.setAttributeType(resultSet.getString(2));
collector.setAttributeValue(resultSet.getString(3));
collector.setOrganizationName(resultSet.getString(4));
return collector;
},
namedPreparedStatement ->
Expand All @@ -246,7 +248,7 @@ public Map<String, List<OrgDiscoveryAttribute>> getOrganizationsDiscoveryAttribu
throw handleServerException(ERROR_CODE_ERROR_LISTING_ORGANIZATIONS_DISCOVERY_ATTRIBUTES, e,
rootOrganizationId);
}
return buildOrganizationDiscoveryAttributesFromRawData(rowDataCollectors);
return buildOrganizationsDiscoveryFromRawData(rowDataCollectors);
}

@Override
Expand All @@ -268,48 +270,73 @@ public String getOrganizationIdByDiscoveryAttribute(String attributeType, String
}
}

private Map<String, List<OrgDiscoveryAttribute>> buildOrganizationDiscoveryAttributesFromRawData(
private List<OrganizationDiscovery> buildOrganizationsDiscoveryFromRawData(
List<OrganizationDiscoveryRowDataCollector> organizationRowDataCollectors) {

Map<String, List<OrgDiscoveryAttribute>> organizationDiscoveryAttributeMap = new HashMap<>();
List<OrganizationDiscovery> discoveryList = new ArrayList<>();

for (OrganizationDiscoveryRowDataCollector collector : organizationRowDataCollectors) {

String orgId = collector.getId();
String organizationId = collector.getId();
String organizationName = collector.getOrganizationName();
String attributeType = collector.getAttributeType();
String attributeValue = collector.getAttributeValue();

if (organizationDiscoveryAttributeMap.containsKey(orgId)) {
List<OrgDiscoveryAttribute> discoveryAttributes = organizationDiscoveryAttributeMap.get(orgId);
boolean newAttribute = true;
OrganizationDiscovery existingDiscovery = null;
for (OrganizationDiscovery discovery : discoveryList) {
if (StringUtils.equals(discovery.getOrganizationId(), organizationId)) {
existingDiscovery = discovery;
break;
}
}

if (existingDiscovery == null) {
OrgDiscoveryAttribute orgDiscoveryAttribute = new OrgDiscoveryAttribute();
orgDiscoveryAttribute.setType(attributeType);
orgDiscoveryAttribute.setValues(Collections.singletonList(attributeValue));
List<OrgDiscoveryAttribute> orgDiscoveryAttributeList = new ArrayList<>();
orgDiscoveryAttributeList.add(orgDiscoveryAttribute);

OrganizationDiscovery organizationDiscovery = new OrganizationDiscovery();
organizationDiscovery.setOrganizationId(organizationId);
organizationDiscovery.setOrganizationName(organizationName);
organizationDiscovery.setDiscoveryAttributes(orgDiscoveryAttributeList);

discoveryList.add(organizationDiscovery);
} else {
List<OrgDiscoveryAttribute> discoveryAttributes = existingDiscovery.getDiscoveryAttributes();
boolean attributeExists = false;
String newAttributeValue = null;
List<String> existingAttributeValues = null;
for (OrgDiscoveryAttribute attribute : discoveryAttributes) {
if (StringUtils.equals(attribute.getType(), attributeType)) {
List<String> values = attribute.getValues();
values.add(attributeValue);
attribute.setValues(values);
newAttribute = false;
existingAttributeValues = attribute.getValues();
newAttributeValue = attributeValue;
attributeExists = true;
break;
}
}
if (newAttribute) {

if (attributeExists) {
for (OrgDiscoveryAttribute attribute : discoveryAttributes) {
if (StringUtils.equals(attribute.getType(), attributeType)) {
if (existingAttributeValues == null) {
attribute.setValues(Collections.singletonList(newAttributeValue));
break;
}
List<String> attributeValues = new ArrayList<>(existingAttributeValues);
attributeValues.add(newAttributeValue);
attribute.setValues(attributeValues);
break;
}
}
} else {
OrgDiscoveryAttribute orgDiscoveryAttribute = new OrgDiscoveryAttribute();
orgDiscoveryAttribute.setType(attributeType);
orgDiscoveryAttribute.setValues(Collections.singletonList(attributeValue));
discoveryAttributes.add(orgDiscoveryAttribute);
organizationDiscoveryAttributeMap.put(orgId, discoveryAttributes);
}
} else {
List<OrgDiscoveryAttribute> discoveryAttributes = new ArrayList<>();
OrgDiscoveryAttribute orgDiscoveryAttribute = new OrgDiscoveryAttribute();
orgDiscoveryAttribute.setType(attributeType);
List<String> values = new ArrayList<>();
values.add(attributeValue);
orgDiscoveryAttribute.setValues(values);
discoveryAttributes.add(orgDiscoveryAttribute);
organizationDiscoveryAttributeMap.put(orgId, discoveryAttributes);
}
}

return organizationDiscoveryAttributeMap;
return discoveryList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class OrganizationDiscoveryRowDataCollector {
private String id;
private String attributeType;
private String attributeValue;
private String organizationName;

public String getId() {

Expand Down Expand Up @@ -56,4 +57,14 @@ public void setAttributeValue(String attributeValue) {

this.attributeValue = attributeValue;
}

public String getOrganizationName() {

return organizationName;
}

public void setOrganizationName(String organizationName) {

this.organizationName = organizationName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2023, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

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

import java.util.List;

/**
* Model class for holding discovery attributes along with the basic organization details.
*/
public class OrganizationDiscovery {

private String organizationId;
private String organizationName;
private List<OrgDiscoveryAttribute> discoveryAttributes;

public String getOrganizationId() {

return organizationId;
}

public void setOrganizationId(String organizationId) {

this.organizationId = organizationId;
}

public String getOrganizationName() {

return organizationName;
}

public void setOrganizationName(String organizationName) {

this.organizationName = organizationName;
}

public List<OrgDiscoveryAttribute> getDiscoveryAttributes() {

return discoveryAttributes;
}

public void setDiscoveryAttributes(List<OrgDiscoveryAttribute> discoveryAttributes) {

this.discoveryAttributes = discoveryAttributes;
}
}