Optimize Code #5103
authorPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 9 Sep 2015 11:43:52 +0000 (13:43 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 9 Sep 2015 11:43:52 +0000 (13:43 +0200)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/store/SearchManager.java

index 4025e4197ad193603deba7e8dc1218506301b9f6..e5861f668c07471ec6ac4309da0fb56ac17711ae 100644 (file)
@@ -65,10 +65,7 @@ public class SearchManager {
        public List<TaxonNameBase> findNames(IIdentifiableEntityServiceConfigurator configurator){
                if(checkLargeResult(CdmStore.getService(INameService.class).countByTitle(configurator))){
                        List<TaxonNameBase> records = CdmStore.getService(INameService.class).findByTitle(configurator).getRecords();
        public List<TaxonNameBase> findNames(IIdentifiableEntityServiceConfigurator configurator){
                if(checkLargeResult(CdmStore.getService(INameService.class).countByTitle(configurator))){
                        List<TaxonNameBase> records = CdmStore.getService(INameService.class).findByTitle(configurator).getRecords();
-                       TaxonNameBase uuidSearchResult = addUuidSearchResults(configurator, INameService.class);
-            if(uuidSearchResult!=null){
-                records.add(uuidSearchResult);
-            }
+                       addUuidSearchResults(records, configurator, INameService.class);
             return records;
                }
                return NO_RESULTS;
             return records;
                }
                return NO_RESULTS;
@@ -99,10 +96,7 @@ public class SearchManager {
        public List<Reference> findReferences(IIdentifiableEntityServiceConfigurator configurator){
                if(checkLargeResult(CdmStore.getService(IReferenceService.class).countByTitle(configurator))){
                        List<Reference> records = CdmStore.getService(IReferenceService.class).findByTitle(configurator).getRecords();
        public List<Reference> findReferences(IIdentifiableEntityServiceConfigurator configurator){
                if(checkLargeResult(CdmStore.getService(IReferenceService.class).countByTitle(configurator))){
                        List<Reference> records = CdmStore.getService(IReferenceService.class).findByTitle(configurator).getRecords();
-                       Reference uuidSearchResult = addUuidSearchResults(configurator, IReferenceService.class);
-            if(uuidSearchResult!=null){
-                records.add(uuidSearchResult);
-            }
+                       addUuidSearchResults(records, configurator, IReferenceService.class);
             return records;
                }
                return NO_RESULTS;
             return records;
                }
                return NO_RESULTS;
@@ -111,10 +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();
        public List<AgentBase> findAgents(IIdentifiableEntityServiceConfigurator configurator){
                if(checkLargeResult(CdmStore.getService(IAgentService.class).countByTitle(configurator))){
                        List<AgentBase> records = CdmStore.getService(IAgentService.class).findByTitle(configurator).getRecords();
-                       AgentBase uuidSearchResult = addUuidSearchResults(configurator, IAgentService.class);
-                       if(uuidSearchResult!=null){
-                           records.add(uuidSearchResult);
-                       }
+                       addUuidSearchResults(records, configurator, IAgentService.class);
             return records;
                }
                return NO_RESULTS;
             return records;
                }
                return NO_RESULTS;
@@ -122,18 +113,18 @@ public class SearchManager {
 
     /**
      * Check search string if it is a {@link UUID} and, if <code>true</code>, search for the corresponding entity.
 
     /**
      * 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
      */
      * @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) {
+    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);
         String titleSearchString = configurator.getTitleSearchString();
                try {
                    UUID uuid = UUID.fromString(titleSearchString);
-                   return CdmStore.getService(service).find(uuid);
+                   records.add(CdmStore.getService(service).find(uuid));
                } catch (IllegalArgumentException e) {
                    //search string was no UUID
                }
                } catch (IllegalArgumentException e) {
                    //search string was no UUID
                }
-               return null;
     }
 
        @SuppressWarnings("unchecked")
     }
 
        @SuppressWarnings("unchecked")
@@ -194,10 +185,7 @@ public class SearchManager {
                else if(checkLargeResult(CdmStore.getService(IOccurrenceService.class).countOccurrences(configurator))){
                        records = CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
                }
                else if(checkLargeResult(CdmStore.getService(IOccurrenceService.class).countOccurrences(configurator))){
                        records = CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
                }
-               SpecimenOrObservationBase uuidSearchResult = addUuidSearchResults(configurator, IOccurrenceService.class);
-        if(uuidSearchResult!=null){
-            records.add(uuidSearchResult);
-        }
+               addUuidSearchResults(records, configurator, IOccurrenceService.class);
                return records;
        }
 
                return records;
        }
 
@@ -205,10 +193,7 @@ public class SearchManager {
                String userNameSearchString = sqlizeTitleSearchString(configurator);
                // TODO why are users not identifiable entities?
                List<User> records = CdmStore.getService(IUserService.class).listByUsername(userNameSearchString, null, null, null, null, null, null);
                String userNameSearchString = sqlizeTitleSearchString(configurator);
                // TODO why are users not identifiable entities?
                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);
-               }
+               addUuidSearchResults(records, configurator, IUserService.class);
         return records;
        }
 
         return records;
        }
 
@@ -217,10 +202,7 @@ public class SearchManager {
                String groupNameSearchString = sqlizeTitleSearchString(configurator);
                // TODO why are groups not identifiable entities?
                List<Group> records = CdmStore.getService(IGroupService.class).listByName(groupNameSearchString, null, null, null, null, null, null);
                String groupNameSearchString = sqlizeTitleSearchString(configurator);
                // TODO why are groups not identifiable entities?
                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);
-               }
+               addUuidSearchResults(records, configurator, IGroupService.class);
         return records;
        }
 
         return records;
        }
 
@@ -242,10 +224,7 @@ public class SearchManager {
        public List findTaxa(IIdentifiableEntityServiceConfigurator configurator) {
                if(checkLargeResult(CdmStore.getService(ITaxonService.class).countByTitle(configurator))){
                        List<TaxonBase> records = CdmStore.getService(ITaxonService.class).findByTitle(configurator).getRecords();
        public List findTaxa(IIdentifiableEntityServiceConfigurator configurator) {
                if(checkLargeResult(CdmStore.getService(ITaxonService.class).countByTitle(configurator))){
                        List<TaxonBase> records = CdmStore.getService(ITaxonService.class).findByTitle(configurator).getRecords();
-                       TaxonBase uuidSearchResult = addUuidSearchResults(configurator, ITaxonService.class);
-                       if(uuidSearchResult!=null){
-                           records.add(uuidSearchResult);
-                       }
+                       addUuidSearchResults(records, configurator, ITaxonService.class);
             return records;
                }
                return NO_RESULTS;
             return records;
                }
                return NO_RESULTS;