Search in sources :

Example 16 with SchemaMethod

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

the class StructrMethodDefinition method createDatabaseSchema.

// ----- package methods -----
SchemaMethod createDatabaseSchema(final App app, final AbstractSchemaNode schemaNode) throws FrameworkException {
    final PropertyMap getOrCreateProperties = new PropertyMap();
    final PropertyMap updateProperties = new PropertyMap();
    int index = 0;
    getOrCreateProperties.put(SchemaMethod.name, getName());
    getOrCreateProperties.put(SchemaMethod.signature, getSignature());
    getOrCreateProperties.put(SchemaMethod.codeType, getCodeType());
    getOrCreateProperties.put(SchemaMethod.returnType, getReturnType());
    getOrCreateProperties.put(SchemaMethod.schemaNode, schemaNode);
    getOrCreateProperties.put(SchemaMethod.source, getSource());
    getOrCreateProperties.put(SchemaMethod.comment, getComment());
    getOrCreateProperties.put(SchemaMethod.exceptions, getExceptions().toArray(new String[0]));
    getOrCreateProperties.put(SchemaMethod.overridesExisting, overridesExisting());
    getOrCreateProperties.put(SchemaMethod.callSuper, callSuper());
    getOrCreateProperties.put(SchemaMethod.doExport, doExport());
    SchemaMethod method = app.nodeQuery(SchemaMethod.class).and(getOrCreateProperties).getFirst();
    if (method == null) {
        method = app.create(SchemaMethod.class, getOrCreateProperties);
    }
    updateProperties.put(SchemaMethod.isPartOfBuiltInSchema, true);
    method.setProperties(SecurityContext.getSuperUserInstance(), updateProperties);
    // create database schema for method parameters
    for (final StructrParameterDefinition param : parameters) {
        param.createDatabaseSchema(app, method, index++);
    }
    // return modified property
    return method;
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) SchemaMethod(org.structr.core.entity.SchemaMethod)

Example 17 with SchemaMethod

use of org.structr.core.entity.SchemaMethod 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 18 with SchemaMethod

use of org.structr.core.entity.SchemaMethod 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 19 with SchemaMethod

use of org.structr.core.entity.SchemaMethod 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)

Example 20 with SchemaMethod

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

the class SchemaMethodsTest method test03SchemaMethodOnEntityOfBuiltinType.

@Test
public void test03SchemaMethodOnEntityOfBuiltinType() {
    final String builtinTypeName = "File";
    final String schemaMethodName = "testFileMethod";
    User admin = null;
    try (final Tx tx = app.tx()) {
        admin = createAdminUser();
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        // Add schema method "testFileMethod" to built-in File class
        SchemaNode fileNodeDef = app.nodeQuery(SchemaNode.class).andName(builtinTypeName).getFirst();
        final PropertyMap testFileMethodProperties = new PropertyMap();
        testFileMethodProperties.put(SchemaMethod.name, schemaMethodName);
        testFileMethodProperties.put(SchemaMethod.source, "()");
        testFileMethodProperties.put(SchemaMethod.schemaNode, fileNodeDef);
        SchemaMethod testFileMethod = app.create(SchemaMethod.class, testFileMethodProperties);
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    File testFile = null;
    try (final Tx tx = app.tx()) {
        // Create File instance
        testFile = app.create(File.class, "Test File");
        testFile.setProperty(File.owner, admin);
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).headers("X-User", ADMIN_USERNAME, "X-Password", ADMIN_PASSWORD).body("{}").expect().statusCode(200).when().post(builtinTypeName + "/" + testFile.getUuid() + "/" + schemaMethodName);
        tx.success();
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : SchemaNode(org.structr.core.entity.SchemaNode) User(org.structr.web.entity.User) PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) SchemaMethod(org.structr.core.entity.SchemaMethod) FrameworkException(org.structr.common.error.FrameworkException) File(org.structr.web.entity.File) FrameworkException(org.structr.common.error.FrameworkException) Test(org.junit.Test)

Aggregations

SchemaMethod (org.structr.core.entity.SchemaMethod)24 Tx (org.structr.core.graph.Tx)17 FrameworkException (org.structr.common.error.FrameworkException)16 App (org.structr.core.app.App)10 StructrApp (org.structr.core.app.StructrApp)10 SchemaNode (org.structr.core.entity.SchemaNode)10 PropertyMap (org.structr.core.property.PropertyMap)10 Test (org.junit.Test)9 SchemaProperty (org.structr.core.entity.SchemaProperty)4 NodeAttribute (org.structr.core.graph.NodeAttribute)4 User (org.structr.web.entity.User)4 LinkedList (java.util.LinkedList)3 TreeMap (java.util.TreeMap)3 AbstractSchemaNode (org.structr.core.entity.AbstractSchemaNode)3 SchemaView (org.structr.core.entity.SchemaView)3 TreeSet (java.util.TreeSet)2 StructrTest (org.structr.common.StructrTest)2 ConfigurationProvider (org.structr.schema.ConfigurationProvider)2 URI (java.net.URI)1 DirectoryStream (java.nio.file.DirectoryStream)1