adapt vaadin to new package structure for persistence.permission
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / view / registration / StartRegistrationPresenter.java
index 0b0ecfcb37e456d716f11a5cdfdc302139d154f2..8de2775fd8d0b6bf18cb0a28bffe8e1417195ccd 100644 (file)
@@ -8,9 +8,18 @@
 */
 package eu.etaxonomy.cdm.vaadin.view.registration;
 
+import java.util.Collection;
 import java.util.EnumSet;
-
-import org.springframework.context.event.EventListener;
+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;
 import org.vaadin.viritin.fields.LazyComboBox;
 
 import com.vaadin.server.SystemError;
@@ -19,14 +28,22 @@ import com.vaadin.spring.annotation.SpringComponent;
 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.hibernate.permission.CRUD;
-import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
+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.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;
 import eu.etaxonomy.cdm.vaadin.ui.RegistrationUIDefaults;
-import eu.etaxonomy.cdm.vaadin.util.CdmTitleCacheCaptionGenerator;
 import eu.etaxonomy.cdm.vaadin.view.reference.ReferencePopupEditor;
 import eu.etaxonomy.vaadin.mvp.AbstractEditorPresenter;
 import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
@@ -50,23 +67,69 @@ public class StartRegistrationPresenter extends AbstractEditorPresenter<Registra
 
     private boolean registrationInProgress;
 
+    @Autowired
+    protected CdmFilterablePagingProviderFactory pagingProviderFactory;
+
+    private TypifiedEntityFilterablePagingProvider<Reference> referencePagingProvider;
+
     public StartRegistrationPresenter (){
         super();
     }
 
+
     /**
      * {@inheritDoc}
      */
     @Override
-    public void onPresenterReady() {
-
-        super.onPresenterReady();
-
-        CdmFilterablePagingProvider<Reference, Reference> pagingProvider = new CdmFilterablePagingProvider<Reference, Reference>(
-                getRepo().getReferenceService());
-        CdmTitleCacheCaptionGenerator<Reference> titleCacheGenrator = new CdmTitleCacheCaptionGenerator<Reference>();
+    public void handleViewEntered() {
+
+        super.handleViewEntered();
+
+        referencePagingProvider = pagingProviderFactory.referenceEntityReferencePagingProvider(
+                new ReferenceEllypsisFormatter(ReferenceEllypsisFormatter.LabelType.BIBLIOGRAPHIC),
+                ReferenceEllypsisFormatter.INIT_STRATEGY
+                );
+        TypedEntityCaptionGenerator<Reference> titleCacheGenrator = new TypedEntityCaptionGenerator<Reference>();
+        // 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();
+        }
     }
 
     /**
@@ -81,25 +144,29 @@ public class StartRegistrationPresenter extends AbstractEditorPresenter<Registra
         super.handleViewExit();
     }
 
-    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).ADD")
+
+    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
     public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
 
-        if(getView() == null || getView().getNewPublicationButton() != event.getSourceComponent()){
+        if(getView() == null || getView().getNewPublicationButton() != event.getSource()){
             return;
         }
-        newReferencePopup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
-        EnumSet<ReferenceType> refTypes = RegistrationUIDefaults.REFERENCE_TYPES.clone();
+
+        newReferencePopup = openPopupEditor(ReferencePopupEditor.class, event);
+        EnumSet<ReferenceType> refTypes = RegistrationUIDefaults.PRINTPUB_REFERENCE_TYPES.clone();
         refTypes.remove(ReferenceType.Section);
         newReferencePopup.withReferenceTypes(refTypes);
+
         newReferencePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
         newReferencePopup.withDeleteButton(true);
         newReferencePopup.loadInEditor(null);
+        newReferencePopup.getTypeSelect().setValue(ReferenceType.Article);
     }
 
-    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).REMOVE")
+    @EventBusListenerMethod(filter = EditorActionTypeFilter.Remove.class)
     public void onReferenceEditorActionRemove(ReferenceEditorAction event) {
 
-        if(getView().getRemoveNewPublicationButton() != event.getSourceComponent()){
+        if(getView().getRemoveNewPublicationButton() != event.getSource()){
             return;
         }
         DeleteResult result = getRepo().getReferenceService().delete(newReference);
@@ -120,16 +187,17 @@ public class StartRegistrationPresenter extends AbstractEditorPresenter<Registra
         getView().getNewPublicationLabel().setVisible(false);
     }
 
-    @EventListener
+    @EventBusListenerMethod
     public void onDoneWithPopupEvent(DoneWithPopupEvent event){
+
         if(event.getPopup() == newReferencePopup){
             if(event.getReason() == Reason.SAVE){
 
                 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);
@@ -147,27 +215,29 @@ public class StartRegistrationPresenter extends AbstractEditorPresenter<Registra
         }
     }
 
-    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).ADD")
+    @SuppressWarnings("null")
+    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
     public void onRegistrationEditorActionAdd(RegistrationEditorAction event) {
 
-        if(getView().getContinueButton() != event.getSourceComponent()){
+        if(getView().getContinueButton() != event.getSource()){
             return;
         }
-        Integer referenceId = null;
-        LazyComboBox<Reference> referenceCombobox = getView().getReferenceCombobox();
+
+        UUID referenceUuid = null;
+        LazyComboBox<TypedEntityReference<Reference>> referenceCombobox = getView().getReferenceCombobox();
         referenceCombobox.commit();
         if(newReference != null){
-            referenceId = newReference.getId();
+            referenceUuid = newReference.getUuid();
        // } else if(referenceCombobox.getValue() != null) {
-        } else if ( event.getEntityId() != null) { // HACKED, see view implementation
-            referenceId = event.getEntityId();
+        } else if ( event.getEntityUuid() != null) { // HACKED, see view implementation
+            referenceUuid = event.getEntityUuid();
         }
-        if(referenceId == null){
+        if(referenceUuid == null){
             getView().getContinueButton().setComponentError(new UserError("Can't continue. No Reference is chosen."));
             getView().getContinueButton().setEnabled(false);
         }
         registrationInProgress = true;
-        eventBus.publishEvent(new NavigationEvent(RegistrationWorksetViewBean.NAME, Integer.toString(referenceId)));
+        viewEventBus.publish(EventScope.UI, this, new NavigationEvent(RegistrationWorksetViewBean.NAME, referenceUuid.toString()));
 
     }
 
@@ -185,8 +255,7 @@ public class StartRegistrationPresenter extends AbstractEditorPresenter<Registra
      */
     @Override
     protected void saveBean(RegistrationDTO bean) {
-        // TODO Auto-generated method stub
-
+        // not needed //
     }
 
     /**
@@ -194,8 +263,7 @@ public class StartRegistrationPresenter extends AbstractEditorPresenter<Registra
      */
     @Override
     protected void deleteBean(RegistrationDTO bean) {
-        // TODO Auto-generated method stub
-
+        // not needed //
     }
 
 }