Search in sources :

Example 16 with PropertyConverter

use of org.structr.core.converter.PropertyConverter in project structr by structr.

the class RDFItem method setProperty.

protected Object setProperty(final Class nodeType, final GraphObject instance, final String propertyName, final Object value) throws FrameworkException {
    final ConfigurationProvider config = StructrApp.getConfiguration();
    Object convertedValue = value;
    if (convertedValue != null) {
        final PropertyKey key = StructrApp.key(nodeType, propertyName);
        if (key != null) {
            final PropertyConverter inputConverter = key.inputConverter(SecurityContext.getSuperUserInstance());
            if (inputConverter != null) {
                convertedValue = inputConverter.convert(convertedValue);
            }
            if (convertedValue == null) {
                OWLParserv2.logger.println("Invalid converted value " + convertedValue + ", source was " + value);
            }
            return instance.setProperty(key, convertedValue);
        } else {
            System.out.println("Key " + propertyName + " not found on " + nodeType.getSimpleName());
        }
    }
    return null;
}
Also used : ConfigurationProvider(org.structr.schema.ConfigurationProvider) PropertyConverter(org.structr.core.converter.PropertyConverter) GraphObject(org.structr.core.GraphObject) PropertyKey(org.structr.core.property.PropertyKey)

Example 17 with PropertyConverter

use of org.structr.core.converter.PropertyConverter in project structr by structr.

the class HtmlServlet method resolvePossiblePropertyNamesForObjectResolution.

