@Override public String toString() { return asEnum().name(); }
private void addHashChangeNotifications(AbstractElement<?, ?> entity, Hashes newHashes, List<Notification<?, ?>> notifications) { DBG.debugf("Adding hash change notifications to entity %s", entity.getPath()); if (entity instanceof Syncable) { Syncable el = (Syncable) entity; if (!Objects.equals(el.getSyncHash(), newHashes.getSyncHash())) { notifications.add(new Notification<>(el, el, syncHashChanged())); } } if (entity instanceof ContentHashable) { ContentHashable el = (ContentHashable) entity; if (!Objects.equals(el.getContentHash(), newHashes.getContentHash())) { notifications.add(new Notification<>(el, el, contentHashChanged())); } } if (entity instanceof IdentityHashable) { IdentityHashable el = (IdentityHashable) entity; if (!Objects.equals(el.getIdentityHash(), newHashes.getIdentityHash())) { notifications.add(new Notification<>(el, el, identityHashChanged())); } } }
private boolean processDeletion(ProcessingTree<BE> changesTree, List<Notification<?, ?>> ns) { DBG.debugf("Checking for deletion of entity %s", changesTree.cp); Optional<Notification<?, ?>> deleteNotification = ns.stream() .filter(n -> n.getAction() == Action.deleted() && n.getValue() instanceof AbstractElement) .filter(n -> ((AbstractElement<?, ?>) n.getValue()).getPath().equals(changesTree.cp)) .findAny(); if (deleteNotification.isPresent()) { DBG.debugf("Processing deletion of the entity %s", changesTree.cp); //just emit what the caller wanted and don't bother any longer Notification<?, ?> n = deleteNotification.get(); correctedChanges.add(new EntityAndPendingNotifications<BE, AbstractElement<?, ?>>( null, (AbstractElement<?, ?>) n.getValue(), ns)); changesTree.dfsTraversal(pt -> { correctChanges(pt, false); return true; }); DBG.debugf("Done processing deletion of entity %s", changesTree.cp); return true; } DBG.debugf("No delete notifications found for entity %s", changesTree.cp); return false; }
@SuppressWarnings("unchecked") public static <BE, E extends AbstractElement<?, U>, U extends AbstractElement.Update> void update( Class<E> entityClass, Transaction<BE> tx, Query entityQuery, U update, TransactionParticipant<BE, U> preUpdateCheck, BiConsumer<BE, Transaction<BE>> postUpdateCheck) { BE entity = tx.querySingle(entityQuery); if (entity == null) { if (update instanceof Relationship.Update) { throw new RelationNotFoundException((String) null, Query.filters(entityQuery)); } else { throw new EntityNotFoundException(entityClass, Query.filters(entityQuery)); } } if (preUpdateCheck != null) { preUpdateCheck.execute(entity, update, tx); } E orig = tx.convert(entity, entityClass); tx.update(entity, update); if (postUpdateCheck != null) { postUpdateCheck.accept(entity, tx); } E updated = tx.convert(entity, entityClass); tx.getPreCommit().addNotifications(new EntityAndPendingNotifications<>(entity, updated, new Action.Update<>(orig, update), Action.updated())); }
tx.getPreCommit().addNotifications(new EntityAndPendingNotifications<>(be, e, deleted())); };
@Override public void update(String id, Relationship.Update update) throws RelationNotFoundException { //TODO this doesn't respect the current position in the graph inTx(tx -> { try { BE relationshipObject = tx.find(CanonicalPath.of().relationship(id) .get()); tx.update(relationshipObject, update); Relationship r = tx.convert(relationshipObject, Relationship.class); tx.getPreCommit().addNotifications(new EntityAndPendingNotifications<>(relationshipObject, r, new Notification<>(new Action.Update<>(r, update), r, updated()))); return null; } catch (ElementNotFoundException e) { throw new RelationNotFoundException(id, Query.filters(context.select().with(RelationWith.id(id)).get())); } }); }
@Override protected EntityAndPendingNotifications<BE, MetadataPack> wireUpNewEntity(BE entity, MetadataPack.Blueprint blueprint, CanonicalPath parentPath, BE parent, Transaction<BE> tx) { Set<Notification<?, ?>> newRels = new HashSet<>(); blueprint.getMembers().forEach((p) -> { try { BE member = tx.find(p); BE rel = tx.relate(entity, member, incorporates.name(), null); Relationship r = tx.convert(rel, Relationship.class); newRels.add(new Notification<>(r, r, created())); } catch (ElementNotFoundException e) { throw new EntityNotFoundException(p.getSegment().getElementType().getSimpleName(), Query.filters(Query.to(p))); } }); MetadataPack entityObject = tx.convert(entity, MetadataPack.class); return new EntityAndPendingNotifications<>(entity, entityObject, newRels); }
public static <BE> EntityAndPendingNotifications<BE, Relationship> deleteAssociation(Transaction<BE> tx, Query sourceQuery, SegmentType sourceType, String relationship, BE target) { BE source = getSingle(tx, sourceQuery, sourceType); BE relationshipObject; try { relationshipObject = tx.getRelationship(source, target, relationship); } catch (ElementNotFoundException e) { throw new RelationNotFoundException(sourceType, relationship, Query.filters(sourceQuery), null, e); } RelationshipRules.checkDelete(tx, source, Relationships.Direction.outgoing, relationship, target); Relationship ret = tx.convert(relationshipObject, Relationship.class); tx.delete(relationshipObject); return new EntityAndPendingNotifications<>(relationshipObject, ret, new Notification<>(ret, ret, deleted())); }
.filter(n -> n.getAction().asEnum() == Action.Enumerated.CREATED) .findAny();
@Override public void delete(String id) throws RelationNotFoundException { //TODO this doesn't respect the current position in the graph inTx((tx) -> { try { BE relationshipObject = tx.find(CanonicalPath.of().relationship(id).get()); BE source = tx.getRelationshipSource(relationshipObject); BE target = tx.getRelationshipTarget(relationshipObject); String relationshipName = tx.extractRelationshipName(relationshipObject); RelationshipRules.checkDelete(tx, source, Relationships.Direction.outgoing, relationshipName, target); Relationship r = tx.convert(relationshipObject, Relationship.class); tx.delete(relationshipObject); tx.getPreCommit().addNotifications( new EntityAndPendingNotifications<>(relationshipObject, r, deleted())); return null; } catch (ElementNotFoundException e) { throw new RelationNotFoundException(id, Query.filters(context.select().with(RelationWith.id(id)).get())); } }); } }
public static <BE> EntityAndPendingNotifications<BE, Relationship> createAssociation(Transaction<BE> tx, BE source, String relationship, BE target, Map<String, Object> properties) { if (tx.hasRelationship(source, target, relationship)) { throw new RelationAlreadyExistsException(relationship, Query.filters(Query.to(tx.extractCanonicalPath (source)))); } RelationshipRules.checkCreate(tx, source, Relationships.Direction.outgoing, relationship, target); BE relationshipObject = tx.relate(source, target, relationship, properties); Relationship ret = tx.convert(relationshipObject, Relationship.class); return new EntityAndPendingNotifications<>(relationshipObject, ret, new Notification<>(ret, ret, created())); }