ref #9189: move datasource to webapp
[taxeditor.git] / eu.etaxonomy.taxeditor.webapp / src / main / java / eu / etaxonomy / taxeditor / webapp / datasource / wizard / CdmDataSourceTypeSelectionWizardPage.java
diff --git a/eu.etaxonomy.taxeditor.webapp/src/main/java/eu/etaxonomy/taxeditor/webapp/datasource/wizard/CdmDataSourceTypeSelectionWizardPage.java b/eu.etaxonomy.taxeditor.webapp/src/main/java/eu/etaxonomy/taxeditor/webapp/datasource/wizard/CdmDataSourceTypeSelectionWizardPage.java
new file mode 100644 (file)
index 0000000..0cedb73
--- /dev/null
@@ -0,0 +1,298 @@
+/**
+* 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 java.util.ArrayList;
+
+import org.eclipse.jface.wizard.IWizardPage;
+import org.eclipse.jface.wizard.Wizard;
+import org.eclipse.jface.wizard.WizardPage;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Combo;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Text;
+
+import eu.etaxonomy.cdm.database.DatabaseTypeEnum;
+import eu.etaxonomy.cdm.database.ICdmDataSource;
+import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
+import eu.etaxonomy.taxeditor.webapp.datasource.common.CdmDataSourceRepository;
+
+/**
+ * <p>CdmDataSourceTypeSelectionWizardPage class.</p>
+ *
+ * @author n.hoffmann
+ * @created 18.05.2009
+ * @version 1.0
+ */
+public class CdmDataSourceTypeSelectionWizardPage extends WizardPage implements ModifyListener{
+
+       public static final DatabaseTypeEnum[] supportedDatabaseTypes = new DatabaseTypeEnum[]{
+               DatabaseTypeEnum.MySQL,
+               DatabaseTypeEnum.H2,
+               DatabaseTypeEnum.PostgreSQL
+               /*DatabaseTypeEnum.SqlServer2005*/
+       };
+
+
+       private ArrayList<DatabaseTypeEnum> databaseTypes;
+
+       private Text datasourceNameText;
+       private String dataSourceName;
+       private Combo databaseTypeCombo;
+
+       private Composite composite;
+       private Composite editDatasourceComposite;
+
+       private NomenclaturalCode nomenclaturalCode;
+
+       private boolean dataBaseTypeSelected = false;
+       private boolean dataSourceNameSet = false;
+
+       private ICdmDataSource dataSource;
+
+       private WizardPage nextPage;
+
+       private CdmDataSourceCredentialsWizardPage credentialsWizardPage;
+
+       /**
+        * <p>Constructor for CdmDataSourceTypeSelectionWizardPage.</p>
+        *
+        * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
+        */
+       public CdmDataSourceTypeSelectionWizardPage(ICdmDataSource dataSource) {
+               super("DataSourceWizardPage");
+
+               this.dataSource = dataSource;
+
+               String pageName = dataSource == null ? "Create New Datasource" : "Edit Existing Datasource";
+
+               setTitle(pageName);
+       }
+
+       /* (non-Javadoc)
+        * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
+        */
+       /** {@inheritDoc} */
+       @Override
+    public void createControl(Composite parent) {
+
+               setPageComplete(false);
+
+               // Create top-level composite
+               composite = new Composite(parent, SWT.NONE);
+               GridLayout gridLayout = new GridLayout();
+               gridLayout.numColumns = 1;
+               composite.setLayout(gridLayout);
+
+               // Create editDatasourceComposite to display a dataSource's name and type
+               editDatasourceComposite = new Composite(composite, SWT.NONE);
+               GridData datasourceGridData = new GridData(SWT.FILL, SWT.TOP, true, true);
+               editDatasourceComposite.setLayoutData(datasourceGridData);
+               GridLayout datasourceLayout = new GridLayout();
+               datasourceLayout.numColumns = 2;
+               editDatasourceComposite.setLayout(datasourceLayout);
+
+               // Create label and input for dataSource name
+               Label datasourceNameLabel = new Label(editDatasourceComposite, SWT.NONE);
+               datasourceNameLabel.setText("Datasource Name:");
+               datasourceNameText = new Text(editDatasourceComposite, SWT.BORDER);
+               datasourceNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
+               datasourceNameText.addModifyListener(this);
+
+               // Create label and dropdown for database type
+               Label databaseTypeLabel = new Label(editDatasourceComposite, SWT.NONE);
+               databaseTypeLabel.setText("Database Type:");
+               databaseTypeCombo = new Combo(editDatasourceComposite, SWT.BORDER | SWT.READ_ONLY);
+               GridData comboLayout = new GridData(SWT.FILL, SWT.CENTER, false, false);
+               databaseTypeCombo.setLayoutData(comboLayout);
+               populateComboBoxItems();
+
+               // Create listener to display database type-specific config options
+               databaseTypeCombo.addSelectionListener(new SelectionAdapter() {
+                       @Override
+            public void widgetSelected(SelectionEvent e) {
+
+                               // Get database type at the selected index
+                               DatabaseTypeEnum type = databaseTypes.get(databaseTypeCombo.getSelectionIndex());
+
+                               addDatabasePage(type);
+                               setDataBaseTypeSelected(true);
+                               checkPageComplete();
+                       }
+               });
+
+               // make the composite the wizard pages control
+               setControl(composite);
+       }
+
+       private void populateComboBoxItems() {
+
+               // Init DB types
+               if (databaseTypes == null) {
+                       databaseTypes = new ArrayList<DatabaseTypeEnum>();
+               }
+
+               // Add types to the type drop-down and to the types collection
+               for (DatabaseTypeEnum type : supportedDatabaseTypes) {
+                       databaseTypeCombo.add(type.getName());
+                       databaseTypes.add(type);
+               }
+       }
+
+       /**
+        * @param type
+        */
+       private void addDatabasePage(DatabaseTypeEnum type) {
+               // add credentials wizard page according to selection
+               Wizard wizard = (Wizard) getWizard();
+               credentialsWizardPage = null;
+
+
+               if(type == DatabaseTypeEnum.H2){
+                       credentialsWizardPage = new CdmDataSourceH2WizardPage(dataSource,CdmDataSourceWizard.Mode.CREATE);
+               }
+               else if(type == DatabaseTypeEnum.MySQL){
+                       credentialsWizardPage = new CdmDataSourceMySQLWizardPage(dataSource, CdmDataSourceWizard.Mode.CREATE);
+               }
+               else if(type == DatabaseTypeEnum.PostgreSQL){
+                       credentialsWizardPage = new CdmDataSourcePostgreSQLServerWizardPage(dataSource, CdmDataSourceWizard.Mode.CREATE);
+               }
+
+//             else if(type == DatabaseTypeEnum.SqlServer2005){
+//                     credentialsWizardPage = new CdmDataSourceSQLServerWizardPage(dataSource);
+//             }
+
+               if(credentialsWizardPage != null && wizard.getPage(credentialsWizardPage.getName()) != null){
+                       nextPage = (WizardPage) wizard.getPage(credentialsWizardPage.getName());
+               }else{
+                       wizard.addPage(credentialsWizardPage);
+                       nextPage = credentialsWizardPage;
+               }
+
+               getContainer().updateButtons();
+       }
+
+
+       /* (non-Javadoc)
+        * @see org.eclipse.jface.wizard.WizardPage#getNextPage()
+        */
+       /** {@inheritDoc} */
+       @Override
+       public IWizardPage getNextPage() {
+               return nextPage;
+       }
+
+       /* (non-Javadoc)
+        * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
+        */
+       /** {@inheritDoc} */
+       @Override
+    public void modifyText(ModifyEvent e) {
+               String name = datasourceNameText.getText();
+
+               if(name.length() == 0){
+                       setDataSourceNameSet(false);
+                       setErrorMessage("DataSource name must not be empty.");
+               }else if(CdmDataSourceRepository.getDataSource(name) != null){
+                       setDataSourceNameSet(false);
+                       setErrorMessage("DataSource with the same name already exists");
+               }else{
+                       setDataSourceNameSet(true);
+                       setErrorMessage(null);
+               }
+               dataSourceName = name;
+               checkPageComplete();
+       }
+
+       /* (non-Javadoc)
+        * @see org.eclipse.jface.wizard.WizardPage#canFlipToNextPage()
+        */
+       /**
+        * <p>checkPageComplete</p>
+        */
+       public void checkPageComplete() {
+               boolean complete = isDataBaseTypeSelected();
+               complete &= isDataSourceNameSet();
+
+               setPageComplete(complete);
+       }
+
+       /**
+        * <p>Getter for the field <code>dataSourceName</code>.</p>
+        *
+        * @return a {@link java.lang.String} object.
+        */
+       public String getDataSourceName() {
+               return dataSourceName;
+       }
+
+       /**
+        * <p>Getter for the field <code>credentialsWizardPage</code>.</p>
+        *
+        * @return the credentialsWizardPage
+        */
+       public CdmDataSourceCredentialsWizardPage getCredentialsWizardPage() {
+               return credentialsWizardPage;
+       }
+
+       /**
+        * <p>isDataBaseTypeSelected</p>
+        *
+        * @return the dataBaseTypeSelected
+        */
+       public boolean isDataBaseTypeSelected() {
+               return dataBaseTypeSelected;
+       }
+
+       /**
+        * <p>Setter for the field <code>dataBaseTypeSelected</code>.</p>
+        *
+        * @param dataBaseTypeSelected the dataBaseTypeSelected to set
+        */
+       public void setDataBaseTypeSelected(boolean dataBaseTypeSelected) {
+               this.dataBaseTypeSelected = dataBaseTypeSelected;
+       }
+
+       /**
+        * <p>isDataSourceNameSet</p>
+        *
+        * @return the dataSourceNameSet
+        */
+       public boolean isDataSourceNameSet() {
+               return dataSourceNameSet;
+       }
+
+       /**
+        * <p>Setter for the field <code>dataSourceNameSet</code>.</p>
+        *
+        * @param dataSourceNameSet the dataSourceNameSet to set
+        */
+       public void setDataSourceNameSet(boolean dataSourceNameSet) {
+               this.dataSourceNameSet = dataSourceNameSet;
+       }
+
+       /**
+        * <p>Getter for the field <code>nomenclaturalCode</code>.</p>
+        *
+        * @return the nomenclaturalCode
+        */
+       public NomenclaturalCode getNomenclaturalCode() {
+               return nomenclaturalCode;
+       }
+
+
+}