Project

General

Profile

Download (7.03 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 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

    
11
package eu.etaxonomy.taxeditor.store;
12

    
13
import java.util.ArrayList;
14
import java.util.Arrays;
15
import java.util.List;
16

    
17
import org.eclipse.jface.dialogs.MessageDialog;
18
import org.eclipse.swt.widgets.Display;
19

    
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.IIdentifiableEntityServiceConfigurator;
28
import eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator;
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.IIdentifiableEntity;
33
import eu.etaxonomy.cdm.model.common.RelationshipBase;
34
import eu.etaxonomy.cdm.model.common.User;
35
import eu.etaxonomy.cdm.model.common.UuidAndTitleCache;
36
import eu.etaxonomy.cdm.model.name.NameRelationship;
37
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
38
import eu.etaxonomy.cdm.model.occurrence.DerivedUnitBase;
39
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
40
import eu.etaxonomy.cdm.model.reference.Reference;
41
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
42
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
43
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
44

    
45
/**
46
 * @author n.hoffmann
47
 * @created Dec 8, 2010
48
 * @version 1.0
49
 */
50
public class SearchManager {
51
	
52
	public static final List NO_RESULTS = Arrays.asList(new Object[]{});
53
	
54
	public static final String WILDCARD = "*";
55
	
56
	public static int NO_COUNT = -1;
57
	
58
	// TODO make this configurable via preferences
59
	private static final int MAX_RESULTS_BEFORE_WARNING = 500; 
60
	
61
	/**
62
	 * <p>findNames</p>
63
	 *
64
	 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
65
	 * @return a {@link java.util.List} object.
66
	 */
67
	public List<TaxonNameBase> findNames(IIdentifiableEntityServiceConfigurator configurator){
68
		if(checkLargeResult(CdmStore.getService(INameService.class).countByTitle(configurator))){
69
			return CdmStore.getService(INameService.class).findByTitle(configurator).getRecords();
70
		}
71
		return NO_RESULTS;
72
	}
73
	
74
	public List<NameRelationship> findNameRelationships(
75
			IIdentifiableEntityServiceConfigurator configurator) {
76
		if(true){
77
			return NO_RESULTS;
78
		}
79
		
80
		List<NameRelationship> relationships = new ArrayList<NameRelationship>(); 
81
		List<RelationshipBase> all = CdmStore.getService(INameService.class).getAllRelationships(0, 0);
82
		
83
		for (RelationshipBase relationship : all){
84
			if(relationship instanceof NameRelationship){
85
				relationships.add((NameRelationship) relationship);
86
			}
87
		}
88
		
89
		
90
		return relationships;
91
		
92
	}
93

    
94
	/**
95
	 * <p>findTaxaAndNames</p>
96
	 *
97
	 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator} object.
98
	 * @return a {@link java.util.List} object.
99
	 */
100
	public List<UuidAndTitleCache<TaxonBase>> findTaxaAndNames(ITaxonServiceConfigurator configurator){
101
		return CdmStore.getService(ITaxonService.class).findTaxaAndNamesForEditor(configurator);
102
	}
103
	
104
	
105
	/**
106
	 * <p>findReferences</p>
107
	 *
108
	 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
109
	 * @return a {@link java.util.List} object.
110
	 */
111
	public List<Reference> findReferences(IIdentifiableEntityServiceConfigurator configurator){
112
		if(checkLargeResult(CdmStore.getService(IReferenceService.class).countByTitle(configurator))){
113
			return  CdmStore.getService(IReferenceService.class).findByTitle(configurator).getRecords();
114
		}
115
		return NO_RESULTS;
116
	}
117
	
118
	
119
	/**
120
	 * <p>findAgents</p>
121
	 *
122
	 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
123
	 * @return a {@link java.util.List} object.
124
	 */
125
	public List<AgentBase> findAgents(IIdentifiableEntityServiceConfigurator configurator){
126
		if(checkLargeResult(CdmStore.getService(IAgentService.class).countByTitle(configurator))){
127
			return CdmStore.getService(IAgentService.class).findByTitle(configurator).getRecords();
128
		}
129
		return NO_RESULTS;
130
	}
131

    
132
	/**
133
	 * <p>findTeamOrPersons</p>
134
	 *
135
	 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
136
	 * @return a {@link java.util.List} object.
137
	 */
138
	public List<AgentBase> findTeamOrPersons(IIdentifiableEntityServiceConfigurator configurator){
139
		configurator.setClazz(TeamOrPersonBase.class);
140
		return findAgents(configurator);
141
	}
142
	
143
	
144
	/**
145
	 * <p>findOccurrences</p>
146
	 *
147
	 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
148
	 * @return a {@link java.util.List} object.
149
	 */
150
	public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator configurator){
151
		// by default we do not show field observations. This may be configured via preferences
152
		boolean showFieldObservations = PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.BULK_EDITOR_OCCURRENCE_SHOW_FIELD_OBSERVATIONS);
153
		if(! showFieldObservations){
154
			configurator.setClazz(DerivedUnitBase.class);
155
		}
156
		if(checkLargeResult(CdmStore.getService(IOccurrenceService.class).countByTitle(configurator))){
157
			return CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
158
		}
159
		return NO_RESULTS;
160
	}
161
	
162
	
163
	public List<User> findUsers(IIdentifiableEntityServiceConfigurator configurator){
164
		String userNameSearchString = sqlizeTitleSearchString(configurator);
165
		// TODO why are users not identifiable entities?
166
		return CdmStore.getService(IUserService.class).listByUsername(userNameSearchString, null, null, null, null, null, null);
167
	}
168
	
169
	
170
	public List<Group> findGroups(IIdentifiableEntityServiceConfigurator configurator){
171
		String groupNameSearchString = sqlizeTitleSearchString(configurator);
172
		// TODO why are groups not identifiable entities?
173
		return CdmStore.getService(IGroupService.class).listByName(groupNameSearchString, null, null, null, null, null, null);
174
	}
175
	
176
	
177
	/**
178
	 * @param count
179
	 * @return
180
	 */
181
	private boolean checkLargeResult(int count) {
182
		if(count > MAX_RESULTS_BEFORE_WARNING){
183
			return MessageDialog.openConfirm(Display.getDefault().getActiveShell(), "Large result expected", 
184
					String.format("The current search will return %s objects. This will " +
185
							"take a long time and/or might render the editor unusable. Please consider refining your search.", count));
186
		}else{
187
			return true;
188
		}		
189
	}
190
	
191
	/**
192
	 * 
193
	 * @param configurator
194
	 * @return
195
	 */
196
	private String sqlizeTitleSearchString(IIdentifiableEntityServiceConfigurator configurator){
197
		return configurator.getTitleSearchString().replace(WILDCARD, "%");
198
	}
199

    
200

    
201
}
(6-6/9)