ref #7833 registration start view excludes unpublished references
[cdm-vaadin.git] / src / main / java / eu / etaxonomy / cdm / vaadin / view / registration / StartRegistrationPresenter.java
1 /**
2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.cdm.vaadin.view.registration;
10
11 import java.util.Collection;
12 import java.util.EnumSet;
13 import java.util.Set;
14 import java.util.UUID;
15 import java.util.stream.Collectors;
16
17 import org.hibernate.criterion.Criterion;
18 import org.hibernate.criterion.Restrictions;
19 import org.joda.time.DateTime;
20 import org.springframework.beans.factory.annotation.Autowired;
21 import org.vaadin.spring.events.EventScope;
22 import org.vaadin.spring.events.annotation.EventBusListenerMethod;
23 import org.vaadin.viritin.fields.LazyComboBox;
24
25 import com.vaadin.server.SystemError;
26 import com.vaadin.server.UserError;
27 import com.vaadin.spring.annotation.SpringComponent;
28 import com.vaadin.spring.annotation.ViewScope;
29
30 import eu.etaxonomy.cdm.api.service.DeleteResult;
31 import eu.etaxonomy.cdm.api.service.dto.RegistrationDTO;
32 import eu.etaxonomy.cdm.format.ReferenceEllypsisFormatter;
33 import eu.etaxonomy.cdm.model.reference.Reference;
34 import eu.etaxonomy.cdm.model.reference.ReferenceType;
35 import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
36 import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmAuthority;
37 import eu.etaxonomy.cdm.persistence.hibernate.permission.Operation;
38 import eu.etaxonomy.cdm.persistence.query.MatchMode;
39 import eu.etaxonomy.cdm.ref.TypedEntityReference;
40 import eu.etaxonomy.cdm.service.CdmFilterablePagingProviderFactory;
41 import eu.etaxonomy.cdm.service.TypifiedEntityFilterablePagingProvider;
42 import eu.etaxonomy.cdm.service.UserHelperAccess;
43 import eu.etaxonomy.cdm.vaadin.event.EditorActionTypeFilter;
44 import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
45 import eu.etaxonomy.cdm.vaadin.event.RegistrationEditorAction;
46 import eu.etaxonomy.cdm.vaadin.ui.RegistrationUIDefaults;
47 import eu.etaxonomy.cdm.vaadin.view.reference.ReferencePopupEditor;
48 import eu.etaxonomy.vaadin.mvp.AbstractEditorPresenter;
49 import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
50 import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent;
51 import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent.Reason;
52
53 /**
54 * @author a.kohlbecker
55 * @since Jul 11, 2017
56 *
57 */
58 @SpringComponent
59 @ViewScope
60 public class StartRegistrationPresenter extends AbstractEditorPresenter<RegistrationDTO, StartRegistrationView> {
61
62 private static final long serialVersionUID = 2283189121081612574L;
63
64 private ReferencePopupEditor newReferencePopup;
65
66 private Reference newReference;
67
68 private boolean registrationInProgress;
69
70 @Autowired
71 protected CdmFilterablePagingProviderFactory pagingProviderFactory;
72
73 private TypifiedEntityFilterablePagingProvider<Reference> referencePagingProvider;
74
75 public StartRegistrationPresenter (){
76 super();
77 }
78
79
80 /**
81 * {@inheritDoc}
82 */
83 @Override
84 public void handleViewEntered() {
85
86 super.handleViewEntered();
87
88 referencePagingProvider = pagingProviderFactory.referenceEntityReferencePagingProvider(
89 new ReferenceEllypsisFormatter(ReferenceEllypsisFormatter.LabelType.BIBLIOGRAPHIC),
90 ReferenceEllypsisFormatter.INIT_STRATEGY
91 );
92 TypedEntityCaptionGenerator<Reference> titleCacheGenrator = new TypedEntityCaptionGenerator<Reference>();
93 // referencePagingProvider.addRestriction(new Restriction("type", Operator.AND_NOT, null, ReferenceType.Section, ReferenceType.Journal, ReferenceType.PrintSeries));
94 Criterion criterion = Restrictions.not(Restrictions.or(Restrictions.in("type", new ReferenceType[]{ReferenceType.Section, ReferenceType.Journal, ReferenceType.PrintSeries})));
95
96 if(!UserHelperAccess.userHelper().userIsAdmin()){
97 Collection<CdmAuthority> referencePermissions = UserHelperAccess.userHelper().findUserPermissions(Reference.class, Operation.UPDATE);
98 boolean generalUpdatePermission = referencePermissions.stream().anyMatch(p -> p.getTargetUUID() == null);
99 if(!generalUpdatePermission){
100 // exclude unpublished publications
101 DateTime nowLocal = new DateTime();
102 String dateString = nowLocal.toString("yyyyMMdd");
103 logger.debug("dateString:" + dateString);
104 Criterion pulishedOnly = Restrictions.or(
105 Restrictions.and(Restrictions.isNull("datePublished.start"), Restrictions.isNull("datePublished.end"), Restrictions.isNull("datePublished.freeText")),
106 Restrictions.and(Restrictions.isNotNull("datePublished.start"), Restrictions.sqlRestriction("datePublished_start < " + dateString)),
107 Restrictions.and(Restrictions.isNull("datePublished.start"), Restrictions.isNotNull("datePublished.end"), Restrictions.sqlRestriction("datePublished_end < " + dateString))
108 );
109 // restrict by allowed reference uuids
110 Set<UUID> allowedUuids = referencePermissions.stream().filter(p -> p.getTargetUUID() != null).map(CdmAuthority::getTargetUUID).collect(Collectors.toSet());
111 Criterion uuidRestriction = Restrictions.in("uuid", allowedUuids);
112 criterion = Restrictions.and(criterion, Restrictions.or(pulishedOnly, uuidRestriction));
113 }
114 }
115 referencePagingProvider.addCriterion(criterion);
116 getView().getReferenceCombobox().setCaptionGenerator(titleCacheGenrator);
117 getView().getReferenceCombobox().loadFrom(referencePagingProvider, referencePagingProvider, referencePagingProvider.getPageSize());
118 }
119
120 /**
121 * @param value
122 * @return
123 */
124 public void updateReferenceSearchMode(MatchMode value) {
125 if(referencePagingProvider != null && value != null){
126 referencePagingProvider.setMatchMode(value);
127 getView().getReferenceCombobox().refresh();
128 }
129 }
130
131 /**
132 * {@inheritDoc}
133 */
134 @Override
135 public void handleViewExit() {
136 if(!registrationInProgress && newReference != null){
137 logger.info("Deleting newly created Reference due to canceled registration");
138 getRepo().getReferenceService().delete(newReference);
139 }
140 super.handleViewExit();
141 }
142
143
144 @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
145 public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
146
147 if(getView() == null || getView().getNewPublicationButton() != event.getSource()){
148 return;
149 }
150
151 newReferencePopup = openPopupEditor(ReferencePopupEditor.class, event);
152 EnumSet<ReferenceType> refTypes = RegistrationUIDefaults.PRINTPUB_REFERENCE_TYPES.clone();
153 refTypes.remove(ReferenceType.Section);
154 newReferencePopup.withReferenceTypes(refTypes);
155
156 newReferencePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
157 newReferencePopup.withDeleteButton(true);
158 newReferencePopup.loadInEditor(null);
159 newReferencePopup.getTypeSelect().setValue(ReferenceType.Article);
160 }
161
162 @EventBusListenerMethod(filter = EditorActionTypeFilter.Remove.class)
163 public void onReferenceEditorActionRemove(ReferenceEditorAction event) {
164
165 if(getView().getRemoveNewPublicationButton() != event.getSource()){
166 return;
167 }
168 DeleteResult result = getRepo().getReferenceService().delete(newReference);
169 if(!result.isOk()){
170 String message = "";
171 for(Exception e : result.getExceptions()){
172 message += e.getMessage() + "\n" + e.getStackTrace().toString() + "\n";
173 }
174 getView().getRemoveNewPublicationButton().setComponentError(new SystemError(message));
175 }
176
177 getView().getReferenceCombobox().setEnabled(false);
178
179 getView().getRemoveNewPublicationButton().setVisible(false);
180
181 getView().getNewPublicationButton().setVisible(true);
182 getView().getNewPublicationLabel().setCaption(null);
183 getView().getNewPublicationLabel().setVisible(false);
184 }
185
186 @EventBusListenerMethod
187 public void onDoneWithPopupEvent(DoneWithPopupEvent event){
188
189 if(event.getPopup() == newReferencePopup){
190 if(event.getReason() == Reason.SAVE){
191
192 newReference = newReferencePopup.getBean();
193
194 // TODO the bean contained in the popup editor is not yet updated at this point.
195 // so re reload it using the uuid since new beans will not have an Id at this point.
196 newReference = getRepo().getReferenceService().find(newReference.getUuid());
197
198 getView().getReferenceCombobox().setValue(null); // deselect
199 getView().getReferenceCombobox().setEnabled(false);
200
201 getView().getContinueButton().setEnabled(true);
202
203 getView().getNewPublicationButton().setVisible(false);
204
205 getView().getRemoveNewPublicationButton().setVisible(true);
206 getView().getNewPublicationLabel().setCaption(newReference.getTitleCache());
207 getView().getNewPublicationLabel().setVisible(true);
208 }
209
210 newReferencePopup = null;
211 }
212 }
213
214 @SuppressWarnings("null")
215 @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
216 public void onRegistrationEditorActionAdd(RegistrationEditorAction event) {
217
218 if(getView().getContinueButton() != event.getSource()){
219 return;
220 }
221
222 UUID referenceUuid = null;
223 LazyComboBox<TypedEntityReference<Reference>> referenceCombobox = getView().getReferenceCombobox();
224 referenceCombobox.commit();
225 if(newReference != null){
226 referenceUuid = newReference.getUuid();
227 // } else if(referenceCombobox.getValue() != null) {
228 } else if ( event.getEntityUuid() != null) { // HACKED, see view implementation
229 referenceUuid = event.getEntityUuid();
230 }
231 if(referenceUuid == null){
232 getView().getContinueButton().setComponentError(new UserError("Can't continue. No Reference is chosen."));
233 getView().getContinueButton().setEnabled(false);
234 }
235 registrationInProgress = true;
236 viewEventBus.publish(EventScope.UI, this, new NavigationEvent(RegistrationWorksetViewBean.NAME, referenceUuid.toString()));
237
238 }
239
240 /**
241 * {@inheritDoc}
242 */
243 @Override
244 protected RegistrationDTO loadBeanById(Object identifier) {
245 // not needed //
246 return null;
247 }
248
249 /**
250 * {@inheritDoc}
251 */
252 @Override
253 protected void saveBean(RegistrationDTO bean) {
254 // not needed //
255 }
256
257 /**
258 * {@inheritDoc}
259 */
260 @Override
261 protected void deleteBean(RegistrationDTO bean) {
262 // not needed //
263 }
264
265 }