/** * helper to load nodes relation to document * * @param document relationLink document/link * @param direction "in" or "out" * @return INode instance or null */ private @Nullable INode getRelatedEntity(ODocument document, String direction) { Object nodeO = document.field(direction, ORecordId.class); if (nodeO == null) { logger.error( "Could not create related entity while converting relation with direction " + direction); return null; } if (nodeO instanceof ORecordId) nodeO = repositoryFactory.getDb().load((ORecordId) nodeO); if (nodeO == null) { logger.error("Invalid record in direction " + direction + ": " + document.toString()); return null; } // convert ODocument nodeDoc = (ODocument) nodeO; // slim node: just set title and id INode node = new Node(); node.setTitle(nodeDoc.field("title")); node.setId(nodeDoc.getIdentity().toString()); return node; /* old -not performant String id; if (relationO instanceof OIdentifiable) id = ((ORecordId)relationO).getIdentity().toString(); else { logger.error("Invalid class type: " + relationO.getClass().getName()); return null; } return nodeRepository.find(id);*/ }
@Override public void deleteByRelation(INode node) { // delete source reference pointing to relation node OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<>( "select * from Relation where relationLink.in = " + node.getId() + " OR relationLink.out = " + node.getId()); List<ODocument> result = db.command(query).execute(); // remove relation and link for (ODocument document : result) { ODocument relationLink = getRelationLink(document, true); if (relationLink != null && db.load(relationLink.getIdentity()) != null) relationLink.delete(); document.delete(); } }
@Override public List<IRelation> findByRelation(INode node) { List<IRelation> list = new LinkedList<>(); // no NPEs if (node == null) return list; initDb(); // execute query OSQLSynchQuery<ODocument> query = new OSQLSynchQuery<>( "select * from Relation where relationLink.in = " + node.getId() + " OR relationLink.out = " + node.getId()); List<ODocument> result = db.command(query).execute(); // populate set for (ODocument document : result) list.add(convertToEntity(document)); return list; }