use of org.structr.core.GraphObject in project structr by structr.
the class CreateRelationshipCommand method createRelationship.
private synchronized <A extends NodeInterface, B extends NodeInterface, R extends Relation<A, B, ?, ?>> R createRelationship(final A fromNode, final B toNode, final Class<R> relType, final PropertyMap attributes) throws FrameworkException {
// disable updating access time when creating relationships
securityContext.disableModificationOfAccessTime();
final RelationshipFactory<R> factory = new RelationshipFactory(securityContext);
final PropertyMap properties = new PropertyMap(attributes);
final CreationContainer tmp = new CreationContainer();
final R template = instantiate(relType);
final Node startNode = fromNode.getNode();
final Node endNode = toNode.getNode();
final Date now = new Date();
final Principal user = securityContext.getCachedUser();
template.ensureCardinality(securityContext, fromNode, toNode);
// date properties need converter
AbstractRelationship.createdDate.setProperty(securityContext, tmp, now);
AbstractRelationship.lastModifiedDate.setProperty(securityContext, tmp, now);
// set initial properties manually (caution, this can only be used for primitive properties!)
tmp.getData().put(GraphObject.id.jsonName(), getNextUuid());
tmp.getData().put(GraphObject.type.jsonName(), relType.getSimpleName());
tmp.getData().put(AbstractRelationship.relType.jsonName(), template.name());
tmp.getData().put(AbstractRelationship.sourceId.jsonName(), fromNode.getUuid());
tmp.getData().put(AbstractRelationship.targetId.jsonName(), toNode.getUuid());
tmp.getData().put(AbstractRelationship.visibleToPublicUsers.jsonName(), false);
tmp.getData().put(AbstractRelationship.visibleToAuthenticatedUsers.jsonName(), false);
tmp.getData().put(AbstractRelationship.cascadeDelete.jsonName(), template.getCascadingDeleteFlag());
if (user != null) {
tmp.getData().put(AbstractRelationship.createdBy.jsonName(), user.getUuid());
}
// create relationship including initial properties
final Relationship rel = startNode.createRelationshipTo(endNode, template, tmp.getData());
final R newRel = factory.instantiateWithType(rel, relType, null, true);
if (newRel != null) {
newRel.setProperties(securityContext, properties);
// notify transaction handler
TransactionCommand.relationshipCreated(user, newRel);
// notify relationship of its creation
newRel.onRelationshipCreation();
// iterate post creation transformations
for (Transformation<GraphObject> transformation : StructrApp.getConfiguration().getEntityCreationTransformations(newRel.getClass())) {
transformation.apply(securityContext, newRel);
}
}
// enable access time update again for subsequent calls
securityContext.enableModificationOfAccessTime();
return newRel;
}
use of org.structr.core.GraphObject in project structr by structr.
the class CypherQueryCommand method execute.
public List<GraphObject> execute(String query, Map<String, Object> parameters, boolean includeHiddenAndDeleted, boolean publicOnly) throws FrameworkException {
DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
RelationshipFactory relFactory = new RelationshipFactory(securityContext);
NodeFactory nodeFactory = new NodeFactory(securityContext);
List<GraphObject> resultList = new LinkedList<>();
// graphdb can be null..
if (graphDb != null) {
try (final NativeResult result = graphDb.execute(query, parameters != null ? parameters : Collections.emptyMap())) {
while (result.hasNext()) {
final Map<String, Object> row = result.next();
for (Entry<String, Object> entry : row.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
final Object obj = handleObject(nodeFactory, relFactory, key, value, includeHiddenAndDeleted, publicOnly, 0);
if (obj != null) {
if (obj instanceof GraphObject) {
resultList.add((GraphObject) obj);
} else if (obj instanceof Collection) {
final List<Object> nonGraphObjectResult = new LinkedList<>();
for (final Object item : ((Collection) obj)) {
if (item instanceof GraphObject) {
resultList.add((GraphObject) item);
} else {
nonGraphObjectResult.add(item);
}
}
if (!nonGraphObjectResult.isEmpty()) {
// Wrap non-graph-objects in simple list
final GraphObjectMap graphObject = new GraphObjectMap();
graphObject.setProperty(new GenericProperty(key), nonGraphObjectResult);
resultList.add(graphObject);
}
} else {
logger.warn("Unable to handle Cypher query result object of type {}, ignoring.", obj.getClass().getName());
}
}
}
}
}
}
return resultList;
}
use of org.structr.core.GraphObject in project structr by structr.
the class ReplaceFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
try {
if (!arrayHasLengthAndAllElementsNotNull(sources, 2)) {
return null;
}
final String template = sources[0].toString();
GraphObject node = null;
if (sources[1] instanceof GraphObject) {
node = (GraphObject) sources[1];
}
if (sources[1] instanceof List) {
final List list = (List) sources[1];
if (list.size() == 1 && list.get(0) instanceof GraphObject) {
node = (GraphObject) list.get(0);
}
}
if (node != null) {
// recursive replacement call, be careful here
return Scripting.replaceVariables(ctx, node, template);
}
return "";
} 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 SortFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
if (sources == null || sources.length == 0) {
return null;
}
// Default sort key
String sortKey = "name";
if (sources.length > 1 && sources[1] instanceof String) {
sortKey = (String) sources[1];
}
if (sources.length >= 1) {
if (sources[0] instanceof List) {
final List list = (List) sources[0];
final Iterator iterator = list.iterator();
if (iterator.hasNext()) {
final Object firstElement = iterator.next();
if (firstElement instanceof GraphObject) {
final Class type = firstElement.getClass();
final PropertyKey key = StructrApp.key(type, sortKey);
final boolean descending = sources.length == 3 && sources[2] != null && "true".equals(sources[2].toString());
if (key != null) {
List<GraphObject> sortCollection = (List<GraphObject>) list;
Collections.sort(sortCollection, new GraphObjectComparator(key, descending));
return sortCollection;
}
} else if (firstElement instanceof String) {
final String[] stringArray = (String[]) list.toArray(new String[list.size()]);
Arrays.sort(stringArray);
return Arrays.asList(stringArray);
}
}
}
} else {
logParameterError(caller, sources, ctx.isJavaScriptContext());
}
return sources[0];
}
use of org.structr.core.GraphObject in project structr by structr.
the class StructrPropertyValueChannel method getConvertedPropertyValue.
// ----- public static methods -----
public static String getConvertedPropertyValue(final SecurityContext securityContext, final GraphObject graphObject, final PropertyKey key) throws IOException, FrameworkException {
final PropertyConverter inputConverter = key.inputConverter(securityContext);
Object actualValue = graphObject.getProperty(key);
if (inputConverter != null) {
actualValue = inputConverter.revert(actualValue);
}
if (actualValue != null) {
return actualValue.toString();
}
return "";
}
Aggregations