Search in sources :

Example 6 with EmptySearchAttribute

use of org.structr.core.graph.search.EmptySearchAttribute in project structr by structr.

the class CollectionNotionProperty method getSearchAttribute.

@Override
public SearchAttribute getSearchAttribute(SecurityContext securityContext, Occurrence occur, List<T> searchValues, boolean exactMatch, final Query query) {
    final Predicate<GraphObject> predicate = query != null ? query.toPredicate() : null;
    final SourceSearchAttribute attr = new SourceSearchAttribute(occur);
    final Set<GraphObject> intersectionResult = new LinkedHashSet<>();
    boolean alreadyAdded = false;
    try {
        if (searchValues != null && !searchValues.isEmpty()) {
            final PropertyKey key = notion.getPrimaryPropertyKey();
            final PropertyConverter inputConverter = key.inputConverter(securityContext);
            final List<Object> transformedValues = new LinkedList<>();
            boolean allBlank = true;
            // transform search values using input convert of notion property
            for (T searchValue : searchValues) {
                if (inputConverter != null) {
                    transformedValues.add(inputConverter.convert(searchValue));
                } else {
                    transformedValues.add(searchValue);
                }
            }
            // iterate over transformed values
            for (Object searchValue : transformedValues) {
                // check if the list contains non-empty search values
                if (StringUtils.isBlank(searchValue.toString())) {
                    continue;
                } else {
                    allBlank = false;
                }
                final App app = StructrApp.getInstance(securityContext);
                if (exactMatch) {
                    Result<AbstractNode> result = app.nodeQuery(collectionProperty.relatedType()).and(notion.getPrimaryPropertyKey(), searchValue).getResult();
                    for (AbstractNode node : result.getResults()) {
                        switch(occur) {
                            case REQUIRED:
                                if (!alreadyAdded) {
                                    // the first result is the basis of all subsequent intersections
                                    intersectionResult.addAll(collectionProperty.getRelatedNodesReverse(securityContext, node, declaringClass, predicate));
                                    // the next additions are intersected with this one
                                    alreadyAdded = true;
                                } else {
                                    intersectionResult.retainAll(collectionProperty.getRelatedNodesReverse(securityContext, node, declaringClass, predicate));
                                }
                                break;
                            case OPTIONAL:
                                intersectionResult.addAll(collectionProperty.getRelatedNodesReverse(securityContext, node, declaringClass, predicate));
                                break;
                            case FORBIDDEN:
                                break;
                        }
                    }
                } else {
                    Result<AbstractNode> result = app.nodeQuery(collectionProperty.relatedType()).and(notion.getPrimaryPropertyKey(), searchValue, false).getResult();
                    // loose search behaves differently, all results must be combined
                    for (AbstractNode node : result.getResults()) {
                        intersectionResult.addAll(collectionProperty.getRelatedNodesReverse(securityContext, node, declaringClass, predicate));
                    }
                }
            }
            if (allBlank) {
                // value in the given field
                return new EmptySearchAttribute(this, Collections.emptyList());
            } else {
                attr.setResult(intersectionResult);
            }
        } else {
            // value in the given field
            return new EmptySearchAttribute(this, Collections.emptyList());
        }
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
    return attr;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) FrameworkException(org.structr.common.error.FrameworkException) SourceSearchAttribute(org.structr.core.graph.search.SourceSearchAttribute) AbstractNode(org.structr.core.entity.AbstractNode) EmptySearchAttribute(org.structr.core.graph.search.EmptySearchAttribute) GraphObject(org.structr.core.GraphObject) LinkedList(java.util.LinkedList) PropertyConverter(org.structr.core.converter.PropertyConverter) GraphObject(org.structr.core.GraphObject)

Aggregations

LinkedHashSet (java.util.LinkedHashSet)6 GraphObject (org.structr.core.GraphObject)6 EmptySearchAttribute (org.structr.core.graph.search.EmptySearchAttribute)6 SourceSearchAttribute (org.structr.core.graph.search.SourceSearchAttribute)6 FrameworkException (org.structr.common.error.FrameworkException)2 App (org.structr.core.app.App)2 StructrApp (org.structr.core.app.StructrApp)2 PropertyConverter (org.structr.core.converter.PropertyConverter)2 AbstractNode (org.structr.core.entity.AbstractNode)2 NodeInterface (org.structr.core.graph.NodeInterface)2 LinkedList (java.util.LinkedList)1