TraversalContext<BE, E> toCreatedEntity(E entity, boolean cache) { return new TraversalContext<>(inventory, Query.to(entity.getPath()), Query.empty(), backend, entityClass, configuration, observableContext, transactionRetries, this, cache ? entity : null, null); }
private void createCustomRelationships(BE entity, Relationships.Direction direction, Map<String, Set<CanonicalPath>> otherEnds, Transaction<BE> tx) { otherEnds.forEach((name, ends) -> ends.forEach((end) -> { try { BE endObject = tx.find(end); BE from = direction == outgoing ? entity : endObject; BE to = direction == outgoing ? endObject : entity; EntityAndPendingNotifications<BE, Relationship> res = Util.createAssociation(tx, from, name, to, null); tx.getPreCommit().addNotifications(res); } catch (ElementNotFoundException e) { throw new EntityNotFoundException(Query.filters(Query.to(end))); } })); } }
@SuppressWarnings("unchecked") public static Query queryTo(Query sourcePath, Path path) { if (path instanceof CanonicalPath) { return Query.to((CanonicalPath) path); } else { Query.SymmetricExtender extender = sourcePath.extend().path(); extender.with(With.relativePath(null, (RelativePath) path)); return extender.get(); } }
@SuppressWarnings("unchecked") @Override protected String getProposedId(Transaction<BE> tx, MetadataPack.Blueprint blueprint) { Iterator<? extends Entity<? extends Entity.Blueprint, ?>> members = blueprint.getMembers().stream() .map((p) -> { SegmentType type = p.getSegment().getElementType(); Class<?> cls = Entity.entityTypeFromSegmentType(type); try { BE e = tx.find(p); return (Entity<? extends Entity.Blueprint, ?>) tx.convert(e, cls); } catch (ElementNotFoundException ex) { throw new EntityNotFoundException(cls, Query.filters(Query.to(p))); } }).iterator(); return IdentityHash.of(members, context.inventory.keepTransaction(tx)); }
private boolean __correctChangesPrologue(ProcessingTree<BE> changedEntity) { if (changedEntity.cp == null) { return false; } if (!changedEntity.isSignificant()) { //fast track - this is an entity that has not been CRUD'd but is present in the tree to complete the //hierarchy DBG.debug("Entity not hash-significant. Proceeding quickly."); return false; } if (!(Entity.Blueprint.class.isAssignableFrom( Inventory.types().bySegment(changedEntity.cp.getSegment().getElementType()).getBlueprintType()))) { //this is currently the case for metadata packs, which do not have their IDs assigned by the user. //we therefore mark them as processed. DBG.debug("Metadatapacks not processable. Bailing out quickly."); return true; } try { DBG.debug("Refreshing the entity in the current transaction."); changedEntity.loadFrom(tx); } catch (ElementNotFoundException e) { //ok, we're inside a delete and the root entity no longer exists... bail out quickly... if (processDeletion(changedEntity, changedEntity.notifications)) { return true; } else { throw new EntityNotFoundException(Query.filters(Query.to(changedEntity.cp))); } } return processDeletion(changedEntity, changedEntity.notifications); }
@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> 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())); }