Skip to content

Commit

Permalink
#184 JsonKPILayout.groupByField
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Mar 3, 2017
1 parent 714b333 commit 57a272e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

import org.adempiere.ad.expression.api.IStringExpression;
import org.adempiere.ad.expression.api.impl.StringExpressionCompiler;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.util.Check;
import org.adempiere.util.GuavaCollectors;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
Expand Down Expand Up @@ -83,10 +85,21 @@ private KPI(final Builder builder)
defaultTimeRange = builder.defaultTimeRange;

fields = ImmutableList.copyOf(builder.fields);
groupByField = fields.stream()
final List<KPIField> groupByFieldsList = fields.stream()
.filter(KPIField::isGroupBy)
.findFirst()
.orElse(null);
.collect(GuavaCollectors.toImmutableList());
if (groupByFieldsList.isEmpty())
{
groupByField = null;
}
else if (groupByFieldsList.size() == 1)
{
groupByField = groupByFieldsList.get(0);
}
else
{
throw new AdempiereException("Only one group by field allowed but we found: " + groupByFieldsList);
}

esSearchIndex = builder.esSearchIndex;
esSearchTypes = builder.esSearchTypes;
Expand Down Expand Up @@ -132,13 +145,19 @@ public List<KPIField> getFields()

public KPIField getGroupByField()
{
final KPIField groupByField = getGroupByFieldOrNull();
if (groupByField == null)
{
throw new IllegalStateException("KPI has no group by field defined");
}
return groupByField;
}

public KPIField getGroupByFieldOrNull()
{
return groupByField;
}

public Duration getDefaultTimeRange()
{
return defaultTimeRange;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ public static final JsonKPIFieldLayout offsetField(final KPIField kpiField, fina
@JsonProperty("fieldName")
private final String fieldName;

@JsonProperty("groupBy")
private final boolean groupBy;
// NOTE: not needed because we are providing a separate groupByField in JsonKPILayout
// @JsonProperty("groupBy")
// private final boolean groupBy;

@JsonProperty("dataType")
private final String dataType;
Expand Down Expand Up @@ -95,7 +96,7 @@ public JsonKPIFieldLayout(final KPIField kpiField, final boolean isOffsetField,
description = kpiField.getDescription(adLanguage);
unit = kpiField.getUnit();

groupBy = kpiField.isGroupBy();
// groupBy = kpiField.isGroupBy();
dataType = kpiField.getValueType().toJson();

color = kpiField.getColor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ public static final JsonKPILayout of(final KPI kpi, final JSONOptions jsonOpts)

@JsonProperty("pollIntervalSec")
private final int pollIntervalSec;


@JsonProperty("groupByField")
@JsonInclude(JsonInclude.Include.NON_NULL)
private final JsonKPIFieldLayout groupByField;

@JsonProperty("fields")
private final List<JsonKPIFieldLayout> fields;

Expand All @@ -72,10 +76,21 @@ public JsonKPILayout(final KPI kpi, final JSONOptions jsonOpts)

pollIntervalSec = kpi.getPollIntervalSec();

//
// Group by field
final KPIField groupByField = kpi.getGroupByFieldOrNull();
this.groupByField = groupByField == null ? null : JsonKPIFieldLayout.field(groupByField, jsonOpts);

final ImmutableList.Builder<JsonKPIFieldLayout> jsonFields = ImmutableList.builder();
final boolean hasCompareOffset = kpi.hasCompareOffset();
for (final KPIField kpiField : kpi.getFields())
{
// Don't add the group by field to our fields list
if (kpiField.isGroupBy())
{
continue;
}

jsonFields.add(JsonKPIFieldLayout.field(kpiField, jsonOpts));

if (hasCompareOffset && !kpiField.isGroupBy())
Expand Down

0 comments on commit 57a272e

Please sign in to comment.