Project

General

Profile

Download (11.7 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.List;
14

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

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

    
24
import eu.etaxonomy.cdm.model.name.Rank;
25
import eu.etaxonomy.cdm.model.name.Registration;
26
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
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.model.reference.ReferenceFactory;
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.event.registration.RegistrationWorkflowEvent;
40
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
41
import eu.etaxonomy.cdm.vaadin.util.converter.TypeDesignationSetManager.TypeDesignationWorkingSet;
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

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

    
56
    private static final long serialVersionUID = 1L;
57

    
58
    @Autowired
59
    private IRegistrationWorkingSetService workingSetService;
60

    
61
    private RegistrationWorkingSet workingset;
62

    
63
    /**
64
     *
65
     */
66
    public RegistrationWorkflowPresenter() {
67
    }
68

    
69
    @EventListener
70
    protected void onRegistrationStartEvent(RegistrationWorkflowEvent event){
71

    
72
        boolean HACK = true;
73
        if(workingset != null && !HACK){
74
            Logger.getLogger(RegistrationWorkflowPresenter.class).warn("Can't start a new workflow over an existing one.");
75
            return;
76
        }
77

    
78
        if(event.isStart()) {
79
            workingset = new RegistrationWorkingSet();
80
            Registration reg = Registration.NewInstance();
81
            reg.setName(TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES()));
82
            getView().setHeaderText("New Registration");
83
            try {
84
                workingset.add(reg);
85
            } catch (RegistrationValidationException error) {
86
                getView().getWorkflow().setComponentError(new SystemError(error));
87
            }
88
            getView().setWorkingset(workingset);
89
        } else {
90
            Integer citationID = event.getCitationID();
91
            presentWorkingSetByRegID(citationID);
92
        }
93

    
94
    }
95

    
96
    /**
97
     * @param registrationID
98
     * @deprecated use other method working sets should only be addressed by the referenceID
99
     */
100
    @Deprecated
101
    private void presentWorkingSetByRegID(Integer citationID) {
102
        try {
103
            workingset = workingSetService.loadWorkingSetByCitationID(citationID);
104
        } catch (RegistrationValidationException error) {
105
            getView().getWorkflow().setComponentError(new SystemError(error));
106
        }
107
        getView().setHeaderText("Registrations in " + workingset.getCitation());
108
        getView().setWorkingset(workingset);
109
    }
110

    
111
    /**
112
     * @param registrationID
113
     */
114
    private void presentWorkingSet(Integer referenceID) {
115
        try {
116
            workingset = workingSetService.loadWorkingSetByReferenceID(referenceID);
117
        } catch (RegistrationValidationException error) {
118
            getView().getWorkflow().setComponentError(new SystemError(error));
119
        }
120
        getView().setHeaderText("Registration for " + workingset.getCitation());
121
        getView().setWorkingset(workingset);
122
    }
123

    
124
    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).ADD && #event.sourceComponent == null")
125
    public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
126
        Reference reference = ReferenceFactory.newGeneric();
127
        ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
128
        popup.showInEditor(reference);
129
    }
130

    
131
    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).EDIT && #event.sourceComponent == null")
132
    public void onReferenceEditorActionEdit(ReferenceEditorAction event) {
133
        TransactionStatus tx = getRepo().startTransaction(false);
134
        Reference reference = getRepo().getReferenceService().find(event.getEntityId());
135
        ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
136
        popup.showInEditor(reference);
137
        popup.withDeleteButton(true);
138
        getRepo().commitTransaction(tx);
139
    }
140

    
141
    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).EDIT && #event.sourceComponent == null")
142
    public void onRegistrationEditorAction(RegistrationEditorAction event) {
143
        TransactionStatus tx = getRepo().startTransaction(false);
144
        Registration registration = getRepo().getRegistrationService().find(event.getEntityId());
145
        RegistrationPopupEditor popup = getNavigationManager().showInPopup(RegistrationPopupEditor.class);
146
        popup.showInEditor(registration);
147
        getRepo().commitTransaction(tx);
148
    }
149

    
150
    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).EDIT && #event.sourceComponent == null")
151
    public void onTaxonNameEditorAction(TaxonNameEditorAction event) {
152
        TransactionStatus tx = getRepo().startTransaction(false);
153
        // FIXME optional:
154
        // A) allow full initialization of the entity here, the Presenter of the Editor should
155
        //    provide the intiStrategy via a static method so that it can be used
156
        // B) only pass the identifier (Object) to the View and the Presenter is responsible for
157
        //    loading and initializing  the entity
158
        TaxonName taxonName = getRepo().getNameService().find(event.getEntityId());
159
        TaxonNamePopupEditor popup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class);
