@SuppressWarnings("unchecked") public static Filter[] pathTo(CanonicalPath path) { if (!path.isDefined()) { return new Filter[0]; } List<Filter> fs = new ArrayList<>(); for (Path.Segment s : path.getPath()) { fs.add(Related.by(Relationships.WellKnown.contains)); fs.add(With.type(Entity.entityTypeFromSegmentType(s.getElementType()))); fs.add(With.id(s.getElementId())); } if (fs.size() < 2) { return new Filter[0]; } else { //remove the first 'contains' defined in the loop above List<Filter> ret = fs.subList(1, fs.size()); return ret.toArray(new Filter[ret.size()]); } }
/** * Returns a child builder of a pre-existing child. * @param childPath the path to the child * @return the child builder or null */ public ChildBuilder<This> getChild(Path.Segment childPath) { Map<EntityType, Set<FullNode>> myChildren = children.get(myPath); if (myChildren == null) { return null; } EntityType childType = EntityType.of(childPath.getElementType()); Set<FullNode> childrenOfType = myChildren.get(childType); return childrenOfType.stream().filter(child -> child.getEntity().getId().equals(childPath.getElementId())) .findAny().map(child -> { RelativePath rp = myPath.modified().extend(childPath).get(); return new ChildBuilder<>(castThis(), rp, blueprints, children); }).orElse(null); }
/** * Returns a child builder of a pre-existing child. * @param childPath the path to the child * @return the child builder or null */ public ChildBuilder<This> getChild(Path.Segment childPath) { Map<EntityType, Set<FullNode>> myChildren = children.get(myPath); if (myChildren == null) { return null; } EntityType childType = EntityType.of(childPath.getElementType()); Set<FullNode> childrenOfType = myChildren.get(childType); return childrenOfType.stream().filter(child -> child.getEntity().getId().equals(childPath.getElementId())) .findAny().map(child -> { RelativePath rp = myPath.modified().extend(childPath).get(); return new ChildBuilder<>(castThis(), rp, blueprints, children); }).orElse(null); }
@SuppressWarnings("unchecked") public void visit(GraphTraversal<?, ?> query, With.DataAt dataPos, QueryTranslationState state) { goBackFromEdges(query, state); query.out(hasData.name()); for (Path.Segment seg : dataPos.getDataPath().getPath()) { if (SegmentType.up.equals(seg.getElementType())) { query.in(contains.name()); } else { query.out(contains.name()); } query.has(__type.name(), Constants.Type.structuredData.name()); // map members have both index and key (so that the order of the elements is preserved) // list members have only the index Integer index = toInteger(seg.getElementId()); if (index == null) { query.has(Constants.Property.__structuredDataKey.name(), seg.getElementId()); } else { //well, the map could have a numeric key, so we cannot say it has to be a list index here. GraphTraversal<?, ?>[] indexOrKey = new GraphTraversal<?, ?>[2]; indexOrKey[0] = __.has(Constants.Property.__structuredDataIndex.name(), index) .hasNot(Constants.Property.__structuredDataKey.name()); indexOrKey[1] = __.has(Constants.Property.__structuredDataKey.name(), seg.getElementId()); query.or((Traversal[]) indexOrKey); } } }
@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."); } }
@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."); } }
@Override public Element find(CanonicalPath path) throws ElementNotFoundException { Iterator<? extends Element> it; if (SegmentType.rl.equals(path.getSegment().getElementType())) { //__eid is globally unique for relationships it = context.getGraph().traversal().E().has(__eid.name(), path.getSegment().getElementId()); } else { it = context.getGraph().traversal().V() .hasLabel(Constants.Type.of(path.getSegment().getElementType()).name()) .has(__cp.name(), path.toString()); } if (!it.hasNext()) { throw new ElementNotFoundException(); } return it.next(); }
private Vertex common(org.hawkular.inventory.paths.CanonicalPath path, String name, Map<String, Object> properties, Class<? extends Entity<?, ?>> cls) { try { checkProperties(properties, Constants.Type.of(cls).getMappedProperties()); Map<String, Object> props = properties == null ? Collections.emptyMap() : properties; Object[] keyVals = asKeyValues(props, T.label, Constants.Type.of(cls).name(), __type.name(), Constants.Type.of(cls).name(), __eid.name(), path.getSegment().getElementId(), __cp.name(), path.toString(), Constants.Property.name.name(), name); return context.getGraph().addVertex(keyVals); } catch (RuntimeException e) { throw context.translateException(e, path); } } }, null);
@Override public void preDelete(BE dataEntity, Transaction<BE> tx) { CanonicalPath dataPath = tx.extractCanonicalPath(dataEntity); BE ot = null; try { ot = tx.find(dataPath.up()); } catch (ElementNotFoundException e) { Fetcher.throwNotFoundException(context); } if (ReadWrite.isResourceTypeInMetadataPack(ot, tx)) { throw new IllegalArgumentException( "Data '" + dataPath.getSegment().getElementId() + "' cannot be deleted" + " 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."); } }
@Override public void preDelete(BE dataEntity, Transaction<BE> tx) { CanonicalPath dataPath = tx.extractCanonicalPath(dataEntity); BE rt = null; try { rt = tx.find(dataPath.up()); } catch (ElementNotFoundException e) { Fetcher.throwNotFoundException(context); } if (ReadWrite.isResourceTypeInMetadataPack(rt, tx)) { throw new IllegalArgumentException( "Data '" + dataPath.getSegment().getElementId() + "' cannot be deleted" + " under resource type " + dataPath.up() + ", because it is part of a meta data pack." + " Doing this would invalidate meta data pack's identity."); } }
public DataEntity(CanonicalPath path, StructuredData value, Map<String, Object> properties) { this(path.up(), DataRole.valueOf(path.getSegment().getElementId()), value, properties); }
public EntityAlreadyExistsException(Throwable cause, CanonicalPath entityPath) { this(cause, entityPath.getSegment().getElementId(), asFilters(entityPath)); }
default Single descendContained(RelativePath resourcePath) { ReadContained parent = this; Single access = null; for(Path.Segment s : resourcePath.getPath()) { access = parent.get(s.getElementId()); parent = access.resources(); } if (access == null) { throw new IllegalArgumentException("Empty resource path"); } return access; } }
/** * @return the id of the element. */ public String getId() { return path.getSegment().getElementId(); }
public DataEntity(CanonicalPath path, StructuredData value, String identityHash, String contentHash, String syncHash, Map<String, Object> properties) { this(path.up(), DataRole.valueOf(path.getSegment().getElementId()), value, identityHash, contentHash, syncHash, properties); }
private void apply(Path.Segment segment, GraphTraversal<?, ?> pipeline) { pipeline.has(__type.name(), Constants.Type.of(Entity.typeFromSegmentType(segment.getElementType())).name()); pipeline.has(__eid.name(), segment.getElementId()); }
/** * @return the id of the element. */ public String getId() { return path.getSegment().getElementId(); }
@Override public Single visitOperationType(Void parameter) { ResourceTypes.Single rt = inspect(path.up(), ResourceTypes.Single.class); return accessInterface.cast(rt.operationTypes().get(path.getSegment().getElementId())); }
private static String segmentToString(Path.Segment segment) { return (segment.getElementType() == null ? "" : (segment.getElementType().getSerialized() + ";")) + segment.getElementId(); } }
@Override public Single visitMetadataPack(Void parameter) { return accessInterface.cast(tenants().get(path.up().getSegment().getElementId()) .metadataPacks().get(path.getSegment().getElementId())); }