Project

General

Profile

Download (9.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.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.TypeDesignationWorkingsetEditorAction;
38
import eu.etaxonomy.cdm.vaadin.event.registration.RegistrationWorkflowEvent;
39
import eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet;
40
import eu.etaxonomy.cdm.vaadin.view.name.TaxonNamePopupEditor;
41
import eu.etaxonomy.cdm.vaadin.view.reference.ReferencePopupEditor;
42
import eu.etaxonomy.vaadin.mvp.AbstractPresenter;
43

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

    
53
    private static final long serialVersionUID = 1L;
54

    
55
    @Autowired
56
    private IRegistrationWorkingSetService workingSetService;
57

    
58
    private RegistrationWorkingSet workingset;
59

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

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

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

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

    
91
    }
92

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

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

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

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

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

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

    
159
    @EventListener(condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Action).EDIT && #event.sourceComponent == null")
160
    public void onTypedesignationsEditorAction(TypeDesignationWorkingsetEditorAction event) {
161
        TransactionStatus tx = getRepo().startTransaction(false);
162
        TaxonName taxonName = getRepo().getNameService().find(event.getEntityId());
163
        TaxonNamePopupEditor popup = getNavigationManager().showInPopup(TaxonNamePopupEditor.class);
164
        popup.showInEditor(taxonName);
165
        popup.withDeleteButton(true);
166
        // in the registration application inReferences should only edited centrally
167
        popup.getNomReferenceCombobox().setEnabled(false);
168
        getRepo().commitTransaction(tx);
169
    }
170

    
171
    @EventListener(classes=ShowDetailsEvent.class, condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet)")
172
    public void onShowRegistrationWorkingSetMessages(ShowDetailsEvent<?,?> event) { // WARNING don't use more specific generic type arguments
173
        List<String> messages = new ArrayList<>();
174
        for(RegistrationDTO dto : workingset.getRegistrationDTOs()){
175
            dto.getMessages().forEach(m -> messages.add(dto.getSummary() + ": " + m));
176
        }
177
        if(event.getProperty().equals("messages")){
178
            getView().openDetailsPopup("Messages", messages);
179
        }
180
    }
181

    
182
    @EventListener(classes=ShowDetailsEvent.class, condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO)")
183
    public void onShowRegistrationMessages(ShowDetailsEvent<?,?> event) { // WARNING don't use more specific generic type arguments
184
        RegistrationDTO regDto = workingSetService.loadDtoById((Integer)event.getIdentifier());
185
        if(event.getProperty().equals("messages")){
186
            if(getView() != null){
187
                getView().openDetailsPopup("Messages", regDto.getMessages());
188
            }
189
        }
190
    }
191

    
192
    @EventListener
193
    public void onEntityChangeEvent(EntityChangeEvent event){
194
        if(Reference.class.isAssignableFrom(event.getEntityType())){
195
            if(workingset.getCitationId().equals(event.getEntityId())){
196
                refreshView();
197
            }
198
        } else
199
        if(Registration.class.isAssignableFrom(event.getEntityType())){
200
            if(workingset.getRegistrations().stream().anyMatch(reg -> reg.getId() == event.getEntityId())){
201
                refreshView();
202
            }
203
        } else
204
        if(TaxonName.class.isAssignableFrom(event.getEntityType())){
205
            if(workingset.getRegistrationDTOs().stream().anyMatch(reg ->
206
                reg.getTypifiedName() != null
207
                && reg.getTypifiedName().getId() == event.getEntityId())){
208
                    refreshView();
209
            }
210
        } else
211
        if(TypeDesignationBase.class.isAssignableFrom(event.getEntityType())){
212
            if(workingset.getRegistrationDTOs().stream().anyMatch(
213
                    reg -> reg.getTypeDesignations().stream().anyMatch(
214
                            td -> td.getId() == event.getEntityId()
215
                            )
216
                        )
217
                    ){
218
                refreshView();
219
            }
220
        }
221

    
222
    }
223

    
224
    /**
225
     *
226
     */
227
    protected void refreshView() {
228
        presentWorkingSet(workingset.getCitationId());
229
    }
230

    
231
}
(12-12/17)