Search in sources :

Example 1 with ActionEntry

use of org.structr.schema.action.ActionEntry in project structr by structr.

the class SchemaHelper method formatCreationCallback.

public static void formatCreationCallback(final AbstractSchemaNode schemaNode, final StringBuilder src, final String name, final List<ActionEntry> actionList) {
    src.append("\n\t@Override\n");
    src.append("\tpublic void ");
    src.append(name);
    src.append("(final SecurityContext arg0, final ErrorBuffer arg1) throws FrameworkException {\n\n");
    src.append("\t\tsuper.");
    src.append(name);
    src.append("(arg0, arg1);\n\n");
    for (final ActionEntry action : actionList) {
        src.append("\t\t").append(action.getSource("this")).append(";\n");
    }
    src.append("\t}\n");
}
Also used : ActionEntry(org.structr.schema.action.ActionEntry)

Example 2 with ActionEntry

use of org.structr.schema.action.ActionEntry 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 3 with ActionEntry

use of org.structr.schema.action.ActionEntry in project structr by structr.

the class SchemaMethod method getActionEntry.

public ActionEntry getActionEntry(final AbstractSchemaNode schemaEntity) throws FrameworkException {
    final ActionEntry entry = new ActionEntry("___" + SchemaHelper.cleanPropertyName(getProperty(AbstractNode.name)), getProperty(SchemaMethod.source), getProperty(SchemaMethod.codeType));
    final List<SchemaMethodParameter> params = getProperty(parameters);
    // Parameters must be sorted by index
    Collections.sort(params, new GraphObjectComparator(SchemaMethodParameter.index, false));
    for (final SchemaMethodParameter parameter : params) {
        entry.addParameter(parameter.getParameterType(), parameter.getName());
    }
    entry.setReturnType(getProperty(returnType));
    entry.setCallSuper(getProperty(callSuper));
    final String[] _exceptions = getProperty(exceptions);
    if (_exceptions != null) {
        for (final String exception : _exceptions) {
            entry.addException(exception);
        }
    }
    // check for overridden methods and determine method signature etc. from superclass(es)
    if (getProperty(overridesExisting)) {
        determineSignature(schemaEntity, entry, getProperty(name));
    }
    // check for overridden methods and determine method signature etc. from superclass(es)
    if (getProperty(doExport)) {
        entry.setDoExport(true);
    }
    return entry;
}
Also used : ActionEntry(org.structr.schema.action.ActionEntry) GraphObjectComparator(org.structr.common.GraphObjectComparator)

Example 4 with ActionEntry

use of org.structr.schema.action.ActionEntry in project structr by structr.

the class SchemaMethod method determineSignature.

