Search in sources :

Example 1 with SchemaProperty

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

the class AdvancedSchemaTest method test03InheritanceOfFileAttributesToSubclassOfImage.

@Test
public void test03InheritanceOfFileAttributesToSubclassOfImage() {
    try (final Tx tx = app.tx()) {
        createAdminUser();
        createResourceAccess("_schema", UiAuthenticator.AUTH_USER_GET);
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        SchemaNode fileNodeDef = app.nodeQuery(SchemaNode.class).andName("File").getFirst();
        SchemaProperty testFileProperty = app.create(SchemaProperty.class);
        final PropertyMap testFileProperties = new PropertyMap();
        testFileProperties.put(SchemaProperty.name, "testFile");
        testFileProperties.put(SchemaProperty.propertyType, "String");
        testFileProperties.put(SchemaProperty.schemaNode, fileNodeDef);
        testFileProperty.setProperties(testFileProperty.getSecurityContext(), testFileProperties);
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        // Create new schema node for dynamic class SubFile which extends File
        SchemaNode subFile = app.create(SchemaNode.class);
        final PropertyMap subFileProperties = new PropertyMap();
        subFileProperties.put(SchemaNode.name, "SubFile");
        subFileProperties.put(SchemaNode.extendsClass, "org.structr.dynamic.Image");
        subFile.setProperties(subFile.getSecurityContext(), subFileProperties);
        // Add String property "testSubFile" to new dynamic class
        SchemaProperty testFileProperty = app.create(SchemaProperty.class);
        final PropertyMap testFileProperties = new PropertyMap();
        testFileProperties.put(SchemaProperty.name, "testSubFile");
        testFileProperties.put(SchemaProperty.propertyType, "String");
        testFileProperties.put(SchemaProperty.schemaNode, subFile);
        testFileProperty.setProperties(testFileProperty.getSecurityContext(), testFileProperties);
        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).expect().statusCode(200).body("result", hasSize(count2 + 1)).body("result", Matchers.hasItem(Matchers.allOf(hasEntry("jsonName", "testFile"), hasEntry("declaringClass", "File")))).body("result", Matchers.hasItem(Matchers.allOf(hasEntry("jsonName", "testSubFile"), hasEntry("declaringClass", "SubFile")))).when().get("/_schema/SubFile/ui");
        tx.success();
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : SchemaNode(org.structr.core.entity.SchemaNode) SchemaProperty(org.structr.core.entity.SchemaProperty) PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) FrameworkException(org.structr.common.error.FrameworkException) FrontendTest(org.structr.web.basic.FrontendTest) ResourceAccessTest(org.structr.web.basic.ResourceAccessTest) Test(org.junit.Test)

Example 2 with SchemaProperty

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

the class AdvancedSchemaTest method test02InheritanceOfFileAttributesToSubclass.

@Test
public void test02InheritanceOfFileAttributesToSubclass() {
    try (final Tx tx = app.tx()) {
        createAdminUser();
        createResourceAccess("_schema", UiAuthenticator.AUTH_USER_GET);
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        SchemaNode fileNodeDef = app.nodeQuery(SchemaNode.class).andName("File").getFirst();
        SchemaProperty testFileProperty = app.create(SchemaProperty.class);
        final PropertyMap changedProperties = new PropertyMap();
        changedProperties.put(SchemaProperty.name, "testFile");
        changedProperties.put(SchemaProperty.propertyType, "String");
        changedProperties.put(SchemaProperty.schemaNode, fileNodeDef);
        testFileProperty.setProperties(testFileProperty.getSecurityContext(), changedProperties);
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        // Create new schema node for dynamic class SubFile which extends File
        SchemaNode subFile = app.create(SchemaNode.class);
        final PropertyMap subFileProperties = new PropertyMap();
        subFileProperties.put(SchemaNode.name, "SubFile");
        subFileProperties.put(SchemaNode.extendsClass, "org.structr.dynamic.File");
        subFile.setProperties(subFile.getSecurityContext(), subFileProperties);
        // Add String property "testSubFile" to new dynamic class
        SchemaProperty testFileProperty = app.create(SchemaProperty.class);
        final PropertyMap testFileProperties = new PropertyMap();
        testFileProperties.put(SchemaProperty.name, "testSubFile");
        testFileProperties.put(SchemaProperty.propertyType, "String");
        testFileProperties.put(SchemaProperty.schemaNode, subFile);
        testFileProperty.setProperties(testFileProperty.getSecurityContext(), testFileProperties);
        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).expect().statusCode(200).body("result", hasSize(count1 + 1)).body("result", Matchers.hasItem(Matchers.allOf(hasEntry("jsonName", "testFile"), hasEntry("declaringClass", "File")))).body("result", Matchers.hasItem(Matchers.allOf(hasEntry("jsonName", "testSubFile"), hasEntry("declaringClass", "SubFile")))).when().get("/_schema/SubFile/ui");
        tx.success();
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : SchemaNode(org.structr.core.entity.SchemaNode) SchemaProperty(org.structr.core.entity.SchemaProperty) PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) FrameworkException(org.structr.common.error.FrameworkException) FrontendTest(org.structr.web.basic.FrontendTest) ResourceAccessTest(org.structr.web.basic.ResourceAccessTest) Test(org.junit.Test)

Example 3 with SchemaProperty

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

the class StructrSchemaPropertiesPath method getDirectoryStream.

