Project

General

Profile

Download (8.9 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.TaxonNameBase;
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.reference.ReferencePopupEditor;
40
import eu.etaxonomy.cdm.vaadin.view.taxon.TaxonNamePopupEditor;
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
        getRepo().commitTransaction(tx);
134
    }
135

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

    
145
    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).EDIT && #event.sourceComponent == null")
146
    public void onTaxonNameEditorAction(TaxonNameEditorAction event) {
147
        TransactionStatus tx = getRepo().startTransaction(false);
148
        TaxonNameBase taxonName = getRepo().getNameService().find(event.getEntityId());
149
        TaxonNamePopupEditor popup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class);
150
        popup.showInEditor(taxonName);
151
        getRepo().commitTransaction(tx);
152
    }
153

    
154

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

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

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

    
205
    }
206

    
207
    /**
208
     *
209
     */
210
    protected void refreshView() {
211
        presentWorkingSet(workingset.getCitationId());
212
    }
213

    
214
}
(12-12/17)