private void determineSignature(final AbstractSchemaNode schemaEntity, final ActionEntry entry, final String methodName) throws FrameworkException {
    final App app = StructrApp.getInstance();
    final Set<String> visitedTypes = new LinkedHashSet<>();
    final Queue<String> typeQueue = new LinkedList<>();
    final String structrPackage = "org.structr.dynamic.";
    // initial type
    addType(typeQueue, schemaEntity);
    while (!typeQueue.isEmpty()) {
        final String typeName = typeQueue.poll();
        String shortTypeName = typeName;
        if (typeName != null && !visitedTypes.contains(typeName)) {
            visitedTypes.add(typeName);
            if (typeName.startsWith(structrPackage)) {
                shortTypeName = typeName.substring(structrPackage.length());
            }
            // try to find schema node for the given type
            final SchemaNode typeNode = app.nodeQuery(SchemaNode.class).andName(shortTypeName).getFirst();
            if (typeNode != null && !typeNode.equals(schemaEntity)) {
                // try to identify overridden schema method from database
                final SchemaMethod superMethod = app.nodeQuery(SchemaMethod.class).and(SchemaMethod.schemaNode, typeNode).and(SchemaMethod.name, methodName).getFirst();
                if (superMethod != null) {
                    final ActionEntry superEntry = superMethod.getActionEntry(typeNode);
                    entry.copy(superEntry);
                    // done
                    return;
                }
                // next type in queue
                addType(typeQueue, typeNode);
            } else {
                // no schema node for the given type found, try internal types
                final Class internalType = SchemaHelper.classForName(typeName);
                if (internalType != null) {
                    if (getSignature(internalType, methodName, entry)) {
                        return;
                    }
                    final Class superclass = internalType.getSuperclass();
                    if (superclass != null) {
                        // examine superclass as well
                        typeQueue.add(superclass.getName());
                        // collect interfaces
                        for (final Class iface : internalType.getInterfaces()) {
                            typeQueue.add(iface.getName());
                        }
                    }
                }
            }
        }
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) LinkedHashSet(java.util.LinkedHashSet) ActionEntry(org.structr.schema.action.ActionEntry) LinkedList(java.util.LinkedList)

Example 5 with ActionEntry

use of org.structr.schema.action.ActionEntry in project structr by structr.

the class SchemaHelper method formatCustomMethods.

public static void formatCustomMethods(final StringBuilder src, final List<ActionEntry> actionList) {
    for (final ActionEntry action : actionList) {
        if (Actions.Type.Custom.equals(action.getType())) {
            formatActiveAction(src, action);
        } else {
            final String source = action.getSource("this", true);
            final String returnType = action.getReturnType();
            final Map<String, String> parameters = action.getParameters();
            if (returnType != null && parameters != null) {
                src.append("\n");
                if (action.overrides()) {
                    src.append("\t@Override\n");
                }
                if (action.doExport()) {
                    src.append("\t@Export\n");
                }
                src.append("\tpublic ");
                src.append(returnType);
                src.append(" ");
                src.append(action.getName());
                src.append("(");
                // output parameters
                for (final Iterator<Entry<String, String>> it = parameters.entrySet().iterator(); it.hasNext(); ) {
                    final Entry<String, String> entry = it.next();
                    src.append("final ");
                    src.append(entry.getValue());
                    src.append(" ");
                    src.append(entry.getKey());
                    if (it.hasNext()) {
                        src.append(", ");
                    }
                }
                src.append(")");
                final List<String> exceptions = action.getExceptions();
                if (!exceptions.isEmpty()) {
                    src.append(" throws ");
                    src.append(StringUtils.join(exceptions, ", "));
                }
                src.append(" {\n");
            } else {
                src.append("\n\t@Export\n");
                src.append("\tpublic java.lang.Object ");
                src.append(action.getName());
                src.append("(final java.util.Map<java.lang.String, java.lang.Object> parameters) throws FrameworkException {\n\n");
            }
            if (action.callSuper()) {
                src.append("\n\t\t// call super\n");
                src.append("\t\tsuper.");
                src.append(action.getName());
                src.append("(");
                src.append(StringUtils.join(parameters.keySet(), ", "));
                src.append(");\n\n");
            }
            if (StringUtils.isNotBlank(source)) {
                src.append("\t\t");
                src.append(source);
                final String trimmed = source.trim();
                if (!trimmed.endsWith(";") && !trimmed.endsWith("}")) {
                    src.append(";");
                }
                src.append("\n");
            }
            if (!"void".equals(returnType) && (StringUtils.isBlank(source) || Actions.Type.Custom.equals(action.getType()))) {
                src.append("\t\treturn null;\n");
            }
            src.append("\t}\n");
        }
    }
}
Also used : ActionEntry(org.structr.schema.action.ActionEntry) Entry(java.util.Map.Entry) ActionEntry(org.structr.schema.action.ActionEntry)

Aggregations

ActionEntry (org.structr.schema.action.ActionEntry)7 App (org.structr.core.app.App)2 StructrApp (org.structr.core.app.StructrApp)2 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 Entry (java.util.Map.Entry)1 PropertyContainer (org.structr.api.graph.PropertyContainer)1 GraphObjectComparator (org.structr.common.GraphObjectComparator)1 AbstractSchemaNode (org.structr.core.entity.AbstractSchemaNode)1 SchemaMethod (org.structr.core.entity.SchemaMethod)1 StringProperty (org.structr.core.property.StringProperty)1