use of org.wso2.carbon.idp.mgt.model.FilterQueryBuilder in project carbon-identity-framework by wso2.
the class IdPManagementDAO method appendFilterQuery.
/**
* Create a sql query and prepared statement for filter.
*
* @param expressionNodes list of filters.
* @param filterQueryBuilder Sql builder object.
* @throws IdentityProviderManagementClientException throw invalid filer attribute exception.
*/
private void appendFilterQuery(List<ExpressionNode> expressionNodes, FilterQueryBuilder filterQueryBuilder) throws IdentityProviderManagementClientException {
StringBuilder filter = new StringBuilder();
if (CollectionUtils.isEmpty(expressionNodes)) {
filterQueryBuilder.setFilterQuery(IdPManagementConstants.EMPTY_STRING);
} else {
for (ExpressionNode expressionNode : expressionNodes) {
String operation = expressionNode.getOperation();
String value = expressionNode.getValue();
String attributeName = expressionNode.getAttributeValue();
if (StringUtils.isNotBlank(attributeName) && StringUtils.isNotBlank(value) && StringUtils.isNotBlank(operation)) {
switch(attributeName) {
case IdPManagementConstants.IDP_NAME:
attributeName = IdPManagementConstants.NAME;
break;
case IdPManagementConstants.IDP_DESCRIPTION:
attributeName = IdPManagementConstants.DESCRIPTION;
break;
case IdPManagementConstants.IDP_HOME_REALM_ID:
attributeName = IdPManagementConstants.HOME_REALM_ID;
break;
case IdPManagementConstants.IDP_IS_ENABLED:
attributeName = IdPManagementConstants.IS_ENABLED;
break;
case IdPManagementConstants.IDP_UUID:
attributeName = IdPManagementConstants.UUID;
break;
default:
String message = "Invalid filter attribute name. Filter attribute : " + attributeName;
throw IdPManagementUtil.handleClientException(IdPManagementConstants.ErrorMessage.ERROR_CODE_RETRIEVE_IDP, message);
}
if (IdPManagementConstants.EQ.equals(operation)) {
filter.append(attributeName).append(" = ? AND ");
filterQueryBuilder.setFilterAttributeValue(value);
} else if (IdPManagementConstants.SW.equals(operation)) {
filter.append(attributeName).append(" like ? AND ");
filterQueryBuilder.setFilterAttributeValue(value + "%");
} else if (IdPManagementConstants.EW.equals(operation)) {
filter.append(attributeName).append(" like ? AND ");
filterQueryBuilder.setFilterAttributeValue("%" + value);
} else if (IdPManagementConstants.CO.equals(operation)) {
filter.append(attributeName).append(" like ? AND ");
filterQueryBuilder.setFilterAttributeValue("%" + value + "%");
} else {
String message = "Invalid filter value. filter: " + operation;
throw IdPManagementUtil.handleClientException(IdPManagementConstants.ErrorMessage.ERROR_CODE_RETRIEVE_IDP, message);
}
}
}
if (StringUtils.isBlank(filter.toString())) {
filterQueryBuilder.setFilterQuery(IdPManagementConstants.EMPTY_STRING);
} else {
filterQueryBuilder.setFilterQuery(filter.toString());
}
}
}
use of org.wso2.carbon.idp.mgt.model.FilterQueryBuilder in project carbon-identity-framework by wso2.
the class IdPManagementDAO method getCountOfFilteredIdPs.
/**
* Get number of IdP count for a matching filter.
*
* @param tenantId Tenant Id of the identity provider.
* @param expressionNode filter value list for IdP search.
* @return number of IdP count for a given filter
* @throws IdentityProviderManagementServerException Error when getting count of Identity Providers.
* @throws IdentityProviderManagementClientException Error when append the filer string.
*/
int getCountOfFilteredIdPs(int tenantId, List<ExpressionNode> expressionNode) throws IdentityProviderManagementServerException, IdentityProviderManagementClientException {
String sqlStmt = IdPManagementConstants.SQLQueries.GET_IDP_COUNT_SQL;
int countOfFilteredIdp = 0;
FilterQueryBuilder filterQueryBuilder = new FilterQueryBuilder();
appendFilterQuery(expressionNode, filterQueryBuilder);
Map<Integer, String> filterAttributeValue = filterQueryBuilder.getFilterAttributeValue();
sqlStmt = sqlStmt + filterQueryBuilder.getFilterQuery() + IdPManagementConstants.SQLQueries.GET_IDP_COUNT_SQL_TAIL;
try (Connection dbConnection = IdentityDatabaseUtil.getDBConnection(false);
PreparedStatement prepStmt = dbConnection.prepareStatement(sqlStmt)) {
for (Map.Entry<Integer, String> prepareStatement : filterAttributeValue.entrySet()) {
prepStmt.setString(prepareStatement.getKey(), prepareStatement.getValue());
}
prepStmt.setInt(filterAttributeValue.entrySet().size() + 1, tenantId);
try (ResultSet rs = prepStmt.executeQuery()) {
if (rs.next()) {
countOfFilteredIdp = Integer.parseInt(rs.getString(1));
}
}
} catch (SQLException e) {
String message = "Error occurred while retrieving Identity Provider count for a tenant : " + IdentityTenantUtil.getTenantDomain(tenantId);
throw IdPManagementUtil.handleServerException(IdPManagementConstants.ErrorMessage.ERROR_CODE_CONNECTING_DATABASE, message, e);
}
return countOfFilteredIdp;
}
use of org.wso2.carbon.idp.mgt.model.FilterQueryBuilder in project carbon-identity-framework by wso2.
the class IdPManagementDAO method getIdPsSearch.
/**
* Get all identity provider's Basic information along with additionally requested information depends on the
* requiredAttributes for a given matching filter.
*
* @param tenantId Tenant Id of the identity provider.
* @param expressionNode List of filter value for IdP search.
* @param limit Limit per page.
* @param offset Offset value.
* @param sortOrder Order of IdP ASC/DESC.
* @param sortBy The attribute need to sort.
* @param requiredAttributes Required attributes which needs to be return.
* @return Identity Provider's Basic Information array along with requested attribute information.
* @throws IdentityProviderManagementServerException Error when getting list of Identity Providers.
* @throws IdentityProviderManagementClientException Error when append the filer string.
*/
List<IdentityProvider> getIdPsSearch(int tenantId, List<ExpressionNode> expressionNode, int limit, int offset, String sortOrder, String sortBy, List<String> requiredAttributes) throws IdentityProviderManagementServerException, IdentityProviderManagementClientException {
FilterQueryBuilder filterQueryBuilder = new FilterQueryBuilder();
appendFilterQuery(expressionNode, filterQueryBuilder);
String sortedOrder = sortBy + " " + sortOrder;
try (Connection dbConnection = IdentityDatabaseUtil.getDBConnection(false);
ResultSet resultSet = getIdpQueryResultSet(dbConnection, sortedOrder, tenantId, offset, limit, filterQueryBuilder, requiredAttributes)) {
return populateIdentityProviderList(resultSet, dbConnection, requiredAttributes, tenantId);
} catch (SQLException e) {
String message = "Error occurred while retrieving Identity Provider for tenant: " + IdentityTenantUtil.getTenantDomain(tenantId);
throw IdPManagementUtil.handleServerException(IdPManagementConstants.ErrorMessage.ERROR_CODE_CONNECTING_DATABASE, message, e);
}
}
Aggregations