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

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

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

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

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

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

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

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

    
263
    }
264

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

    
272
}
(12-12/17)