Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: df61a7bd16fe06f861d261526bccb88103f18e20 (plain) (blame)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
 * <copyright>
 *
 * Copyright (c) 2009 Springsite BV (The Netherlands) and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Martin Taal - Initial API and implementation
 *
 * </copyright>
 *
 * $Id: PersistentStoreAdapter.java,v 1.15 2011/02/21 04:43:18 mtaal Exp $
 */

package org.eclipse.emf.teneo.type;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.teneo.util.StoreUtil;

/**
 * Keeps a list of PersistentLists by efeature. Is used when a new object is persisted and the
 * OR-layer wants to replace the list implementation.
 * 
 * This adapter keeps the PersistentList and ensures that any updates in the original list are also
 * done in the persistent store.
 * 
 * This adapter only operates in case the target object is not read from the persistent store but is
 * persisted there for the first time.
 * 
 * @author <a href="mailto:mtaal@elver.org">Martin Taal</a>
 * @version $Revision: 1.15 $
 */

public class PersistentStoreAdapter implements Adapter {
	private static final long serialVersionUID = 1L;

	private Notifier target;
	private boolean targetCreatedByORM;

	private Map<EStructuralFeature, Object> storeCollections = new HashMap<EStructuralFeature, Object>();

	private Map<String, Object> syntheticProperties = new HashMap<String, Object>();

	public void addStoreCollection(EStructuralFeature eFeature, Object storeCollection) {
		// note that when refresh is called on a persisted object
		// then this call replaces the current collection
		storeCollections.put(eFeature, storeCollection);
	}

	public Object getStoreCollection(EStructuralFeature eFeature) {
		return storeCollections.get(eFeature);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.common.notify.Adapter#getTarget()
	 */
	public Notifier getTarget() {
		return target;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.common.notify.Adapter#isAdapterForType(java.lang.Object)
	 */
	public boolean isAdapterForType(Object type) {
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.common.notify.Adapter#notifyChanged(org.eclipse.emf.common
	 * .notify.Notification)
	 */
	@SuppressWarnings("unchecked")
	public void notifyChanged(Notification notification) {
		final EStructuralFeature eFeature = (EStructuralFeature) notification.getFeature();

		final Object collectionObject = storeCollections.get(eFeature);
		if (collectionObject == null) {
			return;
		}

		final List<Object> list = (collectionObject instanceof List ? (List<Object>) collectionObject
				: null);
		final Map<Object, Object> map = (collectionObject instanceof Map<?, ?> ? (Map<Object, Object>) collectionObject
				: null);

		final boolean isEReference = eFeature instanceof EReference;
		int changedPosition = -1;
		switch (notification.getEventType()) {
		case Notification.ADD:
			if (list != null) {
				if (notification.getPosition() != Notification.NO_INDEX) {
					changedPosition = notification.getPosition();
					list.add(notification.getPosition(), replaceValue(notification.getNewValue(), eFeature));
				} else {
					changedPosition = list.size();
					list.add(replaceValue(notification.getNewValue(), eFeature));
				}
			}
			if (map != null) {
				final Map.Entry<?, ?> entry = (Map.Entry<?, ?>) notification.getNewValue();
				map.put(entry.getKey(), entry.getValue());
			}
			break;
		case Notification.ADD_MANY:
			if (list != null) {
				if (notification.getPosition() != Notification.NO_INDEX) {
					changedPosition = notification.getPosition();
					list.addAll(notification.getPosition(),
							replaceValues((List<Object>) notification.getNewValue(), eFeature));

				} else {
					changedPosition = list.size();
					list.addAll(replaceValues((List<Object>) notification.getNewValue(), eFeature));
				}
			}
			if (map != null) {
				for (Object o : (List<?>) notification.getNewValue()) {
					final Map.Entry<?, ?> entry = (Map.Entry<?, ?>) o;
					map.put(entry.getKey(), entry.getValue());
				}
			}
			break;
		case Notification.REMOVE:
			if (list != null) {
				final Object removed;
				if (notification.getPosition() != Notification.NO_INDEX) {
					removed = list.remove(notification.getPosition());
				} else {
					removed = replaceValue(notification.getOldValue(), eFeature);
					list.remove(removed);
				}
				// recompute them all
				changedPosition = 0;
				if (isEReference) {
					StoreUtil.resetSyntheticListInfo(eFeature, removed);
				}
			}
			if (map != null) {
				final Map.Entry<?, ?> entry = (Map.Entry<?, ?>) notification.getOldValue();
				map.remove(entry.getKey());
			}
			break;
		case Notification.REMOVE_MANY:
			if (list != null) {
				final List<?> removed = replaceValues((List<Object>) notification.getOldValue(), eFeature);
				list.removeAll(removed);
				if (isEReference) {
					for (Object removedObject : removed) {
						StoreUtil.resetSyntheticListInfo(eFeature, removedObject);
					}
				}
				changedPosition = 0;
			}
			if (map != null) {
				for (Object o : (List<?>) notification.getOldValue()) {
					final Map.Entry<?, ?> entry = (Map.Entry<?, ?>) o;
					map.remove(entry.getKey());
				}
			}
			break;
		case Notification.MOVE:
			if (list != null) {
				final int oldPosition = (Integer) notification.getOldValue();
				final int newPosition = notification.getPosition();
				final Object o = list.remove(oldPosition);
				if (o != notification.getNewValue()) {
					throw new IllegalStateException("Persistent list and EList are out of sync");
				}
				list.add(newPosition, o);
				if (newPosition < oldPosition) {
					changedPosition = newPosition;
				} else {
					changedPosition = oldPosition;
				}
			}
			break;
		case Notification.SET:
			if (list != null) {
				final int position = notification.getPosition();
				Object removed = list.set(position, replaceValue(notification.getNewValue(), eFeature));
				changedPosition = position;
				if (isEReference) {
					StoreUtil.resetSyntheticListInfo(eFeature, removed);
				}
			}
			break;
		}

		if (changedPosition > -1 && isEReference) {
			int newIndex = changedPosition;
			for (Object element : list.subList(changedPosition, list.size())) {
				StoreUtil.setSyntheticListOwner(eFeature, element, notification.getNotifier());
				StoreUtil.setSyntheticListIndex(eFeature, element, newIndex++);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.emf.common.notify.Adapter#setTarget(org.eclipse.emf.common .notify.Notifier)
	 */
	public void setTarget(Notifier newTarget) {
		target = newTarget;
	}

	/**
	 * @return the targetCreatedByORM
	 */
	public boolean isTargetCreatedByORM() {
		return targetCreatedByORM;
	}

	/**
	 * @param targetCreatedByORM
	 *          the targetCreatedByORM to set
	 */
	public void setTargetCreatedByORM(boolean targetCreatedByORM) {
		this.targetCreatedByORM = targetCreatedByORM;
	}

	/**
	 * @return the storeCollections
	 */
	public Map<EStructuralFeature, Object> getStoreCollections() {
		return storeCollections;
	}

	protected Object replaceValue(Object value, EStructuralFeature eFeature) {
		return value;
	}

	protected List<Object> replaceValues(List<Object> values, EStructuralFeature eFeature) {
		return values;
	}

	public Object getSyntheticProperty(String property) {
		return syntheticProperties.get(property);
	}

	public void setSyntheticProperty(String property, Object value) {
		syntheticProperties.put(property, value);
	}
}

Back to the top