use of org.structr.core.GraphObject in project structr by structr.
the class GetOrCreateFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
try {
if (sources == null) {
throw new IllegalArgumentException();
}
final SecurityContext securityContext = ctx.getSecurityContext();
final ConfigurationProvider config = StructrApp.getConfiguration();
final App app = StructrApp.getInstance(securityContext);
final PropertyMap properties = new PropertyMap();
// the type to query for
Class type = null;
if (sources.length >= 1 && sources[0] != null) {
final String typeString = sources[0].toString();
type = config.getNodeEntityClass(typeString);
if (type == null) {
logger.warn("Error in get_or_create(): type \"{}\" not found.", typeString);
return ERROR_MESSAGE_TYPE_NOT_FOUND + typeString;
}
}
// exit gracefully instead of crashing..
if (type == null) {
logger.warn("Error in get_or_create(): no type specified. Parameters: {}", getParametersAsString(sources));
return ERROR_MESSAGE_NO_TYPE_SPECIFIED;
}
// experimental: disable result count, prevents instantiation
// of large collections just for counting all the objects..
securityContext.ignoreResultCount(true);
// extension for native javascript objects
if (sources.length == 2 && sources[1] instanceof Map) {
properties.putAll(PropertyMap.inputTypeToJavaType(securityContext, type, (Map) sources[1]));
} else {
final int parameter_count = sources.length;
if (parameter_count % 2 == 0) {
throw new FrameworkException(400, "Invalid number of parameters: " + parameter_count + ". Should be uneven: " + ERROR_MESSAGE_GET_OR_CREATE);
}
for (int c = 1; c < parameter_count; c += 2) {
if (sources[c] == null) {
throw new IllegalArgumentException();
}
final PropertyKey key = StructrApp.key(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);
}
properties.put(key, value);
}
}
}
final GraphObject obj = app.nodeQuery(type).disableSorting().pageSize(1).and(properties).getFirst();
if (obj != null) {
// return existing object
return obj;
}
// create new object
return app.create(type, properties);
} catch (final IllegalArgumentException e) {
logParameterError(caller, sources, ctx.isJavaScriptContext());
return usage(ctx.isJavaScriptContext());
}
}
use of org.structr.core.GraphObject in project structr by structr.
the class KeysFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
try {
if (sources == null) {
throw new IllegalArgumentException();
}
if (sources.length == 2 && sources[0] != null && sources[0] instanceof GraphObject && sources[1] != null) {
final Set<String> keys = new LinkedHashSet<>();
final GraphObject source = (GraphObject) sources[0];
for (final PropertyKey key : source.getPropertyKeys(sources[1].toString())) {
keys.add(key.jsonName());
}
return new LinkedList<>(keys);
} else if (sources.length == 1 && sources[0] != null && sources[0] instanceof GraphObjectMap) {
return new LinkedList<>(((GraphObjectMap) sources[0]).keySet());
} else if (sources.length == 1 && sources[0] != null && sources[0] instanceof Map) {
return new LinkedList<>(((Map) sources[0]).keySet());
} else {
return null;
}
} catch (final IllegalArgumentException e) {
logParameterError(caller, sources, ctx.isJavaScriptContext());
return usage(ctx.isJavaScriptContext());
}
}
use of org.structr.core.GraphObject in project structr by structr.
the class CypherTest method testCypherResultWrapping.
@Test
public void testCypherResultWrapping() {
try (final Tx tx = app.tx()) {
List<TestOne> testOnes = createTestNodes(TestOne.class, 10);
List<TestSix> testSixs = createTestNodes(TestSix.class, 10);
for (final TestOne testOne : testOnes) {
testOne.setProperty(TestOne.manyToManyTestSixs, testSixs);
}
tx.success();
} catch (FrameworkException ex) {
logger.warn("", ex);
fail("Unexpected exception");
}
try (final Tx tx = app.tx()) {
final List<GraphObject> result = app.command(CypherQueryCommand.class).execute("MATCH (n:TestOne) RETURN DISTINCT n");
assertEquals("Invalid wrapped cypher query result", 10, result.size());
for (final GraphObject obj : result) {
System.out.println(obj);
assertEquals("Invalid wrapped cypher query result", TestOne.class, obj.getClass());
}
tx.success();
} catch (FrameworkException ex) {
logger.error("", ex);
}
try (final Tx tx = app.tx()) {
final List<GraphObject> result = app.command(CypherQueryCommand.class).execute("MATCH (n:TestOne)-[r]-(m:TestSix) RETURN DISTINCT n, r, m ");
final Iterator<GraphObject> it = result.iterator();
assertEquals("Invalid wrapped cypher query result", 300, result.size());
while (it.hasNext()) {
// n
assertEquals("Invalid wrapped cypher query result", TestOne.class, it.next().getClass());
// r
assertEquals("Invalid wrapped cypher query result", SixOneManyToMany.class, it.next().getClass());
// m
assertEquals("Invalid wrapped cypher query result", TestSix.class, it.next().getClass());
}
tx.success();
} catch (FrameworkException ex) {
logger.error("", ex);
}
try (final Tx tx = app.tx()) {
final List<GraphObject> result = app.command(CypherQueryCommand.class).execute("MATCH p = (n:TestOne)-[r]-(m:TestSix) RETURN p ");
assertEquals("Invalid wrapped cypher query result", 100, result.size());
for (final GraphObject obj : result) {
assertEquals("Invalid wrapped cypher query result", GraphObjectMap.class, obj.getClass());
}
tx.success();
} catch (FrameworkException ex) {
logger.error("", ex);
}
try (final Tx tx = app.tx()) {
final List<GraphObject> result = app.command(CypherQueryCommand.class).execute("MATCH p = (n:TestOne)-[r]-(m:TestSix) RETURN { nodes: nodes(p), rels: relationships(p) } ");
assertEquals("Invalid wrapped cypher query result", 100, result.size());
for (final GraphObject obj : result) {
assertEquals("Invalid wrapped cypher query result", GraphObjectMap.class, obj.getClass());
final Object nodes = obj.getProperty(new StringProperty("nodes"));
final Object rels = obj.getProperty(new StringProperty("rels"));
assertTrue("Invalid wrapped cypher query result", nodes instanceof Collection);
assertTrue("Invalid wrapped cypher query result", rels instanceof Collection);
final Iterator it = ((Collection) nodes).iterator();
while (it.hasNext()) {
assertEquals("Invalid wrapped cypher query result", TestOne.class, it.next().getClass());
assertEquals("Invalid wrapped cypher query result", TestSix.class, it.next().getClass());
}
for (final Object node : ((Collection) rels)) {
assertEquals("Invalid wrapped cypher query result", SixOneManyToMany.class, node.getClass());
}
}
tx.success();
} catch (FrameworkException ex) {
logger.error("", ex);
}
try (final Tx tx = app.tx()) {
final List<GraphObject> result = app.command(CypherQueryCommand.class).execute("MATCH p = (n:TestOne)-[r]-(m:TestSix) RETURN DISTINCT { path: p, value: 12 } ");
assertEquals("Invalid wrapped cypher query result", 100, result.size());
final Iterator it = result.iterator();
while (it.hasNext()) {
final Object path = it.next();
final Object value = it.next();
assertEquals("Invalid wrapped cypher query result", GraphObjectMap.class, path.getClass());
assertEquals("Invalid wrapped cypher query result", GraphObjectMap.class, value.getClass());
assertEquals("Invalid wrapped cypher query result", 12L, ((GraphObjectMap) value).getProperty(new StringProperty("value")));
}
tx.success();
} catch (FrameworkException ex) {
logger.error("", ex);
}
try (final Tx tx = app.tx()) {
final List<GraphObject> result = app.command(CypherQueryCommand.class).execute("MATCH p = (n:TestOne)-[r]-(m:TestSix) RETURN { nodes: { x : { y : { z : nodes(p) } } } } ");
assertEquals("Invalid wrapped cypher query result", 100, result.size());
for (final GraphObject obj : result) {
assertEquals("Invalid wrapped cypher query result", GraphObjectMap.class, obj.getClass());
final Object nodes = obj.getProperty(new StringProperty("nodes"));
assertTrue("Invalid wrapped cypher query result", nodes instanceof GraphObjectMap);
final Object x = ((GraphObjectMap) nodes).getProperty(new StringProperty("x"));
assertTrue("Invalid wrapped cypher query result", x instanceof GraphObjectMap);
final Object y = ((GraphObjectMap) x).getProperty(new StringProperty("y"));
assertTrue("Invalid wrapped cypher query result", y instanceof GraphObjectMap);
final Object z = ((GraphObjectMap) y).getProperty(new StringProperty("z"));
assertTrue("Invalid wrapped cypher query result", z instanceof Collection);
}
tx.success();
} catch (FrameworkException ex) {
logger.error("", ex);
}
/*
try (final Tx tx = app.tx()) {
final List<GraphObject> result = app.command(CypherQueryCommand.class).execute("MATCH p = (n:TestOne)-[r]-(m:TestSix) RETURN p");
assertEquals("Invalid wrapped cypher query result", 100, result.size());
for (final GraphObject obj : result) {
assertEquals("Invalid wrapped cypher query result", GraphObjectMap.class, obj.getClass());
final Object paths = obj.getProperty(new StringProperty("p"));
assertTrue("Invalid wrapped cypher query result", paths instanceof Iterable);
final Iterator it = ((Iterable)paths).iterator();
while (it.hasNext()) {
assertEquals("Invalid wrapped cypher query result", TestOne.class, it.next().getClass()); // n
assertEquals("Invalid wrapped cypher query result", SixOneManyToMany.class, it.next().getClass()); // r
assertEquals("Invalid wrapped cypher query result", TestSix.class, it.next().getClass()); // m
}
}
tx.success();
} catch (FrameworkException ex) {
logger.error("", ex);
}
*/
}
use of org.structr.core.GraphObject in project structr by structr.
the class ValidationTest method testConcurrentValidation.
@Test
public void testConcurrentValidation() {
final int count = 100;
try (final Tx tx = app.tx()) {
app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Item"), new NodeAttribute(new StringProperty("_name"), "+String!"));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
final Class type = StructrApp.getConfiguration().getNodeEntityClass("Item");
assertNotNull(type);
final PropertyKey name = StructrApp.key(type, "name");
assertNotNull(name);
final Runnable tester = new Runnable() {
@Override
public void run() {
for (int i = 0; i < count; i++) {
// testing must be done in an isolated transaction
try (final Tx tx = app.tx()) {
app.create(type, "Item" + i);
tx.success();
} catch (FrameworkException ignore) {
}
}
}
};
// submit three test instances
final ExecutorService executor = Executors.newCachedThreadPool();
final Future f1 = executor.submit(tester);
final Future f2 = executor.submit(tester);
final Future f3 = executor.submit(tester);
try {
f1.get();
f2.get();
f3.get();
} catch (Throwable ex) {
}
List<GraphObject> result = null;
try (final Tx tx = app.tx()) {
result = app.nodeQuery(type).getAsList();
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
// verify that only count entities have been created.
assertEquals("Invalid concurrent validation result", count, result.size());
executor.shutdownNow();
}
use of org.structr.core.GraphObject in project structr by structr.
the class ValidationTest method testConcurrentValidationOnDynamicProperty.
@Test
public void testConcurrentValidationOnDynamicProperty() {
final int count = 100;
try (final Tx tx = app.tx()) {
app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Item"), new NodeAttribute(new StringProperty("_testXYZ"), "+String!"));
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
final Class type = StructrApp.getConfiguration().getNodeEntityClass("Item");
assertNotNull(type);
final PropertyKey testXYZ = StructrApp.key(type, "testXYZ");
assertNotNull(testXYZ);
final Runnable tester = new Runnable() {
@Override
public void run() {
for (int i = 0; i < count; i++) {
// testing must be done in an isolated transaction
try (final Tx tx = app.tx()) {
app.create(type, new NodeAttribute(testXYZ, "Item" + i));
tx.success();
} catch (FrameworkException fex) {
}
}
}
};
// submit three test instances
final ExecutorService executor = Executors.newCachedThreadPool();
final Future f1 = executor.submit(tester);
final Future f2 = executor.submit(tester);
final Future f3 = executor.submit(tester);
try {
f1.get();
f2.get();
f3.get();
} catch (Throwable ex) {
}
List<GraphObject> result = null;
try (final Tx tx = app.tx()) {
result = app.nodeQuery(type).getAsList();
tx.success();
} catch (FrameworkException fex) {
logger.warn("", fex);
fail("Unexpected exception.");
}
// verify that only count entities have been created.
assertEquals("Invalid concurrent validation result", count, result.size());
executor.shutdownNow();
}
Aggregations