Search in sources :

Example 26 with PropertyConverter

use of org.structr.core.converter.PropertyConverter in project structr by structr.

the class CollectionNotionProperty method convertSearchValue.

@Override
public List<T> convertSearchValue(SecurityContext securityContext, String requestParameter) throws FrameworkException {
    PropertyKey propertyKey = notion.getPrimaryPropertyKey();
    List<T> list = new LinkedList<>();
    if (propertyKey != null) {
        PropertyConverter inputConverter = propertyKey.inputConverter(securityContext);
        if (inputConverter != null) {
            for (String part : requestParameter.split("[,;]+")) {
                list.add((T) inputConverter.convert(part));
            }
        } else {
            for (String part : requestParameter.split("[,;]+")) {
                list.add((T) part);
            }
        }
    }
    return list;
}
Also used : PropertyConverter(org.structr.core.converter.PropertyConverter) LinkedList(java.util.LinkedList)

Example 27 with PropertyConverter

use of org.structr.core.converter.PropertyConverter 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)

Example 28 with PropertyConverter

use of org.structr.core.converter.PropertyConverter in project structr by structr.

the class GroupProperty method setGroupedProperties.

@Override
public void setGroupedProperties(SecurityContext securityContext, PropertyMap source, GraphObject destination) throws FrameworkException {
    if (source.containsKey(nullValuesOnlyProperty)) {
        throw new FrameworkException(422, "Property " + jsonName + " is read-only", new ReadOnlyPropertyToken(destination.getClass().getSimpleName(), nullValuesOnlyProperty));
    }
    if (source.isEmpty()) {
        destination.setProperty(nullValuesOnlyProperty, true);
        return;
    }
    // indicate that this group actually contains values
    destination.setProperty(nullValuesOnlyProperty, false);
    // set properties
    for (PropertyKey key : propertyKeys.values()) {
        Object value = source.get(new GenericProperty(key.jsonName()));
        PropertyConverter converter = key.inputConverter(securityContext);
        if (converter != null) {
            try {
                Object convertedValue = converter.convert(value);
                destination.setProperty(key, convertedValue);
            } catch (FrameworkException fex) {
                logger.warn("Unable to convert grouped property {} on type {}: {}", new Object[] { key.dbName(), source.getClass().getSimpleName(), fex.getMessage() });
            }
        } else {
            destination.setProperty(key, value);
        }
    }
}
Also used : ReadOnlyPropertyToken(org.structr.common.error.ReadOnlyPropertyToken) FrameworkException(org.structr.common.error.FrameworkException) PropertyConverter(org.structr.core.converter.PropertyConverter) GraphObject(org.structr.core.GraphObject)

Example 29 with PropertyConverter

use of org.structr.core.converter.PropertyConverter in project structr by structr.

the class JoinProperty method setProperty.

@Override
public Object setProperty(SecurityContext securityContext, final GraphObject obj, String value) throws FrameworkException {
    final MessageFormat formatter = new MessageFormat(format, Locale.GERMAN);
    Object[] values = null;
    int len = 0;
    try {
        values = formatter.parse(value);
        len = values.length;
    } catch (ParseException pex) {
        throw new FrameworkException(422, pex.getMessage());
    }
    for (int i = 0; i < len; i++) {
        final PropertyKey key = keys.get(i);
        final PropertyConverter inputConverter = key.inputConverter(securityContext);
        if (inputConverter != null) {
            key.setProperty(securityContext, obj, inputConverter.convert(values[i]));
        } else {
            key.setProperty(securityContext, obj, values[i]);
        }
    }
    return null;
}
Also used : MessageFormat(java.text.MessageFormat) FrameworkException(org.structr.common.error.FrameworkException) PropertyConverter(org.structr.core.converter.PropertyConverter) GraphObject(org.structr.core.GraphObject) ParseException(java.text.ParseException)

Example 30 with PropertyConverter

use of org.structr.core.converter.PropertyConverter in project structr by structr.

the class JoinProperty method getProperty.

@Override
public String getProperty(SecurityContext securityContext, GraphObject obj, boolean applyConverter, final Predicate<GraphObject> predicate) {
    final ArrayList<Object> arguments = new ArrayList<>();
    for (Iterator<PropertyKey> it = keys.iterator(); it.hasNext(); ) {
        final PropertyKey key = it.next();
        final PropertyConverter inputConverter = key.inputConverter(securityContext);
        if (inputConverter != null) {
            try {
                final Object value = inputConverter.revert(key.getProperty(securityContext, obj, applyConverter, predicate));
                if (value != null) {
                    arguments.add(value);
                }
            } catch (FrameworkException fex) {
                logger.warn("", fex);
            }
        } else {
            final Object value = key.getProperty(securityContext, obj, applyConverter, predicate);
            if (value != null) {
                arguments.add(value);
            }
        }
    }
    try {
        return MessageFormat.format(format, arguments.toArray());
    } catch (Throwable t) {
    }
    return null;
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) ArrayList(java.util.ArrayList) PropertyConverter(org.structr.core.converter.PropertyConverter) GraphObject(org.structr.core.GraphObject)

Aggregations

PropertyConverter (org.structr.core.converter.PropertyConverter)44 GraphObject (org.structr.core.GraphObject)31 FrameworkException (org.structr.common.error.FrameworkException)28 PropertyKey (org.structr.core.property.PropertyKey)18 SecurityContext (org.structr.common.SecurityContext)11 PropertyMap (org.structr.core.property.PropertyMap)10 ConfigurationProvider (org.structr.schema.ConfigurationProvider)10 Map (java.util.Map)9 AbstractNode (org.structr.core.entity.AbstractNode)6 List (java.util.List)5 App (org.structr.core.app.App)5 StructrApp (org.structr.core.app.StructrApp)5 LinkedList (java.util.LinkedList)4 Test (org.junit.Test)4 StructrTest (org.structr.common.StructrTest)4 Query (org.structr.core.app.Query)4 LinkedHashMap (java.util.LinkedHashMap)3 GraphObjectMap (org.structr.core.GraphObjectMap)3 LinkedHashSet (java.util.LinkedHashSet)2 Entry (java.util.Map.Entry)2