/
EmfModelGenerator.java
1397 lines (1206 loc) · 57.3 KB
/
EmfModelGenerator.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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.ddl.importer.node;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.osgi.util.NLS;
import org.teiid.core.designer.ModelerCoreException;
import org.teiid.core.designer.util.CoreArgCheck;
import org.teiid.core.designer.util.CoreStringUtil;
import org.teiid.designer.core.ModelEditor;
import org.teiid.designer.core.ModelerCore;
import org.teiid.designer.core.extension.AbstractMetaclassNameProvider;
import org.teiid.designer.core.workspace.ModelResource;
import org.teiid.designer.core.workspace.ModelWorkspaceException;
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.extension.registry.ModelExtensionRegistry;
import org.teiid.designer.metamodels.core.Annotation;
import org.teiid.designer.metamodels.core.AnnotationContainer;
import org.teiid.designer.metamodels.core.CoreFactory;
import org.teiid.designer.metamodels.relational.AccessPattern;
import org.teiid.designer.metamodels.relational.BaseTable;
import org.teiid.designer.metamodels.relational.Catalog;
import org.teiid.designer.metamodels.relational.Column;
import org.teiid.designer.metamodels.relational.DirectionKind;
import org.teiid.designer.metamodels.relational.ForeignKey;
import org.teiid.designer.metamodels.relational.Index;
import org.teiid.designer.metamodels.relational.MultiplicityKind;
import org.teiid.designer.metamodels.relational.NullableType;
import org.teiid.designer.metamodels.relational.PrimaryKey;
import org.teiid.designer.metamodels.relational.Procedure;
import org.teiid.designer.metamodels.relational.ProcedureParameter;
import org.teiid.designer.metamodels.relational.ProcedureResult;
import org.teiid.designer.metamodels.relational.ProcedureUpdateCount;
import org.teiid.designer.metamodels.relational.RelationalFactory;
import org.teiid.designer.metamodels.relational.Schema;
import org.teiid.designer.metamodels.relational.SearchabilityType;
import org.teiid.designer.metamodels.relational.Table;
import org.teiid.designer.metamodels.relational.UniqueConstraint;
import org.teiid.designer.metamodels.relational.UniqueKey;
import org.teiid.designer.metamodels.relational.View;
import org.teiid.designer.metamodels.relational.extension.RelationalModelExtensionAssistant;
import org.teiid.designer.metamodels.relational.extension.RelationalModelExtensionConstants;
import org.teiid.designer.relational.Messages;
import org.teiid.designer.relational.RelationalConstants;
import org.teiid.designer.relational.RelationalConstants.DIRECTION;
import org.teiid.designer.relational.RelationalConstants.NULLABLE;
import org.teiid.designer.relational.RelationalConstants.SEARCHABILITY;
import org.teiid.designer.relational.RelationalConstants.TYPES;
import org.teiid.designer.relational.RelationalPlugin;
import org.teiid.designer.relational.compare.DifferenceReport;
import org.teiid.designer.relational.model.DatatypeProcessor;
import org.teiid.designer.relational.model.RelationalAccessPattern;
import org.teiid.designer.relational.model.RelationalColumn;
import org.teiid.designer.relational.model.RelationalForeignKey;
import org.teiid.designer.relational.model.RelationalIndex;
import org.teiid.designer.relational.model.RelationalParameter;
import org.teiid.designer.relational.model.RelationalPrimaryKey;
import org.teiid.designer.relational.model.RelationalProcedure;
import org.teiid.designer.relational.model.RelationalProcedureResultSet;
import org.teiid.designer.relational.model.RelationalReference;
import org.teiid.designer.relational.model.RelationalTable;
import org.teiid.designer.relational.model.RelationalUniqueConstraint;
import org.teiid.designer.relational.model.RelationalView;
import org.teiid.designer.relational.model.RelationalViewProcedure;
import org.teiid.designer.relational.model.RelationalViewTable;
import org.teiid.designer.transformation.model.RelationalViewModelFactory;
/**
* EmfModelGenerator - creates EMF model objects from RelationalReference counterparts.
*/
public class EmfModelGenerator {
/**
* RelationalFactory creates the EMF objects
*/
public static final RelationalFactory FACTORY = RelationalFactory.eINSTANCE;
@SuppressWarnings("javadoc")
public static EmfModelGenerator INSTANCE = new EmfModelGenerator();
public static RelationalViewModelFactory VIEW_MODEL_FACTORY = new RelationalViewModelFactory();
private DatatypeProcessor datatypeProcessor = new DatatypeProcessor();
private ModelEditor modelEditor = ModelerCore.getModelEditor();
private List<DeferredPair> pkList = new ArrayList<DeferredPair>();
private List<DeferredPair> fkList = new ArrayList<DeferredPair>();
private List<DeferredPair> apList = new ArrayList<DeferredPair>();
private List<DeferredPair> ucList = new ArrayList<DeferredPair>();
private List<RelationalIndex> indexList = new ArrayList<RelationalIndex>();
private List<DeferredPair> deferredProcessingList = new ArrayList<DeferredPair>();
private Map<String, Collection<ModelObjectExtensionAssistant>> classNameToMedAssistantsMap = new HashMap<String,Collection<ModelObjectExtensionAssistant>>();
private Set<String> propsWithNoAssistant = new HashSet<String>();
private Set<String> metaclassesWithNoAssistant = new HashSet<String>();
/**
* EMF Model Generator execute
* @param diffReport the difference report
* @param targetModelResource the model resource
* @param progressMonitor progress monitor
* @param totalWork the total work units to be done
* @return the execution status
* @throws ModelerCoreException if problems building model
*/
public IStatus execute(DifferenceReport diffReport, ModelResource targetModelResource, IProgressMonitor progressMonitor, int totalWork) throws ModelerCoreException {
// Clear lists prior to execution
clearLists();
int workUnit = totalWork / 5;
// Add Relational Extension Assistant if necessary
addRelationalExtensionAssistant(targetModelResource);
progressMonitor.setTaskName(Messages.emfModelGenerator_generatingModel);
// 1) Delete Objects from the target Model
progressMonitor.subTask(Messages.emfModelGenerator_deletingPrimaryObjects);
List<RelationalReference> objsToDelete = diffReport.getObjectsToDelete().getList();
for( RelationalReference child : objsToDelete ) {
// Delete object from targetResource
if(child.isChecked()) {
deleteMatchingChild(targetModelResource, child);
}
}
progressMonitor.worked(workUnit);
// 2) Create Primary Objects in the target Model (Keep Deferred Map)
progressMonitor.subTask(Messages.emfModelGenerator_creatingPrimaryObjects);
List<RelationalReference> objsToCreate = diffReport.getObjectsToCreate().getList();
for( RelationalReference child : objsToCreate ) {
// Create new objects in targetResource
if(child.isChecked()) {
createObject(child, targetModelResource);
}
}
progressMonitor.worked(workUnit);
// 3) Update Objects in the target Model (Keep Deferred Map)
progressMonitor.subTask(Messages.emfModelGenerator_updatingPrimaryObjects);
List<RelationalReference> objsToUpdate = diffReport.getObjectsToUpdate().getList();
for( RelationalReference child : objsToUpdate ) {
if(child.isChecked()) {
deleteMatchingChild(targetModelResource, child);
createObject(child, targetModelResource);
}
}
progressMonitor.worked(workUnit);
// 4) Create the 'Secondary' objects - pks, fks, etc
progressMonitor.subTask(Messages.emfModelGenerator_creatingSecondaryObjects);
if(!pkList.isEmpty()) {
for( DeferredPair item : pkList ) {
RelationalPrimaryKey pk = (RelationalPrimaryKey)item.getRelationalReference();
BaseTable table = (BaseTable)item.getEObject();
createPrimaryKey(pk, table, targetModelResource);
}
}
if(!fkList.isEmpty()) {
for( DeferredPair item : fkList ) {
RelationalForeignKey fk = (RelationalForeignKey)item.getRelationalReference();
BaseTable table = (BaseTable)item.getEObject();
createForeignKey(fk, table, targetModelResource);
}
}
if(!apList.isEmpty()) {
for( DeferredPair item : apList ) {
RelationalAccessPattern ap = (RelationalAccessPattern)item.getRelationalReference();
Table table = (Table)item.getEObject();
createAccessPattern(ap, table, targetModelResource);
}
}
if(!ucList.isEmpty()) {
for( DeferredPair item : ucList ) {
RelationalUniqueConstraint uc = (RelationalUniqueConstraint)item.getRelationalReference();
BaseTable table = (BaseTable)item.getEObject();
createUniqueConstraint(uc, table, targetModelResource);
}
}
if(!indexList.isEmpty()) {
for( RelationalIndex index : indexList ) {
createIndex(index, targetModelResource);
}
}
progressMonitor.worked(workUnit);
// 5) Process all 'deferred' operations - Extension properties and descriptions...
progressMonitor.subTask(Messages.emfModelGenerator_settingExtensionProps);
if(!deferredProcessingList.isEmpty()) {
for( DeferredPair item : deferredProcessingList) {
RelationalReference relRef = item.getRelationalReference();
EObject eObj = item.getEObject();
processExtensionProperties(targetModelResource,relRef,eObj);
setDescription(eObj,relRef.getDescription(),targetModelResource);
}
}
progressMonitor.worked(workUnit);
// Build a return status based on errors found
MultiStatus multiStatus = new MultiStatus(RelationalPlugin.PLUGIN_ID, IStatus.OK, Messages.emfModelGenerator_modelGenerationSuccess, null);
if(!this.propsWithNoAssistant.isEmpty()) {
StringBuffer sb = new StringBuffer();
Iterator iter = this.propsWithNoAssistant.iterator();
while(iter.hasNext()) {
String prop = (String)iter.next();
sb.append(prop);
if(iter.hasNext()) sb.append(","); //$NON-NLS-1$
}
multiStatus.add(new Status(IStatus.WARNING, RelationalPlugin.PLUGIN_ID, 0, NLS.bind(Messages.emfModelGenerator_warningAssistantForPropertyNotFound, sb.toString()), null));
} else if(!this.metaclassesWithNoAssistant.isEmpty()) {
StringBuffer sb = new StringBuffer();
Iterator iter = this.metaclassesWithNoAssistant.iterator();
while(iter.hasNext()) {
String prop = (String)iter.next();
sb.append(prop);
if(iter.hasNext()) sb.append(","); //$NON-NLS-1$
}
multiStatus.add(new Status(IStatus.WARNING, RelationalPlugin.PLUGIN_ID, 0, NLS.bind(Messages.emfModelGenerator_warningAssistantForMetaclassNotFound, sb.toString()), null));
}
// Clear lists on completion
clearLists();
return multiStatus;
}
/**
* Clear the lists used during execution
*/
private void clearLists() {
pkList.clear();
fkList.clear();
apList.clear();
ucList.clear();
indexList.clear();
deferredProcessingList.clear();
propsWithNoAssistant.clear();
metaclassesWithNoAssistant.clear();
}
/**
* Create an EMF object, using the provided RelationalReference object
* @param relationalRef the relational model object
* @param modelResource the model resource
* @return the new EMF object
* @throws ModelWorkspaceException if problems building model
*/
public EObject createObject( RelationalReference relationalRef, ModelResource modelResource) throws ModelWorkspaceException {
EObject newEObject = null;
switch (relationalRef.getType()) {
case TYPES.MODEL: {
// NOOP. Shouldn't get here
} break;
case TYPES.SCHEMA: {
// NOOP. Shouldn't get here
} break;
case TYPES.CATALOG: {
// NOOP. Shouldn't get here
} break;
case TYPES.TABLE: {
if( relationalRef instanceof RelationalViewTable ) {
newEObject = VIEW_MODEL_FACTORY.buildObject(relationalRef, modelResource, new NullProgressMonitor());
} else {
newEObject = createBaseTable(relationalRef, modelResource);
modelResource.getEmfResource().getContents().add(newEObject);
}
} break;
case TYPES.VIEW: {
newEObject = createView(relationalRef, modelResource);
modelResource.getEmfResource().getContents().add(newEObject);
} break;
case TYPES.PROCEDURE: {
if( relationalRef instanceof RelationalViewProcedure) {
newEObject = VIEW_MODEL_FACTORY.buildObject(relationalRef, modelResource, new NullProgressMonitor());
} else {
newEObject = createProcedure(relationalRef, modelResource);
modelResource.getEmfResource().getContents().add(newEObject);
}
} break;
case TYPES.INDEX: {
newEObject = createIndex(relationalRef, modelResource);
modelResource.getEmfResource().getContents().add(newEObject);
} break;
case TYPES.UNDEFINED:
default: {
RelationalPlugin.Util.log(IStatus.WARNING,
NLS.bind(Messages.emfModelGenerator_unknown_object_type_0_cannot_be_processed, relationalRef.getName()));
} break;
}
return newEObject;
}
/**
* Delete the emf object the targetResource that matches the supplied RelationalReference
* @param targetResource the target model
* @param ref the relational reference
* @throws ModelerCoreException
*/
private void deleteMatchingChild(ModelResource targetResource, RelationalReference ref) throws ModelerCoreException {
int refType = ref.getType();
Collection<EObject> existingChildren = targetResource.getEmfResource().getContents();
EObject childToDelete = null;
for( EObject child : existingChildren ) {
String eObjName = this.modelEditor.getName(child);
if(refType==RelationalConstants.TYPES.TABLE && child instanceof BaseTable) {
// BaseTable tableEObj = ((BaseTable)child);
// EObject parent = tableEObj.eContainer();
if(CoreStringUtil.equals(eObjName, ref.getName())) {
childToDelete = child;
break;
}
} else if(refType==RelationalConstants.TYPES.VIEW && child instanceof View) {
if(CoreStringUtil.equals(eObjName, ref.getName())) {
childToDelete = child;
break;
}
} else if(refType==RelationalConstants.TYPES.PROCEDURE && child instanceof Procedure) {
if(CoreStringUtil.equals(eObjName, ref.getName())) {
childToDelete = child;
break;
}
} else if(refType==RelationalConstants.TYPES.INDEX && child instanceof Index) {
if(CoreStringUtil.equals(eObjName, ref.getName())) {
childToDelete = child;
break;
}
} else if(refType==RelationalConstants.TYPES.SCHEMA && child instanceof Schema) {
if(CoreStringUtil.equals(eObjName, ref.getName())) {
childToDelete = child;
break;
}
} else if(refType==RelationalConstants.TYPES.CATALOG && child instanceof Catalog) {
if(CoreStringUtil.equals(eObjName, ref.getName())) {
childToDelete = child;
break;
}
}
}
if( childToDelete != null ) {
this.modelEditor.delete(childToDelete);
}
}
/**
* Create EMF BaseTable from the supplied RelationalReference
* @param ref the relational model object
* @param modelResource the model resource
* @return the new object
*/
public EObject createBaseTable( final RelationalReference ref, ModelResource modelResource) {
CoreArgCheck.isInstanceOf(RelationalTable.class, ref);
RelationalTable tableRef = (RelationalTable)ref;
// Create and Set Name
BaseTable baseTable = FACTORY.createBaseTable();
baseTable.setName(tableRef.getName());
baseTable.setSupportsUpdate(tableRef.getSupportsUpdate());
baseTable.setMaterialized(tableRef.isMaterialized());
baseTable.setNameInSource(tableRef.getNameInSource());
baseTable.setSystem(tableRef.isSystem());
baseTable.setCardinality(tableRef.getCardinality());
// Add Columns
for( RelationalColumn column : tableRef.getColumns()) {
createColumn(column, baseTable, modelResource);
}
// Add Primary Keys
RelationalPrimaryKey pk = tableRef.getPrimaryKey();
if( pk != null ) {
pkList.add(new DeferredPair(pk,baseTable));
}
Collection<RelationalUniqueConstraint> ucs = tableRef.getUniqueConstraints();
for(RelationalUniqueConstraint uc: ucs) {
ucList.add(new DeferredPair(uc,baseTable));
}
for( RelationalAccessPattern ap : tableRef.getAccessPatterns()) {
apList.add(new DeferredPair(ap,baseTable));
}
for( RelationalForeignKey fk : tableRef.getForeignKeys()) {
fkList.add(new DeferredPair(fk,baseTable));
}
for( RelationalIndex index : tableRef.getIndexes() ) {
indexList.add(index);
}
// Save objects to Map, so that Extension Properties can be processed later
if(!tableRef.getExtensionProperties().isEmpty() || !CoreStringUtil.isEmpty(tableRef.getDescription())) {
deferredProcessingList.add(new DeferredPair(tableRef, baseTable));
}
return baseTable;
}
/**
* Create EMF View from the supplied RelationalReference
* @param ref the relational model object
* @param modelResource the model resource
* @return the new object
*/
public EObject createView( final RelationalReference ref, ModelResource modelResource) {
CoreArgCheck.isInstanceOf(RelationalView.class, ref);
RelationalView viewRef = (RelationalView)ref;
// Create and Set Name
View view = FACTORY.createView();
view.setName(viewRef.getName());
view.setSupportsUpdate(viewRef.getSupportsUpdate());
view.setMaterialized(viewRef.isMaterialized());
view.setNameInSource(viewRef.getNameInSource());
view.setSystem(viewRef.isSystem());
// Add Columns
for( RelationalColumn column : viewRef.getColumns()) {
createColumn(column, view, modelResource);
}
for( RelationalAccessPattern ap : viewRef.getAccessPatterns()) {
apList.add(new DeferredPair(ap, view));
}
// Add to deferred list, if necessary
updateDeferredList(viewRef,view);
return view;
}
/**
* Create EMF Column from the supplied RelationalReference
* @param ref the relational model object
* @param baseTable the table
* @param modelResource the model resource
* @return the new object
*/
public EObject createColumn( RelationalReference ref, Table baseTable, ModelResource modelResource) {
CoreArgCheck.isInstanceOf(RelationalColumn.class, ref);
RelationalColumn columnRef = (RelationalColumn)ref;
Column column = FACTORY.createColumn();
column.setOwner(baseTable);
column.setName(columnRef.getName());
column.setNameInSource(columnRef.getNameInSource());
column.setAutoIncremented(columnRef.isAutoIncremented());
column.setCaseSensitive(columnRef.isCaseSensitive());
column.setCharacterSetName(columnRef.getCharacterSetName());
column.setCollationName(columnRef.getCollationName());
column.setCurrency(columnRef.isCurrency());
column.setDefaultValue(columnRef.getDefaultValue());
column.setDistinctValueCount(columnRef.getDistinctValueCount());
column.setFixedLength(columnRef.isLengthFixed());
column.setFormat(columnRef.getFormat());
column.setMaximumValue(columnRef.getMaximumValue());
column.setMinimumValue(columnRef.getMinimumValue());
column.setNativeType(columnRef.getNativeType());
column.setNullable(getNullableType(columnRef.getNullable()));
column.setNullValueCount(columnRef.getNullValueCount());
column.setPrecision(columnRef.getPrecision());
column.setRadix(columnRef.getRadix());
column.setScale(columnRef.getScale());
column.setSearchability(getSearchabilityType(columnRef.getSearchability()));
column.setSelectable(columnRef.isSelectable());
column.setSigned(columnRef.isSigned());
column.setUpdateable(columnRef.isUpdateable());
String dType = columnRef.getDatatype();
if( dType == null || dType.length() == 0) {
dType = DatatypeProcessor.DEFAULT_DATATYPE;
}
EObject datatype = this.datatypeProcessor.findDatatype(dType);
if( datatype != null ) {
column.setType(datatype);
String dTypeName = this.modelEditor.getName(datatype);
int datatypeLength = columnRef.getLength();
if( datatypeLength == 0 && DatatypeProcessor.DEFAULT_DATATYPE.equalsIgnoreCase(dTypeName) ) {
columnRef.setLength(DatatypeProcessor.DEFAULT_DATATYPE_LENGTH);
} else {
columnRef.setLength(datatypeLength);
}
}
column.setLength(columnRef.getLength());
// Add to deferred list, if necessary
updateDeferredList(columnRef,column);
return column;
}
/**
* Create EMF Column from the supplied RelationalReference
* @param ref the relational object
* @param procedureResult the procedure result set
* @param modelResource the model resource
* @return the new object
*/
public EObject createColumn( RelationalReference ref, ProcedureResult procedureResult, ModelResource modelResource) {
CoreArgCheck.isInstanceOf(RelationalColumn.class, ref);
RelationalColumn columnRef = (RelationalColumn)ref;
Column column = FACTORY.createColumn();
column.setOwner(procedureResult);
column.setName(columnRef.getName());
column.setNameInSource(columnRef.getNameInSource());
column.setAutoIncremented(columnRef.isAutoIncremented());
column.setCaseSensitive(columnRef.isCaseSensitive());
column.setCharacterSetName(columnRef.getCharacterSetName());
column.setCollationName(columnRef.getCollationName());
column.setCurrency(columnRef.isCurrency());
column.setDefaultValue(columnRef.getDefaultValue());
column.setDistinctValueCount(columnRef.getDistinctValueCount());
column.setFixedLength(columnRef.isLengthFixed());
column.setFormat(columnRef.getFormat());
column.setLength(columnRef.getLength());
column.setMaximumValue(columnRef.getMaximumValue());
column.setMinimumValue(columnRef.getMinimumValue());
column.setNativeType(columnRef.getNativeType());
String nullableStr = columnRef.getNullable();
column.setNullable(getNullableType(nullableStr));
column.setNullValueCount(columnRef.getNullValueCount());
column.setPrecision(columnRef.getPrecision());
column.setRadix(columnRef.getRadix());
column.setScale(columnRef.getScale());
column.setSearchability(getSearchabilityType(columnRef.getSearchability()));
column.setSelectable(columnRef.isSelectable());
column.setSigned(columnRef.isSigned());
column.setUpdateable(columnRef.isUpdateable());
String dType = columnRef.getDatatype();
if( dType == null || dType.length() == 0) {
dType = DatatypeProcessor.DEFAULT_DATATYPE;
}
EObject datatype = this.datatypeProcessor.findDatatype(dType);
if( datatype != null ) {
column.setType(datatype);
String dTypeName = this.modelEditor.getName(datatype);
int datatypeLength = columnRef.getLength();
if( datatypeLength == 0 && DatatypeProcessor.DEFAULT_DATATYPE.equalsIgnoreCase(dTypeName) ) {
columnRef.setLength(DatatypeProcessor.DEFAULT_DATATYPE_LENGTH);
} else {
columnRef.setLength(datatypeLength);
}
}
// Add to deferred list, if necessary
updateDeferredList(columnRef,column);
return column;
}
/**
* Get the DirectionKind given the string representation
* @param dirKindStr the string representation
* @return the DirectionKind object equivalent
*/
private DirectionKind getDirectionKind(String dirKindStr) {
if( DIRECTION.IN.equalsIgnoreCase(dirKindStr) ) {
return DirectionKind.IN_LITERAL;
}
if( DIRECTION.IN_OUT.equalsIgnoreCase(dirKindStr) ) {
return DirectionKind.INOUT_LITERAL;
}
if( DIRECTION.OUT.equalsIgnoreCase(dirKindStr) ) {
return DirectionKind.OUT_LITERAL;
}
if( DIRECTION.RETURN.equalsIgnoreCase(dirKindStr) ) {
return DirectionKind.RETURN_LITERAL;
}
return DirectionKind.UNKNOWN_LITERAL;
}
/**
* Get the ProcedureUpdateCount given the string representation
* @param updCountStr the string representation
* @return the ProcedureUpdateCount object equivalent
*/
private ProcedureUpdateCount getUpdateCount(String updCountStr) {
if( ProcedureUpdateCount.AUTO_LITERAL.getName().equalsIgnoreCase(updCountStr) ) {
return ProcedureUpdateCount.AUTO_LITERAL;
}
if( ProcedureUpdateCount.ONE_LITERAL.getName().equalsIgnoreCase(updCountStr) ) {
return ProcedureUpdateCount.ONE_LITERAL;
}
if( ProcedureUpdateCount.MULTIPLE_LITERAL.getName().equalsIgnoreCase(updCountStr) ) {
return ProcedureUpdateCount.MULTIPLE_LITERAL;
}
if( ProcedureUpdateCount.ZERO_LITERAL.getName().equalsIgnoreCase(updCountStr) ) {
return ProcedureUpdateCount.ZERO_LITERAL;
}
return ProcedureUpdateCount.AUTO_LITERAL;
}
/**
* Get the MultiplicityKind given the string representation
* @param multKindStr the string representation
* @return the MultiplicityKind object equivalent
*/
private MultiplicityKind getMultiplictyKind(String multKindStr) {
if( MultiplicityKind.MANY_LITERAL.getName().equalsIgnoreCase(multKindStr) ) {
return MultiplicityKind.MANY_LITERAL;
}
if( MultiplicityKind.ONE_LITERAL.getName().equalsIgnoreCase(multKindStr) ) {
return MultiplicityKind.ONE_LITERAL;
}
if( MultiplicityKind.ZERO_TO_ONE_LITERAL.getName().equalsIgnoreCase(multKindStr) ) {
return MultiplicityKind.ZERO_TO_ONE_LITERAL;
}
if( MultiplicityKind.ZERO_TO_MANY_LITERAL.getName().equalsIgnoreCase(multKindStr) ) {
return MultiplicityKind.ZERO_TO_MANY_LITERAL;
}
return MultiplicityKind.UNSPECIFIED_LITERAL;
}
/**
* Get the NullableType given the string representation
* @param nullableStr the string representation
* @return the NullableType object equivalent
*/
private NullableType getNullableType(String nullableStr) {
if( NULLABLE.NULLABLE.equalsIgnoreCase(nullableStr) ) {
return NullableType.NULLABLE_LITERAL;
}
if( NULLABLE.NO_NULLS.equalsIgnoreCase(nullableStr) ) {
return NullableType.NO_NULLS_LITERAL;
}
return NullableType.NULLABLE_UNKNOWN_LITERAL;
}
/**
* Get the SearchabilityType given the string representation
* @param searchableStr the string representation
* @return the SearchabilityType object equivalent
*/
private SearchabilityType getSearchabilityType(String searchableStr) {
if( SEARCHABILITY.UNSEARCHABLE.equalsIgnoreCase(searchableStr) ) {
return SearchabilityType.UNSEARCHABLE_LITERAL;
}
if( SEARCHABILITY.ALL_EXCEPT_LIKE.equalsIgnoreCase(searchableStr) ) {
return SearchabilityType.ALL_EXCEPT_LIKE_LITERAL;
}
if( SEARCHABILITY.LIKE_ONLY.equalsIgnoreCase(searchableStr) ) {
return SearchabilityType.LIKE_ONLY_LITERAL;
}
return SearchabilityType.SEARCHABLE_LITERAL;
}
/**
* Create EMF Index from the supplied RelationalReference
* @param ref the relational index object
* @param modelResource the model resource
* @return the index object
*/
public EObject createIndex( RelationalReference ref, ModelResource modelResource) {
CoreArgCheck.isInstanceOf(RelationalIndex.class, ref);
RelationalIndex indexRef = (RelationalIndex)ref;
Index index = FACTORY.createIndex();
index.setName(indexRef.getName());
index.setNameInSource(index.getNameInSource());
index.setFilterCondition(indexRef.getFilterCondition());
index.setAutoUpdate(indexRef.isAutoUpdate());
index.setNullable(indexRef.isNullable());
index.setUnique(indexRef.isUnique());
// Add the columns in the correct order
final List indexColumns = index.getColumns();
for (RelationalColumn relColumn : indexRef.getColumns() ) {
BaseTable baseTable = getTable(relColumn.getParent().getName(), modelResource);
Column column = getColumn(relColumn.getName(), baseTable);
if( column != null ) {
indexColumns.add(column);
}
}
// Add to deferred list, if necessary
updateDeferredList(indexRef,index);
return index;
}
/**
* Create EMF PrimaryKey from the supplied RelationalReference
* @param ref the relational pk object
* @param modelResource the model resource
* @return the primary key object
*/
private EObject createPrimaryKey( RelationalReference ref, BaseTable baseTable, ModelResource modelResource) {
CoreArgCheck.isInstanceOf(RelationalPrimaryKey.class, ref);
RelationalPrimaryKey pkRef = (RelationalPrimaryKey)ref;
// Create the primary key
final PrimaryKey primaryKey = FACTORY.createPrimaryKey();
// Set the reference to the table ..
primaryKey.setTable(baseTable);
primaryKey.setName(pkRef.getName());
primaryKey.setNameInSource(pkRef.getNameInSource());
// Add the columns in the correct order
final List keyColumns = primaryKey.getColumns();
for (RelationalColumn relColumn : pkRef.getColumns() ) {
Column column = getColumn(relColumn.getName(), baseTable);
if( column != null ) {
keyColumns.add(column);
}
}
// Add to deferred list, if necessary
updateDeferredList(pkRef,primaryKey);
return primaryKey;
}
/**
* Create EMF ForeignKey from the supplied RelationalReference
* @param ref the relational fk object
* @param baseTable the table parent of the fk
* @param modelResource the model resource
* @return the foreign key object
*/
public EObject createForeignKey( RelationalReference ref, BaseTable baseTable, ModelResource modelResource) {
CoreArgCheck.isInstanceOf(RelationalForeignKey.class, ref);
RelationalForeignKey fkRef = (RelationalForeignKey)ref;
// Create the primary key
final ForeignKey foreignKey = FACTORY.createForeignKey();
// Set the reference to the table ..
foreignKey.setTable(baseTable);
foreignKey.setName(fkRef.getName());
foreignKey.setNameInSource(fkRef.getNameInSource());
foreignKey.setForeignKeyMultiplicity(getMultiplictyKind(fkRef.getForeignKeyMultiplicity()));
foreignKey.setPrimaryKeyMultiplicity(getMultiplictyKind(fkRef.getPrimaryKeyMultiplicity()));
// Add the columns in the correct order
final List keyColumns = foreignKey.getColumns();
for (RelationalColumn relColumn : fkRef.getColumns() ) {
Column column = getColumn(relColumn.getName(), baseTable);
if( column != null ) {
keyColumns.add(column);
}
}
BaseTable fkTable = getTable(fkRef.getUniqueKeyTableName(), modelResource);
String ukRefName = fkRef.getUniqueKeyName();
if( fkTable != null && ukRefName != null ) {
if( fkTable.getPrimaryKey() != null && fkTable.getPrimaryKey().getName().equalsIgnoreCase(ukRefName)) {
foreignKey.setUniqueKey(fkTable.getPrimaryKey());
} else if( fkTable.getUniqueConstraints().isEmpty() ) {
for( Object key : fkTable.getUniqueConstraints()) {
String keyName = this.modelEditor.getName((UniqueKey)key);
if( keyName.equalsIgnoreCase(ukRefName) ) {
foreignKey.setUniqueKey((UniqueKey)key);
}
}
}
}
// Add to deferred list, if necessary
updateDeferredList(fkRef,foreignKey);
return foreignKey;
}
/**
* Create EMF AccessPattern from the supplied RelationalReference
* @param ref the relational AccessPattern object
* @param baseTable the table parent of the ap
* @param modelResource the model resource
* @return the AccessPattern object
*/
private EObject createAccessPattern( RelationalReference ref, Table baseTable, ModelResource modelResource) {
CoreArgCheck.isInstanceOf(RelationalAccessPattern.class, ref);
RelationalAccessPattern apRef = (RelationalAccessPattern)ref;
// Create the primary key
final AccessPattern accessPattern = FACTORY.createAccessPattern();
// Set the reference to the table ..
accessPattern.setTable(baseTable);
accessPattern.setName(apRef.getName());
accessPattern.setNameInSource(apRef.getNameInSource());
// Add the columns in the correct order
final List keyColumns = accessPattern.getColumns();
for (RelationalColumn relColumn : apRef.getColumns() ) {
Column column = getColumn(relColumn.getName(), baseTable);
if( column != null ) {
keyColumns.add(column);
}
}
// Add to deferred list, if necessary
updateDeferredList(apRef,accessPattern);
return accessPattern;
}
/**
* Create EMF UniqueConstraint from the supplied RelationalReference
* @param ref the relational UniqueConstraint object
* @param baseTable the table parent of the uc
* @param modelResource the model resource
* @return the UniqueConstraint object
*/
private EObject createUniqueConstraint( RelationalReference ref, BaseTable baseTable, ModelResource modelResource) {
CoreArgCheck.isInstanceOf(RelationalUniqueConstraint.class, ref);
RelationalUniqueConstraint ucRef = (RelationalUniqueConstraint)ref;
// Create the primary key
final UniqueConstraint uniqueConstraint = FACTORY.createUniqueConstraint();
// Set the reference to the table ..
uniqueConstraint.setTable(baseTable);
uniqueConstraint.setName(ucRef.getName());
uniqueConstraint.setNameInSource(ucRef.getNameInSource());
// Add the columns in the correct order
final List keyColumns = uniqueConstraint.getColumns();
for (RelationalColumn relColumn : ucRef.getColumns() ) {
Column column = getColumn(relColumn.getName(), baseTable);
if( column != null ) {
keyColumns.add(column);
}
}
// Add to deferred list, if necessary
updateDeferredList(ucRef,uniqueConstraint);
return uniqueConstraint;
}
/**
*
* @param name
* @param baseTable
* @return
*/
private Column getColumn(String name, Table baseTable) {
for( Object column : baseTable.getColumns()) {
if( column instanceof Column && ((Column)column).getName().equalsIgnoreCase(name) ) {
return (Column)column;
}
}
return null;
}
/**
*
* @param tableName
* @param modelResource
* @return
*/
private BaseTable getTable(String tableName, ModelResource modelResource) {
try {
for (EObject eObj : modelResource.getEmfResource().getContents() ) {
String eObjName = this.modelEditor.getName(eObj);
if( eObj instanceof BaseTable && eObjName != null && eObjName.equalsIgnoreCase(tableName)) {
return (BaseTable)eObj;
}
}
} catch (ModelWorkspaceException e) {
RelationalPlugin.Util.log(IStatus.ERROR,
NLS.bind(Messages.emfModelGenerator_error_finding_table_named, tableName));
}
return null;
}
/**
* Create EMF Procedure from the supplied RelationalReference
* @param ref the relational Procedure object
* @param modelResource the model resource
* @return the Procedure object
*/
public EObject createProcedure( final RelationalReference ref, ModelResource modelResource) {
CoreArgCheck.isInstanceOf(RelationalProcedure.class, ref);
RelationalProcedure procedureRef = (RelationalProcedure)ref;
// Create and Set Name
Procedure procedure = FACTORY.createProcedure();
procedure.setName(procedureRef.getName());
procedure.setNameInSource(procedureRef.getNameInSource());
procedure.setFunction(procedureRef.isFunction());
procedure.setUpdateCount(getUpdateCount(procedureRef.getUpdateCount()));
// Add Columns
for( RelationalParameter paramRef : procedureRef.getParameters()) {
createParameter(paramRef, procedure, modelResource);
}
if( procedureRef.getResultSet() != null ) {
createResultSet(procedureRef.getResultSet(), procedure, modelResource);
}
// Add to deferred list, if necessary
updateDeferredList(procedureRef,procedure);
return procedure;
}
/**
* Create EMF ProcedureParameter from the supplied RelationalReference
* @param ref the parameter object
* @param procedure the parent procedure
* @param modelResource the model resource
* @return the ProcedureParameter EObject
*/
public EObject createParameter( RelationalReference ref, Procedure procedure, ModelResource modelResource) {
CoreArgCheck.isInstanceOf(RelationalParameter.class, ref);
RelationalParameter parameterRef = (RelationalParameter)ref;
ProcedureParameter parameter = FACTORY.createProcedureParameter();
parameter.setProcedure(procedure);
parameter.setName(parameterRef.getName());
parameter.setNameInSource(parameterRef.getNameInSource());
parameter.setDefaultValue(parameterRef.getDefaultValue());
parameter.setDirection(getDirectionKind(parameterRef.getDirection()));
parameter.setNativeType(parameterRef.getNativeType());
parameter.setNullable(getNullableType(parameterRef.getNullable()));
parameter.setPrecision(parameterRef.getPrecision());
parameter.setRadix(parameterRef.getRadix());
parameter.setScale(parameterRef.getScale());
String dType = parameterRef.getDatatype();
if( dType == null || dType.length() == 0) {
dType = DatatypeProcessor.DEFAULT_DATATYPE;
}
EObject datatype = this.datatypeProcessor.findDatatype(dType);
if( datatype != null ) {
parameter.setType(datatype);
String dTypeName = this.modelEditor.getName(datatype);
int datatypeLength = parameterRef.getLength();
if( datatypeLength == 0 && DatatypeProcessor.DEFAULT_DATATYPE.equalsIgnoreCase(dTypeName) ) {
parameter.setLength(DatatypeProcessor.DEFAULT_DATATYPE_LENGTH);
} else {
parameter.setLength(datatypeLength);
}
}