Project

General

Profile

Download (7.53 KB) Statistics
| Branch: | Tag: | Revision:
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.hibernate.permission.CRUD;
29
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
30
import eu.etaxonomy.cdm.service.CdmFilterablePagingProviderFactory;
31
import eu.etaxonomy.cdm.vaadin.event.EditorActionTypeFilter;
32
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
33
import eu.etaxonomy.cdm.vaadin.event.RegistrationEditorAction;
34
import eu.etaxonomy.cdm.vaadin.ui.RegistrationUIDefaults;
35
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
    @Autowired
60
    protected CdmFilterablePagingProviderFactory pagingProviderFactory;
61

    
62
    public StartRegistrationPresenter (){
63
        super();
64
    }
65

    
66

    
67
    /**
68
     * {@inheritDoc}
69
     */
70
    @Override
71
    public void onPresenterReady() {
72

    
73
        super.onPresenterReady();
74

    
75
        CdmFilterablePagingProvider<Reference, Reference> pagingProvider = pagingProviderFactory.referencePagingProvider();
76
        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

    
94
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
95
    public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
96

    
97
        if(getView() == null || getView().getNewPublicationButton() != event.getSource()){
98
            return;
99
        }
100

    
101
        newReferencePopup = openPopupEditor(ReferencePopupEditor.class, event);
102
        EnumSet<ReferenceType> refTypes = RegistrationUIDefaults.PRINTPUB_REFERENCE_TYPES.clone();
103
        refTypes.remove(ReferenceType.Section);
104
        newReferencePopup.withReferenceTypes(refTypes);
105
        newReferencePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
106
        newReferencePopup.withDeleteButton(true);
107
        newReferencePopup.loadInEditor(null);
108
    }
109

    
110
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Remove.class)
111
    public void onReferenceEditorActionRemove(ReferenceEditorAction event) {
112

    
113
        if(getView().getRemoveNewPublicationButton() != event.getSource()){
114
            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
    @EventBusListenerMethod
135
    public void onDoneWithPopupEvent(DoneWithPopupEvent event){
136

    
137
        if(event.getPopup() == newReferencePopup){
138
            if(event.getReason() == Reason.SAVE){
139

    
140
                newReference = newReferencePopup.getBean();
141

    
142
                // 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
                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
    @SuppressWarnings("null")
163
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
164
    public void onRegistrationEditorActionAdd(RegistrationEditorAction event) {
165

    
166
        if(getView().getContinueButton() != event.getSource()){
167
            return;
168
        }
169

    
170
        UUID referenceUuid = null;
171
        LazyComboBox<Reference> referenceCombobox = getView().getReferenceCombobox();
172
        referenceCombobox.commit();
173
        if(newReference != null){
174
            referenceUuid = newReference.getUuid();
175
       // } else if(referenceCombobox.getValue() != null) {
176
        } else if ( event.getEntityUuid() != null) { // HACKED, see view implementation
177
            referenceUuid = event.getEntityUuid();
178
        }
179
        if(referenceUuid == null){
180
            getView().getContinueButton().setComponentError(new UserError("Can't continue. No Reference is chosen."));
181
            getView().getContinueButton().setEnabled(false);
182
        }
183
        registrationInProgress = true;
184
        viewEventBus.publish(EventScope.UI, this, new NavigationEvent(RegistrationWorksetViewBean.NAME, referenceUuid.toString()));
185

    
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
        // not needed //
203
    }
204

    
205
    /**
206
     * {@inheritDoc}
207
     */
208
    @Override
209
    protected void deleteBean(RegistrationDTO bean) {
210
        // not needed //
211
    }
212

    
213
}
(16-16/20)