2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.vaadin
.view
.registration
;
11 import java
.util
.Collection
;
12 import java
.util
.EnumSet
;
14 import java
.util
.UUID
;
15 import java
.util
.stream
.Collectors
;
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
;
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
;
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
;
54 * @author a.kohlbecker
60 public class StartRegistrationPresenter
extends AbstractEditorPresenter
<RegistrationDTO
, StartRegistrationView
> {
62 private static final long serialVersionUID
= 2283189121081612574L;
64 private ReferencePopupEditor newReferencePopup
;
66 private Reference newReference
;
68 private boolean registrationInProgress
;
71 protected CdmFilterablePagingProviderFactory pagingProviderFactory
;
73 private TypifiedEntityFilterablePagingProvider
<Reference
> referencePagingProvider
;
75 public StartRegistrationPresenter (){
84 public void handleViewEntered() {
86 super.handleViewEntered();
88 referencePagingProvider
= pagingProviderFactory
.referenceEntityReferencePagingProvider(
89 new ReferenceEllypsisFormatter(ReferenceEllypsisFormatter
.LabelType
.BIBLIOGRAPHIC
),
90 ReferenceEllypsisFormatter
.INIT_STRATEGY
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
})));
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
))
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
));
115 referencePagingProvider
.addCriterion(criterion
);
116 getView().getReferenceCombobox().setCaptionGenerator(titleCacheGenrator
);
117 getView().getReferenceCombobox().loadFrom(referencePagingProvider
, referencePagingProvider
, referencePagingProvider
.getPageSize());
124 public void updateReferenceSearchMode(MatchMode value
) {
125 if(referencePagingProvider
!= null && value
!= null){
126 referencePagingProvider
.setMatchMode(value
);
127 getView().getReferenceCombobox().refresh();
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
);
140 super.handleViewExit();
144 @EventBusListenerMethod(filter
= EditorActionTypeFilter
.Add
.class)
145 public void onReferenceEditorActionAdd(ReferenceEditorAction event
) {
147 if(getView() == null || getView().getNewPublicationButton() != event
.getSource()){
151 newReferencePopup
= openPopupEditor(ReferencePopupEditor
.class, event
);
152 EnumSet
<ReferenceType
> refTypes
= RegistrationUIDefaults
.PRINTPUB_REFERENCE_TYPES
.clone();
153 refTypes
.remove(ReferenceType
.Section
);
154 newReferencePopup
.withReferenceTypes(refTypes
);
156 newReferencePopup
.grantToCurrentUser(EnumSet
.of(CRUD
.UPDATE
, CRUD
.DELETE
));
157 newReferencePopup
.withDeleteButton(true);
158 newReferencePopup
.loadInEditor(null);
159 newReferencePopup
.getTypeSelect().setValue(ReferenceType
.Article
);
162 @EventBusListenerMethod(filter
= EditorActionTypeFilter
.Remove
.class)
163 public void onReferenceEditorActionRemove(ReferenceEditorAction event
) {
165 if(getView().getRemoveNewPublicationButton() != event
.getSource()){
168 DeleteResult result
= getRepo().getReferenceService().delete(newReference
);
171 for(Exception e
: result
.getExceptions()){
172 message
+= e
.getMessage() + "\n" + e
.getStackTrace().toString() + "\n";
174 getView().getRemoveNewPublicationButton().setComponentError(new SystemError(message
));
177 getView().getReferenceCombobox().setEnabled(false);
179 getView().getRemoveNewPublicationButton().setVisible(false);
181 getView().getNewPublicationButton().setVisible(true);
182 getView().getNewPublicationLabel().setCaption(null);
183 getView().getNewPublicationLabel().setVisible(false);
186 @EventBusListenerMethod
187 public void onDoneWithPopupEvent(DoneWithPopupEvent event
){
189 if(event
.getPopup() == newReferencePopup
){
190 if(event
.getReason() == Reason
.SAVE
){
192 newReference
= newReferencePopup
.getBean();
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());
198 getView().getReferenceCombobox().setValue(null); // deselect
199 getView().getReferenceCombobox().setEnabled(false);
201 getView().getContinueButton().setEnabled(true);
203 getView().getNewPublicationButton().setVisible(false);
205 getView().getRemoveNewPublicationButton().setVisible(true);
206 getView().getNewPublicationLabel().setCaption(newReference
.getTitleCache());
207 getView().getNewPublicationLabel().setVisible(true);
210 newReferencePopup
= null;
214 @SuppressWarnings("null")
215 @EventBusListenerMethod(filter
= EditorActionTypeFilter
.Add
.class)
216 public void onRegistrationEditorActionAdd(RegistrationEditorAction event
) {
218 if(getView().getContinueButton() != event
.getSource()){
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();
231 if(referenceUuid
== null){
232 getView().getContinueButton().setComponentError(new UserError("Can't continue. No Reference is chosen."));
233 getView().getContinueButton().setEnabled(false);
235 registrationInProgress
= true;
236 viewEventBus
.publish(EventScope
.UI
, this, new NavigationEvent(RegistrationWorksetViewBean
.NAME
, referenceUuid
.toString()));
244 protected RegistrationDTO
loadBeanById(Object identifier
) {
253 protected void saveBean(RegistrationDTO bean
) {
261 protected void deleteBean(RegistrationDTO bean
) {