Project

General

Profile

Download (9.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.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.TaxonName;
27
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
28
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
29
import eu.etaxonomy.cdm.model.reference.Reference;
30
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
31
import eu.etaxonomy.cdm.service.IRegistrationWorkingSetService;
32
import eu.etaxonomy.cdm.vaadin.event.EntityChangeEvent;
33
import eu.etaxonomy.cdm.vaadin.event.ReferenceEditorAction;
34
import eu.etaxonomy.cdm.vaadin.event.RegistrationEditorAction;
35
import eu.etaxonomy.cdm.vaadin.event.ShowDetailsEvent;
36
import eu.etaxonomy.cdm.vaadin.event.TaxonNameEditorAction;
37
import eu.etaxonomy.cdm.vaadin.event.registration.RegistrationWorkflowEvent;
38
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
39
import eu.etaxonomy.cdm.vaadin.view.name.TaxonNamePopupEditor;
40
import eu.etaxonomy.cdm.vaadin.view.reference.ReferencePopupEditor;
41
import eu.etaxonomy.vaadin.mvp.AbstractPresenter;
42

    
43
/**
44
 * @author a.kohlbecker
45
 * @since Mar 3, 2017
46
 *
47
 */
48
@SpringComponent
49
@ViewScope
50
public class RegistrationWorkflowPresenter extends AbstractPresenter<RegistrationWorkflowView> implements Serializable{
51

    
52
    private static final long serialVersionUID = 1L;
53

    
54
    @Autowired
55
    private IRegistrationWorkingSetService workingSetService;
56

    
57
    private RegistrationWorkingSet workingset;
58

    
59
    /**
60
     *
61
     */
62
    public RegistrationWorkflowPresenter() {
63
    }
64

    
65
    @EventListener
66
    protected void onRegistrationStartEvent(RegistrationWorkflowEvent event){
67

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

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

    
90
    }
91

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

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

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

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

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

    
146
    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).EDIT && #event.sourceComponent == null")
147
    public void onTaxonNameEditorAction(TaxonNameEditorAction event) {
148
        TransactionStatus tx = getRepo().startTransaction(false);
149
        TaxonName taxonName = getRepo().getNameService().find(event.getEntityId());
150
        TaxonNamePopupEditor popup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class);
151
        popup.showInEditor(taxonName);
152
        popup.withDeleteButton(true);
153
        // in the registration application inReferences should only edited centrally
154
        popup.getNomReferenceCombobox().setEnabled(false);
155
        getRepo().commitTransaction(tx);
156
    }
157

    
158

    
159
    @EventListener(classes=ShowDetailsEvent.class, condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet)")
160
    public void onShowRegistrationWorkingSetMessages(ShowDetailsEvent<?,?> event) { // WARNING don't use more specific generic type arguments
161
        List<String> messages = new ArrayList<>();
162
        for(RegistrationDTO dto : workingset.getRegistrationDTOs()){
163
            dto.getMessages().forEach(m -> messages.add(dto.getSummary() + ": " + m));
164
        }
165
        if(event.getProperty().equals("messages")){
166
            getView().openDetailsPopup("Messages", messages);
167
        }
168
    }
169

    
170
    @EventListener(classes=ShowDetailsEvent.class, condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO)")
171
    public void onShowRegistrationMessages(ShowDetailsEvent<?,?> event) { // WARNING don't use more specific generic type arguments
172
        RegistrationDTO regDto = workingSetService.loadDtoById((Integer)event.getIdentifier());
173
        if(event.getProperty().equals("messages")){
174
            if(getView() != null){
175
                getView().openDetailsPopup("Messages", regDto.getMessages());
176
            }
177
        }
178
    }
179

    
180
    @EventListener
181
    public void onEntityChangeEvent(EntityChangeEvent event){
182
        if(Reference.class.isAssignableFrom(event.getEntityType())){
183
            if(workingset.getCitationId().equals(event.getEntityId())){
184
                refreshView();
185
            }
186
        } else
187
        if(Registration.class.isAssignableFrom(event.getEntityType())){
188
            if(workingset.getRegistrations().stream().anyMatch(reg -> reg.getId() == event.getEntityId())){
189
                refreshView();
190
            }
191
        } else
192
        if(TaxonName.class.isAssignableFrom(event.getEntityType())){
193
            if(workingset.getRegistrationDTOs().stream().anyMatch(reg ->
194
                reg.getTypifiedName() != null
195
                && reg.getTypifiedName().getId() == event.getEntityId())){
196
                    refreshView();
197
            }
198
        } else
199
        if(TypeDesignationBase.class.isAssignableFrom(event.getEntityType())){
200
            if(workingset.getRegistrationDTOs().stream().anyMatch(
201
                    reg -> reg.getTypeDesignations().values().stream().anyMatch(
202
                            tds -> tds.stream().anyMatch(
203
                                    td -> td.getId() == event.getEntityId()
204
                                    ))
205
                            )
206
                    ){
207
                refreshView();
208
            }
209
        }
210

    
211
    }
212

    
213
    /**
214
     *
215
     */
216
    protected void refreshView() {
217
        presentWorkingSet(workingset.getCitationId());
218
    }
219

    
220
}
(12-12/17)