diff --git a/freebim-parent/freebim-data-neo4j/src/main/java/at/freebim/db/service/impl/RelationServiceImpl.java b/freebim-parent/freebim-data-neo4j/src/main/java/at/freebim/db/service/impl/RelationServiceImpl.java index ac3c066..52e21f5 100644 --- a/freebim-parent/freebim-data-neo4j/src/main/java/at/freebim/db/service/impl/RelationServiceImpl.java +++ b/freebim-parent/freebim-data-neo4j/src/main/java/at/freebim/db/service/impl/RelationServiceImpl.java @@ -729,7 +729,10 @@ public UpdateRelati List nodeFields = new ArrayList(); for(Class c:classes) { for(Field f:c.getDeclaredFields()) { - nodeFields.add(f); + Relationship t = f.getAnnotation(Relationship.class); + if (t != null) { + nodeFields.add(f); + } } } @@ -793,6 +796,38 @@ public UpdateRelati BaseNode rn = this.template.load(BaseNode.class, relatedNodeId); rn = loadNode(relatedNodeId, rn.getClass()); + //get all fields + Set> classesRn = getAllExtendedOrImplementedTypesRecursively(rn.getClass()); + List nodeFieldsRn = new ArrayList(); + for(Class c:classesRn) { + for(Field fRn:c.getDeclaredFields()) { + Relationship tRn = fRn.getAnnotation(Relationship.class); + if (tRn != null) { + nodeFieldsRn.add(fRn); + } + } + } + + Field currentRn = null; + for(Field fRn: nodeFields) { + fRn.setAccessible(true); + Relationship tRn = fRn.getAnnotation(Relationship.class); + if (tRn != null) { + String typeRn = tRn.type(); + String directionRn = tRn.direction(); + if (typeRn.equals(t.type()) && (((direction.equals("OUT") && directionRn.equals("INCOMING")) || + (direction.equals("IN") && directionRn.equals("OUTGOING")) || direction.equals("BOTH")))) { + currentRn = fRn; + + if (currentRn.get(rn) == null) { + currentRn.set(rn, currentRn.getType().newInstance()); + } + + break; + } + } + } + if (rn != null) { try { if (in) { @@ -817,29 +852,50 @@ public UpdateRelati if (rel.getId() != null) { BaseRel existingRel = existingRelations.get(rel.getId()); if (rel.equals(existingRel)) { - existingRelations.remove(rel.getId()); + rel.setId(existingRel.getId()); + ((ArrayList>)tempRel).add((BaseRel)existingRel); logger.debug("old {} relation unchanged for nodeId={}", type, nodeId); } else { + ((ArrayList>)tempRel).add(rel); + if (currentRn != null) { BaseRel toRemove = null; - for(BaseRel updateRel: (ArrayList>)tempRel) { - if (updateRel.getId().equals(rel.getId())) { - toRemove = updateRel; + for(BaseRel rnRel:((ArrayList>)currentRn.get(rn))) { + if (rnRel.getId().equals(rel.getId())) { + toRemove = rnRel; } } - if (toRemove != null) { - ((ArrayList>)tempRel).remove(toRemove); + ((ArrayList>)currentRn.get(rn)).remove(toRemove); } - + ((ArrayList>)currentRn.get(rn)).add(rel); + } + updateRelationsResult.affectedNodes.add(nodeId); + updateRelationsResult.affectedNodes.add(rn.getNodeId()); + } + existingRelations.remove(rel.getId()); + } else { + rel.setId(null); + ((ArrayList>)tempRel).add(rel); + if (currentRn != null) { + ((ArrayList>)currentRn.get(rn)).add(rel); } + updateRelationsResult.affectedNodes.add(nodeId); + updateRelationsResult.affectedNodes.add(rn.getNodeId()); } - ((ArrayList>)tempRel).add(rel); + } } } f.set(node, tempRel); + + //delete remaining relationships + for(BaseRel rel: existingRelations.values()) { + Map params = new HashMap<>(); + params.put("nodeId", rel.getId()); + this.getTemplate().query("Match (n)-[x]-(y) where id(x)={nodeId} delete x;", params); + } } } catch (IllegalArgumentException | IllegalAccessException | SecurityException | InstantiationException e) { logger.debug("old {} Exception when updating relations for nodeId={}", type, nodeId); @@ -849,7 +905,7 @@ public UpdateRelati } } } - node = saveNode(node); + this.getTemplate().save(node); updateRelationsResult.baseNode = node;