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)); } }
@Override protected void preCreate(OperationType.Blueprint blueprint, Transaction<BE> tx) { //disallow this if the parent resource type is a part of a metadata pack if (tx.traverseToSingle(getParent(tx), Query.path().with(asTargetBy(incorporates), With.type(MetadataPack.class)).get()) != null) { throw new IllegalArgumentException("Cannot create an operation type of resource type included in " + "a meta data pack. This would invalidate the metadata pack's identity."); } }
public final void update(Id id, U update) throws EntityNotFoundException { inTx(tx -> { Query q = id == null ? context.select().get() : context.select().with(id(id.toString())).get(); Util.update(context.entityClass, tx, q, update, (e, u, t) -> preUpdate(id, e, u, t), this::postUpdate); return null; }); }
@SuppressWarnings({"ConstantConditions", "unchecked"}) public static Query to(CanonicalPath entity) { return Query.path().with(With.path(entity)).get(); }
@Override public void preCreate(DataEntity.Blueprint blueprint, Transaction<BE> tx) { BE pack = tx.querySingle(context.select().path().with(Related .asTargetBy(incorporates), With.type(MetadataPack.class)).get()); if (pack != null) { BE rt = tx.querySingle(context.select().get()); throw new IllegalArgumentException( "Data '" + blueprint.getId() + "' cannot be created" + " under resource type " + tx.extractCanonicalPath(rt) + ", because it is part of a meta data pack." + " Doing this would invalidate meta data pack's identity."); } }
@Override public void preCreate(DataEntity.Blueprint blueprint, Transaction<BE> transaction) { BE mp = transaction.querySingle(context.select().path().with(asTargetBy(contains), asTargetBy(incorporates), With.type(MetadataPack.class)).get()); if (mp != null) { BE ot = transaction.querySingle(context.select().get()); throw new IllegalArgumentException( "Data '" + blueprint.getId() + "' cannot be created" + " under operation type " + transaction.extractCanonicalPath(ot) + ", because the owning resource type is part of a meta data pack." + " Doing this would invalidate meta data pack's identity."); } }
private static <BE> boolean isInMetadataPack(BE metricType, Transaction<BE> tx) { return tx.traverseToSingle(metricType, Query.path().with(Related.asTargetBy(incorporates), With.type(MetadataPack.class)).get()) != null; }
private static <BE> boolean isResourceTypeInMetadataPack(BE operationType, Transaction<BE> tx) { return tx.traverseToSingle(operationType, Query.path().with(asTargetBy(contains), asTargetBy(incorporates), With.type(MetadataPack.class)).get()) != null; } }
static <BE> Relationship disassociate(TraversalContext<BE, ?> context, SegmentType sourceEntityType, Relationships.WellKnown relationship, Path id) { return inTx(context, tx -> { BE target = Util.find(tx, context.sourcePath, id); Query sourceQuery = context.sourcePath.extend().filter().with(type(sourceEntityType)).get(); EntityAndPendingNotifications<BE, Relationship> rel = Util.deleteAssociation(tx, sourceQuery, sourceEntityType, relationship.name(), target); tx.getPreCommit().addNotifications(rel); return rel.getEntity(); }); }
@Override public final void initialize(Configuration configuration) { this.backend = doInitialize(configuration); tenantContext = new TraversalContext<>(this, Query.empty(), Query.path().with(With.type(Tenant.class)).get(), backend, Tenant.class, configuration, observableContext, transactionConstructor); relationshipContext = new TraversalContext<>(this, Query.empty(), Query.path().get(), backend, Relationship.class, configuration, observableContext, transactionConstructor); this.configuration = configuration; }
@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(); } }
@Override public void update(U u) throws EntityNotFoundException, RelationNotFoundException { inTx(tx -> { Util.update(context.entityClass, tx, context.select().get(), u, this::preUpdate, this::postUpdate); return null; }); if (useCachedEntity && context.getCreatedEntity() != null) { context.setCreatedEntity((E) context.getCreatedEntity().update().with(u)); } }
/** * @return a new traversal context set up using this builder */ TraversalContext<BE, E> get() { return new TraversalContext<>(sourceContext.inventory, pathExtender.get(), selectExtender.get(), sourceContext.backend, entityClass, sourceContext.configuration, sourceContext.observableContext, sourceContext.transactionRetries, sourceContext, null, sourceContext.transactionConstructor); }
static <BE> Relationship associate(TraversalContext<BE, ?> context, SegmentType sourceEntityType, Relationships.WellKnown relationship, Path id) { return inTx(context, tx -> { BE target = Util.find(tx, context.sourcePath, id); Query sourceQuery = context.sourcePath.extend().filter().with(type(sourceEntityType)).get(); BE source = tx.querySingle(sourceQuery); EntityAndPendingNotifications<BE, Relationship> rel = Util.createAssociation(tx, source, relationship.name(), target, null); tx.getPreCommit().addNotifications(rel); return rel.getEntity(); }); }
@SuppressWarnings("unchecked") public static Query extendTo(TraversalContext<?, ?> context, Path path) { if (path instanceof CanonicalPath) { return context.select().with(With.path((CanonicalPath) path)).get(); } else { Marker marker = Marker.next(); return context.sourcePath.extend().path().with(marker).with(context.selectCandidates) .with(With.relativePath(marker.getLabel(), (RelativePath) path)).get(); } }
private static <BE> boolean isResourceTypeInMetadataPack(BE resourceType, Transaction<BE> tx) { return tx.traverseToSingle(resourceType, Query.path() .with(asTargetBy(incorporates), type(MetadataPack.class)).get()) != null; } }
@Override public void delete() { inTx(tx -> { Util.delete(context.entityClass, tx, context.select().get(), this::preDelete, this::postDelete); return null; }); useCachedEntity = false; context.setCreatedEntity(null); }
@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)); }
static <BE> Relationship associationWith(TraversalContext<BE, ?> context, SegmentType sourceEntityType, Relationships.WellKnown relationship, Path path) throws RelationNotFoundException { return inTx(context, tx -> { Query sourceQuery = context.sourcePath.extend().filter().with(type(sourceEntityType)).get(); Query targetQuery = Util.queryTo(context.sourcePath, path); SegmentType targetType = path.getSegment().getElementType(); return Util.getAssociation(tx, sourceQuery, sourceEntityType, targetQuery, targetType, relationship.name()); }); }