commit 4f7ccb8756f21eed80db5ead22051239b2af89d4
Author: Patrick Plitzner <p.plitzner@bgbm.org>
Date:   Tue Oct 11 10:46:31 2016 +0200

    ref #5458 Implement multi select for NamedArea selection

diff --git a/src/main/java/eu/etaxonomy/cdm/vaadin/container/NamedAreaContainer.java b/src/main/java/eu/etaxonomy/cdm/vaadin/container/NamedAreaContainer.java
new file mode 100644
index 00000000..9306cafc
--- /dev/null
+++ b/src/main/java/eu/etaxonomy/cdm/vaadin/container/NamedAreaContainer.java
@@ -0,0 +1,28 @@
+package eu.etaxonomy.cdm.vaadin.container;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import com.vaadin.data.util.BeanItemContainer;
+
+import eu.etaxonomy.cdm.model.common.TermVocabulary;
+import eu.etaxonomy.cdm.model.location.NamedArea;
+
+public class NamedAreaContainer extends BeanItemContainer<NamedArea> {
+
+    private static final long serialVersionUID = 9150424479597481361L;
+
+	public static final Map<String, NamedArea> titleToNamedAreaMap = new HashMap<>();
+
+	public NamedAreaContainer(TermVocabulary<NamedArea> vocabulary)
+	        throws IllegalArgumentException {
+	    super(NamedArea.class);
+	    Set<NamedArea> namedAreas = vocabulary.getTerms();
+	    for (NamedArea namedArea: namedAreas) {
+	        titleToNamedAreaMap.put(namedArea.getTitleCache(), namedArea);
+	        addBean(namedArea);
+	    }
+	}
+
+}
diff --git a/src/main/java/eu/etaxonomy/cdm/vaadin/model/LazyLoadedContainer.java b/src/main/java/eu/etaxonomy/cdm/vaadin/model/LazyLoadedContainer.java
index 8f0d9587..ed01518d 100644
--- a/src/main/java/eu/etaxonomy/cdm/vaadin/model/LazyLoadedContainer.java
+++ b/src/main/java/eu/etaxonomy/cdm/vaadin/model/LazyLoadedContainer.java
@@ -9,6 +9,7 @@ import com.vaadin.data.util.BeanItem;
 import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
