Project

General

Profile

Download (7.61 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 handleViewEntered() {
72

    
73
        super.handleViewEntered();
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

    
106
        newReferencePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
107
        newReferencePopup.withDeleteButton(true);
108
        newReferencePopup.loadInEditor(null);
109
        newReferencePopup.getTypeSelect().setValue(ReferenceType.Article);
110
    }
111

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

    
115
        if(getView().getRemoveNewPublicationButton() != event.getSource()){
116
            return;
117
        }
118
        DeleteResult result = getRepo().getReferenceService().delete(newReference);
119
        if(!result.isOk()){
120
            String message = "";
121
            for(Exception e : result.getExceptions()){
122
                message += e.getMessage() + "\n" + e.getStackTrace().toString() + "\n";
123
            }
124
            getView().getRemoveNewPublicationButton().setComponentError(new SystemError(message));
125
        }
126

    
127
        getView().getReferenceCombobox().setEnabled(false);
128

    
129
        getView().getRemoveNewPublicationButton().setVisible(false);
130

    
131
        getView().getNewPublicationButton().setVisible(true);
132
        getView().getNewPublicationLabel().setCaption(null);
133
        getView().getNewPublicationLabel().setVisible(false);
134
    }
135

    
136
    @EventBusListenerMethod
137
    public void onDoneWithPopupEvent(DoneWithPopupEvent event){
138

    
139
        if(event.getPopup() == newReferencePopup){
140
            if(event.getReason() == Reason.SAVE){
141

    
142
                newReference = newReferencePopup.getBean();
143

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

    
148
                getView().getReferenceCombobox().setValue(null);  // deselect
149
                getView().getReferenceCombobox().setEnabled(false);
150

    
151
                getView().getContinueButton().setEnabled(true);
152

    
153
                getView().getNewPublicationButton().setVisible(false);
154

    
155
                getView().getRemoveNewPublicationButton().setVisible(true);
156
                getView().getNewPublicationLabel().setCaption(newReference.getTitleCache());
157
                getView().getNewPublicationLabel().setVisible(true);
158
            }
159

    
160
            newReferencePopup = null;
161
        }
162
    }
163

    
164
    @SuppressWarnings("null")
165
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
166
    public void onRegistrationEditorActionAdd(RegistrationEditorAction event) {
167

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

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

    
188
    }
189

    
190
    /**
191
     * {@inheritDoc}
192
     */
193
    @Override
194
    protected RegistrationDTO loadBeanById(Object identifier) {
195
        // not needed //
196
        return null;
197
    }
198

    
199
    /**
200
     * {@inheritDoc}
201
     */
202
    @Override
203
    protected void saveBean(RegistrationDTO bean) {
204
        // not needed //
205
    }
206

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

    
215
}
(16-16/20)