<commit_before>package org.openforis.calc.metadata.task;

import java.util.List;

import org.openforis.calc.engine.Task;
import org.openforis.calc.engine.Workspace;
import org.openforis.calc.metadata.AoiHierarchy;
import org.openforis.calc.metadata.AoiLevel;

/**
 * Task responsible for assigning AOI codes and/or ids to the first phase plots.
 * 
 * @author <NAME>
 */
public final class UpdateSamplingUnitAoisTask extends Task {
	
	@Override
	protected void execute() throws Throwable {
		Workspace ws = getWorkspace();
		deleteSamplingUnitAois(ws.getId());
		populateSamplingUnitAois(ws);
	}

	private void deleteSamplingUnitAois(int wsId) {
		createPsqlBuilder()
			.deleteFrom("calc.sampling_unit_aoi")
			.where("workspace_id = ?")
			.execute(wsId);
	}

	private void populateSamplingUnitAois(Workspace ws) {
		List<AoiHierarchy> hierarchies = ws.getAoiHierarchies();
		for ( AoiHierarchy hierarchy : hierarchies ) {
			List<AoiLevel> levels = hierarchy.getLevels();
			
			AoiLevel childLevel = null;
			for ( int i = levels.size() - 1 ; i >= 0 ; i-- ) {
				AoiLevel level = levels.get(i);
				log().debug("AOI Level: "+level);
				
				if (childLevel == null) {
					insertLeafStratumAois(ws, level);
				} else {
					insertAncestorStratumAois(ws, childLevel);
				}
				
				childLevel = level;
			}
		}
	}

	private void insertAncestorStratumAois(Workspace ws,
			AoiLevel childLevel) {
		createPsqlBuilder()
		.insertInto("calc.sampling_unit_aoi","sampling_unit_id", "aoi_id","workspace_id")
		.select("s.sampling_unit_id", "a.parent_aoi_id", ws.getId() )
		.from("calc.sampling_unit_aoi s")
		.innerJoin("calc.aoi a")
		.on("s.aoi_id = a.id")
		.and("a.aoi_level_id = " + childLevel.getId() )
		.execute();
	}

	private void insertLeafStratumAois(Workspace ws, AoiLevel level) {
		createPsqlBuilder()
			.insertInto("calc.sampling_unit_aoi","sampling_unit_id", "aoi_id","workspace_id")
			.select("u.id", "a.id", ws.getId())
			.from("calc.sampling_unit u")
			.innerJoin("calc.entity e")
			.on("u.entity_id = e.id")
			.and("e.workspace_id  = " + ws.getId())
			.innerJoin("calc.aoi a")
			.on("ST_Contains(a.shape, u.location)")
			.and("a.aoi_level_id = " + level.getId())
			.execute();
	}


}

<commit_msg>CALC-145 - Migrate UpdateSamplingUnitAoisTask to new Psql API<commit_after>package org.openforis.calc.metadata.task;

import static org.openforis.calc.persistence.jooq.tables.AoiTable.AOI;
import static org.openforis.calc.persistence.jooq.tables.EntityTable.ENTITY;
import static org.openforis.calc.persistence.jooq.tables.SamplingUnitAoiTable.SAMPLING_UNIT_AOI;
import static org.openforis.calc.persistence.jooq.tables.SamplingUnitTable.SAMPLING_UNIT;

import java.util.List;

import org.jooq.Insert;
import org.jooq.Record;
import org.jooq.impl.DSL;
import org.openforis.calc.engine.Task;
import org.openforis.calc.engine.Workspace;
import org.openforis.calc.metadata.AoiHierarchy;
import org.openforis.calc.metadata.AoiLevel;

/**
 * Task responsible for assigning AOI codes and/or ids to the first phase plots.
 * 
 * @author <NAME>
 * @author <NAME>
 */
public final class UpdateSamplingUnitAoisTask extends Task {
	
	@Override
	protected void execute() throws Throwable {
		Workspace ws = getWorkspace();
		deleteSamplingUnitAois(ws.getId());
		populateSamplingUnitAois(ws);
	}

	private void deleteSamplingUnitAois(int wsId) {
		psql()
			.delete(SAMPLING_UNIT_AOI)
			.where(SAMPLING_UNIT_AOI.WORKSPACE_ID.eq(wsId))
			.execute();
	}

	private void populateSamplingUnitAois(Workspace ws) {
		List<AoiHierarchy> hierarchies = ws.getAoiHierarchies();
		for ( AoiHierarchy hierarchy : hierarchies ) {
			List<AoiLevel> levels = hierarchy.getLevels();
			
			AoiLevel childLevel = null;
			for ( int i = levels.size() - 1 ; i >= 0 ; i-- ) {
				AoiLevel level = levels.get(i);
				log().debug("AOI Level: "+level);
				
				if (childLevel == null) {
					insertLeafStratumAois(ws, level);
				} else {
					insertAncestorStratumAois(ws, childLevel);
				}
				
				childLevel = level;
			}
		}
	}

	private void insertAncestorStratumAois(Workspace ws,
			AoiLevel childLevel) {
		Insert<Record> insert = psql()
			.insertInto(SAMPLING_UNIT_AOI, 
					SAMPLING_UNIT_AOI.SAMPLING_UNIT_ID,
					SAMPLING_UNIT_AOI.AOI_ID,
					SAMPLING_UNIT_AOI.WORKSPACE_ID)
			.select(psql()
					.select(
							SAMPLING_UNIT_AOI.SAMPLING_UNIT_ID,
							AOI.PARENT_AOI_ID, 
							DSL.val(ws.getId()))
					.from(SAMPLING_UNIT_AOI)
						.join(AOI)
						.on(AOI.ID.eq(SAMPLING_UNIT_AOI.AOI_ID))
					.where(AOI.AOI_LEVEL_ID.eq(childLevel.getId()))
					);
		insert.execute();
	}

	private void insertLeafStratumAois(Workspace ws, AoiLevel level) {
		Insert<Record> insert = psql()
			.insertInto(SAMPLING_UNIT_AOI, 
					SAMPLING_UNIT_AOI.SAMPLING_UNIT_ID, 
					SAMPLING_UNIT_AOI.AOI_ID, 
					SAMPLING_UNIT_AOI.WORKSPACE_ID)
			.select(psql()
					.select(SAMPLING_UNIT.ID, AOI.ID, DSL.val(ws.getId()))
					.from(SAMPLING_UNIT)
						.join(ENTITY)
							.on(SAMPLING_UNIT.ENTITY_ID.eq(ENTITY.ID))
						.join(AOI).on("ST_Contains("+ AOI.SHAPE +","+ SAMPLING_UNIT.LOCATION+")")
					.where(ENTITY.WORKSPACE_ID.eq(ws.getId())
							.and(AOI.AOI_LEVEL_ID.eq(level.getId()))
					)
				);
		insert.execute();
	}


}


{'fraction_non_alphanumeric': 0.12869276218611522, 'fraction_numerical': 0.001292466765140325, 'mean_word_length': 3.354501607717042, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 14, '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': '6724797', 'n_tokens_mistral': 2364, 'n_tokens_neox': 2012, 'n_words': 309}