Project

General

Profile

Download (12.4 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.ArrayList;
12
import java.util.List;
13

    
14
import org.springframework.beans.factory.annotation.Autowired;
15
import org.springframework.context.event.EventListener;
16
import org.springframework.security.core.Authentication;
17

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

    
22
import eu.etaxonomy.cdm.api.service.INameService;
23
import eu.etaxonomy.cdm.api.service.IRegistrationService;
24
import eu.etaxonomy.cdm.model.common.User;
25
import eu.etaxonomy.cdm.model.name.Rank;
26
import eu.etaxonomy.cdm.model.name.Registration;
27
import eu.etaxonomy.cdm.model.name.TaxonName;
28
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
29
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
30
import eu.etaxonomy.cdm.model.reference.Reference;
31
import eu.etaxonomy.cdm.service.CdmStore;
32
import eu.etaxonomy.cdm.service.IRegistrationWorkingSetService;
33
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
34
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
35
import eu.etaxonomy.cdm.vaadin.event.RegistrationEditorAction;
36
import eu.etaxonomy.cdm.vaadin.event.ShowDetailsEvent;
37
import eu.etaxonomy.cdm.vaadin.event.TaxonNameEditorAction;
38
import eu.etaxonomy.cdm.vaadin.event.TypeDesignationWorkingsetEditorAction;
39
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
40
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationSetManager.TypeDesignationWorkingSetType;
41
import eu.etaxonomy.cdm.vaadin.view.name.RegistrationAndWorkingsetId;
42
import eu.etaxonomy.cdm.vaadin.view.name.SpecimenTypeDesignationWorkingsetPopupEditor;
43
import eu.etaxonomy.cdm.vaadin.view.name.TaxonNamePopupEditor;
44
import eu.etaxonomy.cdm.vaadin.view.reference.ReferencePopupEditor;
45
import eu.etaxonomy.vaadin.mvp.AbstractPresenter;
46
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent;
47
import eu.etaxonomy.vaadin.ui.view.DoneWithPopupEvent.Reason;
48

    
49
/**
50
 * @author a.kohlbecker
51
 * @since Mar 3, 2017
52
 *
53
 */
54
@SpringComponent
55
@ViewScope
56
public class RegistrationWorkflowPresenter extends AbstractPresenter<RegistrationWorkflowView> {
57

    
58
    private static final long serialVersionUID = 1L;
59

    
60
    @Autowired
61
    private IRegistrationWorkingSetService workingSetService;
62

    
63
    /**
64
     * @return the workingSetService
65
     */
66
    public IRegistrationWorkingSetService getWorkingSetService() {
67
        ensureBoundConversation();
68
        return workingSetService;
69
    }
70

    
71
    private RegistrationWorkingSet workingset;
72

    
73
    private TaxonName newTaxonNameForRegistration;
74

    
75

    
76
    /**
77
     *
78
     */
79
    public RegistrationWorkflowPresenter() {
80
    }
81

    
82

    
83
    /**
84
     * Always create a new Store
85
     *
86
     * @return
87
     */
88
    protected CdmStore<Registration, IRegistrationService> getRegistrationStore(){
89
        return new CdmStore<Registration, IRegistrationService>(getRepo(), getRepo().getRegistrationService());
90
    }
91

    
92
    /**
93
     * Always create a new Store
94
     *
95
     * @return
96
     */
97
    protected CdmStore<TaxonName, INameService> getTaxonNameStore(){
98
        return new  CdmStore<TaxonName, INameService>(getRepo(), getRepo().getNameService());
99
    }
100

    
101

    
102
    /**
103
     * {@inheritDoc}
104
     */
105
    @Override
106
    public void handleViewEntered() {
107

    
108
        super.handleViewEntered();
109
        presentWorkingSet(getView().getCitationID());
110
    }
111

    
112
    /**
113
     * @param registrationID
114
     */
115
    private void presentWorkingSet(Integer referenceID) {
116
        try {
117
            workingset = getWorkingSetService().loadWorkingSetByReferenceID(referenceID);
118
        } catch (RegistrationValidationException error) {
119
            getView().getWorkflow().setComponentError(new SystemError(error));
120
        }
121
        if(workingset == null || workingset.getCitationId() == null){
122
            Reference citation = getRepo().getReferenceService().find(referenceID);
123
            workingset = new RegistrationWorkingSet(citation);
124
        }
125
        getView().setHeaderText("Registration for " + workingset.getCitation());
126
        getView().setWorkingset(workingset);
127
    }
128

    
129
    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).ADD && #event.sourceComponent == null")
130
    public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
131
        ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
132
        popup.loadInEditor(null);
133
    }
