Search in sources :

Example 1 with PropertyConverter

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

the class SetFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    if (arrayHasMinLengthAndAllElementsNotNull(sources, 2)) {
        final SecurityContext securityContext = ctx.getSecurityContext();
        final ConfigurationProvider config = StructrApp.getConfiguration();
        Class type = null;
        PropertyMap propertyMap = null;
        if (sources[0] instanceof GraphObject) {
            final GraphObject source = (GraphObject) sources[0];
            type = source.getEntityType();
        }
        if (type == null) {
            throw new FrameworkException(422, "Can't get type of object '" + sources[0].toString() + "' in set() method!");
        }
        final GraphObject sourceObject = (GraphObject) sources[0];
        if (sources.length == 2 && sources[1] instanceof Map) {
            propertyMap = PropertyMap.inputTypeToJavaType(securityContext, type, (Map) sources[1]);
        } else if (sources.length == 2 && sources[1] instanceof GraphObjectMap) {
            propertyMap = PropertyMap.inputTypeToJavaType(securityContext, type, ((GraphObjectMap) sources[1]).toMap());
        } else if (sources.length == 2 && sources[1] instanceof String) {
            final Gson gson = new GsonBuilder().create();
            final Map<String, Object> values = deserialize(gson, sources[1].toString());
            if (values != null) {
                propertyMap = PropertyMap.inputTypeToJavaType(securityContext, type, values);
            }
        } else if (sources.length > 2) {
            propertyMap = new PropertyMap();
            final int parameter_count = sources.length;
            if (parameter_count % 2 == 0) {
                throw new FrameworkException(400, "Invalid number of parameters: " + parameter_count + ". Should be uneven: " + (ctx.isJavaScriptContext() ? ERROR_MESSAGE_SET_JS : ERROR_MESSAGE_SET));
            }
            for (int c = 1; c < parameter_count; c += 2) {
                final PropertyKey key = config.getPropertyKeyForJSONName(type, sources[c].toString());
                if (key != null) {
                    final PropertyConverter inputConverter = key.inputConverter(securityContext);
                    Object value = sources[c + 1];
                    if (inputConverter != null) {
                        value = inputConverter.convert(value);
                    }
                    propertyMap.put(key, value);
                }
            }
        } else {
            throw new FrameworkException(422, "Invalid use of builtin method set, usage: set(entity, params..)");
        }
        if (propertyMap != null) {
            sourceObject.setProperties(securityContext, propertyMap);
        }
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
    }
    return "";
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) GsonBuilder(com.google.gson.GsonBuilder) ConfigurationProvider(org.structr.schema.ConfigurationProvider) Gson(com.google.gson.Gson) GraphObject(org.structr.core.GraphObject) PropertyMap(org.structr.core.property.PropertyMap) GraphObjectMap(org.structr.core.GraphObjectMap) SecurityContext(org.structr.common.SecurityContext) PropertyConverter(org.structr.core.converter.PropertyConverter) GraphObject(org.structr.core.GraphObject) PropertyMap(org.structr.core.property.PropertyMap) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap) PropertyKey(org.structr.core.property.PropertyKey)

Example 2 with PropertyConverter

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

the class Property method determineSearchType.

