Search in sources :

Example 6 with PropertyKey

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

the class CMISObjectWrapper method getProperties.

@Override
public Properties getProperties() {
    final FilteredPropertyList properties = new FilteredPropertyList(propertyFilter);
    final BindingsObjectFactory objFactory = new BindingsObjectFactoryImpl();
    properties.add(objFactory.createPropertyIdData(PropertyIds.BASE_TYPE_ID, baseTypeId.value()));
    properties.add(objFactory.createPropertyIdData(PropertyIds.OBJECT_TYPE_ID, type));
    properties.add(objFactory.createPropertyIdData(PropertyIds.OBJECT_ID, id));
    properties.add(objFactory.createPropertyStringData(PropertyIds.NAME, name));
    properties.add(objFactory.createPropertyStringData(PropertyIds.DESCRIPTION, description));
    properties.add(objFactory.createPropertyStringData(PropertyIds.CREATED_BY, createdBy));
    properties.add(objFactory.createPropertyStringData(PropertyIds.LAST_MODIFIED_BY, lastModifiedBy));
    properties.add(objFactory.createPropertyDateTimeData(PropertyIds.CREATION_DATE, creationDate));
    properties.add(objFactory.createPropertyDateTimeData(PropertyIds.LAST_MODIFICATION_DATE, lastModificationDate));
    // add dynamic properties
    if (dynamicPropertyMap != null) {
        for (final Entry<PropertyKey, Object> entry : dynamicPropertyMap.entrySet()) {
            final PropertyKey key = entry.getKey();
            final PropertyType dataType = key.getDataType();
            if (dataType != null) {
                switch(dataType) {
                    case BOOLEAN:
                        properties.add(objFactory.createPropertyBooleanData(key.jsonName(), (Boolean) entry.getValue()));
                        break;
                    case DATETIME:
                        properties.add(objFactory.createPropertyDateTimeData(key.jsonName(), valueOrNull((Date) entry.getValue())));
                        break;
                    case DECIMAL:
                        properties.add(objFactory.createPropertyDecimalData(key.jsonName(), valueOrNull((Double) entry.getValue())));
                        break;
                    case INTEGER:
                        // INTEGER includes int and long so we have to cover both cases here
                        properties.add(objFactory.createPropertyIntegerData(key.jsonName(), intOrNull(entry.getValue())));
                        break;
                    case STRING:
                        properties.add(objFactory.createPropertyStringData(key.jsonName(), (String) entry.getValue()));
                        break;
                }
            }
        }
    }
    // initialize type-dependent properties
    createProperties(objFactory, properties);
    // filter properties according to filter expression
    return objFactory.createPropertiesData(properties.getList());
}
Also used : BindingsObjectFactoryImpl(org.apache.chemistry.opencmis.commons.impl.dataobjects.BindingsObjectFactoryImpl) BindingsObjectFactory(org.apache.chemistry.opencmis.commons.spi.BindingsObjectFactory) GraphObject(org.structr.core.GraphObject) PropertyType(org.apache.chemistry.opencmis.commons.enums.PropertyType) PropertyKey(org.structr.core.property.PropertyKey)

Example 7 with PropertyKey

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

the class ValuesFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    // if (arrayHasMinLengthAndAllElementsNotNull(sources, 2) && sources[0] instanceof GraphObject) {
    if (sources.length >= 2 && sources[0] instanceof GraphObject) {
        final Set<Object> values = new LinkedHashSet<>();
        final GraphObject source = (GraphObject) sources[0];
        for (final PropertyKey key : source.getPropertyKeys(sources[1].toString())) {
            values.add(source.getProperty(key));
        }
        return new LinkedList<>(values);
    // } else if (arrayHasMinLengthAndAllElementsNotNull(sources, 1) && sources[0] instanceof GraphObjectMap) {
    } else if (sources.length >= 1) {
        if (sources[0] instanceof GraphObjectMap) {
            return new LinkedList<>(((GraphObjectMap) sources[0]).values());
        } else if (sources[0] instanceof Map) {
            return new LinkedList<>(((Map) sources[0]).values());
        }
    }
    logParameterError(caller, sources, ctx.isJavaScriptContext());
    return "";
}
Also used : LinkedHashSet(java.util.LinkedHashSet) GraphObjectMap(org.structr.core.GraphObjectMap) GraphObject(org.structr.core.GraphObject) GraphObject(org.structr.core.GraphObject) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap) PropertyKey(org.structr.core.property.PropertyKey) LinkedList(java.util.LinkedList)