private void resolvePossiblePropertyNamesForObjectResolution(final ConfigurationProvider config, final Query query, final String name) {
    for (final String possiblePropertyName : possiblePropertyNamesForEntityResolving) {
        final String[] parts = possiblePropertyName.split("\\.");
        String className = AbstractNode.class.getSimpleName();
        String keyName = AbstractNode.name.jsonName();
        switch(parts.length) {
            case 2:
                className = parts[0];
                keyName = parts[1];
                break;
            default:
                logger.warn("Unable to process key for object resolution {}.", possiblePropertyName);
                break;
        }
        if (StringUtils.isNoneBlank(className, keyName)) {
            final Class type = config.getNodeEntityClass(className);
            if (type != null) {
                final PropertyKey key = StructrApp.key(type, keyName);
                if (key != null) {
                    try {
                        final PropertyConverter converter = key.inputConverter(SecurityContext.getSuperUserInstance());
                        if (converter != null) {
                            // try converted value, fail silenty
                            query.or(key, converter.convert(name));
                        } else {
                            // try unconverted value, fail silently if it doesn't work
                            query.or(key, name);
                        }
                    } catch (FrameworkException ignore) {
                    }
                } else {
                    logger.warn("Unable to find property key {} of type {} defined in key {} used for object resolution.", new Object[] { keyName, className, possiblePropertyName });
                }
            } else {
                logger.warn("Unable to find type {} defined in key {} used for object resolution.", new Object[] { className, possiblePropertyName });
            }
        }
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) PropertyConverter(org.structr.core.converter.PropertyConverter) PropertyKey(org.structr.core.property.PropertyKey)

Example 18 with PropertyConverter

use of org.structr.core.converter.PropertyConverter in project structr by structr.

the class CreateAndAppendDOMNodeCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final Map<String, Object> nodeData = webSocketData.getNodeData();
    final String parentId = (String) nodeData.get("parentId");
    final String childContent = (String) nodeData.get("childContent");
    final String pageId = webSocketData.getPageId();
    Boolean inheritVisibilityFlags = (Boolean) nodeData.get("inheritVisibilityFlags");
    if (inheritVisibilityFlags == null) {
        inheritVisibilityFlags = false;
    }
    // remove configuration elements from the nodeData so we don't set it on the node
    nodeData.remove("parentId");
    nodeData.remove("inheritVisibilityFlags");
    if (pageId != null) {
        // check for parent ID before creating any nodes
        if (parentId == null) {
            getWebSocket().send(MessageBuilder.status().code(422).message("Cannot add node without parentId").build(), true);
            return;
        }
        // check if parent node with given ID exists
        final DOMNode parentNode = getDOMNode(parentId);
        if (parentNode == null) {
            getWebSocket().send(MessageBuilder.status().code(404).message("Parent node not found").build(), true);
            return;
        }
        final Document document = getPage(pageId);
        if (document != null) {
            final String tagName = (String) nodeData.get("tagName");
            nodeData.remove("tagName");
            try {
                DOMNode newNode;
                if (tagName != null && "comment".equals(tagName)) {
                    newNode = (DOMNode) document.createComment("#comment");
                } else if (tagName != null && "template".equals(tagName)) {
                    newNode = (DOMNode) document.createTextNode("#template");
                    try {
                        newNode.unlockSystemPropertiesOnce();
                        newNode.setProperties(newNode.getSecurityContext(), new PropertyMap(NodeInterface.type, Template.class.getSimpleName()));
                    } catch (FrameworkException fex) {
                        logger.warn("Unable to set type of node {} to Template: {}", new Object[] { newNode.getUuid(), fex.getMessage() });
                    }
                } else if (tagName != null && !tagName.isEmpty()) {
                    if ("custom".equals(tagName)) {
                        try {
                            // experimental: create DOM element with literal tag
                            newNode = (DOMElement) StructrApp.getInstance(webSocket.getSecurityContext()).create(DOMElement.class, new NodeAttribute(StructrApp.key(DOMElement.class, "tag"), "custom"), new NodeAttribute(StructrApp.key(DOMElement.class, "hideOnDetail"), false), new NodeAttribute(StructrApp.key(DOMElement.class, "hideOnIndex"), false));
                            if (newNode != null && document != null) {
                                newNode.doAdopt((Page) document);
                            }
                        } catch (FrameworkException fex) {
                            // abort
                            getWebSocket().send(MessageBuilder.status().code(422).message(fex.getMessage()).build(), true);
                            return;
                        }
                    } else {
                        newNode = (DOMNode) document.createElement(tagName);
                    }
                } else {
                    newNode = (DOMNode) document.createTextNode("#text");
                }
                // Instantiate node again to get correct class
                newNode = getDOMNode(newNode.getUuid());
                // append new node to parent
                if (newNode != null) {
                    parentNode.appendChild(newNode);
                    for (Entry entry : nodeData.entrySet()) {
                        final String key = (String) entry.getKey();
                        final Object val = entry.getValue();
                        PropertyKey propertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(newNode.getClass(), key);
                        if (propertyKey != null) {
                            try {
                                Object convertedValue = val;
                                PropertyConverter inputConverter = propertyKey.inputConverter(SecurityContext.getSuperUserInstance());
                                if (inputConverter != null) {
                                    convertedValue = inputConverter.convert(val);
                                }
                                newNode.setProperties(newNode.getSecurityContext(), new PropertyMap(propertyKey, convertedValue));
                            } catch (FrameworkException fex) {
                                logger.warn("Unable to set node property {} of node {} to {}: {}", new Object[] { propertyKey, newNode.getUuid(), val, fex.getMessage() });
                            }
                        }
                    }
                    PropertyMap visibilityFlags = null;
                    if (inheritVisibilityFlags) {
                        visibilityFlags = new PropertyMap();
                        visibilityFlags.put(DOMNode.visibleToAuthenticatedUsers, parentNode.getProperty(DOMNode.visibleToAuthenticatedUsers));
                        visibilityFlags.put(DOMNode.visibleToPublicUsers, parentNode.getProperty(DOMNode.visibleToPublicUsers));
                        try {
                            newNode.setProperties(newNode.getSecurityContext(), visibilityFlags);
                        } catch (FrameworkException fex) {
                            logger.warn("Unable to inherit visibility flags for node {} from parent node {}", newNode, parentNode);
                        }
                    }
                    // create a child text node if content is given
                    if (StringUtils.isNotBlank(childContent)) {
                        final DOMNode childNode = (DOMNode) document.createTextNode(childContent);
                        newNode.appendChild(childNode);
                        if (inheritVisibilityFlags) {
                            try {
                                childNode.setProperties(childNode.getSecurityContext(), visibilityFlags);
                            } catch (FrameworkException fex) {
                                logger.warn("Unable to inherit visibility flags for node {} from parent node {}", childNode, newNode);
                            }
                        }
                    }
                }
            } catch (DOMException dex) {
                // send DOM exception
                getWebSocket().send(MessageBuilder.status().code(422).message(dex.getMessage()).build(), true);
            }
        } else {
            getWebSocket().send(MessageBuilder.status().code(404).message("Page not found").build(), true);
        }
    } else {
        getWebSocket().send(MessageBuilder.status().code(422).message("Cannot create node without pageId").build(), true);
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) FrameworkException(org.structr.common.error.FrameworkException) Document(org.w3c.dom.Document) Template(org.structr.web.entity.dom.Template) DOMException(org.w3c.dom.DOMException) Entry(java.util.Map.Entry) PropertyMap(org.structr.core.property.PropertyMap) PropertyConverter(org.structr.core.converter.PropertyConverter) DOMNode(org.structr.web.entity.dom.DOMNode) PropertyKey(org.structr.core.property.PropertyKey)

Example 19 with PropertyConverter

use of org.structr.core.converter.PropertyConverter in project structr by structr.

the class MapPropertyGroup method getGroupedProperties.

// ~--- get methods ----------------------------------------------------
@Override
public PropertyMap getGroupedProperties(SecurityContext securityContext, GraphObject source) {
    PropertyMap groupedProperties = new PropertyMap();
    for (PropertyKey key : propertyKeys) {
        PropertyConverter converter = key.inputConverter(securityContext);
        if (converter != null) {
            try {
                Object convertedValue = converter.revert(source.getProperty(key));
                groupedProperties.put(key, convertedValue);
            } catch (FrameworkException fex) {
                logger.warn("Unable to convert grouped property {} on type {}: {}", new Object[] { key.dbName(), source.getClass().getSimpleName(), fex.getMessage() });
            }
        } else {
            groupedProperties.put(key, source.getProperty(key));
        }
    }
    return groupedProperties;
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) FrameworkException(org.structr.common.error.FrameworkException) PropertyConverter(org.structr.core.converter.PropertyConverter) PropertyKey(org.structr.core.property.PropertyKey)

Example 20 with PropertyConverter

use of org.structr.core.converter.PropertyConverter 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)

Aggregations

PropertyConverter (org.structr.core.converter.PropertyConverter)44 GraphObject (org.structr.core.GraphObject)31 FrameworkException (org.structr.common.error.FrameworkException)28 PropertyKey (org.structr.core.property.PropertyKey)18 SecurityContext (org.structr.common.SecurityContext)11 PropertyMap (org.structr.core.property.PropertyMap)10 ConfigurationProvider (org.structr.schema.ConfigurationProvider)10 Map (java.util.Map)9 AbstractNode (org.structr.core.entity.AbstractNode)6 List (java.util.List)5 App (org.structr.core.app.App)5 StructrApp (org.structr.core.app.StructrApp)5 LinkedList (java.util.LinkedList)4 Test (org.junit.Test)4 StructrTest (org.structr.common.StructrTest)4 Query (org.structr.core.app.Query)4 LinkedHashMap (java.util.LinkedHashMap)3 GraphObjectMap (org.structr.core.GraphObjectMap)3 LinkedHashSet (java.util.LinkedHashSet)2 Entry (java.util.Map.Entry)2