-
Notifications
You must be signed in to change notification settings - Fork 49
/
EmfModelObjectExtensionAssistant.java
607 lines (508 loc) · 24.9 KB
/
EmfModelObjectExtensionAssistant.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
* JBoss, Home of Professional Open Source.
*
* See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
*
* See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
*/
package org.teiid.designer.core.extension;
import static org.teiid.designer.core.ModelerCore.Util;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Properties;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.EMap;
import org.eclipse.emf.ecore.EObject;
import org.teiid.core.designer.properties.PropertyDefinition;
import org.teiid.core.designer.util.CoreArgCheck;
import org.teiid.core.designer.util.CoreStringUtil;
import org.teiid.core.designer.util.I18nUtil;
import org.teiid.designer.core.ModelerCore;
import org.teiid.designer.core.util.ModelObjectClassNameVisitor;
import org.teiid.designer.core.util.ModelResourceContainerFactory;
import org.teiid.designer.core.util.ModelVisitorProcessor;
import org.teiid.designer.core.workspace.ModelResource;
import org.teiid.designer.core.workspace.ModelUtil;
import org.teiid.designer.extension.ExtensionPlugin;
import org.teiid.designer.extension.definition.ModelExtensionAssistant;
import org.teiid.designer.extension.definition.ModelExtensionDefinition;
import org.teiid.designer.extension.definition.ModelObjectExtensionAssistant;
import org.teiid.designer.extension.properties.ModelExtensionPropertyDefinition;
import org.teiid.designer.metamodels.core.Annotation;
/**
* The <code>EmfModelObjectExtensionAssistant</code> is a model extension assistant that knows how to work with {@link EObject}s.
*
* @since 8.0
*/
public class EmfModelObjectExtensionAssistant extends ModelObjectExtensionAssistant {
private static final String PREFIX = I18nUtil.getPropertyPrefix(EmfModelObjectExtensionAssistant.class);
/**
* {@inheritDoc}
*
* @see org.teiid.designer.extension.definition.ModelObjectExtensionAssistant#cleanupModelResourceMEDs(java.lang.Object)
* @throws Exception if error trying to detect and remove incompatible MEDs from model resource
*/
@Override
public void cleanupModelResourceMEDs(Object modelResource) throws Exception {
CoreArgCheck.isNotNull(modelResource, "modelResource is null"); //$NON-NLS-1$
CoreArgCheck.isInstanceOf(ModelResource.class, modelResource);
ModelResource theModelResource = (ModelResource)modelResource;
Collection<String> supportedNamespaces = ModelExtensionUtils.getSupportedNamespaces(theModelResource);
for( String namespacePrefix : supportedNamespaces ) {
ModelExtensionAssistant assistant = ExtensionPlugin.getInstance().getRegistry().getModelExtensionAssistant(namespacePrefix);
ModelExtensionDefinition med = assistant.getModelExtensionDefinition();
if( assistant instanceof ModelObjectExtensionAssistant ) {
if (!med.extendsMetamodelUri(theModelResource.getModelAnnotation().getPrimaryMetamodelUri())) {
// remove MED
((ModelObjectExtensionAssistant)assistant).removeModelExtensionDefinition(modelResource);
} else if (!med.getSupportedModelTypes().contains(theModelResource.getModelType()) ) {
// remove MED
((ModelObjectExtensionAssistant)assistant).removeModelExtensionDefinition(modelResource);
}
}
}
}
/**
* @param modelObject the model object (must be either an {@link EObject}, {@link ModelResource}, or an {@link IFile}.
* @return the model resource or <code>null</code> if not found
*/
protected ModelResource getModelResource( Object modelObject ) throws Exception {
ModelResource modelResource = null;
if (modelObject instanceof ModelResource) {
modelResource = (ModelResource)modelObject;
} else if (modelObject instanceof EObject) {
modelResource = ModelerCore.getModelEditor().findModelResource((EObject)modelObject);
} else if (modelObject instanceof IFile) {
modelResource = ModelerCore.getModelEditor().findModelResource((IFile)modelObject);
}
return modelResource;
}
/**
* {@inheritDoc}
*
* @see org.teiid.designer.extension.definition.ModelObjectExtensionAssistant#getOverriddenValue(java.lang.Object, java.lang.String)
* @throws IllegalArgumentException if the model object is not an {@link EObject}, if property is from a different namespace, or
* if the associated property definition can't be found
*/
@Override
public String getOverriddenValue( Object modelObject,
String propId ) throws Exception {
CoreArgCheck.isInstanceOf(EObject.class, modelObject);
Annotation annotation = ModelExtensionUtils.getModelObjectAnnotation((EObject)modelObject, false);
if (annotation != null) {
EMap<String, String> tags = annotation.getTags();
return getOverriddenValue(modelObject, propId, tags.get(propId));
}
// model object is using default value
return null;
}
/**
* @param modelObject the model object whose property value is being requested (cannot be <code>null</code>)
* @param propId the identifier of the property being checked (cannot be <code>null</code> or empty)
* @param currentValue the model object's current property value (can be <code>null</code> or empty)
* @return the overridden value (never <code>null</code> but can be empty)
* @throws Exception if there is a problem obtaining the property value
*/
protected String getOverriddenValue( Object modelObject,
String propId,
String currentValue ) throws Exception {
// make sure the property definition is found
PropertyDefinition propDefn = getPropertyDefinition(modelObject, propId);
if (propDefn == null) {
return null;
}
String defaultValue = propDefn.getDefaultValue();
// don't add if value equals default value
if ((CoreStringUtil.isEmpty(currentValue) && CoreStringUtil.isEmpty(defaultValue))
|| CoreStringUtil.equals(currentValue, defaultValue)) {
currentValue = null;
}
// value is different than default value
return currentValue;
}
/**
* {@inheritDoc}
*
* @see org.teiid.designer.extension.definition.ModelObjectExtensionAssistant#getOverriddenValues(java.lang.Object)
* @throws IllegalArgumentException if the model object is not an {@link EObject}
*/
@Override
public Properties getOverriddenValues( Object modelObject ) throws Exception {
CoreArgCheck.isInstanceOf(EObject.class, modelObject);
Properties props = new Properties();
if (supportsMyNamespace(modelObject)) {
Annotation annotation = ModelExtensionUtils.getModelObjectAnnotation((EObject)modelObject, false);
if (annotation != null) {
ModelExtensionDefinition med = getModelExtensionDefinition();
EMap<String, String> tags = annotation.getTags();
for (String propId : tags.keySet()) {
// only get properties of my namespace
if (ModelExtensionPropertyDefinition.Utils.isExtensionPropertyId(propId, med)) {
try {
String overridenValue = getOverriddenValue(modelObject, propId, tags.get(propId));
if (!CoreStringUtil.isEmpty(overridenValue)) {
props.put(propId, overridenValue);
}
} catch (Exception e) {
Util.log(e);
}
}
}
}
}
return props;
}
/**
* @param modelObject the model object whose property definition is being requested (cannot be <code>null</code>)
* @param propId the property identifier whose property definition is being requested (cannot be <code>null</code> or empty)
* @return the property definition or <code>null</code> if not found
* @throws Exception if accessing a property definition or value
*/
protected ModelExtensionPropertyDefinition getPropertyDefinition( Object modelObject,
String propId ) throws Exception {
CoreArgCheck.isInstanceOf(EObject.class, modelObject);
// make sure right namespace
if (ModelExtensionPropertyDefinition.Utils.isExtensionPropertyId(propId, getModelExtensionDefinition())) {
final Collection<String> modelTypes = getModelExtensionDefinition().getSupportedModelTypes();
boolean modelTypeSupported = false;
// if supported model types is empty then all model types are supported
if (modelTypes.isEmpty()) {
modelTypeSupported = true;
} else {
final ModelResource modelResource = getModelResource(modelObject);
if (modelResource != null) {
String modelTypeLiteral = modelResource.getModelType().getLiteral();
modelTypeSupported = modelTypes.contains(modelTypeLiteral);
}
}
if (modelTypeSupported) {
return getModelExtensionDefinition().getPropertyDefinition(modelObject.getClass().getName(), propId);
}
}
return null;
}
/**
* {@inheritDoc}
*
* @see org.teiid.designer.extension.definition.ModelObjectExtensionAssistant#getPropertyDefinitions(java.lang.Object)
*/
@Override
public Collection<ModelExtensionPropertyDefinition> getPropertyDefinitions(Object modelObject) throws Exception {
if (!(modelObject instanceof EObject) || !supportsMyNamespace(modelObject)) {
return Collections.emptyList();
}
String metaclassName = modelObject.getClass().getName();
ModelExtensionDefinition med = getModelExtensionDefinition();
Collection<ModelExtensionPropertyDefinition> propDefinitions = new ArrayList<ModelExtensionPropertyDefinition>();
for (ModelExtensionPropertyDefinition potentialPropDefn : med.getPropertyDefinitions(metaclassName)) {
// let assistant determine if property definition is valid for model object
ModelExtensionPropertyDefinition propDefn = getPropertyDefinition(modelObject, potentialPropDefn.getId());
if (propDefn != null) {
propDefinitions.add(propDefn);
}
}
return propDefinitions;
}
/**
* {@inheritDoc}
*
* @see org.teiid.designer.extension.definition.ModelObjectExtensionAssistant#getPropertyValue(java.lang.Object,
* java.lang.String)
*/
@Override
public String getPropertyValue( Object modelObject,
String propId ) throws Exception {
String value = getOverriddenValue(modelObject, propId);
// if no overridden value then return default value if property definition exists
if (CoreStringUtil.isEmpty(value)) {
PropertyDefinition propDefn = getPropertyDefinition(modelObject, propId);
return ((propDefn == null) ? null : propDefn.getDefaultValue());
}
return value;
}
/**
* {@inheritDoc}
*
* @see org.teiid.designer.extension.definition.ModelObjectExtensionAssistant#getPropertyValues(java.lang.Object)
* @throws IllegalArgumentException if the model object is not an {@link EObject}
*/
@Override
public Properties getPropertyValues( Object modelObject ) throws Exception {
CoreArgCheck.isInstanceOf(EObject.class, modelObject);
Properties props = new Properties();
if (supportsMyNamespace(modelObject)) {
// get properties with overridden values
props = getOverriddenValues(modelObject);
// add properties using default value
for (ModelExtensionPropertyDefinition propDefn : getModelExtensionDefinition().getPropertyDefinitions(modelObject.getClass()
.getName())) {
if (!props.containsKey(propDefn.getId())) {
String defaultValue = propDefn.getDefaultValue();
// add only if there is a default value
if (!CoreStringUtil.isEmpty(defaultValue)) {
props.put(propDefn.getId(), defaultValue);
}
}
}
}
return props;
}
/**
* {@inheritDoc}
*
* @see org.teiid.designer.extension.definition.ModelObjectExtensionAssistant#getSupportedNamespaces(java.lang.Object)
*/
@Override
public Collection<String> getSupportedNamespaces( Object modelObject ) throws Exception {
ModelResource modelResource = getModelResource(modelObject);
if (modelResource == null) {
return Collections.emptyList();
}
return ModelExtensionUtils.getSupportedNamespaces(modelResource);
}
/**
* {@inheritDoc}
*
* @see org.teiid.designer.extension.definition.ModelObjectExtensionAssistant#hasExtensionProperties(java.io.File)
*/
@Override
public boolean hasExtensionProperties( File file ) throws Exception {
IWorkspace workspace = ModelerCore.getWorkspace();
IPath location = Path.fromOSString(file.getAbsolutePath());
IFile modelFile = workspace.getRoot().getFileForLocation(location);
if ((modelFile != null) && ModelUtil.isModelFile(modelFile.getFullPath())) {
ModelResource modelResource = getModelResource(modelFile);
if (modelResource != null) {
return ModelExtensionUtils.getSupportedNamespaces(modelResource).contains(getNamespacePrefix());
}
}
// none found
return false;
}
/**
* {@inheritDoc}
*
* @see org.teiid.designer.extension.definition.ModelObjectExtensionAssistant#hasExtensionProperties(java.lang.Object)
*/
@Override
public boolean hasExtensionProperties( Object modelObject ) throws Exception {
return !getPropertyValues(modelObject).isEmpty();
}
/**
* {@inheritDoc}
*
* @see org.teiid.designer.extension.definition.ModelObjectExtensionAssistant#isModelExtensionDefinitionRelated(java.lang.Object)
* @throws IllegalArgumentException if the model object is not an {@link EObject}
*/
@Override
public boolean isModelExtensionDefinitionRelated( Object modelObject ) throws Exception {
CoreArgCheck.isInstanceOf(EObject.class, modelObject);
return ModelExtensionUtils.isModelExtensionDefinitionRelated((EObject)modelObject);
}
/**
* {@inheritDoc}
*
* @see org.teiid.designer.extension.definition.ModelObjectExtensionAssistant#getModelExtensionDefinition(java.lang.Object)
*/
@Override
public ModelExtensionDefinition getModelExtensionDefinition( Object modelObject ) throws Exception {
ModelResource modelResource = getModelResource(modelObject);
if (modelResource == null) {
return null;
}
return ModelExtensionUtils.getModelExtensionDefinition(this, modelResource);
}
/**
* {@inheritDoc}
*
* @see org.teiid.designer.extension.definition.ModelObjectExtensionAssistant#removeModelExtensionDefinition(java.lang.Object)
*/
@Override
public void removeModelExtensionDefinition( Object modelObject ) throws Exception {
ModelResource modelResource = getModelResource(modelObject);
if (modelResource != null) {
ModelExtensionUtils.removeModelExtensionDefinition(modelResource, getNamespacePrefix());
// find model objects with classes that match the extended metaclasses in the MED
ModelExtensionDefinition definition = getModelExtensionDefinition();
String[] metaclasses = definition.getExtendedMetaclasses();
ModelObjectClassNameVisitor visitor = new ModelObjectClassNameVisitor(Arrays.asList(metaclasses));
ModelVisitorProcessor processor = new ModelVisitorProcessor(visitor, ModelVisitorProcessor.MODE_VISIBLE_CONTAINMENTS);
processor.walk(modelResource, ModelVisitorProcessor.DEPTH_INFINITE);
// remove overridden properties
for (EObject eObject : visitor.getResult()) {
Annotation annotation = ModelExtensionUtils.getModelObjectAnnotation(eObject, false);
if (annotation != null) {
String metaclassName = eObject.getClass().getName();
for (ModelExtensionPropertyDefinition propDefn : definition.getPropertyDefinitions(metaclassName)) {
removeProperty(eObject, propDefn.getId());
}
}
}
}
}
/**
* {@inheritDoc}
*
* @see org.teiid.designer.extension.definition.ModelObjectExtensionAssistant#removeProperty(java.lang.Object, java.lang.String)
*/
@Override
public void removeProperty( Object modelObject,
String propId ) throws Exception {
CoreArgCheck.isInstanceOf(EObject.class, modelObject);
// make sure property defined in MED
if (!ModelExtensionPropertyDefinition.Utils.isExtensionPropertyId(propId, getModelExtensionDefinition())) {
throw new Exception(Util.getString(PREFIX + "wrongNamespacePrefix", propId, getNamespacePrefix())); //$NON-NLS-1$
}
ModelExtensionUtils.removeProperty((EObject)modelObject, propId, true);
}
/**
* {@inheritDoc}
*
* @see org.teiid.designer.extension.definition.ModelObjectExtensionAssistant#saveModelExtensionDefinition(java.lang.Object)
*/
@Override
public void saveModelExtensionDefinition( Object modelObject ) throws Exception {
ModelResource modelResource = getModelResource(modelObject);
assert (modelResource != null) : "Model resource should not be null"; //$NON-NLS-1$
boolean update = supportsMyNamespace(modelObject); // see if this is an update
ModelExtensionUtils.updateModelExtensionDefinition(modelResource, getModelExtensionDefinition());
// remove any properties that are no longer supported by the MED
if (update) {
// ModelExtensionRegistry registry = ExtensionPlugin.getInstance().getRegistry();
//
// for (Object eObject : modelResource.getEObjects()) {
// assert eObject instanceof EObject;
// // TODO remove orphaned properties from model object. getOverriddenValues only returns registered properties
// Properties overriddenProperties = getOverriddenValues(eObject);
//
// if (!overriddenProperties.isEmpty()) {
// String metaclassName = eObject.getClass().getName();
//
// for (String propId : overriddenProperties.stringPropertyNames()) {
// // remove property if it doesn't exist any more
// if (registry.getPropertyDefinition(metaclassName, propId) == null) {
// removeProperty(modelObject, propId);
// }
// }
// }
// }
}
}
/**
* {@inheritDoc}
*
* @see org.teiid.designer.extension.definition.ModelObjectExtensionAssistant#setPropertyValue(java.lang.Object,
* java.lang.String, java.lang.String)
*/
@Override
public void setPropertyValue( Object modelObject,
String propId,
String newValue ) throws Exception {
PropertyDefinition propDefn = getPropertyDefinition(modelObject, propId);
if (propDefn == null) {
throw new Exception(Util.getString(PREFIX + "propertyDefinitionNotFound", propId)); //$NON-NLS-1$
}
CoreArgCheck.isInstanceOf(EObject.class, modelObject);
boolean valueIsDefault = CoreStringUtil.equals(propDefn.getDefaultValue(), newValue);
Annotation annotation = ModelExtensionUtils.getModelObjectAnnotation((EObject)modelObject, true);
// default values are not saved in the model object annotation
if (valueIsDefault) {
if (annotation != null) {
annotation.getTags().removeKey(propId);
// delete the model object annotation if no more properties AND there is no description
if (annotation.getTags().isEmpty() && annotation.getDescription() == null) {
ModelResourceContainerFactory.deleteAnnotation(annotation);
}
}
} else {
// value is not equal to default so create annotation if necessary
if (annotation == null) {
annotation = ModelExtensionUtils.getModelObjectAnnotation((EObject)modelObject, true);
}
// only save if new value is different than old value
String oldValue = (String)annotation.getTags().get(propId);
// only set value if different than the default
if (!CoreStringUtil.equals(oldValue, newValue)) {
// remove key if empty value
if (CoreStringUtil.isEmpty(newValue)) {
annotation.getTags().removeKey(propId);
} else {
annotation.getTags().put(propId, newValue);
}
}
}
}
/**
* {@inheritDoc}
*
* @see org.teiid.designer.extension.definition.ModelObjectExtensionAssistant#supportsMedOperation(java.lang.String,
* java.lang.Object)
*/
@Override
public boolean supportsMedOperation( String proposedOperationName,
Object context ) {
CoreArgCheck.isNotEmpty(proposedOperationName, "proposedOperationName is empty"); //$NON-NLS-1$
try {
if (context instanceof IFile) {
ModelResource modelResource = getModelResource(context);
if (modelResource == null) {
return false;
}
Collection<String> modelTypes = getModelExtensionDefinition().getSupportedModelTypes();
// if supported model types is empty then all all model types are supported
if (!modelTypes.isEmpty()) {
String modelTypeLiteral = modelResource.getModelType().getLiteral();
if (!modelTypes.contains(modelTypeLiteral)) {
return false; // model type not supported
}
}
if (MedOperations.ADD_MED_TO_MODEL.equals(proposedOperationName)) {
// model must NOT be currently supporting namespace
if (!supportsMyNamespace(modelResource)) {
// make sure model is of right metamodel URI to be extended by the MED
String metamodelUri = getModelExtensionDefinition().getMetamodelUri();
// MED does not have URI yet
if (CoreStringUtil.isEmpty(metamodelUri)) {
return false;
}
// check to make sure MED metamodel URI is the same as model's
return metamodelUri.equals(modelResource.getPrimaryMetamodelUri());
}
// model already has namespace stored so can't be added again
return false;
}
if (MedOperations.DELETE_MED_FROM_MODEL.equals(proposedOperationName)) {
return supportsMyNamespace(modelResource); // model must be currently supporting namespace
}
}
} catch (Exception e) {
Util.log(e);
return false;
}
return super.supportsMedOperation(proposedOperationName, context);
}
/**
* {@inheritDoc}
*
* @see org.teiid.designer.extension.definition.ModelObjectExtensionAssistant#supportsMyNamespace(java.lang.Object)
*/
@Override
public boolean supportsMyNamespace( Object modelObject ) throws Exception {
ModelResource modelResource = getModelResource(modelObject);
if (modelResource == null) {
return false;
}
return ModelExtensionUtils.isSupportedNamespace(modelResource, getNamespacePrefix());
}
@Override
public boolean supportsProperty(Object modelObject, String propId)
throws Exception {
return getPropertyDefinition(modelObject, propId) != null;
}
}