// $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 eu.etaxonomy.cdm.api.service.IReferenceService;
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.api.service.config.IIdentifiableEntityServiceConfigurator;
-import eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator;
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.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.occurrence.SpecimenOrObservationType;
import eu.etaxonomy.cdm.model.reference.Reference;
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
+import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
/**
* @author n.hoffmann
* @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;
-
+ private static final int MAX_RESULTS_BEFORE_WARNING = 500;
+
/**
* <p>findNames</p>
*
* @return a {@link java.util.List} object.
*/
public List<TaxonNameBase> findNames(IIdentifiableEntityServiceConfigurator configurator){
- // TODO we want to use IIdentifiableEntityServiceConfigurator for all find methods
- // unfortunately this is not consistently implemented in the library.
- // FIXME use proper method once it is implemented in the library
- String titleSearchString = getTitleSearchString(configurator);
-
- return checkLargeResult(configurator, countNames(configurator)) ? CdmStore.getService(INameService.class).getNamesByName(titleSearchString) : NO_RESULTS;
+ if(checkLargeResult(CdmStore.getService(INameService.class).countByTitle(configurator))){
+ return CdmStore.getService(INameService.class).findByTitle(configurator).getRecords();
+ }
+ return NO_RESULTS;
}
-
+ public List<NameRelationship> findNameRelationships(
+ IIdentifiableEntityServiceConfigurator configurator) {
+ if(true){
+ return NO_RESULTS;
+ }
+
+ 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;
- private int countNames(IIdentifiableEntityServiceConfigurator configurator){
- return NO_COUNT;
}
/**
* @param configurator a {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator} object.
* @return a {@link java.util.List} object.
*/
- public List<UuidAndTitleCache<TaxonBase>> findTaxaAndNames(ITaxonServiceConfigurator configurator){
-
- return checkLargeResult(configurator, countTaxaAndNames(configurator)) ? CdmStore.getService(ITaxonService.class).findTaxaAndNamesForEditor(configurator) : NO_RESULTS;
- }
-
- private int countTaxaAndNames(ITaxonServiceConfigurator configurator){
- return NO_COUNT;
+ public List<UuidAndTitleCache<TaxonBase>> findTaxaAndNames(IFindTaxaAndNamesConfigurator<TaxonBase> configurator){
+ return CdmStore.getService(ITaxonService.class).findTaxaAndNamesForEditor(configurator);
}
-
+
+
/**
* <p>findReferences</p>
*
* @return a {@link java.util.List} object.
*/
public List<Reference> findReferences(IIdentifiableEntityServiceConfigurator configurator){
- // TODO we want to use IIdentifiableEntityServiceConfigurator for all find methods
- // unfortunately this is not consistently implemented in the library.
- // FIXME use proper method once it is implemented in the library
- String titleSearchString = getTitleSearchString(configurator);
-
- return checkLargeResult(configurator, countReferences(configurator)) ? CdmStore.getService(IReferenceService.class).findByTitle(null, titleSearchString, null, null, null, null, null, null).getRecords() : NO_RESULTS;
- }
-
- private int countReferences(IIdentifiableEntityServiceConfigurator configurator){
- return NO_COUNT;
+ if(checkLargeResult(CdmStore.getService(IReferenceService.class).countByTitle(configurator))){
+ return CdmStore.getService(IReferenceService.class).findByTitle(configurator).getRecords();
+ }
+ return NO_RESULTS;
}
-
+
+
/**
* <p>findAgents</p>
*
* @return a {@link java.util.List} object.
*/
public List<AgentBase> findAgents(IIdentifiableEntityServiceConfigurator configurator){
- // TODO we want to use IIdentifiableEntityServiceConfigurator for all find methods
- // unfortunately this is not consistently implemented in the library.
- // FIXME use proper method once it is implemented in the library
- String titleSearchString = getTitleSearchString(configurator);
-
- return checkLargeResult(configurator, countAgents(configurator)) ? CdmStore.getService(IAgentService.class).findByTitle(null, titleSearchString, null, null, null, null, null, null).getRecords() : NO_RESULTS;
- }
-
- private int countAgents(IIdentifiableEntityServiceConfigurator configurator){
- return NO_COUNT;
+ if(checkLargeResult(CdmStore.getService(IAgentService.class).countByTitle(configurator))){
+ return CdmStore.getService(IAgentService.class).findByTitle(configurator).getRecords();
+ }
+ return NO_RESULTS;
}
/**
* @return a {@link java.util.List} object.
*/
public List<AgentBase> findTeamOrPersons(IIdentifiableEntityServiceConfigurator configurator){
- // TODO move this to cdmlib
- List<AgentBase> result = new ArrayList<AgentBase>();
- for (AgentBase agent : findAgents(configurator)) {
- result.add(agent);
- }
- return result;
+ configurator.setClazz(TeamOrPersonBase.class);
+ return findAgents(configurator);
}
-
+
/**
- * @param configurator
- * @return
+ * 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 the configurator to use for the search
+ * @return a list of the SpecimenOrObservationBases matching the search parameters found
*/
- private int countTeamOrPersons(IIdentifiableEntityServiceConfigurator configurator) {
- return NO_COUNT;
+ 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){
- // TODO we want to use IIdentifiableEntityServiceConfigurator for all find methods
- // unfortunately this is not consistently implemented in the library.
- // FIXME use proper method once it is implemented in the library
- String titleSearchString = getTitleSearchString(configurator);
-
- return checkLargeResult(configurator, countOccurrences(configurator)) ? CdmStore.getService(IOccurrenceService.class).findByTitle(SpecimenOrObservationBase.class, titleSearchString, null, null, null, null, null, null).getRecords() : NO_RESULTS;
+ public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator configurator, boolean showFieldUnits){
+ if(! showFieldUnits){
+ configurator.setClazz(DerivedUnit.class);
+ }
+ if(checkLargeResult(CdmStore.getService(IOccurrenceService.class).countByTitle(configurator))){
+ return CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
+ }
+ return NO_RESULTS;
}
-
- private int countOccurrences(IIdentifiableEntityServiceConfigurator configurator){
- return NO_COUNT;
+
+ /**
+ * Searches for {@link SpecimenOrObservationBase} with the parameters specified in the {@link IIdentifiableEntityServiceConfigurator}
+ * which match the given {@link SpecimenOrObservationType}
+ * @param configurator the configurator to use for the search
+ * @param type the type/record basis the specimens must have
+ * @return a list of the SpecimenOrObservationBases matching the search parameters found
+
+ * @deprecated method needs to be optimized with a direct SQL query instead of iterating over all specimens
+ */
+ @Deprecated
+ //TODO: method needs to be optimized with a direct SQL query instead of iterating over all specimens
+ public List<SpecimenOrObservationBase> findOccurrencesByType(IIdentifiableEntityServiceConfigurator configurator, SpecimenOrObservationType type){
+ List<SpecimenOrObservationBase> filteredOccurrences = new ArrayList<SpecimenOrObservationBase>();
+ List<SpecimenOrObservationBase> occurrences = findOccurrences(configurator, true);
+ if(type!=null){
+ for(SpecimenOrObservationBase occurrence:occurrences){
+ if(occurrence.getRecordBasis().equals(type)
+ || occurrence.getRecordBasis().isKindOf(type)
+ || type == SpecimenOrObservationType.Unknown){
+ filteredOccurrences.add(occurrence);
+ }
+ }
+ }
+ else{
+ filteredOccurrences = occurrences;
+ }
+ return filteredOccurrences;
}
-
+
public List<User> findUsers(IIdentifiableEntityServiceConfigurator configurator){
- String userNameSearchString = getTitleSearchString(configurator);
-
- return checkLargeResult(configurator, countUsers(configurator)) ? CdmStore.getService(IUserService.class).listByUsername(userNameSearchString, null, null, null, null, null, null) : NO_RESULTS;
- }
-
- private int countUsers(IIdentifiableEntityServiceConfigurator configurator){
- return NO_COUNT;
+ String userNameSearchString = sqlizeTitleSearchString(configurator);
+ // TODO why are users not identifiable entities?
+ return CdmStore.getService(IUserService.class).listByUsername(userNameSearchString, null, null, null, null, null, null);
}
-
+
+
public List<Group> findGroups(IIdentifiableEntityServiceConfigurator configurator){
- String groupNameSearchString = getTitleSearchString(configurator);
-
- return checkLargeResult(configurator, countGroups(configurator)) ? CdmStore.getService(IGroupService.class).listByName(groupNameSearchString, null, null, null, null, null, null) : NO_RESULTS;
+ String groupNameSearchString = sqlizeTitleSearchString(configurator);
+ // TODO why are groups not identifiable entities?
+ return CdmStore.getService(IGroupService.class).listByName(groupNameSearchString, null, null, null, null, null, null);
}
-
- private int countGroups(IIdentifiableEntityServiceConfigurator configurator){
- return NO_COUNT;
- }
-
+
+
/**
- * @param countNames
+ * @param count
* @return
*/
- private boolean checkLargeResult(IIdentifiableEntityServiceConfigurator configurator, int countNames) {
- if(isWildCardOnly(configurator) || countNames > MAX_RESULTS_BEFORE_WARNING){
- return MessageDialog.openConfirm(Display.getDefault().getActiveShell(), "Large result exptected", "The current search could lead to a very large result. Retrieving this result set might take a long time and/or render the editor unusable.");
+ private boolean checkLargeResult(int count) {
+ if(count > MAX_RESULTS_BEFORE_WARNING){
+ 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;
- }
- }
-
- private boolean isWildCardOnly(IIdentifiableEntityServiceConfigurator configurator){
- return WILDCARD.equals(configurator.getTitleSearchString());
+ }
}
-
+
/**
- * TODO this will be obsolete once IIdentifiableEntityServiceConfigurator is implemented in find* methods
- *
+ *
* @param configurator
* @return
*/
- private String getTitleSearchString(IIdentifiableEntityServiceConfigurator configurator){
+ private String sqlizeTitleSearchString(IIdentifiableEntityServiceConfigurator configurator){
return configurator.getTitleSearchString().replace(WILDCARD, "%");
}
+
+
}