Search in sources :

Example 6 with PropertyConverter

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

the class PropertyMap method javaTypeToDatabaseType.

// ----- static methods -----
public static PropertyMap javaTypeToDatabaseType(SecurityContext securityContext, GraphObject wrapped, Map<String, Object> source) throws FrameworkException {
    final PropertyMap resultMap = new PropertyMap();
    final GraphObject entity = unwrap(wrapped);
    if (source != null) {
        for (Entry<String, Object> entry : source.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (key != null) {
                final PropertyKey propertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(entity.getClass(), key);
                final PropertyConverter converter = propertyKey.databaseConverter(securityContext, entity);
                if (converter != null) {
                    try {
                        Object propertyValue = converter.convert(value);
                        resultMap.put(propertyKey, propertyValue);
                    } catch (ClassCastException cce) {
                        throw new FrameworkException(422, "Invalid JSON input for key " + propertyKey.jsonName() + ", expected a JSON " + propertyKey.typeName() + ".");
                    }
                } else {
                    resultMap.put(propertyKey, value);
                }
            }
        }
    }
    return resultMap;
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) PropertyConverter(org.structr.core.converter.PropertyConverter) GraphObject(org.structr.core.GraphObject) GraphObject(org.structr.core.GraphObject)

Example 7 with PropertyConverter

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

the class PropertyMap method databaseTypeToJavaType.

public static PropertyMap databaseTypeToJavaType(SecurityContext securityContext, Class<? extends GraphObject> entityType, Map<String, Object> source) throws FrameworkException {
    PropertyMap resultMap = new PropertyMap();
    if (source != null) {
        for (Entry<String, Object> entry : source.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (key != null) {
                PropertyKey propertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(entityType, key);
                PropertyConverter converter = propertyKey.databaseConverter(securityContext);
                if (converter != null) {
                    try {
                        Object propertyValue = converter.revert(value);
                        resultMap.put(propertyKey, propertyValue);
                    } catch (ClassCastException cce) {
                        throw new FrameworkException(422, "Invalid JSON input for key " + propertyKey.jsonName() + ", expected a JSON " + propertyKey.typeName() + ".");
                    }
                } else {
                    resultMap.put(propertyKey, value);
                }
            }
        }
    }
    return resultMap;
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) PropertyConverter(org.structr.core.converter.PropertyConverter) GraphObject(org.structr.core.GraphObject)

Example 8 with PropertyConverter

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

the class EntityNotionProperty method getSearchAttribute.

@Override
public SearchAttribute getSearchAttribute(SecurityContext securityContext, Occurrence occur, T searchValue, 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 (searchValue != null && !StringUtils.isBlank(searchValue.toString())) {
            final App app = StructrApp.getInstance(securityContext);
            final PropertyKey key = notion.getPrimaryPropertyKey();
            final PropertyConverter inputConverter = key != null ? key.inputConverter(securityContext) : null;
            // transform search values using input convert of notion property
            final Object transformedValue = inputConverter != null ? inputConverter.convert(searchValue) : searchValue;
            if (exactMatch) {
                Result<AbstractNode> result = app.nodeQuery(entityProperty.relatedType()).and(key, transformedValue).getResult();
                for (AbstractNode node : result.getResults()) {
                    switch(occur) {
                        case REQUIRED:
                            if (!alreadyAdded) {
                                // the first result is the basis of all subsequent intersections
                                intersectionResult.addAll(entityProperty.getRelatedNodesReverse(securityContext, node, declaringClass, predicate));
                                // the next additions are intersected with this one
                                alreadyAdded = true;
                            } else {
                                intersectionResult.retainAll(entityProperty.getRelatedNodesReverse(securityContext, node, declaringClass, predicate));
                            }
                            break;
                        case OPTIONAL:
                            intersectionResult.addAll(entityProperty.getRelatedNodesReverse(securityContext, node, declaringClass, predicate));
                            break;
                        case FORBIDDEN:
                            break;
                    }
                }
            } else {
                Result<AbstractNode> result = app.nodeQuery(entityProperty.relatedType()).and(key, transformedValue, false).getResult();
                // loose search behaves differently, all results must be combined
                for (AbstractNode node : result.getResults()) {
                    intersectionResult.addAll(entityProperty.getRelatedNodesReverse(securityContext, node, declaringClass, predicate));
                }
            }
            attr.setResult(intersectionResult);
        } else {
            // value in the given field
            return new EmptySearchAttribute(this, null);
        }
    } 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) PropertyConverter(org.structr.core.converter.PropertyConverter) GraphObject(org.structr.core.GraphObject)

