use of org.apache.syncope.core.persistence.api.dao.search.AnyCond in project syncope by apache.
the class SyncopeLogic method searchAssignableGroups.
@PreAuthorize("isAuthenticated()")
public Pair<Integer, List<GroupTO>> searchAssignableGroups(final String realm, final String term, final int page, final int size) {
AssignableCond assignableCond = new AssignableCond();
assignableCond.setRealmFullPath(realm);
SearchCond searchCond;
if (StringUtils.isNotBlank(term)) {
AnyCond termCond = new AnyCond(AttributeCond.Type.ILIKE);
termCond.setSchema("name");
String termSearchableValue = (term.startsWith("*") && !term.endsWith("*")) ? term + "%" : (!term.startsWith("*") && term.endsWith("*")) ? "%" + term : (term.startsWith("*") && term.endsWith("*") ? term : "%" + term + "%");
termCond.setExpression(termSearchableValue);
searchCond = SearchCond.getAndCond(SearchCond.getLeafCond(assignableCond), SearchCond.getLeafCond(termCond));
} else {
searchCond = SearchCond.getLeafCond(assignableCond);
}
int count = searchDAO.count(SyncopeConstants.FULL_ADMIN_REALMS, searchCond, AnyTypeKind.GROUP);
OrderByClause orderByClause = new OrderByClause();
orderByClause.setField("name");
orderByClause.setDirection(OrderByClause.Direction.ASC);
List<Group> matching = searchDAO.search(SyncopeConstants.FULL_ADMIN_REALMS, searchCond, page, size, Collections.singletonList(orderByClause), AnyTypeKind.GROUP);
List<GroupTO> result = matching.stream().map(group -> groupDataBinder.getGroupTO(group, false)).collect(Collectors.toList());
return Pair.of(count, result);
}
use of org.apache.syncope.core.persistence.api.dao.search.AnyCond in project syncope by apache.
the class AbstractAnySearchDAO method matches.
@Override
public <T extends Any<?>> boolean matches(final T any, final SearchCond cond) {
AnyCond keycond = new AnyCond(AttributeCond.Type.EQ);
keycond.setSchema("key");
keycond.setExpression(any.getKey());
return !search(SearchCond.getAndCond(SearchCond.getLeafCond(keycond), cond), any.getType().getKind()).isEmpty();
}
use of org.apache.syncope.core.persistence.api.dao.search.AnyCond in project syncope by apache.
the class AbstractAnySearchDAO method check.
protected Triple<PlainSchema, PlainAttrValue, AnyCond> check(final AnyCond cond, final AnyTypeKind kind) {
AnyCond condClone = SerializationUtils.clone(cond);
AnyUtils attrUtils = anyUtilsFactory.getInstance(kind);
// Keeps track of difference between entity's getKey() and JPA @Id fields
if ("key".equals(condClone.getSchema())) {
condClone.setSchema("id");
}
Field anyField = ReflectionUtils.findField(attrUtils.anyClass(), condClone.getSchema());
if (anyField == null) {
LOG.warn("Ignoring invalid schema '{}'", condClone.getSchema());
throw new IllegalArgumentException();
}
PlainSchema schema = new JPAPlainSchema();
schema.setKey(anyField.getName());
for (AttrSchemaType attrSchemaType : AttrSchemaType.values()) {
if (anyField.getType().isAssignableFrom(attrSchemaType.getType())) {
schema.setType(attrSchemaType);
}
}
// Deal with any Integer fields logically mapping to boolean values
boolean foundBooleanMin = false;
boolean foundBooleanMax = false;
if (Integer.class.equals(anyField.getType())) {
for (Annotation annotation : anyField.getAnnotations()) {
if (Min.class.equals(annotation.annotationType())) {
foundBooleanMin = ((Min) annotation).value() == 0;
} else if (Max.class.equals(annotation.annotationType())) {
foundBooleanMax = ((Max) annotation).value() == 1;
}
}
}
if (foundBooleanMin && foundBooleanMax) {
schema.setType(AttrSchemaType.Boolean);
}
// Deal with any fields representing relationships to other entities
if (anyField.getType().getAnnotation(Entity.class) != null) {
Method relMethod = null;
try {
relMethod = ClassUtils.getPublicMethod(anyField.getType(), "getKey", new Class<?>[0]);
} catch (Exception e) {
LOG.error("Could not find {}#getKey", anyField.getType(), e);
}
if (relMethod != null && String.class.isAssignableFrom(relMethod.getReturnType())) {
condClone.setSchema(condClone.getSchema() + "_id");
schema.setType(AttrSchemaType.String);
}
}
PlainAttrValue attrValue = attrUtils.newPlainAttrValue();
if (condClone.getType() != AttributeCond.Type.LIKE && condClone.getType() != AttributeCond.Type.ILIKE && condClone.getType() != AttributeCond.Type.ISNULL && condClone.getType() != AttributeCond.Type.ISNOTNULL) {
try {
((JPAPlainSchema) schema).validator().validate(condClone.getExpression(), attrValue);
} catch (ValidationException e) {
LOG.error("Could not validate expression '" + condClone.getExpression() + "'", e);
throw new IllegalArgumentException();
}
}
return Triple.of(schema, attrValue, condClone);
}
use of org.apache.syncope.core.persistence.api.dao.search.AnyCond in project syncope by apache.
the class DefaultPullCorrelationRule method getSearchCond.
@Override
public SearchCond getSearchCond(final ConnectorObject connObj, final Provision provision) {
Map<String, Item> mappingItems = provision.getMapping().getItems().stream().collect(Collectors.toMap(Item::getIntAttrName, Function.identity()));
// search for anys by attribute(s) specified in the policy
SearchCond searchCond = null;
for (String schema : conf.getSchemas()) {
Item mappingItem = mappingItems.get(schema);
Attribute attr = mappingItem == null ? null : connObj.getAttributeByName(mappingItem.getExtAttrName());
if (attr == null) {
throw new IllegalArgumentException("Connector object does not contains the attributes to perform the search: " + schema);
}
AttributeCond.Type type;
String expression = null;
if (attr.getValue() == null || attr.getValue().isEmpty() || (attr.getValue().size() == 1 && attr.getValue().get(0) == null)) {
type = AttributeCond.Type.ISNULL;
} else {
type = AttributeCond.Type.EQ;
expression = attr.getValue().size() > 1 ? attr.getValue().toString() : attr.getValue().get(0).toString();
}
SearchCond nodeCond;
// any objects: just key or name can be selected
if ("key".equalsIgnoreCase(schema) || "username".equalsIgnoreCase(schema) || "name".equalsIgnoreCase(schema)) {
AnyCond cond = new AnyCond();
cond.setSchema(schema);
cond.setType(type);
cond.setExpression(expression);
nodeCond = SearchCond.getLeafCond(cond);
} else {
AttributeCond cond = new AttributeCond();
cond.setSchema(schema);
cond.setType(type);
cond.setExpression(expression);
nodeCond = SearchCond.getLeafCond(cond);
}
searchCond = searchCond == null ? nodeCond : SearchCond.getAndCond(searchCond, nodeCond);
}
return searchCond;
}
use of org.apache.syncope.core.persistence.api.dao.search.AnyCond in project syncope by apache.
the class SearchCondConverterTest method ilike.
@Test
public void ilike() {
String fiql = new UserFiqlSearchConditionBuilder().is("username").equalToIgnoreCase("ros*").query();
assertEquals("username=~ros*", fiql);
AttributeCond attrCond = new AnyCond(AttributeCond.Type.ILIKE);
attrCond.setSchema("username");
attrCond.setExpression("ros%");
SearchCond simpleCond = SearchCond.getLeafCond(attrCond);
assertEquals(simpleCond, SearchCondConverter.convert(fiql));
}
Aggregations