Merge branch 'develop' into featureTreeEditor
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / store / SearchManager.java
index 395550a7bbad3761097df09ae417939bae6dba74..75dd9500649820f7aa2eec0ea0195f60fd767c02 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$
 /**
 * Copyright (C) 2007 EDIT
 * European Distributed Institute of Taxonomy
@@ -18,8 +17,10 @@ import java.util.UUID;
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.swt.widgets.Display;
 
+import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.api.service.IAgentService;
 import eu.etaxonomy.cdm.api.service.IGroupService;
+import eu.etaxonomy.cdm.api.service.IMediaService;
 import eu.etaxonomy.cdm.api.service.INameService;
 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
 import eu.etaxonomy.cdm.api.service.IReferenceService;
@@ -35,34 +36,37 @@ 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.media.Media;
 import eu.etaxonomy.cdm.model.name.NameRelationship;
-import eu.etaxonomy.cdm.model.name.TaxonNameBase;
+import eu.etaxonomy.cdm.model.name.TaxonName;
 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.l10n.Messages;
 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 
 /**
  * @author n.hoffmann
  * @created Dec 8, 2010
- * @version 1.0
  */
 public class SearchManager {
 
        public static final List NO_RESULTS = Arrays.asList(new Object[]{});
 
-       public static final String WILDCARD = "*";
+       public static final String WILDCARD = "*"; //$NON-NLS-1$
 
        public static int NO_COUNT = -1;
 
+
        // TODO make this configurable via preferences
        private static final int MAX_RESULTS_BEFORE_WARNING = 500;
 
-       public List<TaxonNameBase> findNames(IIdentifiableEntityServiceConfigurator configurator){
+       public List<TaxonName> findNames(IIdentifiableEntityServiceConfigurator configurator, ConversationHolder conversation){
+
                if(checkLargeResult(CdmStore.getService(INameService.class).countByTitle(configurator))){
-                       List<TaxonNameBase> records = CdmStore.getService(INameService.class).findByTitle(configurator).getRecords();
+                       List<TaxonName> records = CdmStore.getService(INameService.class).findByTitle(configurator).getRecords();
                        addUuidSearchResults(records, configurator, INameService.class);
             return records;
                }
@@ -101,6 +105,7 @@ public class SearchManager {
        }
 
        public List<AgentBase> findAgents(IIdentifiableEntityServiceConfigurator configurator){
+
                if(checkLargeResult(CdmStore.getService(IAgentService.class).countByTitle(configurator))){
                        List<AgentBase> records = CdmStore.getService(IAgentService.class).findByTitle(configurator).getRecords();
                        addUuidSearchResults(records, configurator, IAgentService.class);
@@ -119,7 +124,7 @@ public class SearchManager {
         String titleSearchString = configurator.getTitleSearchString();
                try {
                    UUID uuid = UUID.fromString(titleSearchString);
-                   T foundRecord = CdmStore.getService(service).find(uuid);
+                   T foundRecord = (T) CdmStore.getService(service).load(uuid, configurator.getPropertyPaths());
                    if(foundRecord!=null){
                        records.add(foundRecord);
                    }
@@ -161,25 +166,25 @@ public class SearchManager {
        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"
+                       "collection", //$NON-NLS-1$
+                       "descriptions", //$NON-NLS-1$
+                       "identifiers", //$NON-NLS-1$
+                       "derivationEvents.originals", //$NON-NLS-1$
+                       "derivedFrom.originals", //$NON-NLS-1$
+                       "gatheringEvent.country.representations", //$NON-NLS-1$
+                       "gatheringEvent.collector", //$NON-NLS-1$
+                       "gatheringEvent.locality", //$NON-NLS-1$
+                       "descriptions.descriptionElements", //$NON-NLS-1$
+                       "kindOfUnit", //$NON-NLS-1$
+                       "amplificationResults", //$NON-NLS-1$
+                       "sequences.singleReadAlignments", //$NON-NLS-1$
+                       "mediaSpecimen" //$NON-NLS-1$
                });
 
                List<String> occurrencePropertyPaths = new ArrayList<String>();
                occurrencePropertyPaths.addAll(BASE_OCCURRENCE_INIT_STRATEGY);
                for(String propertyPath:BASE_OCCURRENCE_INIT_STRATEGY) {
-                   occurrencePropertyPaths.add("derivationEvents.derivatives." + propertyPath);
+                   occurrencePropertyPaths.add("derivationEvents.derivatives." + propertyPath); //$NON-NLS-1$
                }
                configurator.setPropertyPaths(occurrencePropertyPaths);
 
@@ -209,17 +214,20 @@ public class SearchManager {
 
 
        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));
+           return checkLargeResult(count, MAX_RESULTS_BEFORE_WARNING);
+       }
+
+       private boolean checkLargeResult(int count, int maxBeforWarning) {
+               if(count > maxBeforWarning){
+                       return MessageDialog.openConfirm(Display.getDefault().getActiveShell(), Messages.SearchManager_LARGE_RESULT_EXPECTED,
+                                       String.format(Messages.SearchManager_LONG_SEARCH_WARNING, count));
                }else{
                        return true;
                }
        }
 
        private String sqlizeTitleSearchString(IIdentifiableEntityServiceConfigurator configurator){
-               return configurator.getTitleSearchString().replace(WILDCARD, "%");
+               return configurator.getTitleSearchString().replace(WILDCARD, "%"); //$NON-NLS-1$
        }
 
        public List findTaxa(IIdentifiableEntityServiceConfigurator configurator) {
@@ -231,5 +239,14 @@ public class SearchManager {
                return NO_RESULTS;
        }
 
+       public List findMedia(IIdentifiableEntityServiceConfigurator configurator) {
+        if(checkLargeResult(CdmStore.getService(IMediaService.class).countByTitle(configurator))){
+            List<Media> records = CdmStore.getService(IMediaService.class).findByTitle(configurator).getRecords();
+            addUuidSearchResults(records, configurator, IMediaService.class);
+            return records;
+        }
+        return NO_RESULTS;
+    }
+
 
 }