ref #10373 remove all datePublished is null or-restriction and do some refactoring...
authorAndreas Müller <a.mueller@bgbm.org>
Tue, 23 Jan 2024 14:29:47 +0000 (15:29 +0100)
committerAndreas Müller <a.mueller@bgbm.org>
Tue, 23 Jan 2024 14:29:47 +0000 (15:29 +0100)
src/main/java/eu/etaxonomy/cdm/service/CdmFilterablePagingProvider.java
src/main/java/eu/etaxonomy/cdm/vaadin/view/registration/StartRegistrationPresenter.java

index 3466000ce3753b3f2ccd3c64cc07e900c616ea5d..e166d5390b883b0cc781030b2e1274bc28f827eb 100644 (file)
@@ -146,40 +146,21 @@ public class CdmFilterablePagingProvider<T extends IdentifiableEntity, V extends
 
         checkNotMixed();
 
-        Pager<V> page;
-
         clearSession(); // clear the session from remains of previous service calls, see issue #7559
+        long count = 0;
         if(!restrictions.isEmpty()){
             // LogUtils.setLevel("org.hibernate.SQL", Level.TRACE);
             List<Restriction<?>> preparedRestrictions = prepareRestrictions(filter, matchMode);
-            page = service.findByTitleWithRestrictions(
-                    type,
-                    filter,
-                    matchMode,
-                    preparedRestrictions,
-                    1,
-                    0,
-                    null,
-                    null
-                  );
+            count = service.countByTitleWithRestrictions(type, filter, matchMode, preparedRestrictions);
         } else {
-            page = service.findByTitle(
-                    type,
-                    filter,
-                    matchMode,
-                    criteria,
-                    1,
-                    0,
-                    null,
-                    null
-                  );
+            count = service.countByTitle(type, filter, matchMode, criteria);
         }
 
 
         if(logger.isTraceEnabled()){
-            logger.trace("size() -  count: " + page.getCount().intValue());
+            logger.trace("size() -  count: " + count);
         }
-        return page.getCount().intValue();
+        return Long.valueOf(count).intValue();
     }
 
     /**
index e257d40737ec74147c98d947b97dcab08e99910e..67f59f56501caad168f0f74e26304517067a5742 100644 (file)
@@ -92,34 +92,48 @@ public class StartRegistrationPresenter
                 );
         TypedEntityCaptionGenerator<Reference> titleCacheGenrator = new TypedEntityCaptionGenerator<>();
         // referencePagingProvider.addRestriction(new Restriction("type", Operator.AND_NOT, null, ReferenceType.Section, ReferenceType.Journal, ReferenceType.PrintSeries));
+        Criterion criterion = getReferenceFilterCriterion();
+        referencePagingProvider.addCriterion(criterion);
+        getView().getReferenceCombobox().setCaptionGenerator(titleCacheGenrator);
+        getView().getReferenceCombobox().loadFrom(referencePagingProvider, referencePagingProvider, referencePagingProvider.getPageSize());
+    }
+
+    private Criterion getReferenceFilterCriterion() {
+
+        //never use sections, journals and print series
         Criterion criterion = Restrictions.not(Restrictions.or(Restrictions.in("type", new ReferenceType[]{ReferenceType.Section, ReferenceType.Journal, ReferenceType.PrintSeries})));
 
         if(!UserHelperAccess.userHelper().userIsAdmin()){
             Collection<CdmAuthority> referencePermissions = UserHelperAccess.userHelper().findUserPermissions(Reference.class, Operation.UPDATE);
             boolean generalUpdatePermission = referencePermissions.stream().anyMatch(p -> p.getTargetUUID() == null);
             if(!generalUpdatePermission){
-                // exclude unpublished publications
-                DateTime nowLocal = new DateTime();
-                String dateString = nowLocal.toString("yyyyMMdd");
-                logger.debug("dateString:" + dateString);
-                Criterion pulishedOnly = Restrictions.or(
-                        Restrictions.and(Restrictions.isNull("datePublished.start"), Restrictions.isNull("datePublished.end"), Restrictions.isNull("datePublished.freeText")),
-                        Restrictions.and(Restrictions.isNotNull("datePublished.start"), Restrictions.sqlRestriction("datePublished_start < " + dateString)),
-                        Restrictions.and(Restrictions.isNull("datePublished.start"), Restrictions.isNotNull("datePublished.end"), Restrictions.sqlRestriction("datePublished_end < " + dateString))
-                        );
-                // restrict by allowed reference uuids
-                Set<UUID> allowedUuids = referencePermissions.stream().filter(p -> p.getTargetUUID() != null).map(CdmAuthority::getTargetUUID).collect(Collectors.toSet());
-                if(!allowedUuids.isEmpty()){
-                    Criterion uuidRestriction = Restrictions.in("uuid", allowedUuids);
-                    criterion = Restrictions.and(criterion, Restrictions.or(pulishedOnly, uuidRestriction));
-                } else {
-                    criterion = Restrictions.and(criterion, pulishedOnly);
-                }
+                criterion = addUuidAndPublicationDateBasedFilter(criterion, referencePermissions);
             }
         }
-        referencePagingProvider.addCriterion(criterion);
-        getView().getReferenceCombobox().setCaptionGenerator(titleCacheGenrator);
-        getView().getReferenceCombobox().loadFrom(referencePagingProvider, referencePagingProvider, referencePagingProvider.getPageSize());
+        return criterion;
+    }
+
+    private Criterion addUuidAndPublicationDateBasedFilter(Criterion criterion,
+            Collection<CdmAuthority> referencePermissions) {
+
+        // exclude unpublished publications
+        DateTime nowLocal = new DateTime();
+        String dateString = nowLocal.toString("yyyyMMdd");
+        logger.debug("dateString:" + dateString);
+        Criterion pulishedOnlyCriterion = Restrictions.or(
+                Restrictions.and(Restrictions.isNotNull("datePublished.start"), Restrictions.sqlRestriction("datePublished_start < " + dateString)),
+                Restrictions.and(Restrictions.isNull("datePublished.start"), Restrictions.isNotNull("datePublished.end"), Restrictions.sqlRestriction("datePublished_end < " + dateString))
+                );
+
+        // restrict by allowed reference uuids
+        Set<UUID> allowedUuids = referencePermissions.stream().filter(p -> p.getTargetUUID() != null).map(CdmAuthority::getTargetUUID).collect(Collectors.toSet());
+        if(!allowedUuids.isEmpty()){
+            Criterion uuidRestriction = Restrictions.in("uuid", allowedUuids);
+            criterion = Restrictions.and(criterion, Restrictions.or(pulishedOnlyCriterion, uuidRestriction));
+        } else {
+            criterion = Restrictions.and(criterion, pulishedOnlyCriterion);
+        }
+        return criterion;
     }
 
     public void updateReferenceSearchMode(MatchMode value) {