adapt vaadin to new package structure for persistence.permission
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / view / registration / StartRegistrationPresenter.java
index 696b5cf74ad47206a6559331b30be6a051169fdb..8de2775fd8d0b6bf18cb0a28bffe8e1417195ccd 100644 (file)
@@ -8,9 +8,15 @@
 */
 package eu.etaxonomy.cdm.vaadin.view.registration;
 
+import java.util.Collection;
 import java.util.EnumSet;
+import java.util.Set;
 import java.util.UUID;
+import java.util.stream.Collectors;
 
+import org.hibernate.criterion.Criterion;
+import org.hibernate.criterion.Restrictions;
+import org.joda.time.DateTime;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.vaadin.spring.events.EventScope;
 import org.vaadin.spring.events.annotation.EventBusListenerMethod;
@@ -23,15 +29,17 @@ import com.vaadin.spring.annotation.ViewScope;
 
 import eu.etaxonomy.cdm.api.service.DeleteResult;
 import eu.etaxonomy.cdm.api.service.dto.RegistrationDTO;
+import eu.etaxonomy.cdm.format.reference.ReferenceEllypsisFormatter;
+import eu.etaxonomy.cdm.model.permission.CRUD;
 import eu.etaxonomy.cdm.model.reference.Reference;
 import eu.etaxonomy.cdm.model.reference.ReferenceType;
-import eu.etaxonomy.cdm.persistence.dao.common.Restriction;
-import eu.etaxonomy.cdm.persistence.dao.common.Restriction.Operator;
-import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
+import eu.etaxonomy.cdm.persistence.permission.CdmAuthority;
+import eu.etaxonomy.cdm.persistence.permission.Operation;
+import eu.etaxonomy.cdm.persistence.query.MatchMode;
 import eu.etaxonomy.cdm.ref.TypedEntityReference;
 import eu.etaxonomy.cdm.service.CdmFilterablePagingProviderFactory;
-import eu.etaxonomy.cdm.service.ReferenceLabelProvider;
 import eu.etaxonomy.cdm.service.TypifiedEntityFilterablePagingProvider;
+import eu.etaxonomy.cdm.service.UserHelperAccess;
 import eu.etaxonomy.cdm.vaadin.event.EditorActionTypeFilter;
 import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
 import eu.etaxonomy.cdm.vaadin.event.RegistrationEditorAction;
@@ -62,6 +70,8 @@ public class StartRegistrationPresenter extends AbstractEditorPresenter<Registra
     @Autowired
     protected CdmFilterablePagingProviderFactory pagingProviderFactory;
 
+    private TypifiedEntityFilterablePagingProvider<Reference> referencePagingProvider;
+
     public StartRegistrationPresenter (){
         super();
     }
@@ -75,15 +85,51 @@ public class StartRegistrationPresenter extends AbstractEditorPresenter<Registra
 
         super.handleViewEntered();
 
-        TypifiedEntityFilterablePagingProvider<Reference> pagingProvider =
-                pagingProviderFactory.referenceEntityReferencePagingProvider(
-                        new ReferenceLabelProvider(ReferenceLabelProvider.LabelType.BIBLIOGRAPHIC),
-                        ReferenceLabelProvider.INIT_STRATEGY
-                        );
+        referencePagingProvider = pagingProviderFactory.referenceEntityReferencePagingProvider(
+                new ReferenceEllypsisFormatter(ReferenceEllypsisFormatter.LabelType.BIBLIOGRAPHIC),
+                ReferenceEllypsisFormatter.INIT_STRATEGY
+                );
         TypedEntityCaptionGenerator<Reference> titleCacheGenrator = new TypedEntityCaptionGenerator<Reference>();
-        pagingProvider.addRestriction(new Restriction("type", Operator.AND_NOT, null, ReferenceType.Section, ReferenceType.Journal, ReferenceType.PrintSeries));
+        // referencePagingProvider.addRestriction(new Restriction("type", Operator.AND_NOT, null, ReferenceType.Section, ReferenceType.Journal, ReferenceType.PrintSeries));
+        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);
+                }
+            }
+        }
+        referencePagingProvider.addCriterion(criterion);
         getView().getReferenceCombobox().setCaptionGenerator(titleCacheGenrator);
-        getView().getReferenceCombobox().loadFrom(pagingProvider, pagingProvider, pagingProvider.getPageSize());
+        getView().getReferenceCombobox().loadFrom(referencePagingProvider, referencePagingProvider, referencePagingProvider.getPageSize());
+    }
+
+    /**
+     * @param value
+     * @return
+     */
+    public void updateReferenceSearchMode(MatchMode value) {
+        if(referencePagingProvider != null && value != null){
+            referencePagingProvider.setMatchMode(value);
+            getView().getReferenceCombobox().refresh();
+        }
     }
 
     /**
@@ -150,8 +196,8 @@ public class StartRegistrationPresenter extends AbstractEditorPresenter<Registra
                 newReference = newReferencePopup.getBean();
 
                 // TODO the bean contained in the popup editor is not yet updated at this point.
-                //      so re reload it using the uuid since new beans will not have an Id at this point.
-                newReference = getRepo().getReferenceService().find(newReference.getUuid());
+                //      so we reload it using the uuid since new beans will not have an Id at this point.
+                newReference = getRepo().getReferenceService().load(newReference.getUuid());
 
                 getView().getReferenceCombobox().setValue(null);  // deselect
                 getView().getReferenceCombobox().setEnabled(false);