Project

General

Profile

Download (10.5 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.taxeditor.store;
11

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

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

    
20
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
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.TaxonName;
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.l10n.Messages;
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
 */
54
public class SearchManager {
55

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

    
58
	public static final String WILDCARD = "*"; //$NON-NLS-1$
59

    
60
	public static int NO_COUNT = -1;
61

    
62
    private static final int MAX_RESULTS_BEFORE_WARNING = 500;
63

    
64
	public List<TaxonName> findNames(IIdentifiableEntityServiceConfigurator configurator, ConversationHolder conversation){
65
	    List<TaxonName> records = CdmStore.getService(INameService.class).findByTitle(configurator).getRecords();
66
	    addUuidSearchResults(records, configurator, INameService.class);
67
	    return records;
68
	}
69

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

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

    
80
		for (RelationshipBase<?,?,?> relationship : all){
81
			if(relationship instanceof NameRelationship){
82
				relationships.add((NameRelationship) relationship);
83
			}
84
		}
85
		return relationships;
86
	}
87

    
88
	public List<UuidAndTitleCache<? extends IdentifiableEntity>> findTaxaAndNames(IFindTaxaAndNamesConfigurator configurator){
89
		return CdmStore.getService(ITaxonService.class).findTaxaAndNamesForEditor(configurator);
90
	}
91

    
92
	public List<Reference> findReferences(IIdentifiableEntityServiceConfigurator configurator){
93
	    List<Reference> records = CdmStore.getService(IReferenceService.class).findByTitle(configurator).getRecords();
94
	    addUuidSearchResults(records, configurator, IReferenceService.class);
95
	    return records;
96
	}
97

    
98
	public List<AgentBase> findAgents(IIdentifiableEntityServiceConfigurator configurator){
99
	    List<AgentBase> records = CdmStore.getService(IAgentService.class).findByTitle(configurator).getRecords();
100
	    addUuidSearchResults(records, configurator, IAgentService.class);
101
	    return records;
102
	}
103

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

    
123
	@SuppressWarnings("unchecked")
124
	public List<TeamOrPersonBase> findTeamOrPersons(IIdentifiableEntityServiceConfigurator configurator){
125
	    configurator.setClazz(TeamOrPersonBase.class);
126
	    return (List)findAgents(configurator);
127
	}
128

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

    
143

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

    
172
		List<String> occurrencePropertyPaths = new ArrayList<String>();
173
		occurrencePropertyPaths.addAll(BASE_OCCURRENCE_INIT_STRATEGY);
174
		for(String propertyPath:BASE_OCCURRENCE_INIT_STRATEGY) {
175
		    occurrencePropertyPaths.add("derivationEvents.derivatives." + propertyPath); //$NON-NLS-1$
176
		}
177
		configurator.setPropertyPaths(occurrencePropertyPaths);
178

    
179
        if(showCountWarning && checkLargeResult(CdmStore.getService(IOccurrenceService.class).countOccurrences(configurator))){
180
            records = CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
181
        }
182
        else{
183
            records = CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
184
        }
185
		addUuidSearchResults(records, configurator, IOccurrenceService.class);
186
		return records;
187
	}
188

    
189
	public List<User> findUsers(IIdentifiableEntityServiceConfigurator configurator){
190
		String userNameSearchString = sqlizeTitleSearchString(configurator);
191
		// TODO why are users not identifiable entities?
192
        List<User> records = CdmStore.getService(IUserService.class).listByUsername(userNameSearchString,
193
                configurator.getMatchMode(), configurator.getCriteria(), configurator.getPageSize(),
194
                configurator.getPageNumber(), configurator.getOrderHints(), configurator.getPropertyPaths());
195
        addUuidSearchResults(records, configurator, IUserService.class);
196
        return records;
197
	}
198

    
199

    
200

    
201
    private boolean checkLargeResult(long count) {
202
        return checkLargeResult(count, MAX_RESULTS_BEFORE_WARNING);
203
    }
204

    
205
    private boolean checkLargeResult(long count, int maxBeforWarning) {
206
        if(count > maxBeforWarning){
207
            return MessageDialog.openConfirm(Display.getDefault().getActiveShell(), Messages.SearchManager_LARGE_RESULT_EXPECTED,
208
                    String.format(Messages.SearchManager_LONG_SEARCH_WARNING, count));
209
        }else{
210
            return true;
211
        }
212
    }
213

    
214
	public List<Group> findGroups(IIdentifiableEntityServiceConfigurator configurator){
215
		String groupNameSearchString = sqlizeTitleSearchString(configurator);
216
		// TODO why are groups not identifiable entities?
217
        List<Group> records = CdmStore.getService(IGroupService.class).listByName(groupNameSearchString,
218
                configurator.getMatchMode(), configurator.getCriteria(), configurator.getPageSize(),
219
                configurator.getPageNumber(), configurator.getOrderHints(), configurator.getPropertyPaths());
220
        addUuidSearchResults(records, configurator, IGroupService.class);
221
        return records;
222
	}
223

    
224

    
225
	private String sqlizeTitleSearchString(IIdentifiableEntityServiceConfigurator configurator){
226
		return configurator.getTitleSearchString().replace(WILDCARD, "%"); //$NON-NLS-1$
227
	}
228

    
229
	public List findTaxa(IIdentifiableEntityServiceConfigurator configurator) {
230
	    List<TaxonBase> records = CdmStore.getService(ITaxonService.class).findByTitle(configurator).getRecords();
231
	    addUuidSearchResults(records, configurator, ITaxonService.class);
232
	    return records;
233
	}
234

    
235
	public List findMedia(IIdentifiableEntityServiceConfigurator configurator) {
236
	    List<Media> records = CdmStore.getService(IMediaService.class).findByTitle(configurator).getRecords();
237
	    addUuidSearchResults(records, configurator, IMediaService.class);
238
	    return records;
239
    }
240

    
241
}
(9-9/14)