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
.EnumSet
;
13 import org
.springframework
.context
.event
.EventListener
;
14 import org
.vaadin
.viritin
.fields
.LazyComboBox
;
16 import com
.vaadin
.server
.SystemError
;
17 import com
.vaadin
.server
.UserError
;
18 import com
.vaadin
.spring
.annotation
.SpringComponent
;
19 import com
.vaadin
.spring
.annotation
.ViewScope
;
21 import eu
.etaxonomy
.cdm
.api
.service
.DeleteResult
;
22 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
23 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceType
;
24 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.CRUD
;
25 import eu
.etaxonomy
.cdm
.service
.CdmFilterablePagingProvider
;
26 import eu
.etaxonomy
.cdm
.vaadin
.event
.ReferenceEditorAction
;
27 import eu
.etaxonomy
.cdm
.vaadin
.event
.RegistrationEditorAction
;
28 import eu
.etaxonomy
.cdm
.vaadin
.ui
.RegistrationUIDefaults
;
29 import eu
.etaxonomy
.cdm
.vaadin
.util
.CdmTitleCacheCaptionGenerator
;
30 import eu
.etaxonomy
.cdm
.vaadin
.view
.reference
.ReferencePopupEditor
;
31 import eu
.etaxonomy
.vaadin
.mvp
.AbstractEditorPresenter
;
32 import eu
.etaxonomy
.vaadin
.ui
.navigation
.NavigationEvent
;
33 import eu
.etaxonomy
.vaadin
.ui
.view
.DoneWithPopupEvent
;
34 import eu
.etaxonomy
.vaadin
.ui
.view
.DoneWithPopupEvent
.Reason
;
37 * @author a.kohlbecker
43 public class StartRegistrationPresenter
extends AbstractEditorPresenter
<RegistrationDTO
, StartRegistrationView
> {
45 private static final long serialVersionUID
= 2283189121081612574L;
47 private ReferencePopupEditor newReferencePopup
;
49 private Reference newReference
;
51 private boolean registrationInProgress
;
53 public StartRegistrationPresenter (){
61 public void onPresenterReady() {
63 super.onPresenterReady();
65 CdmFilterablePagingProvider
<Reference
, Reference
> pagingProvider
= new CdmFilterablePagingProvider
<Reference
, Reference
>(
66 getRepo().getReferenceService());
67 CdmTitleCacheCaptionGenerator
<Reference
> titleCacheGenrator
= new CdmTitleCacheCaptionGenerator
<Reference
>();
68 getView().getReferenceCombobox().setCaptionGenerator(titleCacheGenrator
);
69 getView().getReferenceCombobox().loadFrom(pagingProvider
, pagingProvider
, pagingProvider
.getPageSize());
76 public void handleViewExit() {
77 if(!registrationInProgress
&& newReference
!= null){
78 logger
.info("Deleting newly created Reference due to canceled registration");
79 getRepo().getReferenceService().delete(newReference
);
81 super.handleViewExit();
84 @EventListener(condition
= "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).ADD")
85 public void onReferenceEditorActionAdd(ReferenceEditorAction event
) {
87 if(getView() == null || getView().getNewPublicationButton() != event
.getSourceComponent()){
90 newReferencePopup
= getNavigationManager().showInPopup(ReferencePopupEditor
.class);
91 EnumSet
<ReferenceType
> refTypes
= RegistrationUIDefaults
.REFERENCE_TYPES
.clone();
92 refTypes
.remove(ReferenceType
.Section
);
93 newReferencePopup
.withReferenceTypes(refTypes
);
94 newReferencePopup
.grantToCurrentUser(EnumSet
.of(CRUD
.UPDATE
, CRUD
.DELETE
));
95 newReferencePopup
.withDeleteButton(true);
96 newReferencePopup
.loadInEditor(null);
99 @EventListener(condition
= "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).REMOVE")
100 public void onReferenceEditorActionRemove(ReferenceEditorAction event
) {
102 if(getView().getRemoveNewPublicationButton() != event
.getSourceComponent()){
105 DeleteResult result
= getRepo().getReferenceService().delete(newReference
);
108 for(Exception e
: result
.getExceptions()){
109 message
+= e
.getMessage() + "\n" + e
.getStackTrace().toString() + "\n";
111 getView().getRemoveNewPublicationButton().setComponentError(new SystemError(message
));
114 getView().getReferenceCombobox().setEnabled(false);
116 getView().getRemoveNewPublicationButton().setVisible(false);
118 getView().getNewPublicationButton().setVisible(true);
119 getView().getNewPublicationLabel().setCaption(null);
120 getView().getNewPublicationLabel().setVisible(false);
124 public void onDoneWithPopupEvent(DoneWithPopupEvent event
){
125 if(event
.getPopup() == newReferencePopup
){
126 if(event
.getReason() == Reason
.SAVE
){
128 newReference
= newReferencePopup
.getBean();
130 // TODO the bean contained in the popup editor is not yet updated at this point.
131 // so re reload it using the uuid since new beans will not have an Id at this point.
132 newReference
= getRepo().getReferenceService().find(newReference
.getUuid());
134 getView().getReferenceCombobox().setValue(null); // deselect
135 getView().getReferenceCombobox().setEnabled(false);
137 getView().getContinueButton().setEnabled(true);
139 getView().getNewPublicationButton().setVisible(false);
141 getView().getRemoveNewPublicationButton().setVisible(true);
142 getView().getNewPublicationLabel().setCaption(newReference
.getTitleCache());
143 getView().getNewPublicationLabel().setVisible(true);
146 newReferencePopup
= null;
150 @EventListener(condition
= "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).ADD")
151 public void onRegistrationEditorActionAdd(RegistrationEditorAction event
) {
153 if(getView().getContinueButton() != event
.getSourceComponent()){
156 Integer referenceId
= null;
157 LazyComboBox
<Reference
> referenceCombobox
= getView().getReferenceCombobox();
158 referenceCombobox
.commit();
159 if(newReference
!= null){
160 referenceId
= newReference
.getId();
161 // } else if(referenceCombobox.getValue() != null) {
162 } else if ( event
.getEntityId() != null) { // HACKED, see view implementation
163 referenceId
= event
.getEntityId();
165 if(referenceId
== null){
166 getView().getContinueButton().setComponentError(new UserError("Can't continue. No Reference is chosen."));
167 getView().getContinueButton().setEnabled(false);
169 registrationInProgress
= true;
170 eventBus
.publishEvent(new NavigationEvent(RegistrationWorksetViewBean
.NAME
, Integer
.toString(referenceId
)));
178 protected RegistrationDTO
loadBeanById(Object identifier
) {
187 protected void saveBean(RegistrationDTO bean
) {
188 // TODO Auto-generated method stub
196 protected void deleteBean(RegistrationDTO bean
) {
197 // TODO Auto-generated method stub