Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 02b97de7bc0317066bcf5d7cc868e49583547ea7 (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
262
263
264
265
266
267
268
269
270
271
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 * 
 * 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:
 *   CEA LIST - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.cdo.internal.core;

import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.getFirst;

import java.util.Set;
import java.util.concurrent.Executor;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.cdo.CDOLock;
import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.id.CDOIDUtil;
import org.eclipse.emf.cdo.common.revision.CDORevision;
import org.eclipse.emf.cdo.dawn.spi.DawnState;
import org.eclipse.emf.cdo.eresource.CDOResourceNode;
import org.eclipse.emf.cdo.util.CDOUtil;
import org.eclipse.emf.cdo.view.CDOView;
import org.eclipse.emf.cdo.view.CDOViewInvalidationEvent;
import org.eclipse.emf.cdo.view.CDOViewSet;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.util.ECrossReferenceAdapter;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.emf.transaction.ResourceSetChangeEvent;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.papyrus.cdo.core.resource.CDOAwareTransactionalEditingDomain;
import org.eclipse.papyrus.cdo.core.util.CDOFunctions;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;

/**
 * This is the CDOUtils type. Enjoy.
 */
public class CDOUtils {

	private static final Set<String> CDO_URI_SCHEMES = ImmutableSet.of("cdo", "dawn"); //$NON-NLS-1$ //$NON-NLS-2$

	private static Executor broadcastExecutor = new DirectExecutor();

	/**
	 * Not instantiable by clients.
	 */
	private CDOUtils() {
		super();
	}

	public static <T> T adapt(Object object, Class<? extends T> type) {
		T result = null;

		if(type.isInstance(object)) {
			result = type.cast(object);
		} else {
			if(object instanceof IAdaptable) {
				result = type.cast(((IAdaptable)object).getAdapter(type));
			}

			if((result == null) && object instanceof Notifier) {
				result = getFirst(filter(((Notifier)object).eAdapters(), type), null);
			}
		}

		return result;
	}

	public static boolean isCDOURI(URI uri) {
		return CDO_URI_SCHEMES.contains(uri.scheme());
	}

	public static boolean isCDOEditingDomain(EditingDomain domain) {
		return domain instanceof CDOAwareTransactionalEditingDomain;
	}

	public static ResourceSet getResourceSet(Notifier notifier) {
		ResourceSet result = null;

		if(notifier instanceof CDOViewSet) {
			result = ((CDOViewSet)notifier).getResourceSet();
		} else if(notifier instanceof ResourceSet) {
			result = (ResourceSet)notifier;
		} else if(notifier instanceof Resource) {
			result = ((Resource)notifier).getResourceSet();
		} else if(notifier instanceof CDOResourceNode) {
			// folders (resource nodes that aren't resources) are not in a
			// resource set, so get the associated view's resource set
			CDOView view = ((CDOResourceNode)notifier).cdoView();
			if(view != null) {
				result = view.getResourceSet();
			}
		} else if(notifier instanceof EObject) {
			result = getResourceSet(((EObject)notifier).eResource());
		}

		return result;
	}

	public static boolean isCDOObject(EObject object) {
		ResourceSet resourceSet = getResourceSet(object);

		return (resourceSet != null) && (CDOUtil.getViewSet(resourceSet) != null);
	}

	public static CDOObject getCDOObject(EObject object) {
		CDOObject result = null;

		if(isCDOObject(object)) {
			result = CDOUtil.getCDOObject(object);
		}

		return result;
	}

	public static CDOID getCDOID(EObject object) {
		CDOObject cdo = getCDOObject(object);
		return (cdo == null) ? CDOIDUtil.createExternal(EcoreUtil.getURI(object).toString()) : cdo.cdoID();
	}

	public static CDOView getView(ResourceSet resourceSet) {
		CDOView result = null;

		CDOViewSet viewSet = CDOUtil.getViewSet(resourceSet);
		if(viewSet != null) {
			CDOView[] views = viewSet.getViews();
			if(views.length > 0) {
				result = views[0];
			}
		}

		return result;
	}

	public static boolean isLockable(CDOObject object) {
		// transient objects do not have lock states, nor do those that are not
		// in a view or are in a closed view
		CDOView view = object.cdoView();
		return (view != null) && !view.isClosed() && (object.cdoLockState() != null);
	}

	public static boolean isLocked(CDOObject object, boolean remotely) {
		CDOLock lock = isLockable(object) ? object.cdoWriteLock() : null;

		return (lock != null) && (remotely ? lock.isLockedByOthers() : lock.isLocked());
	}

	public static void lock(CDOObject object) {
		object.cdoWriteLock().lock();
	}

	public static void unlock(CDOObject object) {
		object.cdoWriteLock().unlock();
	}

	public static DawnState computeState(EObject object) {
		DawnState result = DawnState.CLEAN;

		CDOObject cdo = getCDOObject(object);
		if(cdo != null) {
			if(cdo.cdoConflict()) {
				result = DawnState.CONFLICT;
			} else if(isLocked(cdo, true)) {
				result = DawnState.LOCKED_REMOTELY;
			} else if(isLocked(cdo, false)) {
				result = DawnState.LOCKED_LOCALLY;
			}
		}

		return result;
	}

	public static boolean isReadOnly(CDOObject object) {
		// an object is read-only if somebody else has it locked
		boolean result = CDOUtils.isLocked(object, true);

		// or if the current user doesn't have permission to write it
		if(!result) {
			CDORevision revision = object.cdoRevision();
			result = (revision != null) && !revision.getPermission().isWritable();
		}

		return result;
	}

	public static Iterable<EObject> getEObjects(Iterable<? extends CDOObject> cdoObjects) {
		return Iterables.transform(cdoObjects, CDOFunctions.getEObject());
	}

	public static Iterable<EStructuralFeature.Setting> crossReference(EObject object) {
		Iterable<EStructuralFeature.Setting> result;

		ECrossReferenceAdapter adapter = adapt(object, ECrossReferenceAdapter.class);
		if(adapter != null) {
			result = adapter.getInverseReferences(object);
		} else {
			EObject tree = EcoreUtil.getRootContainer(object);
			Resource resource = tree.eResource();
			ResourceSet rset = (resource == null) ? null : resource.getResourceSet();

			if(rset != null) {
				result = EcoreUtil.UsageCrossReferencer.find(object, rset);
			} else if(resource != null) {
				result = EcoreUtil.UsageCrossReferencer.find(object, resource);
			} else {
				result = EcoreUtil.UsageCrossReferencer.find(object, tree);
			}
		}

		return result;
	}

	/**
	 * <p>
	 * Runs a code block that broadcasts notification of {@link ResourceSetChangeEvent}s, {@link CDOViewInvalidationEvent}s, etc. to listeners using
	 * the most appropriate {@linkplain #setBroadcastExecutor() executor} available. This allows a UI-safe execution to be injected if necessary.
	 * </p>
	 * <p>
	 * <strong>Note</strong> that there is no assurance that this block will be executed either synchronously or asynchronously.
	 * </p>
	 * 
	 * @param broadcastCommand
	 *        the notification command to execute
	 */
	public static void notify(Runnable broadcastCommand) {
		broadcastExecutor.execute(broadcastCommand);
	}

	public static void notify(final TransactionalEditingDomain domain, final Runnable broadcastCommand) {

		broadcastExecutor.execute(new Runnable() {

			public void run() {
				try {
					domain.runExclusive(broadcastCommand);
				} catch (Exception e) {
					Activator.log.error("Exception in execution of notification broadcast.", e); //$NON-NLS-1$
				}
			}
		});
	}

	public static void setBroadcastExecutor(Executor executor) {
		broadcastExecutor = (executor == null) ? new DirectExecutor() : executor;
	}

	//
	// Nested types
	//

	private static final class DirectExecutor implements Executor {

		public void execute(Runnable command) {
			command.run();
		}
	}
}

Back to the top