Search in sources :

Example 11 with Group

use of org.structr.core.entity.Group in project structr by structr.

the class RemoveFromGroupFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        if (!arrayHasLengthAndAllElementsNotNull(sources, 2)) {
            return "";
        }
        if (!(sources[0] instanceof Group)) {
            logger.warn("Error: first argument is not a Group. Parameters: {}", getParametersAsString(sources));
            return "Error: first argument is not a Group.";
        }
        if (!(sources[1] instanceof Principal)) {
            logger.warn("Error: second argument is not a Principal. Parameters: {}", getParametersAsString(sources));
            return "Error: second argument is not a Principal.";
        }
        final Group group = (Group) sources[0];
        final Principal user = (Principal) sources[1];
        group.removeMember(user);
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
    return "";
}
Also used : Group(org.structr.core.entity.Group) Principal(org.structr.core.entity.Principal)

Example 12 with Group

use of org.structr.core.entity.Group in project structr by structr.

the class IsInGroupFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        if (!arrayHasLengthAndAllElementsNotNull(sources, 2)) {
            return "";
        }
        if (!(sources[0] instanceof Group)) {
            logger.warn("Error: first argument is not a Group. Parameters: {}", getParametersAsString(sources));
            return "Error: first argument is not a Group.";
        }
        if (!(sources[1] instanceof Principal)) {
            logger.warn("Error: second argument is not a Principal. Parameters: {}", getParametersAsString(sources));
            return "Error: second argument is not a Principal.";
        }
        final RelationshipType type = StructrApp.getInstance().getDatabaseService().forName(RelationshipType.class, "CONTAINS");
        final Group group = (Group) sources[0];
        final Principal user = (Principal) sources[1];
        return group.hasRelationshipTo(type, user);
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : Group(org.structr.core.entity.Group) RelationshipType(org.structr.api.graph.RelationshipType) Principal(org.structr.core.entity.Principal)

Example 13 with Group

use of org.structr.core.entity.Group in project structr by structr.

the class BasicTest method test03ModifyConstantBooleanProperty.

/**
 * Test the results of setProperty and getProperty of a node
 */
