Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 41dfc4a3c676fc3d734ce030878ad10c1bc18bc4 (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
/*****************************************************************************
 * Copyright (c) 2013, 2017 CEA LIST 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:
 *   CEA LIST - Initial API and implementation
 *   Christian W. Damus (CEA) - bug 429242
 *
 *****************************************************************************/
package org.eclipse.papyrus.cdo.internal.core.importer;

import java.util.Collection;
import java.util.Collections;
import java.util.Set;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.papyrus.cdo.core.importer.IModelTransferConfiguration;
import org.eclipse.papyrus.cdo.core.importer.IModelTransferNode;
import org.eclipse.papyrus.cdo.core.importer.IModelTransferOperation;
import org.eclipse.papyrus.cdo.internal.core.l10n.Messages;
import org.eclipse.papyrus.infra.core.resource.sasheditor.DiModel;

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

/**
 * This is the ModelTransferNode type. Enjoy.
 */
public class ModelTransferNode implements IModelTransferNode {

	private final ModelTransferConfiguration config;

	private Resource resource;

	private String name;

	private Set<Resource> components = Sets.newLinkedHashSet();

	private Set<IModelTransferNode> dependencies = Sets.newLinkedHashSet();

	private Set<IModelTransferNode> dependents = Sets.newLinkedHashSet();

	public ModelTransferNode(ModelTransferConfiguration config, Resource resource) {
		super();

		this.config = config;
		this.resource = resource;
	}

	void initialize(IModelTransferOperation.Context context) {

		context.run(new IModelTransferOperation() {

			@Override
			public Diagnostic run(IProgressMonitor monitor) {
				SubMonitor sub = SubMonitor.convert(monitor, Messages.ModelTransferNode_0, 2);

				components.add(resource);
				scanForComponents();
				sub.worked(1);

				scanForDependencies();
				sub.worked(1);

				sub.done();

				return Diagnostic.OK_INSTANCE;
			}
		});
	}

	@Override
	public String getName() {
		if (name == null) {
			URI uri = getPrimaryResourceURI();

			String path = uri.path();
			if (uri.isPlatformResource()) {
				// trim the project segment
				path = path.substring(("/" + uri.segment(0)).length()); //$NON-NLS-1$
			} // else a file: URI's path does not include the device, so it's OK

			this.name = path;
		}

		return name;
	}

	@Override
	public IModelTransferConfiguration getConfiguration() {
		return config;
	}

	Resource getPrimaryResource() {
		return resource;
	}

	@Override
	public URI getPrimaryResourceURI() {
		return resource.getURI();
	}

	@Override
	public Collection<URI> getResourceURIs() {
		ImmutableSet.Builder<URI> result = ImmutableSet.builder();

		for (Resource next : components) {
			result.add(next.getURI());
		}

		return result.build();
	}

	@Override
	public Collection<IModelTransferNode> getDependencies() {
		return Collections.unmodifiableSet(dependencies);
	}

	@Override
	public Collection<IModelTransferNode> getDependents() {
		return Collections.unmodifiableSet(dependents);
	}

	void addDependent(IModelTransferNode node) {
		dependents.add(node);
	}

	@Override
	public int hashCode() {
		return getPrimaryResourceURI().hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		return (obj instanceof IModelTransferNode) && getPrimaryResourceURI().equals(((IModelTransferNode) obj).getPrimaryResourceURI());
	}

	@Override
	public String toString() {
		return String.format("ModelTransferNode(%s)", getName()); //$NON-NLS-1$
	}

	void scanForComponents() {
		Resource self = getPrimaryResource();
		for (Resource next : DependencyAdapter.getDependencies(self)) {
			if (DependencyAdapter.getDIResource(next) == self) {
				components.add(next);
			}
		}
	}

	void scanForDependencies() {
		// for each component resource, find the external resources that it
		// references and, for any that seems to have a primary resource, get
		// its node

		URIConverter converter = resource.getResourceSet().getURIConverter();

		for (Resource component : components) {
			for (Resource xref : DependencyAdapter.getDependencies(component)) {
				URI primary = findPrimaryResource(xref.getURI(), converter);
				if ((primary != null) && converter.exists(primary, null)) {
					IModelTransferNode node = config.getNode(primary);
					if ((node != null) && !node.equals(this)) {
						dependencies.add(node);
					}
				}
			}
		}
	}

	private URI findPrimaryResource(URI componentURI, URIConverter converter) {
		URI result = null;

		URI candidate = componentURI.trimFileExtension().appendFileExtension(DiModel.DI_FILE_EXTENSION);
		if (converter.exists(candidate, null)) {
			result = candidate;
		}

		return result;
	}

	@Override
	public boolean isModelParentUnit(IModelTransferNode other) {
		boolean result = false;

		out: for (URI childURI : getResourceURIs()) {
			Resource child = config.getResourceSet().getResource(childURI, false);
			if (child != null) {
				for (EObject root : child.getContents()) {
					EObject container = root.eContainer();
					if (container != null) {
						URI uri = EcoreUtil.getURI(container).trimFragment();
						if (other.getResourceURIs().contains(uri)) {
							// found the parent unit
							result = true;
							break out;
						}
					}
				}
			}
		}

		return result;
	}

	@Override
	public boolean isModelSubUnit(IModelTransferNode other) {
		boolean result = false;

		out: for (URI uri : other.getResourceURIs()) {
			Resource possibleChild = config.getResourceSet().getResource(uri, false);
			if (possibleChild != null) {
				for (EObject root : possibleChild.getContents()) {
					EObject container = root.eContainer();
					if (container != null) {
						URI parentURI = EcoreUtil.getURI(container).trimFragment();
						if (getResourceURIs().contains(parentURI)) {
							// found a child unit
							result = true;
							break out;
						}
					}
				}
			}
		}

		return result;
	}
}

Back to the top