<commit_before>package org.openforis.calc.chain.post;

import java.util.ArrayList;
import java.util.List;

import org.openforis.calc.chain.pre.CreateDataTablesTask;
import org.openforis.calc.engine.Task;
import org.openforis.calc.engine.Workspace;
import org.openforis.calc.metadata.AoiHierarchy;
import org.openforis.calc.metadata.AoiHierarchyLevel;
import org.openforis.calc.metadata.CategoricalVariable;
import org.openforis.calc.metadata.Entity;
import org.openforis.calc.metadata.QuantitativeVariable;
import org.openforis.calc.metadata.Variable;
import org.openforis.calc.metadata.VariableAggregate;
import org.openforis.calc.persistence.postgis.PsqlBuilder;

/**
 * Creates and populates aggregate tables for sampling unit entities and descendants.
 * One for each AOI level (at AOI/stratum level) is created.
 * 
 * @author <NAME>
 * @author <NAME>
 */
public final class CreateAggregateTablesTask extends Task {
	@Override
	protected void execute() throws Throwable {
		Workspace workspace = getWorkspace();

		List<Entity> entities = workspace.getEntities();
		List<AoiHierarchy> aoiHierarchies = workspace.getAoiHierarchies();
		for (Entity entity : entities) {
			if ( entity.isSamplingUnit() ) {
				for (AoiHierarchy hierarchy : aoiHierarchies) {
					List<AoiHierarchyLevel> levels = hierarchy.getLevels();
					for (AoiHierarchyLevel level : levels) {
						createAggregateTable(entity, level);
					}
				}
			}
		}
	}

	private void createAggregateTable(Entity entity, AoiHierarchyLevel level) {
		String factTable = entity.getDataTable();
		String aoiFkColumn = level.getFkColumn();
		String levelName = level.getName();
		String aggTable = "_agg_"+levelName+"_stratum_"+factTable;
		Integer entityId = entity.getId();
		
		List<String> select = new ArrayList<String>();
		List<String> groupBy = new ArrayList<String>();
		List<Variable> variables = entity.getVariables();
		for (Variable variable : variables) {
			if ( variable instanceof CategoricalVariable ) {
				addDimensionColumn((CategoricalVariable) variable, groupBy);
			} else if ( variable instanceof QuantitativeVariable ){
				addMeasureColumn((QuantitativeVariable) variable, select);
			} else {
				throw new UnsupportedOperationException("Unknown variable class");
			}
		}
		groupBy.add(CreateDataTablesTask.STRATUM_ID);
		groupBy.add(aoiFkColumn);
		select.addAll(0, groupBy);
		
		//add aggregate fact count column
		select.add("count(*) as " + "_agg_cnt");

		if ( isDebugMode() ) {
			createPsqlBuilder().dropTableIfExistsCascade(aggTable).execute();
		}
		
		createAggregateTable(factTable, aoiFkColumn, aggTable, entityId, select, groupBy);
	}

	private void addDimensionColumn(CategoricalVariable var, List<String> groupBy) {
		String idCol = var.getCategoryIdColumn();
		if ( idCol != null && var.isDisaggregate() ) {
			groupBy.add(idCol);
		}
	}<commit_msg>Fixed compile error due to cleanup<commit_after>package org.openforis.calc.chain.post;

import java.util.ArrayList;
import java.util.List;

import org.openforis.calc.chain.pre.CreateDataTablesTask;
import org.openforis.calc.engine.Task;
import org.openforis.calc.engine.Workspace;
import org.openforis.calc.metadata.AoiHierarchy;
import org.openforis.calc.metadata.AoiHierarchyLevel;
import org.openforis.calc.metadata.CategoricalVariable;
import org.openforis.calc.metadata.Entity;
import org.openforis.calc.metadata.QuantitativeVariable;
import org.openforis.calc.metadata.Variable;
import org.openforis.calc.metadata.VariableAggregate;
import org.openforis.calc.persistence.postgis.PsqlBuilder;

/**
 * Creates and populates aggregate tables for sampling unit entities and descendants.
 * One for each AOI level (at AOI/stratum level) is created.
 * 
 * @author <NAME>
 * @author <NAME>
 */
public final class CreateAggregateTablesTask extends Task {
	private static final String STRATUM_ID = "stratum_id";

	@Override
	protected void execute() throws Throwable {
		Workspace workspace = getWorkspace();

		List<Entity> entities = workspace.getEntities();
		List<AoiHierarchy> aoiHierarchies = workspace.getAoiHierarchies();
		for (Entity entity : entities) {
			if ( entity.isSamplingUnit() ) {
				for (AoiHierarchy hierarchy : aoiHierarchies) {
					List<AoiHierarchyLevel> levels = hierarchy.getLevels();
					for (AoiHierarchyLevel level : levels) {
						createAggregateTable(entity, level);
					}
				}
			}
		}
	}

	private void createAggregateTable(Entity entity, AoiHierarchyLevel level) {
		String factTable = entity.getDataTable();
		String aoiFkColumn = level.getFkColumn();
		String levelName = level.getName();
		String aggTable = "_agg_"+levelName+"_stratum_"+factTable;
		Integer entityId = entity.getId();
		
		List<String> select = new ArrayList<String>();
		List<String> groupBy = new ArrayList<String>();
		List<Variable> variables = entity.getVariables();
		for (Variable variable : variables) {
			if ( variable instanceof CategoricalVariable ) {
				addDimensionColumn((CategoricalVariable) variable, groupBy);
			} else if ( variable instanceof QuantitativeVariable ){
				addMeasureColumn((QuantitativeVariable) variable, select);
			} else {
				throw new UnsupportedOperationException("Unknown variable class");
			}
		}
		groupBy.add(STRATUM_ID);
		groupBy.add(aoiFkColumn);
		select.addAll(0, groupBy);
		
		//add aggregate fact count column
		select.add("count(*) as " + "_agg_cnt");

		if ( isDebugMode() ) {
			createPsqlBuilder().dropTableIfExistsCascade(aggTable).execute();
		}
		
		createAggregateTable(factTable, aoiFkColumn, aggTable, entityId, select, groupBy);
	}

	private void addDimensionColumn(CategoricalVariable var, List<String> groupBy) {
		String idCol = var.getCategoryIdColumn();
		if ( idCol != null && var.isDisaggregate() ) {
			groupBy.add(idCol);
		}
	}
{'fraction_non_alphanumeric': 0.11003292323687403, 'fraction_numerical': 0.00034656038814763474, 'mean_word_length': 4.987551867219917, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '3527093', 'n_tokens_mistral': 1951, 'n_tokens_neox': 1779, 'n_words': 381}