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
|
|
15
|
import org.springframework.beans.factory.annotation.Autowired;
|
16
|
import org.springframework.context.annotation.Scope;
|
17
|
|
18
|
import com.vaadin.spring.annotation.SpringComponent;
|
19
|
|
20
|
import eu.etaxonomy.cdm.api.service.IRegistrationService;
|
21
|
import eu.etaxonomy.cdm.ext.common.ExternalServiceException;
|
22
|
import eu.etaxonomy.cdm.ext.registration.messages.IRegistrationMessageService;
|
23
|
import eu.etaxonomy.cdm.ext.registration.messages.Message;
|
24
|
import eu.etaxonomy.cdm.model.common.User;
|
25
|
import eu.etaxonomy.cdm.model.name.Registration;
|
26
|
import eu.etaxonomy.cdm.vaadin.event.error.ExternalServiceExceptionHandler;
|
27
|
import eu.etaxonomy.cdm.vaadin.security.UserHelper;
|
28
|
import eu.etaxonomy.vaadin.mvp.AbstractPresenter;
|
29
|
|
30
|
/**
|
31
|
* @author a.kohlbecker
|
32
|
* @since Feb 27, 2018
|
33
|
*
|
34
|
*/
|
35
|
@SpringComponent
|
36
|
@Scope("prototype")
|
37
|
public class RegistrationMessagesPresenter extends AbstractPresenter<RegistrationMessagesView> {
|
38
|
|
39
|
private static final long serialVersionUID = -1069755744585623770L;
|
40
|
|
41
|
@Autowired
|
42
|
IRegistrationMessageService messageService;
|
43
|
|
44
|
@Autowired
|
45
|
IRegistrationService registrationService;
|
46
|
|
47
|
Registration registration;
|
48
|
|
49
|
@Override
|
50
|
public void onPresenterReady() {
|
51
|
getView().getErrrorHandler().registerHandler(
|
52
|
new ExternalServiceExceptionHandler("The external messages service reported an error")
|
53
|
);
|
54
|
}
|
55
|
|
56
|
/**
|
57
|
* @param identifier
|
58
|
*/
|
59
|
public void loadMessagesFor(Integer id) {
|
60
|
if(registration == null){
|
61
|
registration = registrationService.load(id, Arrays.asList("submitter"));
|
62
|
}
|
63
|
try {
|
64
|
List<Message> messages = messageService.listMessages(registration);
|
65
|
getView().showMessages("On Registration " + registration.getIdentifier(), messages);
|
66
|
} catch (ExternalServiceException e) {
|
67
|
throw new RuntimeException(e);
|
68
|
}
|
69
|
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* @param value
|
74
|
*/
|
75
|
public void postMessage(String message) {
|
76
|
|
77
|
User user = UserHelper.fromSession().user();
|
78
|
List<Message> activeMessages;
|
79
|
try {
|
80
|
activeMessages = messageService.listActiveMessagesFor(registration, user);
|
81
|
} catch (ExternalServiceException e) {
|
82
|
throw new RuntimeException(e);
|
83
|
}
|
84
|
User toUser = null;
|
85
|
if(UserHelper.fromSession().userIsRegistrationCurator()){
|
86
|
toUser = registration.getSubmitter();
|
87
|
} else {
|
88
|
Stack<Message> stack = new Stack<>();
|
89
|
stack.addAll(activeMessages);
|
90
|
while(!stack.empty()){
|
91
|
toUser = stack.pop().getFrom();
|
92
|
if(!toUser.equals(user)){
|
93
|
break;
|
94
|
}
|
95
|
}
|
96
|
if(toUser == null){
|
97
|
throw new RuntimeException("Only a curator can initiate a communication");
|
98
|
}
|
99
|
}
|
100
|
try {
|
101
|
messageService.postMessage(registration, message, user, toUser);
|
102
|
loadMessagesFor(registration.getId());
|
103
|
} catch (ExternalServiceException e) {
|
104
|
logger.error(e);
|
105
|
throw new RuntimeException(e);
|
106
|
}
|
107
|
|
108
|
|
109
|
}
|
110
|
|
111
|
}
|