// $Id$
/**
* Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy
+* European Distributed Institute of Taxonomy
* http://www.e-taxonomy.eu
-*
+*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* See LICENSE.TXT at the top of this package for the full license terms.
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import java.util.UUID;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import eu.etaxonomy.cdm.api.service.INameService;
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
import eu.etaxonomy.cdm.api.service.IReferenceService;
+import eu.etaxonomy.cdm.api.service.IService;
import eu.etaxonomy.cdm.api.service.ITaxonService;
import eu.etaxonomy.cdm.api.service.IUserService;
import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
import eu.etaxonomy.cdm.model.agent.AgentBase;
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
import eu.etaxonomy.cdm.model.common.Group;
+import eu.etaxonomy.cdm.model.common.ICdmBase;
+import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
import eu.etaxonomy.cdm.model.common.RelationshipBase;
import eu.etaxonomy.cdm.model.common.User;
-import eu.etaxonomy.cdm.model.common.UuidAndTitleCache;
import eu.etaxonomy.cdm.model.name.NameRelationship;
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
-import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
import eu.etaxonomy.cdm.model.reference.Reference;
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
+import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
* @version 1.0
*/
public class SearchManager {
-
+
public static final List NO_RESULTS = Arrays.asList(new Object[]{});
-
+
public static final String WILDCARD = "*";
-
+
public static int NO_COUNT = -1;
-
+
// TODO make this configurable via preferences
- private static final int MAX_RESULTS_BEFORE_WARNING = 500;
-
- /**
- * <p>findNames</p>
- *
- * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
- * @return a {@link java.util.List} object.
- */
+ private static final int MAX_RESULTS_BEFORE_WARNING = 500;
+
public List<TaxonNameBase> findNames(IIdentifiableEntityServiceConfigurator configurator){
if(checkLargeResult(CdmStore.getService(INameService.class).countByTitle(configurator))){
- return CdmStore.getService(INameService.class).findByTitle(configurator).getRecords();
+ List<TaxonNameBase> records = CdmStore.getService(INameService.class).findByTitle(configurator).getRecords();
+ addUuidSearchResults(records, configurator, INameService.class);
+ return records;
}
return NO_RESULTS;
}
-
+
public List<NameRelationship> findNameRelationships(
IIdentifiableEntityServiceConfigurator configurator) {
if(true){
+ //if activated again remember to add uuid search results like in other searches
return NO_RESULTS;
}
-
- List<NameRelationship> relationships = new ArrayList<NameRelationship>();
+
+ List<NameRelationship> relationships = new ArrayList<NameRelationship>();
List<RelationshipBase> all = CdmStore.getService(INameService.class).getAllRelationships(0, 0);
-
+
for (RelationshipBase relationship : all){
if(relationship instanceof NameRelationship){
relationships.add((NameRelationship) relationship);
}
}
-
-
return relationships;
-
}
- /**
- * <p>findTaxaAndNames</p>
- *
- * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator} object.
- * @return a {@link java.util.List} object.
- */
- public List<UuidAndTitleCache<TaxonBase>> findTaxaAndNames(IFindTaxaAndNamesConfigurator<TaxonBase> configurator){
+ public List<UuidAndTitleCache<IdentifiableEntity>> findTaxaAndNames(IFindTaxaAndNamesConfigurator<TaxonBase> configurator){
return CdmStore.getService(ITaxonService.class).findTaxaAndNamesForEditor(configurator);
}
-
-
- /**
- * <p>findReferences</p>
- *
- * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
- * @return a {@link java.util.List} object.
- */
+
public List<Reference> findReferences(IIdentifiableEntityServiceConfigurator configurator){
if(checkLargeResult(CdmStore.getService(IReferenceService.class).countByTitle(configurator))){
- return CdmStore.getService(IReferenceService.class).findByTitle(configurator).getRecords();
+ List<Reference> records = CdmStore.getService(IReferenceService.class).findByTitle(configurator).getRecords();
+ addUuidSearchResults(records, configurator, IReferenceService.class);
+ return records;
}
return NO_RESULTS;
}
-
-
- /**
- * <p>findAgents</p>
- *
- * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
- * @return a {@link java.util.List} object.
- */
+
public List<AgentBase> findAgents(IIdentifiableEntityServiceConfigurator configurator){
if(checkLargeResult(CdmStore.getService(IAgentService.class).countByTitle(configurator))){
- return CdmStore.getService(IAgentService.class).findByTitle(configurator).getRecords();
+ List<AgentBase> records = CdmStore.getService(IAgentService.class).findByTitle(configurator).getRecords();
+ addUuidSearchResults(records, configurator, IAgentService.class);
+ return records;
}
return NO_RESULTS;
}
+ /**
+ * Check search string if it is a {@link UUID} and, if <code>true</code>, search for the corresponding entity.
+ * @param records the list to which the search results are added
+ * @param configurator the configurator holding the search string
+ * @param service the service to use for searching
+ */
+ private <T extends ICdmBase> void addUuidSearchResults(List<T> records, IIdentifiableEntityServiceConfigurator configurator, Class<? extends IService<T>> service) {
+ String titleSearchString = configurator.getTitleSearchString();
+ try {
+ UUID uuid = UUID.fromString(titleSearchString);
+ T foundRecord = CdmStore.getService(service).find(uuid);
+ if(foundRecord!=null){
+ records.add(foundRecord);
+ }
+ } catch (IllegalArgumentException e) {
+ //search string was no UUID
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public List<TeamOrPersonBase> findTeamOrPersons(IIdentifiableEntityServiceConfigurator configurator){
+ configurator.setClazz(TeamOrPersonBase.class);
+ return (List)findAgents(configurator);
+ }
+
/**
- * <p>findTeamOrPersons</p>
+ * Searches for {@link SpecimenOrObservationBase} with the parameters specified in the
+ * {@link IIdentifiableEntityServiceConfigurator}<br>
+ * <br>
+ * Note: FieldUnits are omitted by default. See {@link #findOccurrences(IIdentifiableEntityServiceConfigurator, boolean)}
*
- * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
- * @return a {@link java.util.List} object.
+ * @param configurator the configurator to use for the search
+ * @return a list of the SpecimenOrObservationBases matching the search parameters found
*/
- public List<AgentBase> findTeamOrPersons(IIdentifiableEntityServiceConfigurator configurator){
- configurator.setClazz(TeamOrPersonBase.class);
- return findAgents(configurator);
+ public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator configurator){
+ // by default we do not show field units. This may be configured via preferences
+ return findOccurrences(configurator, PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.BULK_EDITOR_OCCURRENCE_SHOW_FIELD_UNITS));
}
-
-
+
+
/**
- * <p>findOccurrences</p>
+ * Searches for {@link SpecimenOrObservationBase} with the parameters specified in the
+ * {@link IIdentifiableEntityServiceConfigurator}
*
- * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
- * @return a {@link java.util.List} object.
+ * @param configurator the configurator to use for the search
+ * @return a list of the SpecimenOrObservationBases found
+ * @param showFieldUnits if <code>true</code> then also FieldUnits are searched
+ * @return
*/
- public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator configurator){
- // by default we do not show field observations. This may be configured via preferences
- boolean showFieldObservations = PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.BULK_EDITOR_OCCURRENCE_SHOW_FIELD_OBSERVATIONS);
- if(! showFieldObservations){
- configurator.setClazz(DerivedUnit.class);
+ public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase> configurator, boolean showFieldUnits){
+ List<SpecimenOrObservationBase> records = new ArrayList<SpecimenOrObservationBase>();
+ final List<String> BASE_OCCURRENCE_INIT_STRATEGY = Arrays.asList(new String[] {
+ "collection",
+ "descriptions",
+ "identifiers",
+ "derivationEvents.originals",
+ "derivedFrom.originals",
+ "gatheringEvent.country.representations",
+ "gatheringEvent.collector",
+ "gatheringEvent.locality",
+ "descriptions.descriptionElements",
+ "kindOfUnit",
+ "amplificationResults",
+ "sequences.singleReadAlignments",
+ "mediaSpecimen"
+ });
+
+ List<String> occurrencePropertyPaths = new ArrayList<String>();
+ occurrencePropertyPaths.addAll(BASE_OCCURRENCE_INIT_STRATEGY);
+ for(String propertyPath:BASE_OCCURRENCE_INIT_STRATEGY) {
+ occurrencePropertyPaths.add("derivationEvents.derivatives." + propertyPath);
}
- if(checkLargeResult(CdmStore.getService(IOccurrenceService.class).countByTitle(configurator))){
- return CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
+ configurator.setPropertyPaths(occurrencePropertyPaths);
+
+ if(checkLargeResult(CdmStore.getService(IOccurrenceService.class).countOccurrences(configurator))){
+ records = CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
}
- return NO_RESULTS;
+ addUuidSearchResults(records, configurator, IOccurrenceService.class);
+ return records;
}
-
-
+
public List<User> findUsers(IIdentifiableEntityServiceConfigurator configurator){
String userNameSearchString = sqlizeTitleSearchString(configurator);
// TODO why are users not identifiable entities?
- return CdmStore.getService(IUserService.class).listByUsername(userNameSearchString, null, null, null, null, null, null);
+ List<User> records = CdmStore.getService(IUserService.class).listByUsername(userNameSearchString, null, null, null, null, null, null);
+ addUuidSearchResults(records, configurator, IUserService.class);
+ return records;
}
-
-
+
+
public List<Group> findGroups(IIdentifiableEntityServiceConfigurator configurator){
String groupNameSearchString = sqlizeTitleSearchString(configurator);
// TODO why are groups not identifiable entities?
- return CdmStore.getService(IGroupService.class).listByName(groupNameSearchString, null, null, null, null, null, null);
+ List<Group> records = CdmStore.getService(IGroupService.class).listByName(groupNameSearchString, null, null, null, null, null, null);
+ addUuidSearchResults(records, configurator, IGroupService.class);
+ return records;
}
-
-
- /**
- * @param count
- * @return
- */
+
+
private boolean checkLargeResult(int count) {
if(count > MAX_RESULTS_BEFORE_WARNING){
- return MessageDialog.openConfirm(Display.getDefault().getActiveShell(), "Large result expected",
+ return MessageDialog.openConfirm(Display.getDefault().getActiveShell(), "Large result expected",
String.format("The current search will return %s objects. This will " +
"take a long time and/or might render the editor unusable. Please consider refining your search.", count));
}else{
return true;
- }
+ }
}
-
- /**
- *
- * @param configurator
- * @return
- */
+
private String sqlizeTitleSearchString(IIdentifiableEntityServiceConfigurator configurator){
return configurator.getTitleSearchString().replace(WILDCARD, "%");
}
+ public List findTaxa(IIdentifiableEntityServiceConfigurator configurator) {
+ if(checkLargeResult(CdmStore.getService(ITaxonService.class).countByTitle(configurator))){
+ List<TaxonBase> records = CdmStore.getService(ITaxonService.class).findByTitle(configurator).getRecords();
+ addUuidSearchResults(records, configurator, ITaxonService.class);
+ return records;
+ }
+ return NO_RESULTS;
+ }
+
}