Project

General

Profile

Download (8.17 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.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.ref.TypedEntityReference;
32
import eu.etaxonomy.cdm.service.CdmFilterablePagingProviderFactory;
33
import eu.etaxonomy.cdm.service.ReferenceLabelProvider;
34
import eu.etaxonomy.cdm.service.TypifiedEntityFilterablePagingProvider;
35
import eu.etaxonomy.cdm.vaadin.event.EditorActionTypeFilter;
36
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
37
import eu.etaxonomy.cdm.vaadin.event.RegistrationEditorAction;
38
import eu.etaxonomy.cdm.vaadin.ui.RegistrationUIDefaults;
39
import eu.etaxonomy.cdm.vaadin.view.reference.ReferencePopupEditor;
40
import eu.etaxonomy.vaadin.mvp.AbstractEditorPresenter;
41
import eu.etaxonomy.vaadin.ui.navigation.NavigationEvent;
42
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent;
43
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent.Reason;
44

    
45
/**
46
 * @author a.kohlbecker
47
 * @since Jul 11, 2017
48
 *
49
 */
50
@SpringComponent
51
@ViewScope
52
public class StartRegistrationPresenter extends AbstractEditorPresenter<RegistrationDTO, StartRegistrationView> {
53

    
54
    private static final long serialVersionUID = 2283189121081612574L;
55

    
56
    private ReferencePopupEditor newReferencePopup;
57

    
58
    private Reference newReference;
59

    
60
    private boolean registrationInProgress;
61

    
62
    @Autowired
63
    protected CdmFilterablePagingProviderFactory pagingProviderFactory;
64

    
65
    public StartRegistrationPresenter (){
66
        super();
67
    }
68

    
69

    
70
    /**
71
     * {@inheritDoc}
72
     */
73
    @Override
74
    public void handleViewEntered() {
75

    
76
        super.handleViewEntered();
77

    
78
        TypifiedEntityFilterablePagingProvider<Reference> pagingProvider =
79
                pagingProviderFactory.referenceEntityReferencePagingProvider(
80
                        new ReferenceLabelProvider(ReferenceLabelProvider.LabelType.BIBLIOGRAPHIC),
81
                        ReferenceLabelProvider.INIT_STRATEGY
82
                        );
83
        TypedEntityCaptionGenerator<Reference> titleCacheGenrator = new TypedEntityCaptionGenerator<Reference>();
84
        pagingProvider.addRestriction(new Restriction("type", Operator.AND_NOT, null, ReferenceType.Section, ReferenceType.Journal, ReferenceType.PrintSeries));
85
        getView().getReferenceCombobox().setCaptionGenerator(titleCacheGenrator);
86
        getView().getReferenceCombobox().loadFrom(pagingProvider, pagingProvider, pagingProvider.getPageSize());
87
    }
88

    
89
    /**
90
     * {@inheritDoc}
91
     */
92
    @Override
93
    public void handleViewExit() {
94
        if(!registrationInProgress && newReference != null){
95
            logger.info("Deleting newly created Reference due to canceled registration");
96
            getRepo().getReferenceService().delete(newReference);
97
        }
98
        super.handleViewExit();
99
    }
100

    
101

    
102
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
103
    public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
104

    
105
        if(getView() == null || getView().getNewPublicationButton() != event.getSource()){
106
            return;
107
        }
108

    
109
        newReferencePopup = openPopupEditor(ReferencePopupEditor.class, event);
110
        EnumSet<ReferenceType> refTypes = RegistrationUIDefaults.PRINTPUB_REFERENCE_TYPES.clone();
111
        refTypes.remove(ReferenceType.Section);
112
        newReferencePopup.withReferenceTypes(refTypes);
113

    
114
        newReferencePopup.grantToCurrentUser(EnumSet.of(CRUD.UPDATE, CRUD.DELETE));
115
        newReferencePopup.withDeleteButton(true);
116
        newReferencePopup.loadInEditor(null);
117
        newReferencePopup.getTypeSelect().setValue(ReferenceType.Article);
118
    }
119

    
120
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Remove.class)
121
    public void onReferenceEditorActionRemove(ReferenceEditorAction event) {
122

    
123
        if(getView().getRemoveNewPublicationButton() != event.getSource()){
124
            return;
125
        }
126
        DeleteResult result = getRepo().getReferenceService().delete(newReference);
127
        if(!result.isOk()){
128
            String message = "";
129
            for(Exception e : result.getExceptions()){
130
                message += e.getMessage() + "\n" + e.getStackTrace().toString() + "\n";
131
            }
132
            getView().getRemoveNewPublicationButton().setComponentError(new SystemError(message));
133
        }
134

    
135
        getView().getReferenceCombobox().setEnabled(false);
136

    
137
        getView().getRemoveNewPublicationButton().setVisible(false);
138

    
139
        getView().getNewPublicationButton().setVisible(true);
140
        getView().getNewPublicationLabel().setCaption(null);
141
        getView().getNewPublicationLabel().setVisible(false);
142
    }
143

    
144
    @EventBusListenerMethod
145
    public void onDoneWithPopupEvent(DoneWithPopupEvent event){
146

    
147
        if(event.getPopup() == newReferencePopup){
148
            if(event.getReason() == Reason.SAVE){
149

    
150
                newReference = newReferencePopup.getBean();
151

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

    
156
                getView().getReferenceCombobox().setValue(null);  // deselect
157
                getView().getReferenceCombobox().setEnabled(false);
158

    
159
                getView().getContinueButton().setEnabled(true);
160

    
161
                getView().getNewPublicationButton().setVisible(false);
162

    
163
                getView().getRemoveNewPublicationButton().setVisible(true);
164
                getView().getNewPublicationLabel().setCaption(newReference.getTitleCache());
165
                getView().getNewPublicationLabel().setVisible(true);
166
            }
167

    
168
            newReferencePopup = null;
169
        }
170
    }
171

    
172
    @SuppressWarnings("null")
173
    @EventBusListenerMethod(filter = EditorActionTypeFilter.Add.class)
174
    public void onRegistrationEditorActionAdd(RegistrationEditorAction event) {
175

    
176
        if(getView().getContinueButton() != event.getSource()){
177
            return;
178
        }
179

    
180
        UUID referenceUuid = null;
181
        LazyComboBox<TypedEntityReference<Reference>> referenceCombobox = getView().getReferenceCombobox();
182
        referenceCombobox.commit();
183
        if(newReference != null){
184
            referenceUuid = newReference.getUuid();
185
       // } else if(referenceCombobox.getValue() != null) {
186
        } else if ( event.getEntityUuid() != null) { // HACKED, see view implementation
187
            referenceUuid = event.getEntityUuid();
188
        }
189
        if(referenceUuid == null){
190
            getView().getContinueButton().setComponentError(new UserError("Can't continue. No Reference is chosen."));
191
            getView().getContinueButton().setEnabled(false);
192
        }
193
        registrationInProgress = true;
194
        viewEventBus.publish(EventScope.UI, this, new NavigationEvent(RegistrationWorksetViewBean.NAME, referenceUuid.toString()));
195

    
196
    }
197

    
198
    /**
199
     * {@inheritDoc}
200
     */
201
    @Override
202
    protected RegistrationDTO loadBeanById(Object identifier) {
203
        // not needed //
204
        return null;
205
    }
206

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

    
215
    /**
216
     * {@inheritDoc}
217
     */
218
    @Override
219
    protected void deleteBean(RegistrationDTO bean) {
220
        // not needed //
221
    }
222

    
223
}
(16-16/21)