Project

General

Profile

Download (7.34 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

    
13
import org.vaadin.spring.events.EventScope;
14
import org.vaadin.spring.events.annotation.EventBusListenerMethod;
15
import org.vaadin.viritin.fields.LazyComboBox;
16

    
17
import com.vaadin.server.SystemError;
18
import com.vaadin.server.UserError;
19
import com.vaadin.spring.annotation.SpringComponent;
20
import com.vaadin.spring.annotation.ViewScope;
21

    
22
import eu.etaxonomy.cdm.api.service.DeleteResult;
23
import eu.etaxonomy.cdm.model.reference.Reference;
24
import eu.etaxonomy.cdm.model.reference.ReferenceType;
25
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
26
import eu.etaxonomy.cdm.service.CdmFilterablePagingProvider;
27
import eu.etaxonomy.cdm.vaadin.event.EditorActionTypeFilter;
28
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
29
import eu.etaxonomy.cdm.vaadin.event.RegistrationEditorAction;
30
import eu.etaxonomy.cdm.vaadin.ui.RegistrationUIDefaults;
31
import eu.etaxonomy.cdm.vaadin.util.CdmTitleCacheCaptionGenerator;
32
import eu.etaxonomy.cdm.vaadin.view.reference.ReferencePopupEditor;
33
import eu.etaxonomy.vaadin.mvp.AbstractEditorPresenter;
34
import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
35
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent;
36
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent.Reason;
37

    
38
/**
39
 * @author a.kohlbecker
40
 * @since Jul 11, 2017
41
 *
42
 */
43
@SpringComponent
44
@ViewScope
45
public class StartRegistrationPresenter extends AbstractEditorPresenter<RegistrationDTO, StartRegistrationView> {
46

    
47
    private static final long serialVersionUID = 2283189121081612574L;
48

    
49
    private ReferencePopupEditor newReferencePopup;
50

    
51
    private Reference newReference;
52

    
53
    private boolean registrationInProgress;
54

    
55
    public StartRegistrationPresenter (){
56
        super();
57
    }
58

    
59

    
60
    /**
61
     * {@inheritDoc}
62
     */
63
    @Override
64
    public void onPresenterReady() {
65

    
66
        super.onPresenterReady();
67

    
68
        CdmFilterablePagingProvider<Reference, Reference> pagingProvider = new CdmFilterablePagingProvider<Reference, Reference>(
69
                getRepo().getReferenceService());
70
        CdmTitleCacheCaptionGenerator<Reference> titleCacheGenrator = new CdmTitleCacheCaptionGenerator<Reference>();
71
        getView().getReferenceCombobox().setCaptionGenerator(titleCacheGenrator);
72
        getView().getReferenceCombobox().loadFrom(pagingProvider, pagingProvider, pagingProvider.getPageSize());
73
    }
74

    
75
    /**
76
     * {@inheritDoc}
77
     */
78
    @Override
79
    public void handleViewExit() {
80
        if(!registrationInProgress && newReference != null){
81
            logger.info("Deleting newly created Reference due to canceled registration");
82
            getRepo().getReferenceService().delete(newReference);
83
        }
84
        super.handleViewExit();
85
    }
86

    
87

    
88
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
89
    public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
90

    
91
        if(getView() == null || getView().getNewPublicationButton() != event.getSourceComponent()){
92
            return;
93
        }
94

    
95
        newReferencePopup = getNavigationManager().showInPopup(ReferencePopupEditor.class, getView());
96
        EnumSet<ReferenceType> refTypes = RegistrationUIDefaults.PRINTPUB_REFERENCE_TYPES.clone();
97
        refTypes.remove(ReferenceType.Section);
98
        newReferencePopup.withReferenceTypes(refTypes);
99
        newReferencePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
100
        newReferencePopup.withDeleteButton(true);
101
        newReferencePopup.loadInEditor(null);
102
    }
103

    
104
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Remove.class)
105
    public void onReferenceEditorActionRemove(ReferenceEditorAction event) {
106

    
107
        if(getView().getRemoveNewPublicationButton() != event.getSourceComponent()){
108
            return;
109
        }
110
        DeleteResult result = getRepo().getReferenceService().delete(newReference);
111
        if(!result.isOk()){
112
            String message = "";
113
            for(Exception e : result.getExceptions()){
114
                message += e.getMessage() + "\n" + e.getStackTrace().toString() + "\n";
115
            }
116
            getView().getRemoveNewPublicationButton().setComponentError(new SystemError(message));
117
        }
118

    
119
        getView().getReferenceCombobox().setEnabled(false);
120

    
121
        getView().getRemoveNewPublicationButton().setVisible(false);
122

    
123
        getView().getNewPublicationButton().setVisible(true);
124
        getView().getNewPublicationLabel().setCaption(null);
125
        getView().getNewPublicationLabel().setVisible(false);
126
    }
