3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.store
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Arrays
;
15 import java
.util
.List
;
17 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
18 import org
.eclipse
.swt
.widgets
.Display
;
20 import eu
.etaxonomy
.cdm
.api
.service
.IAgentService
;
21 import eu
.etaxonomy
.cdm
.api
.service
.IGroupService
;
22 import eu
.etaxonomy
.cdm
.api
.service
.INameService
;
23 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
24 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
25 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
26 import eu
.etaxonomy
.cdm
.api
.service
.IUserService
;
27 import eu
.etaxonomy
.cdm
.api
.service
.config
.IFindTaxaAndNamesConfigurator
;
28 import eu
.etaxonomy
.cdm
.api
.service
.config
.IIdentifiableEntityServiceConfigurator
;
29 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
30 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
31 import eu
.etaxonomy
.cdm
.model
.common
.Group
;
32 import eu
.etaxonomy
.cdm
.model
.common
.RelationshipBase
;
33 import eu
.etaxonomy
.cdm
.model
.common
.User
;
34 import eu
.etaxonomy
.cdm
.model
.common
.UuidAndTitleCache
;
35 import eu
.etaxonomy
.cdm
.model
.name
.NameRelationship
;
36 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
37 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
38 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
39 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
40 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
41 import eu
.etaxonomy
.taxeditor
.preference
.IPreferenceKeys
;
42 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
46 * @created Dec 8, 2010
49 public class SearchManager
{
51 public static final List NO_RESULTS
= Arrays
.asList(new Object
[]{});
53 public static final String WILDCARD
= "*";
55 public static int NO_COUNT
= -1;
57 // TODO make this configurable via preferences
58 private static final int MAX_RESULTS_BEFORE_WARNING
= 500;
63 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
64 * @return a {@link java.util.List} object.
66 public List
<TaxonNameBase
> findNames(IIdentifiableEntityServiceConfigurator configurator
){
67 if(checkLargeResult(CdmStore
.getService(INameService
.class).countByTitle(configurator
))){
68 return CdmStore
.getService(INameService
.class).findByTitle(configurator
).getRecords();
73 public List
<NameRelationship
> findNameRelationships(
74 IIdentifiableEntityServiceConfigurator configurator
) {
79 List
<NameRelationship
> relationships
= new ArrayList
<NameRelationship
>();
80 List
<RelationshipBase
> all
= CdmStore
.getService(INameService
.class).getAllRelationships(0, 0);
82 for (RelationshipBase relationship
: all
){
83 if(relationship
instanceof NameRelationship
){
84 relationships
.add((NameRelationship
) relationship
);
94 * <p>findTaxaAndNames</p>
96 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator} object.
97 * @return a {@link java.util.List} object.
99 public List
<UuidAndTitleCache
<TaxonBase
>> findTaxaAndNames(IFindTaxaAndNamesConfigurator
<TaxonBase
> configurator
){
100 return CdmStore
.getService(ITaxonService
.class).findTaxaAndNamesForEditor(configurator
);
105 * <p>findReferences</p>
107 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
108 * @return a {@link java.util.List} object.
110 public List
<Reference
> findReferences(IIdentifiableEntityServiceConfigurator configurator
){
111 if(checkLargeResult(CdmStore
.getService(IReferenceService
.class).countByTitle(configurator
))){
112 return CdmStore
.getService(IReferenceService
.class).findByTitle(configurator
).getRecords();
121 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
122 * @return a {@link java.util.List} object.
124 public List
<AgentBase
> findAgents(IIdentifiableEntityServiceConfigurator configurator
){
125 if(checkLargeResult(CdmStore
.getService(IAgentService
.class).countByTitle(configurator
))){
126 return CdmStore
.getService(IAgentService
.class).findByTitle(configurator
).getRecords();
132 * <p>findTeamOrPersons</p>
134 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
135 * @return a {@link java.util.List} object.
137 public List
<AgentBase
> findTeamOrPersons(IIdentifiableEntityServiceConfigurator configurator
){
138 configurator
.setClazz(TeamOrPersonBase
.class);
139 return findAgents(configurator
);
144 * <p>findOccurrences</p>
146 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
147 * @return a {@link java.util.List} object.
149 public List
<SpecimenOrObservationBase
> findOccurrences(IIdentifiableEntityServiceConfigurator configurator
){
150 // by default we do not show field units. This may be configured via preferences
151 boolean showFieldUnits
= PreferencesUtil
.getPreferenceStore().getBoolean(IPreferenceKeys
.BULK_EDITOR_OCCURRENCE_SHOW_FIELD_OBSERVATIONS
);
152 if(! showFieldUnits
){
153 configurator
.setClazz(DerivedUnit
.class);
155 if(checkLargeResult(CdmStore
.getService(IOccurrenceService
.class).countByTitle(configurator
))){
156 return CdmStore
.getService(IOccurrenceService
.class).findByTitle(configurator
).getRecords();
162 public List
<User
> findUsers(IIdentifiableEntityServiceConfigurator configurator
){
163 String userNameSearchString
= sqlizeTitleSearchString(configurator
);
164 // TODO why are users not identifiable entities?
165 return CdmStore
.getService(IUserService
.class).listByUsername(userNameSearchString
, null, null, null, null, null, null);
169 public List
<Group
> findGroups(IIdentifiableEntityServiceConfigurator configurator
){
170 String groupNameSearchString
= sqlizeTitleSearchString(configurator
);
171 // TODO why are groups not identifiable entities?
172 return CdmStore
.getService(IGroupService
.class).listByName(groupNameSearchString
, null, null, null, null, null, null);
180 private boolean checkLargeResult(int count
) {
181 if(count
> MAX_RESULTS_BEFORE_WARNING
){
182 return MessageDialog
.openConfirm(Display
.getDefault().getActiveShell(), "Large result expected",
183 String
.format("The current search will return %s objects. This will " +
184 "take a long time and/or might render the editor unusable. Please consider refining your search.", count
));
192 * @param configurator
195 private String
sqlizeTitleSearchString(IIdentifiableEntityServiceConfigurator configurator
){
196 return configurator
.getTitleSearchString().replace(WILDCARD
, "%");