Merge branch 'release/5.18.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
10 package eu.etaxonomy.taxeditor.local.datasource.wizard;
11
12 import org.eclipse.jface.wizard.Wizard;
13
14 import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
15 import eu.etaxonomy.cdm.database.ICdmDataSource;
16 import eu.etaxonomy.taxeditor.local.datasource.common.CdmDataSourceRepository;
17
18 /**
19 * <p>CdmDataSourceWizard class.</p>
20 *
21 * @author n.hoffmann
22 * @created 18.05.2009
23 * @version 1.0
24 */
25 public class CdmDataSourceWizard extends Wizard {
26
27 private CdmDataSourceCredentialsWizardPage dataSourcePage;
28
29 private CdmDataSourceTypeSelectionWizardPage dataSourceSelectionPage;
30
31 private ICdmDataSource dataSource;
32
33 private String dataSourceName;
34
35 //private boolean editMode;
36
37 public enum Mode {
38 CREATE,
39 EDIT,
40 CLONE
41 }
42
43 // default mode is to create a new datasource
44 Mode mode = Mode.CREATE;
45
46 /**
47 * <p>Constructor for CdmDataSourceWizard.</p>
48 */
49 public CdmDataSourceWizard() {
50 super();
51 this.mode = Mode.CREATE;
52 setForcePreviousAndNextButtons(true);
53 setWindowTitle("Datasource Dialog");
54
55 }
56
57
58 /**
59 * <p>Constructor for CdmDataSourceWizard.</p>
60 *
61 * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
62 */
63 public CdmDataSourceWizard(ICdmDataSource dataSource, Mode mode) {
64 super();
65 if(dataSource != null){
66 this.mode = mode;
67 this.dataSource = dataSource;
68 dataSourceName = dataSource.getName();
69 }
70 //setForcePreviousAndNextButtons(true);
71 setWindowTitle("Datasource Dialog");
72 }
73
74
75 /* (non-Javadoc)
76 * @see org.eclipse.jface.wizard.Wizard#addPages()
77 */
78 /** {@inheritDoc} */
79 @Override
80 public void addPages() {
81 switch(mode) {
82 case EDIT:
83 case CLONE:
84 if(dataSource.getDatabaseType() == DatabaseTypeEnum.H2){
85 dataSourcePage = new CdmDataSourceH2WizardPage(dataSource, mode);
86 }else if(dataSource.getDatabaseType() == DatabaseTypeEnum.MySQL){
87 dataSourcePage = new CdmDataSourceMySQLWizardPage(dataSource, mode);
88 }else if(dataSource.getDatabaseType() == DatabaseTypeEnum.SqlServer2005){
89 dataSourcePage = new CdmDataSourceSQLServerWizardPage(dataSource,mode);
90 }else if(dataSource.getDatabaseType() == DatabaseTypeEnum.PostgreSQL){
91 dataSourcePage = new CdmDataSourcePostgreSQLServerWizardPage(dataSource, mode);
92 }else{
93 throw new RuntimeException("Editing a datasource of type '" + dataSource.getDatabaseType() + "' is not supported yet.");
94 }
95
96 this.addPage(dataSourcePage);
97 case CREATE:
98 dataSourceSelectionPage = new CdmDataSourceTypeSelectionWizardPage(dataSource);
99 this.addPage(dataSourceSelectionPage);
100 default:
101
102 }
103
104 }
105
106 /* (non-Javadoc)
107 * @see org.eclipse.jface.wizard.Wizard#performFinish()
108 */
109 /** {@inheritDoc} */
110 @Override
111 public boolean performFinish() {
112 ICdmDataSource dataSource;
113 switch(mode) {
114 case EDIT:
115 dataSource = dataSourcePage.getUpdatedDataSource();
116 if(dataSourceName.equals(dataSource.getName())) {
117 CdmDataSourceRepository.update(dataSourceName, dataSource);
118 } else {
119 CdmDataSourceRepository.replace(dataSourceName, dataSource);
120 }
121 return true;
122 case CLONE:
123 dataSource = dataSourcePage.getUpdatedDataSource();
124 CdmDataSourceRepository.save(dataSourcePage.getDataSourceName(), dataSource);
125 return true;
126 case CREATE:
127 if(dataSourceSelectionPage.getCredentialsWizardPage() != null){
128 CdmDataSourceCredentialsWizardPage credentialsWizardPage = dataSourceSelectionPage.getCredentialsWizardPage();
129 CdmDataSourceRepository.save(dataSourceSelectionPage.getDataSourceName(), credentialsWizardPage.getUpdatedDataSource());
130 return true;
131 } else {
132 throw new IllegalStateException("Expected a datasource credentials page to exist");
133 }
134 default:
135 return false;
136 }
137
138 }
139
140 /* (non-Javadoc)
141 * @see org.eclipse.jface.wizard.Wizard#canFinish()
142 */
143 /** {@inheritDoc} */
144 @Override
145 public boolean canFinish() {
146 switch(mode) {
147 case EDIT:
148 case CLONE:
149 return dataSourcePage.isPageComplete();
150 case CREATE:
151 boolean result = true;
152 result &= dataSourceSelectionPage.isPageComplete();
153 if(dataSourceSelectionPage.getCredentialsWizardPage() != null){
154 result &= dataSourceSelectionPage.getCredentialsWizardPage().isPageComplete();
155 }
156 return result;
157 default:
158 return false;
159 }
160
161 }
162 }