consistent popup opening, using boundTargetField to identify field to be updated
[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.EnumSet;
12 import java.util.UUID;
13
14 import org.vaadin.spring.events.EventScope;
15 import org.vaadin.spring.events.annotation.EventBusListenerMethod;
16 import org.vaadin.viritin.fields.LazyComboBox;
17
18 import com.vaadin.server.SystemError;
19 import com.vaadin.server.UserError;
20 import com.vaadin.spring.annotation.SpringComponent;
21 import com.vaadin.spring.annotation.ViewScope;
22
23 import eu.etaxonomy.cdm.api.service.DeleteResult;
24 import eu.etaxonomy.cdm.api.service.dto.RegistrationDTO;
25 import eu.etaxonomy.cdm.model.reference.Reference;
26 import eu.etaxonomy.cdm.model.reference.ReferenceType;
27 import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
28 import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
29 import eu.etaxonomy.cdm.vaadin.event.EditorActionTypeFilter;
30 import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
31 import eu.etaxonomy.cdm.vaadin.event.RegistrationEditorAction;
32 import eu.etaxonomy.cdm.vaadin.ui.RegistrationUIDefaults;
33 import eu.etaxonomy.cdm.vaadin.util.CdmTitleCacheCaptionGenerator;
34 import eu.etaxonomy.cdm.vaadin.view.reference.ReferencePopupEditor;
35 import eu.etaxonomy.vaadin.mvp.AbstractEditorPresenter;
36 import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
37 import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent;
38 import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent.Reason;
39
40 /**
41 * @author a.kohlbecker
42 * @since Jul 11, 2017
43 *
44 */
45 @SpringComponent
46 @ViewScope
47 public class StartRegistrationPresenter extends AbstractEditorPresenter<RegistrationDTO, StartRegistrationView> {
48
49 private static final long serialVersionUID = 2283189121081612574L;
50
51 private ReferencePopupEditor newReferencePopup;
52
53 private Reference newReference;
54
55 private boolean registrationInProgress;
56
57 public StartRegistrationPresenter (){
58 super();
59 }
60
61
62 /**
63 * {@inheritDoc}
64 */
65 @Override
66 public void onPresenterReady() {
67
68 super.onPresenterReady();
69
70 CdmFilterablePagingProvider<Reference, Reference> pagingProvider = new CdmFilterablePagingProvider<Reference, Reference>(
71 getRepo().getReferenceService());
72 CdmTitleCacheCaptionGenerator<Reference> titleCacheGenrator = new CdmTitleCacheCaptionGenerator<Reference>();
73 getView().getReferenceCombobox().setCaptionGenerator(titleCacheGenrator);
74 getView().getReferenceCombobox().loadFrom(pagingProvider, pagingProvider, pagingProvider.getPageSize());
75 }
76
77 /**
78 * {@inheritDoc}
79 */
80 @Override
81 public void handleViewExit() {
82 if(!registrationInProgress && newReference != null){
83 logger.info("Deleting newly created Reference due to canceled registration");
84 getRepo().getReferenceService().delete(newReference);
85 }
86 super.handleViewExit();
87 }
88
89
90 @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
91 public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
92
93 if(getView() == null || getView().getNewPublicationButton() != event.getSource()){
94 return;
95 }
96
97 newReferencePopup = openPopupEditor(ReferencePopupEditor.class, event);
98 EnumSet<ReferenceType> refTypes = RegistrationUIDefaults.PRINTPUB_REFERENCE_TYPES.clone();
99 refTypes.remove(ReferenceType.Section);
100 newReferencePopup.withReferenceTypes(refTypes);
101 newReferencePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
102 newReferencePopup.withDeleteButton(true);
103 newReferencePopup.loadInEditor(null);
104 }
105
106 @EventBusListenerMethod(filter = EditorActionTypeFilter.Remove.class)
107 public void onReferenceEditorActionRemove(ReferenceEditorAction event) {
108
109 if(getView().getRemoveNewPublicationButton() != event.getSource()){
110 return;
111 }
112 DeleteResult result = getRepo().getReferenceService().delete(newReference);
113 if(!result.isOk()){
114 String message = "";
115 for(Exception e : result.getExceptions()){
116 message += e.getMessage() + "\n" + e.getStackTrace().toString() + "\n";
117 }
118 getView().getRemoveNewPublicationButton().setComponentError(new SystemError(message));
119 }
120
121 getView().getReferenceCombobox().setEnabled(false);
122
123 getView().getRemoveNewPublicationButton().setVisible(false);
124
125 getView().getNewPublicationButton().setVisible(true);
126 getView().getNewPublicationLabel().setCaption(null);
127 getView().getNewPublicationLabel().setVisible(false);
128 }
129
130 @EventBusListenerMethod
131 public void onDoneWithPopupEvent(DoneWithPopupEvent event){
132
133 if(event.getPopup() == newReferencePopup){
134 if(event.getReason() == Reason.SAVE){
135
136 newReference = newReferencePopup.getBean();
137
138 // TODO the bean contained in the popup editor is not yet updated at this point.
139 // so re reload it using the uuid since new beans will not have an Id at this point.
140 newReference = getRepo().getReferenceService().find(newReference.getUuid());
141
142 getView().getReferenceCombobox().setValue(null); // deselect
143 getView().getReferenceCombobox().setEnabled(false);
144
145 getView().getContinueButton().setEnabled(true);
146
147 getView().getNewPublicationButton().setVisible(false);
148
149 getView().getRemoveNewPublicationButton().setVisible(true);
150 getView().getNewPublicationLabel().setCaption(newReference.getTitleCache());
151 getView().getNewPublicationLabel().setVisible(true);
152 }
153
154 newReferencePopup = null;
155 }
156 }
157
158 @SuppressWarnings("null")
159 @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
160 public void onRegistrationEditorActionAdd(RegistrationEditorAction event) {
161
162 if(getView().getContinueButton() != event.getSource()){
163 return;
164 }
165
166 UUID referenceUuid = null;
167 LazyComboBox<Reference> referenceCombobox = getView().getReferenceCombobox();
168 referenceCombobox.commit();
169 if(newReference != null){
170 referenceUuid = newReference.getUuid();
171 // } else if(referenceCombobox.getValue() != null) {
172 } else if ( event.getEntityUuid() != null) { // HACKED, see view implementation
173 referenceUuid = event.getEntityUuid();
174 }
175 if(referenceUuid == null){
176 getView().getContinueButton().setComponentError(new UserError("Can't continue. No Reference is chosen."));
177 getView().getContinueButton().setEnabled(false);
178 }
179 registrationInProgress = true;
180 viewEventBus.publish(EventScope.UI, this, new NavigationEvent(RegistrationWorksetViewBean.NAME, referenceUuid.toString()));
181
182 }
183
184 /**
185 * {@inheritDoc}
186 */
187 @Override
188 protected RegistrationDTO loadBeanById(Object identifier) {
189 // not needed //
190 return null;
191 }
192
193 /**
194 * {@inheritDoc}
195 */
196 @Override
197 protected void saveBean(RegistrationDTO bean) {
198 // TODO Auto-generated method stub
199
200 }
201
202 /**
203 * {@inheritDoc}
204 */
205 @Override
206 protected void deleteBean(RegistrationDTO bean) {
207 // TODO Auto-generated method stub
208
209 }
210
211 }