Search in sources :

Example 21 with SchemaProperty

use of org.structr.core.entity.SchemaProperty in project structr by structr.

the class StructrSchema method replaceDatabaseSchema.

/**
 * Replaces the current Structr schema with the given new schema. This
 * method is the reverse of createFromDatabase above.
 *
 * @param app
 * @param newSchema the new schema to replace the current Structr schema
 *
 * @throws FrameworkException
 * @throws URISyntaxException
 */
public static void replaceDatabaseSchema(final App app, final JsonSchema newSchema) throws FrameworkException, URISyntaxException {
    Services.getInstance().setOverridingSchemaTypesAllowed(true);
    try (final Tx tx = app.tx()) {
        for (final SchemaRelationshipNode schemaRelationship : app.nodeQuery(SchemaRelationshipNode.class).getAsList()) {
            app.delete(schemaRelationship);
        }
        for (final SchemaNode schemaNode : app.nodeQuery(SchemaNode.class).getAsList()) {
            app.delete(schemaNode);
        }
        for (final SchemaMethod schemaMethod : app.nodeQuery(SchemaMethod.class).getAsList()) {
            app.delete(schemaMethod);
        }
        for (final SchemaMethodParameter schemaMethodParameter : app.nodeQuery(SchemaMethodParameter.class).getAsList()) {
            app.delete(schemaMethodParameter);
        }
        for (final SchemaProperty schemaProperty : app.nodeQuery(SchemaProperty.class).getAsList()) {
            app.delete(schemaProperty);
        }
        for (final SchemaView schemaView : app.nodeQuery(SchemaView.class).getAsList()) {
            app.delete(schemaView);
        }
        newSchema.createDatabaseSchema(app, JsonSchema.ImportMode.replace);
        tx.success();
    }
}
Also used : SchemaView(org.structr.core.entity.SchemaView) SchemaNode(org.structr.core.entity.SchemaNode) SchemaProperty(org.structr.core.entity.SchemaProperty) Tx(org.structr.core.graph.Tx) SchemaMethod(org.structr.core.entity.SchemaMethod) SchemaRelationshipNode(org.structr.core.entity.SchemaRelationshipNode) SchemaMethodParameter(org.structr.core.entity.SchemaMethodParameter)

Example 22 with SchemaProperty

use of org.structr.core.entity.SchemaProperty in project structr by structr.

the class StructrScriptProperty method createDatabaseSchema.

@Override
SchemaProperty createDatabaseSchema(final App app, final AbstractSchemaNode schemaNode) throws FrameworkException {
    final SchemaProperty property = super.createDatabaseSchema(app, schemaNode);
    final PropertyMap properties = new PropertyMap();
    final String contentType = getContentType();
    if (contentType != null) {
        switch(contentType) {
            case "application/x-structr-javascript":
            case "application/x-structr-script":
                properties.put(SchemaProperty.propertyType, Type.Function.name());
                break;
            case "application/x-cypher":
                properties.put(SchemaProperty.propertyType, Type.Cypher.name());
        }
    } else {
        // default
        properties.put(SchemaProperty.propertyType, Type.Function.name());
    }
    properties.put(SchemaProperty.format, source);
    // set properties in bulk
    property.setProperties(SecurityContext.getSuperUserInstance(), properties);
    return property;
}
Also used : SchemaProperty(org.structr.core.entity.SchemaProperty) PropertyMap(org.structr.core.property.PropertyMap)

Example 23 with SchemaProperty

use of org.structr.core.entity.SchemaProperty in project structr by structr.

the class StructrThumbnailProperty method createDatabaseSchema.

@Override
SchemaProperty createDatabaseSchema(final App app, final AbstractSchemaNode schemaNode) throws FrameworkException {
    final SchemaProperty property = super.createDatabaseSchema(app, schemaNode);
    final PropertyMap properties = new PropertyMap();
    properties.put(SchemaProperty.propertyType, Type.Thumbnail.name());
    properties.put(SchemaProperty.format, getFormat());
    property.setProperties(SecurityContext.getSuperUserInstance(), properties);
    return property;
}
Also used : SchemaProperty(org.structr.core.entity.SchemaProperty) PropertyMap(org.structr.core.property.PropertyMap)

Example 24 with SchemaProperty

use of org.structr.core.entity.SchemaProperty in project structr by structr.

the class StructrTypeDefinition method createDatabaseSchema.

