use of org.structr.core.graph.RelationshipInterface in project structr by structr.
the class InvertibleModificationOperation method collectNodes.
private static void collectNodes(final DOMNode node, final Map<String, DOMNode> indexMappedNodes, final Map<String, DOMNode> hashMappedNodes, final Map<DOMNode, Integer> depthMap, final int depth, final Map<Integer, Integer> childIndexMap) {
// early exit
if (node == null) {
return;
}
Integer pos = childIndexMap.get(depth);
if (pos == null) {
pos = 0;
}
int position = pos;
childIndexMap.put(depth, ++position);
// store node with its tree index
final String hash = "[" + depth + ":" + position + "]";
indexMappedNodes.put(hash, node);
// store node with its data hash
String dataHash = node.getDataHash();
if (dataHash == null) {
dataHash = node.getIdHash();
}
hashMappedNodes.put(dataHash, node);
depthMap.put(node, depth);
// recurse
for (final RelationshipInterface childRel : node.getChildRelationships()) {
collectNodes((DOMNode) childRel.getTargetNode(), indexMappedNodes, hashMappedNodes, depthMap, depth + 1, childIndexMap);
}
}
use of org.structr.core.graph.RelationshipInterface in project structr by structr.
the class SearchCommand method getAllSubtypesAsStringSet.
public static synchronized Set<String> getAllSubtypesAsStringSet(final String type) {
Set<String> allSubtypes = subtypeMapForType.get(type);
if (allSubtypes == null) {
logger.debug("Subtype map cache miss.");
allSubtypes = new LinkedHashSet<>();
subtypeMapForType.put(type, allSubtypes);
final ConfigurationProvider configuration = StructrApp.getConfiguration();
final Map<String, Class<? extends NodeInterface>> nodeEntities = configuration.getNodeEntities();
final Map<String, Class<? extends RelationshipInterface>> relEntities = configuration.getRelationshipEntities();
// add type first (this is neccesary because two class objects of the same dynamic type node are not equal
// to each other and not assignable, if the schema node was modified in the meantime)
allSubtypes.add(type);
// scan all node entities for subtypes
for (final Map.Entry<String, Class<? extends NodeInterface>> entity : nodeEntities.entrySet()) {
final Class entityType = entity.getValue();
final Set<Class> ancestors = typeAndAllSupertypes(entityType);
for (final Class superClass : ancestors) {
final String superClasSimpleName = superClass.getSimpleName();
final String superClassFullName = superClass.getName();
if ((superClassFullName.startsWith("org.structr.") || superClassFullName.startsWith("com.structr.")) && superClasSimpleName.equals(type)) {
allSubtypes.add(entityType.getSimpleName());
}
}
}
// scan all relationship entities for subtypes
for (final Map.Entry<String, Class<? extends RelationshipInterface>> entity : relEntities.entrySet()) {
final Class entityType = entity.getValue();
final Set<Class> ancestors = typeAndAllSupertypes(entityType);
for (final Class superClass : ancestors) {
final String superClasSimpleName = superClass.getSimpleName();
final String superClassFullName = superClass.getName();
if ((superClassFullName.startsWith("org.structr.") || superClassFullName.startsWith("com.structr.")) && superClasSimpleName.equals(type)) {
allSubtypes.add(entityType.getSimpleName());
}
}
}
} else {
logger.debug("Subtype map cache hit.");
}
return Collections.unmodifiableSet(allSubtypes);
}
use of org.structr.core.graph.RelationshipInterface in project structr by structr.
the class StructrUiTest method createTestRelationships.
protected List<RelationshipInterface> createTestRelationships(final Class relType, final int number) throws FrameworkException {
List<GenericNode> nodes = createTestNodes(GenericNode.class, 2);
final GenericNode startNode = nodes.get(0);
final GenericNode endNode = nodes.get(1);
List<RelationshipInterface> rels = new LinkedList<>();
for (int i = 0; i < number; i++) {
rels.add(app.create(startNode, endNode, relType));
}
return rels;
}
use of org.structr.core.graph.RelationshipInterface in project structr by structr.
the class TypeResource method doPost.
@Override
public RestMethodResult doPost(final Map<String, Object> propertySet) throws FrameworkException {
// virtual type?
if (virtualType != null) {
virtualType.transformInput(securityContext, entityClass, propertySet);
}
if (isNode) {
final RestMethodResult result = new RestMethodResult(HttpServletResponse.SC_CREATED);
final NodeInterface newNode = createNode(propertySet);
if (newNode != null) {
result.addHeader("Location", buildLocationHeader(newNode));
result.addContent(newNode);
}
result.serializeAsPrimitiveArray(true);
// finally: return 201 Created
return result;
} else {
final App app = StructrApp.getInstance(securityContext);
final Relation template = getRelationshipTemplate();
final ErrorBuffer errorBuffer = new ErrorBuffer();
if (template != null) {
final NodeInterface sourceNode = identifyStartNode(template, propertySet);
final NodeInterface targetNode = identifyEndNode(template, propertySet);
final PropertyMap properties = PropertyMap.inputTypeToJavaType(securityContext, entityClass, propertySet);
RelationshipInterface newRelationship = null;
if (sourceNode == null) {
errorBuffer.add(new EmptyPropertyToken(entityClass.getSimpleName(), template.getSourceIdProperty()));
}
if (targetNode == null) {
errorBuffer.add(new EmptyPropertyToken(entityClass.getSimpleName(), template.getTargetIdProperty()));
}
if (errorBuffer.hasError()) {
throw new FrameworkException(422, "Source node ID and target node ID of relationsips must be set", errorBuffer);
}
template.ensureCardinality(securityContext, sourceNode, targetNode);
newRelationship = app.create(sourceNode, targetNode, entityClass, properties);
RestMethodResult result = new RestMethodResult(HttpServletResponse.SC_CREATED);
if (newRelationship != null) {
result.addHeader("Location", buildLocationHeader(newRelationship));
result.addContent(newRelationship);
}
result.serializeAsPrimitiveArray(true);
// finally: return 201 Created
return result;
}
// shouldn't happen
throw new NotFoundException("Type" + rawType + " does not exist");
}
}
use of org.structr.core.graph.RelationshipInterface in project structr by structr.
the class DeleteFunction method deleteObject.
// ----- private methods -----
private void deleteObject(final App app, final Object obj) throws FrameworkException {
if (obj instanceof NodeInterface) {
app.delete((NodeInterface) obj);
}
if (obj instanceof RelationshipInterface) {
app.delete((RelationshipInterface) obj);
}
if (obj instanceof Iterable) {
if (batched) {
final Iterable iterable = (Iterable) obj;
final Iterator iterator = iterable.iterator();
int count = 0;
while (iterator.hasNext()) {
try (final Tx tx = app.tx()) {
while (iterator.hasNext()) {
deleteObject(app, iterator.next());
if ((++count % batchSize) == 0) {
break;
}
}
tx.success();
}
logger.debug("Committing batch after {} objects", count);
// reset count
count = 0;
}
} else {
for (final Object o : (Iterable) obj) {
deleteObject(app, o);
}
}
}
}
Aggregations