+import eu.etaxonomy.cdm.vaadin.presenter.dbstatus.DistributionTablePresenter;
 import eu.etaxonomy.cdm.vaadin.view.dbstatus.IDistributionTableComponent;
 
 public class LazyLoadedContainer extends BeanContainer implements Serializable, IDistributionTableComponent {
@@ -18,34 +19,34 @@ public class LazyLoadedContainer extends BeanContainer implements Serializable,
 
 	DistributionTableComponentListener listener;
 
-	
+
 	public LazyLoadedContainer(Class type) throws IllegalArgumentException {
 		super(type);
 	}
-//	
+//
 //	public LazyLoadedContainer(Class type, IClassificationService classificationService, ITaxonNodeService taxonNodeService, IDescriptionService descriptionService) throws IllegalArgumentException {
 //		super(type);
 //		this.classificationService = classificationService;
 //		this.taxonNodeService = taxonNodeService;
 //		this.descriptionService = descriptionService;
-//		
+//
 //	}
-	
+
 	@Override
 	public int size(){
 		return listener.getSizeOfTaxonNode();
 	}
-	
+
 	@Override
 	public BeanItem getItem(Object itemId){
 		TaxonNode taxonNode = ((TaxonNode) itemId);
 		CdmBase.deproxy(taxonNode, TaxonNode.class);
-		Taxon taxon = (Taxon)taxonNode.getTaxon();
+		Taxon taxon = taxonNode.getTaxon();
 		CdmBase.deproxy(taxon, Taxon.class);
 		CdmTaxonTableCollection cttc = new CdmTaxonTableCollection(taxon);
 		return new BeanItem(cttc);
 	}
-	
+
 	@Override
 	public List getItemIds(){
 		List<TaxonNode> listAllNodesForClassification = listener.getAllNodes();
@@ -55,10 +56,10 @@ public class LazyLoadedContainer extends BeanContainer implements Serializable,
 
 
 	@Override
-	public void addListener(DistributionTableComponentListener listener) {
+	public void addListener(DistributionTablePresenter listener) {
 		this.listener = listener;
 	}
 
-	
+
 
 }
\ No newline at end of file
diff --git a/src/main/java/eu/etaxonomy/cdm/vaadin/presenter/dbstatus/DistributionTablePresenter.java b/src/main/java/eu/etaxonomy/cdm/vaadin/presenter/dbstatus/DistributionTablePresenter.java
index c4286125..5b2d8e4a 100644
--- a/src/main/java/eu/etaxonomy/cdm/vaadin/presenter/dbstatus/DistributionTablePresenter.java
+++ b/src/main/java/eu/etaxonomy/cdm/vaadin/presenter/dbstatus/DistributionTablePresenter.java
@@ -20,6 +20,7 @@ import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
 import eu.etaxonomy.cdm.api.service.ITaxonService;
 import eu.etaxonomy.cdm.api.service.ITermService;
 import eu.etaxonomy.cdm.api.service.IVocabularyService;
+import eu.etaxonomy.cdm.common.CdmUtils;
 import eu.etaxonomy.cdm.model.common.CdmBase;
 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
 import eu.etaxonomy.cdm.model.common.Language;
@@ -147,9 +148,12 @@ public class DistributionTablePresenter implements IDistributionTableComponent.D
 //      Collections.sort(list);
         return list;
     }
-    
-    private List<String> getNamedAreas(){
+
+    public List<String> getNamedAreas(){
     	String selectedAreas = (String) VaadinSession.getCurrent().getAttribute("selectedAreas");
+    	if(CdmUtils.isBlank(selectedAreas)){
+    	    return getTermList();
+    	}
     	return Arrays.asList(selectedAreas.split(","));
     }
 
@@ -227,9 +231,8 @@ public class DistributionTablePresenter implements IDistributionTableComponent.D
 		for (TaxonNode taxonNode : getAllNodes()) {
 			nodeIds.add(taxonNode.getId());
 		}
-		List<String> termList = getTermList();
 		List<String> namesAreaUuids = getNamedAreas();
-		CdmSQLContainer container = new CdmSQLContainer(CdmQueryFactory.generateTaxonDistributionQuery(termList, nodeIds, namesAreaUuids));
+		CdmSQLContainer container = new CdmSQLContainer(CdmQueryFactory.generateTaxonDistributionQuery(nodeIds, namesAreaUuids));
 		return container;
 	}
 
diff --git a/src/main/java/eu/etaxonomy/cdm/vaadin/util/CdmQueryFactory.java b/src/main/java/eu/etaxonomy/cdm/vaadin/util/CdmQueryFactory.java
index bb21f586..0c2362cc 100644
--- a/src/main/java/eu/etaxonomy/cdm/vaadin/util/CdmQueryFactory.java
+++ b/src/main/java/eu/etaxonomy/cdm/vaadin/util/CdmQueryFactory.java
@@ -59,7 +59,7 @@ public class CdmQueryFactory {
         return generateQueryDelegate(SELECT_QUERY, COUNT_QUERY, CONTAINS_QUERY);
     }
 
-    public static QueryDelegate generateTaxonDistributionQuery(List<String> termList, List<Integer> taxonNodeIds, List<String> namesAreaUuids) throws SQLException {
+    public static QueryDelegate generateTaxonDistributionQuery(List<Integer> taxonNodeIds, List<String> namedAreas) throws SQLException {
 
     	String idString = "";
     	Iterator<Integer> nodeIterator = taxonNodeIds.iterator();
@@ -93,8 +93,8 @@ public class CdmQueryFactory {
         		"tb.titleCache AS "+TAXON_COLUMN+", " +
         		"rank.titleCache AS "+RANK_COLUMN+", ";
 
-        for(String term : termList){
-        		SELECT_QUERY += "MAX( IF(area.titleCache = '"+ term +"', statusTerm.titleCache, NULL) ) as '"+ term +"'," ;
+        for(String namedArea : namedAreas){
+            SELECT_QUERY += "MAX( IF(area.titleCache = '"+ namedArea +"', statusTerm.titleCache, NULL) ) as '"+ namedArea +"'," ;
         }
         SELECT_QUERY = StringUtils.stripEnd(SELECT_QUERY, ",")+" ";
         SELECT_QUERY= SELECT_QUERY + FROM_QUERY + GROUP_BY + ORDER_BY;
diff --git a/src/main/java/eu/etaxonomy/cdm/vaadin/util/DistributionEditorUtil.java b/src/main/java/eu/etaxonomy/cdm/vaadin/util/DistributionEditorUtil.java
index e964964f..d54de3fa 100644
--- a/src/main/java/eu/etaxonomy/cdm/vaadin/util/DistributionEditorUtil.java
+++ b/src/main/java/eu/etaxonomy/cdm/vaadin/util/DistributionEditorUtil.java
@@ -2,6 +2,8 @@ package eu.etaxonomy.cdm.vaadin.util;
 
 import java.util.Set;
 
+import org.apache.commons.lang.StringUtils;
+
 import com.vaadin.server.VaadinSession;
 import com.vaadin.ui.Notification;
 import com.vaadin.ui.UI;
@@ -26,10 +28,11 @@ public class DistributionEditorUtil {
 	    VaadinSession.getCurrent().setAttribute("selectedTerm", term.getUuid());
 	    String selectedAreaUuids = "";
 	    for (NamedArea namedArea : selectedAreas) {
-			selectedAreaUuids += namedArea.getUuid().toString()+",";
+			selectedAreaUuids += namedArea.getTitleCache()+",";
 		}
+	    selectedAreaUuids = StringUtils.stripEnd(selectedAreaUuids, ",");
 	    VaadinSession.getCurrent().setAttribute("selectedAreas", selectedAreaUuids);
-	
+
 	    //navigate to table view
 	    UI.getCurrent().getNavigator().navigateTo("table");
 	}
diff --git a/src/main/java/eu/etaxonomy/cdm/vaadin/view/dbstatus/DistributionSelectionView.java b/src/main/java/eu/etaxonomy/cdm/vaadin/view/dbstatus/DistributionSelectionView.java
index 2692435f..af9baef8 100644
--- a/src/main/java/eu/etaxonomy/cdm/vaadin/view/dbstatus/DistributionSelectionView.java
+++ b/src/main/java/eu/etaxonomy/cdm/vaadin/view/dbstatus/DistributionSelectionView.java
@@ -27,6 +27,7 @@ import eu.etaxonomy.cdm.model.common.DefinedTermBase;
 import eu.etaxonomy.cdm.model.common.TermVocabulary;
 import eu.etaxonomy.cdm.model.location.NamedArea;
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
+import eu.etaxonomy.cdm.vaadin.container.NamedAreaContainer;
 import eu.etaxonomy.cdm.vaadin.container.TaxonNodeContainer;
 
 public class DistributionSelectionView extends CustomComponent implements IDistributionSelectionComponent, View, ClickListener{
@@ -98,17 +99,17 @@ public class DistributionSelectionView extends CustomComponent implements IDistr
 			}
 		});
         taxonTree.setItemCaptionPropertyId(TaxonNodeContainer.LABEL);
-        
+
         distributionAreaBox.addValueChangeListener(new ValueChangeListener() {
-			
+
 			@Override
 			public void valueChange(ValueChangeEvent event) {
-				TermVocabulary<NamedArea> voc = (TermVocabulary<NamedArea>) event.getProperty().getValue();
-				IndexedContainer indexedContainer = new IndexedContainer(voc.getTerms());
-				namedAreaList.setContainerDataSource(indexedContainer);
+				TermVocabulary<NamedArea> vocabulary = (TermVocabulary<NamedArea>) event.getProperty().getValue();
+				NamedAreaContainer container = new NamedAreaContainer(vocabulary);
+				namedAreaList.setContainerDataSource(container);
 			}
 		});
-        
+
 		List<TermVocabulary<DefinedTermBase>> namedAreaList = distListener.getNamedAreaList();
 		Container d = new IndexedContainer(namedAreaList);
 		distributionAreaBox.setContainerDataSource(d);
diff --git a/src/main/java/eu/etaxonomy/cdm/vaadin/view/dbstatus/DistributionTableView.java b/src/main/java/eu/etaxonomy/cdm/vaadin/view/dbstatus/DistributionTableView.java
index 58366f37..0a13bfbf 100644
--- a/src/main/java/eu/etaxonomy/cdm/vaadin/view/dbstatus/DistributionTableView.java
+++ b/src/main/java/eu/etaxonomy/cdm/vaadin/view/dbstatus/DistributionTableView.java
@@ -35,6 +35,7 @@ import eu.etaxonomy.cdm.vaadin.component.DetailWindow;
 import eu.etaxonomy.cdm.vaadin.component.HorizontalToolbar;
 import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
 import eu.etaxonomy.cdm.vaadin.container.PresenceAbsenceTermContainer;
+import eu.etaxonomy.cdm.vaadin.presenter.dbstatus.DistributionTablePresenter;
 import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
 
 public class DistributionTableView extends CustomComponent implements IDistributionTableComponent, View{
@@ -72,7 +73,7 @@ public class DistributionTableView extends CustomComponent implements IDistribut
     private HorizontalToolbar toolbar;
 	private Table table;
 
-	private DistributionTableComponentListener listener;
+	private DistributionTablePresenter listener;
 
 	private List<String> columnList;
 	private ArrayList<String> headerList;
@@ -96,14 +97,14 @@ public class DistributionTableView extends CustomComponent implements IDistribut
 		mainLayout.setImmediate(false);
 		mainLayout.setWidth("100%");
 		mainLayout.setHeight("100%");
-		
+
 		setWidth("100.0%");
 		setHeight("100.0%");
 
 		//Horizontal Toolbar
 		toolbar = new HorizontalToolbar();
 		mainLayout.addComponent(toolbar, "top:0.0px;right:0.0px;");
-		
+
 		// table
 		table = new Table();
 		table.setImmediate(false);
@@ -114,7 +115,7 @@ public class DistributionTableView extends CustomComponent implements IDistribut
 	}
 
 	@Override
-	public void addListener(DistributionTableComponentListener listener) {
+	public void addListener(DistributionTablePresenter listener) {
 	   this.listener = listener;
 	}
 
@@ -134,7 +135,7 @@ public class DistributionTableView extends CustomComponent implements IDistribut
 		table.setSortEnabled(true);
 
 		columnList = new ArrayList<String>(Arrays.asList(new String[]{CdmQueryFactory.TAXON_COLUMN,CdmQueryFactory.RANK_COLUMN}));
-		List<String> termList = listener.getTermList();
+		List<String> termList = listener.getNamedAreas();
 		columnList.addAll(termList);
 		Object[] visibleColumns = columnList.toArray();
 		table.setVisibleColumns(visibleColumns);
diff --git a/src/main/java/eu/etaxonomy/cdm/vaadin/view/dbstatus/IDistributionTableComponent.java b/src/main/java/eu/etaxonomy/cdm/vaadin/view/dbstatus/IDistributionTableComponent.java
index 7250153d..bae8702e 100644
--- a/src/main/java/eu/etaxonomy/cdm/vaadin/view/dbstatus/IDistributionTableComponent.java
+++ b/src/main/java/eu/etaxonomy/cdm/vaadin/view/dbstatus/IDistributionTableComponent.java
@@ -19,6 +19,7 @@ import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
 import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
 import eu.etaxonomy.cdm.vaadin.model.LazyLoadedContainer;
+import eu.etaxonomy.cdm.vaadin.presenter.dbstatus.DistributionTablePresenter;
 
 public interface IDistributionTableComponent {
 
@@ -70,5 +71,5 @@ public interface IDistributionTableComponent {
         int updateDistributionField(String distributionArea, Object comboValue, Taxon taxon);
 
 	}
-	public void addListener(DistributionTableComponentListener listener);
+	public void addListener(DistributionTablePresenter listener);
 }
