ref #9189: move datasource to webapp
[taxeditor.git] / eu.etaxonomy.taxeditor.webapp / src / main / java / eu / etaxonomy / taxeditor / webapp / view / datasource / handler / DataSourceMenuPropertyTester.java
diff --git a/eu.etaxonomy.taxeditor.webapp/src/main/java/eu/etaxonomy/taxeditor/webapp/view/datasource/handler/DataSourceMenuPropertyTester.java b/eu.etaxonomy.taxeditor.webapp/src/main/java/eu/etaxonomy/taxeditor/webapp/view/datasource/handler/DataSourceMenuPropertyTester.java
new file mode 100644 (file)
index 0000000..436d9c6
--- /dev/null
@@ -0,0 +1,88 @@
+/**
+ * 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.view.datasource.handler;
+
+import org.eclipse.core.expressions.PropertyTester;
+import org.eclipse.jface.viewers.IStructuredSelection;
+
+import eu.etaxonomy.cdm.model.metadata.CdmMetaDataPropertyName;
+import eu.etaxonomy.taxeditor.webapp.view.datasource.CdmMetaDataAwareDataSourceContainer;
+
+/**
+ * @author n.hoffmann
+ * @created Sep 23, 2010
+ * @version 1.0
+ */
+public class DataSourceMenuPropertyTester extends PropertyTester {
+
+       private static final String COMPATIBLE = "isCompatible";
+       private static final String TEST = "test";
+       private static final String HAS_DATA_MODEL = "hasDataModel";
+
+       /** {@inheritDoc} */
+       @Override
+       public boolean test(Object receiver, String property, Object[] args,
+                       Object expectedValue) {
+
+               Object[] selectedElements = ((IStructuredSelection) receiver).toArray();
+               if (selectedElements.length == 0 && property.equals(TEST)) {
+                       // nothing selected so all tests should fail
+                       return true;
+               }
+               if (selectedElements.length == 0 && COMPATIBLE.equals(property)) {
+                       // nothing selected so all tests should fail
+                       return true;
+               }
+
+               if (COMPATIBLE.equals(property)) {
+                       return isCompatible(selectedElements);
+               }
+               if (HAS_DATA_MODEL.equals(property)) {
+                   return hasDataModel(selectedElements);
+               }
+               return false;
+       }
+
+          private boolean hasDataModel(Object[] selectedElements) {
+
+               if (selectedElements.length == 0 ) {
+                   // nothing selected so all tests should fail
+                   return false;
+               }
+               for (Object object : selectedElements) {
+
+                   if (object instanceof CdmMetaDataAwareDataSourceContainer) {
+                       CdmMetaDataAwareDataSourceContainer container = (CdmMetaDataAwareDataSourceContainer) object;
+                       String schemaVersion = container.getMetaData(CdmMetaDataPropertyName.DB_SCHEMA_VERSION);
+                    return !schemaVersion.equals(CdmMetaDataAwareDataSourceContainer.DEFAULT_ENTRY);
+                   }
+               }
+               return false;
+
+           }
+
+
+       private boolean isCompatible(Object[] selectedElements) {
+
+               if (selectedElements.length == 0 ) {
+                       // nothing selected so all tests should fail
+                       return false;
+               }
+               for (Object object : selectedElements) {
+
+                       if (object instanceof CdmMetaDataAwareDataSourceContainer) {
+                               CdmMetaDataAwareDataSourceContainer container = (CdmMetaDataAwareDataSourceContainer) object;
+                               return container.isDataSourceCompatible();
+                       }
+               }
+               return false;
+
+       }
+}