Example 9 with PropertyConverter

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

the class StructrPropertyValueChannel method setConvertedPropertyValue.

public static void setConvertedPropertyValue(final SecurityContext securityContext, final GraphObject graphObject, final PropertyKey key, final String value) throws IOException, FrameworkException {
    final PropertyConverter converter = key.inputConverter(securityContext);
    final String previousUuid = graphObject.getUuid();
    Object actualValue = value;
    if (converter != null) {
        actualValue = converter.convert(actualValue);
    }
    if (key.isReadOnly()) {
        graphObject.unlockReadOnlyPropertiesOnce();
    }
    if (key.isSystemInternal()) {
        graphObject.unlockSystemPropertiesOnce();
    }
    // set value
    graphObject.setProperty(key, actualValue);
    // move "hidden" entries to new UUID
    if (GraphObject.id.equals(key)) {
        final HiddenFileEntry entry = StructrPath.HIDDEN_PROPERTY_FILES.get(previousUuid);
        if (entry != null) {
            StructrPath.HIDDEN_PROPERTY_FILES.remove(previousUuid);
            StructrPath.HIDDEN_PROPERTY_FILES.put(value, entry);
        }
    }
}
Also used : PropertyConverter(org.structr.core.converter.PropertyConverter) GraphObject(org.structr.core.GraphObject) HiddenFileEntry(org.structr.files.ssh.filesystem.StructrPath.HiddenFileEntry)

Example 10 with PropertyConverter

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

the class GetFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    final SecurityContext securityContext = ctx.getSecurityContext();
    try {
        if (!arrayHasLengthAndAllElementsNotNull(sources, 2)) {
            return "";
        }
        final String keyName = sources[1].toString();
        GraphObject dataObject = null;
        // handle GraphObject
        if (sources[0] instanceof GraphObject) {
            dataObject = (GraphObject) sources[0];
        }
        // handle first element of a list of graph objects
        if (sources[0] instanceof List) {
            final List list = (List) sources[0];
            final int size = list.size();
            if (size == 1) {
                final Object value = list.get(0);
                if (value != null) {
                    if (value instanceof GraphObject) {
                        dataObject = (GraphObject) list.get(0);
                    } else {
                        return "get(): first element of collection is of type " + value.getClass() + " which is not supported.";
                    }
                } else {
                    return "get(): first element of collection is null.";
                }
            }
        }
        // handle map separately
        if (sources[0] instanceof Map && !(sources[0] instanceof GraphObjectMap)) {
            final Map map = (Map) sources[0];
            return map.get(keyName);
        }
        // handle request object
        if (sources[0] instanceof HttpServletRequest) {
            final HttpServletRequest request = (HttpServletRequest) sources[0];
            return request.getParameter(keyName);
        }
        if (dataObject != null) {
            final PropertyKey key = StructrApp.key(dataObject.getClass(), keyName);
            if (key != null) {
                final PropertyConverter inputConverter = key.inputConverter(securityContext);
                Object value = dataObject.getProperty(key);
                if (inputConverter != null) {
                    return inputConverter.revert(value);
                }
                return dataObject.getProperty(key);
            }
            return "";
        } else {
            return ERROR_MESSAGE_GET_ENTITY;
        }
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) GraphObjectMap(org.structr.core.GraphObjectMap) SecurityContext(org.structr.common.SecurityContext) PropertyConverter(org.structr.core.converter.PropertyConverter) List(java.util.List) GraphObject(org.structr.core.GraphObject) GraphObject(org.structr.core.GraphObject) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap) PropertyKey(org.structr.core.property.PropertyKey)

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