Search in sources :

Example 1 with NotInTransactionException

use of org.structr.api.NotInTransactionException in project structr by structr.

the class BasicTest method test01CreateNode.

@Test
public void test01CreateNode() {
    try {
        try {
            // Create node out of transaction => should give a NotInTransactionException
            app.create(TestOne.class);
            fail("Should have raised a NotInTransactionException");
        } catch (NotInTransactionException e) {
        }
        try {
            // Try to create node without parameters => should fail
            app.create(TestOne.class);
            fail("Should have raised a NotInTransactionException");
        } catch (NotInTransactionException e) {
        }
        AbstractNode node = null;
        try (final Tx tx = app.tx()) {
            node = app.create(TestOne.class);
            tx.success();
        }
        assertTrue(node != null);
        assertTrue(node instanceof TestOne);
    } catch (FrameworkException ex) {
        logger.error("", ex);
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) NotInTransactionException(org.structr.api.NotInTransactionException) AbstractNode(org.structr.core.entity.AbstractNode) TestOne(org.structr.core.entity.TestOne) Test(org.junit.Test)

Example 2 with NotInTransactionException

use of org.structr.api.NotInTransactionException in project structr by structr.

the class AbstractPrimitiveProperty method setProperty.

@Override
public Object setProperty(final SecurityContext securityContext, final GraphObject obj, final T value) throws FrameworkException {
    final PropertyConverter converter = databaseConverter(securityContext, PropertyMap.unwrap(obj));
    Object convertedValue = value;
    if (converter != null) {
        convertedValue = converter.convert(value);
    }
    // use transformators from property
    for (final String fqcn : transformators) {
        // first test, use caching here later..
        final Transformer transformator = getTransformator(fqcn);
        if (transformator != null) {
            convertedValue = transformator.setProperty(PropertyMap.unwrap(obj), this, convertedValue);
        }
    }
    final PropertyContainer propertyContainer = obj.getPropertyContainer();
    if (propertyContainer != null) {
        if (!TransactionCommand.inTransaction()) {
            throw new NotInTransactionException("setProperty outside of transaction");
        }
        boolean internalSystemPropertiesUnlocked = (obj instanceof CreationContainer);
        // collect modified properties
        if (obj instanceof AbstractNode) {
            if (!unvalidated) {
                TransactionCommand.nodeModified(securityContext.getCachedUser(), (AbstractNode) obj, AbstractPrimitiveProperty.this, propertyContainer.hasProperty(dbName()) ? propertyContainer.getProperty(dbName()) : null, value);
            }
            internalSystemPropertiesUnlocked = ((AbstractNode) obj).internalSystemPropertiesUnlocked;
        } else if (obj instanceof AbstractRelationship) {
            if (!unvalidated) {
                TransactionCommand.relationshipModified(securityContext.getCachedUser(), (AbstractRelationship) obj, AbstractPrimitiveProperty.this, propertyContainer.hasProperty(dbName()) ? propertyContainer.getProperty(dbName()) : null, value);
            }
            internalSystemPropertiesUnlocked = ((AbstractRelationship) obj).internalSystemPropertiesUnlocked;
        }
        // catch all sorts of errors and wrap them in a FrameworkException
        try {
            // save space
            if (convertedValue == null) {
                propertyContainer.removeProperty(dbName());
            } else {
                if (!isSystemInternal() || internalSystemPropertiesUnlocked) {
                    propertyContainer.setProperty(dbName(), convertedValue);
                } else {
                    logger.warn("Tried to set internal system property {} to {}. Action was denied.", new Object[] { dbName(), convertedValue });
                }
            }
            updateAccessInformation(securityContext, propertyContainer);
        } catch (final RetryException rex) {
            // don't catch RetryException here
            throw rex;
        } catch (Throwable t) {
            // throw FrameworkException with the given cause
            final FrameworkException fex = new FrameworkException(500, "Unable to set property " + jsonName() + " on entity with ID " + obj.getUuid() + ": " + t.toString());
            fex.initCause(t);
            throw fex;
        }
        if (isIndexed()) {
            // work
            if (!isPassivelyIndexed()) {
                index(obj, convertedValue);
            }
        }
    }
    return null;
}
Also used : PropertyContainer(org.structr.api.graph.PropertyContainer) Transformer(org.structr.schema.Transformer) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) AbstractRelationship(org.structr.core.entity.AbstractRelationship) CreationContainer(org.structr.core.graph.CreationContainer) RetryException(org.structr.api.RetryException) NotInTransactionException(org.structr.api.NotInTransactionException) PropertyConverter(org.structr.core.converter.PropertyConverter) GraphObject(org.structr.core.GraphObject)

Example 3 with NotInTransactionException

use of org.structr.api.NotInTransactionException in project structr by structr.

the class BasicTest method test01DeleteRelationship.

@Test
public void test01DeleteRelationship() {
    try {
        final TestOne testOne = createTestNode(TestOne.class);
        final TestSix testSix = createTestNode(TestSix.class);
        SixOneOneToOne rel = null;
        assertNotNull(testOne);
        assertNotNull(testSix);
        try (final Tx tx = app.tx()) {
            rel = app.create(testSix, testOne, SixOneOneToOne.class);
            tx.success();
        }
        assertNotNull(rel);
        try {
            // try to delete relationship
            rel.getRelationship().delete();
            fail("Should have raised an org.neo4j.graphdb.NotInTransactionException");
        } catch (NotInTransactionException e) {
        }
        // Relationship still there
        assertNotNull(rel);
        try (final Tx tx = app.tx()) {
            app.delete(rel);
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            String uuid = rel.getUuid();
            fail("Deleted entity should have thrown an exception on access.");
        } catch (NotFoundException iex) {
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) NotInTransactionException(org.structr.api.NotInTransactionException) NotFoundException(org.structr.api.NotFoundException) TestOne(org.structr.core.entity.TestOne) SixOneOneToOne(org.structr.core.entity.SixOneOneToOne) TestSix(org.structr.core.entity.TestSix) Test(org.junit.Test)

Aggregations

NotInTransactionException (org.structr.api.NotInTransactionException)3 FrameworkException (org.structr.common.error.FrameworkException)3 Test (org.junit.Test)2 AbstractNode (org.structr.core.entity.AbstractNode)2 TestOne (org.structr.core.entity.TestOne)2 Tx (org.structr.core.graph.Tx)2 NotFoundException (org.structr.api.NotFoundException)1 RetryException (org.structr.api.RetryException)1 PropertyContainer (org.structr.api.graph.PropertyContainer)1 GraphObject (org.structr.core.GraphObject)1 PropertyConverter (org.structr.core.converter.PropertyConverter)1 AbstractRelationship (org.structr.core.entity.AbstractRelationship)1 SixOneOneToOne (org.structr.core.entity.SixOneOneToOne)1 TestSix (org.structr.core.entity.TestSix)1 CreationContainer (org.structr.core.graph.CreationContainer)1 Transformer (org.structr.schema.Transformer)1