Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8af6a52911d5293fd0d2c9c4381fb4894212cdd2 (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
/*******************************************************************************
 * Copyright (c) 2013 Atos
 * 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:
 *     Arthur Daussy - initial implementation
 *******************************************************************************/
package org.eclipse.papyrus.team.collaborative.strategy.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.edit.domain.EditingDomain;
//import org.eclipse.papyrus.infra.emf.readonly.IReadOnlyHandler;
import org.eclipse.papyrus.infra.core.resource.IReadOnlyHandler;
import org.eclipse.papyrus.infra.emf.readonly.AbstractReadOnlyHandler;
import org.eclipse.papyrus.team.collaborative.core.ICollaborativeManager;
import org.eclipse.papyrus.team.collaborative.core.IExtendedURI;
import org.eclipse.papyrus.team.collaborative.core.participants.locker.ILocker;
import org.eclipse.papyrus.team.collaborative.core.utils.CollabFunctionsFactory;
import org.eclipse.papyrus.team.collaborative.strategy.ui.actions.LockAction;

import com.google.common.base.Optional;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;


/**
 * The Class CollabReadOnlyHandler.
 * This read only handler implement {@link IReadOnlyHandler} using the collaborative framework
 */
public class CollabReadOnlyHandler extends AbstractReadOnlyHandler {

	public CollabReadOnlyHandler(EditingDomain editingDomain) {
		super(editingDomain);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.papyrus.readonly.IReadOnlyHandler#isReadOnly(org.eclipse.emf.common.util.URI[], org.eclipse.emf.edit.domain.EditingDomain)
	 */
	public Optional<Boolean> anyReadOnly(URI[] uris) {
		if(uris != null && uris.length > 0 && getEditingDomain() != null) {
			ArrayList<URI> urisList = Lists.newArrayList(uris);
			Collection<IExtendedURI> extendedURICollection = Collections2.transform(urisList, CollabFunctionsFactory.getURIToExtendedURIWithContainment());
			HashSet<IExtendedURI> extendURISet = Sets.newHashSet(extendedURICollection);
			ResourceSet resourceSet = getEditingDomain().getResourceSet();
			if(ICollaborativeManager.INSTANCE.isCollab(extendURISet, resourceSet)) {
				ILocker locker;
				locker = ICollaborativeManager.INSTANCE.getLocker(extendURISet, resourceSet);
				if(locker == null) {
					return Optional.absent();
				}
				for(IExtendedURI extendURI : locker.getExtendedSet()) {
					if(!locker.isLocked(extendURI).isOK()) {
						return Optional.of(true);
					}
				}
			}
		}
		return Optional.absent();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.papyrus.readonly.IReadOnlyHandler#enableWrite(org.eclipse.emf.common.util.URI[], org.eclipse.emf.edit.domain.EditingDomain)
	 */
	public Optional<Boolean> makeWritable(URI[] uris) {
		ArrayList<URI> urisList = Lists.newArrayList(uris);
		Collection<IExtendedURI> extendedURICollection = Collections2.transform(urisList, CollabFunctionsFactory.getURIToExtendedURIWithContainment());
		HashSet<IExtendedURI> extendedURISet = Sets.newHashSet(extendedURICollection);
		ResourceSet resourceSet = getEditingDomain().getResourceSet();
		if(ICollaborativeManager.INSTANCE.isCollab(extendedURISet, resourceSet)) {
			IStatus status = LockAction.doSafeLock(resourceSet, extendedURISet, true);
			if(!status.isOK()) {
				return Optional.absent();
			}
			return Optional.of(true);
		}
		return Optional.absent();
	}

}

Back to the top