127

    
128
    @EventBusListenerMethod
129
    public void onDoneWithPopupEvent(DoneWithPopupEvent event){
130

    
131
        if(event.getPopup() == newReferencePopup){
132
            if(event.getReason() == Reason.SAVE){
133

    
134
                newReference = newReferencePopup.getBean();
135

    
136
                // TODO the bean contained in the popup editor is not yet updated at this point.
137
                //      so re reload it using the uuid since new beans will not have an Id at this point.
138
                newReference = getRepo().getReferenceService().find(newReference.getUuid());
139

    
140
                getView().getReferenceCombobox().setValue(null);  // deselect
141
                getView().getReferenceCombobox().setEnabled(false);
142

    
143
                getView().getContinueButton().setEnabled(true);
144

    
145
                getView().getNewPublicationButton().setVisible(false);
146

    
147
                getView().getRemoveNewPublicationButton().setVisible(true);
148
                getView().getNewPublicationLabel().setCaption(newReference.getTitleCache());
149
                getView().getNewPublicationLabel().setVisible(true);
150
            }
151

    
152
            newReferencePopup = null;
153
        }
154
    }
155

    
156
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
157
    public void onRegistrationEditorActionAdd(RegistrationEditorAction event) {
158

    
159
        if(getView().getContinueButton() != event.getSourceComponent()){
160
            return;
161
        }
162

    
163
        Integer referenceId = null;
164
        LazyComboBox<Reference> referenceCombobox = getView().getReferenceCombobox();
165
        referenceCombobox.commit();
166
        if(newReference != null){
167
            referenceId = newReference.getId();
168
       // } else if(referenceCombobox.getValue() != null) {
169
        } else if ( event.getEntityId() != null) { // HACKED, see view implementation
170
            referenceId = event.getEntityId();
171
        }
172
        if(referenceId == null){
173
            getView().getContinueButton().setComponentError(new UserError("Can't continue. No Reference is chosen."));
174
            getView().getContinueButton().setEnabled(false);
175
        }
176
        registrationInProgress = true;
177
        viewEventBus.publish(EventScope.UI, this, new NavigationEvent(RegistrationWorksetViewBean.NAME, Integer.toString(referenceId)));
178

    
179
    }
180

    
181
    /**
182
     * {@inheritDoc}
183
     */
184
    @Override
185
    protected RegistrationDTO loadBeanById(Object identifier) {
186
        // not needed //
187
        return null;
188
    }
189

    
190
    /**
191
     * {@inheritDoc}
192
     */
193
    @Override
194
    protected void saveBean(RegistrationDTO bean) {
195
        // TODO Auto-generated method stub
196

    
197
    }
198

    
199
    /**
200
     * {@inheritDoc}
201
     */
202
    @Override
203
    protected void deleteBean(RegistrationDTO bean) {
204
        // TODO Auto-generated method stub
205

    
206
    }
207

    
208
}
(15-15/19)