Skip to content

Commit

Permalink
Changed the default orderby for Observations to phenomenonTime, id
Browse files Browse the repository at this point in the history
  • Loading branch information
hylkevds committed Feb 23, 2024
1 parent a6faaf5 commit f632045
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ make sure to check and update your HELM settings.
* Added support for headers in JSON-Batch requets, enabling JSON-Patch requests in JSON-Batch requests.
* Added option for KeycloakAuthProvider to locally register user.
* Implemented expand option for MQTT Topics: `v1.1/Observations?$select=id&$expand=Datastream($expand=Thing)`
* Changed the default orderby for Observations to `phenomenonTime, id`.

**Internal changes & Bugfixes**
* Fixed #1754: resultTime/phenomenonTime mixup in MultiDatastream.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@
import de.fraunhofer.iosb.ilt.frostserver.property.NavigationPropertyMain.NavigationPropertyEntity;
import de.fraunhofer.iosb.ilt.frostserver.property.NavigationPropertyMain.NavigationPropertyEntitySet;
import de.fraunhofer.iosb.ilt.frostserver.property.Property;
import de.fraunhofer.iosb.ilt.frostserver.query.OrderBy;
import de.fraunhofer.iosb.ilt.frostserver.query.expression.Path;
import de.fraunhofer.iosb.ilt.frostserver.util.exception.IncompleteEntityException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
Expand Down Expand Up @@ -108,10 +109,6 @@ public class EntityType implements Annotatable, Comparable<EntityType> {
* The set of Navigation properties pointing to entity sets.
*/
private final Set<NavigationPropertyMain<EntitySet>> navigationSets = new TreeSet<>();
/**
* The map of NavigationProperties by their target EntityTypes.
*/
private final Map<EntityType, NavigationPropertyMain> navigationPropertiesByTarget = new HashMap<>();

private final Map<String, EntityValidator> validatorsCreateEntity = new LinkedHashMap<>();
private final Map<String, EntityValidator> validatorsUpdateEntity = new LinkedHashMap<>();
Expand All @@ -121,6 +118,11 @@ public class EntityType implements Annotatable, Comparable<EntityType> {
*/
private final List<Annotation> annotations = new ArrayList<>();

/**
* The list of oderby's to use by default.
*/
private final List<OrderBy> orderbyDefaults = new ArrayList<>();

/**
* The ModelRegistry this EntityType is registered on.
*/
Expand Down Expand Up @@ -191,9 +193,19 @@ public void init() {
} else {
navigationEntities.add(np);
}
navigationPropertiesByTarget.put(np.getEntityType(), np);
}
}
// Make sure we have a default orderby and that it contains the primary key last.
boolean pkOrder = false;
final String pkName = primaryKey.getName();
for (OrderBy order : orderbyDefaults) {
if (pkName.equals(order.getExpression().toUrl())) {
pkOrder = true;
}
}
if (!pkOrder) {
orderbyDefaults.add(new OrderBy(new Path(primaryKey), OrderBy.OrderType.ASCENDING));
}
}

/**
Expand Down Expand Up @@ -524,4 +536,17 @@ public Id parsePrimaryKey(String input) {
throw new IllegalArgumentException("Can not use " + ((rawId == null) ? "null" : rawId.getClass().getName()) + " (" + input + ") as an Id");
}

public List<OrderBy> getOrderbyDefaults() {
return orderbyDefaults;
}

public EntityType addOrderByDefault(OrderBy order) {
orderbyDefaults.add(order);
return this;
}

public EntityType clearOrderByDefaults() {
orderbyDefaults.clear();
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
import de.fraunhofer.iosb.ilt.frostserver.model.EntityType;
import de.fraunhofer.iosb.ilt.frostserver.model.ModelRegistry;
import de.fraunhofer.iosb.ilt.frostserver.model.core.annotations.Annotation;
import de.fraunhofer.iosb.ilt.frostserver.property.Property;
import de.fraunhofer.iosb.ilt.frostserver.query.OrderBy;
import de.fraunhofer.iosb.ilt.frostserver.query.expression.Path;
import de.fraunhofer.iosb.ilt.frostserver.util.StringHelper;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
Expand Down Expand Up @@ -77,6 +81,15 @@ public class DefEntityType implements AnnotatedConfigurable<Void, Void> {
@EditorString.EdOptsString()
private String table;

/**
* The properties to order entities of this type by.
*/
@ConfigurableField(editor = EditorList.class,
label = "OrderBy", description = "The default way to order this entity type.")
@EditorList.EdOptsList(editor = EditorString.class)
@EditorString.EdOptsString()
private List<String> orderByDflt;

