package org.openforis.collect.io.metadata;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

import org.apache.commons.io.IOUtils;
import org.openforis.collect.model.CollectSurvey;
import org.openforis.commons.io.csv.CsvWriter;
import org.openforis.concurrency.Job;
import org.openforis.concurrency.Task;
import org.openforis.idm.metamodel.EntityDefinition;
import org.openforis.idm.metamodel.NodeDefinition;
import org.openforis.idm.metamodel.NodeDefinitionVisitor;
import org.openforis.idm.metamodel.NodeLabel.Type;
import org.openforis.idm.metamodel.Schema;

/**
 * 
 * @author S. Ricci
 *
 */
public class SchemaSummaryCSVExportJob extends Job {

	private CollectSurvey survey;
	private File outputFile;
	
	@Override
	protected void createInternalVariables() throws Throwable {
		super.createInternalVariables();
		outputFile = File.createTempFile("collect_schema_export", ".csv");
	}
	
	@Override
	protected void buildTasks() throws Throwable {
		Task task = new Task() {
			@Override
			protected void execute() throws Throwable {
				FileOutputStream out = new FileOutputStream(outputFile);
				final CsvWriter csvWriter = new CsvWriter(new BufferedWriter(new OutputStreamWriter(out)), ',', '"');
				try {
					csvWriter.writeHeaders(new String[] {"id", "path", "type", "label"});
					
					Schema schema = survey.getSchema();
					schema.traverse(new NodeDefinitionVisitor() {
						@Override
						public void visit(NodeDefinition nodeDefn) {
							csvWriter.writeNext(new String[] {
									Integer.toString(nodeDefn.getId()), 
									nodeDefn.getPath(),
									nodeDefn instanceof EntityDefinition ? "entity": "attribute",
									nodeDefn.getLabel(Type.INSTANCE)
								});
						}
					});
				} finally {
					IOUtils.closeQuietly(csvWriter);
				}
			}
		};
		addTask(task);
	}
	
	public void setSurvey(CollectSurvey survey) {
		this.survey = survey;
	}
	
	public File getOutputFile() {
		return outputFile;
	}
	
}
@@ -204,16 +204,76 @@
 Utils;%0D%0A
+import org.openforis.collect.designer.model.AttributeType;%0D%0A
 import o
@@ -442,16 +442,73 @@
 .Task;%0D%0A
+import org.openforis.idm.metamodel.AttributeDefinition;%0D%0A
 import o
@@ -1501,16 +1501,34 @@
  %22type%22,
+ %22attribute_type%22,
  %22label%22
@@ -1894,16 +1894,138 @@
 bute%22,%0D%0A
+%09%09%09%09%09%09%09%09%09nodeDefn instanceof AttributeDefinition ? AttributeType.valueOf((AttributeDefinition) nodeDefn).getLabel(): %22%22,%0D%0A
 %09%09%09%09%09%09%09%09

Include attribute type in survey schema summary export