Search in sources :

Example 91 with FrameworkException

use of org.structr.common.error.FrameworkException in project structr by structr.

the class BulkDeleteCommand method bulkDelete.

public void bulkDelete(final Iterator<GraphObject> iterator) throws FrameworkException {
    final App app = StructrApp.getInstance(securityContext);
    final long count = bulkGraphOperation(securityContext, iterator, 1000, "DeleteObjects", new BulkGraphOperation<GraphObject>() {

        @Override
        public void handleGraphObject(final SecurityContext securityContext, final GraphObject obj) {
            try {
                if (obj.isNode()) {
                    final AbstractNode node = (AbstractNode) obj;
                    if (!node.isGranted(Permission.delete, securityContext)) {
                        logger.warn("Could not delete {} because {} has no delete permission", obj.getUuid(), securityContext.getUser(false));
                    } else {
                        app.delete((NodeInterface) obj);
                    }
                } else {
                    app.delete((RelationshipInterface) obj);
                }
            } catch (FrameworkException fex) {
                logger.warn("Unable to delete node {}: {}", obj.getUuid(), fex.toString());
            }
        }

        @Override
        public void handleThrowable(SecurityContext securityContext, Throwable t, GraphObject node) {
            logger.warn("Unable to delete node {}", node.getUuid());
        }

        @Override
        public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
            logger.warn("Unable to delete nodes {}", t.toString());
        }
    });
    info("Done with deleting {} nodes", count);
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) SecurityContext(org.structr.common.SecurityContext) GraphObject(org.structr.core.GraphObject)

Example 92 with FrameworkException

use of org.structr.common.error.FrameworkException 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)

Example 93 with FrameworkException

use of org.structr.common.error.FrameworkException in project structr by structr.

the class BulkSetUuidCommand method execute.

// ~--- methods --------------------------------------------------------
@Override
public void execute(Map<String, Object> attributes) throws FrameworkException {
    final String nodeType = (String) attributes.get("type");
    final String relType = (String) attributes.get("relType");
    final Boolean allNodes = (Boolean) attributes.get("allNodes");
    final Boolean allRels = (Boolean) attributes.get("allRels");
    final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    final SecurityContext superUserContext = SecurityContext.getSuperUserInstance();
    final NodeFactory nodeFactory = new NodeFactory(superUserContext);
    final RelationshipFactory relFactory = new RelationshipFactory(superUserContext);
    if (nodeType != null || Boolean.TRUE.equals(allNodes)) {
        Iterator<AbstractNode> nodeIterator = null;
        if (Boolean.TRUE.equals(allNodes)) {
            nodeIterator = Iterables.map(nodeFactory, graphDb.getAllNodes()).iterator();
            info("Start setting UUID on all nodes");
        } else {
            nodeIterator = Iterables.map(nodeFactory, graphDb.getNodesByTypeProperty(nodeType)).iterator();
            info("Start setting UUID on nodes of type {}", new Object[] { nodeType });
        }
        final long count = bulkGraphOperation(securityContext, nodeIterator, 1000, "SetNodeUuid", new BulkGraphOperation<AbstractNode>() {

            @Override
            public void handleGraphObject(final SecurityContext securityContext, final AbstractNode node) {
                try {
                    if (node.getProperty(GraphObject.id) == null) {
                        node.unlockSystemPropertiesOnce();
                        node.setProperty(GraphObject.id, NodeServiceCommand.getNextUuid());
                    }
                } catch (FrameworkException fex) {
                    logger.warn("Unable to set UUID of node {}", node, fex);
                }
            }

            @Override
            public void handleThrowable(SecurityContext securityContext, Throwable t, AbstractNode node) {
                logger.warn("Unable to set UUID of node {}", node, t);
            }

            @Override
            public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
                logger.warn("Unable to set UUID on node", t);
            }

            @Override
            public boolean doValidation() {
                return false;
            }
        });
        info("Done with setting UUID on {} nodes", count);
        return;
    }
    if (relType != null || Boolean.TRUE.equals(allRels)) {
        Iterator<AbstractRelationship> relIterator = null;
        if (Boolean.TRUE.equals(allRels)) {
            relIterator = Iterables.map(relFactory, graphDb.getAllRelationships()).iterator();
            info("Start setting UUID on all rels", new Object[] { relType });
        } else {
            relIterator = Iterables.map(relFactory, graphDb.getRelationshipsByType(relType)).iterator();
            info("Start setting UUID on rels of type {}", new Object[] { relType });
        }
        final long count = bulkGraphOperation(securityContext, relIterator, 1000, "SetRelationshipUuid", new BulkGraphOperation<AbstractRelationship>() {

            @Override
            public void handleGraphObject(SecurityContext securityContext, AbstractRelationship rel) {
                try {
                    if (rel.getProperty(GraphObject.id) == null) {
                        rel.unlockSystemPropertiesOnce();
                        rel.setProperty(GraphObject.id, NodeServiceCommand.getNextUuid());
                    }
                } catch (FrameworkException fex) {
                    logger.warn("Unable to set UUID of relationship {}: {}", new Object[] { rel, fex.getMessage() });
                }
            }

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

            @Override
            public void handleTransactionFailure(SecurityContext securityContext, Throwable t) {
                logger.warn("Unable to set UUID on relationships {}", t.toString());
            }

            @Override
            public boolean doValidation() {
                return false;
            }
        });
        info("Done with setting UUID on {} relationships", count);
        return;
    }
    info("Unable to determine entity type to set UUID.");
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) AbstractRelationship(org.structr.core.entity.AbstractRelationship) DatabaseService(org.structr.api.DatabaseService) SecurityContext(org.structr.common.SecurityContext)

