-
Notifications
You must be signed in to change notification settings - Fork 22
/
SiriusConfigurationProvider.java
259 lines (241 loc) · 10.7 KB
/
SiriusConfigurationProvider.java
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
/*******************************************************************************
* Copyright (c) 2016 Obeo.
* 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:
* Obeo - initial API and implementation
*
*******************************************************************************/
package org.obeonetwork.m2doc.sirius.providers.configuration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.NullProgressMonitor;
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.ResourceSet;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.sirius.business.api.modelingproject.ModelingProject;
import org.eclipse.sirius.business.api.session.Session;
import org.eclipse.sirius.business.api.session.SessionManager;
import org.eclipse.sirius.business.internal.session.SessionTransientAttachment;
import org.eclipse.sirius.ext.base.Option;
import org.eclipse.sirius.tools.api.command.semantic.AddSemanticResourceCommand;
import org.eclipse.ui.PlatformUI;
import org.obeonetwork.m2doc.genconf.GenconfUtils;
import org.obeonetwork.m2doc.genconf.Generation;
import org.obeonetwork.m2doc.genconf.provider.IConfigurationProvider;
import org.obeonetwork.m2doc.properties.TemplateCustomProperties;
import org.obeonetwork.m2doc.sirius.M2DocSiriusUtils;
import org.obeonetwork.m2doc.template.DocumentTemplate;
/**
* Configuration provider for Sirius.
* Automatically add session registered EPackage URIs in configuration model.
*
* @author <a href="mailto:nathalie.lepine@obeo.fr">Nathalie Lepine</a>
*/
@SuppressWarnings("restriction")
public class SiriusConfigurationProvider implements IConfigurationProvider {
/**
* Mapping of {@link Generation} to {@link Session}.
*/
private final Map<Generation, Session> sessions = new HashMap<>();
/**
* {@link Set} of {@link Session} that need to be closed.
*/
private final Set<Session> sessionToClose = new HashSet<>();
/**
* Mapping of {@link Session} to {@link SessionTransientAttachment}.
*/
private final Map<Session, SessionTransientAttachment> transientAttachments = new HashMap<>();
/**
* {@inheritDoc}
*
* @see
* org.obeonetwork.m2doc.provider.configuration.IProviderConfiguration#postCreateConfigurationModel(org.obeonetwork.m2doc.properties.
* TemplateCustomProperties, org.eclipse.core.resources.File, org.obeonetwork.m2doc.genconf.Generation)
*/
@Override
public void postCreateConfigurationModel(TemplateCustomProperties templateProperties, URI templateURI,
Generation generation) {
// add generation resource in session
ModelingProject modelingProject = getModelingProject(templateURI);
if (modelingProject != null) {
Session session = modelingProject.getSession();
if (session != null) {
// add semantic resource to session
addConfigurationModelToSession(generation, session);
}
}
}
/**
* Add generation resource to session.
*
* @param generation
* Generation
* @param session
* Session
*/
protected void addConfigurationModelToSession(Generation generation, Session session) {
URI uri = generation.eResource().getURI();
AddSemanticResourceCommand cmd = new AddSemanticResourceCommand(session, uri, new NullProgressMonitor());
session.getTransactionalEditingDomain().getCommandStack().execute(cmd);
}
/**
* Return template file project if it is a modeling project.
*
* @param templateURI
* the template {@link URI}
* @return modeling project
*/
protected ModelingProject getModelingProject(URI templateURI) {
if (templateURI.isPlatformResource()) {
IResource r = ResourcesPlugin.getWorkspace().getRoot().findMember(templateURI.toPlatformString(true));
if (r != null) {
IProject project = r.getProject();
Option<ModelingProject> optionalModelingProject = ModelingProject.asModelingProject(project);
if (optionalModelingProject.some()) {
return optionalModelingProject.get();
}
}
}
return null;
}
/**
* Get ePackage nsuri from all session resources.
*
* @param session
* Session
* @return uris list
*/
protected List<String> addURIs(Session session) {
List<String> uris = new ArrayList<>();
for (Resource resource : session.getSemanticResources()) {
if (!resource.getContents().isEmpty() && resource.getContents().get(0) instanceof EObject
&& resource.getContents().get(0).eClass() != null
&& resource.getContents().get(0).eClass().getEPackage() != null
&& resource.getContents().get(0).eClass().getEPackage().getNsURI() != null
&& !resource.getContents().get(0).eClass().getEPackage().getNsURI().isEmpty()) {
String nsURI = resource.getContents().get(0).eClass().getEPackage().getNsURI();
if (!uris.contains(nsURI)) {
uris.add(nsURI);
}
}
}
return uris;
}
/**
* {@inheritDoc}
*
* @see
* org.obeonetwork.m2doc.provider.configuration.IProviderConfiguration#preCreateConfigurationModel(org.obeonetwork.m2doc.properties.
* TemplateCustomProperties, org.eclipse.core.resources.File)
*/
@Override
public void preCreateConfigurationModel(TemplateCustomProperties templateProperties, URI templateURI) {
// do nothing.
}
/**
* {@inheritDoc}
*
* @see org.obeonetwork.m2doc.genconf.provider.IConfigurationProvider#postValidateTemplate(org.eclipse.core.resources.File,
* org.obeonetwork.m2doc.template.DocumentTemplate, org.obeonetwork.m2doc.genconf.Generation)
*/
@Override
public boolean postValidateTemplate(URI templateURI, DocumentTemplate documentTemplate, Generation generation) {
// do nothing.
return true;
}
/**
* {@inheritDoc}
*
* @see org.obeonetwork.m2doc.genconf.provider.IConfigurationProvider#preValidateTemplate(org.eclipse.core.resources.File,
* org.obeonetwork.m2doc.template.DocumentTemplate, org.obeonetwork.m2doc.genconf.Generation)
*/
@Override
public void preValidateTemplate(URI templateURI, DocumentTemplate documentTemplate, Generation generation) {
// do nothing.
}
/**
* {@inheritDoc}
*
* @see org.obeonetwork.m2doc.genconf.provider.IConfigurationProvider#preGenerate(org.obeonetwork.m2doc.genconf.Generation,
* org.eclipse.core.resources.IProject, org.eclipse.core.resources.File, org.eclipse.core.resources.File)
*/
@Override
public void preGenerate(Generation generation, URI templateURI, URI generatedURI) {
// do nothing.
}
/**
* {@inheritDoc}
*
* @see org.obeonetwork.m2doc.genconf.provider.IConfigurationProvider#postGenerate(org.obeonetwork.m2doc.genconf.Generation,
* org.eclipse.core.resources.IProject, org.eclipse.core.resources.File, org.eclipse.core.resources.File,
* org.obeonetwork.m2doc.template.DocumentTemplate)
*/
@Override
public List<URI> postGenerate(Generation generation, URI templateURI, URI generatedURI,
DocumentTemplate documentTemplate) {
final Session session = sessions.remove(generation);
if (session != null) {
if (session.isOpen()) {
session.getTransactionalEditingDomain().getResourceSet().eAdapters()
.remove(transientAttachments.remove(session));
}
if (sessionToClose.remove(session)) {
session.close(new NullProgressMonitor());
}
}
return Collections.emptyList();
}
@Override
public ResourceSet createResourceSetForModels(Generation generation) {
ResourceSet created = null;
final Map<String, String> options = GenconfUtils.getOptions(generation);
final String representationsFileName = options.get(M2DocSiriusUtils.SIRIUS_SESSION_OPTION);
if (representationsFileName != null) {
final URI sessionURI = GenconfUtils.getResolvedURI(generation,
URI.createURI(representationsFileName, false));
if (URIConverter.INSTANCE.exists(sessionURI, Collections.emptyMap())) {
try {
final Session session = SessionManager.INSTANCE.getSession(sessionURI, new NullProgressMonitor());
sessions.put(generation, session);
if (!session.isOpen()) {
session.open(new NullProgressMonitor());
sessionToClose.add(session);
}
created = session.getTransactionalEditingDomain().getResourceSet();
SessionTransientAttachment transiantAttachment = new SessionTransientAttachment(session);
created.eAdapters().add(transiantAttachment);
transientAttachments.put(session, transiantAttachment);
// CHECKSTYLE:OFF
} catch (Exception e) {
// CHECKSTYLE:ON
// TODO remove this workaround see https://support.jira.obeo.fr/browse/VP-5389
if (PlatformUI.isWorkbenchRunning()) {
MessageDialog.openWarning(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
"Unable to open Sirius Session",
"Check the " + M2DocSiriusUtils.SIRIUS_SESSION_OPTION
+ " option or try to open the session manually by double clicking the .aird file:\n"
+ e.getMessage());
}
}
} else {
throw new IllegalArgumentException("The Sirius session doesn't exists: " + sessionURI);
}
}
return created;
}
}