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