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