Skip to main content
summaryrefslogtreecommitdiffstats
blob: d476ed0aea88584f6927211acee5f2694ed5fd01 (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
/*****************************************************************************
 * Copyright (c) 2012, 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *  Christian W. Damus (CEA) - Don't assume that profiles are in XMI resources (CDO)
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.profilefacet.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.gmfdiag.common.model.NotationModel;
import org.eclipse.papyrus.infra.table.instance.papyrustableinstance.PapyrusTableInstance;
import org.eclipse.papyrus.uml.profilefacet.messages.Messages;
import org.eclipse.papyrus.uml.profilefacet.metamodel.profilefacet.ProfileFacetSet;
import org.eclipse.papyrus.uml.profilefacet.metamodel.profilefacet.StereotypeFacet;
import org.eclipse.uml2.uml.Profile;



/**
 * 
 * This class provides utilities for the AdditionalContentsFacetSet.
 * This facetset is used to provide additional contents for table EFacet (Currently,
 * this main job is to provide FacetSet to edit easily the properties of the stereotypes into a table
 * 
 */
public class AdditionalContentsUtils {

	private AdditionalContentsUtils() {
		//to prevent instanciation
	}

	public static final String ADDITIONAL_CONTENTS_EPCKAGE_NAME = "AdditionalContentsForTable"; //$NON-NLS-1$

	public static final String ADDITIONAL_CONTENTS_EPACKAGE_NS_URI = "http://www.eclipse.org/papyrustable/additionalcontentsepackage"; //$NON-NLS-1$

	public static final String ADDITIONAL_CONTENTS_EPACKAGE_PREFIX = "additionalcontentsepackage"; //$NON-NLS-1$

	public static final String ADDITIONAL_CONTENTS_FACET_SET_DOCUMENTATION = Messages.AdditionalContentsUtils_ProfileEFacetDescription;

	public static final String ADDITIONAL_CONTENTS_QUERY_SET_NAME = "AdditionalContentsModelQuerySetForTable"; //$NON-NLS-1$

	public static EPackage createAdditionalContentsEPackage() {
		final EPackage additionFeatureRootFacetSet = EcoreFactory.eINSTANCE.createEPackage();
		additionFeatureRootFacetSet.setName(AdditionalContentsUtils.ADDITIONAL_CONTENTS_EPCKAGE_NAME);
		additionFeatureRootFacetSet.setNsPrefix(AdditionalContentsUtils.ADDITIONAL_CONTENTS_EPACKAGE_PREFIX);
		additionFeatureRootFacetSet.setNsURI(AdditionalContentsUtils.ADDITIONAL_CONTENTS_EPACKAGE_NS_URI);
		return additionFeatureRootFacetSet;
	}

	public static EPackage getAdditionalContentsEPackage(final PapyrusTableInstance table) {
		EPackage ePackage = null;
		final ModelSet modelSet = (ModelSet)table.eResource().getResourceSet();
		final Resource resource = modelSet.getAssociatedResource(table.eResource(), NotationModel.NOTATION_FILE_EXTENSION, true);
		final Iterator<EObject> iter = resource.getContents().iterator();
		while(iter.hasNext() && ePackage == null) {
			final EObject current = iter.next();
			if(current instanceof EPackage) {
				final EPackage tmp = (EPackage)current;
				if(AdditionalContentsUtils.ADDITIONAL_CONTENTS_EPACKAGE_NS_URI.equals(tmp.getNsURI())) {
					ePackage = tmp;
				}
			}
		}

		return ePackage;
	}

	/**
	 * 
	 * @param source
	 *        a resource
	 * @return
	 *         return the EPackage with the uri {@link #ADDITIONAL_CONTENTS_FACET_SET_NS_URI} in the resource or <code>null</code> if not found
	 */
	public static EPackage getAdditionalContentsEPackage(final Resource source) {
		if(source != null) {
			EPackage additionalSet = null;
			Iterator<EObject> iter = source.getContents().iterator();
			while(iter.hasNext() && additionalSet == null) {
				final EObject contents = iter.next();
				if(contents instanceof EPackage && AdditionalContentsUtils.ADDITIONAL_CONTENTS_EPACKAGE_NS_URI.equals(((EPackage)contents).getNsURI())) {
					additionalSet = (EPackage)contents;
				}
			}
			return additionalSet;
		}
		return null;
	}

	/**
	 * 
	 * @param resourceSet
	 *        a resource set
	 * @return all ProfileFacetSet owned by the resource setassuming that :
	 *         <ul>
	 *         <li>the ProfileFacetSet are stored as root of the resource</li>
	 *         <li>the ProfileFacetSet are stored in the notation file</li>
	 *         </ul>
	 */
	public static final Collection<ProfileFacetSet> getAllProfileFacetSets(final ResourceSet resourceSet) {
		final Collection<ProfileFacetSet> facetSets = new HashSet<ProfileFacetSet>();
		final TransactionalEditingDomain domain = (TransactionalEditingDomain)EMFHelper.resolveEditingDomain(resourceSet);

		for(final Resource current : resourceSet.getResources()) {
			//we assume that all ProfileFacetSet are stored into notation files
			if(NotationModel.NOTATION_FILE_EXTENSION.equals(current.getURI().fileExtension()) && !domain.isReadOnly(current)) {
				final EPackage additionalContentsEPackage = getAdditionalContentsEPackage(current);
				if(additionalContentsEPackage != null) {
					for(final EPackage currentEPackage : additionalContentsEPackage.getESubpackages()) {
						if(currentEPackage instanceof ProfileFacetSet) {
							facetSets.add((ProfileFacetSet)currentEPackage);
						}
					}
				}
			}
		}
		return facetSets;
	}

	/**
	 * 
	 * @param profile
	 *        a profile
	 * @return
	 *         the list of the existing {@link ProfileFacetSet} for this profile.
	 *         This is a collection, because in case of a model splitted in several file, we could have several representation for the same profile
	 */
	public static final Collection<ProfileFacetSet> getAllFacetSet(final Profile profile) {
		final String profileID = profile.eResource().getURIFragment(profile);

		final Collection<ProfileFacetSet> facetSets = new HashSet<ProfileFacetSet>();
		final ResourceSet resourceSet = profile.eResource().getResourceSet();

		final Collection<ProfileFacetSet> allExistingFacetSets = getAllProfileFacetSets(resourceSet);
		for(final ProfileFacetSet current : allExistingFacetSets) {
			if(profileID.equals(((ProfileFacetSet)current).getRepresentedElement_XMI_ID())) {
				facetSets.add(current);
			}
		}
		return facetSets;
	}

	/**
	 * 
	 * @param profileFacetSet
	 *        a profile facet set
	 * @return
	 *         the list all {@link StereotypeFacet} owned by this {@link ProfileFacetSet} and its sub - {@link ProfileFacetSet}
	 */
	public static final Collection<StereotypeFacet> getAllStereotypeFacets(final ProfileFacetSet profileFacetSet) {
		final Collection<StereotypeFacet> facets = new HashSet<StereotypeFacet>();
		for(final ProfileFacetSet current : profileFacetSet.getSubProfileFacetSet()) {
			facets.addAll(getAllStereotypeFacets(current));
		}
		facets.addAll(profileFacetSet.getStereotypeFacets());
		return facets;
	}

	/**
	 * 
	 * @param resourceSet
	 *        a resourceset
	 * @return
	 *         <code>true</code> if there is not several ProfileFacetSet representing Profile with the same ID in the same resource.
	 *         That is to say that we can have seral ProfileFacetSet representing Profile with the same id owned by differents resources!
	 */
	public static final boolean areAllFacetSetUniqueByResource(final ResourceSet resourceSet) {
		final Map<String, Collection<Resource>> XMI_ID_Resource = new HashMap<String, Collection<Resource>>();
		final Collection<ProfileFacetSet> allExistingProfileFacetSet = getAllProfileFacetSets(resourceSet);
		for(ProfileFacetSet profileFacetSet : allExistingProfileFacetSet) {
			final String id = profileFacetSet.getRepresentedElement_XMI_ID();
			if(XMI_ID_Resource.containsKey(id)) {
				if(XMI_ID_Resource.get(id).contains(profileFacetSet.eResource())) {
					return false;
				}
			} else {
				Collection<Resource> res = new ArrayList<Resource>();
				XMI_ID_Resource.put(id, res);
			}
			XMI_ID_Resource.get(id).add(profileFacetSet.eResource());
		}
		return true;
	}

	/**
	 * 
	 * @param profile
	 *        a profile
	 * @param additionalResourceEPackage
	 *        an epackage
	 * @return
	 *         the {@link ProfileFacetSet} for this profile if it is owned by the epackage or <code>null</code> if not found
	 */
	public static final ProfileFacetSet findProfileFacetSet(final Profile profile, final EPackage additionalResourceEPackage) {
		final XMIResource resource = (XMIResource)profile.eResource();
		final String profileID = resource.getID(profile);
		for(final EPackage current : additionalResourceEPackage.getESubpackages()) {
			if(current instanceof ProfileFacetSet) {
				if(profileID.equals(((ProfileFacetSet)current).getRepresentedElement_XMI_ID())) {
					return (ProfileFacetSet)current;
				}
			}
		}
		return null;
	}
}

Back to the top