Project

General

Profile

Download (12.6 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.io.Serializable;
12
import java.util.ArrayList;
13
import java.util.Arrays;
14
import java.util.List;
15

    
16
import org.apache.log4j.Logger;
17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.context.event.EventListener;
19
import org.springframework.transaction.TransactionStatus;
20

    
21
import com.vaadin.server.SystemError;
22
import com.vaadin.spring.annotation.SpringComponent;
23
import com.vaadin.spring.annotation.ViewScope;
24

    
25
import eu.etaxonomy.cdm.model.name.Rank;
26
import eu.etaxonomy.cdm.model.name.Registration;
27
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
28
import eu.etaxonomy.cdm.model.name.TaxonName;
29
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
30
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
31
import eu.etaxonomy.cdm.model.reference.Reference;
32
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
33
import eu.etaxonomy.cdm.service.IRegistrationWorkingSetService;
34
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
35
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
36
import eu.etaxonomy.cdm.vaadin.event.RegistrationEditorAction;
37
import eu.etaxonomy.cdm.vaadin.event.ShowDetailsEvent;
38
import eu.etaxonomy.cdm.vaadin.event.TaxonNameEditorAction;
39
import eu.etaxonomy.cdm.vaadin.event.TypeDesignationWorkingsetEditorAction;
40
import eu.etaxonomy.cdm.vaadin.event.registration.RegistrationWorkflowEvent;
41
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
42
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationSetManager.TypeDesignationWorkingSet;
43
import eu.etaxonomy.cdm.vaadin.view.name.SpecimenTypeDesignationWorkingsetPopupEditor;
44
import eu.etaxonomy.cdm.vaadin.view.name.TaxonNamePopupEditor;
45
import eu.etaxonomy.cdm.vaadin.view.reference.ReferencePopupEditor;
46
import eu.etaxonomy.vaadin.mvp.AbstractPresenter;
47

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

    
57
    private static final long serialVersionUID = 1L;
58

    
59
    @Autowired
60
    private IRegistrationWorkingSetService workingSetService;
61

    
62
    private RegistrationWorkingSet workingset;
63

    
64
    private List<String> specimenTypeDesignationWorkingsetInitStrategy = Arrays.asList(new String[]{
65
            "typeDesignations.typeStatus.representations",
66
            "typeDesignations.typeSpecimen.sources",
67
            "typeDesignations.typeSpecimen.mediaSpecimen.representations.parts",
68
            "typeDesignations.typeSpecimen.collection",
69
            "typeDesignations.typeSpecimen.derivedFrom.type",
70
            "typeDesignations.typeSpecimen.derivedFrom.derivatives",
71
            // Need to initialize all properties of the DerivedUnit to avoid LIEs while converting DerivedUnit with the DerivedUnitConverter:
72
            "typeDesignations.typeSpecimen.*",
73
    });
74

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

    
81
    @EventListener
82
    protected void onRegistrationStartEvent(RegistrationWorkflowEvent event){
83

    
84
        boolean HACK = true;
85
        if(workingset != null && !HACK){
86
            Logger.getLogger(RegistrationWorkflowPresenter.class).warn("Can't start a new workflow over an existing one.");
87
            return;
88
        }
89

    
90
        if(event.isStart()) {
91
            workingset = new RegistrationWorkingSet();
92
            Registration reg = Registration.NewInstance();
93
            reg.setName(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()));
94
            getView().setHeaderText("New Registration");
95
            try {
96
                workingset.add(reg);
97
            } catch (RegistrationValidationException error) {
98
                getView().getWorkflow().setComponentError(new SystemError(error));
99
            }
100
            getView().setWorkingset(workingset);
101
        } else {
102
            Integer citationID = event.getCitationID();
103
            presentWorkingSetByRegID(citationID);
104
        }
105

    
106
    }
107

    
108
    /**
109
     * @param registrationID
110
     * @deprecated use other method working sets should only be addressed by the referenceID
111
     */
