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 <code>TAG_DATASOURCE="tagDataSource"</code> */
public static final String TAG_DATASOURCE = "tagDataSource";
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;
* @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;
}
*
* @return a {@link java.util.List} object.
*/
- public static List<ICdmDataSource> getAll() {
- List<ICdmDataSource> dataSources = new ArrayList<ICdmDataSource>();
+ public static List<ICdmSource> getAll() {
+ List<ICdmSource> remoteSources = new ArrayList<ICdmSource>();
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;
}
/**
return null;
}
+
/**
* <p>Getter for the field <code>currentDataSource</code>.</p>
*
* @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;
}
/**
* @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)){
* @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;
}
* @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);
+ }
+
+ /**
+ * <p>save</p>
+ *
+ * @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);
+ }
+
+ /**
+ * <p>update</p>
+ *
+ * @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;
}
/**
* @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;
}
* 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;
private static IMemento readMemento(){
try {
- return MementoHelper.readMementoFromFile(getDataSourceStateFile());
+ return MementoHelper.readMementoFromFile(getCdmSourceStateFile());
} catch (FileNotFoundException e) {
return initializeMemento();
}
* Save the workbench UI in a persistence file.
*/
private static IMemento saveMementoToFile(IMemento memento) {
- return MementoHelper.saveMementoToFile(memento, getDataSourceStateFile());
+ return MementoHelper.saveMementoToFile(memento, getCdmSourceStateFile());
}
}
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;
* @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;
protected Composite parent;
+ protected String name;
protected String database;
protected String username;
protected String password;
protected NomenclaturalCode nomenclaturalCode;
+ CdmDataSourceWizard.Mode mode;
+
+ /**
+ * <p>Constructor for CdmDataSourceCredentialsWizardPage.</p>
+ *
+ * @param pageName a {@link java.lang.String} object.
+ */
+ protected CdmDataSourceCredentialsWizardPage(String pageName, ICdmDataSource dataSource) {
+ super(pageName);
+ this.setPageComplete(false);
+ setDataSource(dataSource);
+ }
+
/**
* <p>Constructor for CdmDataSourceCredentialsWizardPage.</p>
*
* @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)
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();
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
});
setControl(composite);
-
+
init();
+
+ if(mode == CdmDataSourceWizard.Mode.CLONE) {
+ getDataSource().setName("");
+ getDataSource().setDatabase("");
+ text_dataSourceName.setText("");
+ text_databaseName.setText("");
+ }
}
/**
// 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);
// 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);
// 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);
// 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
*/
}
}
-
+ 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);
}
}
* <p>checkPageComplete</p>
*/
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();
*
* @param dataSource the dataSource to set
*/
- public void setDataSource(ICdmDataSource dataSource) {
+ public void setDataSource(ICdmDataSource dataSource) {
this.dataSource = dataSource;
}
*
* @return the dataSource
*/
- public ICdmDataSource getDataSource() {
+ public ICdmDataSource getUpdatedDataSource() {
+ updateDataSource();
+ return dataSource;
+ }
+
+ protected ICdmDataSource getDataSource() {
return dataSource;
}
* @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();
- }
-
+ }
/**
* <p>modifyTextWithoutTriggeringListeners</p>
*
* @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)
*/
/** {@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)
private int port;
+
+
/**
* <p>Constructor for CdmDataSourceMySQLWizardPage.</p>
*
* @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);
+
+ }
+
+ /**
+ * <p>Constructor for CdmDataSourceMySQLWizardPage.</p>
+ *
+ * @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)
// 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);
// 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);
// 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);
+
}
*/
/** {@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)
/** {@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);
}
}
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)
// 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);
text_port = new Text(locationGroup, SWT.BORDER);
text_port.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
- text_port.addModifyListener(this);
+
}
/* (non-Javadoc)
*/
@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);
}
}
* @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);
/** {@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)
/** {@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);
}
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;
/**
* <p>Constructor for CdmDataSourceWizard.</p>
*/
public CdmDataSourceWizard() {
super();
- editMode = false;
+ 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) {
+ 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");
}
/** {@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)
/** {@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)
/** {@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;
}
+
}
}