dc954df6344194aaafa69bc3acc2642fd423368e
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / derivateSearch / 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.view.derivateSearch;
11
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import org.eclipse.core.commands.ExecutionException;
16 import org.eclipse.core.commands.NotEnabledException;
17 import org.eclipse.core.commands.NotHandledException;
18 import org.eclipse.core.commands.common.NotDefinedException;
19 import org.eclipse.jface.viewers.ArrayContentProvider;
20 import org.eclipse.jface.viewers.DoubleClickEvent;
21 import org.eclipse.jface.viewers.IDoubleClickListener;
22 import org.eclipse.jface.viewers.TableViewer;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Event;
26 import org.eclipse.swt.widgets.Listener;
27 import org.eclipse.swt.widgets.Widget;
28 import org.eclipse.ui.handlers.IHandlerService;
29
30 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
31 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
32 import eu.etaxonomy.cdm.api.service.config.FindOccurrencesConfigurator;
33 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
34 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
35 import eu.etaxonomy.cdm.model.taxon.Taxon;
36 import eu.etaxonomy.taxeditor.model.AbstractUtility;
37 import eu.etaxonomy.taxeditor.model.MessagingUtils;
38 import eu.etaxonomy.taxeditor.store.CdmStore;
39 import eu.etaxonomy.taxeditor.store.SearchManager;
40 import eu.etaxonomy.taxeditor.ui.dialog.selection.SelectionDialogFactory;
41 import eu.etaxonomy.taxeditor.ui.mvc.combo.EnumTermComboController;
42
43 /**
44 * Controller class for the {@link DerivateSearchComposite}.<br>
45 * This controller contains the logic to search the data base for {@link SpecimenOrObservationBase}s
46 * and handles GUI events.
47 *
48 */
49 public class DerivateSearchCompositeController implements Listener{
50
51 private final DerivateSearchComposite derivateSearchComposite;
52 private EnumTermComboController<SpecimenOrObservationType> comboSpecimenTypeController;
53 private final DerivateSearchView derivateSearchView;
54 private DerivateLabelProvider labelProvider;
55 private Taxon selectedTaxon;
56
57 /**
58 * @param parent
59 * @param derivateSearchView
60 */
61 public DerivateSearchCompositeController(Composite parent, DerivateSearchView derivateSearchView) {
62 this.derivateSearchComposite = new DerivateSearchComposite(parent, SWT.NONE);
63 this.derivateSearchView = derivateSearchView;
64 init();
65 }
66
67 private void init(){
68 //derivate type combo
69 comboSpecimenTypeController = new EnumTermComboController<SpecimenOrObservationType>(derivateSearchComposite.getComboDerivateType(), null, null, SpecimenOrObservationType.class, 0);
70 comboSpecimenTypeController.setSelection(SpecimenOrObservationType.Unknown);
71
72 //add listeners
73 derivateSearchComposite.getSearchField().addListener(SWT.KeyDown, this);
74 derivateSearchComposite.getButtonSearch().addListener(SWT.Selection, this);
75 derivateSearchComposite.getBtnBrowseTaxa().addListener(SWT.Selection, this);
76 derivateSearchComposite.getBtnClearTaxon().addListener(SWT.Selection, this);
77
78
79 //result table
80 TableViewer resultViewer = derivateSearchComposite.getResultViewer();
81 resultViewer.setContentProvider(new ArrayContentProvider());
82 labelProvider = new DerivateLabelProvider();
83 resultViewer.setLabelProvider(labelProvider);
84 resultViewer.addDoubleClickListener(new IDoubleClickListener() {
85 @Override
86 public void doubleClick(DoubleClickEvent event) {
87 String commandId = "eu.etaxonomy.taxeditor.editor.handler.openDerivateView";
88
89 IHandlerService handlerService = (IHandlerService) AbstractUtility.getService(IHandlerService.class);
90 try {
91 handlerService.executeCommand(commandId, null);
92 } catch (ExecutionException e) {
93 MessagingUtils.error(DerivateSearchCompositeController.class, e);
94 } catch (NotDefinedException e) {
95 MessagingUtils.error(DerivateSearchCompositeController.class, e);
96 } catch (NotEnabledException e) {
97 MessagingUtils.error(DerivateSearchCompositeController.class, e);
98 } catch (NotHandledException e) {
99 MessagingUtils.error(DerivateSearchCompositeController.class, e);
100 }
101
102 }
103 });
104
105 }
106
107 private void searchDerivates(){
108 if(!derivateSearchView.getConversationHolder().isBound()){
109 derivateSearchView.setFocus(); //rebind the conversation
110 }
111
112 String queryString = derivateSearchComposite.getSearchField().getText();
113 SpecimenOrObservationType specimenType = comboSpecimenTypeController.getSelection();
114
115 FindOccurrencesConfigurator config = new FindOccurrencesConfigurator();
116 config.setAssociatedTaxonUuid(selectedTaxon!=null?selectedTaxon.getUuid():null);
117 config.setSpecimenType(specimenType.equals(SpecimenOrObservationType.Unknown)?null:specimenType);//unknown := all types
118 config.setTitleSearchString(queryString);
119 if(selectedTaxon!=null){
120 config.setRetrieveIndirectlyAssociatedSpecimens(true);
121 config.setSpecimenType(null);
122 }
123
124 SearchManager searchManager = new SearchManager();
125 List<SpecimenOrObservationBase> occurrences = searchManager.findOccurrences(config);
126
127 //filter out specimens of wrong type (only if an associated taxon was selected)
128 if(selectedTaxon!=null && !specimenType.equals(SpecimenOrObservationType.Unknown)){
129 List<SpecimenOrObservationBase> specimensOfCorrectType = new ArrayList<SpecimenOrObservationBase>();
130 for(SpecimenOrObservationBase<?> occurrence:occurrences){
131 if(occurrence.getRecordBasis().equals(specimenType)
132 || occurrence.getRecordBasis().isKindOf(specimenType)){
133 specimensOfCorrectType.add(occurrence);
134 }
135 }
136 occurrences = specimensOfCorrectType;
137 }
138
139 //filter out assigned specimens
140 if(derivateSearchComposite.getBtnFilterUndeterminedSpecimen().getSelection()){
141 List<SpecimenOrObservationBase> specimensWithNoDetermination = new ArrayList<SpecimenOrObservationBase>();
142 for(SpecimenOrObservationBase<?> result:occurrences){
143 if(CdmStore.getService(IOccurrenceService.class).countDeterminations(result, null)==0){
144 specimensWithNoDetermination.add(result);
145 }
146 }
147 occurrences = specimensWithNoDetermination;
148 }
149 derivateSearchComposite.getResultViewer().setInput(occurrences);
150 }
151
152 @Override
153 public void handleEvent(Event event) {
154 Widget eventSource = event.widget;
155 if(eventSource == derivateSearchComposite.getButtonSearch()
156 || (eventSource == derivateSearchComposite.getSearchField() && event.keyCode == SWT.CR)){
157 searchDerivates();
158 }
159 else if(eventSource==derivateSearchComposite.getBtnBrowseTaxa()){
160 selectedTaxon = SelectionDialogFactory.getSelectionFromDialog(Taxon.class, derivateSearchView.getSite().getShell(), derivateSearchView.getConversationHolder(), null);
161 if(selectedTaxon!=null){
162 derivateSearchComposite.getTextTaxonName().setText(selectedTaxon.getTitleCache());
163 }
164 }
165 else if(eventSource==derivateSearchComposite.getBtnClearTaxon()){
166 selectedTaxon = null;
167 derivateSearchComposite.getTextTaxonName().setText("");
168 }
169 }
170
171 /**
172 * Sets enable status of all input fields
173 * @param enabled
174 */
175 public void setEnabled(boolean enabled) {
176 if(!derivateSearchComposite.isDisposed()){
177 derivateSearchComposite.setEnabled(enabled);
178 }
179 }
180
181 /**
182 * Resets all input fields
183 */
184 public void reset(){
185 if(!derivateSearchComposite.isDisposed()){
186 derivateSearchComposite.getResultViewer().setInput(null);
187 derivateSearchComposite.getSearchField().setText("");
188 derivateSearchComposite.getComboDerivateType().deselectAll();
189 comboSpecimenTypeController.setSelection(SpecimenOrObservationType.Unknown);
190 }
191 selectedTaxon = null;
192 }
193
194 public TableViewer getResultViewer() {
195 return derivateSearchComposite.getResultViewer();
196 }
197
198 public void setFocus() {
199 derivateSearchComposite.getResultViewer().getControl().setFocus();
200 }
201
202 /**
203 * @param conversationHolder
204 */
205 public void setConversation(ConversationHolder conversationHolder) {
206 labelProvider.setConversation(conversationHolder);
207 }
208
209 }