Project

General

Profile

Download (3.45 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 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.util.Arrays;
12
import java.util.List;
13
import java.util.Stack;
14
import java.util.UUID;
15

    
16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.context.annotation.Scope;
18

    
19
import com.vaadin.spring.annotation.SpringComponent;
20

    
21
import eu.etaxonomy.cdm.api.service.IRegistrationService;
22
import eu.etaxonomy.cdm.ext.common.ExternalServiceException;
23
import eu.etaxonomy.cdm.ext.registration.messages.IRegistrationMessageService;
24
import eu.etaxonomy.cdm.ext.registration.messages.Message;
25
import eu.etaxonomy.cdm.model.common.User;
26
import eu.etaxonomy.cdm.model.name.Registration;
27
import eu.etaxonomy.cdm.vaadin.event.error.ExternalServiceExceptionHandler;
28
import eu.etaxonomy.cdm.vaadin.permission.UserHelper;
29
import eu.etaxonomy.vaadin.mvp.AbstractPresenter;
30

    
31
/**
32
 * @author a.kohlbecker
33
 * @since Feb 27, 2018
34
 *
35
 */
36
@SpringComponent
37
@Scope("prototype")
38
public class RegistrationMessagesPresenter extends AbstractPresenter<RegistrationMessagesView> {
39

    
40
    private static final long serialVersionUID = -1069755744585623770L;
41

    
42
    @Autowired
43
    IRegistrationMessageService messageService;
44

    
45
    @Autowired
46
    IRegistrationService registrationService;
47

    
48
    Registration registration;
49

    
50
    @Override
51
    public void onPresenterReady() {
52
        getView().getErrrorHandler().registerHandler(
53
                new ExternalServiceExceptionHandler("The external messages service reported an error")
54
                );
55
    }
56

    
57
    /**
58
     * @param identifier
59
     */
60
    public void loadMessagesFor(UUID uuid) {
61
        if(registration == null){
62
            registration = registrationService.load(uuid, Arrays.asList("submitter"));
63
        }
64
        try {
65
            List<Message> messages = messageService.listMessages(registration);
66
            getView().showMessages("On Registration " + registration.getIdentifier(), messages);
67
        } catch (ExternalServiceException e) {
68
            throw new RuntimeException(e);
69
        }
70

    
71
    }
72

    
73
    /**
74
     * @param value
75
     */
76
    public void postMessage(String message) {
77

    
78
        User user = UserHelper.fromSession().user();
79
        List<Message> activeMessages;
80
        try {
81
            activeMessages = messageService.listActiveMessagesFor(registration, user);
82
        } catch (ExternalServiceException e) {
83
            throw new RuntimeException(e);
84
        }
85
        User toUser = null;
86
        if(UserHelper.fromSession().userIsRegistrationCurator()){
87
            toUser = registration.getSubmitter();
88
        } else {
89
            Stack<Message> stack = new Stack<>();
90
            stack.addAll(activeMessages);
91
            while(!stack.empty()){
92
                toUser = stack.pop().getFrom();
93
                if(!toUser.equals(user)){
94
                    break;
95
                }
96
            }
97
            if(toUser == null){
98
                throw new RuntimeException("Only a curator can initiate a communication");
99
            }
100
        }
101
        try {
102
            messageService.postMessage(registration, message, user, toUser);
103
            loadMessagesFor(registration.getUuid());
104
        } catch (ExternalServiceException e) {
105
            logger.error(e);
106
            throw new RuntimeException(e);
107
        }
108

    
109

    
110
    }
111

    
112
}
(8-8/20)