112
    @Deprecated
113
    private void presentWorkingSetByRegID(Integer citationID) {
114
        try {
115
            workingset = workingSetService.loadWorkingSetByCitationID(citationID);
116
        } catch (RegistrationValidationException error) {
117
            getView().getWorkflow().setComponentError(new SystemError(error));
118
        }
119
        getView().setHeaderText("Registrations in " + workingset.getCitation());
120
        getView().setWorkingset(workingset);
121
    }
122

    
123
    /**
124
     * @param registrationID
125
     */
126
    private void presentWorkingSet(Integer referenceID) {
127
        try {
128
            workingset = workingSetService.loadWorkingSetByReferenceID(referenceID);
129
        } catch (RegistrationValidationException error) {
130
            getView().getWorkflow().setComponentError(new SystemError(error));
131
        }
132
        getView().setHeaderText("Registration for " + workingset.getCitation());
133
        getView().setWorkingset(workingset);
134
    }
135

    
136
    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).ADD && #event.sourceComponent == null")
137
    public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
138
        Reference reference = ReferenceFactory.newGeneric();
139
        ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
140
        popup.showInEditor(reference);
141
    }
142

    
143
    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).EDIT && #event.sourceComponent == null")
144
    public void onReferenceEditorActionEdit(ReferenceEditorAction event) {
145
        TransactionStatus tx = getRepo().startTransaction(false);
146
        Reference reference = getRepo().getReferenceService().find(event.getEntityId());
147
        ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
148
        popup.showInEditor(reference);
149
        popup.withDeleteButton(true);
150
        getRepo().commitTransaction(tx);
151
    }
152

    
153
    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).EDIT && #event.sourceComponent == null")
154
    public void onRegistrationEditorAction(RegistrationEditorAction event) {
155
        TransactionStatus tx = getRepo().startTransaction(false);
156
        Registration registration = getRepo().getRegistrationService().find(event.getEntityId());
157
        RegistrationPopupEditor popup = getNavigationManager().showInPopup(RegistrationPopupEditor.class);
158
        popup.showInEditor(registration);
159
        getRepo().commitTransaction(tx);
160
    }
161

    
162
    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).EDIT && #event.sourceComponent == null")
163
    public void onTaxonNameEditorAction(TaxonNameEditorAction event) {
164

    
165
        TransactionStatus tx = getRepo().startTransaction(false);
166
        // FIXME optional:
167
        // A) allow full initialization of the entity here, the Presenter of the Editor should
168
        //    provide the intiStrategy via a static method so that it can be used
169
        // B) only pass the identifier (Object) to the View and the Presenter is responsible for
170
        //    loading and initializing  the entity
171
        TaxonName taxonName = getRepo().getNameService().find(event.getEntityId());
172
        TaxonNamePopupEditor popup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class);
173
        popup.showInEditor(taxonName);
174
        popup.withDeleteButton(true);
175
        // in the registration application inReferences should only edited centrally
176
        popup.getNomReferenceCombobox().setEnabled(false);
177
        getRepo().commitTransaction(tx);
178
    }
179

    
180
    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).EDIT && #event.sourceComponent == null")
181
    public void onTypeDesignationsEditorActionEdit(TypeDesignationWorkingsetEditorAction event) {
182

    
183
            TransactionStatus tx = getRepo().startTransaction(false);
184
            List<Integer> ids = new ArrayList<>();
185
            ids.add(event.getRegistrationId());
186
            Registration reg = getRepo().getRegistrationService().loadByIds(ids, specimenTypeDesignationWorkingsetInitStrategy).get(0);
187
            RegistrationDTO regDTO = new RegistrationDTO(reg);
188
            TypeDesignationWorkingSet typeDesignationWorkingSet = regDTO.getTypeDesignationWorkingSet(event.getEntityId());
189
            if(typeDesignationWorkingSet.isSpecimenTypeDesigationWorkingSet()){
190
                // check SpecimenTypeDesignation
191
                SpecimenTypeDesignationWorkingsetPopupEditor popup = getNavigationManager().showInPopup(SpecimenTypeDesignationWorkingsetPopupEditor.class);
192
                popup.showInEditor(regDTO.getSpecimenTypeDesignationWorkingSetDTO(typeDesignationWorkingSet.getBaseEntityReference()));
193
            } else {
194
                // FIXME implement NameTypeDesignationWorkingsetPopupEditor
195
            }
196
            getRepo().commitTransaction(tx);
197
    }
