/**
 * 
 */
package org.openforis.collect.io.data.csv;

import org.apache.commons.lang3.ArrayUtils;
import org.openforis.collect.metamodel.CollectAnnotations;
import org.openforis.collect.model.CollectSurvey;
import org.openforis.idm.metamodel.TaxonAttributeDefinition;

/**
 * @author M. Togna
 * @author S. Ricci
 * 
 */
public class TaxonColumnProvider extends CompositeAttributeColumnProvider<TaxonAttributeDefinition> {

	private static final String[] DEFAULT_FIELDS = new String[] {
			TaxonAttributeDefinition.CODE_FIELD_NAME,
			TaxonAttributeDefinition.SCIENTIFIC_NAME_FIELD_NAME,
			TaxonAttributeDefinition.VERNACULAR_NAME_FIELD_NAME,
			TaxonAttributeDefinition.LANGUAGE_CODE_FIELD_NAME,
			TaxonAttributeDefinition.LANGUAGE_VARIETY_FIELD_NAME
	};
	
	private static final String[] SHOW_FAMILY_FIELDS = ArrayUtils.addAll(DEFAULT_FIELDS, new String[] {
			TaxonAttributeDefinition.FAMILY_CODE_FIELD_NAME,
			TaxonAttributeDefinition.FAMILY_SCIENTIFIC_NAME_FIELD_NAME
	});

	public TaxonColumnProvider(CSVExportConfiguration config, TaxonAttributeDefinition defn) {
		super(config, defn);
	}

	@Override
	protected String[] getFieldNames() {
		CollectSurvey survey = attributeDefinition.getSurvey();
		CollectAnnotations annotations = survey.getAnnotations();
		boolean showFamily = annotations.isShowFamily(attributeDefinition);
		return showFamily ? SHOW_FAMILY_FIELDS : DEFAULT_FIELDS;
	}

}

@@ -54,16 +54,97 @@
 csv;%0D%0A%0D%0A
+import java.util.ArrayList;%0D%0Aimport java.util.Arrays;%0D%0Aimport java.util.List;%0D%0A%0D%0A
 import o
@@ -892,21 +892,16 @@
 tring%5B%5D 
-SHOW_
 FAMILY_F
@@ -911,42 +911,8 @@
 DS =
- ArrayUtils.addAll(DEFAULT_FIELDS,
  new
@@ -1042,17 +1042,16 @@
 NAME%0D%0A%09%7D
-)
 ;%0D%0A%0D%0A%09pu
@@ -1347,142 +1347,553 @@
 %0D%0A%09%09
-boolean showFamily = annotations.isShowFamily(attributeDefinition);%0D%0A%09%09return showFamily ? SHOW_FAMILY_FIELDS : DEFAULT_FIELDS
+List%3CString%3E visibleFields = new ArrayList%3CString%3E(Arrays.asList(%0D%0A%09%09%09%09survey.getUIOptions().getVisibleFields(attributeDefinition)));%0D%0A%09%09boolean showFamily = annotations.isShowFamily(attributeDefinition);%0D%0A%09%09if (! visibleFields.contains(TaxonAttributeDefinition.CODE_FIELD_NAME)) %7B%0D%0A%09%09%09//always include CODE field%0D%0A%09%09%09visibleFields.add(0, TaxonAttributeDefinition.CODE_FIELD_NAME);%0D%0A%09%09%7D%0D%0A%09%09if (! showFamily) %7B%0D%0A%09%09%09visibleFields.removeAll(Arrays.asList(FAMILY_FIELDS));%0D%0A%09%09%7D%0D%0A%09%09return visibleFields.toArray(new String%5BvisibleFields.size()%5D)
 ;%0D%0A%09%7D%0D%0A
-%0D%0A
 %7D%0D%0A

Include only visible fields in data export for Taxon attribute