-
Notifications
You must be signed in to change notification settings - Fork 49
/
JdbcImportWizard.java
1063 lines (916 loc) · 41.7 KB
/
JdbcImportWizard.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.jdbc.ui.wizards;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.datatools.connectivity.IConnectionProfile;
import org.eclipse.emf.common.util.URI;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbench;
import org.teiid.core.designer.event.IChangeListener;
import org.teiid.core.designer.event.IChangeNotifier;
import org.teiid.core.designer.util.CoreArgCheck;
import org.teiid.core.designer.util.CoreStringUtil;
import org.teiid.core.designer.util.I18nUtil;
import org.teiid.core.designer.util.Stopwatch;
import org.teiid.core.designer.util.StringUtilities;
import org.teiid.core.designer.util.FileUtils;
import org.teiid.designer.compare.DifferenceReport;
import org.teiid.designer.compare.ui.wizard.IDifferencingWizard;
import org.teiid.designer.compare.ui.wizard.ShowDifferencesPage;
import org.teiid.designer.core.ModelerCore;
import org.teiid.designer.core.notification.util.DefaultIgnorableNotificationSource;
import org.teiid.designer.core.validation.rules.CoreValidationRulesUtil;
import org.teiid.designer.core.workspace.ModelResource;
import org.teiid.designer.core.workspace.ModelResourceImpl;
import org.teiid.designer.core.workspace.ModelUtil;
import org.teiid.designer.core.workspace.ModelWorkspaceException;
import org.teiid.designer.core.workspace.OpenableImpl;
import org.teiid.designer.datatools.connection.ConnectionInfoHelper;
import org.teiid.designer.datatools.connection.DataSourceConnectionHelper;
import org.teiid.designer.datatools.connection.IConnectionInfoProvider;
import org.teiid.designer.datatools.ui.DatatoolsUiConstants;
import org.teiid.designer.extension.ExtensionPlugin;
import org.teiid.designer.extension.registry.ModelExtensionRegistry;
import org.teiid.designer.jdbc.JdbcException;
import org.teiid.designer.jdbc.JdbcPlugin;
import org.teiid.designer.jdbc.JdbcSource;
import org.teiid.designer.jdbc.metadata.JdbcDatabase;
import org.teiid.designer.jdbc.metadata.JdbcNode;
import org.teiid.designer.jdbc.relational.JdbcImporter;
import org.teiid.designer.jdbc.relational.RelationalModelProcessor;
import org.teiid.designer.jdbc.relational.impl.RelationalModelProcessorImpl;
import org.teiid.designer.jdbc.relational.util.JdbcModelProcessorManager;
import org.teiid.designer.jdbc.relational.util.JdbcRelationalUtil;
import org.teiid.designer.jdbc.ui.ModelerJdbcUiConstants;
import org.teiid.designer.jdbc.ui.util.JdbcUiUtil;
import org.teiid.designer.metamodels.core.ModelAnnotation;
import org.teiid.designer.metamodels.core.ModelType;
import org.teiid.designer.metamodels.relational.RelationalPackage;
import org.teiid.designer.metamodels.relational.extension.CoreModelExtensionAssistant;
import org.teiid.designer.metamodels.relational.extension.CoreModelExtensionConstants;
import org.teiid.designer.runtime.spi.ITeiidServer;
import org.teiid.designer.ui.UiPlugin;
import org.teiid.designer.ui.common.product.ProductCustomizerMgr;
import org.teiid.designer.ui.common.util.WidgetUtil;
import org.teiid.designer.ui.common.wizard.AbstractWizard;
import org.teiid.designer.ui.common.wizard.NoOpenProjectsWizardPage;
import org.teiid.designer.ui.editors.ModelEditorManager;
import org.teiid.designer.ui.viewsupport.DesignerPropertiesUtil;
import org.teiid.designer.ui.viewsupport.IPropertiesContext;
import org.teiid.designer.ui.viewsupport.ModelUtilities;
import org.teiid.designer.ui.viewsupport.ModelerUiViewUtils;
/**
* @since 8.0
*/
public class JdbcImportWizard extends AbstractWizard
implements IPropertiesContext, IDifferencingWizard, IJdbcImportInfoProvider, ModelerJdbcUiConstants, ModelerJdbcUiConstants.Images {
private static final String I18N_PREFIX = I18nUtil.getPropertyPrefix(JdbcImportWizard.class);
private static final String TITLE = getString("title"); //$NON-NLS-1$
private static final ImageDescriptor IMAGE = UiPlugin.getDefault().getImageDescriptor(IMPORT_DATABASE_ICON);
private static final String COPY_ERROR_MESSAGE = getString("copyErrorMessage"); //$NON-NLS-1$
private static final String IMPORT_ERROR_MESSAGE = getString("importErrorMessage"); //$NON-NLS-1$
private static final String UNDERSCORE_ONE = "_"; //$NON-NLS-1$
private static final String UNDERSCORE_TWO = "__"; //$NON-NLS-1$
private boolean doNotFinish = false;
/**
* @since 4.0
*/
private static String getString( final String id ) {
return Util.getString(I18N_PREFIX + id);
}
private JdbcImporter importer;
private IContainer folder;
private String modelName;
private IStatus status;
JdbcSourceSelectionPage srcPg;
private JdbcImportOptionsPage optionsPg;
private ShowDifferencesPage diffsPg;
DifferenceReport drDifferenceReport;
ProcessorPack ppProcessorPack;
private Properties designerProperties;
private boolean openProjectExists = true;
private IProject newProject;
boolean controlsHaveBeenCreated = false;
private boolean isVirtual;
private boolean isVdbSourceModel;
private IJdbcImportPostProcessor[] postProcessors;
// Need to cash the profile when connection is selected so we can use it in Finish method to
// inject the connection info into model.
private IConnectionProfile connectionProfile;
// private boolean isTeiidJdbcConnection = false;
// private String vdbName;
/**
* @since 4.0
*/
public JdbcImportWizard() {
super(UiPlugin.getDefault(), TITLE, IMAGE);
}
/**
* @see org.eclipse.jface.wizard.IWizard#canFinish()
* @since 4.0
*/
@Override
public boolean canFinish() {
boolean bCanFinish = false;
boolean bUpdateSelected = false;
// was update selected?
if (this.optionsPg != null) {
bUpdateSelected = this.optionsPg.updateSelected();
}
// if so, has user seen the diffs page?
boolean bDiffsDisplayed = false;
if (this.diffsPg != null) {
bDiffsDisplayed = this.diffsPg.isVisible();
}
boolean bUpdateStepsPending = false;
if (bUpdateSelected) {
if (!bDiffsDisplayed) {
bUpdateStepsPending = true;
}
}
bCanFinish = super.canFinish() && getFolder() != null && !bUpdateStepsPending;
return bCanFinish;
}
/**
* Method used to notify any pages that need to know if the connection changed so they can clear info, settings, or do any
* clean-up work. REQUIRED for Defect 19426 This is intended to be called in sqlConnectionChanged() if a connection is
* successfuly established.
*
* @since 4.3
*/
protected void connectionEstablished() {
// Default does nothing?
}
/**
* Creates the wizard page responsible for selection of types of metadata to import.
*
* @since 4.3
*/
protected ShowDifferencesPage createDifferencesPage() {
return new ShowDifferencesPage(this);
}
/**
* Creates the wizard page responsible for selection of types of metadata to import.
*
* @since 4.3
*/
protected IWizardPage createMetadataPage() {
JdbcImportMetadataPage page = new JdbcImportMetadataPage();
page.setImporter(this.importer);
return page;
}
/**
* Creates the wizard page responsible for selection of specific source objects from which to import.
*
* @since 4.3
*/
protected IWizardPage createObjectsPage() {
JdbcImportObjectsPage page = new JdbcImportObjectsPage();
page.setImporter(this.importer);
return page;
}
/**
* Creates the wizard page responsible for general import options.
*
* @since 4.3
*/
protected JdbcImportOptionsPage createOptionsPage() {
JdbcImportOptionsPage page = new JdbcImportOptionsPage();
page.setImporter(this.importer);
return page;
}
/**
* @see org.eclipse.jface.wizard.IWizard#createPageControls(org.eclipse.swt.widgets.Composite)
*/
@Override
public void createPageControls( Composite pageContainer ) {
super.createPageControls(pageContainer);
controlsHaveBeenCreated = true;
}
/**
* @see org.eclipse.jface.wizard.IWizard#dispose()
* @since 4.0
*/
@Override
public void dispose() {
try {
this.importer.disconnect();
} catch (final SQLException err) {
JdbcUiUtil.showAccessError(err);
}
super.dispose();
}
/**
* @see org.eclipse.jface.wizard.IWizard#performFinish()
* @since 4.0
*/
@Override
public boolean finish() {
if( this.doNotFinish ) {
return false;
}
if (!this.srcPg.connect()) {
return false;
}
boolean result = false;
/*
* 'finish' will use the previously created 'processor' instead of creating a fresh one, if one was previously created.
*/
// Save object selections from previous page
final IRunnableWithProgress op = new IRunnableWithProgress() {
@Override
public void run( final IProgressMonitor monitor ) throws InvocationTargetException {
// Wrap in transaction so it doesn't result in Significant Undoable
boolean started = ModelerCore.startTxn(false, false, "Changing Sql Connections", //$NON-NLS-1$
new DefaultIgnorableNotificationSource(JdbcImportWizard.this, modelName));
boolean succeeded = false;
try {
runFinish(monitor);
if (getDesignerProperties() != null) {
DesignerPropertiesUtil.setSourceModelName(getDesignerProperties(), modelName);
}
succeeded = true;
} finally {
if (started) {
if (succeeded) {
ModelerCore.commitTxn();
} else {
ModelerCore.rollbackTxn();
}
}
}
}
};
try {
new ProgressMonitorDialog(getShell()).run(true, true, op);
switch (this.status.getSeverity()) {
case IStatus.WARNING:
case IStatus.INFO:
case IStatus.OK:
break;
case IStatus.ERROR:
default:
Util.log(this.status);
WidgetUtil.showError(IMPORT_ERROR_MESSAGE);
break;
}
result = true;
} catch (Throwable err) {
if (err instanceof InvocationTargetException) {
err = ((InvocationTargetException)err).getTargetException();
}
JdbcUiUtil.showError(err, IMPORT_ERROR_MESSAGE);
}
return result;
}
/**
* @see org.teiid.designer.jdbc.ui.wizards.IJdbcImportInfoProvider#getDatabase()
* @since 5.0
*/
@Override
public JdbcDatabase getDatabase() {
return this.importer.getDatabase();
}
/**
* @see org.teiid.designer.jdbc.ui.wizards.IJdbcImportInfoProvider#getDifferenceReports()
* @since 5.0
*/
@Override
public List getDifferenceReports() {
final List reports = new ArrayList();
if (drDifferenceReport == null || diffTargetChanged(drDifferenceReport)) {
// Save object selections from previous page
final IRunnableWithProgress op = new IRunnableWithProgress() {
@Override
public void run( final IProgressMonitor monitor ) throws InvocationTargetException {
try {
final JdbcSource src = getSource();
final RelationalModelProcessor processor = JdbcModelProcessorManager.createRelationalModelProcessor(srcPg.getMetadataProcessor());
processor.setMoveRatherThanCopyAdds(!isUpdatedModel());
// set property on processor for Inclusion of incomplete FKs.
final boolean includeIncompleteFKs = getDatabase().getIncludes().includeIncompleteFKs();
processor.setIncludeIncompleteFKs(includeIncompleteFKs);
final IFile modelFile = getFolder().getFile(new Path(getModelName()));
final ModelResource resrc = ModelerCore.create(modelFile);
final ModelAnnotation modelAnnotation = resrc.getModelAnnotation();
modelAnnotation.setPrimaryMetamodelUri(RelationalPackage.eNS_URI);
ModelType type = ModelType.PHYSICAL_LITERAL;
isVirtual = optionsPg.isVirtual();
if( isVirtual ) {
type = ModelType.VIRTUAL_LITERAL;
}
modelAnnotation.setModelType(type);
if (resrc instanceof ModelResourceImpl) {
((ModelResourceImpl)resrc).setModelType(type);
}
if (processor instanceof RelationalModelProcessorImpl) {
JdbcImportWizard.this.drDifferenceReport = ((RelationalModelProcessorImpl)processor).generateDifferenceReport(resrc,
getDatabase(),
src.getImportSettings(),
monitor);
}
// any post create tasks?
// capture objects in the processorpack
ppProcessorPack = new ProcessorPack(processor, src, modelFile, resrc);
} catch (final OperationCanceledException err) {
// do nothing in particular
} catch (final Exception err) {
throw new InvocationTargetException(err);
} finally {
monitor.done();
}
}
};
// now run it
try {
new ProgressMonitorDialog(getShell()).run(true, true, op);
} catch (Throwable err) {
if (err instanceof InvocationTargetException) {
err = ((InvocationTargetException)err).getTargetException();
}
JdbcUiUtil.showError(err, IMPORT_ERROR_MESSAGE);
}
}
if (drDifferenceReport != null) {
reports.add(drDifferenceReport);
}
return reports;
}
/**
* Determines if current modelName selection is different than the DifferenceReport target
*
* @diffReport the difference report
* @return 'true' if the selected modelName is different than the diff report target
*/
private boolean diffTargetChanged( DifferenceReport diffReport ) {
boolean result = false;
// Get the target modelName from the difference report
String diffReportTargetModel = getDifferenceTargetModelName(diffReport);
// Determine if difference report target is different than selected modelName
if (diffReportTargetModel != null && !diffReportTargetModel.equalsIgnoreCase(getModelName())) {
result = true;
}
return result;
}
/**
* helper method to get the target model name from the difference report
*
* @param diffReport the difference report
* @return the diff report target model name
*/
private String getDifferenceTargetModelName( DifferenceReport diffReport ) {
String targetModelName = null;
String uriStr = diffReport.getResultUri();
URI uri = URI.createURI(uriStr);
if (uri.isFile()) {
File theFile = new File(uri.toFileString());
targetModelName = theFile.getName();
}
return targetModelName;
}
/**
* @since 4.0
*/
IContainer getFolder() {
return this.folder;
}
private IJdbcImportInfoProvider getImportInfoProvider() {
return this;
}
/**
* @see org.teiid.designer.jdbc.ui.wizards.IJdbcImportInfoProvider#getModelName()
* @since 5.0
*/
@Override
public String getModelName() {
return this.modelName;
}
/**
* @see org.teiid.designer.jdbc.ui.wizards.IJdbcImportInfoProvider#getModelResource()
* @since 5.5.3
*/
@Override
public ModelResource getModelResource() {
return this.ppProcessorPack.getModelResource();
}
/**
* @see org.teiid.designer.jdbc.ui.wizards.IJdbcImportInfoProvider#getPassword()
* @since 5.0
*/
@Override
public String getPassword() {
String result = null;
IWizardPage[] pages = getPages();
if (pages != null) {
for (int i = 0; i < pages.length; ++i) {
if (pages[i] instanceof JdbcSourceSelectionPage) {
result = ((JdbcSourceSelectionPage)pages[i]).getPassword();
break;
}
}
}
return result;
}
private IJdbcImportPostProcessor[] getPostProcessors() {
if (this.postProcessors == null) {
final String EXT_PT = ExtensionPoints.JdbcImportPostProcessor.ID;
final String CLASS_ATTR = ExtensionPoints.JdbcImportPostProcessor.CLASS_NAME;
IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(PLUGIN_ID, EXT_PT);
// shouldn't happen but check to be sure
if (extensionPoint == null) {
Util.log(IStatus.ERROR, Util.getString(I18N_PREFIX + "noExtensionPointFound", EXT_PT)); //$NON-NLS-1$
this.postProcessors = new IJdbcImportPostProcessor[0];
return this.postProcessors;
}
try {
// get all extensions
IExtension[] extensions = extensionPoint.getExtensions();
if (extensions.length != 0) {
List temp = new ArrayList(extensions.length);
for (int i = 0; i < extensions.length; ++i) {
IConfigurationElement[] elements = extensions[i].getConfigurationElements();
for (int j = 0; j < elements.length; ++j) {
try {
Object obj = elements[j].createExecutableExtension(CLASS_ATTR);
if (obj instanceof IJdbcImportPostProcessor) {
temp.add(obj);
} else {
Object[] params = new Object[] {obj.getClass(),
elements[j].getDeclaringExtension().getUniqueIdentifier()};
Util.log(IStatus.ERROR, Util.getString(I18N_PREFIX + "incorrectClass", params)); //$NON-NLS-1$
}
} catch (Exception theException) {
Object[] params = new Object[] {elements[j].getAttribute(CLASS_ATTR),
elements[j].getDeclaringExtension().getUniqueIdentifier()};
Util.log(IStatus.ERROR, Util.getString(I18N_PREFIX + "initError", params)); //$NON-NLS-1$
}
}
}
temp.toArray(this.postProcessors = new IJdbcImportPostProcessor[temp.size()]);
} else {
// no post processors found
this.postProcessors = new IJdbcImportPostProcessor[0];
}
} catch (Exception theException) {
Util.log(IStatus.ERROR, theException, Util.getString(I18N_PREFIX + "unexpectedErrorProcessingExtensions", EXT_PT)); //$NON-NLS-1$
this.postProcessors = new IJdbcImportPostProcessor[0];
}
}
return this.postProcessors;
}
/**
* @see org.teiid.designer.jdbc.ui.wizards.IJdbcImportInfoProvider#getSource()
* @since 5.0
*/
@Override
public JdbcSource getSource() {
return this.importer.getSource();
}
/**
* @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
* @since 4.0
*/
@Override
public void init( final IWorkbench workbench,
final IStructuredSelection inputSelection ) {
IStructuredSelection selection = inputSelection;
Object seletedObj = selection.getFirstElement();
boolean isSourceModel = false;
try {
if (seletedObj instanceof IFile) {
ModelResource modelResource = ModelUtil.getModelResource((IFile)seletedObj, false);
isSourceModel = ModelUtilities.isPhysical(modelResource);
}
} catch (Exception e) {
Util.log(e);
}
// We need to determine if the product characteristics dictate a hidden project or not
if (!isSourceModel && ProductCustomizerMgr.getInstance().getProductCharacteristics().isHiddenProjectCentric()) {
selection = new StructuredSelection(ProductCustomizerMgr.getInstance().getProductCharacteristics().getHiddenProject());
}
this.importer = new JdbcImporter();
// If not null, set folder to current selection if a folder or to containing folder if a model object
if (!selection.isEmpty() &&
(selection.getFirstElement() instanceof IProject && ((IProject)selection.getFirstElement()).isOpen() ) ) {
final Object obj = selection.getFirstElement();
final IContainer folder = ModelUtil.getContainer(obj);
try {
if (folder != null && folder.getProject().getNature(ModelerCore.NATURE_ID) != null) {
this.folder = folder;
}
} catch (final CoreException err) {
Util.log(err);
WidgetUtil.showError(err);
}
ModelResource model = null;
try {
model = JdbcRelationalUtil.getPhysicalModifiableRelationalModel(obj);
if (model != null) {
for (final Iterator iter = model.getAllRootEObjects().iterator(); iter.hasNext();) {
if (iter.next() instanceof JdbcSource) {
// If we want to support multiple source settings for a given model, then the importer needs
// to be updated to figure out which one to use initially during an update
this.importer.setUpdatedModel(model);
String name = FileUtils.getFilenameWithoutExtension(model.getItemName());
String validName = CoreValidationRulesUtil.getValidString(name, null, -1);
if (validName != null) {
name = validName;
while (name.indexOf(UNDERSCORE_TWO) > -1) {
name = name.replaceAll(UNDERSCORE_TWO, UNDERSCORE_ONE);
}
}
this.modelName = name + ModelerCore.MODEL_FILE_EXTENSION;
break;
}
}
}
} catch (final ModelWorkspaceException err) {
if (model != null && !model.hasErrors()) {
// Unexpected ...
Util.log(err);
}
WidgetUtil.showError(err);
}
} else {
openProjectExists = ModelerUiViewUtils.workspaceHasOpenModelProjects();
if( !openProjectExists ) {
newProject = ModelerUiViewUtils.queryUserToCreateModelProject();
if( newProject != null ) {
selection = new StructuredSelection(newProject);
openProjectExists = true;
} else {
addPage(NoOpenProjectsWizardPage.getStandardPage());
return;
}
}
}
// Create listener for changes to SQL connection
this.srcPg = new JdbcSourceSelectionPage(this.importer.getSource());
this.srcPg.setImporter(this.importer);
this.srcPg.addChangeListener(new IChangeListener() {
@Override
public void stateChanged( final IChangeNotifier notifier ) {
if (controlsHaveBeenCreated) {
// Wrap in transaction so it doesn't result in Significant Undoable
boolean started = ModelerCore.startTxn(false, false, "Changing Sql Connections", this); //$NON-NLS-1$
boolean succeeded = false;
try {
sqlConnectionChanged(JdbcImportWizard.this.srcPg);
succeeded = true;
} finally {
if (started) {
if (succeeded) {
ModelerCore.commitTxn();
} else {
ModelerCore.rollbackTxn();
}
}
}
}
}
});
this.optionsPg = createOptionsPage();
// If updating, mark options page incomplete to force user to select update checkbox on page
if (this.importer.getUpdatedModel() != null) {
this.optionsPg.setPageComplete(false);
}
this.diffsPg = createDifferencesPage();
addPage(this.srcPg);
addPage(createMetadataPage());
addPage(createObjectsPage());
addPage(this.optionsPg);
addPage(this.diffsPg);
}
/**
* @since 4.0
*/
boolean isUpdatedModel() {
return (this.importer.getUpdatedModel() != null);
}
void runFinish( IProgressMonitor monitor ) throws InvocationTargetException {
Stopwatch totalWatch = new Stopwatch();
totalWatch.start();
Stopwatch sWatch = new Stopwatch();
try {
sWatch.start();
if (ppProcessorPack == null) {
final JdbcSource src = getSource();
final RelationalModelProcessor processor = JdbcModelProcessorManager.createRelationalModelProcessor(srcPg.getMetadataProcessor());
// set property on processor for Inclusion of incomplete FKs.
final boolean includeIncompleteFKs = getDatabase().getIncludes().includeIncompleteFKs();
processor.setIncludeIncompleteFKs(includeIncompleteFKs);
// Added for debug performance logging purposes
processor.setMoveRatherThanCopyAdds(!isUpdatedModel());
final IFile modelFile = getFolder().getFile(new Path(getModelName()));
final ModelResource resrc = ModelerCore.create(modelFile);
final ModelAnnotation modelAnnotation = resrc.getModelAnnotation();
modelAnnotation.setPrimaryMetamodelUri(RelationalPackage.eNS_URI);
ModelType type = ModelType.PHYSICAL_LITERAL;
this.isVirtual = this.optionsPg.isVirtual();
this.isVdbSourceModel= this.importer.isVdbSourceModel();
if( this.isVirtual ) {
type = ModelType.VIRTUAL_LITERAL;
}
modelAnnotation.setModelType(type);
if (resrc instanceof ModelResourceImpl) {
((ModelResourceImpl)resrc).setModelType(type);
}
// Moved this call to AFTER the MODEL TYPE has been set.
ModelUtilities.initializeModelContainers(resrc, "Jdbc Import", this); //$NON-NLS-1$
if( this.isVdbSourceModel && srcPg.isTeiidConnection() ) {
// Inject VDB source model properties: locked = TRUE, vdb-name = "xxxx" , vdb-version = "y"
ModelExtensionRegistry registry = ExtensionPlugin.getInstance().getRegistry();
CoreModelExtensionAssistant assistant =
(CoreModelExtensionAssistant)registry.getModelExtensionAssistant(CoreModelExtensionConstants.NAMESPACE_PROVIDER.getNamespacePrefix());
assistant.saveModelExtensionDefinition(resrc);
assistant.setPropertyValue(modelAnnotation, CoreModelExtensionConstants.PropertyIds.LOCKED, Boolean.TRUE.toString());
assistant.setPropertyValue(modelAnnotation, CoreModelExtensionConstants.PropertyIds.VDB_NAME, srcPg.getVdbName());
}
JdbcImportWizard.this.status = processor.execute(resrc, getDatabase(), src.getImportSettings(), monitor);
// capture objects in the processorpack
ppProcessorPack = new ProcessorPack(processor, src, modelFile, resrc);
} else {
/*
* handle the case where we have already created a processor, allowed the user to modify the Diff Report, etc.
* ppProcessorPack The processor has special code inside its 'performMerge' method that uses the existing
* DifferenceProcessor if one exists (meaning it had been created by the 'generateProcessor(?)' method...So all we
* need to do here is to call 'execute' using the preexisting RelationalModelProcessor.
*/
JdbcImportWizard.this.status = ppProcessorPack.getProcessor().execute(ppProcessorPack.getModelResource(),
getDatabase(),
ppProcessorPack.getJdbcSource().getImportSettings(),
monitor);
}
sWatch.stop();
//
// post processors
//
if (!monitor.isCanceled() && JdbcImportWizard.this.status.getSeverity() != IStatus.ERROR) {
IJdbcImportPostProcessor[] processors = getPostProcessors();
for (int i = 0; i < processors.length; ++i) {
try {
processors[i].postProcess(getImportInfoProvider());
} catch (Exception theException) {
Util.log(IStatus.ERROR,
theException,
Util.getString(I18N_PREFIX + "postProcessingError", processors[i].getClass())); //$NON-NLS-1$
}
}
}
// cleanup
if (JdbcImportWizard.this.status.getSeverity() != IStatus.ERROR) {
sWatch.start(true);
// Remove the old source setting in the model, in case this is an update
// The loop below assumes that only one source setting can exist for a given model
final List objs = ppProcessorPack.getModelResource().getAllRootEObjects();
for (final Iterator iter = objs.iterator(); iter.hasNext();) {
if (iter.next() instanceof JdbcSource) {
iter.remove();
break;
}
}
// Add the source setting to the root of the model
ppProcessorPack.getJdbcSource().setJdbcDriver(null);
objs.add(ppProcessorPack.getJdbcSource());
sWatch.stop();
sWatch.start(true);
// Inject the connectionProfile into the ModelResource
if (this.connectionProfile != null && ! isUpdatedModel()) {
IConnectionInfoProvider provider = new JDBCConnectionInfoProvider();
provider.setConnectionInfo(ppProcessorPack.getModelResource(), this.connectionProfile);
if( !isVirtual ) {
String jndiName = importer.getJBossJndiName();
if( !StringUtilities.isEmpty(jndiName) ) {
ConnectionInfoHelper helper = new ConnectionInfoHelper();
helper.setJNDIName(ppProcessorPack.getModelResource(), jndiName);
}
}
}
// Check if Virtual, then re-set ModelType
if( isVirtual ) {
ppProcessorPack.getModelResource().getModelAnnotation().setModelType(ModelType.VIRTUAL_LITERAL);
((ModelResourceImpl)ppProcessorPack.getModelResource()).setModelType(ModelType.VIRTUAL_LITERAL);
}
// Auto save the model & refresh
((OpenableImpl)ppProcessorPack.getModelResource()).forceSave(monitor);
sWatch.stop();
sWatch.start(true);
final IResource resrc = this.folder.findMember(getModelName());
if (resrc != null) {
resrc.refreshLocal(IResource.DEPTH_INFINITE, monitor);
}
sWatch.stop();
sWatch.start(true);
ModelEditorManager.activate(ppProcessorPack.getModelFile(), true);
sWatch.stop();
}
} catch (final OperationCanceledException err) {
// do nothing in particular
} catch (final Exception err) {
throw new InvocationTargetException(err);
} finally {
monitor.done();
ppProcessorPack = null;
totalWatch.stop();
}
}
/**
* @since 4.0
*/
void setFolder( final IContainer folder ) {
CoreArgCheck.isNotNull(folder);
this.folder = folder;
}
/**
* @since 4.0
*/
void setModelName( final String name ) {
CoreArgCheck.isNotEmpty(name);
this.modelName = name;
}
ModelResource getUpdatedModel() {
return this.importer.getUpdatedModel();
}
/**
* @throws ModelWorkspaceException
* @since 4.0
*/
void setUpdatedModel( ModelResource model ) throws ModelWorkspaceException {
this.importer.setUpdatedModel(model);
}
/**
* @since 4.0
*/
void sqlConnectionChanged( final JdbcSourceSelectionPage page ) {
final Connection sqlConnection = page.getConnection();
this.connectionProfile = page.getConnectionProfile();
if (sqlConnection == null) {
this.importer.setDatabase(null);
} else {
try {
final JdbcSource src = (JdbcSource)ModelerCore.getModelEditor().copy(page.getSource());
this.importer.setSource(src);
final JdbcDatabase db = JdbcPlugin.getJdbcDatabase(src, sqlConnection);
JdbcPlugin.ensureNonNullImportSettings(src);
this.importer.setDatabase(db);
if (getModelName() == null) {
String tempModelName = CoreValidationRulesUtil.getValidString(db.getName(), null, -1);
if (tempModelName == null) {
tempModelName = db.getName();
}
tempModelName = FileUtils.toFileNameWithExtension(tempModelName, ModelerCore.MODEL_FILE_EXTENSION);
String validName = tempModelName;
while (validName.indexOf(UNDERSCORE_TWO) > -1) {
validName = validName.replaceAll(UNDERSCORE_TWO, UNDERSCORE_ONE);
}
tempModelName = validName;
if (!CoreStringUtil.isEmpty(tempModelName)
&& ModelerCore.getWorkspace().validateName(tempModelName, IResource.FILE).isOK()) {
this.modelName = tempModelName;
}
}
if (this.folder != null) {
final IResource resrc = this.folder.findMember(this.modelName);
if (resrc != null) {
final ModelResource model = JdbcRelationalUtil.getPhysicalModifiableRelationalModel(resrc);
if (model != null) {
this.importer.setUpdatedModel(model);
// Mark options page incomplete to force user to select update checkbox on page
this.optionsPg.setPageComplete(false);
}
}
}
// Cache the CP so it can be used during Finish method
this.connectionProfile = page.getConnectionProfile();
connectionEstablished();
} catch (final Exception err) {
JdbcUiUtil.showError(err, COPY_ERROR_MESSAGE);
}
}
getContainer().updateButtons();
}
JdbcNode findNode( final IPath path,
final JdbcNode parent ) throws JdbcException {
return this.importer.findNode(path, parent);
}
@Override
public void setProperties(Properties props) {
this.designerProperties = props;
if (this.folder == null) {
IContainer project = DesignerPropertiesUtil.getProject(designerProperties);
IContainer srcFolder = DesignerPropertiesUtil.getSourcesFolder(this.designerProperties);
if (srcFolder != null) {
setFolder(srcFolder);
} else if( project != null ) {
setFolder(project);
}
}
if( this.connectionProfile == null ) {
// check for project property and if sources folder property exists
String profileName = DesignerPropertiesUtil.getConnectionProfileName(this.designerProperties);
if( profileName != null && !profileName.isEmpty() ) {
// Select profile
srcPg.selectConnectionProfile(profileName);
}
}
}
/**
*
* @return designer properties
*/
public Properties getDesignerProperties() {
return this.designerProperties;
}
protected boolean handleCreateDataSource() {
boolean didDeployDS = false;
DataSourceConnectionHelper helper = new DataSourceConnectionHelper(ppProcessorPack.getModelResource(), connectionProfile);
if( importer.doCreateDataSource() && DataSourceConnectionHelper.isServerConnected() ) {
String dsName = importer.getJBossJndiName();
String jndiName = importer.getJBossJndiName();
Properties connProps = helper.getModelConnectionProperties();
String dsType = helper.getDataSourceType();
try {
DataSourceConnectionHelper.getServer().getOrCreateDataSource(dsName, jndiName, dsType, connProps);
didDeployDS = true;
} catch (Exception e) {
DatatoolsUiConstants.UTIL.log(e);
}