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

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

There is a newer version: 7.4.9
Show newest version
package org.molgenis.data.rest.v2;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.molgenis.data.AggregateAnonymizer;
import org.molgenis.data.AggregateResult;
import org.molgenis.data.AnonymizedAggregateResult;
import org.molgenis.data.AttributeMetaData;
import org.molgenis.data.Entity;

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

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

	public AggregateResultResponse getAggs()
	{
		return aggs;
	}

	public AttributeMetaDataResponseV2 getXAttr()
	{
		return xAttr;
	}

	public AttributeMetaDataResponseV2 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 != AggregateAnonymizer.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 (AttributeMetaData attr : entity.getEntityMetaData().getAtomicAttributes())
					{
						switch (attr.getDataType().getEnumType())
						{
							case XREF:
							case CATEGORICAL:
							case MREF:
							case CATEGORICAL_MREF:
							case COMPOUND:
							case FILE:
								break;
							// $CASES-OMITTED$
							default:
								valueMap.put(attr.getName(), entity.getString(attr.getName()));
						}
					}
					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;
		}
	}
}