View Javadoc
1   package de.fhdw.wtf.context.core;
2   
3   import java.math.BigInteger;
4   import java.util.Collection;
5   import java.util.HashMap;
6   import java.util.Iterator;
7   import java.util.Map;
8   import java.util.Vector;
9   
10  import de.fhdw.wtf.context.exception.FrameworkException;
11  import de.fhdw.wtf.persistence.facade.IDManager;
12  import de.fhdw.wtf.persistence.facade.TypeManager;
13  import de.fhdw.wtf.persistence.facade.TypeManagerImplementation;
14  import de.fhdw.wtf.persistence.meta.IntegerValue;
15  import de.fhdw.wtf.persistence.meta.Link;
16  import de.fhdw.wtf.persistence.meta.MapLink;
17  import de.fhdw.wtf.persistence.meta.Object;
18  import de.fhdw.wtf.persistence.meta.StringValue;
19  import de.fhdw.wtf.persistence.meta.UnidirectionalLink;
20  import de.fhdw.wtf.persistence.meta.UserObject;
21  import de.fhdw.wtf.persistence.meta.UserType;
22  import de.fhdw.wtf.persistence.utils.Tuple;
23  
24  /**
25   * An implementation of a Context, which can be used in Test cases. It partly simulates the behavior of the persistence
26   * layer, so model classes can be tested without needing the database.
27   * 
28   */
29  public class TestCaseContext extends Context {
30  	
31  	/**
32  	 * The IDManager which is used to collect Type information. It has to be filled before either from the database or
33  	 * from a file.
34  	 */
35  	private final IDManager manager = IDManager.instance();
36  	
37  	/**
38  	 * The instance Item id will be synthetically created.
39  	 */
40  	private static long instanceItemId = 0;
41  	
42  	/**
43  	 * This cache is used to store objects stored via {@link #set} operations.
44  	 */
45  	private final Map<UserObject, Map<String, Collection<Tuple<UnidirectionalLink, Object>>>> cache;
46  	
47  	/**
48  	 * The model prefix of the types.
49  	 */
50  	private final String modelPrefix;
51  	
52  	/**
53  	 * Creates a TestCaseContext.
54  	 * 
55  	 * @param modelPrefix
56  	 *            The model prefix of the types.
57  	 */
58  	public TestCaseContext(final String modelPrefix) {
59  		this.cache = new HashMap<>();
60  		this.modelPrefix = modelPrefix;
61  	}
62  	
63  	/**
64  	 * Returns the next available unique identifier.
65  	 * 
66  	 * @return The next available unique identifier.
67  	 */
68  	protected static long getInstanceItemId() {
69  		final long result = instanceItemId;
70  		instanceItemId++;
71  		return result;
72  	}
73  	
74  	@Override
75  	public Collection<UserObject> find(final String associationName, final String value) {
76  		throw new FrameworkException("A find method for String objects is not yet implemented");
77  	}
78  	
79  	@Override
80  	public Collection<UserObject> find(final String associationName, final BigInteger value) {
81  		throw new FrameworkException("A find method for Integer objects is not yet implemented");
82  	}
83  	
84  	@Override
85  	public Collection<Tuple<UnidirectionalLink, UserObject>> inverseGet(final String associationName,
86  			final UserObject object) {
87  		throw new FrameworkException("An inverse get method is not yet implemented");
88  	}
89  	
90  	@Override
91  	public Collection<Tuple<UnidirectionalLink, Object>> get(final UserObject object, final String associationName) {
92  		return this.cache.get(object).get(associationName);
93  	}
94  	
95  	@Override
96  	public Collection<Tuple<MapLink, Object>> get(final UserObject object,
97  			final String associationName,
98  			final String key) {
99  		// TODO Auto-generated method stub
100 		return null;
101 	}
102 	
103 	@Override
104 	public Collection<Tuple<MapLink, Object>> get(final UserObject object,
105 			final String associationName,
106 			final BigInteger key) {
107 		// TODO Auto-generated method stub
108 		return null;
109 	}
110 	
111 	@Override
112 	public Collection<Tuple<MapLink, Object>> get(final UserObject object,
113 			final String associationName,
114 			final UserObject key) {
115 		// TODO Auto-generated method stub
116 		return null;
117 	}
118 	
119 	@Override
120 	public UserObject create(final String typeName) {
121 		return UserObject.init(
122 				getInstanceItemId(),
123 				new UserType(this.manager.findIdForType(typeName.substring(this.modelPrefix.length() + 1)), null,
124 						false, false));
125 	}
126 	
127 	@Override
128 	public UnidirectionalLink set(final UserObject object, final String associationName, final String val) {
129 		final UnidirectionalLink result =
130 				new UnidirectionalLink(getInstanceItemId(), object, new StringValue(val), null);
131 		this.put(object, associationName, result);
132 		return result;
133 	}
134 	
135 	/**
136 	 * Stores an object/association/link relation in the cache.
137 	 * 
138 	 * @param object
139 	 *            The owner.
140 	 * @param associationName
141 	 *            The name of the association.
142 	 * @param result
143 	 *            The link.
144 	 */
145 	private void put(final UserObject object, final String associationName, final UnidirectionalLink result) {
146 		if (!this.cache.containsKey(object)) {
147 			this.cache.put(object, new HashMap<String, Collection<Tuple<UnidirectionalLink, Object>>>());
148 		}
149 		this.putInner(this.cache.get(object), associationName, result);
150 	}
151 	
152 	/**
153 	 * Stores an object/association/link relation in a map that is part of the cache.
154 	 * 
155 	 * @param map
156 	 *            The map for some owner object.
157 	 * @param associationName
158 	 *            The name of the association.
159 	 * @param result
160 	 *            The link.
161 	 */
162 	private void putInner(final Map<String, Collection<Tuple<UnidirectionalLink, Object>>> map,
163 			final String associationName,
164 			final UnidirectionalLink result) {
165 		if (!map.containsKey(associationName)) {
166 			map.put(associationName, new Vector<Tuple<UnidirectionalLink, Object>>());
167 		}
168 		map.get(associationName).add(new Tuple<>(result, result.getTarget()));
169 	}
170 	
171 	@Override
172 	public UnidirectionalLink set(final UserObject object, final String associationName, final BigInteger val) {
173 		final UnidirectionalLink result =
174 				new UnidirectionalLink(getInstanceItemId(), object, new IntegerValue(val), null);
175 		this.put(object, associationName, result);
176 		return result;
177 	}
178 	
179 	@Override
180 	public UnidirectionalLink set(final UserObject object, final String associationName, final UserObject target) {
181 		final UnidirectionalLink result = new UnidirectionalLink(getInstanceItemId(), object, target, null);
182 		this.put(object, associationName, result);
183 		return result;
184 	}
185 	
186 	@Override
187 	public MapLink put(final UserObject owner, final String associationName, final UserObject target, final String key) {
188 		// TODO Auto-generated method stub
189 		return null;
190 	}
191 	
192 	@Override
193 	public MapLink put(final UserObject owner, final String associationName, final BigInteger target, final String key) {
194 		// TODO Auto-generated method stub
195 		return null;
196 	}
197 	
198 	@Override
199 	public MapLink put(final UserObject owner, final String associationName, final String target, final String key) {
200 		// TODO Auto-generated method stub
201 		return null;
202 	}
203 	
204 	@Override
205 	public MapLink put(final UserObject owner,
206 			final String associationName,
207 			final UserObject target,
208 			final BigInteger key) {
209 		// TODO Auto-generated method stub
210 		return null;
211 	}
212 	
213 	@Override
214 	public MapLink put(final UserObject owner,
215 			final String associationName,
216 			final BigInteger target,
217 			final BigInteger key) {
218 		// TODO Auto-generated method stub
219 		return null;
220 	}
221 	
222 	@Override
223 	public MapLink put(final UserObject owner, final String associationName, final String target, final BigInteger key) {
224 		// TODO Auto-generated method stub
225 		return null;
226 	}
227 	
228 	@Override
229 	public MapLink put(final UserObject owner,
230 			final String associationName,
231 			final UserObject target,
232 			final UserObject key) {
233 		// TODO Auto-generated method stub
234 		return null;
235 	}
236 	
237 	@Override
238 	public MapLink put(final UserObject owner,
239 			final String associationName,
240 			final BigInteger target,
241 			final UserObject key) {
242 		// TODO Auto-generated method stub
243 		return null;
244 	}
245 	
246 	@Override
247 	public MapLink put(final UserObject owner, final String associationName, final String target, final UserObject key) {
248 		// TODO Auto-generated method stub
249 		return null;
250 	}
251 	
252 	@Override
253 	public void commit() {
254 		// nothing to do
255 	}
256 	
257 	@Override
258 	public void rollback() {
259 		// nothing to do
260 	}
261 	
262 	@Override
263 	public void savePoint() {
264 		// nothing to do
265 	}
266 	
267 	@Override
268 	public void rollbackToSavePoint() {
269 		// nothing to do
270 	}
271 	
272 	@Override
273 	public void unset(final Link toUnset) {
274 		final Iterator<Collection<Tuple<UnidirectionalLink, Object>>> iterator =
275 				this.cache.get(toUnset.getOwner()).values().iterator();
276 		while (iterator.hasNext()) {
277 			final Collection<Tuple<UnidirectionalLink, Object>> current = iterator.next();
278 			final Iterator<Tuple<UnidirectionalLink, Object>> innerIterator = current.iterator();
279 			while (innerIterator.hasNext()) {
280 				final Tuple<UnidirectionalLink, Object> innerCurrent = innerIterator.next();
281 				if (innerCurrent.getFirst().equals(toUnset)) {
282 					innerIterator.remove();
283 				}
284 			}
285 		}
286 	}
287 	
288 	@Override
289 	public TypeManager getTypeManager() {
290 		return TypeManagerImplementation.getInstance();
291 	}
292 	
293 	@Override
294 	public Collection<UserObject> getObjectsByType(final String typeName) {
295 		// TODO Auto-generated method stub
296 		return null;
297 	}
298 	
299 	@Override
300 	public UserObject checkout(final long object) {
301 		// TODO Auto-generated method stub
302 		return null;
303 	}
304 	
305 }