BotBean.java
2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package fi.codecrew.moya.beans;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Singleton;
import javax.jms.JMSException;
import javax.jms.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import fi.codecrew.moya.facade.EventFacade;
import fi.codecrew.moya.facade.EventUserFacade;
import fi.codecrew.moya.facade.UserFacade;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.LanEvent;
import fi.codecrew.moya.model.User;
import fi.codecrew.moya.utilities.moyamessage.MoyaEventMessage;
import fi.codecrew.moya.utilities.moyamessage.MoyaEventType;
import fi.iudex.utils.irc.IrcBot;
/**
* Session Bean implementation class BotBean
*/
@Singleton
@LocalBean
@Lock(LockType.READ)
public class BotBean implements BotBeanLocal {
private static final Logger logger = LoggerFactory.getLogger(BotBean.class);
/**
* Default constructor.
*/
public BotBean() {
}
private IrcBot bot;
@EJB
private EventFacade eventfacade;
@EJB
private UserFacade userfacade;
@EJB
private EventUserFacade eventuserfacade;
private final Set<MoyaEventType> ignoreTypes = ConcurrentHashMap.newKeySet();
public void message(Message message) {
try {
MoyaEventMessage msg = message.getBody(MoyaEventMessage.class);
// do not flood irc-channel, message only really needed ones
if (msg.getEventtype() != null && getIgnoreTypes().contains(msg.getEventtype()))
return;
String event = "unknown event";
if (msg.getEventId() != null) {
LanEvent e = eventfacade.find(msg.getEventId());
if (e != null)
event = e.getName();
}
StringBuilder sb = new StringBuilder();
sb.append(event).append(" ");
sb.append(msg.getEventtype()).append(" ");
if (msg.getCurrentUserId() != null) {
User user = userfacade.find(msg.getCurrentUserId());
if (user != null) {
sb.append("user: ").append(user.getLogin()).append(" ");
}
}
if (msg.getEventUserId() != null) {
EventUser eu = eventuserfacade.find(msg.getEventUserId());
if (eu != null && !eu.getUser().getId().equals(msg.getCurrentUserId())) {
sb.append("for user: ").append(eu.getUser().getLogin()).append(": ");
}
}
sb.append(msg.getDescription());
bot.say(sb.toString());
} catch (JMSException e) {
logger.warn("Error sending message", e);
bot.say("Error parsing message " + e.getMessage());
}
}
@Override
public Set<MoyaEventType> getIgnoreTypes() {
return ignoreTypes;
}
public IrcBot getBot() {
return bot;
}
public void setBot(IrcBot bot) {
this.bot = bot;
}
@Override
public void addBot(IrcBot bot) {
setBot(bot);
}
}