public static <BE> BE getSingle(Transaction<BE> backend, Query query, SegmentType entityType) { BE result = backend.querySingle(query); if (result == null) { throw new EntityNotFoundException(entityType, Query.filters(query)); } return result; }
static void throwNotFoundException(TraversalContext<?, ?> context) { if (Entity.class.isAssignableFrom(context.entityClass)) { throw new EntityNotFoundException(context.entityClass, Query.filters(context.select().get())); } else { //TODO this is not correct? throw new RelationNotFoundException((String) null, Query.filters(context.sourcePath)); } }
private static void addPathsToLeaves(Query tree, List<Filter[]> workingPath, List<List<Filter>> results) { if (tree.getSubTrees().isEmpty()) { //this is the leaf List<Filter> pathToLeaf = new ArrayList<>(); Consumer<Filter> addOps = (f) -> { if (!(f instanceof NoopFilter)) { pathToLeaf.add(f); } }; for (Filter[] fs : workingPath) { Arrays.asList(fs).forEach(addOps); } Arrays.asList(filters(tree.getFragments())).forEach(addOps); results.add(pathToLeaf); } else { workingPath.add(filters(tree.getFragments())); for (Query subTree : tree.getSubTrees()) { addPathsToLeaves(subTree, workingPath, results); } workingPath.remove(workingPath.size() - 1); } }
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") @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)); }
public static <BE> Relationship getAssociation(Transaction<BE> tx, Query sourceQuery, SegmentType sourceType, Query targetQuery, SegmentType targetType, String rel) { BE source = getSingle(tx, sourceQuery, sourceType); BE target = getSingle(tx, targetQuery, targetType); BE relationship; try { relationship = tx.getRelationship(source, target, rel); } catch (ElementNotFoundException e) { throw new RelationNotFoundException(sourceType, rel, Query.filters(sourceQuery), null, null); } return tx.convert(relationship, Relationship.class); }
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); }
@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())); }
@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())); }
throw new EntityNotFoundException(originEntityType, Query.filters(context.select().get()));
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())); }
@Override protected String getProposedId(Transaction<BE> tx, Feed.Blueprint blueprint) { BE tenant = tx.querySingle(context.sourcePath.extend().filter() .with(type(Tenant.class)).get()); if (tenant == null) { throw new EntityNotFoundException(Tenant.class, Query.filters(context.sourcePath)); } CanonicalPath feedPath = tx.extractCanonicalPath(tenant) .extend(Feed.SEGMENT_TYPE, blueprint.getId()).get(); return context.configuration.getFeedIdStrategy().generate(context.inventory.keepTransaction(tx), new Feed(feedPath, null, null, null)); }
@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())); } }); } }
@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())); } }); }
throw new EntityAlreadyExistsException(id, Query.filters(existenceCheck));