All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.molgenis.api.data.v2.EntityAggregatesResponse Maven / Gradle / Ivy

There is a newer version: 8.4.5
Show newest version
package org.molgenis.api.data.v2;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.molgenis.data.util.EntityTypeUtils.isReferenceType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.molgenis.data.Entity;
import org.molgenis.data.aggregation.AggregateResult;
import org.molgenis.data.meta.model.Attribute;
import org.molgenis.data.security.aggregation.AnonymizedAggregateResult;

public class EntityAggregatesResponse extends EntityCollectionResponseV2 {
  private final AggregateResultResponse aggs;
  private final AttributeResponseV2 xAttr;
  private final AttributeResponseV2 yAttr;

  public EntityAggregatesResponse(
      AggregateResult aggs, AttributeResponseV2 xAttr, AttributeResponseV2 yAttr, String href) {
    super(href);
    this.aggs = checkNotNull(AggregateResultResponse.toResponse(aggs));
    this.xAttr = xAttr;
    this.yAttr = yAttr;
  }

  public AggregateResultResponse getAggs() {
    return aggs;
  }

  public AttributeResponseV2 getXAttr() {
    return xAttr;
  }

  public AttributeResponseV2 getYAttr() {
    return yAttr;
  }

  public static class AggregateResultResponse {
    private final List> matrix;
    private final List xLabels;
    private final List yLabels;
    private final Integer threshold;

    public AggregateResultResponse(
        List> matrix, List xLabels, List yLabels, Integer threshold) {
      this.matrix = matrix;
      this.xLabels = xLabels;
      this.yLabels = yLabels;
      this.threshold = threshold;
    }

    public static AggregateResultResponse toResponse(AggregateResult aggs) {
      List> matrix = aggs.getMatrix();
      List xLabels = convert(aggs.getxLabels());
      List yLabels = convert(aggs.getyLabels());
      Integer threshold = toAggregateThreshold(aggs);
      return new AggregateResultResponse(matrix, xLabels, yLabels, threshold);
    }

    private static Integer toAggregateThreshold(AggregateResult aggs) {
      Integer threshold;
      if (aggs instanceof AnonymizedAggregateResult) {
        int thresholdInt = ((AnonymizedAggregateResult) aggs).getAnonymizationThreshold();
        if (thresholdInt != AnonymizedAggregateResult.AGGREGATE_ANONYMIZATION_VALUE) {
          threshold = thresholdInt;
        } else {
          threshold = null;
        }
      } else {
        threshold = null;
      }
      return threshold;
    }

    private static List convert(List xLabels) {
      return xLabels.stream()
          .map(
              xLabel -> {
                Object value;
                if (xLabel instanceof Entity) {
                  Map valueMap = new HashMap<>();
                  Entity entity = (Entity) xLabel;
                  for (Attribute attr : entity.getEntityType().getAtomicAttributes()) {
                    if (!isReferenceType(attr)) {
                      Object attributeValue = entity.get(attr.getName());
                      valueMap.put(
                          attr.getName(),
                          attributeValue != null ? attributeValue.toString() : null);
                    }
                  }
                  value = valueMap;
                } else {
                  value = xLabel;
                }
                return value;
              })
          .collect(Collectors.toList());
    }

    public List> getMatrix() {
      return matrix;
    }

    public List getxLabels() {
      return xLabels;
    }

    public List getyLabels() {
      return yLabels;
    }

    public Integer getThreshold() {
      return threshold;
    }
  }
}