Search in sources :

Example 1 with PropertyContainer

use of org.structr.api.graph.PropertyContainer 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)

Example 2 with PropertyContainer

use of org.structr.api.graph.PropertyContainer 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 PropertyContainer

use of org.structr.api.graph.PropertyContainer in project structr by structr.

the class SchemaHelper method extractViews.

public static void extractViews(final Schema entity, final Map<String, Set<String>> views, final Set<String> relPropertyNames, final ErrorBuffer errorBuffer) throws FrameworkException {
    final PropertyContainer propertyContainer = entity.getPropertyContainer();
    final ConfigurationProvider config = StructrApp.getConfiguration();
    Class superClass = config.getNodeEntityClass(entity.getSuperclassName());
    if (superClass == null) {
        superClass = config.getRelationshipEntityClass(entity.getSuperclassName());
    }
    if (superClass == null) {
        superClass = AbstractNode.class;
    }
    for (final String rawViewName : getViews(propertyContainer)) {
        if (!rawViewName.startsWith("___") && propertyContainer.hasProperty(rawViewName)) {
            final String value = propertyContainer.getProperty(rawViewName).toString();
            final String[] parts = value.split("[,\\s]+");
            final String viewName = rawViewName.substring(2);
            if (entity instanceof AbstractSchemaNode) {
                final List<String> nonGraphProperties = new LinkedList<>();
                final List<SchemaProperty> properties = new LinkedList<>();
                final AbstractSchemaNode schemaNode = (AbstractSchemaNode) entity;
                final App app = StructrApp.getInstance();
                if (app.nodeQuery(SchemaView.class).and(SchemaView.schemaNode, schemaNode).and(AbstractNode.name, viewName).getFirst() == null) {
                    // add parts to view, overrides defaults (because of clear() above)
                    for (int i = 0; i < parts.length; i++) {
                        String propertyName = parts[i].trim();
                        while (propertyName.startsWith("_")) {
                            propertyName = propertyName.substring(1);
                        }
                        // append this as a workaround to include remote properties
                        if (propertyName.endsWith("Property")) {
                            propertyName = propertyName.substring(0, propertyName.length() - "Property".length());
                        }
                        final SchemaProperty propertyNode = app.nodeQuery(SchemaProperty.class).and(SchemaProperty.schemaNode, schemaNode).andName(propertyName).getFirst();
                        if (propertyNode != null) {
                            properties.add(propertyNode);
                        } else {
                            nonGraphProperties.add(propertyName);
                        }
                    }
                    app.create(SchemaView.class, new NodeAttribute<>(SchemaView.schemaNode, schemaNode), new NodeAttribute<>(SchemaView.schemaProperties, properties), new NodeAttribute<>(SchemaView.name, viewName), new NodeAttribute<>(SchemaView.nonGraphProperties, StringUtils.join(nonGraphProperties, ",")));
                    schemaNode.removeProperty(new StringProperty(rawViewName));
                }
            }
        }
    }
    final List<SchemaView> schemaViews = entity.getSchemaViews();
    if (schemaViews != null) {
        for (final SchemaView schemaView : schemaViews) {
            final String nonGraphProperties = schemaView.getProperty(SchemaView.nonGraphProperties);
            final String viewName = schemaView.getName();
            // clear view before filling it again
            Set<String> view = views.get(viewName);
            if (view == null) {
                view = new LinkedHashSet<>();
                views.put(viewName, view);
            }
            final List<SchemaProperty> schemaProperties = schemaView.getProperty(SchemaView.schemaProperties);
            for (final SchemaProperty property : schemaProperties) {
                if (property.getProperty(SchemaProperty.isBuiltinProperty) && !property.getProperty(SchemaProperty.isDynamic)) {
                    view.add(SchemaHelper.cleanPropertyName(property.getPropertyName()));
                } else {
                    view.add(SchemaHelper.cleanPropertyName(property.getPropertyName() + "Property"));
                }
            }
            // add properties that are not part of the graph
            if (StringUtils.isNotBlank(nonGraphProperties)) {
                for (final String propertyName : nonGraphProperties.split("[, ]+")) {
                    if (SchemaHelper.isDynamic(entity.getClassName(), propertyName)) {
                        view.add(SchemaHelper.cleanPropertyName(propertyName + "Property"));
                    } else if (relPropertyNames.contains(propertyName)) {
                        view.add(SchemaHelper.cleanPropertyName(propertyName) + "Property");
                    } else if (basePropertyNames.contains(propertyName)) {
                        view.add(SchemaHelper.cleanPropertyName(propertyName));
                    } else {
                        logger.warn("Unknown property {} in non-graph properties, ignoring.", propertyName);
                        SchemaHelper.isDynamic(entity.getClassName(), propertyName);
                    }
                }
            }
            final String order = schemaView.getProperty(SchemaView.sortOrder);
            if (order != null) {
                applySortOrder(view, order);
            }
        }
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) PropertyContainer(org.structr.api.graph.PropertyContainer) SchemaProperty(org.structr.core.entity.SchemaProperty) StringProperty(org.structr.core.property.StringProperty) LinkedList(java.util.LinkedList) SchemaView(org.structr.core.entity.SchemaView) AbstractSchemaNode(org.structr.core.entity.AbstractSchemaNode)

Example 4 with PropertyContainer

use of org.structr.api.graph.PropertyContainer in project structr by structr.

the class SecurityDelegate method setAllowed.

private static void setAllowed(final RelationshipInterface graphObject, final PropertyKey<String[]> key, final String[] allowed) {
    if (allowed.length == 0) {
        StructrApp.getInstance().delete((RelationshipInterface) graphObject);
    } else {
        final PropertyContainer propertyContainer = graphObject.getPropertyContainer();
        propertyContainer.setProperty(key.dbName(), allowed);
    }
}
Also used : PropertyContainer(org.structr.api.graph.PropertyContainer)

Example 5 with PropertyContainer

use of org.structr.api.graph.PropertyContainer in project structr by structr.

the class SyncCommand method importDatabase.

private static void importDatabase(final DatabaseService graphDb, final SecurityContext securityContext, final ZipInputStream zis, boolean doValidation, final Long batchSize) throws FrameworkException, IOException {
    final App app = StructrApp.getInstance();
    final DataInputStream dis = new DataInputStream(new BufferedInputStream(zis));
    final long internalBatchSize = batchSize != null ? batchSize : 200;
    final String uuidPropertyName = GraphObject.id.dbName();
    final Map<String, Node> uuidMap = new LinkedHashMap<>();
    final Set<Long> deletedNodes = new HashSet<>();
    double t0 = System.nanoTime();
    PropertyContainer currentObject = null;
    String currentKey = null;
    boolean finished = false;
    long totalNodeCount = 0;
    long totalRelCount = 0;
    do {
        try (final Tx tx = app.tx(doValidation)) {
            final List<Relationship> rels = new LinkedList<>();
            final List<Node> nodes = new LinkedList<>();
            long nodeCount = 0;
            long relCount = 0;
            do {
                try {
                    // store current position
                    dis.mark(4);
                    // read one byte
                    byte objectType = dis.readByte();
                    // skip newlines
                    if (objectType == '\n') {
                        continue;
                    }
                    if (objectType == 'N') {
                        // break loop after 200 objects, commit and restart afterwards
                        if (nodeCount + relCount >= internalBatchSize) {
                            dis.reset();
                            break;
                        }
                        currentObject = graphDb.createNode(Collections.EMPTY_SET, Collections.EMPTY_MAP);
                        nodeCount++;
                        // store for later use
                        nodes.add((Node) currentObject);
                    } else if (objectType == 'R') {
                        // break look after 200 objects, commit and restart afterwards
                        if (nodeCount + relCount >= internalBatchSize) {
                            dis.reset();
                            break;
                        }
                        String startId = (String) deserialize(dis);
                        String endId = (String) deserialize(dis);
                        String relTypeName = (String) deserialize(dis);
                        Node endNode = uuidMap.get(endId);
                        Node startNode = uuidMap.get(startId);
                        if (startNode != null && endNode != null) {
                            if (deletedNodes.contains(startNode.getId()) || deletedNodes.contains(endNode.getId())) {
                                System.out.println("NOT creating relationship between deleted nodes..");
                                currentObject = null;
                                currentKey = null;
                            } else {
                                RelationshipType relType = RelationshipType.forName(relTypeName);
                                currentObject = startNode.createRelationshipTo(endNode, relType);
                                // store for later use
                                rels.add((Relationship) currentObject);
                                relCount++;
                            }
                        } else {
                            System.out.println("NOT creating relationship of type " + relTypeName + ", start: " + startId + ", end: " + endId);
                            currentObject = null;
                            currentKey = null;
                        }
                    } else {
                        // reset if not at the beginning of a line
                        dis.reset();
                        if (currentKey == null) {
                            try {
                                currentKey = (String) deserialize(dis);
                            } catch (Throwable t) {
                                logger.warn("", t);
                            }
                        } else {
                            final Object obj = deserialize(dis);
                            if (obj != null && currentObject != null) {
                                if (uuidPropertyName.equals(currentKey) && currentObject instanceof Node) {
                                    final String uuid = (String) obj;
                                    uuidMap.put(uuid, (Node) currentObject);
                                }
                                if (currentKey.length() != 0) {
                                    // store object in DB
                                    currentObject.setProperty(currentKey, obj);
                                    // set type label
                                    if (currentObject instanceof Node && NodeInterface.type.dbName().equals(currentKey)) {
                                        ((Node) currentObject).addLabel(graphDb.forName(Label.class, (String) obj));
                                    }
                                } else {
                                    logger.error("Invalid property key for value {}, ignoring", obj);
                                }
                            } else {
                                logger.warn("No current object to store property in.");
                            }
                            currentKey = null;
                        }
                    }
                } catch (EOFException eofex) {
                    finished = true;
                }
            } while (!finished);
            totalNodeCount += nodeCount;
            totalRelCount += relCount;
            logger.info("Imported {} nodes and {} rels, committing transaction..", new Object[] { totalNodeCount, totalRelCount });
            tx.success();
        }
    } while (!finished);
    // build schema
    try (final Tx tx = app.tx()) {
        SchemaHelper.reloadSchema(new ErrorBuffer(), securityContext.getSessionId());
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
    }
    final Map<String, Object> params = new HashMap<>();
    params.put("removeUnused", false);
    // set correct labels after schema has been compiled
    app.command(BulkCreateLabelsCommand.class).execute(params);
    double t1 = System.nanoTime();
    double time = ((t1 - t0) / 1000000000.0);
    DecimalFormat decimalFormat = new DecimalFormat("0.000000000", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
    logger.info("Import done in {} s", decimalFormat.format(time));
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) PropertyContainer(org.structr.api.graph.PropertyContainer) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Node(org.structr.api.graph.Node) AbstractNode(org.structr.core.entity.AbstractNode) AbstractSchemaNode(org.structr.core.entity.AbstractSchemaNode) DecimalFormat(java.text.DecimalFormat) RelationshipType(org.structr.api.graph.RelationshipType) LinkedHashMap(java.util.LinkedHashMap) BufferedInputStream(java.io.BufferedInputStream) EOFException(java.io.EOFException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) FrameworkException(org.structr.common.error.FrameworkException) DataInputStream(java.io.DataInputStream) LinkedList(java.util.LinkedList) ErrorBuffer(org.structr.common.error.ErrorBuffer) AbstractRelationship(org.structr.core.entity.AbstractRelationship) Relationship(org.structr.api.graph.Relationship) GraphObject(org.structr.core.GraphObject)

Aggregations

PropertyContainer (org.structr.api.graph.PropertyContainer)7 AbstractSchemaNode (org.structr.core.entity.AbstractSchemaNode)4 FrameworkException (org.structr.common.error.FrameworkException)3 GraphObject (org.structr.core.GraphObject)3 App (org.structr.core.app.App)3 StructrApp (org.structr.core.app.StructrApp)3 LinkedList (java.util.LinkedList)2 PropertyConverter (org.structr.core.converter.PropertyConverter)2 AbstractNode (org.structr.core.entity.AbstractNode)2 AbstractRelationship (org.structr.core.entity.AbstractRelationship)2 SchemaProperty (org.structr.core.entity.SchemaProperty)2 StringProperty (org.structr.core.property.StringProperty)2 Transformer (org.structr.schema.Transformer)2 BufferedInputStream (java.io.BufferedInputStream)1 DataInputStream (java.io.DataInputStream)1 EOFException (java.io.EOFException)1 DecimalFormat (java.text.DecimalFormat)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1