Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aggregation queries support #266 #279

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
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 @@ -3,7 +3,12 @@
import com.fasterxml.jackson.core.JsonGenerator;
import com.nhl.link.rest.EntityProperty;
import com.nhl.link.rest.ResourceEntity;
import com.nhl.link.rest.encoder.*;
import com.nhl.link.rest.encoder.CollectionEncoder;
import com.nhl.link.rest.encoder.DataResponseEncoder;
import com.nhl.link.rest.encoder.Encoder;
import com.nhl.link.rest.encoder.EncoderFilter;
import com.nhl.link.rest.encoder.GenericEncoder;
import com.nhl.link.rest.encoder.PropertyMetadataEncoder;
import com.nhl.link.rest.meta.LrRelationship;
import com.nhl.link.rest.runtime.encoder.EncoderService;
import com.nhl.link.rest.runtime.encoder.IAttributeEncoderFactory;
Expand All @@ -28,8 +33,7 @@ public SenchaEncoderService(@Inject List<EncoderFilter> filters,
}

@Override
public <T> Encoder dataEncoder(ResourceEntity<T> entity) {
CollectionEncoder resultEncoder = resultEncoder(entity);
protected DataResponseEncoder toDataResponseEncoder(CollectionEncoder resultEncoder) {
return new DataResponseEncoder("data", resultEncoder, "total", GenericEncoder.encoder()) {
@Override
protected void encodeObjectBody(Object object, JsonGenerator out) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public boolean willEncode(String propertyName, Object object, Encoder delegate)

ResourceEntity<E2> e2Descriptor = getResourceEntity(E2.class);
e2Descriptor.includeId();
e2Descriptor.setIncoming(metadataService.getLrRelationship(E3.class, E3.E2.getName()));

ResourceEntity<E3> e3Descriptor = getResourceEntity(E3.class);
e3Descriptor.includeId();
Expand Down
22 changes: 22 additions & 0 deletions link-rest/src/main/java/com/nhl/link/rest/AggregationType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.nhl.link.rest;

public enum AggregationType {

AVERAGE("avg"),

SUM("sum"),

MINIMUM("min"),

MAXIMUM("max");

private String functionName;

AggregationType(String functionName) {
this.functionName = functionName;
}

public String functionName() {
return functionName;
}
}
40 changes: 40 additions & 0 deletions link-rest/src/main/java/com/nhl/link/rest/ResourceEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@
public class ResourceEntity<T> {

private boolean idIncluded;
private boolean countIncluded;

private LrEntity<T> lrEntity;
private Map<String, LrAttribute> attributes;
private Collection<String> defaultProperties;

private Map<AggregationType, List<LrAttribute>> aggregatedAttributes;

private String applicationBase;
private String mapByPath;
private ResourceEntity<?> mapBy;
private Map<String, ResourceEntity<?>> children;
private Map<String, ResourceEntity<?>> aggregateChildren;
private LrRelationship incoming;
private List<Ordering> orderings;
private Expression qualifier;
Expand All @@ -48,7 +52,9 @@ public ResourceEntity(LrEntity<T> lrEntity) {
this.idIncluded = false;
this.attributes = new HashMap<>();
this.defaultProperties = new HashSet<>();
this.aggregatedAttributes = new HashMap<>();
this.children = new HashMap<>();
this.aggregateChildren = new HashMap<>();
this.orderings = new ArrayList<>(2);
this.extraProperties = new HashMap<>();
this.lrEntity = lrEntity;
Expand All @@ -70,6 +76,10 @@ public LrRelationship getIncoming() {
return incoming;
}

public void setIncoming(LrRelationship incoming) {
this.incoming = incoming;
}

public Expression getQualifier() {
return qualifier;
}
Expand Down Expand Up @@ -128,6 +138,10 @@ public ResourceEntity<?> getChild(String name) {
return children.get(name);
}

public Map<String, ResourceEntity<?>> getAggregateChildren() {
return aggregateChildren;
}

public Map<String, EntityProperty> getExtraProperties() {
return extraProperties;
}
Expand Down Expand Up @@ -238,4 +252,30 @@ public boolean isFiltered() {
public void setFiltered(boolean filtered) {
this.filtered = filtered;
}

public void includeCount() {
this.countIncluded = true;
}

public boolean isCountIncluded() {
return countIncluded;
}

public List<LrAttribute> getAggregatedAttributes(AggregationType aggregationType) {
return aggregatedAttributes.computeIfAbsent(aggregationType, it -> new ArrayList<>());
}

public boolean isAggregate() {
if (countIncluded) {
return true;
}

for (Collection<LrAttribute> attributes : aggregatedAttributes.values()) {
if (attributes.size() > 0) {
return true;
}
}

return false;
}
}
Loading