134

    
135
    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).EDIT && #event.sourceComponent == null")
136
    public void onReferenceEditorActionEdit(ReferenceEditorAction event) {
137
        ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
138
        popup.withDeleteButton(true);
139
        popup.loadInEditor(event.getEntityId());
140
    }
141

    
142
    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).EDIT && #event.sourceComponent == null")
143
    public void onRegistrationEditorAction(RegistrationEditorAction event) {
144
        RegistrationPopupEditor popup = getNavigationManager().showInPopup(RegistrationPopupEditor.class);
145
        popup.loadInEditor(event.getEntityId());
146
    }
147

    
148
    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).EDIT && #event.sourceComponent == null")
149
    public void onTaxonNameEditorActionEdit(TaxonNameEditorAction event) {
150

    
151
        TaxonNamePopupEditor popup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class);
152
        popup.withDeleteButton(true);
153
        // disable NomReferenceCombobox:
154
        // the in the registration application inReferences should only edited centrally
155
        popup.getNomReferenceCombobox().setEnabled(false);
156
        popup.loadInEditor(event.getEntityId());
157
    }
158

    
159
    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).ADD")
160
    public void onTaxonNameEditorActionAdd(TaxonNameEditorAction event) {
161

    
162
        newTaxonNameForRegistration = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
163
        newTaxonNameForRegistration.setNomenclaturalReference(getRepo().getReferenceService().find(workingset.getCitationId()));
164
        EntityChangeEvent nameSaveEvent = getTaxonNameStore().saveBean(newTaxonNameForRegistration);
165
        newTaxonNameForRegistration = getRepo().getNameService().find(nameSaveEvent.getEntityId());
166
        TaxonNamePopupEditor popup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class);
167
        popup.withDeleteButton(true);
168
        popup.loadInEditor(newTaxonNameForRegistration.getId());
169
        // disable NomReferenceCombobox:
170
        // the in the registration application inReferences should only edited centrally
171
        popup.getNomReferenceCombobox().setEnabled(false);
172
    }
173

    
174
    @EventListener
175
    public void onDoneWithTaxonnameEditor(DoneWithPopupEvent event) throws RegistrationValidationException{
176
        if(event.getPopup() instanceof TaxonNamePopupEditor){
177
            if(newTaxonNameForRegistration != null && event.getReason().equals(Reason.SAVE)){
178
                // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
179
                // move into RegistrationWorkflowStateMachine
180
                long identifier = System.currentTimeMillis();
181
                Registration reg = Registration.NewInstance(
182
                        "http://phycobank.org/" + identifier,
183
                        "" + identifier,
184
                        getRepo().getNameService().find(newTaxonNameForRegistration.getId()),
185
                        null);
186
                Authentication authentication = currentSecurityContext().getAuthentication();
187
                reg.setSubmitter((User)authentication.getPrincipal());
188
                // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
189

    
190
                getRegistrationStore().saveBean(reg);
191

    
192
                workingset.add(reg);
193
                refreshView();
194
            } else if(event.getReason().equals(Reason.CANCEL)){
195
                // clean up
196
                getTaxonNameStore().deleteBean(newTaxonNameForRegistration);
197
            }
198
            newTaxonNameForRegistration = null;
199
        }
200

    
201
    }
202
    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).EDIT && #event.sourceComponent == null")
