d42f08d8d06b6829917ff5afebcc85ff6be40c31
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / derivate / searchFilter / DerivateSearchCompositeController.java
1 // $Id$
2 /**
3 * Copyright (C) 2013 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.editor.view.derivate.searchFilter;
11
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.List;
15 import java.util.UUID;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Event;
20 import org.eclipse.swt.widgets.Listener;
21 import org.eclipse.swt.widgets.Widget;
22
23 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
24 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
25 import eu.etaxonomy.cdm.api.service.config.FindOccurrencesConfigurator;
26 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
27 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
28 import eu.etaxonomy.cdm.model.taxon.Taxon;
29 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
30 import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
31 import eu.etaxonomy.taxeditor.store.CdmStore;
32 import eu.etaxonomy.taxeditor.store.SearchManager;
33 import eu.etaxonomy.taxeditor.ui.dialog.selection.SelectionDialogFactory;
34 import eu.etaxonomy.taxeditor.ui.mvc.combo.EnumTermComboController;
35 import eu.etaxonomy.taxeditor.view.derivateSearch.DerivateLabelProvider;
36
37 /**
38 * Controller class for the {@link DerivateSearchComposite}.<br>
39 * This controller contains the logic to search the data base for {@link SpecimenOrObservationBase}s
40 * and handles GUI events.
41 *
42 */
43 public class DerivateSearchCompositeController implements Listener{
44
45 private final DerivateSearchComposite derivateSearchComposite;
46 private EnumTermComboController<SpecimenOrObservationType> comboSpecimenTypeController;
47 private DerivateLabelProvider labelProvider;
48 private Taxon selectedTaxon;
49 private DerivateView derivativeEditor;
50
51 public DerivateSearchCompositeController(Composite parent, DerivateView derivativeEditor) {
52 this.derivateSearchComposite = new DerivateSearchComposite(parent, SWT.NONE);
53 this.derivativeEditor = derivativeEditor;
54 init();
55 }
56
57 private void init(){
58 //derivate type combo
59 comboSpecimenTypeController = new EnumTermComboController<SpecimenOrObservationType>(derivateSearchComposite.getComboDerivateType(), null, null, SpecimenOrObservationType.class, 0);
60 comboSpecimenTypeController.setSelection(SpecimenOrObservationType.Unknown);
61
62 //add listeners
63 derivateSearchComposite.getSearchField().addListener(SWT.KeyDown, this);
64 derivateSearchComposite.getButtonSearch().addListener(SWT.Selection, this);
65 derivateSearchComposite.getBtnBrowseTaxa().addListener(SWT.Selection, this);
66 derivateSearchComposite.getBtnClearTaxon().addListener(SWT.Selection, this);
67
68 }
69
70 private void searchDerivates(){
71 // if(!conversation.isBound()){
72 // conversation.bind();
73 // }
74
75 String queryString = derivateSearchComposite.getSearchField().getText();
76 SpecimenOrObservationType specimenType = comboSpecimenTypeController.getSelection();
77
78 FindOccurrencesConfigurator config = new FindOccurrencesConfigurator();
79 config.setAssociatedTaxonUuid(selectedTaxon!=null?selectedTaxon.getUuid():null);
80 config.setSpecimenType(specimenType.equals(SpecimenOrObservationType.Unknown)?null:specimenType);//unknown := all types
81 config.setTitleSearchString(queryString);
82 if(selectedTaxon!=null){
83 config.setRetrieveIndirectlyAssociatedSpecimens(true);
84 config.setSpecimenType(null);
85 }
86
87 SearchManager searchManager = new SearchManager();
88 List<SpecimenOrObservationBase> occurrences = searchManager.findOccurrences(config, true);
89
90 //filter out specimens of wrong type (only if an associated taxon was selected)
91 if(selectedTaxon!=null && !specimenType.equals(SpecimenOrObservationType.Unknown)){
92 List<SpecimenOrObservationBase> specimensOfCorrectType = new ArrayList<SpecimenOrObservationBase>();
93 for(SpecimenOrObservationBase<?> occurrence:occurrences){
94 if(occurrence.getRecordBasis().equals(specimenType)
95 || occurrence.getRecordBasis().isKindOf(specimenType)){
96 specimensOfCorrectType.add(occurrence);
97 }
98 }
99 occurrences = specimensOfCorrectType;
100 }
101
102 //filter out (un-)assigned specimens
103 int selectionIndex = derivateSearchComposite.getComboTaxonAssignment().getSelectionIndex();
104 List<SpecimenOrObservationBase<?>> specimenWithAssociations = new ArrayList<SpecimenOrObservationBase<?>>();
105 if(selectionIndex!=DerivateSearchComposite.ALL_SPECIMENS){
106 for (SpecimenOrObservationBase specimenOrObservationBase : occurrences) {
107 Collection<TaxonBase<?>> associatedTaxa = CdmStore.getService(IOccurrenceService.class).listAssociatedTaxa(specimenOrObservationBase, null, null, null, null);
108 if(!associatedTaxa.isEmpty()){
109 specimenWithAssociations.add(specimenOrObservationBase);
110 }
111 }
112 }
113 if(selectionIndex==DerivateSearchComposite.UNASSIGNED_SPECIMENS){
114 for (SpecimenOrObservationBase<?> specimenOrObservationBase : specimenWithAssociations) {
115 derivativeEditor.removeHierarchy(specimenOrObservationBase);
116 }
117 derivativeEditor.updateRootEntities();
118 }
119 else{
120 if(selectionIndex==DerivateSearchComposite.ASSIGNED_SPECIMENS){
121 occurrences = new ArrayList<SpecimenOrObservationBase>(specimenWithAssociations);
122 }
123 List<UUID> derivateUuids = new ArrayList<UUID>();
124 for (SpecimenOrObservationBase specimenOrObservationBase : occurrences) {
125 derivateUuids.add(specimenOrObservationBase.getUuid());
126 }
127 derivativeEditor.updateRootEntities(derivateUuids);
128 }
129 }
130
131 @Override
132 public void handleEvent(Event event) {
133 Widget eventSource = event.widget;
134 if(eventSource == derivateSearchComposite.getButtonSearch()
135 || (eventSource == derivateSearchComposite.getSearchField() && event.keyCode == SWT.CR)){
136 searchDerivates();
137 }
138 else if(eventSource==derivateSearchComposite.getBtnBrowseTaxa()){
139 selectedTaxon = SelectionDialogFactory.getSelectionFromDialog(Taxon.class, this.derivateSearchComposite.getShell(), derivativeEditor.getConversationHolder(), null);
140 if(selectedTaxon!=null){
141 derivateSearchComposite.getTextTaxonName().setText(selectedTaxon.getTitleCache());
142 }
143 }
144 else if(eventSource==derivateSearchComposite.getBtnClearTaxon()){
145 selectedTaxon = null;
146 derivateSearchComposite.getTextTaxonName().setText("");
147 }
148 }
149
150 /**
151 * Sets enable status of all input fields
152 * @param enabled
153 */
154 public void setEnabled(boolean enabled) {
155 if(!derivateSearchComposite.isDisposed()){
156 derivateSearchComposite.setEnabled(enabled);
157 }
158 }
159
160 public void setLayoutData(Object layoutData){
161 derivateSearchComposite.setLayoutData(layoutData);
162 }
163
164 /**
165 * Resets all input fields
166 */
167 public void reset(){
168 if(!derivateSearchComposite.isDisposed()){
169 derivateSearchComposite.getSearchField().setText("");
170 derivateSearchComposite.getComboDerivateType().deselectAll();
171 comboSpecimenTypeController.setSelection(SpecimenOrObservationType.Unknown);
172 }
173 selectedTaxon = null;
174 }
175
176 public void setConversation(ConversationHolder conversationHolder) {
177 labelProvider.setConversation(conversationHolder);
178 }
179
180 }