This repository has been archived by the owner on Oct 17, 2018. It is now read-only.
/
BahBahNotificationConsumerService.java
109 lines (97 loc) · 4.26 KB
/
BahBahNotificationConsumerService.java
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
/*******************************************************************************
* Copyright (c) 2013 BSI Business Systems Integration AG.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* BSI Business Systems Integration AG - initial API and implementation
******************************************************************************/
package org.eclipsescout.demo.bahbah.client.services;
import java.util.Date;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.scout.commons.logger.IScoutLogger;
import org.eclipse.scout.commons.logger.ScoutLogManager;
import org.eclipse.scout.rt.client.ClientAsyncJob;
import org.eclipse.scout.rt.client.ClientSyncJob;
import org.eclipse.scout.rt.client.IClientSession;
import org.eclipse.scout.rt.client.services.common.clientnotification.ClientNotificationConsumerEvent;
import org.eclipse.scout.rt.shared.services.common.clientnotification.IClientNotification;
import org.eclipse.scout.service.AbstractService;
import org.eclipsescout.demo.bahbah.client.ui.desktop.Desktop;
import org.eclipsescout.demo.bahbah.client.ui.desktop.outlines.pages.UserNodePage;
import org.eclipsescout.demo.bahbah.client.ui.forms.ChatForm;
import org.eclipsescout.demo.bahbah.shared.notification.MessageNotification;
import org.eclipsescout.demo.bahbah.shared.notification.RefreshBuddiesNotification;
public class BahBahNotificationConsumerService extends AbstractService implements IBahBahNotificationConsumerService {
private static IScoutLogger logger = ScoutLogManager.getLogger(BahBahNotificationConsumerService.class);
private void handleMessage(MessageNotification notification) {
UserNodePage userPage = getUserNodePage();
String buddy = notification.getSenderName();
if (userPage != null) {
try {
ChatForm form = userPage.getChatForm(buddy);
if (form != null) {
form.getHistoryField().addMessage(false, buddy, form.getUserName(), new Date(), notification.getMessage(), notification.getOriginalServerNode(), notification.getProvidingServerNode());
}
}
catch (Throwable t) {
logger.error("handling of remote message failed.", t);
}
}
}
@Override
public void handleEvent(ClientNotificationConsumerEvent e, boolean sync) {
logger.info("received client notification event for user '" + ClientSyncJob.getCurrentSession().getUserId() + "'");
final IClientNotification notification = e.getClientNotification();
final IClientSession session = ClientSyncJob.getCurrentSession();
// deal with notification in async jobs to prevent blocking of the model thread
if (notification instanceof RefreshBuddiesNotification) {
new ClientAsyncJob("async wrapper (refresh buddies)", session) {
@Override
protected void runVoid(IProgressMonitor monitor) throws Throwable {
new ClientSyncJob("refresh buddies", session) {
@Override
protected void runVoid(IProgressMonitor monitor1) throws Throwable {
handleRefreshBuddies();
}
}.schedule();
}
}.schedule();
}
else if (notification instanceof MessageNotification) {
new ClientAsyncJob("async wrapper (message)", session) {
@Override
protected void runVoid(IProgressMonitor monitor) throws Throwable {
new ClientSyncJob("message", session) {
@Override
protected void runVoid(IProgressMonitor monitor1) throws Throwable {
handleMessage((MessageNotification) notification);
}
}.schedule();
}
}.schedule();
}
}
private UserNodePage getUserNodePage() {
if (Desktop.get() == null) {
return null;
}
else {
return Desktop.get().getUserNodePage();
}
}
private void handleRefreshBuddies() {
UserNodePage userPage = getUserNodePage();
if (userPage != null) {
try {
logger.info("refreshing buddies on client");
userPage.updateBuddyPages();
}
catch (Throwable t) {
logger.error("handling of remote message failed.", t);
}
}
}
}