Search in sources :

Example 31 with PropertyConverter

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

the class FindRelationshipFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        if (sources == null) {
            throw new IllegalArgumentException();
        }
        final SecurityContext securityContext = ctx.getSecurityContext();
        final ConfigurationProvider config = StructrApp.getConfiguration();
        final Query query = StructrApp.getInstance(securityContext).relationshipQuery().sort(GraphObject.createdDate).order(false);
        // the type to query for
        Class type = null;
        if (sources.length >= 1 && sources[0] != null) {
            final String typeString = sources[0].toString();
            type = config.getRelationshipEntityClass(typeString);
            if (type != null) {
                query.andTypes(type);
            } else {
                logger.warn("Error in find_relationship(): type \"{}\" not found.", typeString);
                return ERROR_MESSAGE_FIND_RELATIONSHIP_TYPE_NOT_FOUND + typeString;
            }
        }
        // exit gracefully instead of crashing..
        if (type == null) {
            logger.warn("Error in find_relationship(): no type specified. Parameters: {}", getParametersAsString(sources));
            return ERROR_MESSAGE_FIND_RELATIONSHIP_NO_TYPE_SPECIFIED;
        }
        // experimental: disable result count, prevents instantiation
        // of large collections just for counting all the objects..
        securityContext.ignoreResultCount(true);
        // extension for native javascript objects
        if (sources.length == 2 && sources[1] instanceof Map) {
            query.and(PropertyMap.inputTypeToJavaType(securityContext, type, (Map) sources[1]));
        } else if (sources.length == 2) {
            if (sources[1] == null) {
                throw new IllegalArgumentException();
            }
            // special case: second parameter is a UUID
            final PropertyKey key = StructrApp.key(type, "id");
            query.and(key, sources[1].toString());
            return query.getFirst();
        } else {
            final int parameter_count = sources.length;
            if (parameter_count % 2 == 0) {
                throw new FrameworkException(400, "Invalid number of parameters: " + parameter_count + ". Should be uneven: " + ERROR_MESSAGE_FIND_RELATIONSHIP);
            }
            for (int c = 1; c < parameter_count; c += 2) {
                if (sources[c] == null) {
                    throw new IllegalArgumentException();
                }
                final PropertyKey key = StructrApp.key(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);
                    }
                    query.and(key, value);
                }
            }
        }
        return query.getAsList();
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : Query(org.structr.core.app.Query) FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider) 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) PropertyKey(org.structr.core.property.PropertyKey)

Example 32 with PropertyConverter

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

the class GetOrNullFunction 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 null;
        }
        GraphObject dataObject = null;
        if (sources[0] instanceof GraphObject) {
            dataObject = (GraphObject) sources[0];
        }
        if (sources[0] instanceof List) {
            final List list = (List) sources[0];
            if (list.size() == 1 && list.get(0) instanceof GraphObject) {
                dataObject = (GraphObject) list.get(0);
            }
        }
        if (dataObject != null) {
            final String keyName = sources[1].toString();
            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 "";
        }
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
    return null;
}
Also used : 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) PropertyKey(org.structr.core.property.PropertyKey)

Example 33 with PropertyConverter

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

the class FindFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        if (sources == null) {
            throw new IllegalArgumentException();
        }
        final SecurityContext securityContext = ctx.getSecurityContext();
        final ConfigurationProvider config = StructrApp.getConfiguration();
        final App app = StructrApp.getInstance(securityContext);
        final Query query = app.nodeQuery().sort(GraphObject.createdDate).order(false);
        // paging applied by surrounding slice() function
        applyRange(securityContext, query);
        // the type to query for
        Class type = null;
        if (sources.length >= 1 && sources[0] != null) {
            final String typeString = sources[0].toString();
            type = config.getNodeEntityClass(typeString);
            if (type != null) {
                query.andTypes(type);
            } else {
                logger.warn("Error in find(): type \"{}\" not found.", typeString);
                return ERROR_MESSAGE_FIND_TYPE_NOT_FOUND + typeString;
            }
        }
        // exit gracefully instead of crashing..
        if (type == null) {
            logger.warn("Error in find(): no type specified. Parameters: {}", getParametersAsString(sources));
            return ERROR_MESSAGE_FIND_NO_TYPE_SPECIFIED;
        }
        // experimental: disable result count, prevents instantiation
        // of large collections just for counting all the objects..
        securityContext.ignoreResultCount(true);
        // extension for native javascript objects
        if (sources.length == 2 && sources[1] instanceof Map) {
            query.and(PropertyMap.inputTypeToJavaType(securityContext, type, (Map) sources[1]));
        } else if (sources.length == 2) {
            if (sources[1] == null) {
                throw new IllegalArgumentException();
            }
            // special case: second parameter is a UUID
            final PropertyKey key = StructrApp.key(type, "id");
            query.and(key, sources[1].toString());
            return query.getFirst();
        } else {
            final int parameter_count = sources.length;
            if (parameter_count % 2 == 0) {
                throw new FrameworkException(400, "Invalid number of parameters: " + parameter_count + ". Should be uneven: " + ERROR_MESSAGE_FIND);
            }
            for (int c = 1; c < parameter_count; c += 2) {
                if (sources[c] == null) {
                    throw new IllegalArgumentException();
                }
                final PropertyKey key = StructrApp.key(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);
                    }
                    query.and(key, value);
                }
            }
        }
        return query.getAsList();
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    } finally {
        resetRange();
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Query(org.structr.core.app.Query) FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider) 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) PropertyKey(org.structr.core.property.PropertyKey)

Example 34 with PropertyConverter

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

the class CreateFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    if (sources != null && sources.length > 0) {
        final SecurityContext securityContext = ctx.getSecurityContext();
        final ConfigurationProvider config = StructrApp.getConfiguration();
        PropertyMap propertyMap;
        Class type = null;
        if (sources.length >= 1 && sources[0] != null) {
            type = config.getNodeEntityClass(sources[0].toString());
        }
        if (type == null) {
            throw new FrameworkException(422, "Unknown type '" + sources[0].toString() + "' in create() method!");
        }
        // extension for native javascript objects
        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 {
            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_CREATE_JS : ERROR_MESSAGE_CREATE));
            }
            for (int c = 1; c < parameter_count; c += 2) {
                final PropertyKey key = StructrApp.key(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);
                }
            }
        }
        return StructrApp.getInstance(securityContext).create(type, propertyMap);
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider) GraphObjectMap(org.structr.core.GraphObjectMap) SecurityContext(org.structr.common.SecurityContext) PropertyConverter(org.structr.core.converter.PropertyConverter) PropertyMap(org.structr.core.property.PropertyMap) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap) PropertyKey(org.structr.core.property.PropertyKey)

Example 35 with PropertyConverter

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

the class PropertyTest method testDatabaseConverterOnCollectionProperty.

/**
 * Test of databaseConverter method, of class CollectionProperty.
 */
@Test
public void testDatabaseConverterOnCollectionProperty() {
    Property<List<TestOne>> instance = TestSix.manyToManyTestOnes;
    PropertyConverter expResult = null;
    PropertyConverter result = instance.databaseConverter(securityContext, null);
    assertEquals(expResult, result);
}
Also used : PropertyConverter(org.structr.core.converter.PropertyConverter) LinkedList(java.util.LinkedList) List(java.util.List) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

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