/
EventStreamService.groovy
308 lines (220 loc) · 8.75 KB
/
EventStreamService.groovy
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package org.fogbeam.quoddy;
import java.util.Calendar
import org.fogbeam.quoddy.stream.ActivityStreamItem;
import org.fogbeam.quoddy.stream.EventType;
import org.fogbeam.quoddy.stream.ShareTarget;
import org.fogbeam.quoddy.stream.StreamItemBase;
class EventStreamService {
def userService;
def jmsService;
def eventQueueService;
def existDBService;
public void saveActivity( ActivityStreamItem activity )
{
println "about to save activity...";
if( !activity.save(flush:true) )
{
println( "Saving activity FAILED");
activity.errors.allErrors.each { println it };
}
else
{
println "Successfully saved ActivityStreamItem: ${activity.id}";
}
}
public List<ActivityStreamItem> getRecentActivitiesForUser( final User user, final int maxCount, final UserStream userStream )
{
println "getting recent activities for user, using stream: ${userStream}";
println "streamId = ${userStream.id}";
// ok, in this version we select the events to return, based on the specification defined by the
// passed in userStream instance.
// in the very earliest cut of this, we're going to ignore messages waiting on the queue and just
// go straight to the database. Once we're confident we have all the selectors and filters
// working that way, we can revisit what to do about queued messages.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR_OF_DAY, -2160 );
Date cutoffDate = cal.getTime();
List<ActivityStreamItem> recentActivityStreamItems = new ArrayList<ActivityStreamItem>();
List<User> friends = userService.listFriends( user );
if( friends != null && friends.size() >= 0 )
{
println "Found ${friends.size()} friends";
List<Integer> friendIds = new ArrayList<Integer>();
for( User friend: friends )
{
def id = friend.id;
println( "Adding friend id: ${id}, userId: ${friend.userId} to list" );
friendIds.add( id );
}
String query = "select item ";
println "query now: ${query}";
if( !userStream.includeAllUsers && !userStream.includeSelfOnly )
{
// println "filtering by user";
// query = query + ", stream ";
}
println "query now: ${query}";
query = query + " from ActivityStreamItem as item ";
println "query now: ${query}";
if( !userStream.includeAllUsers && !userStream.includeSelfOnly )
{
println "filtering by user";
query = query + ", UserStream as stream ";
}
println "query now: ${query}";
query = query + " where item.published >= :cutoffDate " +
" and ( item.owner.id in (:friendIds) " +
" and not ( item.owner <> :owner and item.objectClass = 'BusinessEventSubscriptionItem' ) " +
" and not ( item.owner <> :owner and item.objectClass = 'CalendarFeedItem' ) " +
" and not ( item.owner <> :owner and item.objectClass = 'RssFeedItem' ) " +
" and not ( item.owner <> :owner and item.objectClass = 'ActivitiUserTask' ) " +
") " +
" and ( item.targetUuid = :targetUuid or item.targetUuid = :userUuid)";
println "query now: ${query}";
/* deal with event type filter */
if( userStream.includeAllEventTypes )
{
// don't do anything, the default query returns all event types
println "selecting ALL event types";
}
else
{
println "filtering by event type";
// start limiting the return types based on what we have in the
// stream object...
Set<EventType> eventTypesToInclude = userStream.getEventTypesToInclude();
println "query was: ${query}";
boolean first = true;
for( EventType eventType : eventTypesToInclude )
{
println "adding eventType: ${eventType}";
if( first )
{
query = query + " and (";
first = false;
}
else
{
query = query + " or ";
}
query = query + " item.objectClass = " + eventType.name;
}
query = query + " )";
println "query now: ${query}";
}
/* deal with user filter */
if( userStream.includeAllUsers )
{
// nothing to do, default query will return hits from all eligible users
}
else if( userStream.includeSelfOnly )
{
// left alone, the existing default query would return posts from any
// user in the friends list. We should rework the entire base query
// but - for now - we can cheat and just add an extra and clause to
// filter down to the user id of our user
query = query + " and item.owner = :owner";
}
else
{
println "adding userUuidsIncluded and streamid filters to query";
query = query + " and item.owner.uuid in elements( stream.userUuidsIncluded ) and stream.id = :streamId ) ";
}
/* deal with user list filter */
/* deal with group filter */
/* deal with subscription filter */
query = query + " order by item.published desc";
println "executing query: $query";
// for the purpose of this query, treat a user as their own friend... that is, we
// will want to read Activities created by this user (we see our own updates in our
// own feed)
friendIds.add( user.id );
ShareTarget streamPublic = ShareTarget.findByName( ShareTarget.STREAM_PUBLIC );
def parameters = ['cutoffDate':cutoffDate,
// 'oldestOriginTime':new Date(oldestOriginTime),
'friendIds':friendIds,
'targetUuid':streamPublic.uuid, 'owner': user, 'userUuid': user.uuid]
if( !userStream.includeAllUsers && !userStream.includeSelfOnly )
{
parameters << ['streamId':userStream.id]
}
println "Using parameters map: ${parameters}";
List<ActivityStreamItem> queryResults =
ActivityStreamItem.executeQuery( query,
parameters,
['max': maxCount ]);
println "adding ${queryResults.size()} activities read from DB";
for( ActivityStreamItem event : queryResults ) {
println "Populating XML into SubscriptionEvents";
println "event = ${event}";
event.streamObject = existDBService.populateSubscriptionEventWithXmlDoc( event.streamObject );
recentActivityStreamItems.add( event );
}
}
else
{
println( "no friends, so no activity read from DB" );
}
return recentActivityStreamItems;
}
public StreamItemBase getEventById( final long eventId )
{
StreamItemBase event = StreamItemBase.findById( eventId );
return event;
}
public StreamItemBase getEventByUuid( final String uuid )
{
StreamItemBase event = StreamItemBase.findByUuid( uuid );
return event;
}
public ActivityStreamItem getActivityStreamItemById( final long itemId )
{
ActivityStreamItem item = ActivityStreamItem.findById( itemId );
return item;
}
public ActivityStreamItem getActivityStreamItemByUuid( final String uuid )
{
ActivityStreamItem item = ActivityStreamItem.findByUuid( uuid );
return item;
}
// NOTE: we will probably need a version of this that supports
// chunking, since returning *every* item in the stream will consume
// massive memory once the system has been in use for a while.
public List<StreamItemBase> getAllStreamItems()
{
List<StreamItemBase> items = new ArrayList<StreamItemBase>();
items.addAll( StreamItemBase.findAll() );
return items;
}
// NOTE: we will probably need a version of this that supports
// chunking, since returning *every* item in the stream will consume
// massive memory once the system has been in use for a while.
public List<ActivityStreamItem> getAllActivityStreamItems()
{
/* NOTE: We'll also need a way to not return multiple ASI's that point
* to the same streamObject, once we support sharing / re-sharing of stream
* entries
*/
List<ActivityStreamItem> items = new ArrayList<ActivityStreamItem>();
items.addAll( ActivityStreamItem.findAll() );
return items;
}
/*
* NOTE: this version just assumes we're only looking for things shared to "Public".
* we haven't yet really fully integrated the ability to select different "ShareTarget"
* instances, but we'll have to account for that later.
*/
public List<ActivityStreamItem> getStatusUpdatesForUser( final User user )
{
List<ActivityStreamItem> statusUpdatesForUser = new ArrayList<ActivityStreamItem>();
ShareTarget streamPublic = ShareTarget.findByName( ShareTarget.STREAM_PUBLIC );
// TODO: write query to get activitystreamitem (status updates) for a User
List<ActivityStreamItem> results =
ActivityStreamItem.executeQuery(
"select actItem from ActivityStreamItem as actItem where actItem.owner = :owner " +
" and actItem.verb = :verb and actItem.targetUuid = :targetUuid",
['owner':user, verb:'quoddy_status_update', targetUuid:streamPublic.uuid] );
statusUpdatesForUser.addAll( results );
return statusUpdatesForUser;
}
}