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.model.MessagingUtils;
-import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
+import eu.etaxonomy.taxeditor.remoting.source.CdmPersistentRemoteSource;
+import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSourceException;
+import eu.etaxonomy.taxeditor.remoting.source.ICdmRemoteSource;
import eu.etaxonomy.taxeditor.store.CdmStore;
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
public class CdmDataSourceRepository{
private static final String CURRENT_DATASOURCE = "currentDataSource";
private static final String CURRENT_DATASOURCE_POSTFIX = "currentDataSourcePostFix";
private static final String DEFAULT_DATASOURCE_STATE_FILENAME = "datasource.xml";
-
+
private static final String DEFAULT_DATASOURCE_NAME = "cdm";
private static final String DEFAULT_DATASOURCE_POSTFIX = CdmPersistentDataSource.DATASOURCE_BEAN_POSTFIX;
-
+
private static ICdmSource currentCdmSource;
private static IMemento memento;
-
+
private static String lastUsedCdmSourceName;
private static String lastUsedCdmSourcePostFix;
-
-
-
+
+
+
/**
* <p>Getter for the field <code>lastUsedDataSourceName</code>.</p>
*
*/
public static void updateLastUsedDataSource(){
if(lastUsedCdmSourceName == null){
- memento = readMemento();
+ memento = readMemento();
lastUsedCdmSourceName = memento != null ? memento.getString(CURRENT_DATASOURCE) : DEFAULT_DATASOURCE_NAME;
lastUsedCdmSourcePostFix = memento != null ? memento.getString(CURRENT_DATASOURCE_POSTFIX) : DEFAULT_DATASOURCE_POSTFIX;
-
+
}
-
+
}
/**
return true;
}
-
+
/**
* <p>getAll</p>
*
* @return a {@link java.util.List} object.
*/
public static List<ICdmSource> getAll() {
- List<ICdmSource> remoteSources = new ArrayList<ICdmSource>();
-
+ List<ICdmSource> remoteSources = new ArrayList<>();
+
+ if (CdmPersistentDataSource.getAllDataSources() == null){
+ MessagingUtils.errorDialog("Could not read root element", CdmPersistentDataSource.class,
+ "Could not read element in datasources.xml in .cdmLibrary folder. Maybe your datasources.xml file is broken.",
+ TaxeditorStorePlugin.PLUGIN_ID, null, true);
+ return remoteSources;
+ }
for(ICdmDataSource dataSource : CdmPersistentDataSource.getAllDataSources()){
try {
remoteSources.add(CdmPersistentDataSource.NewInstance(dataSource.getName()));
MessagingUtils.error(CdmDataSourceRepository.class, "Could not find dataSource", e);
}
}
-
+
try {
- for(ICdmRemoteSource remoteSource : CdmPersistentRemoteSource.getAllRemoteSources()){
+ for(ICdmRemoteSource remoteSource : CdmPersistentRemoteSource.getAllRemoteSources()){
remoteSources.add(remoteSource);
}
} catch (CdmRemoteSourceException e) {
MessagingUtils.error(CdmDataSourceRepository.class, "Error retrieving remote sources", e);
}
-
+
// TODO sort by database name
-
+
return remoteSources;
}
-
+
/**
* <p>getDataSource</p>
*
* @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
*/
public static ICdmDataSource getDataSource(String name){
-
+
for(ICdmDataSource dataSource : CdmPersistentDataSource.getAllDataSources()){
try {
if(dataSource.getName().equals(name)){
MessagingUtils.error(CdmDataSourceRepository.class, "Could not find dataSource", e);
}
}
-
+
return null;
}
-
-
+
+
/**
* <p>Getter for the field <code>currentDataSource</code>.</p>
*
* @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
- * @throws CdmRemoteSourceException
+ * @throws CdmRemoteSourceException
*/
public static ICdmSource getCurrentCdmSource() throws CdmRemoteSourceException {
-
+
if (currentCdmSource == null) {
updateLastUsedDataSource();
-
+
if(lastUsedCdmSourceName == null) {
return null;
}
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);
+ setCurrentCdmSource(createDefaultH2DataSource());
}
- } else if (lastUsedCdmSourcePostFix.equals(CdmPersistentRemoteSource.REMOTESOURCE_BEAN_POSTFIX)) {
+ } else if (lastUsedCdmSourcePostFix.equals(CdmPersistentRemoteSource.REMOTESOURCE_BEAN_POSTFIX)) {
currentCdmSource = CdmPersistentRemoteSource.NewInstance(lastUsedCdmSourceName);
} else {
throw new CdmRemoteSourceException("Unkown Cdm Source Type");
}
return currentCdmSource;
}
-
+
+ /**
+ * Creates a default H2 CDM Data Source
+ *
+ * @return the newly created data source
+ */
+ public static ICdmDataSource createDefaultH2DataSource() {
+ ICdmDataSource h2DataSource = CdmDataSource.NewH2EmbeddedInstance(
+ DEFAULT_DATASOURCE_NAME, "sa", "");
+ save(h2DataSource.getName(), h2DataSource);
+ return h2DataSource;
+ }
+
/**
* <p>Setter for the field <code>currentDataSource</code>.</p>
*
*/
public static boolean setCurrentCdmSource(ICdmSource cdmSource) {
currentCdmSource = cdmSource;
- NomenclaturalCode dataSourceNomenclaturalCode = cdmSource.getNomenclaturalCode();
- NomenclaturalCode applicationNomenclaturalCode = PreferencesUtil.getPreferredNomenclaturalCode();
-
- if( dataSourceNomenclaturalCode != null && ! dataSourceNomenclaturalCode.equals(applicationNomenclaturalCode)){
- PreferencesUtil.setPreferredNomenclaturalCode(dataSourceNomenclaturalCode);
- MessagingUtils.informationDialog("Nomenclatural Code Change", "The Datasource that was just " +
- "loaded has a different nomenclatural code than the one stored in Preferences. " +
- "The nomenclatural code was changed in the application.");
- }
+// NomenclaturalCode dataSourceNomenclaturalCode = cdmSource.getNomenclaturalCode();
+// NomenclaturalCode applicationNomenclaturalCode = PreferencesUtil.getPreferredNomenclaturalCode(true);
+//
+// if( dataSourceNomenclaturalCode != null && ! dataSourceNomenclaturalCode.equals(applicationNomenclaturalCode)){
+// PreferencesUtil.setPreferredNomenclaturalCode(dataSourceNomenclaturalCode, true);
+// MessagingUtils.informationDialog("Nomenclatural Code Change", "The Datasource that was just " +
+// "loaded has a different nomenclatural code than the one stored in Preferences. " +
+// "The nomenclatural code was changed in the application.");
+// }
return true;
- }
-
+ }
+
/**
* <p>changeDataSource</p>
*
* @return a boolean.
*/
public static boolean changeDataSource(final ICdmSource cdmSource) {
- saveAsCurrentDatabaseToMemento(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
+ * @throws CdmRemoteSourceException
*/
public static ICdmPersistentSource save(String strCdmSourceName, ICdmRemoteSource cdmSource) throws CdmRemoteSourceException {
- return CdmPersistentRemoteSource.save(strCdmSourceName, cdmSource);
+ 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
+ * @throws CdmRemoteSourceException
*/
- public static ICdmPersistentSource save(String strCdmSourceName, ICdmDataSource cdmSource) {
- return CdmPersistentDataSource.save(strCdmSourceName, cdmSource);
+ public static ICdmPersistentSource save(String strCdmSourceName, ICdmDataSource cdmSource) {
+ return CdmPersistentDataSource.save(strCdmSourceName, cdmSource);
}
-
+
/**
* <p>update</p>
*
}
return null;
}
-
+
public static ICdmPersistentSource replace(String strCdmSourceName, ICdmDataSource cdmSource){
try {
return CdmPersistentDataSource.replace(strCdmSourceName, cdmSource);
}
return null;
}
-
+
/**
* <p>update</p>
*
*/
public static ICdmPersistentSource update(String strCdmSourceName, ICdmRemoteSource cdmSource){
try {
- return CdmPersistentRemoteSource.update(strCdmSourceName, cdmSource);
+ return CdmPersistentRemoteSource.update(strCdmSourceName, cdmSource);
} catch (Exception e) {
MessagingUtils.error(CdmDataSourceRepository.class, "Error updating CDM Source", e);
-
+
}
return null;
}
-
+
/*********************************************************
* Memento Handling *
*********************************************************/
-
+
private static void saveAsCurrentDatabaseToMemento(ICdmSource cdmSource){
if(memento == null) {
memento = readMemento();
}
memento.putString(CURRENT_DATASOURCE, cdmSource.getName());
-
+
// Set postfix to distinguish between data and remote sources
if(cdmSource instanceof CdmPersistentRemoteSource) {
memento.putString(CURRENT_DATASOURCE_POSTFIX, CdmPersistentRemoteSource.REMOTESOURCE_BEAN_POSTFIX);
} else {
memento.putString(CURRENT_DATASOURCE_POSTFIX, CdmPersistentDataSource.DATASOURCE_BEAN_POSTFIX);
}
-
-
+
+
saveMementoToFile(memento);
}
-
+
/*
* Answer the workbench state file.
*/
path = path.append(DEFAULT_DATASOURCE_STATE_FILENAME);
return path.toFile();
}
-
+
private static IMemento readMemento(){
try {
return MementoHelper.readMementoFromFile(getCdmSourceStateFile());
return initializeMemento();
}
}
-
+
private static IMemento initializeMemento() {
-
+
XMLMemento memento = XMLMemento.createWriteRoot(TAG_DATASOURCE);
memento.putString(CURRENT_DATASOURCE, DEFAULT_DATASOURCE_NAME);
saveMementoToFile(memento);
-
+
return readMemento();
}
*/
private static IMemento saveMementoToFile(IMemento memento) {
return MementoHelper.saveMementoToFile(memento, getCdmSourceStateFile());
- }
+ }
}