Project

General

Profile

Download (9.62 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.INameService;
24
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
25
import eu.etaxonomy.cdm.api.service.IReferenceService;
26
import eu.etaxonomy.cdm.api.service.IService;
27
import eu.etaxonomy.cdm.api.service.ITaxonService;
28
import eu.etaxonomy.cdm.api.service.IUserService;
29
import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
30
import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
31
import eu.etaxonomy.cdm.model.agent.AgentBase;
32
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
33
import eu.etaxonomy.cdm.model.common.Group;
34
import eu.etaxonomy.cdm.model.common.ICdmBase;
35
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
36
import eu.etaxonomy.cdm.model.common.RelationshipBase;
37
import eu.etaxonomy.cdm.model.common.User;
38
import eu.etaxonomy.cdm.model.name.NameRelationship;
39
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
40
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
41
import eu.etaxonomy.cdm.model.reference.Reference;
42
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
43
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
44
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
45
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
46

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

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

    
56
	public static final String WILDCARD = "*";
57

    
58
	public static int NO_COUNT = -1;
59

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

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

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

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

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

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

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

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

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

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

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

    
151

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

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

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

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

    
201

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

    
210

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

    
221
	private String sqlizeTitleSearchString(IIdentifiableEntityServiceConfigurator configurator){
222
		return configurator.getTitleSearchString().replace(WILDCARD, "%");
223
	}
224

    
225
	public List findTaxa(IIdentifiableEntityServiceConfigurator configurator) {
226
		if(checkLargeResult(CdmStore.getService(ITaxonService.class).countByTitle(configurator))){
227
			List<TaxonBase> records = CdmStore.getService(ITaxonService.class).findByTitle(configurator).getRecords();
228
			addUuidSearchResults(records, configurator, ITaxonService.class);
229
            return records;
230
		}
231
		return NO_RESULTS;
232
	}
233

    
234

    
235
}
(8-8/13)