/** * If the provided path is canonical, it is prefixed with the {@code canonicalPrefix} and returned. If the provided * path is relative, it is resolved against the {@code relativeOrigin} and then converted to a canonical path. * <p> * <p>The path can be partially untyped. * * @param path the string representation of a path (either canonical or relative) * @param canonicalPrefix the prefix to apply to a canonical path * @param relativeOrigin origin to resolve a relative path against * @param intendedFinalType the intended type of the final segment of the path * @return the canonical path represented by the provided path string * @see Path#fromPartiallyUntypedString(String, CanonicalPath, CanonicalPath, SegmentType) */ public static CanonicalPath canonicalize(String path, CanonicalPath canonicalPrefix, CanonicalPath relativeOrigin, SegmentType intendedFinalType) { Path p = Path.fromPartiallyUntypedString(path, canonicalPrefix, relativeOrigin, intendedFinalType); if (p instanceof RelativePath) { return ((RelativePath) p).applyTo(relativeOrigin); } else { return p.toCanonicalPath(); } }
@Override public Path deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { String val = jp.getValueAsString(); if (val.isEmpty()) { return RelativePath.empty().get(); } return Path.fromString(jp.getValueAsString()); } }
protected void checkPathLegal(Path targetPath) { if (!context.entityClass.getSimpleName().equals(targetPath.getSegment().getElementType().getSimpleName())) { throw new IllegalArgumentException("Current position in the inventory traversal expects entities of type " + context.entityClass.getSimpleName() + " which is incompatible with the provided path: " + targetPath); } }
@Override public Path deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { String str = jp.getValueAsString(); CanonicalPath co = CURRENT_CANONICAL_ORIGIN.get(); CanonicalPath ro = CURRENT_RELATIVE_PATH_ORIGIN.get(); Class<?> entityType = CURRENT_ENTITY_TYPE.get(); return Path.fromPartiallyUntypedString(str, co, ro, getIntendedFinalType(entityType)); }
/** * Tries to find an element at given path. * * @param tx current transaction in the traversal (if path is canonical, this is not used) * @param path either canonical or relative path of the element to find * @return the element * @throws ElementNotFoundException if the element is not found */ @SuppressWarnings("unchecked") public static <BE> BE find(Transaction<BE> tx, Query sourcePath, Path path) throws EntityNotFoundException { BE element; if (path.isCanonical()) { try { element = tx.find(path.toCanonicalPath()); } catch (ElementNotFoundException e) { throw new EntityNotFoundException("Entity not found on path: " + path); } } else { Query query = queryTo(sourcePath, path); element = getSingle(tx, query, null); } return element; }
@Override public void serialize(Path value, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeString(value.toString()); } }
@Override public void serialize(Path value, JsonGenerator jgen, SerializerProvider provider) throws IOException { //this is easy for relative paths - just use its string representation if (value instanceof RelativePath) { jgen.writeString(value.toString()); return; } //strip the tenant from the canonical path so that it appears to users it doesn't even exist. They don't //need to provide it in their paths and the API doesn't show it to them either. String output = value.toString(); int firstDelimIdx = output.indexOf(Path.PATH_DELIM); int secondDelim = output.indexOf(Path.PATH_DELIM, firstDelimIdx + 1); if (secondDelim < 0) { jgen.writeString(output); } else { jgen.writeString(output.substring(secondDelim)); } } }
public ElementTypes<?, ?, ?> byPath(Path path) { return bySegment(path.getSegment().getElementType()); }
TraversalContext<BE, E> proceedTo(Path path) { if (!AbstractElement.segmentTypeFromType(entityClass).equals(path.getSegment().getElementType())) { throw new IllegalArgumentException("Path doesn't point to the type of element currently being accessed."); } return replacePath(Util.extendTo(this, path)); }
if (!SegmentType.r.equals(p.getSegment().getElementType())) { throw new IllegalArgumentException("Descend can only traverse child resources.");
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()); }); }