Merge branch 'release/5.19.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.local / src / main / java / eu / etaxonomy / taxeditor / local / datasource / wizard / CdmDataSourceWizard.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.local.datasource.wizard;
10
11 import org.eclipse.jface.wizard.Wizard;
12
13 import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
14 import eu.etaxonomy.cdm.database.ICdmDataSource;
15 import eu.etaxonomy.taxeditor.local.datasource.common.CdmDataSourceRepository;
16
17 /**
18 * <p>CdmDataSourceWizard class.</p>
19 *
20 * @author n.hoffmann
21 * @created 18.05.2009
22 */
23 public class CdmDataSourceWizard extends Wizard {
24
25 private CdmDataSourceCredentialsWizardPage dataSourcePage;
26
27 private CdmDataSourceTypeSelectionWizardPage dataSourceSelectionPage;
28
29 private ICdmDataSource dataSource;
30
31 private String dataSourceName;
32
33 //private boolean editMode;
34
35 public enum Mode {
36 CREATE,
37 EDIT,
38 CLONE
39 }
40
41 // default mode is to create a new datasource
42 Mode mode = Mode.CREATE;
43
44 /**
45 * <p>Constructor for CdmDataSourceWizard.</p>
46 */
47 public CdmDataSourceWizard() {
48 super();
49 this.mode = Mode.CREATE;
50 setForcePreviousAndNextButtons(true);
51 setWindowTitle("Datasource Dialog");
52 }
53
54 /**
55 * <p>Constructor for CdmDataSourceWizard.</p>
56 *
57 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
58 */
59 public CdmDataSourceWizard(ICdmDataSource dataSource, Mode mode) {
60 super();
61 if(dataSource != null){
62 this.mode = mode;
63 this.dataSource = dataSource;
64 dataSourceName = dataSource.getName();
65 }
66 //setForcePreviousAndNextButtons(true);
67 setWindowTitle("Datasource Dialog");
68 }
69
70 @Override
71 public void addPages() {
72 switch(mode) {
73 case EDIT:
74 case CLONE:
75 if(dataSource.getDatabaseType() == DatabaseTypeEnum.H2){
76 dataSourcePage = new CdmDataSourceH2WizardPage(dataSource, mode);
77 }else if(dataSource.getDatabaseType() == DatabaseTypeEnum.MySQL){
78 dataSourcePage = new CdmDataSourceMySQLWizardPage(dataSource, mode);
79 }else if(dataSource.getDatabaseType() == DatabaseTypeEnum.SqlServer2005){
80 dataSourcePage = new CdmDataSourceSQLServerWizardPage(dataSource,mode);
81 }else if(dataSource.getDatabaseType() == DatabaseTypeEnum.PostgreSQL){
82 dataSourcePage = new CdmDataSourcePostgreSQLServerWizardPage(dataSource, mode);
83 }else{
84 throw new RuntimeException("Editing a datasource of type '" + dataSource.getDatabaseType() + "' is not supported yet.");
85 }
86
87 this.addPage(dataSourcePage);
88 case CREATE:
89 dataSourceSelectionPage = new CdmDataSourceTypeSelectionWizardPage(dataSource);
90 this.addPage(dataSourceSelectionPage);
91 default:
92
93 }
94 }
95
96 @Override
97 public boolean performFinish() {
98 ICdmDataSource dataSource;
99 switch(mode) {
100 case EDIT:
101 dataSource = dataSourcePage.getUpdatedDataSource();
102 if(dataSourceName.equals(dataSource.getName())) {
103 CdmDataSourceRepository.update(dataSourceName, dataSource);
104 } else {
105 CdmDataSourceRepository.replace(dataSourceName, dataSource);
106 }
107 return true;
108 case CLONE:
109 dataSource = dataSourcePage.getUpdatedDataSource();
110 CdmDataSourceRepository.save(dataSourcePage.getDataSourceName(), dataSource);
111 return true;
112 case CREATE:
113 if(dataSourceSelectionPage.getCredentialsWizardPage() != null){
114 CdmDataSourceCredentialsWizardPage credentialsWizardPage = dataSourceSelectionPage.getCredentialsWizardPage();
115 CdmDataSourceRepository.save(dataSourceSelectionPage.getDataSourceName(), credentialsWizardPage.getUpdatedDataSource());
116 return true;
117 } else {
118 throw new IllegalStateException("Expected a datasource credentials page to exist");
119 }
120 default:
121 return false;
122 }
123
124 }
125
126 @Override
127 public boolean canFinish() {
128 switch(mode) {
129 case EDIT:
130 case CLONE:
131 return dataSourcePage.isPageComplete();
132 case CREATE:
133 boolean result = true;
134 result &= dataSourceSelectionPage.isPageComplete();
135 if(dataSourceSelectionPage.getCredentialsWizardPage() != null){
136 result &= dataSourceSelectionPage.getCredentialsWizardPage().isPageComplete();
137 }
138 return result;
139 default:
140 return false;
141 }
142 }
143 }