@Override
public DirectoryStream<Path> getDirectoryStream(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 SchemaProperty schemaProperty : schemaNode.getProperty(SchemaNode.schemaProperties)) {
                        nodes.add(new StructrSchemaPropertyPath(fs, StructrSchemaPropertiesPath.this, schemaNode, schemaProperty));
                    }
                    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) SchemaProperty(org.structr.core.entity.SchemaProperty) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) StructrPath(org.structr.files.ssh.filesystem.StructrPath) DirectoryStream(java.nio.file.DirectoryStream) LinkedList(java.util.LinkedList)

Example 4 with SchemaProperty

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

the class StructrSchemaPropertiesPath method resolveStructrPath.

@Override
public StructrPath resolveStructrPath(final String pathComponent) {
    final App app = StructrApp.getInstance(fs.getSecurityContext());
    StructrPath path = null;
    try (final Tx tx = app.tx()) {
        for (final SchemaProperty schemaProperty : schemaNode.getProperty(SchemaNode.schemaProperties)) {
            if (pathComponent.equals(schemaProperty.getName())) {
                path = new StructrSchemaPropertyPath(fs, StructrSchemaPropertiesPath.this, schemaNode, schemaProperty);
            }
        }
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
    return path;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) SchemaProperty(org.structr.core.entity.SchemaProperty) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) StructrPath(org.structr.files.ssh.filesystem.StructrPath)

Example 5 with SchemaProperty

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

the class SchemaHelper method extractProperties.

public static String extractProperties(final Schema entity, final Set<String> propertyNames, final Set<Validator> validators, final Set<String> compoundIndexKeys, final Set<String> enums, final Map<String, Set<String>> views, final List<String> propertyValidators, final ErrorBuffer errorBuffer) throws FrameworkException {
    final PropertyContainer propertyContainer = entity.getPropertyContainer();
    final StringBuilder src = new StringBuilder();
    // output property source code and collect views
    for (String propertyName : SchemaHelper.getProperties(propertyContainer)) {
        if (!propertyName.startsWith("__") && propertyContainer.hasProperty(propertyName)) {
            String rawType = propertyContainer.getProperty(propertyName).toString();
            PropertySourceGenerator parser = SchemaHelper.getSourceGenerator(errorBuffer, entity.getClassName(), new StringBasedPropertyDefinition(propertyName, rawType));
            if (parser != null) {
                // migrate properties
                if (entity instanceof AbstractSchemaNode) {
                    parser.createSchemaPropertyNode((AbstractSchemaNode) entity, propertyName);
                }
            }
        }
    }
    final List<SchemaProperty> schemaProperties = entity.getSchemaProperties();
    if (schemaProperties != null) {
        for (final SchemaProperty schemaProperty : schemaProperties) {
            String propertyName = schemaProperty.getPropertyName();
            String oldName = propertyName;
            int count = 1;
            if (propertyNames.contains(propertyName)) {
                while (propertyNames.contains(propertyName)) {
                    propertyName = propertyName + count++;
                }
                logger.warn("Property name {} already present in type {}, renaming to {}", oldName, entity.getClassName(), propertyName);
                schemaProperty.setProperty(SchemaProperty.name, propertyName);
            }
            propertyNames.add(propertyName);
            if (!schemaProperty.getProperty(SchemaProperty.isBuiltinProperty)) {
                // migrate property source
                if (Type.Function.equals(schemaProperty.getPropertyType())) {
                    // Note: This is a temporary migration from the old format to the new readFunction property
                    final String format = schemaProperty.getFormat();
                    if (format != null) {
                        try {
                            schemaProperty.setProperty(SchemaProperty.readFunction, format);
                            schemaProperty.setProperty(SchemaProperty.format, null);
                        } catch (FrameworkException ex) {
                            logger.warn("", ex);
                        }
                    }
                }
                final PropertySourceGenerator parser = SchemaHelper.getSourceGenerator(errorBuffer, entity.getClassName(), schemaProperty);
                if (parser != null) {
                    // add property name to set for later use
                    propertyNames.add(schemaProperty.getPropertyName());
                    // append created source from parser
                    parser.getPropertySource(src, entity);
                    // register global elements created by parser
                    validators.addAll(parser.getGlobalValidators());
                    compoundIndexKeys.addAll(parser.getCompoundIndexKeys());
                    enums.addAll(parser.getEnumDefinitions());
                    // built-in schema properties are configured manually
                    if (!schemaProperty.isPartOfBuiltInSchema()) {
                        // register property in default view
                        addPropertyToView(PropertyView.Ui, propertyName, views);
                    }
                }
            }
            final String[] propertyValidatorsArray = schemaProperty.getProperty(SchemaProperty.validators);
            if (propertyValidatorsArray != null) {
                propertyValidators.addAll(Arrays.asList(propertyValidatorsArray));
            }
        }
    }
    return src.toString();
}
Also used : PropertyContainer(org.structr.api.graph.PropertyContainer) SchemaProperty(org.structr.core.entity.SchemaProperty) FrameworkException(org.structr.common.error.FrameworkException) AbstractSchemaNode(org.structr.core.entity.AbstractSchemaNode) PropertySourceGenerator(org.structr.schema.parser.PropertySourceGenerator) PasswordPropertySourceGenerator(org.structr.schema.parser.PasswordPropertySourceGenerator) StringPropertySourceGenerator(org.structr.schema.parser.StringPropertySourceGenerator) StringBasedPropertyDefinition(org.structr.schema.parser.StringBasedPropertyDefinition)

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