160
        popup.showInEditor(taxonName);
161
        popup.withDeleteButton(true);
162
        // in the registration application inReferences should only edited centrally
163
        popup.getNomReferenceCombobox().setEnabled(false);
164
        getRepo().commitTransaction(tx);
165
    }
166

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

    
170
            TransactionStatus tx = getRepo().startTransaction(false);
171
            Registration reg = getRepo().getRegistrationService().find(event.getRegistrationId());
172
            RegistrationDTO regDTO = new RegistrationDTO(reg);
173
            TypeDesignationWorkingSet typeDesignationWorkingSet = regDTO.getTypeDesignationWorkingSet(event.getEntityId());
174
            if(typeDesignationWorkingSet.isSpecimenTypeDesigationWorkingSet()){
175
                // check SpecimenTypeDesignation
176
                SpecimenTypeDesignationWorkingsetPopupEditor popup = getNavigationManager().showInPopup(SpecimenTypeDesignationWorkingsetPopupEditor.class);
177
                popup.showInEditor(regDTO.getSpecimenTypeDesignationWorkingSetDTO(typeDesignationWorkingSet.getBaseEntityReference()));
178
            } else {
179
                // FIXME implement NameTypeDesignationWorkingsetPopupEditor
180
            }
181
            getRepo().commitTransaction(tx);
182
    }
183

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

    
187
        if(event.getNewEntityType().equals(SpecimenTypeDesignation.class)){
188
            TransactionStatus tx = getRepo().startTransaction(false);
189
            Registration reg = getRepo().getRegistrationService().find(event.getRegistrationId());
190
            RegistrationDTO regDTO = new RegistrationDTO(reg);
191
            SpecimenTypeDesignationWorkingsetPopupEditor popup = getNavigationManager().showInPopup(SpecimenTypeDesignationWorkingsetPopupEditor.class);
192
            popup.showInEditor(null);
193
            popup.withDeleteButton(true);
194
            getRepo().commitTransaction(tx);
195
        }
196
    }
197

    
198
    @EventListener(classes=ShowDetailsEvent.class, condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet)")
199
    public void onShowRegistrationWorkingSetMessages(ShowDetailsEvent<?,?> event) { // WARNING don't use more specific generic type arguments
200
        List<String> messages = new ArrayList<>();
201
        for(RegistrationDTO dto : workingset.getRegistrationDTOs()){
202
            dto.getMessages().forEach(m -> messages.add(dto.getSummary() + ": " + m));
203
        }
204
        if(event.getProperty().equals("messages")){
205
            getView().openDetailsPopup("Messages", messages);
206
        }
207
    }
208

    
209
    @EventListener(classes=ShowDetailsEvent.class, condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO)")
210
    public void onShowRegistrationMessages(ShowDetailsEvent<?,?> event) { // WARNING don't use more specific generic type arguments
211
        RegistrationDTO regDto = workingSetService.loadDtoById((Integer)event.getIdentifier());
212
        if(event.getProperty().equals("messages")){
213
            if(getView() != null){
214
                getView().openDetailsPopup("Messages", regDto.getMessages());
215
            }
216
        }
217
    }
218

    
219
    @EventListener
220
    public void onEntityChangeEvent(EntityChangeEvent event){
221
        if(Reference.class.isAssignableFrom(event.getEntityType())){
222
            if(workingset.getCitationId().equals(event.getEntityId())){
223
                refreshView();
224
            }
225
        } else
226
        if(Registration.class.isAssignableFrom(event.getEntityType())){
227
            if(workingset.getRegistrations().stream().anyMatch(reg -> reg.getId() == event.getEntityId())){
228
                refreshView();
229
            }
230
        } else
231
        if(TaxonName.class.isAssignableFrom(event.getEntityType())){
232
            if(workingset.getRegistrationDTOs().stream().anyMatch(reg ->
233
                reg.getTypifiedName() != null
234
                && reg.getTypifiedName().getId() == event.getEntityId())){
235
                    refreshView();
236
            }
237
        } else
238
        if(TypeDesignationBase.class.isAssignableFrom(event.getEntityType())){
239
            if(workingset.getRegistrationDTOs().stream().anyMatch(
240
                    reg -> reg.getTypeDesignations().stream().anyMatch(
241
                            td -> td.getId() == event.getEntityId()
242
                            )
243
                        )
244
                    ){
245
                refreshView();
246
            }
247
        }
248

    
249
    }
250

    
251
    /**
252
     *
253
     */
254
    protected void refreshView() {
255
        presentWorkingSet(workingset.getCitationId());
256
    }
257

    
258
}
(12-12/17)