protected void determineSearchType(final SecurityContext securityContext, final String requestParameter, final boolean exactMatch, final Query query) throws FrameworkException {
    if (StringUtils.startsWith(requestParameter, "[") && StringUtils.endsWith(requestParameter, "]")) {
        // check for existance of range query string
        Matcher matcher = rangeQueryPattern.matcher(requestParameter);
        if (matcher.matches()) {
            if (matcher.groupCount() == 2) {
                String rangeStart = matcher.group(1);
                String rangeEnd = matcher.group(2);
                PropertyConverter inputConverter = inputConverter(securityContext);
                Object rangeStartConverted = rangeStart;
                Object rangeEndConverted = rangeEnd;
                if (inputConverter != null) {
                    rangeStartConverted = inputConverter.convert(rangeStartConverted);
                    rangeEndConverted = inputConverter.convert(rangeEndConverted);
                }
                query.andRange(this, rangeStartConverted, rangeEndConverted);
                return;
            }
            logger.warn("Unable to determine range query bounds for {}", requestParameter);
        } else {
            if ("[]".equals(requestParameter)) {
                if (isIndexedWhenEmpty()) {
                    // requestParameter contains only [],
                    // which we use as a "not-blank" selector
                    query.notBlank(this);
                    return;
                } else {
                    throw new FrameworkException(400, "PropertyKey " + jsonName() + " must be indexedWhenEmpty() to be used in not-blank search query.");
                }
            } else {
                throw new FrameworkException(422, "Invalid range pattern.");
            }
        }
    }
    if (requestParameter.contains(",") && requestParameter.contains(";")) {
        throw new FrameworkException(422, "Mixing of AND and OR not allowed in request parameters");
    }
    if (requestParameter.contains(";")) {
        if (multiValueSplitAllowed()) {
            // descend into a new group
            query.and();
            for (final String part : requestParameter.split("[;]+")) {
                query.or(this, convertSearchValue(securityContext, part), exactMatch);
            }
            // ascend to the last group
            query.parent();
        } else {
            query.or(this, convertSearchValue(securityContext, requestParameter), exactMatch);
        }
    } else {
        query.and(this, convertSearchValue(securityContext, requestParameter), exactMatch);
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) Matcher(java.util.regex.Matcher) PropertyConverter(org.structr.core.converter.PropertyConverter) GraphObject(org.structr.core.GraphObject)

Example 3 with PropertyConverter

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

the class Property method convertSearchValue.

@Override
public T convertSearchValue(final SecurityContext securityContext, final String requestParameter) throws FrameworkException {
    PropertyConverter inputConverter = inputConverter(securityContext);
    Object convertedSearchValue = requestParameter;
    if (inputConverter != null) {
        convertedSearchValue = inputConverter.convert(convertedSearchValue);
    }
    return (T) convertedSearchValue;
}
Also used : PropertyConverter(org.structr.core.converter.PropertyConverter) GraphObject(org.structr.core.GraphObject)

Example 4 with PropertyConverter

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

the class PropertyMap method javaTypeToDatabaseType.

public static Map<String, Object> javaTypeToDatabaseType(SecurityContext securityContext, Class<? extends GraphObject> entity, PropertyMap properties) throws FrameworkException {
    Map<String, Object> databaseTypedProperties = new LinkedHashMap<>();
    for (Entry<PropertyKey, Object> entry : properties.entrySet()) {
        PropertyKey propertyKey = entry.getKey();
        PropertyConverter converter = propertyKey.databaseConverter(securityContext);
        if (converter != null) {
            try {
                Object propertyValue = converter.convert(entry.getValue());
                databaseTypedProperties.put(propertyKey.jsonName(), propertyValue);
            } catch (ClassCastException cce) {
                throw new FrameworkException(422, "Invalid JSON input for key " + propertyKey.jsonName() + ", expected a JSON " + propertyKey.typeName() + ".");
            }
        } else {
            databaseTypedProperties.put(propertyKey.jsonName(), entry.getValue());
        }
    }
    return databaseTypedProperties;
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) PropertyConverter(org.structr.core.converter.PropertyConverter) GraphObject(org.structr.core.GraphObject) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with PropertyConverter

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

the class PropertyMap method inputTypeToJavaType.

public static PropertyMap inputTypeToJavaType(final SecurityContext securityContext, Class<? extends GraphObject> entity, final Map<String, Object> source) throws FrameworkException {
    final String batchType = securityContext.getAttribute("batchType", "__");
    if (batchType.equals(source.get("type"))) {
        // only to batching if a type is set for which batch is enable
        final Integer count = securityContext.getAttribute("objectCount", 0);
        final Integer overall = securityContext.getAttribute("overallCount", 0);
        securityContext.setAttribute("objectCount", count + 1);
        securityContext.setAttribute("overallCount", overall + 1);
        if (count == 100) {
            final Tx tx = (Tx) securityContext.getAttribute("currentTransaction");
            if (tx != null) {
                logger.info("Committing batch transaction after {} objects of type {}.", overall, batchType);
                // try to commit this batch
                tx.success();
                tx.close();
                // open new transaction and store it in context
                securityContext.setAttribute("currentTransaction", StructrApp.getInstance(securityContext).tx());
                securityContext.setAttribute("objectCount", 0);
            }
        }
    }
    PropertyMap resultMap = new PropertyMap();
    if (source != null) {
        // caution, source can be null when an empty nested property group is encountered!
        for (final Entry<String, Object> entry : source.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (key != null) {
                final PropertyKey propertyKey = StructrApp.getConfiguration().getPropertyKeyForJSONName(entity, key);
                if (propertyKey != null) {
                    final PropertyConverter converter = propertyKey.inputConverter(securityContext);
                    if (converter != null && value != null && !propertyKey.valueType().isAssignableFrom(value.getClass())) {
                        try {
                            // test
                            converter.setContext(source);
                            Object propertyValue = converter.convert(value);
                            resultMap.put(propertyKey, propertyValue);
                        } catch (ClassCastException cce) {
                            logger.warn("", 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 : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) 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