Project

General

Profile

Download (10.3 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
import java.util.UUID;
17

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

    
21
import eu.etaxonomy.cdm.api.service.IAgentService;
22
import eu.etaxonomy.cdm.api.service.IGroupService;
23
import eu.etaxonomy.cdm.api.service.IMediaService;
24
import eu.etaxonomy.cdm.api.service.INameService;
25
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
26
import eu.etaxonomy.cdm.api.service.IReferenceService;
27
import eu.etaxonomy.cdm.api.service.IService;
28
import eu.etaxonomy.cdm.api.service.ITaxonService;
29
import eu.etaxonomy.cdm.api.service.IUserService;
30
import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
31
import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
32
import eu.etaxonomy.cdm.model.agent.AgentBase;
33
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
34
import eu.etaxonomy.cdm.model.common.Group;
35
import eu.etaxonomy.cdm.model.common.ICdmBase;
36
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
37
import eu.etaxonomy.cdm.model.common.RelationshipBase;
38
import eu.etaxonomy.cdm.model.common.User;
39
import eu.etaxonomy.cdm.model.media.Media;
40
import eu.etaxonomy.cdm.model.name.NameRelationship;
41
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
42
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
43
import eu.etaxonomy.cdm.model.reference.Reference;
44
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
45
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
46
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
47
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
48

    
49
/**
50
 * @author n.hoffmann
51
 * @created Dec 8, 2010
52
 * @version 1.0
53
 */
54
public class SearchManager {
55

    
56
	public static final List NO_RESULTS = Arrays.asList(new Object[]{});
57

    
58
	public static final String WILDCARD = "*";
59

    
60
	public static int NO_COUNT = -1;
61

    
62
	// TODO make this configurable via preferences
63
	private static final int MAX_RESULTS_BEFORE_WARNING = 500;
64

    
65
	public List<TaxonNameBase> findNames(IIdentifiableEntityServiceConfigurator configurator){
66
		if(checkLargeResult(CdmStore.getService(INameService.class).countByTitle(configurator))){
67
			List<TaxonNameBase> records = CdmStore.getService(INameService.class).findByTitle(configurator).getRecords();
68
			addUuidSearchResults(records, configurator, INameService.class);
69
            return records;
70
		}
71
		return NO_RESULTS;
72
	}
73

    
74
	public List<NameRelationship> findNameRelationships(
75
			IIdentifiableEntityServiceConfigurator configurator) {
76
		if(true){
77
		    //if activated again remember to add uuid search results like in other searches
78
			return NO_RESULTS;
79
		}
80

    
81
		List<NameRelationship> relationships = new ArrayList<NameRelationship>();
82
		List<RelationshipBase> all = CdmStore.getService(INameService.class).getAllRelationships(0, 0);
83

    
84
		for (RelationshipBase relationship : all){
85
			if(relationship instanceof NameRelationship){
86
				relationships.add((NameRelationship) relationship);
87
			}
88
		}
89
		return relationships;
90
	}
91

    
92
	public List<UuidAndTitleCache<IdentifiableEntity>> findTaxaAndNames(IFindTaxaAndNamesConfigurator<TaxonBase> configurator){
93
		return CdmStore.getService(ITaxonService.class).findTaxaAndNamesForEditor(configurator);
94
	}
95

    
96
	public List<Reference> findReferences(IIdentifiableEntityServiceConfigurator configurator){
97
		if(checkLargeResult(CdmStore.getService(IReferenceService.class).countByTitle(configurator))){
98
			List<Reference> records = CdmStore.getService(IReferenceService.class).findByTitle(configurator).getRecords();
99
			addUuidSearchResults(records, configurator, IReferenceService.class);
100
            return records;
101
		}
102
		return NO_RESULTS;
103
	}
104

    
105
	public List<AgentBase> findAgents(IIdentifiableEntityServiceConfigurator configurator){
106
		if(checkLargeResult(CdmStore.getService(IAgentService.class).countByTitle(configurator))){
107
			List<AgentBase> records = CdmStore.getService(IAgentService.class).findByTitle(configurator).getRecords();
108
			addUuidSearchResults(records, configurator, IAgentService.class);
109
            return records;
110
		}
111
		return NO_RESULTS;
112
	}
113

    
114
    /**
115
     * Check search string if it is a {@link UUID} and, if <code>true</code>, search for the corresponding entity.
116
     * @param records the list to which the search results are added
117
     * @param configurator the configurator holding the search string
118
     * @param service the service to use for searching
119
     */
120
    private <T extends ICdmBase> void addUuidSearchResults(List<T> records, IIdentifiableEntityServiceConfigurator configurator, Class<? extends IService<T>> service) {
121
        String titleSearchString = configurator.getTitleSearchString();
122
		try {
123
		    UUID uuid = UUID.fromString(titleSearchString);
124
		    T foundRecord = CdmStore.getService(service).find(uuid);
125
		    if(foundRecord!=null){
126
		        records.add(foundRecord);
127
		    }
128
		} catch (IllegalArgumentException e) {
129
		    //search string was no UUID
130
		}
131
    }
132

    
133
	@SuppressWarnings("unchecked")
134
	public List<TeamOrPersonBase> findTeamOrPersons(IIdentifiableEntityServiceConfigurator configurator){
135
	    configurator.setClazz(TeamOrPersonBase.class);
136
	    return (List)findAgents(configurator);
137
	}
138

    
139
	/**
140
	 * Searches for {@link SpecimenOrObservationBase} with the parameters specified in the
141
	 * {@link IIdentifiableEntityServiceConfigurator}<br>
142
	 * <br>
143
	 * Note: FieldUnits are omitted by default. See {@link #findOccurrences(IIdentifiableEntityServiceConfigurator, boolean)}
144
	 *
145
	 * @param configurator the configurator to use for the search
146
	 * @return a list of the SpecimenOrObservationBases matching the search parameters found
147
	 */
148
	public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator configurator){
149
	    // by default we do not show field units. This may be configured via preferences
150
	    return findOccurrences(configurator, PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.BULK_EDITOR_OCCURRENCE_SHOW_FIELD_UNITS));
151
	}
