Search in sources :

Example 1 with Validator

use of org.structr.schema.parser.Validator in project structr by structr.

the class SchemaHelper method getSource.

public static String getSource(final AbstractSchemaNode schemaNode, final ErrorBuffer errorBuffer) throws FrameworkException {
    final Collection<StructrModule> modules = StructrApp.getConfiguration().getModules().values();
    final App app = StructrApp.getInstance();
    final Map<String, List<ActionEntry>> methods = new LinkedHashMap<>();
    final Map<String, Set<String>> viewProperties = new LinkedHashMap<>();
    final List<String> propertyValidators = new LinkedList<>();
    final Set<String> existingPropertyNames = new LinkedHashSet<>();
    final Set<String> compoundIndexKeys = new LinkedHashSet<>();
    final Set<String> propertyNames = new LinkedHashSet<>();
    final Set<String> relationshipPropertyNames = new LinkedHashSet<>();
    final Set<Validator> validators = new LinkedHashSet<>();
    final Set<String> implementedInterfaces = new LinkedHashSet<>();
    final List<String> importStatements = new LinkedList<>();
    final Set<String> enums = new LinkedHashSet<>();
    final StringBuilder src = new StringBuilder();
    final StringBuilder mixinCodeBuffer = new StringBuilder();
    final Class baseType = AbstractNode.class;
    final String _className = schemaNode.getProperty(SchemaNode.name);
    final String _extendsClass = schemaNode.getProperty(SchemaNode.extendsClass);
    final String superClass = _extendsClass != null ? _extendsClass : baseType.getSimpleName();
    final boolean extendsAbstractNode = _extendsClass == null;
    // check superclass
    if (!extendsAbstractNode && !superClass.startsWith("org.structr.dynamic.") && !SchemaHelper.hasType(superClass)) {
        // we can only detect if a type is missing that is usually provided by a module; we
        // can not detect whether a dynamic type is missing because those are only available
        // after compiling the whole set of schema nodes
        logger.warn("Dynamic type {} cannot be used, superclass {} not defined.", schemaNode.getName(), superClass);
        return null;
    }
    // import mixins, check that all types exist and return null otherwise (causing this class to be ignored)
    SchemaHelper.collectInterfaces(schemaNode, implementedInterfaces);
    // package name
    src.append("package org.structr.dynamic;\n\n");
    // include import statements from mixins
    SchemaHelper.formatImportStatements(schemaNode, src, baseType, importStatements);
    if (schemaNode.getProperty(SchemaNode.isInterface)) {
        // create interface
        src.append("public interface ");
        src.append(_className);
        // output implemented interfaces
        if (!implementedInterfaces.isEmpty()) {
            src.append(" extends ");
            src.append(StringUtils.join(implementedInterfaces, ", "));
        }
    } else {
        // create class
        src.append("public ");
        if (schemaNode.getProperty(SchemaNode.isAbstract)) {
            src.append("abstract ");
        }
        src.append("class ");
        src.append(_className);
        src.append(" extends ");
        src.append(superClass);
        // output implemented interfaces
        if (!implementedInterfaces.isEmpty()) {
            src.append(" implements ");
            src.append(StringUtils.join(implementedInterfaces, ", "));
        }
    }
    src.append(" {\n\n");
    // output related node definitions, collect property views
    for (final SchemaRelationshipNode outRel : schemaNode.getProperty(SchemaNode.relatedTo)) {
        final String propertyName = outRel.getPropertyName(_className, existingPropertyNames, true);
        propertyNames.add(propertyName);
        src.append(outRel.getPropertySource(propertyName, true));
        // schema changes are expected to be added to "ui" view.
        if (!outRel.getProperty(SchemaRelationshipNode.isPartOfBuiltInSchema)) {
            addPropertyToView(PropertyView.Ui, propertyName, viewProperties);
        }
        relationshipPropertyNames.add(propertyName);
    }
    // output related node definitions, collect property views
    for (final SchemaRelationshipNode inRel : schemaNode.getProperty(SchemaNode.relatedFrom)) {
        final String propertyName = inRel.getPropertyName(_className, existingPropertyNames, false);
        propertyNames.add(propertyName);
        src.append(inRel.getPropertySource(propertyName, false));
        // schema changes are expected to be added to "ui" view.
        if (!inRel.getProperty(SchemaRelationshipNode.isPartOfBuiltInSchema)) {
            SchemaHelper.addPropertyToView(PropertyView.Ui, propertyName, viewProperties);
        }
        relationshipPropertyNames.add(propertyName);
    }
    src.append(SchemaHelper.extractProperties(schemaNode, propertyNames, validators, compoundIndexKeys, enums, viewProperties, propertyValidators, errorBuffer));
    SchemaHelper.extractViews(schemaNode, viewProperties, relationshipPropertyNames, errorBuffer);
    SchemaHelper.extractMethods(schemaNode, methods);
    // output possible enum definitions
    for (final String enumDefition : enums) {
        src.append(enumDefition);
    }
    for (Entry<String, Set<String>> entry : viewProperties.entrySet()) {
        final String viewName = entry.getKey();
        final Set<String> view = entry.getValue();
        if (!view.isEmpty()) {
            schemaNode.addDynamicView(viewName);
            SchemaHelper.formatView(src, _className, viewName, viewName, view);
        }
    }
    if (schemaNode.getProperty(defaultSortKey) != null) {
        String order = schemaNode.getProperty(defaultSortOrder);
        if (order == null || "desc".equals(order)) {
            order = "GraphObjectComparator.DESCENDING";
        } else {
            order = "GraphObjectComparator.ASCENDING";
        }
        src.append("\n\t@Override\n");
        src.append("\tpublic PropertyKey getDefaultSortKey() {\n");
        src.append("\t\treturn ").append(schemaNode.getProperty(defaultSortKey)).append("Property;\n");
        src.append("\t}\n");
        src.append("\n\t@Override\n");
        src.append("\tpublic String getDefaultSortOrder() {\n");
        src.append("\t\treturn ").append(order).append(";\n");
        src.append("\t}\n");
    }
    SchemaHelper.formatValidators(src, validators, compoundIndexKeys, extendsAbstractNode, propertyValidators);
    SchemaHelper.formatMethods(schemaNode, src, methods, implementedInterfaces);
    // insert dynamic code here
    src.append(mixinCodeBuffer);
    // insert source code from module
    for (final StructrModule module : modules) {
        module.insertSourceCode(schemaNode, src);
    }
    src.append("}\n");
    return src.toString();
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) AbstractNode(org.structr.core.entity.AbstractNode) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) StructrModule(org.structr.module.StructrModule) SchemaRelationshipNode(org.structr.core.entity.SchemaRelationshipNode) List(java.util.List) LinkedList(java.util.LinkedList) Validator(org.structr.schema.parser.Validator)

