Project

General

Profile

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