Project

General

Profile

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

    
39
/**
40
 * @author a.kohlbecker
41
 * @since Mar 3, 2017
42
 *
43
 */
44
@SpringComponent
45
@ViewScope
46
public class RegistrationWorkflowPresenter extends AbstractPresenter<RegistrationWorkflowView> implements Serializable{
47

    
48
    private static final long serialVersionUID = 1L;
49

    
50
    @Autowired
51
    private IRegistrationWorkingSetService workingSetService;
52

    
53
    private RegistrationWorkingSet workingset;
54

    
55
    /**
56
     *
57
     */
58
    public RegistrationWorkflowPresenter() {
59
    }
60

    
61
    @EventListener
62
    protected void onRegistrationStartEvent(RegistrationWorkflowEvent event){
63

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

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

    
86
    }
87

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

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

    
116
    @EventListener(condition = "#event.type ==T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Type).ADD")
117
    public void onReferenceEditorActionAdd(ReferenceEditorAction event) {
118
        Reference reference = ReferenceFactory.newGeneric();
119
        ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
120
        popup.showInEditor(reference);
121
    }
122

    
123
    @EventListener(condition = "#event.type ==T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Type).EDIT")
124
    public void onReferenceEditorActionEdit(ReferenceEditorAction event) {
125
        TransactionStatus tx = getRepo().startTransaction(false);
126
        Reference reference = getRepo().getReferenceService().find(event.getEntityId());
127
        ReferencePopupEditor popup = getNavigationManager().showInPopup(ReferencePopupEditor.class);
128
        popup.showInEditor(reference);
129
        getRepo().commitTransaction(tx);
130
    }
131

    
132
    @EventListener(condition = "#event.type ==T(eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction.Type).EDIT")
133
    public void onRegistrationEditorAction(RegistrationEditorAction event) {
134
        TransactionStatus tx = getRepo().startTransaction(false);
135
        Registration registration = getRepo().getRegistrationService().find(event.getEntityId());
136
        RegistrationPopupEditor popup = getNavigationManager().showInPopup(RegistrationPopupEditor.class);
137
        popup.showInEditor(registration);
138
        getRepo().commitTransaction(tx);
139
    }
140

    
141

    
142
    @EventListener(classes=ShowDetailsEvent.class, condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.model.registration.RegistrationWorkingSet)")
143
    public void onShowRegistrationWorkingSetMessages(ShowDetailsEvent<?,?> event) { // WARNING don't use more specific generic type arguments
144
        List<String> messages = new ArrayList<>();
145
        for(RegistrationDTO dto : workingset.getRegistrationDTOs()){
146
            dto.getMessages().forEach(m -> messages.add(dto.getSummary() + ": " + m));
147
        }
148
        if(event.getProperty().equals("messages")){
149
            getView().openDetailsPopup("Messages", messages);
150
        }
151
    }
152

    
153
    @EventListener(classes=ShowDetailsEvent.class, condition = "#event.type == T(eu.etaxonomy.cdm.vaadin.view.registration.RegistrationDTO)")
154
    public void onShowRegistrationMessages(ShowDetailsEvent<?,?> event) { // WARNING don't use more specific generic type arguments
155
        RegistrationDTO regDto = workingSetService.loadDtoById((Integer)event.getIdentifier());
156
        if(event.getProperty().equals("messages")){
157
            if(getView() != null){
158
                getView().openDetailsPopup("Messages", regDto.getMessages());
159
            }
160
        }
161
    }
162

    
163
    @EventListener
164
    public void onEntityChangeEvent(EntityChangeEvent event){
165
        if(event.getEntityType().isAssignableFrom(Reference.class)){
166
            if(workingset.getCitationId().equals(event.getEntityId())){
167
                refreshView();
168
            }
169
        }
170
        if(event.getEntityType().isAssignableFrom(Registration.class)){
171
            if(workingset.getRegistrations().stream().anyMatch(reg -> reg.getId() == event.getEntityId())){
172
                refreshView();
173
            }
174
        }
175

    
176
    }
177

    
178
    /**
179
     *
180
     */
181
    protected void refreshView() {
182
        presentWorkingSet(workingset.getCitationId());
183
    }
184

    
185
}
(12-12/17)