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 "";
}
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());
}
}
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");
}
}
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.");
}
}
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.");
}
}
Aggregations