/**
* The EntityProperties of the EntityType.
*/
Expand Down Expand Up @@ -162,6 +175,12 @@ public void linkProperties(ModelRegistry modelRegistry) {
defNp.setSourceEntityType(entityType);
defNp.registerProperties(modelRegistry);
}
if (!StringHelper.isNullOrEmpty(orderByDflt)) {
for (String ob : orderByDflt) {
Property property = entityType.getProperty(ob);
entityType.addOrderByDefault(new OrderBy(new Path(property)));
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import de.fraunhofer.iosb.ilt.frostserver.property.NavigationPropertyMain;
import de.fraunhofer.iosb.ilt.frostserver.property.Property;
import de.fraunhofer.iosb.ilt.frostserver.query.expression.Expression;
import de.fraunhofer.iosb.ilt.frostserver.query.expression.Path;
import de.fraunhofer.iosb.ilt.frostserver.util.StringHelper;
import de.fraunhofer.iosb.ilt.frostserver.util.user.PrincipalExtended;
import java.util.ArrayList;
Expand Down Expand Up @@ -190,7 +189,9 @@ public Query validate(EntityType entityType) {
}
}
if (settings.isAlwaysOrder() && !pkOrder && !selectDistinct) {
orderBy.add(new OrderBy(new Path(primaryKey), OrderBy.OrderType.ASCENDING));
for (OrderBy dfltOrder : entityType.getOrderbyDefaults()) {
orderBy.add(dfltOrder);
}
pkOrder = true;
}
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public void createEntities() throws ServiceFailureException {
}
}

OBSERVATIONS.addAll(serviceAdmin.query(mdlSensing.etObservation).top(100).list().toList());
OBSERVATIONS.addAll(serviceAdmin.query(mdlSensing.etObservation).top(100).orderBy("id").list().toList());
} catch (IOException ex) {
LOGGER.error("Failed to read resource", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.time.format.FormatStyle;
import java.util.Collection;
import java.util.Collections;
import java.util.Locale;
import java.util.Objects;
Expand Down Expand Up @@ -85,6 +86,10 @@ public static boolean isNullOrEmpty(String string) {
return string == null || string.isEmpty();
}

public static boolean isNullOrEmpty(Collection collection) {
return collection == null || collection.isEmpty();
}

/**
* Replaces characters that might break logging output. Currently: \n, \r
* and \t
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import de.fraunhofer.iosb.ilt.frostserver.property.type.TypeComplex;
import de.fraunhofer.iosb.ilt.frostserver.property.type.TypeSimpleCustom;
import de.fraunhofer.iosb.ilt.frostserver.property.type.TypeSimplePrimitive;
import de.fraunhofer.iosb.ilt.frostserver.query.OrderBy;
import de.fraunhofer.iosb.ilt.frostserver.query.expression.Path;
import de.fraunhofer.iosb.ilt.frostserver.service.PluginModel;
import de.fraunhofer.iosb.ilt.frostserver.service.PluginRootDocument;
import de.fraunhofer.iosb.ilt.frostserver.service.Service;
Expand Down Expand Up @@ -324,7 +326,8 @@ public boolean linkEntityTypes(PersistenceManager pm) {
.registerProperty(epValidTime)
.registerProperty(epParameters)
.registerProperty(npDatastreamObservation)
.registerProperty(npFeatureOfInterestObservation);
.registerProperty(npFeatureOfInterestObservation)
.addOrderByDefault(new OrderBy(new Path(epPhenomenonTime)));
etObservedProperty
.registerProperty(epIdObsProp)
.registerProperty(epName)
Expand Down

0 comments on commit f632045

Please sign in to comment.