Example 94 with FrameworkException

use of org.structr.common.error.FrameworkException in project structr by structr.

the class SetFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    if (arrayHasMinLengthAndAllElementsNotNull(sources, 2)) {
        final SecurityContext securityContext = ctx.getSecurityContext();
        final ConfigurationProvider config = StructrApp.getConfiguration();
        Class type = null;
        PropertyMap propertyMap = null;
        if (sources[0] instanceof GraphObject) {
            final GraphObject source = (GraphObject) sources[0];
            type = source.getEntityType();
        }
        if (type == null) {
            throw new FrameworkException(422, "Can't get type of object '" + sources[0].toString() + "' in set() method!");
        }
        final GraphObject sourceObject = (GraphObject) sources[0];
        if (sources.length == 2 && sources[1] instanceof Map) {
            propertyMap = PropertyMap.inputTypeToJavaType(securityContext, type, (Map) sources[1]);
        } else if (sources.length == 2 && sources[1] instanceof GraphObjectMap) {
            propertyMap = PropertyMap.inputTypeToJavaType(securityContext, type, ((GraphObjectMap) sources[1]).toMap());
        } else if (sources.length == 2 && sources[1] instanceof String) {
            final Gson gson = new GsonBuilder().create();
            final Map<String, Object> values = deserialize(gson, sources[1].toString());
            if (values != null) {
                propertyMap = PropertyMap.inputTypeToJavaType(securityContext, type, values);
            }
        } else if (sources.length > 2) {
            propertyMap = new PropertyMap();
            final int parameter_count = sources.length;
            if (parameter_count % 2 == 0) {
                throw new FrameworkException(400, "Invalid number of parameters: " + parameter_count + ". Should be uneven: " + (ctx.isJavaScriptContext() ? ERROR_MESSAGE_SET_JS : ERROR_MESSAGE_SET));
            }
            for (int c = 1; c < parameter_count; c += 2) {
                final PropertyKey key = config.getPropertyKeyForJSONName(type, sources[c].toString());
                if (key != null) {
                    final PropertyConverter inputConverter = key.inputConverter(securityContext);
                    Object value = sources[c + 1];
                    if (inputConverter != null) {
                        value = inputConverter.convert(value);
                    }
                    propertyMap.put(key, value);
                }
            }
        } else {
            throw new FrameworkException(422, "Invalid use of builtin method set, usage: set(entity, params..)");
        }
        if (propertyMap != null) {
            sourceObject.setProperties(securityContext, propertyMap);
        }
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
    }
    return "";
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) GsonBuilder(com.google.gson.GsonBuilder) ConfigurationProvider(org.structr.schema.ConfigurationProvider) Gson(com.google.gson.Gson) GraphObject(org.structr.core.GraphObject) PropertyMap(org.structr.core.property.PropertyMap) GraphObjectMap(org.structr.core.GraphObjectMap) SecurityContext(org.structr.common.SecurityContext) PropertyConverter(org.structr.core.converter.PropertyConverter) GraphObject(org.structr.core.GraphObject) PropertyMap(org.structr.core.property.PropertyMap) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap) PropertyKey(org.structr.core.property.PropertyKey)

