Search in sources :

Example 6 with SchemaMethod

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

the class StructrSchemaMethodsPath method getDirectoryStream.

@Override
public DirectoryStream<Path> getDirectoryStream(final DirectoryStream.Filter<? super Path> filter) {
    if (schemaNode != null) {
        return new DirectoryStream() {

            boolean closed = false;

            @Override
            public Iterator iterator() {
                final App app = StructrApp.getInstance(fs.getSecurityContext());
                final List<StructrPath> nodes = new LinkedList<>();
                try (final Tx tx = app.tx()) {
                    for (final SchemaMethod schemaMethod : schemaNode.getProperty(SchemaNode.schemaMethods)) {
                        // schema methods have a virtual file name so that external editors don't get confused
                        String name = schemaMethod.getProperty(SchemaMethod.virtualFileName);
                        if (name == null) {
                            // no virtual file name set, use real name
                            name = schemaMethod.getName();
                        }
                        nodes.add(new StructrSchemaMethodPath(fs, StructrSchemaMethodsPath.this, schemaNode, name));
                    }
                    tx.success();
                } catch (FrameworkException fex) {
                    logger.warn("", fex);
                }
                return nodes.iterator();
            }

            @Override
            public void close() throws IOException {
                closed = true;
            }
        };
    }
    return null;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) SchemaMethod(org.structr.core.entity.SchemaMethod) FrameworkException(org.structr.common.error.FrameworkException) StructrPath(org.structr.files.ssh.filesystem.StructrPath) DirectoryStream(java.nio.file.DirectoryStream) LinkedList(java.util.LinkedList)

Example 7 with SchemaMethod

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

the class SchemaHelper method extractMethods.

public static void extractMethods(final AbstractSchemaNode entity, final Map<String, List<ActionEntry>> actions) throws FrameworkException {
    final PropertyContainer propertyContainer = entity.getPropertyContainer();
    for (final String rawActionName : getActions(propertyContainer)) {
        if (propertyContainer.hasProperty(rawActionName)) {
            final String value = propertyContainer.getProperty(rawActionName).toString();
            if (entity instanceof AbstractSchemaNode) {
                final AbstractSchemaNode schemaNode = (AbstractSchemaNode) entity;
                final App app = StructrApp.getInstance();
                final String methodName = rawActionName.substring(3);
                if (app.nodeQuery(SchemaMethod.class).and(SchemaMethod.schemaNode, schemaNode).and(AbstractNode.name, methodName).getFirst() == null) {
                    app.create(SchemaMethod.class, new NodeAttribute<>(SchemaMethod.schemaNode, schemaNode), new NodeAttribute<>(SchemaMethod.name, methodName), new NodeAttribute<>(SchemaMethod.source, value));
                    schemaNode.removeProperty(new StringProperty(rawActionName));
                }
            }
        }
    }
    final List<SchemaMethod> schemaMethods = entity.getSchemaMethods();
    if (schemaMethods != null) {
        for (final SchemaMethod schemaMethod : schemaMethods) {
            final ActionEntry entry = schemaMethod.getActionEntry(entity);
            final String name = entry.getName();
            List<ActionEntry> actionList = actions.get(name);
            if (actionList == null) {
                actionList = new LinkedList<>();
                actions.put(name, actionList);
            }
            actionList.add(entry);
            Collections.sort(actionList);
        }
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) PropertyContainer(org.structr.api.graph.PropertyContainer) ActionEntry(org.structr.schema.action.ActionEntry) SchemaMethod(org.structr.core.entity.SchemaMethod) AbstractSchemaNode(org.structr.core.entity.AbstractSchemaNode) StringProperty(org.structr.core.property.StringProperty)

Example 8 with SchemaMethod

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

the class StructrGlobalSchemaMethods method deserialize.

void deserialize(final App app) throws FrameworkException {
    try (final Tx tx = app.tx()) {
        for (final SchemaMethod schemaMethod : app.nodeQuery(SchemaMethod.class).and(SchemaMethod.schemaNode, null).sort(SchemaMethod.name).getAsList()) {
            final Map<String, Object> entry = new TreeMap<>();
            globalMethods.add(entry);
            entry.put("name", schemaMethod.getProperty(SchemaMethod.name));
            entry.put("comment", schemaMethod.getProperty(SchemaMethod.comment));
            entry.put("source", schemaMethod.getProperty(SchemaMethod.source));
            entry.put("virtualFileName", schemaMethod.getProperty(SchemaMethod.virtualFileName));
            entry.put("visibleToAuthenticatedUsers", schemaMethod.getProperty(SchemaMethod.visibleToAuthenticatedUsers));
            entry.put("visibleToPublicUsers", schemaMethod.getProperty(SchemaMethod.visibleToPublicUsers));
        }
        tx.success();
    }
}
Also used : Tx(org.structr.core.graph.Tx) SchemaMethod(org.structr.core.entity.SchemaMethod) TreeMap(java.util.TreeMap)

Example 9 with SchemaMethod

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

the class Actions method callWithSecurityContext.

public static Object callWithSecurityContext(final String key, final SecurityContext securityContext, final Map<String, Object> parameters) throws FrameworkException, UnlicensedException {
    final App app = StructrApp.getInstance(securityContext);
    // we might want to introduce caching here at some point in the future..
    // Cache can be invalidated when the schema is rebuilt for example..
    final List<SchemaMethod> methods = app.nodeQuery(SchemaMethod.class).andName(key).getAsList();
    if (methods.isEmpty()) {
        if (!NOTIFICATION_LOGIN.equals(key) && !NOTIFICATION_LOGOUT.equals(key)) {
            logger.warn("Tried to call method {} but no SchemaMethod entity was found.", key);
        }
    } else {
        for (final SchemaMethod method : methods) {
            // only call methods that are NOT part of a schema node
            final AbstractSchemaNode entity = method.getProperty(SchemaMethod.schemaNode);
            if (entity == null) {
                final String source = method.getProperty(SchemaMethod.source);
                if (source != null) {
                    return Actions.execute(securityContext, null, "${" + source + "}", parameters, method.getName());
                } else {
                    logger.warn("Schema method {} has no source code, will NOT be executed.", key);
                }
            } else {
                logger.warn("Schema method {} is attached to an entity, will NOT be executed.", key);
            }
        }
    }
    return null;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) SchemaMethod(org.structr.core.entity.SchemaMethod) AbstractSchemaNode(org.structr.core.entity.AbstractSchemaNode)

Example 10 with SchemaMethod

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

the class GlobalSchemaMethodResource method checkAndConfigure.

@Override
public boolean checkAndConfigure(final String part, final SecurityContext securityContext, final HttpServletRequest request) {
    this.securityContext = securityContext;
    final App app = StructrApp.getInstance(securityContext);
    try (final Tx tx = app.tx()) {
        final List<SchemaMethod> methods = app.nodeQuery(SchemaMethod.class).andName(part).and(SchemaMethod.schemaNode, null).getAsList();
        tx.success();
        if (!methods.isEmpty()) {
            this.methodName = part;
            return true;
        }
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    return false;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) SchemaMethod(org.structr.core.entity.SchemaMethod) FrameworkException(org.structr.common.error.FrameworkException)

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