Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,9 @@ public Class<? extends ModifiableContext<SqlQuerySchema, SqlQueryMapping>> getCo

@Override
public ModifiableContext<SqlQuerySchema, SqlQueryMapping> createContext() {
return ModifiableFeatureEncoderSqlContext.create();
return ModifiableFeatureEncoderSqlContext.create()
.setType(mapping.getMainSchema().getName())
.setMappings(Map.of(mapping.getMainSchema().getName(), mapping));
}

private boolean checkJson(Tuple<SqlQuerySchema, SqlQueryColumn> column) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1299,10 +1299,11 @@ private MutationResult writeFeatures(

ImmutableMutationResult.Builder builder =
ImmutableMutationResult.builder().type(type).hasFeatures(false);
FeatureTokenStatsCollector statsCollector = new FeatureTokenStatsCollector(builder, crs);

Source<FeatureDataSql> featureSqlSource =
featureTokenSource
// TODO: Simple .via(statsCollector)
.via(statsCollector)
.via(
new FeatureEncoderSql(
queryMapping.get().get(0),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* Copyright 2022 interactive instruments GmbH
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package de.ii.xtraplatform.features.sql.domain;

import de.ii.xtraplatform.crs.domain.BoundingBox;
import de.ii.xtraplatform.crs.domain.EpsgCrs;
import de.ii.xtraplatform.features.domain.ImmutableMutationResult.Builder;
import de.ii.xtraplatform.features.domain.SchemaBase.Role;
import de.ii.xtraplatform.features.domain.Tuple;
import de.ii.xtraplatform.geometries.domain.Geometry;
import de.ii.xtraplatform.geometries.domain.transform.MinMaxDeriver;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FeatureTokenStatsCollector extends FeatureTokenTransformerSql {

private static final Logger LOGGER = LoggerFactory.getLogger(FeatureTokenStatsCollector.class);

private final Builder builder;
private final EpsgCrs crs;
private int axis = 0;
private int dim = 2;
private Double xmin = null;
private Double ymin = null;
private Double xmax = null;
private Double ymax = null;
private String start = "";
private String end = "";

public FeatureTokenStatsCollector(Builder builder, EpsgCrs crs) {
this.builder = builder;
this.crs = crs;
}

@Override
public void onStart(ModifiableContext<SqlQuerySchema, SqlQueryMapping> context) {
// TODO: get crs
this.dim = context.geometryDimension().orElse(2);

super.onStart(context);
}

@Override
public void onEnd(ModifiableContext<SqlQuerySchema, SqlQueryMapping> context) {
if (xmin != null && ymin != null && xmax != null && ymax != null) {
builder.spatialExtent(BoundingBox.of(xmin, ymin, xmax, ymax, crs));
}

if (!start.isEmpty() || !end.isEmpty()) {
builder.temporalExtent(Tuple.of(parseTemporal(start), parseTemporal(end)));
}

super.onEnd(context);
}

private Long parseTemporal(String temporal) {
if (temporal.isEmpty()) {
return null;
}
try {
if (temporal.length() > 10) {
return ZonedDateTime.parse(temporal).toInstant().toEpochMilli();
}
return LocalDate.parse(temporal).atStartOfDay().toInstant(ZoneOffset.UTC).toEpochMilli();
} catch (Throwable e) {
return null;
}
}

@Override
public void onValue(ModifiableContext<SqlQuerySchema, SqlQueryMapping> context) {
if (Objects.nonNull(context.value())) {
String value = context.value();

if (hasRole(context, Role.PRIMARY_INSTANT)) {
if (start.isEmpty() || value.compareTo(start) < 0) {
this.start = value;
}
if (end.isEmpty() || value.compareTo(end) > 0) {
this.end = value;
}
} else if (hasRole(context, Role.PRIMARY_INTERVAL_START)) {
if (start.isEmpty() || value.compareTo(start) < 0) {
this.start = value;
}
} else if (hasRole(context, Role.PRIMARY_INTERVAL_END)) {
if (end.isEmpty() || value.compareTo(end) > 0) {
this.end = value;
}
}
}

super.onValue(context);
}

@Override
public void onGeometry(ModifiableContext<SqlQuerySchema, SqlQueryMapping> context) {
if (Objects.nonNull(context.geometry())) {
Geometry<?> value = context.geometry();

double[][] minMax = value.accept(new MinMaxDeriver());
if (minMax.length > 1 && minMax[0].length >= dim && minMax[1].length >= dim) {
this.xmin = minMax[0][0];
this.xmax = minMax[1][0];
this.ymin = minMax[0][1];
this.ymax = minMax[1][1];
}
}
}

private boolean hasRole(ModifiableContext<SqlQuerySchema, SqlQueryMapping> context, Role role) {
return context
.mapping()
.getSchemaForRole(role)
.filter(schema -> Objects.equals(schema.getFullPathAsString(), context.pathAsString()))
.isPresent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,36 @@
*/
package de.ii.xtraplatform.features.sql.domain;

import de.ii.xtraplatform.features.domain.FeatureEventHandler.ModifiableContext;
import de.ii.xtraplatform.features.domain.FeatureTokenContext;
import de.ii.xtraplatform.features.domain.FeatureTokenTransformerBase;
import de.ii.xtraplatform.features.sql.app.ModifiableSqlMutationContext;
import de.ii.xtraplatform.features.sql.app.SqlMutationContext;
import de.ii.xtraplatform.features.domain.pipeline.FeatureEventHandlerSimple.ModifiableContext;
import de.ii.xtraplatform.features.domain.pipeline.FeatureTokenContextSimple;
import de.ii.xtraplatform.features.domain.pipeline.FeatureTokenTransformerBaseSimple;
import java.util.Objects;

public abstract class FeatureTokenTransformerSql
extends FeatureTokenTransformerBase<
SchemaSql, SchemaMappingSql, ModifiableContext<SchemaSql, SchemaMappingSql>> {
extends FeatureTokenTransformerBaseSimple<
SqlQuerySchema, SqlQueryMapping, ModifiableContext<SqlQuerySchema, SqlQueryMapping>> {

private ModifiableContext<SchemaSql, SchemaMappingSql> context;
private ModifiableContext<SqlQuerySchema, SqlQueryMapping> context;

@Override
public Class<? extends ModifiableContext<SchemaSql, SchemaMappingSql>> getContextInterface() {
if (getDownstream() instanceof FeatureTokenContext<?>) {
return ((FeatureTokenContext<ModifiableContext<SchemaSql, SchemaMappingSql>>) getDownstream())
public Class<? extends ModifiableContext<SqlQuerySchema, SqlQueryMapping>> getContextInterface() {
if (getDownstream() instanceof FeatureTokenContextSimple<?>) {
return ((FeatureTokenContextSimple<ModifiableContext<SqlQuerySchema, SqlQueryMapping>>)
getDownstream())
.getContextInterface();
}

return SqlMutationContext.class;
return null;
}

@Override
public final ModifiableContext<SchemaSql, SchemaMappingSql> createContext() {
ModifiableContext<SchemaSql, SchemaMappingSql> context =
getDownstream() instanceof FeatureTokenContext<?>
? ((FeatureTokenContext<ModifiableContext<SchemaSql, SchemaMappingSql>>)
public final ModifiableContext<SqlQuerySchema, SqlQueryMapping> createContext() {
ModifiableContext<SqlQuerySchema, SqlQueryMapping> context =
getDownstream() instanceof FeatureTokenContextSimple<?>
? ((FeatureTokenContextSimple<ModifiableContext<SqlQuerySchema, SqlQueryMapping>>)
getDownstream())
.createContext()
: ModifiableSqlMutationContext.create();
: null;

if (Objects.isNull(this.context)) {
this.context = context;
Expand All @@ -46,7 +45,7 @@ public final ModifiableContext<SchemaSql, SchemaMappingSql> createContext() {
return context;
}

protected final ModifiableContext<SchemaSql, SchemaMappingSql> getContext() {
protected final ModifiableContext<SqlQuerySchema, SqlQueryMapping> getContext() {
if (Objects.isNull(context)) {
return createContext();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,19 @@ default Optional<FeatureSchema> getSchemaForRole(Role role) {
if (role == Role.PRIMARY_GEOMETRY) {
return getMainSchema().getPrimaryGeometry();
}
if (role == Role.PRIMARY_INSTANT) {
return getMainSchema().getPrimaryInstant();
}
if (role == Role.PRIMARY_INTERVAL_START) {
return getMainSchema()
.getPrimaryInterval()
.map(de.ii.xtraplatform.features.domain.Tuple::first);
}
if (role == Role.PRIMARY_INTERVAL_END) {
return getMainSchema()
.getPrimaryInterval()
.map(de.ii.xtraplatform.features.domain.Tuple::second);
}

return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ public void handle(DatasetChange change) {

@Override
public void handle(FeatureChange change) {
executor.submit(() -> featureListeners.forEach(listener -> listener.onFeatureChange(change)));
executor.submit(
() -> {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Handling feature change: {}", change);
}

featureListeners.forEach(
listener -> {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace(
"Notifying feature change listener: {}", listener.getClass().getSimpleName());
}
listener.onFeatureChange(change);
});
});
}
}
Loading