fix #7172 default ReferenceTypes for the registrationUI and disallowing section in...
[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
13 import org.springframework.context.event.EventListener;
14 import org.vaadin.viritin.fields.LazyComboBox;
15
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;
20
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;
35
36 /**
37 * @author a.kohlbecker
38 * @since Jul 11, 2017
39 *
40 */
41 @SpringComponent
42 @ViewScope
43 public class StartRegistrationPresenter extends AbstractEditorPresenter<RegistrationDTO, StartRegistrationView> {
44
45 private static final long serialVersionUID = 2283189121081612574L;
46
47 private ReferencePopupEditor newReferencePopup;
48
49 private Reference newReference;
50
51 private boolean registrationInProgress;
52
53 public StartRegistrationPresenter (){
54 super();
55 }
56
57 /**
58 * {@inheritDoc}
59 */
60 @Override
61 public void onPresenterReady() {
62
63 super.onPresenterReady();
64
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());
70 }
71
72 /**
73 * {@inheritDoc}
74 */
75 @Override
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);
80 }
81 super.handleViewExit();
82 }
83
84 @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).ADD")
85 public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
86
87 if(getView() == null || getView().getNewPublicationButton() != event.getSourceComponent()){
88 return;
89 }
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);
97 }
98
99 @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).REMOVE")
100 public void onReferenceEditorActionRemove(ReferenceEditorAction event) {
101
102 if(getView().getRemoveNewPublicationButton() != event.getSourceComponent()){
103 return;
104 }
105 DeleteResult result = getRepo().getReferenceService().delete(newReference);
106 if(!result.isOk()){
107 String message = "";
108 for(Exception e : result.getExceptions()){
109 message += e.getMessage() + "\n" + e.getStackTrace().toString() + "\n";
110 }
111 getView().getRemoveNewPublicationButton().setComponentError(new SystemError(message));
112 }
113
114 getView().getReferenceCombobox().setEnabled(false);
115
116 getView().getRemoveNewPublicationButton().setVisible(false);
117
118 getView().getNewPublicationButton().setVisible(true);
119 getView().getNewPublicationLabel().setCaption(null);
120 getView().getNewPublicationLabel().setVisible(false);
121 }
122
123 @EventListener
124 public void onDoneWithPopupEvent(DoneWithPopupEvent event){
125 if(event.getPopup() == newReferencePopup){
126 if(event.getReason() == Reason.SAVE){
127
128 newReference = newReferencePopup.getBean();
129
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());
133
134 getView().getReferenceCombobox().setValue(null); // deselect
135 getView().getReferenceCombobox().setEnabled(false);
136
137 getView().getContinueButton().setEnabled(true);
138
139 getView().getNewPublicationButton().setVisible(false);
140
141 getView().getRemoveNewPublicationButton().setVisible(true);
142 getView().getNewPublicationLabel().setCaption(newReference.getTitleCache());
143 getView().getNewPublicationLabel().setVisible(true);
144 }
145
146 newReferencePopup = null;
147 }
148 }
149
150 @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).ADD")
151 public void onRegistrationEditorActionAdd(RegistrationEditorAction event) {
152
153 if(getView().getContinueButton() != event.getSourceComponent()){
154 return;
155 }
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();
164 }
165 if(referenceId == null){
166 getView().getContinueButton().setComponentError(new UserError("Can't continue. No Reference is chosen."));
167 getView().getContinueButton().setEnabled(false);
168 }
169 registrationInProgress = true;
170 eventBus.publishEvent(new NavigationEvent(RegistrationWorksetViewBean.NAME, Integer.toString(referenceId)));
171
172 }
173
174 /**
175 * {@inheritDoc}
176 */
177 @Override
178 protected RegistrationDTO loadBeanById(Object identifier) {
179 // not needed //
180 return null;
181 }
182
183 /**
184 * {@inheritDoc}
185 */
186 @Override
187 protected void saveBean(RegistrationDTO bean) {
188 // TODO Auto-generated method stub
189
190 }
191
192 /**
193 * {@inheritDoc}
194 */
195 @Override
196 protected void deleteBean(RegistrationDTO bean) {
197 // TODO Auto-generated method stub
198
199 }
200
201 }