Project

General

Profile

Download (10.5 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.DerivedUnit;
43
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
44
import eu.etaxonomy.cdm.model.reference.Reference;
45
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
46
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
47
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
48
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
49

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

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

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

    
61
	public static int NO_COUNT = -1;
62

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

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

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

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

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

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

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

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

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

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

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

    
154

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

    
182
		if(configurator.getClazz()==null){
183
		    if(showFieldUnits){
184
		        configurator.setClazz(SpecimenOrObservationBase.class);
185
		    }
186
		    else{
187
		        configurator.setClazz(DerivedUnit.class);
188
		    }
189
		}
190
		List<String> occurrencePropertyPaths = new ArrayList<String>();
191
		occurrencePropertyPaths.addAll(BASE_OCCURRENCE_INIT_STRATEGY);
192
		for(String propertyPath:BASE_OCCURRENCE_INIT_STRATEGY) {
193
		    occurrencePropertyPaths.add("derivationEvents.derivatives." + propertyPath);
194
		}
195
		configurator.setPropertyPaths(occurrencePropertyPaths);
196

    
197
		if(checkLargeResult(CdmStore.getService(IOccurrenceService.class).countOccurrences(configurator))){
198
			records = CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
199
		}
200
		addUuidSearchResults(records, configurator, IOccurrenceService.class);
201
		return records;
202
	}
203

    
204
	public List<User> findUsers(IIdentifiableEntityServiceConfigurator configurator){
205
		String userNameSearchString = sqlizeTitleSearchString(configurator);
206
		// TODO why are users not identifiable entities?
207
		List<User> records = CdmStore.getService(IUserService.class).listByUsername(userNameSearchString, null, null, null, null, null, null);
208
		addUuidSearchResults(records, configurator, IUserService.class);
209
        return records;
210
	}
211

    
212

    
213
	public List<Group> findGroups(IIdentifiableEntityServiceConfigurator configurator){
214
		String groupNameSearchString = sqlizeTitleSearchString(configurator);
215
		// TODO why are groups not identifiable entities?
216
		List<Group> records = CdmStore.getService(IGroupService.class).listByName(groupNameSearchString, null, null, null, null, null, null);
217
		addUuidSearchResults(records, configurator, IGroupService.class);
218
        return records;
219
	}
220

    
221

    
222
	private boolean checkLargeResult(int count) {
223
	    return checkLargeResult(count, MAX_RESULTS_BEFORE_WARNING);
224
	}
225

    
226
	private boolean checkLargeResult(int count, int maxBeforWarning) {
227
		if(count > maxBeforWarning){
228
			return MessageDialog.openConfirm(Display.getDefault().getActiveShell(), "Large result expected",
229
					String.format("The current search will return %s objects. This will " +
230
							"take a long time and/or might render the editor unusable. Please consider refining your search.", count));
231
		}else{
232
			return true;
233
		}
234
	}
235

    
236
	private String sqlizeTitleSearchString(IIdentifiableEntityServiceConfigurator configurator){
237
		return configurator.getTitleSearchString().replace(WILDCARD, "%");
238
	}
239

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

    
249
	public List findMedia(IIdentifiableEntityServiceConfigurator configurator) {
250
        if(checkLargeResult(CdmStore.getService(IMediaService.class).countByTitle(configurator))){
251
            List<Media> records = CdmStore.getService(IMediaService.class).findByTitle(configurator).getRecords();
252
            addUuidSearchResults(records, configurator, IMediaService.class);
253
            return records;
254
        }
255
        return NO_RESULTS;
256
    }
257

    
258

    
259
}
(8-8/13)