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