203
    public void onTypeDesignationsEditorActionEdit(TypeDesignationWorkingsetEditorAction event) {
204

    
205
            if(event.getWorkingSetType() == TypeDesignationWorkingSetType.SPECIMEN_TYPE_DESIGNATION_WORKINGSET ){
206
                SpecimenTypeDesignationWorkingsetPopupEditor popup = getNavigationManager().showInPopup(SpecimenTypeDesignationWorkingsetPopupEditor.class);
207
                popup.loadInEditor(new RegistrationAndWorkingsetId(event.getRegistrationId(), event.getEntityId()));
208
            } else {
209
                // TypeDesignationWorkingSetType.NAME_TYPE_DESIGNATION_WORKINGSET
210
                // FIXME implement NameTypeDesignationWorkingsetPopupEditor
211
            }
212

    
213
    }
214

    
215
    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).ADD && #event.sourceComponent == null")
216
    public void onTypeDesignationsEditorActionAdd(TypeDesignationWorkingsetEditorAction event) {
217

    
218
        if(event.getWorkingSetType() == TypeDesignationWorkingSetType.SPECIMEN_TYPE_DESIGNATION_WORKINGSET){
219
            SpecimenTypeDesignationWorkingsetPopupEditor popup = getNavigationManager().showInPopup(SpecimenTypeDesignationWorkingsetPopupEditor.class);
220
            popup.loadInEditor(null);
221
            popup.withDeleteButton(true);
222
        }
223
    }
224

    
225
    @EventListener(classes=ShowDetailsEvent.class, condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet)")
226
    public void onShowRegistrationWorkingSetMessages(ShowDetailsEvent<?,?> event) { // WARNING don't use more specific generic type arguments
227
        List<String> messages = new ArrayList<>();
228
        for(RegistrationDTO dto : workingset.getRegistrationDTOs()){
229
            dto.getMessages().forEach(m -> messages.add(dto.getSummary() + ": " + m));
230
        }
231
        if(event.getProperty().equals("messages")){
232
            getView().openDetailsPopup("Messages", messages);
233
        }
234
    }
235

    
236
    @EventListener(classes=ShowDetailsEvent.class, condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO)")
237
    public void onShowRegistrationMessages(ShowDetailsEvent<?,?> event) { // WARNING don't use more specific generic type arguments
238
        RegistrationDTO regDto = workingSetService.loadDtoById((Integer)event.getIdentifier());
239
        if(event.getProperty().equals("messages")){
240
            if(getView() != null){
241
                getView().openDetailsPopup("Messages", regDto.getMessages());
242
            }
243
        }
244
    }
245

    
246
    @EventListener
247
    public void onEntityChangeEvent(EntityChangeEvent event){
248
        if(workingset == null){
249
            return;
250
        }
251
        if(Reference.class.isAssignableFrom(event.getEntityType())){
252
            if(workingset.getCitationId().equals(event.getEntityId())){
253
                refreshView();
254
            }
255
        } else
256
        if(Registration.class.isAssignableFrom(event.getEntityType())){
257
            if(workingset.getRegistrations().stream().anyMatch(reg -> reg.getId() == event.getEntityId())){
258
                refreshView();
259
            }
260
        } else
261
        if(TaxonName.class.isAssignableFrom(event.getEntityType())){
262
            if(workingset.getRegistrationDTOs().stream().anyMatch(reg ->
263
                reg.getTypifiedName() != null
264
                && reg.getTypifiedName().getId() == event.getEntityId())){
265
                    refreshView();
266
            }
267
        } else
268
        if(TypeDesignationBase.class.isAssignableFrom(event.getEntityType())){
269
            if(workingset.getRegistrationDTOs().stream().anyMatch(
270
                    reg -> reg.getTypeDesignations().stream().anyMatch(
271
                            td -> td.getId() == event.getEntityId()
272
                            )
273
                        )
274
                    ){
275
                refreshView();
276
            }
277
        }
278

    
279
    }
280

    
281
    /**
282
     *
283
     */
284
    protected void refreshView() {
285
        getConversationHolder().getSession().clear();
286
        presentWorkingSet(workingset.getCitationId());
287
    }
288

    
289
}
(12-12/19)