AbstractSchemaNode createDatabaseSchema(final App app) throws FrameworkException {
    final Map<String, SchemaProperty> schemaProperties = new TreeMap<>();
    final Map<String, SchemaMethod> schemaMethods = new TreeMap<>();
    final PropertyMap createProperties = new PropertyMap();
    final PropertyMap nodeProperties = new PropertyMap();
    // properties that always need to be set
    createProperties.put(SchemaNode.isInterface, isInterface);
    createProperties.put(SchemaNode.isAbstract, isAbstract);
    createProperties.put(SchemaNode.category, category);
    createProperties.put(SchemaNode.isBuiltinType, SchemaService.DynamicSchemaRootURI.equals(root.getId()));
    final T schemaNode = createSchemaNode(app, createProperties);
    for (final StructrPropertyDefinition property : properties) {
        final SchemaProperty schemaProperty = property.createDatabaseSchema(app, schemaNode);
        if (schemaProperty != null) {
            schemaProperties.put(schemaProperty.getName(), schemaProperty);
        }
    }
    // create views and associate the properties
    for (final Entry<String, Set<String>> view : views.entrySet()) {
        final List<SchemaProperty> viewProperties = new LinkedList<>();
        final List<String> nonGraphProperties = new LinkedList<>();
        for (final String propertyName : view.getValue()) {
            final SchemaProperty property = schemaProperties.get(propertyName);
            if (property != null) {
                viewProperties.add(property);
            } else {
                nonGraphProperties.add(propertyName);
            }
        }
        SchemaView viewNode = app.nodeQuery(SchemaView.class).and(SchemaView.schemaNode, schemaNode).and(SchemaView.name, view.getKey()).getFirst();
        if (viewNode == null) {
            viewNode = app.create(SchemaView.class, new NodeAttribute<>(SchemaView.schemaNode, schemaNode), new NodeAttribute<>(SchemaView.name, view.getKey()));
        }
        final PropertyMap updateProperties = new PropertyMap();
        updateProperties.put(SchemaView.schemaProperties, viewProperties);
        updateProperties.put(SchemaView.nonGraphProperties, StringUtils.join(nonGraphProperties, ", "));
        // update properties of existing or new schema view node
        viewNode.setProperties(SecurityContext.getSuperUserInstance(), updateProperties);
    }
    for (final StructrMethodDefinition method : methods) {
        final SchemaMethod schemaMethod = method.createDatabaseSchema(app, schemaNode);
        if (schemaMethod != null) {
            schemaMethods.put(schemaMethod.getName(), schemaMethod);
        }
    }
    // extends
    if (baseTypeReference != null) {
        final Object def = root.resolveURI(baseTypeReference);
        if (def != null && def instanceof JsonType) {
            final JsonType jsonType = (JsonType) def;
            final String superclassName = "org.structr.dynamic." + jsonType.getName();
            nodeProperties.put(SchemaNode.extendsClass, superclassName);
        } else {
            final Class superclass = StructrApp.resolveSchemaId(baseTypeReference);
            if (superclass != null) {
                if (superclass.isInterface()) {
                    nodeProperties.put(SchemaNode.implementsInterfaces, superclass.getName());
                } else {
                    nodeProperties.put(SchemaNode.extendsClass, superclass.getName());
                }
            } else if ("https://structr.org/v1.1/definitions/FileBase".equals(baseTypeReference.toString())) {
                // FileBase doesn't exist any more, but we need to support it for some time..
                nodeProperties.put(SchemaNode.implementsInterfaces, "org.structr.web.entity.File");
            } else if (!StructrApp.getSchemaBaseURI().relativize(baseTypeReference).isAbsolute()) {
                // resolve internal type referenced in special URI
                final URI base = StructrApp.getSchemaBaseURI().resolve("definitions/");
                final URI type = base.relativize(baseTypeReference);
                final String typeName = type.getPath();
                final String parameters = type.getQuery();
                final Class internal = StructrApp.getConfiguration().getNodeEntityClass(typeName);
                if (internal != null) {
                    nodeProperties.put(SchemaNode.extendsClass, getParameterizedType(internal, parameters));
                }
            }
        }
    }
    // implements
    if (!implementedInterfaces.isEmpty()) {
        final Set<String> interfaces = new LinkedHashSet<>();
        for (final URI implementedInterface : implementedInterfaces) {
            final Object def = root.resolveURI(implementedInterface);
            if (def != null && def instanceof JsonType) {
                final JsonType jsonType = (JsonType) def;
                final String superclassName = "org.structr.dynamic." + jsonType.getName();
                if (jsonType.isInterface()) {
                    interfaces.add(superclassName);
                } else {
                    nodeProperties.put(SchemaNode.extendsClass, superclassName);
                }
            } else {
                final Class superclass = StructrApp.resolveSchemaId(implementedInterface);
                if (superclass != null) {
                    interfaces.add(superclass.getName());
                } else {
                    logger.warn("Unable to resolve built-in type {} against Structr schema", implementedInterface);
                    StructrApp.resolveSchemaId(implementedInterface);
                }
            }
        }
        nodeProperties.put(SchemaNode.implementsInterfaces, StringUtils.join(interfaces, ", "));
    }
    schemaNode.setProperties(SecurityContext.getSuperUserInstance(), nodeProperties);
    return schemaNode;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) NodeAttribute(org.structr.core.graph.NodeAttribute) JsonType(org.structr.schema.json.JsonType) SchemaProperty(org.structr.core.entity.SchemaProperty) TreeSet(java.util.TreeSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) SchemaMethod(org.structr.core.entity.SchemaMethod) TreeMap(java.util.TreeMap) URI(java.net.URI) LinkedList(java.util.LinkedList) SchemaView(org.structr.core.entity.SchemaView) PropertyMap(org.structr.core.property.PropertyMap)

