fix #9025 allow for creating a new schema in empty dbs via the remote login dialog
[taxeditor.git] / eu.etaxonomy.taxeditor.webapp / src / main / java / eu / etaxonomy / taxeditor / webapp / CDMServer.java
index 9161fb181661d6ccdd384c1a5850177009ae2b50..5146d0a9b83732d9bad710930beec63c121ab0ca 100644 (file)
@@ -61,6 +61,9 @@ public class CDMServer {
     private File warFile;
     private Server server;
 
+    private boolean potentiallyMissingSchema;
+    private boolean forceSchemaCreate = false;
+
 
     public CDMServer(String dataSourceName, File dataSourcesFile) throws CDMEmbeddedServerException {
         if(StringUtil.isBlank(dataSourceName)) {
@@ -90,6 +93,7 @@ public class CDMServer {
             throw new CDMEmbeddedServerException(ioe);
         }
 
+        //TODO instead of strings, use the constants defined in eu.etaxonomy.cdm.opt.config.DataSourceConfigurer
         System.setProperty("spring.profiles.active", "remoting");
         System.setProperty("cdm.beanDefinitionFile", dataSourcesFile.getAbsolutePath());
         System.setProperty("cdm.datasource", dataSourceName);
@@ -177,9 +181,15 @@ public class CDMServer {
 
         Thread serverThread = new Thread() {
 
+
             @Override
             public void run() {
                 try {
+                    if(isForceSchemaCreate()) {
+                        System.setProperty("cdm.forceSchemaCreate", "true");
+                        potentiallyMissingSchema = false; // reset flag
+                        setForceSchemaCreate(false); // reset flag
+                    }
                     server.start();
                     server.join();
                 } catch (Throwable t) {
@@ -189,7 +199,21 @@ public class CDMServer {
                         Thread.sleep(1000);
                     } catch (InterruptedException e) {
                     }
+                    Throwable cause = t;
+                    while(cause != null) {
+
+                        // pure string comparison to avoid dependencies to hibernate or cdmlib-persistance in the taxeditor
+                        if(cause.getClass().getSimpleName().equals("SchemaExtractionException") || cause.getClass().getSimpleName().equals("CdmDatabaseException") ) {
+                            potentiallyMissingSchema = true;
+                            logger.debug(cause.getClass().getName() + " detected which indicates missing or corrupt schema");
+                            cause = null;
+                        } else {
+                            cause = cause.getCause();
+                        }
+                    }
                     cdmServerError.handleError(new RuntimeException("Error during CDM server startup", t));
+                } finally {
+                    //System.getProperties().remove("cdm.forceSchemaCreate");
                 }
             }
         };
@@ -247,5 +271,17 @@ public class CDMServer {
         }
     }
 
+    public boolean isForceSchemaCreate() {
+        return forceSchemaCreate;
+    }
+
+    public void setForceSchemaCreate(boolean forceSchemaCreate) {
+        this.forceSchemaCreate = forceSchemaCreate;
+    }
+
+    public boolean isPotentiallyMissingSchema() {
+        return potentiallyMissingSchema;
+    }
+
 
 }