package org.openforis.calc.persistence;

import java.io.IOException;

import org.openforis.calc.io.flat.FlatDataStream;
import org.openforis.calc.transformation.InputDataTransformationStep;
import org.openforis.calc.transformation.OutputDataTransformationStep;
import org.openforis.calc.transformation.Transformation;
import org.openforis.calc.transformation.TransformationDatabase;
import org.pentaho.di.core.exception.KettleException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Mino Togna
 */
@Component
@Transactional
public class AreaFactDao {
	// extends JooqDaoSupport<AoiRecord, Aoi> {

	@Autowired
	private TransformationDatabase transformationDatabase;

	public AreaFactDao() {
		super();
	}

	synchronized 
	public void createOrUpdateAreaFactTable(FlatDataStream data, String schema, String tableName) {
		try {
			Transformation transformation = new Transformation(transformationDatabase, "importResults");

			// 1. drop table
			String dropTableSql = "drop table if exists " + schema + "." + tableName + " cascade";
			transformation.executeSql(dropTableSql);

			// 2. read data in memory
			InputDataTransformationStep inputDataTransformation = new InputDataTransformationStep(data);
			transformation.addStep(inputDataTransformation);

			OutputDataTransformationStep outputDataTransformation = new OutputDataTransformationStep(transformationDatabase, schema, tableName);
			transformation.addStep(outputDataTransformation);

			// 3. create table
			transformation.executeSQLStatementsString();

			// 4. exec transformation
			transformation.executeAndWaitUntilFinished();
		} catch ( KettleException e ) {
			throw new PersistenceException("Error while importing estimation results", e);
		} catch ( IOException e ) {
			throw new PersistenceException("Error while reading data input", e);
		}

	}

	// public FlatDataStream streamByHierarchyName(String[] fieldNames, String hierarchyName) {
	// Field<?>[] fields = getFields(fieldNames);
	// if(fields == null || fields.length == 0) {
	// fields = AOI.getFields().toArray(new Field[AOI.getFields().size()]);
	// }
	// Factory create = getJooqFactory();
	//
	// Result<?> result =
	// create
	// .select(fields)
	// .from(AOI)
	// .join(AOI_HIERARCHY)
	// .on(AOI.AOI_HIERARCHY_ID.eq(AOI_HIERARCHY.AOI_HIERARCHY_ID))
	// .where(AOI_HIERARCHY.AOI_HIERARCHY_NAME.eq(hierarchyName))
	// .fetch();
	//
	// return stream( result );
	// }

}

@@ -1819,36 +1819,40 @@
 ile 
-importing estimation results
+saving data into area fact table
 %22, e
@@ -1965,595 +1965,10 @@
 %09%7D%0A%0A
+
 %09%7D%0A%0A
-%09// public FlatDataStream streamByHierarchyName(String%5B%5D fieldNames, String hierarchyName) %7B%0A%09// Field%3C?%3E%5B%5D fields = getFields(fieldNames);%0A%09// if(fields == null %7C%7C fields.length == 0) %7B%0A%09// fields = AOI.getFields().toArray(new Field%5BAOI.getFields().size()%5D);%0A%09// %7D%0A%09// Factory create = getJooqFactory();%0A%09//%0A%09// Result%3C?%3E result =%0A%09// create%0A%09// .select(fields)%0A%09// .from(AOI)%0A%09// .join(AOI_HIERARCHY)%0A%09// .on(AOI.AOI_HIERARCHY_ID.eq(AOI_HIERARCHY.AOI_HIERARCHY_ID))%0A%09// .where(AOI_HIERARCHY.AOI_HIERARCHY_NAME.eq(hierarchyName))%0A%09// .fetch();%0A%09//%0A%09// return stream( result );%0A%09// %7D%0A%0A
 %7D%0A

Refactor of transformations