Example 8 with PropertyKey

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

the class BulkChangeNodePropertyKeyCommand method execute.

// ~--- methods --------------------------------------------------------
@Override
public void execute(final Map<String, Object> properties) throws FrameworkException {
    final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    final SecurityContext superUserContext = SecurityContext.getSuperUserInstance();
    final NodeFactory nodeFactory = new NodeFactory(superUserContext);
    String type = null;
    final String oldKey = (String) properties.get("oldKey");
    final String newKey = (String) properties.get("newKey");
    if (graphDb != null && StringUtils.isNotBlank(oldKey) && StringUtils.isNotBlank(newKey)) {
        Iterator<AbstractNode> nodeIterator = null;
        if (properties.containsKey(AbstractNode.type.dbName())) {
            type = (String) properties.get(AbstractNode.type.dbName());
            nodeIterator = Iterables.map(nodeFactory, graphDb.getNodesByLabel(type)).iterator();
            properties.remove(AbstractNode.type.dbName());
        } else {
            nodeIterator = Iterables.map(nodeFactory, graphDb.getAllNodes()).iterator();
        }
        final long count = bulkGraphOperation(securityContext, nodeIterator, 1000, "ChangeNodePropertyKey", new BulkGraphOperation<AbstractNode>() {

            @Override
            public void handleGraphObject(SecurityContext securityContext, AbstractNode node) {
                for (Entry entry : properties.entrySet()) {
                    String key = (String) entry.getKey();
                    PropertyKey propertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(node.getClass(), key);
                    if (propertyKey != null) {
                        Node dbNode = node.getNode();
                        if (dbNode.hasProperty(newKey)) {
                            logger.error("Node {} has already a property with key {}", new Object[] { node, newKey });
                            throw new IllegalStateException("Node has already a property of the new key");
                        }
                        if (dbNode.hasProperty(oldKey)) {
                            dbNode.setProperty(newKey, dbNode.getProperty(oldKey));
                            dbNode.removeProperty(oldKey);
                        }
                        node.updateInIndex();
                    }
                }
            }

            @Override
            public void handleThrowable(SecurityContext securityContext, Throwable t, AbstractNode node) {
                logger.warn("Unable to set properties of node {}: {}", new Object[] { node.getUuid(), t.getMessage() });
            }

            @Override
            public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
                logger.warn("Unable to set node properties: {}", t.getMessage());
            }
        });
        logger.info("Fixed {} nodes ...", count);
    } else {
        logger.info("No values for oldKey and/or newKey found, aborting.");
    }
    logger.info("Done");
}
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) Entry(java.util.Map.Entry) SecurityContext(org.structr.common.SecurityContext) PropertyKey(org.structr.core.property.PropertyKey)

Example 9 with PropertyKey

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

the class BulkCopyRelationshipPropertyCommand method execute.

@Override
public void execute(final Map<String, Object> map) throws FrameworkException {
    final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    final RelationshipFactory relFactory = new RelationshipFactory(securityContext);
    final String sourceKey = (String) map.get("sourceKey");
    final String destKey = (String) map.get("destKey");
    if (sourceKey == null || destKey == null) {
        throw new IllegalArgumentException("This command requires one argument of type Map. Map must contain values for 'sourceKey' and 'destKey'.");
    }
    if (graphDb != null) {
        final Iterator<AbstractRelationship> relIterator = Iterables.map(relFactory, graphDb.getAllRelationships()).iterator();
        final long count = bulkGraphOperation(securityContext, relIterator, 1000, "CopyRelationshipProperties", new BulkGraphOperation<AbstractRelationship>() {

            @Override
            public void handleGraphObject(SecurityContext securityContext, AbstractRelationship rel) {
                // Treat only "our" rels
                if (rel.getProperty(GraphObject.id) != null) {
                    Class type = rel.getClass();
                    PropertyKey destPropertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(type, destKey);
                    PropertyKey sourcePropertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(type, sourceKey);
                    try {
                        // copy properties
                        rel.setProperty(destPropertyKey, rel.getProperty(sourcePropertyKey));
                    } catch (FrameworkException fex) {
                        logger.warn("Unable to copy relationship property {} of relationship {} to {}: {}", new Object[] { sourcePropertyKey, rel.getUuid(), destPropertyKey, fex.getMessage() });
                    }
                }
            }

            @Override
            public void handleThrowable(SecurityContext securityContext, Throwable t, AbstractRelationship rel) {
                logger.warn("Unable to copy relationship properties of relationship {}: {}", new Object[] { rel.getUuid(), t.getMessage() });
            }

            @Override
            public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
                logger.warn("Unable to copy relationship properties: {}", t.getMessage());
            }
        });
        logger.info("Finished setting properties on {} nodes", count);
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) AbstractRelationship(org.structr.core.entity.AbstractRelationship) DatabaseService(org.structr.api.DatabaseService) SecurityContext(org.structr.common.SecurityContext) GraphObject(org.structr.core.GraphObject) PropertyKey(org.structr.core.property.PropertyKey)

