Project

General

Profile

Download (10.6 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.conversation.ConversationHolder;
22
import eu.etaxonomy.cdm.api.service.IAgentService;
23
import eu.etaxonomy.cdm.api.service.IGroupService;
24
import eu.etaxonomy.cdm.api.service.IMediaService;
25
import eu.etaxonomy.cdm.api.service.INameService;
26
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
27
import eu.etaxonomy.cdm.api.service.IReferenceService;
28
import eu.etaxonomy.cdm.api.service.IService;
29
import eu.etaxonomy.cdm.api.service.ITaxonService;
30
import eu.etaxonomy.cdm.api.service.IUserService;
31
import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
32
import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
33
import eu.etaxonomy.cdm.model.agent.AgentBase;
34
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
35
import eu.etaxonomy.cdm.model.common.Group;
36
import eu.etaxonomy.cdm.model.common.ICdmBase;
37
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
38
import eu.etaxonomy.cdm.model.common.RelationshipBase;
39
import eu.etaxonomy.cdm.model.common.User;
40
import eu.etaxonomy.cdm.model.media.Media;
41
import eu.etaxonomy.cdm.model.name.NameRelationship;
42
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
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.Messages;
48
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
49
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
50

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

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

    
60
	public static final String WILDCARD = "*"; //$NON-NLS-1$
61

    
62
	public static int NO_COUNT = -1;
63

    
64

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

    
68
	public List<TaxonNameBase> findNames(IIdentifiableEntityServiceConfigurator configurator, ConversationHolder conversation){
69

    
70
		if(checkLargeResult(CdmStore.getService(INameService.class).countByTitle(configurator))){
71
			List<TaxonNameBase> records = CdmStore.getService(INameService.class).findByTitle(configurator).getRecords();
72
			addUuidSearchResults(records, configurator, INameService.class);
73
            return records;
74
		}
75
		return NO_RESULTS;
76
	}
77

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

    
85
		List<NameRelationship> relationships = new ArrayList<NameRelationship>();
86
		List<RelationshipBase> all = CdmStore.getService(INameService.class).getAllRelationships(0, 0);
87

    
88
		for (RelationshipBase relationship : all){
89
			if(relationship instanceof NameRelationship){
90
				relationships.add((NameRelationship) relationship);
91
			}
92
		}
93
		return relationships;
94
	}
95

    
96
	public List<UuidAndTitleCache<IdentifiableEntity>> findTaxaAndNames(IFindTaxaAndNamesConfigurator<TaxonBase> configurator){
97
		return CdmStore.getService(ITaxonService.class).findTaxaAndNamesForEditor(configurator);
98
	}
99

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

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

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

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

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

    
157

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

    
185
		List<String> occurrencePropertyPaths = new ArrayList<String>();
186
		occurrencePropertyPaths.addAll(BASE_OCCURRENCE_INIT_STRATEGY);
187
		for(String propertyPath:BASE_OCCURRENCE_INIT_STRATEGY) {
188
		    occurrencePropertyPaths.add("derivationEvents.derivatives." + propertyPath); //$NON-NLS-1$
189
		}
190
		configurator.setPropertyPaths(occurrencePropertyPaths);
191

    
192
		if(checkLargeResult(CdmStore.getService(IOccurrenceService.class).countOccurrences(configurator))){
193
			records = CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
194
		}
195
		addUuidSearchResults(records, configurator, IOccurrenceService.class);
196
		return records;
197
	}
198

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

    
207

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

    
216

    
217
	private boolean checkLargeResult(int count) {
218
	    return checkLargeResult(count, MAX_RESULTS_BEFORE_WARNING);
219
	}
220

    
221
	private boolean checkLargeResult(int count, int maxBeforWarning) {
222
		if(count > maxBeforWarning){
223
			return MessageDialog.openConfirm(Display.getDefault().getActiveShell(), Messages.SearchManager_LARGE_RESULT_EXPECTED,
224
					String.format(Messages.SearchManager_LONG_SEARCH_WARNING, count));
225
		}else{
226
			return true;
227
		}
228
	}
229

    
230
	private String sqlizeTitleSearchString(IIdentifiableEntityServiceConfigurator configurator){
231
		return configurator.getTitleSearchString().replace(WILDCARD, "%"); //$NON-NLS-1$
232
	}
233

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

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

    
252

    
253
}
(8-8/13)