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.util;
24  
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.logging.Logger;
28  
29  import net.sf.sail.common.persistance.memory.MemoryAgent;
30  import net.sf.sail.core.beans.Pod;
31  import net.sf.sail.core.beans.UnknownPodException;
32  import net.sf.sail.core.curnit.Curnit;
33  import net.sf.sail.core.entity.IAgent;
34  import net.sf.sail.core.entity.Role;
35  import net.sf.sail.core.entity.RoleRuleException;
36  import net.sf.sail.core.entity.User;
37  import net.sf.sail.core.uuid.CurnitUuid;
38  import net.sf.sail.core.uuid.OfferingUuid;
39  import net.sf.sail.core.uuid.PodUuid;
40  import net.sf.sail.core.uuid.SessionUuid;
41  import net.sf.sail.core.uuid.UserUuid;
42  
43  import org.apache.commons.lang.StringUtils;
44  import org.doomdark.uuid.UUID;
45  
46  /**
47   * Mock data to use while developing.
48   * 
49   * @author turadg
50   */
51  public class MockData {
52  	/**
53  	 * Logger for this class
54  	 */
55  	private static final Logger logger = Logger.getLogger(MockData.class
56  			.getName());
57  
58  	public static final CurnitUuid CURNIT_ID_1 = new CurnitUuid(
59  			"cccccc01-0000-0000-0000-000000000000");
60  
61  	public static final SessionUuid SESSION_ID_1 = new SessionUuid(new UUID()
62  			.toByteArray());
63  
64  	public static final OfferingUuid RUN_ID_1 = new OfferingUuid(new UUID()
65  			.toByteArray());
66  
67  	public static final UserUuid USER_ID_1 = new UserUuid(UUID.valueOf(
68  			"abcdef01-0000-0000-0000-000000000000").toByteArray());
69  
70  	public static final UserUuid USER_ID_2 = new UserUuid(UUID.valueOf(
71  			"abcdef02-0000-0000-0000-000000000000").toByteArray());
72  
73  	static int userSerial = 1;
74  
75  	public static User fakeUser(UserUuid userUuid) {
76  		return new User(userUuid, "fakeUser" + userSerial++);
77  	}
78  
79  	public static Curnit getMockCurnit() {
80  		Pod rootPod = getMockPod(0);
81  		return getMockCurnit(rootPod);
82  	}
83  
84  	public static Curnit getMockCurnit(Pod rootPod) {
85  		Curnit curnit = new Curnit();
86  		curnit.setCurnitId(CURNIT_ID_1);
87  		try {
88  			curnit.setRootPodId(rootPod.getPodId());
89  		} catch (UnknownPodException e) {
90  			// TODO Auto-generated catch block
91  			e.printStackTrace();
92  			// shouldn't ever happen
93  			throw new RuntimeException(e);
94  		}
95  		return curnit;
96  	}
97  
98  	public static Pod getMockPod(int i) {
99  		String tail = StringUtils.leftPad(Integer.toString(i), 12, '0');
100 		Pod pod = new Pod();
101 		PodUuid podId = new PodUuid("bbbbbbbb-0000-0000-0000-" + tail);
102 		pod.setPodId(podId);
103 		return pod;
104 	}
105 
106 	/**
107 	 * Give each user an INDIVIDUAL agent and put them all in one RUN_WORKGROUP
108 	 * agent
109 	 * 
110 	 * @return collection of these agents described above
111 	 */
112 	public static Collection<IAgent> simpleAgentSet(Collection<User> users) {
113 		Collection<IAgent> agents = new ArrayList<IAgent>(users.size() + 1);
114 
115 		IAgent groupAgent = new MemoryAgent(Role.RUN_WORKGROUP);
116 		agents.add(groupAgent);
117 
118 		for (User user : users) {
119 			try {
120 				groupAgent.addUser(user.getUserUUID());
121 				IAgent individualAgent = new MemoryAgent(Role.INDIVIDUAL);
122 				individualAgent.addUser(user.getUserUUID());
123 				agents.add(individualAgent);
124 			} catch (RoleRuleException e) {
125 				// TODO Auto-generated catch block
126 				logger.severe("Collection -  : exception: " + e); //$NON-NLS-1$
127 			} // expand groupAgent
128 		}
129 
130 		return agents;
131 	}
132 }