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