private List<Filter.NestedPathSegment> deepNestedProperty(PropertyPath path) { List<Filter.NestedPathSegment> segments = new ArrayList<>(); if (path.hasNext()) { PropertyPath next = path.next(); if (next != null && !next.equals(next.getLeafProperty())) { segments.add(new Filter.NestedPathSegment(next.getSegment(), next.getType())); segments.addAll(deepNestedProperty(next)); } } return segments; }
Filter.NestedPathSegment lastNestedPathSegment = nestedPath.get(nestedPath.size() - 1); startNestedEntityTypeLabel = firstNestedPathSegment.getNestedEntityTypeLabel(); endNestedEntityTypeLabel = lastNestedPathSegment.getNestedEntityTypeLabel(); relationshipDirection = firstNestedPathSegment.getRelationshipDirection();
for (Filter.NestedPathSegment nestedPathSegment : filter.getNestedPath()) { resolveRelationshipType(parentOwnerType, nestedPathSegment); ClassInfo nestedClassInfo = session.metaData().classInfo(nestedPathSegment.getPropertyType().getName()); nestedPathSegment.setNestedEntityTypeLabel(session.metaData().entityType(nestedClassInfo.name())); if (session.metaData().isRelationshipEntity(nestedClassInfo.name())) { nestedPathSegment.setNestedRelationshipEntity(true); parentOwnerType = nestedPathSegment.getPropertyType();
@Override public MatchClause append(Filter filter) { boolean wasPreviousSegmentRelationshipEntity = false; for (Filter.NestedPathSegment segment : filter.getNestedPath()) { boolean nestedRelationshipEntity = segment.isNestedRelationshipEntity(); if (!wasPreviousSegmentRelationshipEntity) { if (segment.getRelationshipDirection().equals(Relationship.INCOMING)) { clause.append("<"); } clause.append( String.format("-[%s:`%s`]-", nestedRelationshipEntity && isLastSegment(filter, segment) ? "r" + index : "", segment.getRelationshipType())); if (segment.getRelationshipDirection().equals(Relationship.OUTGOING)) { clause.append(">"); } } if (!nestedRelationshipEntity && !isLastSegment(filter, segment)) { clause.append(String.format("(:`%s`)", segment.getNestedEntityTypeLabel())); } wasPreviousSegmentRelationshipEntity = nestedRelationshipEntity; } clause.append(String.format("(%s) ", "m" + index)); return this; }
@Test public void loadFilmByUserAndRatingUserPlays() { Filter userFilter = new Filter("name", ComparisonOperator.EQUALS, "Michal"); userFilter.setNestedPath( new Filter.NestedPathSegment("ratings", Rating.class), new Filter.NestedPathSegment("user", User.class) ); Filter playsFilter = new Filter("level", ComparisonOperator.EQUALS, "ok"); playsFilter.setOwnerEntityType(Movie.class); playsFilter.setNestedPath( new Filter.NestedPathSegment("ratings", Rating.class), new Filter.NestedPathSegment("user", User.class), new Filter.NestedPathSegment("plays", Plays.class) ); Filters filters = userFilter.and(playsFilter); Collection<Movie> films = session.loadAll(Movie.class, filters); assertThat(films).hasSize(2); }
@Test public void loadFilmByRatingUsersPet() { // Pulp Fiction and Top Gear rated by Michal who owns Catty Filter filter = new Filter("name", ComparisonOperator.EQUALS, "Catty"); filter.setOwnerEntityType(Movie.class); filter.setNestedPath( new Filter.NestedPathSegment("ratings", Rating.class), new Filter.NestedPathSegment("user", User.class), new Filter.NestedPathSegment("pets", Pet.class) ); Collection<Movie> films = session.loadAll(Movie.class, filter); assertThat(films).hasSize(2); }
Filter createDstSwitchFilter(SwitchId switchId) { Filter dstSwitchFilter = new Filter(SWITCH_NAME_PROPERTY_NAME, ComparisonOperator.EQUALS, switchId.toString()); dstSwitchFilter.setNestedPath(new Filter.NestedPathSegment(DEST_SWITCH_FIELD, Switch.class)); return dstSwitchFilter; } }
Filter createSrcSwitchFilter(SwitchId switchId) { Filter srcSwitchFilter = new Filter(SWITCH_NAME_PROPERTY_NAME, ComparisonOperator.EQUALS, switchId.toString()); srcSwitchFilter.setNestedPath(new Filter.NestedPathSegment(SRC_SWITCH_FIELD, Switch.class)); return srcSwitchFilter; }
@Test public void loadRatingByUserNameAndStars() { Filter userNameFilter = new Filter("name", ComparisonOperator.EQUALS, "Michal"); userNameFilter.setNestedPath( new Filter.NestedPathSegment("user", User.class) ); Collection<Rating> ratings = session.loadAll(Rating.class, userNameFilter); assertThat(ratings).hasSize(2); }
private List<Filter.NestedPathSegment> deepNestedProperty(PropertyPath path) { List<Filter.NestedPathSegment> segments = new ArrayList<>(); if (path.hasNext()) { PropertyPath next = path.next(); if (!next.equals(next.getLeafProperty())) { segments.add(new Filter.NestedPathSegment(next.getSegment(), next.getType())); segments.addAll(deepNestedProperty(next)); } } return segments; }
@Test public void loadRatingByUserName() { Filter userNameFilter = new Filter("name", ComparisonOperator.EQUALS, "Michal"); Filter ratingFilter = new Filter("stars", ComparisonOperator.EQUALS, 5); userNameFilter.setNestedPath( new Filter.NestedPathSegment("user", User.class) ); Collection<Rating> ratings = session.loadAll(Rating.class, userNameFilter.and(ratingFilter)); assertThat(ratings).hasSize(1); } @Test
protected void setNestedAttributes(Part part, Filter filter) { List<Filter.NestedPathSegment> segments = new ArrayList<>(); PropertyPath property = part.getProperty(); if (property.hasNext()) { filter.setOwnerEntityType(property.getOwningType().getType()); segments.add(new Filter.NestedPathSegment(property.getSegment(), property.getType())); segments.addAll(deepNestedProperty(property)); filter.setPropertyName(property.getLeafProperty().getSegment()); filter.setNestedPath(segments.toArray(new Filter.NestedPathSegment[0])); } }
@Test public void loadFilmByRatingUserPlays() { Filter filter = new Filter("level", ComparisonOperator.EQUALS, "ok"); filter.setOwnerEntityType(Movie.class); filter.setNestedPath( new Filter.NestedPathSegment("ratings", Rating.class), new Filter.NestedPathSegment("user", User.class), new Filter.NestedPathSegment("plays", Plays.class) ); Collection<Movie> films = session.loadAll(Movie.class, filter); assertThat(films).hasSize(2); }
@Test public void loadFilmByRating() { Filter filter = new Filter("stars", ComparisonOperator.EQUALS, 5); filter.setOwnerEntityType(Movie.class); filter.setNestedPath( new Filter.NestedPathSegment("ratings", Rating.class) ); Collection<Movie> films = session.loadAll(Movie.class, filter); assertThat(films).hasSize(1); }
private void resolveRelationshipType(Class parentOwnerType, Filter.NestedPathSegment segment) { ClassInfo classInfo = session.metaData().classInfo(parentOwnerType.getName()); FieldInfo fieldInfo = classInfo.relationshipFieldByName(segment.getPropertyName()); String defaultRelationshipType = RelationshipUtils.inferRelationshipType(segment.getPropertyName()); updateRelationship(segment, fieldInfo, defaultRelationshipType); }
private void appendDeepNestedFilter(Filter filter) { if (filter.getBooleanOperator().equals(BooleanOperator.OR)) { throw new UnsupportedOperationException("OR is not supported for nested properties on an entity"); } Filter.NestedPathSegment lastPathSegment = filter.getNestedPath().get(filter.getNestedPath().size() - 1); MatchClause clause = new NestedPropertyPathMatchClause(matchClauseId, lastPathSegment.getNestedEntityTypeLabel(), lastPathSegment.isNestedRelationshipEntity()); pathClauses.add(new NestedPathMatchClause(matchClauseId).append(filter)); nestedClauses.add(clause); clause.append(filter); matchClauseId++; }
protected void setNestedAttributes(Part part, Filter filter) { List<Filter.NestedPathSegment> segments = new ArrayList<>(); PropertyPath property = part.getProperty(); if (property.hasNext()) { filter.setOwnerEntityType(property.getOwningType().getType()); segments.add(new Filter.NestedPathSegment(property.getSegment(), property.getType())); segments.addAll(deepNestedProperty(property)); filter.setPropertyName(property.getLeafProperty().getSegment()); filter.setNestedPath(segments.toArray(new Filter.NestedPathSegment[0])); } }