Example 2 with Validator

use of org.structr.schema.parser.Validator in project structr by structr.

the class SchemaHelper method formatValidators.

public static void formatValidators(final StringBuilder src, final Set<Validator> validators, final Set<String> compoundIndexKeys, final boolean extendsAbstractNode, final List<String> propertyValidators) {
    if (!validators.isEmpty() || !compoundIndexKeys.isEmpty()) {
        src.append("\n\t@Override\n");
        src.append("\tpublic boolean isValid(final ErrorBuffer errorBuffer) {\n\n");
        src.append("\t\tboolean valid = super.isValid(errorBuffer);\n\n");
        for (final Validator validator : validators) {
            src.append("\t\tvalid &= ").append(validator.getSource("this", true)).append(";\n");
        }
        if (!compoundIndexKeys.isEmpty()) {
            src.append("\t\tvalid &= ValidationHelper.areValidCompoundUniqueProperties(this, errorBuffer, ").append(StringUtils.join(compoundIndexKeys, ", ")).append(");\n");
        }
        for (final String propertyValidator : propertyValidators) {
            src.append("\t\tvalid &= new ");
            src.append(propertyValidator);
            src.append("().isValid(this, errorBuffer);\n");
        }
        src.append("\n\t\treturn valid;\n");
        src.append("\t}\n");
    }
}
Also used : Validator(org.structr.schema.parser.Validator)

Aggregations

Validator (org.structr.schema.parser.Validator)2 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Set (java.util.Set)1 App (org.structr.core.app.App)1 StructrApp (org.structr.core.app.StructrApp)1 AbstractNode (org.structr.core.entity.AbstractNode)1 SchemaRelationshipNode (org.structr.core.entity.SchemaRelationshipNode)1 StructrModule (org.structr.module.StructrModule)1