152

    
153

    
154
	/**
155
	 * Searches for {@link SpecimenOrObservationBase} with the parameters specified in the
156
	 * {@link IIdentifiableEntityServiceConfigurator}
157
	 *
158
	 * @param configurator the configurator to use for the search
159
	 * @return a list of the SpecimenOrObservationBases found
160
	 * @param showFieldUnits if <code>true</code> then also FieldUnits are searched
161
	 * @return
162
	 */
163
	public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase> configurator, boolean showFieldUnits){
164
	    List<SpecimenOrObservationBase> records = new ArrayList<SpecimenOrObservationBase>();
165
		final List<String> BASE_OCCURRENCE_INIT_STRATEGY = Arrays.asList(new String[] {
166
		        "collection",
167
		        "descriptions",
168
		        "identifiers",
169
		        "derivationEvents.originals",
170
		        "derivedFrom.originals",
171
		        "gatheringEvent.country.representations",
172
		        "gatheringEvent.collector",
173
		        "gatheringEvent.locality",
174
		        "descriptions.descriptionElements",
175
		        "kindOfUnit",
176
		        "amplificationResults",
177
		        "sequences.singleReadAlignments",
178
		        "mediaSpecimen"
179
		});
180

    
181
		List<String> occurrencePropertyPaths = new ArrayList<String>();
182
		occurrencePropertyPaths.addAll(BASE_OCCURRENCE_INIT_STRATEGY);
183
		for(String propertyPath:BASE_OCCURRENCE_INIT_STRATEGY) {
184
		    occurrencePropertyPaths.add("derivationEvents.derivatives." + propertyPath);
185
		}
186
		configurator.setPropertyPaths(occurrencePropertyPaths);
187

    
188
		if(checkLargeResult(CdmStore.getService(IOccurrenceService.class).countOccurrences(configurator))){
189
			records = CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
190
		}
191
		addUuidSearchResults(records, configurator, IOccurrenceService.class);
192
		return records;
193
	}
194

    
195
	public List<User> findUsers(IIdentifiableEntityServiceConfigurator configurator){
196
		String userNameSearchString = sqlizeTitleSearchString(configurator);
197
		// TODO why are users not identifiable entities?
198
		List<User> records = CdmStore.getService(IUserService.class).listByUsername(userNameSearchString, null, null, null, null, null, null);
199
		addUuidSearchResults(records, configurator, IUserService.class);
200
        return records;
201
	}
202

    
203

    
204
	public List<Group> findGroups(IIdentifiableEntityServiceConfigurator configurator){
205
		String groupNameSearchString = sqlizeTitleSearchString(configurator);
206
		// TODO why are groups not identifiable entities?
207
		List<Group> records = CdmStore.getService(IGroupService.class).listByName(groupNameSearchString, null, null, null, null, null, null);
208
		addUuidSearchResults(records, configurator, IGroupService.class);
209
        return records;
210
	}
211

    
212

    
213
	private boolean checkLargeResult(int count) {
214
	    return checkLargeResult(count, MAX_RESULTS_BEFORE_WARNING);
215
	}
216

    
217
	private boolean checkLargeResult(int count, int maxBeforWarning) {
218
		if(count > maxBeforWarning){
219
			return MessageDialog.openConfirm(Display.getDefault().getActiveShell(), "Large result expected",
220
					String.format("The current search will return %s objects. This will " +
221
							"take a long time and/or might render the editor unusable. Please consider refining your search.", count));
222
		}else{
223
			return true;
224
		}
225
	}
226

    
227
	private String sqlizeTitleSearchString(IIdentifiableEntityServiceConfigurator configurator){
228
		return configurator.getTitleSearchString().replace(WILDCARD, "%");
229
	}
230

    
231
	public List findTaxa(IIdentifiableEntityServiceConfigurator configurator) {
232
		if(checkLargeResult(CdmStore.getService(ITaxonService.class).countByTitle(configurator))){
233
			List<TaxonBase> records = CdmStore.getService(ITaxonService.class).findByTitle(configurator).getRecords();
234
			addUuidSearchResults(records, configurator, ITaxonService.class);
235
            return records;
236
		}
237
		return NO_RESULTS;
238
	}
239

    
240
	public List findMedia(IIdentifiableEntityServiceConfigurator configurator) {
241
        if(checkLargeResult(CdmStore.getService(IMediaService.class).countByTitle(configurator))){
242
            List<Media> records = CdmStore.getService(IMediaService.class).findByTitle(configurator).getRecords();
243
            addUuidSearchResults(records, configurator, IMediaService.class);
244
            return records;
245
        }
246
        return NO_RESULTS;
247
    }
248

    
249

    
250
}
(8-8/13)