View Javadoc

1   /*
2    * Copyright (c) 2005 Regents of the University of California (Regents). Created
3    * by TELS, Graduate School of Education, University of California at Berkeley.
4    *
5    * This software is distributed under the GNU Lesser General Public License, v2.
6    *
7    * Permission is hereby granted, without written agreement and without license
8    * or royalty fees, to use, copy, modify, and distribute this software and its
9    * documentation for any purpose, provided that the above copyright notice and
10   * the following two paragraphs appear in all copies of this software.
11   *
12   * REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
13   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
14   * PURPOSE. THE SOFTWAREAND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED
15   * HEREUNDER IS PROVIDED "AS IS". REGENTS HAS NO OBLIGATION TO PROVIDE
16   * MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17   *
18   * IN NO EVENT SHALL REGENTS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
19   * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
20   * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
21   * REGENTS HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22   */
23  package net.sf.sail.common.apps.preview;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import net.sf.sail.common.persistance.memory.MemoryPersistenceImpl;
29  import net.sf.sail.core.beans.SessionContext;
30  import net.sf.sail.core.beans.service.AgentService;
31  import net.sf.sail.core.beans.service.AnnotationService;
32  import net.sf.sail.core.curnit.ICurnit;
33  import net.sf.sail.core.entity.IAgent;
34  import net.sf.sail.core.entity.Offering;
35  import net.sf.sail.core.entity.Role;
36  import net.sf.sail.core.entity.RoleRuleException;
37  import net.sf.sail.core.entity.User;
38  import net.sf.sail.core.session.SessionDataService;
39  import net.sf.sail.core.uuid.OfferingUuid;
40  import net.sf.sail.core.uuid.SessionUuid;
41  import net.sf.sail.core.uuid.UserUuid;
42  
43  import org.doomdark.uuid.UUID;
44  
45  /**
46   * @author turadg
47   */
48  public class PreviewSessionDataService implements SessionDataService {
49  
50  	public static final UserUuid PREVIEW_USERID = new UserUuid(UUID.valueOf(
51  			"abcdef02-0000-0000-0000-000000000000").toByteArray());
52  
53  	public static final User PREVIEW_USER = new User(PREVIEW_USERID,
54  			"Preview User");
55  
56  	public static final OfferingUuid PREVIEW_RUNID = new OfferingUuid(
57  			new UUID().toByteArray());
58  
59  	public static final SessionUuid PREVIEW_SESSIONID = new SessionUuid(
60  			new UUID().toByteArray());
61  
62  	private ICurnit curnit;
63  
64  	private transient Map<SessionContext, AgentService> serviceForSession = new HashMap<SessionContext, AgentService>();
65  
66  	/**
67  	 * @param curnitArchiveUrl
68  	 * @param participants
69  	 */
70  	public PreviewSessionDataService(ICurnit curnit) {
71  		this.curnit = curnit;
72  	}
73  
74  	protected ICurnit getCurnit() {
75  		return curnit;
76  	}
77  
78  	public AgentService getPersistenceService(SessionContext sessionContext) {
79  		AgentService as = serviceForSession.get(sessionContext);
80  		if (as != null)
81  			return as;
82  
83  		// agentService is null, so make it
84  		as = new MemoryPersistenceImpl();
85  
86  		try {
87  			IAgent workgroup = as.createAgent(Role.RUN_WORKGROUP); 
88  			workgroup.addUser(PREVIEW_USER.getUserUUID());
89  		} catch (RoleRuleException e) {
90  			// TODO Auto-generated catch block
91  			e.printStackTrace();
92  		}
93  
94  		try {
95  			IAgent individual = as.createAgent(Role.INDIVIDUAL);
96  			individual.addUser(PREVIEW_USER.getUserUUID());
97  		} catch (RoleRuleException e) {
98  			// TODO Auto-generated catch block
99  			e.printStackTrace();
100 		}
101 
102 		// cache and return
103 		serviceForSession.put(sessionContext, as);
104 		return as;
105 	}
106 
107 	public User getUser(UserUuid userUuid) {
108 		if (!userUuid.equals(PREVIEW_USERID))
109 			throw new IllegalArgumentException();
110 		return PREVIEW_USER;
111 	}
112 
113 	public Offering getOffering() {
114 		Offering run = new Offering();
115 		run.setCurnit(getCurnit());
116 		run.setDescription("Preview Run");
117 		run.setOfferingId(PREVIEW_RUNID);
118 		return run;
119 	}
120 
121 	/* (non-Javadoc)
122 	 * @see net.sf.sail.core.session.SessionDataService#getAnnotationService()
123 	 */
124 	public AnnotationService getAnnotationService()
125 	{
126 		// TODO Auto-generated method stub
127 		return null;
128 	}
129 
130 }