@Test
public void test03ModifyConstantBooleanProperty() {
    try {
        final Class groupType = StructrApp.getConfiguration().getNodeEntityClass("Group");
        final PropertyKey<Boolean> key = StructrApp.key(groupType, "isGroup");
        final PropertyMap props = new PropertyMap();
        final String type = "Group";
        final String name = "TestGroup-1";
        NodeInterface node = null;
        props.put(AbstractNode.type, type);
        props.put(AbstractNode.name, name);
        try (final Tx tx = app.tx()) {
            node = app.create(Group.class, props);
            tx.success();
        }
        try (final Tx tx = app.tx()) {
            // Check defaults
            assertEquals(Group.class.getSimpleName(), node.getProperty(AbstractNode.type));
            assertTrue(node.getProperty(AbstractNode.name).equals(name));
            assertTrue(node.getProperty(key));
        }
        final String name2 = "TestGroup-2";
        try (final Tx tx = app.tx()) {
            // Modify values
            node.setProperty(AbstractNode.name, name2);
            node.setProperty(key, false);
            fail("Should have failed with an exception: Group.isGroup is_read_only_property");
            tx.success();
        } catch (FrameworkException expected) {
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : Group(org.structr.core.entity.Group) PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test)

Example 14 with Group

use of org.structr.core.entity.Group in project structr by structr.

the class ScriptingTest method testWrappingUnwrapping.

@Test
public void testWrappingUnwrapping() {
    // setup phase
    try (final Tx tx = app.tx()) {
        final ActionContext actionContext = new ActionContext(securityContext);
        final TestOne context = app.create(TestOne.class);
        Scripting.evaluate(actionContext, context, "${{ Structr.create('Group', { name: 'Group1' } ); }}", "test");
        Scripting.evaluate(actionContext, context, "${{ Structr.create('Group', 'name', 'Group2'); }}", "test");
        assertEquals("Invalid unwrapping result", 2, app.nodeQuery(Group.class).getAsList().size());
        tx.success();
    } catch (UnlicensedException | FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
}
Also used : Group(org.structr.core.entity.Group) UnlicensedException(org.structr.common.error.UnlicensedException) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) ActionContext(org.structr.schema.action.ActionContext) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 15 with Group

use of org.structr.core.entity.Group in project structr by structr.

the class ScriptingTest method testCollectionOperations.

@Test
public void testCollectionOperations() {
    final Class groupType = StructrApp.getConfiguration().getNodeEntityClass("Group");
    final PropertyKey<List<Principal>> members = StructrApp.key(groupType, "members");
    Group group = null;
    Principal user1 = null;
    Principal user2 = null;
    TestOne testOne = null;
    // setup phase
    try (final Tx tx = app.tx()) {
        group = app.create(Group.class, "Group");
        user1 = app.create(Principal.class, "Tester1");
        user2 = app.create(Principal.class, "Tester2");
        group.setProperty(members, Arrays.asList(new Principal[] { user1 }));
        testOne = app.create(TestOne.class);
        createTestNodes(TestSix.class, 10);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    // test phase, find all the things using scripting
    try (final Tx tx = app.tx()) {
        final ActionContext actionContext = new ActionContext(securityContext);
        // test prerequisites
        assertEquals("Invalid prerequisite", 1, group.getProperty(members).size());
        assertEquals("Invalid prerequisite", user2, Scripting.evaluate(actionContext, group, "${{ return Structr.find('Principal', { name: 'Tester2' })[0]; }}", "test"));
        // test scripting association
        Scripting.evaluate(actionContext, group, "${{ var group = Structr.find('Group')[0]; var users = group.members; users.push(Structr.find('Principal', { name: 'Tester2' })[0]); }}", "test");
        assertEquals("Invalid scripted array operation result", 2, group.getProperty(members).size());
        // reset group
        group.setProperty(members, Arrays.asList(new Principal[] { user1 }));
        // test prerequisites
        assertEquals("Invalid prerequisite", 1, group.getProperty(members).size());
        // test direct push on member property
        Scripting.evaluate(actionContext, group, "${{ var group = Structr.find('Group')[0]; group.members.push(Structr.find('Principal', { name: 'Tester2' })[0]); }}", "test");
        assertEquals("Invalid scripted array operation result", 2, group.getProperty(members).size());
        // test scripting association
        Scripting.evaluate(actionContext, group, "${{ var test = Structr.find('TestOne')[0]; var testSixs = test.manyToManyTestSixs; testSixs.push(Structr.find('TestSix')[0]); }}", "test");
        assertEquals("Invalid scripted array operation result", 1, testOne.getProperty(TestOne.manyToManyTestSixs).size());
        // test direct push on member property
        Scripting.evaluate(actionContext, group, "${{ var test = Structr.find('TestOne')[0]; var testSixs = test.manyToManyTestSixs.push(Structr.find('TestSix')[1]); }}", "test");
        assertEquals("Invalid scripted array operation result", 2, testOne.getProperty(TestOne.manyToManyTestSixs).size());
        tx.success();
    } catch (UnlicensedException | FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
}
Also used : Group(org.structr.core.entity.Group) UnlicensedException(org.structr.common.error.UnlicensedException) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) List(java.util.List) LinkedList(java.util.LinkedList) TestOne(org.structr.core.entity.TestOne) ActionContext(org.structr.schema.action.ActionContext) Principal(org.structr.core.entity.Principal) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Aggregations

Group (org.structr.core.entity.Group)16 Principal (org.structr.core.entity.Principal)13 FrameworkException (org.structr.common.error.FrameworkException)12 Tx (org.structr.core.graph.Tx)12 Test (org.junit.Test)11 LinkedList (java.util.LinkedList)4 TestOne (org.structr.core.entity.TestOne)4 List (java.util.List)3 StructrTest (org.structr.common.StructrTest)3 NodeAttribute (org.structr.core.graph.NodeAttribute)3 ActionContext (org.structr.schema.action.ActionContext)3 UnlicensedException (org.structr.common.error.UnlicensedException)2 App (org.structr.core.app.App)2 StructrApp (org.structr.core.app.StructrApp)2 StructrGraphQLTest (org.structr.rest.common.StructrGraphQLTest)2 StructrUiTest (org.structr.web.StructrUiTest)2 User (org.structr.web.entity.User)2 GroupPrincipal (java.nio.file.attribute.GroupPrincipal)1 UserPrincipal (java.nio.file.attribute.UserPrincipal)1 LinkedHashSet (java.util.LinkedHashSet)1