Example 25 with SchemaProperty

use of org.structr.core.entity.SchemaProperty in project structr by structr.

the class StructrTypeDefinition method deserialize.

void deserialize(final T schemaNode) {
    for (final SchemaProperty property : schemaNode.getProperty(AbstractSchemaNode.schemaProperties)) {
        final StructrPropertyDefinition propertyDefinition = StructrPropertyDefinition.deserialize(this, property);
        if (propertyDefinition != null) {
            properties.add(propertyDefinition);
        }
    }
    for (final SchemaView view : schemaNode.getProperty(AbstractSchemaNode.schemaViews)) {
        if (!View.INTERNAL_GRAPH_VIEW.equals(view.getName())) {
            final Set<String> propertySet = new TreeSet<>();
            for (final SchemaProperty property : view.getProperty(SchemaView.schemaProperties)) {
                propertySet.add(property.getName());
            }
            final String nonGraphProperties = view.getProperty(SchemaView.nonGraphProperties);
            if (nonGraphProperties != null) {
                for (final String property : nonGraphProperties.split("[, ]+")) {
                    final String trimmed = property.trim();
                    if (StringUtils.isNotBlank(trimmed)) {
                        propertySet.add(trimmed);
                    }
                }
            }
            if (!propertySet.isEmpty()) {
                views.put(view.getName(), propertySet);
            }
        }
    }
    for (final SchemaMethod method : schemaNode.getProperty(AbstractSchemaNode.schemaMethods)) {
        final StructrMethodDefinition newMethod = StructrMethodDefinition.deserialize(this, method);
        if (newMethod != null) {
            methods.add(newMethod);
        }
    }
    // $extends
    final String extendsClass = schemaNode.getProperty(SchemaNode.extendsClass);
    if (extendsClass != null) {
        final String typeName = resolveParameterizedType(extendsClass);
        if (extendsClass.startsWith("org.structr.dynamic.")) {
            this.baseTypeReference = root.getId().resolve("definitions/" + typeName);
        } else {
            this.baseTypeReference = StructrApp.getSchemaBaseURI().resolve("definitions/" + typeName);
        }
    }
    // $implements
    final String implementsInterfaces = schemaNode.getProperty(SchemaNode.implementsInterfaces);
    if (implementsInterfaces != null) {
        for (final String impl : implementsInterfaces.split("[, ]+")) {
            final String trimmed = impl.trim();
            if (StringUtils.isNotEmpty(trimmed)) {
                final String typeName = trimmed.substring(trimmed.lastIndexOf(".") + 1);
                if (trimmed.startsWith("org.structr.dynamic.")) {
                    this.implementedInterfaces.add(root.getId().resolve("definitions/" + typeName));
                } else {
                    this.implementedInterfaces.add(StructrApp.getSchemaBaseURI().resolve("definitions/" + typeName));
                }
            }
        }
    }
    this.isInterface = schemaNode.getProperty(SchemaNode.isInterface);
    this.isAbstract = schemaNode.getProperty(SchemaNode.isAbstract);
    this.category = schemaNode.getProperty(SchemaNode.category);
}
Also used : SchemaView(org.structr.core.entity.SchemaView) SchemaProperty(org.structr.core.entity.SchemaProperty) SchemaMethod(org.structr.core.entity.SchemaMethod) TreeSet(java.util.TreeSet)

Aggregations

SchemaProperty (org.structr.core.entity.SchemaProperty)34 PropertyMap (org.structr.core.property.PropertyMap)23 FrameworkException (org.structr.common.error.FrameworkException)7 SchemaNode (org.structr.core.entity.SchemaNode)7 Tx (org.structr.core.graph.Tx)7 LinkedList (java.util.LinkedList)5 App (org.structr.core.app.App)5 StructrApp (org.structr.core.app.StructrApp)5 Test (org.junit.Test)4 SchemaMethod (org.structr.core.entity.SchemaMethod)4 SchemaView (org.structr.core.entity.SchemaView)4 AbstractSchemaNode (org.structr.core.entity.AbstractSchemaNode)3 FrontendTest (org.structr.web.basic.FrontendTest)3 ResourceAccessTest (org.structr.web.basic.ResourceAccessTest)3 LinkedHashSet (java.util.LinkedHashSet)2 TreeSet (java.util.TreeSet)2 PropertyContainer (org.structr.api.graph.PropertyContainer)2 NodeAttribute (org.structr.core.graph.NodeAttribute)2 StructrPath (org.structr.files.ssh.filesystem.StructrPath)2 URI (java.net.URI)1