package com.codenvy.ide.ext.datasource.server;

import java.text.MessageFormat;

import com.codenvy.ide.ext.datasource.shared.DatabaseConfigurationDTO;
import com.codenvy.ide.ext.datasource.shared.exception.DatabaseDefinitionException;

public class JdbcUrlBuilder {

    private static final String URL_TEMPLATE_POSTGRES = "jdbc:postgresql://{0}:{1}/{2}";
    private static final String URL_TEMPLATE_MYSQL = "jdbc:mysql://{0}:{1}/{2}";
    private static final String URL_TEMPLATE_ORACLE = "jdbc:oracle:thin:@{0}:{1}:{2}";
    private static final String URL_TEMPLATE_JTDS ="jdbc:jtds:sqlserver://{0}:{1}/{2}";

    public String getJdbcUrl(final DatabaseConfigurationDTO configuration) throws DatabaseDefinitionException {
        // Should we check and sanitize input values ?
        if (configuration.getDatabaseType() == null) {
            throw new DatabaseDefinitionException("Database type is null in " + configuration.toString());
        }
        switch (configuration.getDatabaseType()) {
            case POSTGRES:
                return getPostgresJdbcUrl(configuration);
            case MYSQL:
                return getMySQLJdbcUrl(configuration);
            case ORACLE:
                return getOracleJdbcUrl(configuration);
            case JTDS:
                return getJTDSJdbcUrl(configuration);
            default:
                throw new DatabaseDefinitionException("Unknown database type "
                                                      + configuration.getDatabaseType()
                                                      + " in "
                                                      + configuration.toString());
        }
    }

    private String getPostgresJdbcUrl(final DatabaseConfigurationDTO configuration) {
        String url = MessageFormat.format(URL_TEMPLATE_POSTGRES,
                                          configuration.getHostname(),
                                          Integer.toString(configuration.getPort()),
                                          configuration.getDatabaseName());
        return url;
    }

    private String getMySQLJdbcUrl(final DatabaseConfigurationDTO configuration) {
        String url = MessageFormat.format(URL_TEMPLATE_MYSQL,
                                          configuration.getHostname(),
                                          Integer.toString(configuration.getPort()),
                                          configuration.getDatabaseName());
        return url;
    }

    private String getOracleJdbcUrl(final DatabaseConfigurationDTO configuration) {
        String url = MessageFormat.format(URL_TEMPLATE_ORACLE,
                configuration.getHostname(),
                Integer.toString(configuration.getPort()),
                configuration.getDatabaseName());
        return url;
    }

    private String getJTDSJdbcUrl(final DatabaseConfigurationDTO configuration) {
        String url = MessageFormat.format(URL_TEMPLATE_JTDS,
                configuration.getHostname(),
                Integer.toString(configuration.getPort()),
                configuration.getDatabaseName());
        return url;
    }
}

@@ -401,16 +401,19 @@
 E_MYSQL 
+   
 = %22jdbc:
@@ -486,16 +486,18 @@
 _ORACLE 
+  
 = %22jdbc:
@@ -573,17 +573,22 @@
 TE_JTDS 
-=
+    = 
 %22jdbc:jt
@@ -615,16 +615,104 @@
 1%7D/%7B2%7D%22;
+%0A    private static final String URL_TEMPLATE_NUODB    = %22jdbc:com.nuodb://%7B0%7D:%7B1%7D/%7B2%7D%22;
 %0A%0A    pu
@@ -1409,32 +1409,111 @@
 configuration);%0A
+            case NUODB:%0A                return getNuoDBJdbcUrl(configuration);%0A
             defa
@@ -2808,16 +2808,42 @@
 ORACLE,%0A
+                          
         
@@ -2871,32 +2871,58 @@
 .getHostname(),%0A
+                          
                 
@@ -2956,32 +2956,58 @@
 ion.getPort()),%0A
+                          
                 
@@ -3210,16 +3210,42 @@
 E_JTDS,%0A
+                          
         
@@ -3273,32 +3273,58 @@
 .getHostname(),%0A
+                          
                 
@@ -3358,32 +3358,462 @@
 ion.getPort()),%0A
+                                          configuration.getDatabaseName());%0A        return url;%0A    %7D%0A%0A    private String getNuoDBJdbcUrl(final DatabaseConfigurationDTO configuration) %7B%0A        String url = MessageFormat.format(URL_TEMPLATE_NUODB,%0A                                          configuration.getHostname(),%0A                                          Integer.toString(configuration.getPort()),%0A                          
                 

Support for nuoDB JBDC uri - #45