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 package net.sf.sail.common.apps.preview;
27
28 import java.util.Collection;
29 import java.util.HashMap;
30 import java.util.Map;
31
32 import net.sf.sail.common.persistance.memory.MemoryPersistenceImpl;
33 import net.sf.sail.core.beans.SessionContext;
34 import net.sf.sail.core.beans.service.AgentService;
35 import net.sf.sail.core.bundle.Bundle;
36 import net.sf.sail.core.entity.IAgent;
37 import net.sf.sail.core.entity.Role;
38 import net.sf.sail.core.entity.RoleRuleException;
39 import net.sf.sail.core.entity.User;
40 import net.sf.sail.core.service.SailDataStoreService;
41 import net.sf.sail.core.service.ServiceContext;
42 import net.sf.sail.core.service.UserService;
43
44
45
46
47
48 public class PreviewSailDataStoreService
49 implements SailDataStoreService, Bundle {
50 private transient Map<SessionContext, AgentService> serviceForSession = new HashMap<SessionContext, AgentService>();
51
52 ServiceContext serviceContext;
53
54
55
56
57 public AgentService getAgentService(SessionContext sessionContext) {
58
59 UserService userService = (UserService)serviceContext.getService(UserService.class);
60
61 AgentService as = serviceForSession.get(sessionContext);
62 if (as != null)
63 return as;
64
65 Collection<User> users = userService.getParticipants();
66 User previewUser = users.iterator().next();
67
68
69 as = new MemoryPersistenceImpl();
70
71 try {
72 IAgent workgroup = as.createAgent(Role.RUN_WORKGROUP);
73 workgroup.addUser(previewUser.getUserUUID());
74 } catch (RoleRuleException e) {
75
76 e.printStackTrace();
77 }
78
79 try {
80 IAgent individual = as.createAgent(Role.INDIVIDUAL);
81 individual.addUser(previewUser.getUserUUID());
82 } catch (RoleRuleException e) {
83
84 e.printStackTrace();
85 }
86
87
88 serviceForSession.put(sessionContext, as);
89 return as;
90 }
91
92
93
94
95
96 public void initializeBundle(ServiceContext serviceContext) {
97
98 this.serviceContext = serviceContext;
99 }
100
101
102
103
104
105 public void registerServices(ServiceContext serviceContext) {
106 serviceContext.addService(SailDataStoreService.class, this);
107 }
108
109 }