Project

General

Profile

Download (3.59 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.api.utility.RoleProber;
23
import eu.etaxonomy.cdm.ext.common.ExternalServiceException;
24
import eu.etaxonomy.cdm.ext.registration.messages.IRegistrationMessageService;
25
import eu.etaxonomy.cdm.ext.registration.messages.Message;
26
import eu.etaxonomy.cdm.model.common.User;
27
import eu.etaxonomy.cdm.model.name.Registration;
28
import eu.etaxonomy.cdm.service.UserHelperAccess;
29
import eu.etaxonomy.cdm.vaadin.event.error.ExternalServiceExceptionHandler;
30
import eu.etaxonomy.cdm.vaadin.permission.RolesAndPermissions;
31
import eu.etaxonomy.vaadin.mvp.AbstractPresenter;
32

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

    
42
    private static final long serialVersionUID = -1069755744585623770L;
43

    
44
    @Autowired
45
    IRegistrationMessageService messageService;
46

    
47
    @Autowired
48
    IRegistrationService registrationService;
49

    
50
    Registration registration;
51

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

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

    
73
    }
74

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

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

    
111

    
112
    }
113

    
114
}
(8-8/20)