<commit_before>package li.strolch.report;

import static li.strolch.utils.helper.StringHelper.DASH;

import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import com.google.gson.JsonObject;

import li.strolch.model.Locator;
import li.strolch.model.ParameterBag;
import li.strolch.model.Resource;
import li.strolch.model.StrolchRootElement;
import li.strolch.model.StrolchValueType;
import li.strolch.model.parameter.DateParameter;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.parameter.StringParameter;
import li.strolch.model.visitor.ElementDateVisitor;
import li.strolch.model.visitor.ElementStateVisitor;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.runtime.StrolchConstants;
import li.strolch.utils.collections.DateRange;
import li.strolch.utils.collections.MapOfSets;
import li.strolch.utils.iso8601.ISO8601FormatFactory;

/**
 * @author <NAME> &lt;<EMAIL>&gt;
 */
public class GenericReport {

	private static final String TYPE_REPORT = "Report";

	private static final String BAG_RELATIONS = "relations";
	private static final String BAG_JOINS = "joins";
	private static final String BAG_PARAMETERS = "parameters";
	private static final String BAG_COLUMNS = "columns";

	private static final String PARAM_OBJECT_TYPE = "objectType";
	private static final String PARAM_DATE_RANGE_SEL = "dateRangeSel";

	private static final String COL_ID = "$id";
	private static final String COL_NAME = "$name";
	private static final String COL_TYPE = "$type";
	private static final String COL_STATE = "$state";
	private static final String COL_DATE = "$date";
	private static final String COL_SEARCH = "$search";

	private static final String SEARCH_SEPARATOR = ":";

	private static final String SUFFIX_REF = "-Ref";

	// input
	private StrolchTransaction tx;
	private String reportId;

	private Resource report;
	private ParameterBag columnsBag;
	private StringParameter dateRangeSelP;

	private DateRange dateRange;
	private MapOfSets<String, String> filtersByType;

	public GenericReport(StrolchTransaction tx, String reportId) {
		this.tx = tx;
		this.reportId = reportId;
	}

	public GenericReport dateRange(DateRange dateRange) {
		this.dateRange = dateRange;
		return this;
	}

	public GenericReport filter(String type, String... ids) {
		if (this.filtersByType == null)
			this.filtersByType = new MapOfSets<>();
		for (String id : ids) {
			this.filtersByType.addElement(type, id);
		}
		return this;
	}

	public GenericReport filter(String type, List<String> ids) {
		if (this.filtersByType == null)
			this.filtersByType = new MapOfSets<>();
		for (String id : ids) {
			this.filtersByType.addElement(type, id);
		}
		return this;
	}

	public Stream<Map<String, StrolchRootElement>> buildStream() {

		// get the report
		this.report = this.tx.getResourceBy(TYPE_REPORT, this.reportId);
		this.columnsBag = this.report.getParameterBag(BAG_COLUMNS);
		this.dateRangeSelP = this.report.getParameter(BAG_PARAMETERS, PARAM_DATE_RANGE_SEL);

		// query the main objects and return a stream
		return queryRows().map(e -> evaluateRow(e)).filter(e -> filter(e));
	}

	public MapOfSets<String, StrolchRootElement> generateFilterCriteria() {<commit_msg>[Minor] Also allow to pass a Set for filter<commit_after>package li.strolch.report;

import static li.strolch.utils.helper.StringHelper.DASH;

import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import com.google.gson.JsonObject;

import li.strolch.model.Locator;
import li.strolch.model.ParameterBag;
import li.strolch.model.Resource;
import li.strolch.model.StrolchRootElement;
import li.strolch.model.StrolchValueType;
import li.strolch.model.parameter.DateParameter;
import li.strolch.model.parameter.Parameter;
import li.strolch.model.parameter.StringParameter;
import li.strolch.model.visitor.ElementDateVisitor;
import li.strolch.model.visitor.ElementStateVisitor;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.runtime.StrolchConstants;
import li.strolch.utils.collections.DateRange;
import li.strolch.utils.collections.MapOfSets;
import li.strolch.utils.iso8601.ISO8601FormatFactory;

/**
 * @author <NAME> &lt;<EMAIL>&gt;
 */
public class GenericReport {

	private static final String TYPE_REPORT = "Report";

	private static final String BAG_RELATIONS = "relations";
	private static final String BAG_JOINS = "joins";
	private static final String BAG_PARAMETERS = "parameters";
	private static final String BAG_COLUMNS = "columns";

	private static final String PARAM_OBJECT_TYPE = "objectType";
	private static final String PARAM_DATE_RANGE_SEL = "dateRangeSel";

	private static final String COL_ID = "$id";
	private static final String COL_NAME = "$name";
	private static final String COL_TYPE = "$type";
	private static final String COL_STATE = "$state";
	private static final String COL_DATE = "$date";
	private static final String COL_SEARCH = "$search";

	private static final String SEARCH_SEPARATOR = ":";

	private static final String SUFFIX_REF = "-Ref";

	// input
	private StrolchTransaction tx;
	private String reportId;

	private Resource report;
	private ParameterBag columnsBag;
	private StringParameter dateRangeSelP;

	private DateRange dateRange;
	private MapOfSets<String, String> filtersByType;

	public GenericReport(StrolchTransaction tx, String reportId) {
		this.tx = tx;
		this.reportId = reportId;
	}

	public GenericReport dateRange(DateRange dateRange) {
		this.dateRange = dateRange;
		return this;
	}

	public GenericReport filter(String type, String... ids) {
		if (this.filtersByType == null)
			this.filtersByType = new MapOfSets<>();
		for (String id : ids) {
			this.filtersByType.addElement(type, id);
		}
		return this;
	}

	public GenericReport filter(String type, List<String> ids) {
		if (this.filtersByType == null)
			this.filtersByType = new MapOfSets<>();
		for (String id : ids) {
			this.filtersByType.addElement(type, id);
		}
		return this;
	}

	public GenericReport filter(String type, Set<String> ids) {
		if (this.filtersByType == null)
			this.filtersByType = new MapOfSets<>();
		for (String id : ids) {
			this.filtersByType.addElement(type, id);
		}
		return this;
	}

	public Stream<Map<String, StrolchRootElement>> buildStream() {

		// get the report
		this.report = this.tx.getResourceBy(TYPE_REPORT, this.reportId);
		this.columnsBag = this.report.getParameterBag(BAG_COLUMNS);
		this.dateRangeSelP = this.report.getParameter(BAG_PARAMETERS, PARAM_DATE_RANGE_SEL);

		// query the main objects and return a stream
		return queryRows().map(e -> evaluateRow(e)).filter(e -> filter(e));
	}

	public MapOfSets<String, StrolchRootElement> generateFilterCriteria() {
{'fraction_non_alphanumeric': 0.10437945991325032, 'fraction_numerical': 0.0022387015530992023, 'mean_word_length': 5.242794759825328, 'pattern_counts': {'":': 2, '<': 23, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5396951', 'n_tokens_mistral': 2357, 'n_tokens_neox': 2252, 'n_words': 552}