Example 95 with FrameworkException

use of org.structr.common.error.FrameworkException in project structr by structr.

the class StructrLDAPWrapper method add.

public void add(final Entry entry) throws LdapException {
    try (final Tx tx = app().tx()) {
        // create while descending
        final Dn dn = entry.getDn();
        final LDAPNode parent = find(dn.getParent());
        if (parent != null) {
            final Attribute objectClasses = entry.get(schemaManager.getAttributeType(SchemaConstants.OBJECT_CLASS_AT_OID));
            final ObjectClassRegistry reg = schemaManager.getObjectClassRegistry();
            final Set<String> classes = new LinkedHashSet<>();
            final Rdn rdn = dn.getRdn();
            String mainClass = null;
            // make rdn schema aware
            if (!rdn.isSchemaAware()) {
                rdn.apply(schemaManager);
            }
            if (objectClasses != null) {
                for (final Value<?> value : objectClasses) {
                    final String cls = value.getString();
                    final String objectClassOid = reg.getOidByName(cls);
                    if (reg.get(objectClassOid).isStructural()) {
                        mainClass = cls;
                    } else {
                        classes.add(cls);
                    }
                }
                final LDAPNode newChild = parent.createChild(rdn.getNormName(), rdn.getName(), mainClass, classes);
                if (newChild != null) {
                    for (final Attribute attr : entry) {
                        AttributeType type = attr.getAttributeType();
                        String oid = null;
                        if (type != null) {
                            oid = type.getOid();
                        } else {
                            type = schemaManager.getAttributeType(attr.getUpId());
                            oid = type.getOid();
                        }
                        newChild.createAttribute(oid, attr.getUpId(), attr);
                    }
                } else {
                    logger.warn("Unable to add entry {}, could not create new instance", entry);
                }
            } else {
                logger.warn("Unable to add entry {}, could not determine object class(es)", entry);
            }
        } else {
            logger.warn("Unable to add entry {}, parent not found", entry);
        }
        tx.success();
    } catch (FrameworkException fex) {
        handleException(fex);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) LDAPNode(org.structr.ldap.entity.LDAPNode) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) NodeAttribute(org.structr.core.graph.NodeAttribute) LDAPAttribute(org.structr.ldap.entity.LDAPAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) ObjectClassRegistry(org.apache.directory.api.ldap.model.schema.registries.ObjectClassRegistry) Dn(org.apache.directory.api.ldap.model.name.Dn) Rdn(org.apache.directory.api.ldap.model.name.Rdn)

Aggregations

FrameworkException (org.structr.common.error.FrameworkException)892 Tx (org.structr.core.graph.Tx)673 Test (org.junit.Test)450 App (org.structr.core.app.App)175 StructrApp (org.structr.core.app.StructrApp)174 StructrUiTest (org.structr.web.StructrUiTest)134 NodeInterface (org.structr.core.graph.NodeInterface)121 StructrTest (org.structr.common.StructrTest)118 PropertyKey (org.structr.core.property.PropertyKey)109 PropertyMap (org.structr.core.property.PropertyMap)105 IOException (java.io.IOException)96 GraphObject (org.structr.core.GraphObject)93 TestOne (org.structr.core.entity.TestOne)92 File (org.structr.web.entity.File)85 SecurityContext (org.structr.common.SecurityContext)78 Principal (org.structr.core.entity.Principal)69 Page (org.structr.web.entity.dom.Page)69 LinkedList (java.util.LinkedList)62 Folder (org.structr.web.entity.Folder)60 NodeAttribute (org.structr.core.graph.NodeAttribute)56