Skip to content

Commit

Permalink
Simplify property selection
Browse files Browse the repository at this point in the history
  • Loading branch information
joostfarla committed Oct 4, 2023
1 parent 85e7823 commit e5df7b4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,17 @@ public B selectedProperties(Set<SelectedProperty> selectedProperties) {
}

public B selectProperty(String name) {
selectedProperties.add(SelectedProperty.builder()
.property(objectType.getProperty(name))
.build());
selectedProperties.add(SelectedProperty.forProperty(objectType.getProperty(name)));
return self();
}

public B selectObjectProperty(String name, Function<ObjectRequest.Builder, DataRequest> selectionFn) {
var property = objectType.getProperty(name);

if (property instanceof Relation relation) {
selectedProperties.add(SelectedProperty.builder()
.property(property)
.nestedRequest(selectionFn.apply(ObjectRequest.builder(model)
.objectType(relation.getTarget())))
.build());
var nestedRequest = selectionFn.apply(ObjectRequest.builder(model)
.objectType(relation.getTarget()));
selectedProperties.add(SelectedProperty.forProperty(property, nestedRequest));
return self();
}

Expand All @@ -80,10 +76,9 @@ public B selectCollectionProperty(String name, Function<CollectionRequest.Builde
var property = objectType.getProperty(name);

if (property instanceof Relation relation) {
selectedProperties.add(SelectedProperty.builder()
.property(property)
.nestedRequest(selectionFn.apply(CollectionRequest.builder(model).objectType(relation.getTarget())))
.build());
var nestedRequest = selectionFn.apply(CollectionRequest.builder(model)
.objectType(relation.getTarget()));
selectedProperties.add(SelectedProperty.forProperty(property, nestedRequest));
return self();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package nl.geostandaarden.imx.orchestrate.engine.exchange;

import lombok.Builder;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import nl.geostandaarden.imx.orchestrate.engine.OrchestrateException;
import nl.geostandaarden.imx.orchestrate.model.Attribute;
import nl.geostandaarden.imx.orchestrate.model.Property;
import nl.geostandaarden.imx.orchestrate.model.Relation;

@Getter
@EqualsAndHashCode
@Builder(toBuilder = true)
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public final class SelectedProperty {

private final Property property;
Expand All @@ -26,12 +28,18 @@ public String toString() {
}

public static SelectedProperty forProperty(Property property) {
if (property instanceof Relation) {
throw new OrchestrateException("Using static constructor for Relation properties is not supported (yet).");
return forProperty(property, null);
}

public static SelectedProperty forProperty(Property property, DataRequest nestedRequest) {
if (property instanceof Attribute && nestedRequest != null) {
throw new OrchestrateException("Attribute properties can not have a nested request.");
}

if (property instanceof Relation && nestedRequest == null) {
throw new OrchestrateException("Relation properties require a nested request.");
}

return SelectedProperty.builder()
.property(property)
.build();
return new SelectedProperty(property, nestedRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,23 +203,17 @@ private FetchOperation fetchSourceObject(ObjectTypeRef sourceTypeRef, Set<Path>

// TODO: Refactor
if (property.getCardinality().isSingular()) {
selectedProperties.add(SelectedProperty.builder()
.property(property)
.nestedRequest(ObjectRequest.builder(targetModel)
.objectType(targetTypeRef)
.objectKey(Map.of())
.selectedProperties(selectIdentity(targetTypeRef))
.build())
.build());
selectedProperties.add(SelectedProperty.forProperty(property, ObjectRequest.builder(targetModel)
.objectType(targetTypeRef)
.objectKey(Map.of())
.selectedProperties(selectIdentity(targetTypeRef))
.build()));
} else {
// TODO: Filter
selectedProperties.add(SelectedProperty.builder()
.property(property)
.nestedRequest(CollectionRequest.builder(targetModel)
.objectType(targetTypeRef)
.selectedProperties(selectIdentity(targetTypeRef))
.build())
.build());
selectedProperties.add(SelectedProperty.forProperty(property, CollectionRequest.builder(targetModel)
.objectType(targetTypeRef)
.selectedProperties(selectIdentity(targetTypeRef))
.build()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ void fetch() {
var resultMono = engine.fetch(request);

StepVerifier.create(resultMono)
.assertNext(result -> {
System.out.println(result.getProperties());
})
.expectNextCount(1)
.expectComplete()
.verify();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package nl.geostandaarden.imx.orchestrate.source.file;

import static nl.geostandaarden.imx.orchestrate.source.file.FileUtils.*;
import static nl.geostandaarden.imx.orchestrate.source.file.FileUtils.getBaseName;
import static nl.geostandaarden.imx.orchestrate.source.file.FileUtils.getObjectKey;
import static nl.geostandaarden.imx.orchestrate.source.file.FileUtils.getObjectProperties;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

Expand Down Expand Up @@ -48,9 +50,10 @@ void getObjectKey_ReturnsKeyMap_ForObjectType() {
void getObjectProperties_ReturnsProperties_ForSelectedProperties() {
var objectNode = createObjectNode();
var objectType = createObjectType();

var selectedProperties = Set.of(
SelectedProperty.builder().property(objectType.getProperty("id")).build(),
SelectedProperty.builder().property(objectType.getProperty("name")).build());
SelectedProperty.forProperty(objectType.getProperty("id")),
SelectedProperty.forProperty(objectType.getProperty("name")));

var objectProperties = getObjectProperties(objectNode, selectedProperties);

Expand Down

0 comments on commit e5df7b4

Please sign in to comment.