Skip to content

Commit

Permalink
fix mutation requests (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
cportele authored and azahnen committed Jan 10, 2024
1 parent 0fab7b4 commit 126379a
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import de.ii.xtraplatform.features.domain.FeatureTokenDecoder;
import de.ii.xtraplatform.features.domain.SchemaBase.Type;
import de.ii.xtraplatform.features.domain.SchemaMapping;
import de.ii.xtraplatform.features.domain.SchemaMappingBase;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand All @@ -37,6 +38,7 @@ public class FeatureTokenDecoderGeoJson
private final JsonParser parser;
private final ByteArrayFeeder feeder;
private final Optional<String> nullValue;
private String type;

private boolean started;
private int depth = -1;
Expand All @@ -53,24 +55,35 @@ public class FeatureTokenDecoderGeoJson
FeatureSchema, SchemaMapping, ModifiableContext<FeatureSchema, SchemaMapping>>
downstream;

public FeatureTokenDecoderGeoJson() {
this(Optional.empty());
// for unit tests
FeatureTokenDecoderGeoJson(String type) {
this(Optional.empty(), type);
}

public FeatureTokenDecoderGeoJson(Optional<String> nullValue) {
this(nullValue, null);
}

private FeatureTokenDecoderGeoJson(Optional<String> nullValue, String type) {
try {
this.parser = JSON_FACTORY.createNonBlockingByteArrayParser();
} catch (IOException e) {
throw new IllegalStateException(e);
}
this.feeder = (ByteArrayFeeder) parser.getNonBlockingInputFeeder();
this.nullValue = nullValue;
this.type = type;
}

@Override
protected void init() {
this.context = createContext();
this.downstream = new FeatureTokenBuffer<>(getDownstream(), context);

if (Objects.isNull(type) && Objects.nonNull(context.mapping())) {
SchemaMappingBase<?> mapping = context.mapping();
type = mapping.getTargetSchema().getSourcePath().orElse(null);
}
}

@Override
Expand Down Expand Up @@ -137,12 +150,12 @@ public boolean advanceParser() {
switch (currentName) {
case "properties":
inProperties = true;
featureDepth = depth;
context.pathTracker().track(0);
// featureDepth = depth;
context.pathTracker().track(type, 0);
break;
case "geometry":
context.setInGeometry(true);
context.pathTracker().track(currentName, 0);
context.pathTracker().track(currentName, 1);
geoPath = context.path();
break;
default:
Expand All @@ -152,7 +165,7 @@ public boolean advanceParser() {
break;
}
// nested array_object start
} else if (!context.pathTracker().asList().isEmpty() && started) {
} else if (context.pathTracker().asList().size() > 1 && started) {
downstream.onObjectStart(context);
// feature in collection start
} else if (depth == featureDepth - 1 && inFeature) {
Expand Down Expand Up @@ -226,19 +239,19 @@ public boolean advanceParser() {

if (context.inGeometry()) {
checkBufferForDimension();
context.pathTracker().track(depth - featureDepth - 1);
context.pathTracker().track(depth - featureDepth);
}
depth -= 1;
if (inProperties) {
context.pathTracker().track(depth - featureDepth - 1);
context.pathTracker().track(depth - featureDepth);
}
lastNameIsArrayDepth -= 1;
// end nested geo array
} else if (context.inGeometry()) {
checkBufferForDimension();
endArray++;
depth -= 1;
context.pathTracker().track(depth - featureDepth);
context.pathTracker().track(depth - featureDepth + 1);
}
break;

Expand All @@ -257,7 +270,7 @@ public boolean advanceParser() {

// end geo
if (context.inGeometry()) {
context.pathTracker().track(depth - featureDepth - 1);
context.pathTracker().track(depth - featureDepth);
}

depth -= 1;
Expand Down Expand Up @@ -288,7 +301,7 @@ public boolean advanceParser() {
downstream.bufferStop(false);
}
if (inProperties) {
context.pathTracker().track(depth - featureDepth - 1);
context.pathTracker().track(depth - featureDepth);
}
break;

Expand Down Expand Up @@ -323,9 +336,9 @@ public boolean advanceParser() {
&& (Objects.equals(currentName, "geometry")
|| Objects.equals(currentName, "properties"))) {
if (Objects.equals(currentName, "properties")) {
context.pathTracker().track(0);
context.pathTracker().track(type, 0);
} else {
context.pathTracker().track(currentName, 0);
context.pathTracker().track(currentName, 1);
}
context.setValue(nullValue.get());
downstream.onValue(context);
Expand All @@ -350,12 +363,12 @@ public boolean advanceParser() {
break;
}

context.pathTracker().track(currentName, 0);
context.pathTracker().track(currentName, 1);
context.setValue(parser.getValueAsString());

downstream.onValue(context);

context.pathTracker().track(0);
context.pathTracker().track(1);
break;
}
// feature id or props or geo value
Expand Down Expand Up @@ -397,13 +410,13 @@ public boolean advanceParser() {

// feature id
if (Objects.equals(currentName, "id")) {
context.pathTracker().track(0);
context.pathTracker().track(1);
}
// why reset depth?
else if (!context.inGeometry()) {
context.pathTracker().track(depth - featureDepth - 1);
} else if (context.inGeometry()) {
context.pathTracker().track(depth - featureDepth);
} else if (context.inGeometry()) {
context.pathTracker().track(depth - featureDepth + 1);
}
}
break;
Expand Down Expand Up @@ -432,6 +445,8 @@ private void startIfNecessary(boolean isCollection) {
if (!started) {
started = true;
inFeature = true;
context.pathTracker().track(type, 0);

if (isCollection) {
featureDepth = 1;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class FeatureTokenDecoderGeoJsonSpec2 extends Specification {
}

def setup() {
decoder = new FeatureTokenDecoderGeoJson()
decoder = new FeatureTokenDecoderGeoJson("biotop")
}

public <T> T runStream(Reactive.Stream<T> stream) {
Expand Down Expand Up @@ -78,7 +78,7 @@ class FeatureTokenDecoderGeoJsonSpec2 extends Specification {
List<Object> tokens = runStream(stream)

then:
tokens == FeatureTokenFixtures.SINGLE_FEATURE
tokens == FeatureTokenFixtures.SINGLE_FEATURE_SOURCE

}

Expand All @@ -95,7 +95,7 @@ class FeatureTokenDecoderGeoJsonSpec2 extends Specification {
List<Object> tokens = runStream(stream)

then:
tokens == FeatureTokenFixtures.SINGLE_FEATURE_POINT
tokens == FeatureTokenFixtures.SINGLE_FEATURE_POINT_SOURCE

}

Expand All @@ -112,7 +112,7 @@ class FeatureTokenDecoderGeoJsonSpec2 extends Specification {
List<Object> tokens = runStream(stream)

then:
tokens == FeatureTokenFixtures.SINGLE_FEATURE_MULTI_POINT
tokens == FeatureTokenFixtures.SINGLE_FEATURE_MULTI_POINT_SOURCE

}

Expand All @@ -129,7 +129,7 @@ class FeatureTokenDecoderGeoJsonSpec2 extends Specification {
List<Object> tokens = runStream(stream)

then:
tokens == FeatureTokenFixtures.SINGLE_FEATURE_MULTI_POLYGON
tokens == FeatureTokenFixtures.SINGLE_FEATURE_MULTI_POLYGON_SOURCE

}

Expand All @@ -146,7 +146,7 @@ class FeatureTokenDecoderGeoJsonSpec2 extends Specification {
List<Object> tokens = runStream(stream)

then:
tokens == FeatureTokenFixtures.SINGLE_FEATURE_NESTED_OBJECT
tokens == FeatureTokenFixtures.SINGLE_FEATURE_NESTED_OBJECT_SOURCE

}

Expand All @@ -163,7 +163,7 @@ class FeatureTokenDecoderGeoJsonSpec2 extends Specification {
List<Object> tokens = runStream(stream)

then:
tokens == FeatureTokenFixtures.SINGLE_FEATURE_VALUE_ARRAY
tokens == FeatureTokenFixtures.SINGLE_FEATURE_VALUE_ARRAY_SOURCE

}

Expand All @@ -180,7 +180,7 @@ class FeatureTokenDecoderGeoJsonSpec2 extends Specification {
List<Object> tokens = runStream(stream)

then:
tokens == FeatureTokenFixtures.SINGLE_FEATURE_NESTED_OBJECT_ARRAYS
tokens == FeatureTokenFixtures.SINGLE_FEATURE_NESTED_OBJECT_ARRAYS_SOURCE

}

Expand All @@ -197,7 +197,7 @@ class FeatureTokenDecoderGeoJsonSpec2 extends Specification {
List<Object> tokens = runStream(stream)

then:
tokens == FeatureTokenFixtures.COLLECTION
tokens == FeatureTokenFixtures.COLLECTION_SOURCE

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,40 @@
*/
package de.ii.xtraplatform.features.sql.domain;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import de.ii.xtraplatform.features.domain.SchemaMappingBase;
import de.ii.xtraplatform.features.domain.SchemaToMappingVisitor;
import de.ii.xtraplatform.geometries.domain.SimpleFeatureGeometry;
import java.util.AbstractMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import org.immutables.value.Value;

@Value.Immutable
@Value.Style(deepImmutablesDetection = true, builder = "new", attributeBuilderDetection = true)
public interface SchemaMappingSql extends SchemaMappingBase<SchemaSql> {

BiFunction<String, Boolean, String> getSourcePathTransformer();
@Override
default SchemaSql schemaWithGeometryType(SchemaSql schema, SimpleFeatureGeometry geometryType) {
return new ImmutableSchemaSql.Builder().from(schema).geometryType(geometryType).build();
}

@Value.Derived
@Value.Auxiliary
default Map<List<String>, List<SchemaSql>> getSchemasByTargetPath() {
return getTargetSchema()
.accept(new SchemaToMappingVisitor<>(getSourcePathTransformer()))
.asMap()
.entrySet()
.stream()
// TODO: removal of first path element only makes sense for geojson, so change in parser
.map(
entry ->
new AbstractMap.SimpleImmutableEntry<>(
entry.getKey().subList(1, entry.getKey().size()),
Lists.newArrayList(entry.getValue())))
.collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));
@Override
default List<String> cleanPath(List<String> path) {
if (path.stream().anyMatch(elem -> elem.contains("{"))) {
return path.stream().map(this::cleanPath).collect(Collectors.toList());
}

return path;
}

// TODO: static cleanup method in PathParser
@Override
default SchemaSql schemaWithGeometryType(SchemaSql schema, SimpleFeatureGeometry geometryType) {
return new ImmutableSchemaSql.Builder().from(schema).geometryType(geometryType).build();
default String cleanPath(String path) {
if (path.contains("{")) {
int i = path.indexOf("{");
if (path.startsWith("filter", i + 1)) {
return path.substring(0, i + 2) + cleanPath(path.substring(i + 2));
}
return path.substring(0, path.indexOf("{"));
}
return path;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MutationSchemaDeriverSpec extends Specification {
def cql = new CqlImpl()

pathParser = new PathParserSql(syntax, cql)
pathParser2 = new SqlPathParser(defaults, cql, filterEncoder)
pathParser2 = new SqlPathParser(defaults, cql, Set.of())
schemaBuilderSql = new MutationSchemaDeriver(pathParser, pathParser2)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MutationSchemaFixtures {
.name("externalprovider_externalprovidername")
.type(Type.OBJECT_ARRAY)
.parentPath(["externalprovider"])
.addRelation(ImmutableSqlRelation.builder()
.addRelation(new ImmutableSqlRelation.Builder()
.cardinality(SqlRelation.CARDINALITY.ONE_2_N)
.sourceContainer("externalprovider")
.sourceField("id")
Expand All @@ -52,7 +52,7 @@ class MutationSchemaFixtures {
.name("explorationsite_task")
.type(Type.OBJECT_ARRAY)
.parentPath(["explorationsite"])
.addRelation(ImmutableSqlRelation.builder()
.addRelation(new ImmutableSqlRelation.Builder()
.cardinality(SqlRelation.CARDINALITY.ONE_2_N)
.sourceContainer("explorationsite")
.sourceField("id")
Expand All @@ -64,7 +64,7 @@ class MutationSchemaFixtures {
.sourcePath("task")
.type(Type.OBJECT)
.parentPath(["explorationsite"])
.addRelation(ImmutableSqlRelation.builder()
.addRelation(new ImmutableSqlRelation.Builder()
.cardinality(SqlRelation.CARDINALITY.ONE_2_ONE)
.sourceContainer("explorationsite_task")
.sourceField("task_fk")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public final W createContext() {
return (W)
ModifiableGenericContext.create()
.setMappings(ImmutableMap.of())
.setQuery(ImmutableFeatureQuery.builder().type("default").build());
.setQuery(ImmutableFeatureQuery.builder().type("default").build())
.setType("default");
}

protected final FeatureEventHandler<U, V, W> getDownstream() {
Expand Down
Loading

0 comments on commit 126379a

Please sign in to comment.