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