From 5ed160dc34a7411d2f2f54984895da9e88ff081a Mon Sep 17 00:00:00 2001 From: Cherian Mathew Date: Tue, 6 May 2014 14:37:45 +0000 Subject: [PATCH] refactored wizard dialog and pages to allow for easy in place editing and cloning of datasources. CdmDataSourceRepository : added methods for (I)PersistentRemoteSource --- .../datasource/CdmDataSourceRepository.java | 133 ++++++++--- .../CdmDataSourceCredentialsWizardPage.java | 209 ++++++++++++++---- .../wizard/CdmDataSourceH2WizardPage.java | 25 ++- .../wizard/CdmDataSourceMySQLWizardPage.java | 71 ++++-- ...mDataSourcePostgreSQLServerWizardPage.java | 59 +++-- .../CdmDataSourceSQLServerWizardPage.java | 53 +++-- .../wizard/CdmDataSourceWizard.java | 75 +++++-- 7 files changed, 477 insertions(+), 148 deletions(-) diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/CdmDataSourceRepository.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/CdmDataSourceRepository.java index 569c35a0e..f6c4828b2 100644 --- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/CdmDataSourceRepository.java +++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/CdmDataSourceRepository.java @@ -23,17 +23,22 @@ import org.eclipse.ui.IMemento; import org.eclipse.ui.XMLMemento; import eu.etaxonomy.cdm.config.CdmPersistentSourceUtils; +import eu.etaxonomy.cdm.config.ICdmPersistentSource; +import eu.etaxonomy.cdm.config.ICdmSource; import eu.etaxonomy.cdm.database.CdmDataSource; import eu.etaxonomy.cdm.database.CdmPersistentDataSource; import eu.etaxonomy.cdm.database.DataSourceNotFoundException; import eu.etaxonomy.cdm.database.ICdmDataSource; import eu.etaxonomy.cdm.model.name.NomenclaturalCode; +import eu.etaxonomy.cdm.remote.CdmPersistentRemoteSource; +import eu.etaxonomy.cdm.remote.CdmRemoteSourceException; +import eu.etaxonomy.cdm.remote.ICdmRemoteSource; import eu.etaxonomy.taxeditor.model.MementoHelper; import eu.etaxonomy.taxeditor.preference.PreferencesUtil; import eu.etaxonomy.taxeditor.store.CdmStore; import eu.etaxonomy.taxeditor.store.StoreUtil; import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin; -public class CdmDataSourceRepository{; +public class CdmDataSourceRepository{ /** Constant TAG_DATASOURCE="tagDataSource" */ public static final String TAG_DATASOURCE = "tagDataSource"; @@ -41,7 +46,7 @@ public class CdmDataSourceRepository{; private static final String DEFAULT_DATASOURCE_STATE_FILENAME = "datasource.xml"; private static final String DEFAULT_DATASOURCE_NAME = "cdm"; - private static ICdmDataSource currentDataSource; + private static ICdmSource currentCdmSource; private static IMemento memento; private static String lastUsedDataSourceName; @@ -64,8 +69,8 @@ public class CdmDataSourceRepository{; * @param dataSource a {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource} object. * @return a boolean. */ - public static boolean delete(CdmPersistentDataSource dataSource) { - CdmPersistentSourceUtils.delete(dataSource); + public static boolean delete(ICdmPersistentSource cdmPersistentSource) { + CdmPersistentSourceUtils.delete(cdmPersistentSource); return true; } @@ -75,20 +80,28 @@ public class CdmDataSourceRepository{; * * @return a {@link java.util.List} object. */ - public static List getAll() { - List dataSources = new ArrayList(); + public static List getAll() { + List remoteSources = new ArrayList(); for(ICdmDataSource dataSource : CdmPersistentDataSource.getAllDataSources()){ try { - dataSources.add(CdmPersistentDataSource.NewInstance(dataSource.getName())); + remoteSources.add(CdmPersistentDataSource.NewInstance(dataSource.getName())); } catch (DataSourceNotFoundException e) { StoreUtil.error(CdmDataSourceRepository.class, "Could not find dataSource", e); } } + try { + for(ICdmRemoteSource remoteSource : CdmPersistentRemoteSource.getAllRemoteSources()){ + remoteSources.add(remoteSource); + } + } catch (CdmRemoteSourceException e) { + StoreUtil.error(CdmDataSourceRepository.class, "Error retrieving remote sources", e); + } + // TODO sort by database name - return dataSources; + return remoteSources; } /** @@ -111,25 +124,35 @@ public class CdmDataSourceRepository{; return null; } + /** *

Getter for the field currentDataSource.

* * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object. + * @throws CdmRemoteSourceException */ - public static ICdmDataSource getCurrentDataSource() { - if (currentDataSource == null) { - try { - currentDataSource = CdmPersistentDataSource.NewInstance(getLastUsedDataSourceName()); - } catch (DataSourceNotFoundException e) { - // fallback creates a new default - ICdmDataSource h2DataSource = CdmDataSource.NewH2EmbeddedInstance( - DEFAULT_DATASOURCE_NAME, "sa", "", PreferencesUtil.getPreferredNomenclaturalCode()); - save(h2DataSource.getName(), h2DataSource); - setCurrentDataSource(h2DataSource); + public static ICdmSource getCurrentCdmSource() throws CdmRemoteSourceException { + + if (currentCdmSource == null) { + String lastUsedCdmSourceName = getLastUsedDataSourceName(); + if(lastUsedCdmSourceName.endsWith(CdmPersistentDataSource.DATASOURCE_BEAN_POSTFIX)) { + try { + currentCdmSource = CdmPersistentDataSource.NewInstance(lastUsedCdmSourceName); + } catch (DataSourceNotFoundException e) { + // fallback creates a new default + ICdmDataSource h2DataSource = CdmDataSource.NewH2EmbeddedInstance( + DEFAULT_DATASOURCE_NAME, "sa", "", PreferencesUtil.getPreferredNomenclaturalCode()); + save(h2DataSource.getName(), h2DataSource); + setCurrentCdmSource(h2DataSource); + } + } else if (lastUsedCdmSourceName.endsWith(CdmPersistentRemoteSource.REMOTESOURCE_BEAN_POSTFIX)) { + currentCdmSource = CdmPersistentRemoteSource.NewInstance(lastUsedCdmSourceName); + } else { + throw new CdmRemoteSourceException("Unkown Cdm Source Type"); } } - return currentDataSource; + return currentCdmSource; } /** @@ -138,9 +161,9 @@ public class CdmDataSourceRepository{; * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object. * @return a boolean. */ - public static boolean setCurrentDataSource(ICdmDataSource dataSource) { - currentDataSource = dataSource; - NomenclaturalCode dataSourceNomenclaturalCode = dataSource.getNomenclaturalCode(); + public static boolean setCurrentCdmSource(ICdmSource cdmSource) { + currentCdmSource = cdmSource; + NomenclaturalCode dataSourceNomenclaturalCode = cdmSource.getNomenclaturalCode(); NomenclaturalCode applicationNomenclaturalCode = PreferencesUtil.getPreferredNomenclaturalCode(); if( dataSourceNomenclaturalCode != null && ! dataSourceNomenclaturalCode.equals(applicationNomenclaturalCode)){ @@ -158,10 +181,9 @@ public class CdmDataSourceRepository{; * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object. * @return a boolean. */ - public static boolean changeDataSource(final ICdmDataSource dataSource) { - saveAsCurrentDatabaseToMemento(dataSource); - - CdmStore.connect(dataSource); + public static boolean changeDataSource(final ICdmSource cdmSource) { + saveAsCurrentDatabaseToMemento(cdmSource); + CdmStore.connect(cdmSource); return true; } @@ -172,9 +194,47 @@ public class CdmDataSourceRepository{; * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object. * @param strDataSourceName a {@link java.lang.String} object. * @return a {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource} object. + * @throws CdmRemoteSourceException + */ + public static ICdmPersistentSource save(String strCdmSourceName, ICdmRemoteSource cdmSource) throws CdmRemoteSourceException { + return CdmPersistentRemoteSource.save(strCdmSourceName, cdmSource); + } + + /** + *

save

+ * + * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object. + * @param strDataSourceName a {@link java.lang.String} object. + * @return a {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource} object. + * @throws CdmRemoteSourceException */ - public static CdmPersistentDataSource save(String strDataSourceName, ICdmDataSource dataSource) { - return CdmPersistentDataSource.save(strDataSourceName, dataSource); + public static ICdmPersistentSource save(String strCdmSourceName, ICdmDataSource cdmSource) { + return CdmPersistentDataSource.save(strCdmSourceName, cdmSource); + } + + /** + *

update

+ * + * @param strDataSourceName a {@link java.lang.String} object. + * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object. + * @return a {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource} object. + */ + public static ICdmPersistentSource update(String strCdmSourceName, ICdmDataSource cdmSource){ + try { + return CdmPersistentDataSource.update(strCdmSourceName, cdmSource); + } catch (Exception e) { + StoreUtil.error(CdmDataSourceRepository.class, "Error updating CDM Source", e); + } + return null; + } + + public static ICdmPersistentSource replace(String strCdmSourceName, ICdmDataSource cdmSource){ + try { + return CdmPersistentDataSource.replace(strCdmSourceName, cdmSource); + } catch (Exception e) { + StoreUtil.error(CdmDataSourceRepository.class, "Error updating CDM Source", e); + } + return null; } /** @@ -184,11 +244,12 @@ public class CdmDataSourceRepository{; * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object. * @return a {@link eu.etaxonomy.cdm.database.CdmPersistentDataSource} object. */ - public static CdmPersistentDataSource update(String strDataSourceName, ICdmDataSource dataSource){ + public static ICdmPersistentSource update(String strCdmSourceName, ICdmRemoteSource cdmSource){ try { - return CdmPersistentDataSource.update(strDataSourceName, dataSource); + return CdmPersistentRemoteSource.update(strCdmSourceName, cdmSource); } catch (Exception e) { - StoreUtil.error(CdmDataSourceRepository.class, "Error updating datasource", e); + StoreUtil.error(CdmDataSourceRepository.class, "Error updating CDM Source", e); + } return null; } @@ -197,17 +258,17 @@ public class CdmDataSourceRepository{; * Memento Handling * *********************************************************/ - private static void saveAsCurrentDatabaseToMemento(ICdmDataSource dataSource){ + private static void saveAsCurrentDatabaseToMemento(ICdmSource cdmSource){ if(memento == null) memento = readMemento(); - memento.putString(CURRENT_DATASOURCE, dataSource.getName()); + memento.putString(CURRENT_DATASOURCE, cdmSource.getName()); saveMementoToFile(memento); } /* * Answer the workbench state file. */ - private static File getDataSourceStateFile() { + private static File getCdmSourceStateFile() { IPath path = TaxeditorStorePlugin.getDefault().getStateLocation(); if (path == null) { return null; @@ -218,7 +279,7 @@ public class CdmDataSourceRepository{; private static IMemento readMemento(){ try { - return MementoHelper.readMementoFromFile(getDataSourceStateFile()); + return MementoHelper.readMementoFromFile(getCdmSourceStateFile()); } catch (FileNotFoundException e) { return initializeMemento(); } @@ -238,6 +299,6 @@ public class CdmDataSourceRepository{; * Save the workbench UI in a persistence file. */ private static IMemento saveMementoToFile(IMemento memento) { - return MementoHelper.saveMementoToFile(memento, getDataSourceStateFile()); + return MementoHelper.saveMementoToFile(memento, getCdmSourceStateFile()); } } diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceCredentialsWizardPage.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceCredentialsWizardPage.java index 1d3e75444..9ca130b9b 100644 --- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceCredentialsWizardPage.java +++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceCredentialsWizardPage.java @@ -28,9 +28,11 @@ import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Text; +import eu.etaxonomy.cdm.database.CdmDataSource; import eu.etaxonomy.cdm.database.CdmPersistentDataSource; import eu.etaxonomy.cdm.database.ICdmDataSource; import eu.etaxonomy.cdm.model.name.NomenclaturalCode; +import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository; import eu.etaxonomy.taxeditor.model.NomenclaturalCodeHelper; import eu.etaxonomy.taxeditor.preference.PreferencesUtil; @@ -42,12 +44,12 @@ import eu.etaxonomy.taxeditor.preference.PreferencesUtil; * @created 19.05.2009 * @version 1.0 */ -public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage implements ModifyListener{ - +public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage implements ModifyListener { private ICdmDataSource dataSource; protected Text text_password; protected Text text_databaseName; + protected Text text_dataSourceName; protected Text text_username; protected Group authenticationGroup; @@ -58,20 +60,41 @@ public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage impl protected Composite parent; + protected String name; protected String database; protected String username; protected String password; protected NomenclaturalCode nomenclaturalCode; + CdmDataSourceWizard.Mode mode; + + /** + *

Constructor for CdmDataSourceCredentialsWizardPage.

+ * + * @param pageName a {@link java.lang.String} object. + */ + protected CdmDataSourceCredentialsWizardPage(String pageName, ICdmDataSource dataSource) { + super(pageName); + this.setPageComplete(false); + setDataSource(dataSource); + } + /** *

Constructor for CdmDataSourceCredentialsWizardPage.

* * @param pageName a {@link java.lang.String} object. */ - protected CdmDataSourceCredentialsWizardPage(String pageName) { + protected CdmDataSourceCredentialsWizardPage(String pageName, ICdmDataSource dataSource, CdmDataSourceWizard.Mode mode) { super(pageName); this.setPageComplete(false); + + this.mode = mode; + if(mode == CdmDataSourceWizard.Mode.CLONE) { + setDataSource(CdmDataSource.NewInstance(dataSource)); + } else { + setDataSource(dataSource); + } } /* (non-Javadoc) @@ -82,13 +105,33 @@ public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage impl this.parent = parent; // Create top-level composite - + parent.setLayout(new GridLayout()); composite = new Composite(parent, SWT.NONE); - composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1)); + composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,2,5)); GridLayout formLayout = new GridLayout(); formLayout.numColumns = 2; composite.setLayout(formLayout); + + + // Create composite for data source name + Composite editDatasourceComposite = new Composite(composite, SWT.NONE); + GridData datasourceGridData = new GridData(SWT.FILL, SWT.TOP, true, true,2,1); + 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:"); + text_dataSourceName = new Text(editDatasourceComposite, SWT.BORDER); + text_dataSourceName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); + + if(getDataSource() == null) { + editDatasourceComposite.setVisible(false); + } + // create a database specific form createDatabaseForm(); @@ -102,7 +145,7 @@ public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage impl Composite buttonComposite = new Composite(composite, SWT.NONE); buttonComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false)); GridLayout buttonLayout = new GridLayout(); - buttonLayout.numColumns = 2; + buttonLayout.numColumns = 1; buttonComposite.setLayout(buttonLayout); // Create test connection button @@ -122,8 +165,15 @@ public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage impl }); setControl(composite); - + init(); + + if(mode == CdmDataSourceWizard.Mode.CLONE) { + getDataSource().setName(""); + getDataSource().setDatabase(""); + text_dataSourceName.setText(""); + text_databaseName.setText(""); + } } /** @@ -133,7 +183,7 @@ public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage impl // Create group composite for authentication data authenticationGroup = new Group(composite, SWT.NONE); authenticationGroup.setText("Authentication"); - authenticationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); + authenticationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 3)); GridLayout authenticationLayout = new GridLayout(); authenticationLayout.numColumns = 2; authenticationGroup.setLayout(authenticationLayout); @@ -145,7 +195,7 @@ public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage impl // Create database name input text_databaseName = new Text(authenticationGroup, SWT.BORDER); text_databaseName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); - text_databaseName.addModifyListener(this); + // Create username label Label usernameLabel = new Label(authenticationGroup, SWT.NONE); @@ -154,7 +204,7 @@ public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage impl // Create username input text_username = new Text(authenticationGroup, SWT.BORDER); text_username.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); - text_username.addModifyListener(this); + // Create password label Label passwordLabel = new Label(authenticationGroup, SWT.NONE); @@ -163,9 +213,38 @@ public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage impl // Create password input text_password = new Text(authenticationGroup, SWT.BORDER | SWT.PASSWORD); text_password.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); + + } + + /** + * Initialize text fields + */ + public void init() { + removeListeners(); + if(getDataSource() != null){ + text_dataSourceName.setText(getDataSource().getName()); + text_databaseName.setText(getDataSource().getDatabase()); + text_username.setText(getDataSource().getUsername()); + text_password.setText(getDataSource().getPassword()); + } + // add listeners after setting text to avoid the modify event being called + // for the initial value + addListeners(); + } + + private void addListeners() { + text_dataSourceName.addModifyListener(this); + text_databaseName.addModifyListener(this); + text_username.addModifyListener(this); text_password.addModifyListener(this); } + private void removeListeners() { + text_dataSourceName.removeModifyListener(this); + text_databaseName.removeModifyListener(this); + text_username.removeModifyListener(this); + text_password.removeModifyListener(this); + } /** * Create a radio button group to select a nomenclatural code from */ @@ -191,21 +270,33 @@ public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage impl } } - + public void testDbConfiguration(){ + testDbConfiguration(false); + } /** * Tries to open a connection to the given dataSource. Generates a message on either * failure or success */ - public void testDbConfiguration(){ + public void testDbConfiguration(boolean ignoreSuccess){ try{ + updateAndCheck(); + updateDataSource(); getDataSource().testConnection(); - MessageDialog.openConfirm(parent.getShell(), "Test successful", "Test successful!"); + if(!ignoreSuccess) { + MessageDialog.openConfirm(parent.getShell(), "Connection Test successful", "Test successful!"); + } } catch(SQLException e){ - MessageDialog.openWarning(parent.getShell(), "Test unsuccessful", "Reason: " + e.getMessage()); + MessageDialog.openWarning(parent.getShell(), "Connection Test unsuccessful", "Reason: " + e.getMessage() + + System.lineSeparator() + + System.lineSeparator() + + "Please double check the connection fields"); throw new RuntimeException(e); } catch (ClassNotFoundException e) { - MessageDialog.openWarning(parent.getShell(), "Test unsuccessful", "Reason: " + e.getMessage()); + MessageDialog.openWarning(parent.getShell(), "Connection Test unsuccessful", "Reason: " + e.getMessage() + + System.lineSeparator() + + System.lineSeparator() + + "Please double check the connection fields"); throw new RuntimeException(e); } } @@ -229,28 +320,21 @@ public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage impl *

checkPageComplete

*/ public void checkPageComplete(){ - boolean complete = database.length() != 0; -// complete &= username.length() != 0; - + boolean complete = false; + if(mode == CdmDataSourceWizard.Mode.CREATE) { + complete = database.length() != 0; + } else { + complete = name.length() != 0 && database.length() != 0; + } this.setPageComplete(complete); } - /** - * Initialize text fields - */ - public void init() { - if(getDataSource() != null){ - modifyTextWithoutTriggeringListeners(text_databaseName, this, getDataSource().getDatabase()); - modifyTextWithoutTriggeringListeners(text_username, this, getDataSource().getUsername()); - modifyTextWithoutTriggeringListeners(text_password, this, getDataSource().getPassword()); - } - } + /** * updates the current datasource with form values */ - public void updateAuthentication(){ - + public void updateAuthentication(){ database = text_databaseName.getText(); username = text_username.getText(); password = text_password.getText(); @@ -270,7 +354,7 @@ public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage impl * * @param dataSource the dataSource to set */ - public void setDataSource(ICdmDataSource dataSource) { + public void setDataSource(ICdmDataSource dataSource) { this.dataSource = dataSource; } @@ -279,7 +363,12 @@ public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage impl * * @return the dataSource */ - public ICdmDataSource getDataSource() { + public ICdmDataSource getUpdatedDataSource() { + updateDataSource(); + return dataSource; + } + + protected ICdmDataSource getDataSource() { return dataSource; } @@ -289,23 +378,63 @@ public abstract class CdmDataSourceCredentialsWizardPage extends WizardPage impl * @return a {@link java.lang.String} object. */ public String getDataSourceName() { - if(dataSource instanceof CdmPersistentDataSource){ - return ((CdmPersistentDataSource) dataSource).getName(); - } - return null; + return name; } /* (non-Javadoc) - * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent) + * @see org.eclipse.swto.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent) */ /** {@inheritDoc} */ public void modifyText(ModifyEvent e) { + //this.setPageComplete(false); + name = text_dataSourceName.getText(); + database = text_databaseName.getText(); + + switch(mode) { + case EDIT: + if(name.length() == 0){ + name = ""; + setErrorMessage("DataSource name must not be empty."); + this.setPageComplete(false); + return; + } else if ((!dataSource.getName().equals(name)) && (CdmDataSourceRepository.getDataSource(name) != null)) { + name = ""; + setErrorMessage("DataSource with the same name already exists"); + this.setPageComplete(false); + return; + } + break; + case CLONE: + if(name.length() == 0){ + name = ""; + setErrorMessage("DataSource name must not be empty."); + this.setPageComplete(false); + } else if (CdmDataSourceRepository.getDataSource(name) != null) { + name = ""; + setErrorMessage("DataSource with the same name already exists"); + this.setPageComplete(false); + return; + } + break; + default: + break; + } + + if(database.length() == 0){ + setErrorMessage("Database name must not be empty."); + this.setPageComplete(false); + } else { + updateAndCheck(); + setErrorMessage(null); + } + } + + + private void updateAndCheck() { updateLocation(); updateAuthentication(); - updateDataSource(); checkPageComplete(); - } - + } /** *

modifyTextWithoutTriggeringListeners

* diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceH2WizardPage.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceH2WizardPage.java index 53cc3472d..5f15d0fce 100644 --- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceH2WizardPage.java +++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceH2WizardPage.java @@ -28,9 +28,8 @@ public class CdmDataSourceH2WizardPage extends CdmDataSourceCredentialsWizardPag * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object. */ protected CdmDataSourceH2WizardPage(ICdmDataSource dataSource) { - super("H2"); - setTitle("Enter credentials for embedded H2 database"); - this.setDataSource(dataSource); + super("H2", dataSource); + setTitle("Enter credentials for embedded H2 database"); } /* (non-Javadoc) @@ -56,11 +55,21 @@ public class CdmDataSourceH2WizardPage extends CdmDataSourceCredentialsWizardPag */ /** {@inheritDoc} */ @Override - public void updateDataSource() { - setDataSource(CdmDataSource.NewH2EmbeddedInstance(database, - username, - password, - nomenclaturalCode)); + public void updateDataSource() { + ICdmDataSource dataSource = getDataSource(); + + if(dataSource == null) { + setDataSource(CdmDataSource.NewH2EmbeddedInstance(database, + username, + password, + nomenclaturalCode)); + } else { + dataSource.setName(name); + dataSource.setDatabase(database); + dataSource.setUsername(username); + dataSource.setPassword(password); + dataSource.setNomenclaturalCode(nomenclaturalCode); + } } /* (non-Javadoc) diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceMySQLWizardPage.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceMySQLWizardPage.java index 264e7be35..48d8dc87b 100644 --- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceMySQLWizardPage.java +++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceMySQLWizardPage.java @@ -37,16 +37,30 @@ public class CdmDataSourceMySQLWizardPage extends CdmDataSourceCredentialsWizard private int port; + + /** *

Constructor for CdmDataSourceMySQLWizardPage.

* * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object. */ protected CdmDataSourceMySQLWizardPage(ICdmDataSource dataSource) { - super("MySQL"); + super("MySQL", dataSource, CdmDataSourceWizard.Mode.CREATE); setTitle("MySQL Server"); setDescription("Enter credentials for MySQL database"); - this.setDataSource(dataSource); + + } + + /** + *

Constructor for CdmDataSourceMySQLWizardPage.

+ * + * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object. + */ + protected CdmDataSourceMySQLWizardPage(ICdmDataSource dataSource, CdmDataSourceWizard.Mode mode) { + super("MySQL", dataSource, mode); + setTitle("MySQL Server"); + setDescription("Enter credentials for MySQL database"); + } /* (non-Javadoc) @@ -58,7 +72,7 @@ public class CdmDataSourceMySQLWizardPage extends CdmDataSourceCredentialsWizard // Create group composite for location data locationGroup = new Group(composite, SWT.NONE); locationGroup.setText("Location"); - locationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1)); + locationGroup.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 2)); GridLayout locationLayout = new GridLayout(); locationLayout.numColumns = 2; locationGroup.setLayout(locationLayout); @@ -70,7 +84,7 @@ public class CdmDataSourceMySQLWizardPage extends CdmDataSourceCredentialsWizard // Create host input text_server = new Text(locationGroup, SWT.BORDER); text_server.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); - text_server.addModifyListener(this); + // Create port label Label portLabel = new Label(locationGroup, SWT.NONE); @@ -79,7 +93,7 @@ public class CdmDataSourceMySQLWizardPage extends CdmDataSourceCredentialsWizard // Create port input text_port = new Text(locationGroup, SWT.BORDER); text_port.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); - text_port.addModifyListener(this); + } @@ -105,13 +119,25 @@ public class CdmDataSourceMySQLWizardPage extends CdmDataSourceCredentialsWizard */ /** {@inheritDoc} */ @Override - public void updateDataSource() { - setDataSource(CdmDataSource.NewMySqlInstance(server, - database, - port, - username, - password, - nomenclaturalCode)); + public void updateDataSource() { + ICdmDataSource dataSource = getDataSource(); + + if(dataSource == null) { + setDataSource(CdmDataSource.NewMySqlInstance(server, + database, + port, + username, + password, + nomenclaturalCode)); + } else { + dataSource.setName(name); + dataSource.setServer(server); + dataSource.setDatabase(database); + dataSource.setPort(port); + dataSource.setUsername(username); + dataSource.setPassword(password); + dataSource.setNomenclaturalCode(nomenclaturalCode); + } } /* (non-Javadoc) @@ -140,10 +166,25 @@ public class CdmDataSourceMySQLWizardPage extends CdmDataSourceCredentialsWizard /** {@inheritDoc} */ @Override public void init() { + super.init(); if(getDataSource() != null){ - modifyTextWithoutTriggeringListeners(text_server, this, getDataSource().getServer()); - modifyTextWithoutTriggeringListeners(text_port, this, getDataSource().getPort()+""); - super.init(); + removeListeners(); + text_server.setText(getDataSource().getServer()); + text_port.setText(String.valueOf(getDataSource().getPort())); + // add listeners after setting text to avoid the modify event being called + // for the initial value + addListeners(); } + + } + + private void addListeners() { + text_server.addModifyListener(this); + text_port.addModifyListener(this); + } + + private void removeListeners() { + text_server.removeModifyListener(this); + text_port.removeModifyListener(this); } } diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourcePostgreSQLServerWizardPage.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourcePostgreSQLServerWizardPage.java index 16f1387fb..72c98ac58 100644 --- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourcePostgreSQLServerWizardPage.java +++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourcePostgreSQLServerWizardPage.java @@ -28,10 +28,10 @@ public class CdmDataSourcePostgreSQLServerWizardPage extends private int port; protected CdmDataSourcePostgreSQLServerWizardPage(ICdmDataSource dataSource) { - super("PostgreSQL Server"); + super("PostgreSQL Server", dataSource); setTitle("PostgreSQL Server"); setDescription("Enter credentials for PostgreSQL Server database"); - this.setDataSource(dataSource); + } /* (non-Javadoc) @@ -54,7 +54,7 @@ public class CdmDataSourcePostgreSQLServerWizardPage extends // Create host input text_server = new Text(locationGroup, SWT.BORDER); text_server.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); - text_server.addModifyListener(this); + // Create port label Label portLabel = new Label(locationGroup, SWT.NONE); @@ -64,7 +64,7 @@ public class CdmDataSourcePostgreSQLServerWizardPage extends text_port = new Text(locationGroup, SWT.BORDER); text_port.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false)); - text_port.addModifyListener(this); + } /* (non-Javadoc) @@ -88,21 +88,54 @@ public class CdmDataSourcePostgreSQLServerWizardPage extends */ @Override public void updateDataSource() { - setDataSource(CdmDataSource.NewPostgreSQLInstance(server, - database, - port, - username, - password, - nomenclaturalCode)); + + + ICdmDataSource dataSource = getDataSource(); + + if(dataSource == null) { + setDataSource(CdmDataSource.NewPostgreSQLInstance(server, + database, + port, + username, + password, + nomenclaturalCode)); + } else { + dataSource.setName(name); + dataSource.setServer(server); + dataSource.setDatabase(database); + dataSource.setPort(port); + dataSource.setUsername(username); + dataSource.setPassword(password); + dataSource.setNomenclaturalCode(nomenclaturalCode); + } } + /* (non-Javadoc) + * @see eu.etaxonomy.taxeditor.datasource.wizard.CdmDataSourceCredentialsWizardPage#init() + */ + /** {@inheritDoc} */ @Override public void init() { + super.init(); if(getDataSource() != null){ - modifyTextWithoutTriggeringListeners(text_server, this, getDataSource().getServer()); - modifyTextWithoutTriggeringListeners(text_port, this, getDataSource().getPort()+""); - super.init(); + removeListeners(); + text_server.setText(getDataSource().getServer()); + text_port.setText(String.valueOf(getDataSource().getPort())); + // add listeners after setting text to avoid the modify event being called + // for the initial value + addListeners(); } + + } + + private void addListeners() { + text_server.addModifyListener(this); + text_port.addModifyListener(this); + } + + private void removeListeners() { + text_server.removeModifyListener(this); + text_port.removeModifyListener(this); } } diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceSQLServerWizardPage.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceSQLServerWizardPage.java index 6e61ced82..f65e7e93b 100644 --- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceSQLServerWizardPage.java +++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceSQLServerWizardPage.java @@ -43,7 +43,7 @@ public class CdmDataSourceSQLServerWizardPage extends CdmDataSourceCredentialsWi * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object. */ public CdmDataSourceSQLServerWizardPage(ICdmDataSource dataSource) { - super("SQL Server"); + super("SQL Server", dataSource); setTitle("SQL Server"); setDescription("Enter credentials for SQL Server database"); this.setDataSource(dataSource); @@ -106,17 +106,25 @@ public class CdmDataSourceSQLServerWizardPage extends CdmDataSourceCredentialsWi /** {@inheritDoc} */ @Override public void updateDataSource() { -// setDataSource(CdmDataSource.NewSqlServer2005Instance(server, -// database, -// port, -// username, -// password, -// nomenclaturalCode)); - setDataSource(CdmDataSource.NewSqlServer2005Instance(server, - database, - port, - username, - password)); + + + + ICdmDataSource dataSource = getDataSource(); + + if(dataSource == null) { + setDataSource(CdmDataSource.NewSqlServer2005Instance(server, + database, + port, + username, + password)); + } else { + dataSource.setName(name); + dataSource.setServer(server); + dataSource.setDatabase(database); + dataSource.setPort(port); + dataSource.setUsername(username); + dataSource.setPassword(password); + } } /* (non-Javadoc) @@ -145,11 +153,26 @@ public class CdmDataSourceSQLServerWizardPage extends CdmDataSourceCredentialsWi /** {@inheritDoc} */ @Override public void init() { + super.init(); if(getDataSource() != null){ - modifyTextWithoutTriggeringListeners(text_server, this, getDataSource().getServer()); - modifyTextWithoutTriggeringListeners(text_port, this, getDataSource().getPort()+""); - super.init(); + removeListeners(); + text_server.setText(getDataSource().getServer()); + text_port.setText(String.valueOf(getDataSource().getPort())); + // add listeners after setting text to avoid the modify event being called + // for the initial value + addListeners(); } + + } + + private void addListeners() { + text_server.addModifyListener(this); + text_port.addModifyListener(this); + } + + private void removeListeners() { + text_server.removeModifyListener(this); + text_port.removeModifyListener(this); } diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceWizard.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceWizard.java index 1dbc8058f..d56b25b51 100644 --- a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceWizard.java +++ b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/datasource/wizard/CdmDataSourceWizard.java @@ -33,31 +33,42 @@ public class CdmDataSourceWizard extends Wizard { private String dataSourceName; - private boolean editMode; + //private boolean editMode; + + public enum Mode { + CREATE, + EDIT, + CLONE + } + + // default mode is to create a new datasource + Mode mode = Mode.CREATE; /** *

Constructor for CdmDataSourceWizard.

*/ public CdmDataSourceWizard() { super(); - editMode = false; + this.mode = Mode.CREATE; setForcePreviousAndNextButtons(true); setWindowTitle("Datasource Dialog"); + } + /** *

Constructor for CdmDataSourceWizard.

* * @param dataSource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object. */ - public CdmDataSourceWizard(ICdmDataSource dataSource) { + public CdmDataSourceWizard(ICdmDataSource dataSource, Mode mode) { super(); if(dataSource != null){ - editMode = true; + this.mode = mode; this.dataSource = dataSource; dataSourceName = dataSource.getName(); } - setForcePreviousAndNextButtons(true); + //setForcePreviousAndNextButtons(true); setWindowTitle("Datasource Dialog"); } @@ -68,23 +79,29 @@ public class CdmDataSourceWizard extends Wizard { /** {@inheritDoc} */ @Override public void addPages() { - - if(editMode){ + switch(mode) { + case EDIT: + case CLONE: if(dataSource.getDatabaseType() == DatabaseTypeEnum.H2){ dataSourcePage = new CdmDataSourceH2WizardPage(dataSource); }else if(dataSource.getDatabaseType() == DatabaseTypeEnum.MySQL){ - dataSourcePage = new CdmDataSourceMySQLWizardPage(dataSource); + dataSourcePage = new CdmDataSourceMySQLWizardPage(dataSource, mode); }else if(dataSource.getDatabaseType() == DatabaseTypeEnum.SqlServer2005){ dataSourcePage = new CdmDataSourceSQLServerWizardPage(dataSource); + }else if(dataSource.getDatabaseType() == DatabaseTypeEnum.PostgreSQL){ + dataSourcePage = new CdmDataSourcePostgreSQLServerWizardPage(dataSource); }else{ throw new RuntimeException("Editing a datasource of type '" + dataSource.getDatabaseType() + "' is not supported yet."); } this.addPage(dataSourcePage); - }else{ + case CREATE: dataSourceSelectionPage = new CdmDataSourceTypeSelectionWizardPage(dataSource); this.addPage(dataSourceSelectionPage); + default: + } + } /* (non-Javadoc) @@ -93,21 +110,32 @@ public class CdmDataSourceWizard extends Wizard { /** {@inheritDoc} */ @Override public boolean performFinish() { - - if(editMode){ - ICdmDataSource dataSource = dataSourcePage.getDataSource(); - CdmDataSourceRepository.update(dataSourceName, dataSource); - + 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; - }else{ + case CREATE: if(dataSourceSelectionPage.getCredentialsWizardPage() != null){ - CdmDataSourceCredentialsWizardPage credentialsWizardPage = dataSourceSelectionPage.getCredentialsWizardPage(); - CdmDataSourceRepository.save(dataSourceSelectionPage.getDataSourceName(), credentialsWizardPage.getDataSource()); + CdmDataSourceCredentialsWizardPage credentialsWizardPage = dataSourceSelectionPage.getCredentialsWizardPage(); + CdmDataSourceRepository.save(dataSourceSelectionPage.getDataSourceName(), credentialsWizardPage.getUpdatedDataSource()); return true; - }else{ + } else { throw new IllegalStateException("Expected a datasource credentials page to exist"); } + default: + return false; } + } /* (non-Javadoc) @@ -116,15 +144,20 @@ public class CdmDataSourceWizard extends Wizard { /** {@inheritDoc} */ @Override public boolean canFinish() { - if(editMode){ + switch(mode) { + case EDIT: + case CLONE: return dataSourcePage.isPageComplete(); - }else{ + case CREATE: boolean result = true; result &= dataSourceSelectionPage.isPageComplete(); if(dataSourceSelectionPage.getCredentialsWizardPage() != null){ result &= dataSourceSelectionPage.getCredentialsWizardPage().isPageComplete(); } - return result; + return result; + default: + return false; } + } } -- 2.34.1