Example 10 with PropertyKey

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

the class BulkSetRelationshipPropertiesCommand method execute.

// ~--- methods --------------------------------------------------------
@Override
public void execute(final Map<String, Object> properties) throws FrameworkException {
    final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    final RelationshipFactory relationshipFactory = new RelationshipFactory(securityContext);
    if (graphDb != null) {
        Iterator<AbstractRelationship> relIterator = null;
        final String typeName = "type";
        if (properties.containsKey(typeName)) {
            relIterator = StructrApp.getInstance(securityContext).relationshipQuery(SchemaHelper.getEntityClassForRawType(typeName)).getAsList().iterator();
            properties.remove(typeName);
        } else {
            relIterator = Iterables.map(relationshipFactory, graphDb.getAllRelationships()).iterator();
        }
        final long count = bulkGraphOperation(securityContext, relIterator, 1000, "SetRelationshipProperties", new BulkGraphOperation<AbstractRelationship>() {

            @Override
            public void handleGraphObject(SecurityContext securityContext, AbstractRelationship rel) {
                // Treat only "our" nodes
                if (rel.getProperty(AbstractRelationship.id) != null) {
                    for (Entry entry : properties.entrySet()) {
                        String key = (String) entry.getKey();
                        Object val = entry.getValue();
                        PropertyKey propertyKey = StructrApp.getConfiguration().getPropertyKeyForDatabaseName(rel.getClass(), key);
                        if (propertyKey != null) {
                            try {
                                rel.setProperty(propertyKey, val);
                            } catch (FrameworkException fex) {
                                logger.warn("Unable to set relationship property {} of relationship {} to {}: {}", new Object[] { propertyKey, rel.getUuid(), val, fex.getMessage() });
                            }
                        }
                    }
                }
            }

            @Override
            public void handleThrowable(SecurityContext securityContext, Throwable t, AbstractRelationship rel) {
                logger.warn("Unable to set properties of relationship {}: {}", new Object[] { rel.getUuid(), t.getMessage() });
            }

            @Override
            public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
                logger.warn("Unable to set relationship properties: {}", t.getMessage());
            }
        });
        logger.info("Finished setting properties on {} relationships", count);
    }
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) AbstractRelationship(org.structr.core.entity.AbstractRelationship) DatabaseService(org.structr.api.DatabaseService) Entry(java.util.Map.Entry) SecurityContext(org.structr.common.SecurityContext) PropertyKey(org.structr.core.property.PropertyKey)

Aggregations

PropertyKey (org.structr.core.property.PropertyKey)177 FrameworkException (org.structr.common.error.FrameworkException)108 Test (org.junit.Test)69 NodeInterface (org.structr.core.graph.NodeInterface)62 Tx (org.structr.core.graph.Tx)61 GraphObject (org.structr.core.GraphObject)59 StructrTest (org.structr.common.StructrTest)39 PropertyMap (org.structr.core.property.PropertyMap)37 List (java.util.List)31 Result (org.structr.core.Result)28 ConfigurationProvider (org.structr.schema.ConfigurationProvider)27 SecurityContext (org.structr.common.SecurityContext)26 LinkedList (java.util.LinkedList)22 StringProperty (org.structr.core.property.StringProperty)22 ErrorToken (org.structr.common.error.ErrorToken)20 Map (java.util.Map)18 PropertyConverter (org.structr.core.converter.PropertyConverter)18 NodeAttribute (org.structr.core.graph.NodeAttribute)17 App (org.structr.core.app.App)16 StructrApp (org.structr.core.app.StructrApp)16