ref #9189: move datasource to webapp
[taxeditor.git] / eu.etaxonomy.taxeditor.webapp / src / main / java / eu / etaxonomy / taxeditor / webapp / datasource / wizard / CdmDataSourceWizard.java
diff --git a/eu.etaxonomy.taxeditor.webapp/src/main/java/eu/etaxonomy/taxeditor/webapp/datasource/wizard/CdmDataSourceWizard.java b/eu.etaxonomy.taxeditor.webapp/src/main/java/eu/etaxonomy/taxeditor/webapp/datasource/wizard/CdmDataSourceWizard.java
new file mode 100644 (file)
index 0000000..9418996
--- /dev/null
@@ -0,0 +1,162 @@
+/**
+* Copyright (C) 2007 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+
+package eu.etaxonomy.taxeditor.webapp.datasource.wizard;
+
+import org.eclipse.jface.wizard.Wizard;
+
+import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
+import eu.etaxonomy.cdm.database.ICdmDataSource;
+import eu.etaxonomy.taxeditor.webapp.datasource.common.CdmDataSourceRepository;
+
+/**
+ * <p>CdmDataSourceWizard class.</p>
+ *
+ * @author n.hoffmann
+ * @created 18.05.2009
+ * @version 1.0
+ */
+public class CdmDataSourceWizard extends Wizard {
+
+       private CdmDataSourceCredentialsWizardPage dataSourcePage;
+
+       private CdmDataSourceTypeSelectionWizardPage dataSourceSelectionPage;
+
+       private ICdmDataSource dataSource;
+
+       private String dataSourceName;
+
+       //private boolean editMode;
+
+       public enum Mode {
+               CREATE,
+               EDIT,
+               CLONE
+       }
+
+       // default mode is to create a new datasource
+       Mode mode = Mode.CREATE;
+
+       /**
+        * <p>Constructor for CdmDataSourceWizard.</p>
+        */
+       public CdmDataSourceWizard() {
+               super();
+               this.mode = Mode.CREATE;
+               setForcePreviousAndNextButtons(true);
+               setWindowTitle("Datasource Dialog");
+
+       }
+
+
+       /**
+        * <p>Constructor for CdmDataSourceWizard.</p>
+        *
+        * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
+        */
+       public CdmDataSourceWizard(ICdmDataSource dataSource, Mode mode) {
+               super();
+               if(dataSource != null){
+                       this.mode = mode;
+                       this.dataSource = dataSource;
+                       dataSourceName = dataSource.getName();
+               }
+               //setForcePreviousAndNextButtons(true);
+               setWindowTitle("Datasource Dialog");
+       }
+
+
+       /* (non-Javadoc)
+        * @see org.eclipse.jface.wizard.Wizard#addPages()
+        */
+       /** {@inheritDoc} */
+       @Override
+       public void addPages() {
+               switch(mode) {
+               case EDIT:
+               case CLONE:
+                       if(dataSource.getDatabaseType() == DatabaseTypeEnum.H2){
+                               dataSourcePage = new CdmDataSourceH2WizardPage(dataSource, mode);
+                       }else if(dataSource.getDatabaseType() == DatabaseTypeEnum.MySQL){
+                               dataSourcePage = new CdmDataSourceMySQLWizardPage(dataSource, mode);
+                       }else if(dataSource.getDatabaseType() == DatabaseTypeEnum.SqlServer2005){
+                               dataSourcePage = new CdmDataSourceSQLServerWizardPage(dataSource,mode);
+                       }else if(dataSource.getDatabaseType() == DatabaseTypeEnum.PostgreSQL){
+                               dataSourcePage = new CdmDataSourcePostgreSQLServerWizardPage(dataSource, mode);
+                       }else{
+                               throw new RuntimeException("Editing a datasource of type '" + dataSource.getDatabaseType() + "' is not supported yet.");
+                       }
+
+                       this.addPage(dataSourcePage);
+               case CREATE:
+                       dataSourceSelectionPage = new CdmDataSourceTypeSelectionWizardPage(dataSource);
+                       this.addPage(dataSourceSelectionPage);
+               default:
+
+               }
+
+       }
+
+       /* (non-Javadoc)
+        * @see org.eclipse.jface.wizard.Wizard#performFinish()
+        */
+       /** {@inheritDoc} */
+       @Override
+       public boolean performFinish() {
+               ICdmDataSource dataSource;
+               switch(mode) {
+               case EDIT:
+                       dataSource = dataSourcePage.getUpdatedDataSource();
+                       if(dataSourceName.equals(dataSource.getName())) {
+                               CdmDataSourceRepository.update(dataSourceName, dataSource);
+                       } else {
+                               CdmDataSourceRepository.replace(dataSourceName, dataSource);
+                       }
+                       return true;
+               case CLONE:
+                       dataSource = dataSourcePage.getUpdatedDataSource();
+                       CdmDataSourceRepository.save(dataSourcePage.getDataSourceName(), dataSource);
+                       return true;
+               case CREATE:
+                       if(dataSourceSelectionPage.getCredentialsWizardPage() != null){
+                               CdmDataSourceCredentialsWizardPage credentialsWizardPage = dataSourceSelectionPage.getCredentialsWizardPage();
+                               CdmDataSourceRepository.save(dataSourceSelectionPage.getDataSourceName(), credentialsWizardPage.getUpdatedDataSource());
+                               return true;
+                       } else {
+                               throw new IllegalStateException("Expected a datasource credentials page to exist");
+                       }
+               default:
+                       return false;
+               }
+
+       }
+
+       /* (non-Javadoc)
+        * @see org.eclipse.jface.wizard.Wizard#canFinish()
+        */
+       /** {@inheritDoc} */
+       @Override
+       public boolean canFinish() {
+               switch(mode) {
+               case EDIT:
+               case CLONE:
+                       return dataSourcePage.isPageComplete();
+               case CREATE:
+                       boolean result = true;
+                       result &= dataSourceSelectionPage.isPageComplete();
+                       if(dataSourceSelectionPage.getCredentialsWizardPage() != null){
+                               result &= dataSourceSelectionPage.getCredentialsWizardPage().isPageComplete();
+                       }
+                       return result;
+               default:
+                       return false;
+               }
+
+       }
+}