Support UUID search in bulk editors (#5103)
authorPatrick Plitzner <p.plitzner@bgbm.org>
Mon, 24 Aug 2015 08:29:14 +0000 (10:29 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Mon, 24 Aug 2015 08:39:19 +0000 (10:39 +0200)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/store/SearchManager.java

index cf673e8..4025e41 100644 (file)
@@ -13,6 +13,7 @@ package eu.etaxonomy.taxeditor.store;
 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;
@@ -22,6 +23,7 @@ import eu.etaxonomy.cdm.api.service.IGroupService;
 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;
@@ -29,6 +31,7 @@ import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurato
 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;
@@ -61,7 +64,12 @@ public class SearchManager {
 
        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();
+                       TaxonNameBase uuidSearchResult = addUuidSearchResults(configurator, INameService.class);
+            if(uuidSearchResult!=null){
+                records.add(uuidSearchResult);
+            }
+            return records;
                }
                return NO_RESULTS;
        }
@@ -69,6 +77,7 @@ public class SearchManager {
        public List<NameRelationship> findNameRelationships(
                        IIdentifiableEntityServiceConfigurator configurator) {
                if(true){
+                   //if activated again remember to add uuid search results like in other searches
                        return NO_RESULTS;
                }
 
@@ -89,22 +98,48 @@ public class SearchManager {
 
        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();
+                       Reference uuidSearchResult = addUuidSearchResults(configurator, IReferenceService.class);
+            if(uuidSearchResult!=null){
+                records.add(uuidSearchResult);
+            }
+            return records;
                }
                return NO_RESULTS;
        }
 
        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();
+                       AgentBase uuidSearchResult = addUuidSearchResults(configurator, IAgentService.class);
+                       if(uuidSearchResult!=null){
+                           records.add(uuidSearchResult);
+                       }
+            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 configurator the configurator holding the search string
+     * @param service the service to use for searching
+     */
+    private <T extends ICdmBase> T addUuidSearchResults(IIdentifiableEntityServiceConfigurator configurator, Class<? extends IService<T>> service) {
+        String titleSearchString = configurator.getTitleSearchString();
+               try {
+                   UUID uuid = UUID.fromString(titleSearchString);
+                   return CdmStore.getService(service).find(uuid);
+               } catch (IllegalArgumentException e) {
+                   //search string was no UUID
+               }
+               return null;
+    }
+
        @SuppressWarnings("unchecked")
        public List<TeamOrPersonBase> findTeamOrPersons(IIdentifiableEntityServiceConfigurator configurator){
-               configurator.setClazz(TeamOrPersonBase.class);
-               return (List)findAgents(configurator);
+           configurator.setClazz(TeamOrPersonBase.class);
+           return (List)findAgents(configurator);
        }
 
        /**
@@ -132,6 +167,7 @@ public class SearchManager {
         * @return
         */
        public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase> configurator, boolean showFieldUnits){
+           List<SpecimenOrObservationBase> records = new ArrayList<SpecimenOrObservationBase>();
                if(configurator.getClazz()==null){
                    if(showFieldUnits){
                        configurator.setClazz(SpecimenOrObservationBase.class);
@@ -142,7 +178,6 @@ public class SearchManager {
                }
                if(configurator.getClazz().equals(SpecimenOrObservationBase.class)){
                    //get FieldUnits + DerivedUnits
-                   List<SpecimenOrObservationBase> records = new ArrayList<SpecimenOrObservationBase>();
                    configurator.setClazz(DerivedUnit.class);
                    int derivedUnitCount = CdmStore.getService(IOccurrenceService.class).countOccurrences(configurator);
                    configurator.setClazz(FieldUnit.class);
@@ -154,26 +189,39 @@ public class SearchManager {
                        configurator.setClazz(FieldUnit.class);
                        records.addAll(CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords());
                    }
-                   return records;
 
                }
-               if(checkLargeResult(CdmStore.getService(IOccurrenceService.class).countOccurrences(configurator))){
-                       return CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
+               else if(checkLargeResult(CdmStore.getService(IOccurrenceService.class).countOccurrences(configurator))){
+                       records = CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
                }
-               return NO_RESULTS;
+               SpecimenOrObservationBase uuidSearchResult = addUuidSearchResults(configurator, IOccurrenceService.class);
+        if(uuidSearchResult!=null){
+            records.add(uuidSearchResult);
+        }
+               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);
+               User uuidSearchResult = addUuidSearchResults(configurator, IUserService.class);
+               if(uuidSearchResult!=null){
+                   records.add(uuidSearchResult);
+               }
+        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);
+               Group uuidSearchResult = addUuidSearchResults(configurator, IGroupService.class);
+               if(uuidSearchResult!=null){
+                   records.add(uuidSearchResult);
+               }
+        return records;
        }
 
 
@@ -193,7 +241,12 @@ public class SearchManager {
 
        public List findTaxa(IIdentifiableEntityServiceConfigurator configurator) {
                if(checkLargeResult(CdmStore.getService(ITaxonService.class).countByTitle(configurator))){
-                       return CdmStore.getService(ITaxonService.class).findByTitle(configurator).getRecords();
+                       List<TaxonBase> records = CdmStore.getService(ITaxonService.class).findByTitle(configurator).getRecords();
+                       TaxonBase uuidSearchResult = addUuidSearchResults(configurator, ITaxonService.class);
+                       if(uuidSearchResult!=null){
+                           records.add(uuidSearchResult);
+                       }
+            return records;
                }
                return NO_RESULTS;
        }