throw new EntityNotFoundException(originEntityType, Query.filters(context.select().get()));
private static <BE> BaseMetricTypes.Read<BE> proceedToMetricTypes(TraversalContext<BE, Tenant> context, Tenants.MetricTypeParents... parents) { return new BaseMetricTypes.Read<>(context.proceedWithParents(MetricType.class, Tenants.MetricTypeParents.class, Tenants.MetricTypeParents.TENANT, parents, (p, extender) -> { switch (p) { case FEED: extender.path().with(by(contains), type(Feed.class), by(contains), type(MetricType.class)); break; case TENANT: extender.path().with(by(contains), type(MetricType.class)); break; default: throw new AssertionError("Unhandled parent type " + p); } })); } }
/** * Appends the filters as query fragments determined by the last call to {@link #filter()} or {@link #path()}. * * <p>The filters is an array of arrays representing a new set of branches to be created at all the leaves of * the current query tree. * * @param filters the filters to append to the leaves of the query tree * @return this instance */ public SymmetricExtender with(Filter[][] filters) { onLeaves(this.filters, (builder) -> { for (Filter[] fs : filters) { builder.branch().with(queryFragmentSupplier.apply(fs)); } }); return this; }
@Override public void synchronize(SyncRequest<B> syncRequest) { inTx(tx -> { BE root = tx.querySingle(context.select().get());
tx.<Pair<BE, E>>query(context.select().get(), pager, conversion, filter);
private static <BE> BaseResources.Read<BE> proceedToResources(TraversalContext<BE, Environment> context, Environments.ResourceParents... parents) { return new BaseResources.Read<>(context.proceedWithParents(Resource.class, Environments.ResourceParents.class, Environments.ResourceParents.ENVIRONMENT, parents, (p, extender) -> { switch (p) { case ENVIRONMENT: extender.path().with(by(contains), type(Resource.class)); break; case FEED: extender.path() .with(by(incorporates), type(Feed.class), by(contains), type(Resource.class)); break; case RESOURCE: //go to root resources, wherever they are extender.path().with(new Filter[][]{ {by(contains), type(Resource.class)}, {by(incorporates), type(Feed.class), by(contains), type(Resource.class)}}); //and then go to the child resources extender.path().with(RecurseFilter.builder().addChain(by(isParentOf), type(Resource .class)).build()); break; default: throw new AssertionError("Unhandled type of resource parent under environment."); } })); }
/** * Loads the entity from the backend and let's the caller do some conversion on either the backend representation of * the entity or the converted entity (both of these are required even during the loading so no unnecessary work is * done by providing both of the to the caller). * * @param conversion the conversion function taking the backend entity as well as the model entity * @param <T> the result type * @return the converted result of loading the entity * @throws EntityNotFoundException * @throws RelationNotFoundException */ protected <T> T loadEntity(EntityConvertor<BE, E, T> conversion) throws EntityNotFoundException, RelationNotFoundException { return inTx(tx -> { BE result = tx.querySingle(context.select().get()); if (result == null) { throwNotFoundException(); } E entity = tx.convert(result, context.entityClass); if (!isApplicable(entity)) { throwNotFoundException(); } return conversion.convert(result, entity, tx); }); }
private static <BE> BaseMetrics.Read<BE> proceedToMetrics(TraversalContext<BE, Environment> context, Environments.MetricParents... parents) { return new BaseMetrics.Read<>(context.proceedWithParents(Metric.class, Environments.MetricParents.class, Environments.MetricParents.ENVIRONMENT, parents, (p, extender) -> { switch (p) { case ENVIRONMENT: extender.path().with(by(contains), type(Metric.class)); break; case FEED: extender.path().with(by(incorporates), type(Feed.class), by(contains), type(Metric.class)); break; case RESOURCE: //out by all possible paths from env to a resource extender.path().with(new Filter[][]{ {by(contains), type(Resource.class)}, {by(incorporates), type(Feed.class), by(contains), type(Resource.class)}}); //out to direct metrics and also metrics of child resources extender.path().with(new Filter[][]{ {by(contains), type(Metric.class)}, {RecurseFilter.builder().addChain(by(contains), type(Resource.class)).build(), by(contains), type(Metric.class)}}); break; default: throw new AssertionError("Unhandled type of metric parent under environment."); } })); }
private static <BE> BaseMetrics.Read<BE> proceedToMetrics(TraversalContext<BE, Feed> context, Feeds.MetricParents... parents) { return new BaseMetrics.Read<>(context.proceedWithParents(Metric.class, Feeds.MetricParents.class, Feeds.MetricParents.FEED, parents, (p, extender) -> { switch (p) { case FEED: extender.path().with(by(contains), type(Metric.class)); break; case RESOURCE: //go to the root resources and their children extender.path().with(new Filter[][]{ {by(contains), type(Resource.class)}, {by(contains), type(Resource.class), RecurseFilter.builder().addChain( by(contains), type(Resource.class)).build()}}); //and from the resources, go to the metrics extender.path().with(by(contains), type(Metric.class)); break; default: throw new AssertionError("Unhandled type of resource parent under feed."); } })); } }
@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 preUpdate(BE dataEntity, DataEntity.Update update, Transaction<BE> tx) { if (update.getValue() == null) { return; } BE mp = tx.traverseToSingle(dataEntity, Query.path().with( asTargetBy(contains), //up to operation type asTargetBy(contains), //up to resource type asTargetBy(incorporates), With.type(MetadataPack.class) // up to the pack ).get()); if (mp != null) { CanonicalPath dataPath = tx.extractCanonicalPath(dataEntity); throw new IllegalArgumentException( "Data '" + dataPath.getSegment().getElementId() + "' cannot be updated" + " under operation type " + dataPath.up() + ", because the owning resource type is part of a meta data pack." + " Doing this would invalidate meta data pack's identity."); } }
private Map.Entry<InventoryStructure<B>, SyncHash.Tree> treeHashAndStructure(Transaction<BE> tx) { BE root = tx.querySingle(context.select().get()); E entity = tx.convert(root, context.entityClass); SyncHash.Tree.Builder bld = SyncHash.Tree.builder(); InventoryStructure.Builder<B> structBld = InventoryStructure.of(Inventory.asBlueprint(entity)); bld.withPath(RelativePath.empty().get()).withHash(entity.getSyncHash()); //the closure is returned in a breadth-first manner Iterator<BE> closure = tx.getTransitiveClosureOver(root, outgoing, contains.name()); if (closure.hasNext()) { Function<BE, Entity<? extends Entity.Blueprint, ?>> convert = e -> (Entity<Entity.Blueprint, ?>) tx.convert(e, tx.extractType(e)); Stream<BE> st = StreamSupport.stream(Spliterators.spliteratorUnknownSize(closure, 0), false); Iterator<Entity<? extends Entity.Blueprint, ?>> entities = st.map(convert).iterator(); buildChildTree(tx, entity.getPath(), singletonList(bld), singletonList(structBld), new ArrayList<>(), new ArrayList<>(), entities.next(), entities); } return new SimpleImmutableEntry<>(structBld.build(), bld.build()); }
@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())); } }); }
/** * This is a sort of copy constructor. * Can be used by subclasses when implementing the {@link #cloneWith(TransactionConstructor)} method. * * @param orig the original instance to copy stuff over from * @param backend if not null, then use this backend instead of the one used by {@code orig} * @param transactionConstructor if not null, then use this ctor instead of the one used by {@code orig} */ protected BaseInventory(BaseInventory<E> orig, InventoryBackend<E> backend, TransactionConstructor<E> transactionConstructor) { this.observableContext = orig.observableContext; this.configuration = orig.configuration; this.backend = backend == null ? orig.backend : backend; this.transactionConstructor = transactionConstructor == null ? orig.transactionConstructor : transactionConstructor; tenantContext = new TraversalContext<>(this, Query.empty(), Query.path().with(With.type(Tenant.class)).get(), this.backend, Tenant.class, configuration, observableContext, this.transactionConstructor); relationshipContext = new TraversalContext<>(this, Query.empty(), Query.path().get(), this.backend, Relationship.class, configuration, observableContext, this.transactionConstructor); }
private static <BE> BaseResourceTypes.Read<BE> proceedToResourceTypes(TraversalContext<BE, Tenant> context, Tenants.ResourceTypeParents... parents) { return new BaseResourceTypes.Read<>(context.proceedWithParents(ResourceType.class, Tenants.ResourceTypeParents.class, Tenants.ResourceTypeParents.TENANT, parents, (p, extender) -> { switch (p) { case FEED: extender.path() .with(by(contains), type(Feed.class), by(contains), type(ResourceType.class)); break; case TENANT: extender.path().with(by(contains), type(ResourceType.class)); break; default: throw new AssertionError("Unhandled parent type " + p); } })); }
@Override public void preUpdate(BE dataEntity, DataEntity.Update update, Transaction<BE> tx) { if (update.getValue() == null) { return; } BE mp = tx.traverseToSingle(dataEntity, Query.path().with( Related.asTargetBy(contains), //up to resource type Related.asTargetBy(incorporates), With.type(MetadataPack.class) // up to the pack ).get()); if (mp != null) { CanonicalPath dataPath = tx.extractCanonicalPath(dataEntity); throw new IllegalArgumentException( "Data '" + dataPath.getSegment().getElementId() + "' cannot be updated" + " under resource type " + dataPath.up() + ", because it is part of a meta data pack." + " Doing this would invalidate meta data pack's identity."); } }
public static <BE> void validate(Transaction<BE> tx, StructuredData data, BE dataEntity) { CanonicalPath path = tx.extractCanonicalPath(dataEntity); DataRole role = DataRole.valueOf(path.ids().getDataRole()); if (role.isSchema()) { try { JsonNode schema = new JsonNodeReader(new ObjectMapper()) .fromInputStream(BaseData.class.getResourceAsStream("/json-meta-schema.json")); CanonicalPath dataPath = tx.extractCanonicalPath(dataEntity); validate(dataPath, convert(data), schema); } catch (IOException e) { throw new IllegalStateException("Could not load the embedded JSON Schema meta-schema."); } } else { validateIfSchemaFound(tx, data, dataEntity, Query.path().with(navigateToSchema(role)).get()); } }
private static <BE> BaseResources.Read<BE> proceedToResources(TraversalContext<BE, Feed> context, Feeds.ResourceParents... parents) { return new BaseResources.Read<>(context.proceedWithParents(Resource.class, Feeds.ResourceParents.class, Feeds.ResourceParents.FEED, parents, (p, extender) -> { switch (p) { case FEED: extender.path().with(by(contains), type(Resource.class)); break; case RESOURCE: extender.path().with(by(contains), type(Resource.class), RecurseFilter.builder() .addChain(by(contains), type(Resource.class)).build()); break; default: throw new AssertionError("Unhandled type of resource parent under feed."); } })); }
@Override public Element descendToData(Element dataEntityRepresentation, RelativePath dataPath) { Query q = Query.path().with(With.dataAt(dataPath)).get(); GraphTraversal<?, ? extends Element> pipeline = dataEntityRepresentation instanceof Vertex ? context.getGraph().traversal().V(dataEntityRepresentation) : context.getGraph().traversal().E(dataEntityRepresentation); FilterApplicator.applyAll(q, pipeline); return drainAfter(pipeline, () -> { if (pipeline.hasNext()) { return pipeline.next(); } else { return null; } }); }