View Javadoc

1   /*
2    * Created on Jun 6, 2006 by scytacki
3    *
4    * Copyright (c) 2006 Regents of the University of California (Regents). Created
5    * by TELS, Graduate School of Education, University of California at Berkeley.
6    *
7    * This software is distributed under the GNU Lesser General Public License, v2.
8    *
9    * Permission is hereby granted, without written agreement and without license
10   * or royalty fees, to use, copy, modify, and distribute this software and its
11   * documentation for any purpose, provided that the above copyright notice and
12   * the following two paragraphs appear in all copies of this software.
13   *
14   * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16   * PURPOSE. THE SOFTWAREAND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
17   * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
18   * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
19   *
20   * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
21   * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
22   * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
23   * REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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   * @author scytacki
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  	/* (non-Javadoc)
55  	 * @see net.sf.sail.core.service.SailDataStoreService#getAgentService()
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  		// agentService is null, so make it
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  			// TODO Auto-generated catch block
76  			e.printStackTrace();
77  		}
78  
79  		try {
80  			IAgent individual = as.createAgent(Role.INDIVIDUAL);
81  			individual.addUser(previewUser.getUserUUID());
82  		} catch (RoleRuleException e) {
83  			// TODO Auto-generated catch block
84  			e.printStackTrace();
85  		}
86  
87  		// cache and return
88  		serviceForSession.put(sessionContext, as);
89  		return as;
90  	}
91  
92  
93  	/* (non-Javadoc)
94  	 * @see net.sf.sail.core.bundle.Bundle#initializeBundle(java.beans.beancontext.BeanContextServices)
95  	 */
96  	public void initializeBundle(ServiceContext serviceContext) {
97  		// TODO Auto-generated method stub
98  		this.serviceContext = serviceContext;
99  	}
100 
101 
102 	/* (non-Javadoc)
103 	 * @see net.sf.sail.core.bundle.Bundle#registerServices(java.beans.beancontext.BeanContextServices)
104 	 */
105 	public void registerServices(ServiceContext serviceContext) {
106 		serviceContext.addService(SailDataStoreService.class, this);
107 	}
108 
109 }