198

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

    
202
        if(event.getNewEntityType().equals(SpecimenTypeDesignation.class)){
203
            TransactionStatus tx = getRepo().startTransaction(false);
204
            Registration reg = getRepo().getRegistrationService().find(event.getRegistrationId());
205
            RegistrationDTO regDTO = new RegistrationDTO(reg);
206
            SpecimenTypeDesignationWorkingsetPopupEditor popup = getNavigationManager().showInPopup(SpecimenTypeDesignationWorkingsetPopupEditor.class);
207
            popup.showInEditor(null);
208
            popup.withDeleteButton(true);
209
            getRepo().commitTransaction(tx);
210
        }
211
    }
212

    
213
    @EventListener(classes=ShowDetailsEvent.class, condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet)")
214
    public void onShowRegistrationWorkingSetMessages(ShowDetailsEvent<?,?> event) { // WARNING don't use more specific generic type arguments
215
        List<String> messages = new ArrayList<>();
216
        for(RegistrationDTO dto : workingset.getRegistrationDTOs()){
217
            dto.getMessages().forEach(m -> messages.add(dto.getSummary() + ": " + m));
218
        }
219
        if(event.getProperty().equals("messages")){
220
            getView().openDetailsPopup("Messages", messages);
221
        }
222
    }
223

    
224
    @EventListener(classes=ShowDetailsEvent.class, condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO)")
225
    public void onShowRegistrationMessages(ShowDetailsEvent<?,?> event) { // WARNING don't use more specific generic type arguments
226
        RegistrationDTO regDto = workingSetService.loadDtoById((Integer)event.getIdentifier());
227
        if(event.getProperty().equals("messages")){
228
            if(getView() != null){
229
                getView().openDetailsPopup("Messages", regDto.getMessages());
230
            }
231
        }
232
    }
233

    
234
    @EventListener
235
    public void onEntityChangeEvent(EntityChangeEvent event){
236
        if(Reference.class.isAssignableFrom(event.getEntityType())){
237
            if(workingset.getCitationId().equals(event.getEntityId())){
238
                refreshView();
239
            }
240
        } else
241
        if(Registration.class.isAssignableFrom(event.getEntityType())){
242
            if(workingset.getRegistrations().stream().anyMatch(reg -> reg.getId() == event.getEntityId())){
243
                refreshView();
244
            }
245
        } else
246
        if(TaxonName.class.isAssignableFrom(event.getEntityType())){
247
            if(workingset.getRegistrationDTOs().stream().anyMatch(reg ->
248
                reg.getTypifiedName() != null
249
                && reg.getTypifiedName().getId() == event.getEntityId())){
250
                    refreshView();
251
            }
252
        } else
253
        if(TypeDesignationBase.class.isAssignableFrom(event.getEntityType())){
254
            if(workingset.getRegistrationDTOs().stream().anyMatch(
255
                    reg -> reg.getTypeDesignations().stream().anyMatch(
256
                            td -> td.getId() == event.getEntityId()
257
                            )
258
                        )
259
                    ){
260
                refreshView();
261
            }
262
        }
263

    
264
    }
265

    
266
    /**
267
     *
268
     */
269
    protected void refreshView() {
270
        presentWorkingSet(workingset.getCitationId());
271
    }
272

    
273
}
(12-12/17)