Search in sources :

Example 1 with Property

use of org.structr.core.property.Property in project structr by structr.

the class BulkFixNodePropertiesCommand method execute.

@Override
public void execute(Map<String, Object> attributes) throws FrameworkException {
    final String propertyName = (String) attributes.get("name");
    final String entityTypeName = (String) attributes.get("type");
    if (entityTypeName != null) {
        final Class type = SchemaHelper.getEntityClassForRawType(entityTypeName);
        if (type != null) {
            final DatabaseService db = StructrApp.getInstance(securityContext).getDatabaseService();
            final NodeFactory factory = new NodeFactory(securityContext);
            final Iterator<AbstractNode> nodeIterator = Iterables.map(factory, db.getNodesByLabel(entityTypeName)).iterator();
            logger.info("Trying to fix properties of all {} nodes", type.getSimpleName());
            long nodeCount = bulkGraphOperation(securityContext, nodeIterator, 100, "FixNodeProperties", new BulkGraphOperation<AbstractNode>() {

                private void fixProperty(AbstractNode node, Property propertyToFix) {
                    Node databaseNode = node.getNode();
                    if (databaseNode.hasProperty(propertyToFix.dbName())) {
                        // check value with property converter
                        PropertyConverter converter = propertyToFix.databaseConverter(securityContext, node);
                        if (converter != null) {
                            try {
                                Object value = databaseNode.getProperty(propertyToFix.dbName());
                                converter.revert(value);
                            } catch (ClassCastException cce) {
                                // exception, needs fix
                                String databaseName = propertyToFix.dbName();
                                Object databaseValue = databaseNode.getProperty(databaseName);
                                Object correctedValue = propertyToFix.fixDatabaseProperty(databaseValue);
                                if (databaseValue != null && correctedValue != null) {
                                    try {
                                        // try to set database value to corrected value
                                        databaseNode.setProperty(databaseName, correctedValue);
                                    } catch (Throwable t) {
                                        logger.warn("Unable to fix property {} of {} with UUID {} which is of type {}", new Object[] { propertyToFix.dbName(), type.getSimpleName(), node.getUuid(), databaseValue != null ? databaseValue.getClass() : "null" });
                                    }
                                }
                            } catch (Throwable t) {
                                // log exceptions of other types
                                logger.warn("", t);
                            }
                        }
                    }
                }

                @Override
                public void handleGraphObject(SecurityContext securityContext, AbstractNode node) {
                    if (propertyName != null) {
                        PropertyKey key = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(type, propertyName);
                        if (key != null) {
                            // needs type cast to Property to use fixDatabaseProperty method
                            if (key instanceof Property) {
                                fixProperty(node, (Property) key);
                            }
                        }
                    } else {
                        for (PropertyKey key : node.getPropertyKeys(PropertyView.All)) {
                            // needs type cast to Property to use fixDatabaseProperty method
                            if (key instanceof Property) {
                                fixProperty(node, (Property) key);
                            }
                        }
                    }
                }
            });
            logger.info("Fixed {} nodes", nodeCount);
            return;
        }
    }
    logger.info("Unable to determine property and/or entity type to fix.");
}
Also used : AbstractNode(org.structr.core.entity.AbstractNode) Node(org.structr.api.graph.Node) AbstractNode(org.structr.core.entity.AbstractNode) DatabaseService(org.structr.api.DatabaseService) PropertyConverter(org.structr.core.converter.PropertyConverter) SecurityContext(org.structr.common.SecurityContext) Property(org.structr.core.property.Property) PropertyKey(org.structr.core.property.PropertyKey)

Example 2 with Property

use of org.structr.core.property.Property in project structr by structr.

the class DOMElement method updateFromNode.

public static void updateFromNode(final DOMElement node, final DOMNode newNode) throws FrameworkException {
    if (newNode instanceof DOMElement) {
        final PropertyKey<String> tagKey = StructrApp.key(DOMElement.class, "tag");
        final PropertyMap properties = new PropertyMap();
        for (Property htmlProp : node.getHtmlAttributes()) {
            properties.put(htmlProp, newNode.getProperty(htmlProp));
        }
        // copy tag
        properties.put(tagKey, newNode.getProperty(tagKey));
        node.setProperties(node.getSecurityContext(), properties);
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) HtmlProperty(org.structr.web.common.HtmlProperty) StringProperty(org.structr.core.property.StringProperty) Property(org.structr.core.property.Property)

Example 3 with Property

use of org.structr.core.property.Property in project structr by structr.

the class ImportGPXFunction method readProperties.

private void readProperties(final Element child, final GraphObjectMap item) {
    final String tagName = child.getTagName();
    final Property property = fieldMapping.get(tagName);
    if (property != null) {
        final Class valueType = property.valueType();
        if (valueType != null) {
            switch(valueType.getSimpleName()) {
                case "Double":
                    storeDouble(child, item, property);
                    break;
                case "String":
                    storeString(child, item, property);
                    break;
                case "Integer":
                    storeInt(child, item, property);
                    break;
            }
        }
    } else {
        if ("author".equals(tagName)) {
            final GraphObjectMap author = readPoint(child);
            if (!author.isEmpty()) {
                item.put(authorProperty, author);
            }
        }
    }
}
Also used : GraphObjectMap(org.structr.core.GraphObjectMap) Property(org.structr.core.property.Property) DoubleProperty(org.structr.core.property.DoubleProperty) IntProperty(org.structr.core.property.IntProperty) GenericProperty(org.structr.core.property.GenericProperty) StringProperty(org.structr.core.property.StringProperty)

Aggregations

Property (org.structr.core.property.Property)3 StringProperty (org.structr.core.property.StringProperty)2 DatabaseService (org.structr.api.DatabaseService)1 Node (org.structr.api.graph.Node)1 SecurityContext (org.structr.common.SecurityContext)1 GraphObjectMap (org.structr.core.GraphObjectMap)1 PropertyConverter (org.structr.core.converter.PropertyConverter)1 AbstractNode (org.structr.core.entity.AbstractNode)1 DoubleProperty (org.structr.core.property.DoubleProperty)1 GenericProperty (org.structr.core.property.GenericProperty)1 IntProperty (org.structr.core.property.IntProperty)1 PropertyKey (org.structr.core.property.PropertyKey)1 PropertyMap (org.structr.core.property.PropertyMap)1 HtmlProperty (org.structr.web.common.HtmlProperty)1