/*  
 *   Copyright 2012 OSBI Ltd
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package org.saiku.datasources.connection;

import org.olap4j.OlapConnection;
import org.olap4j.OlapWrapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

import javax.servlet.ServletContext;

import mondrian.rolap.RolapConnection;

import static org.saiku.datasources.connection.encrypt.CryptoUtil.decrypt;

public class SaikuOlapConnection implements ISaikuConnection {

  @Autowired
  ServletContext servletContext;


  private String name;
  private boolean initialized = false;
  private Properties properties;
  private OlapConnection olapConnection;
  private String username;
  private String password;
  private String passwordenc;

  public ServletContext getServletContext() {
    return servletContext;
  }

  public void setServletContext(ServletContext servletContext) {
    this.servletContext = servletContext;
  }

    public SaikuOlapConnection( String name, Properties props ) {
    this.name = name;
    this.properties = props;
  }

  public SaikuOlapConnection( Properties props ) {
    this.properties = props;
    this.name = props.getProperty( ISaikuConnection.NAME_KEY );
  }

  public boolean connect() throws Exception {
    return connect( properties );
  }

  private String decryptPassword(String password)
    {

        if ( password != null )
        {
            return decrypt(password);
        }
        return null;
  }

  public boolean connect( Properties props ) throws Exception {
        String safemode = null;
            try {
              safemode = servletContext.getInitParameter("safemode");

            }
            catch(Exception e){
             //Safemode doesn't exist, not a problem.
            }
        if(safemode!= null && safemode.equals("true")) {
          return false;
        }
    else {
          this.username = props.getProperty(ISaikuConnection.USERNAME_KEY);
          this.password = props.getProperty(ISaikuConnection.PASSWORD_KEY);
          String driver = props.getProperty(ISaikuConnection.DRIVER_KEY);
          this.passwordenc = props.getProperty(ISaikuConnection.PASSWORD_ENCRYPT_KEY);
          this.properties = props;
          String url = props.getProperty(ISaikuConnection.URL_KEY);
          System.out.println("name:" + name);
          System.out.println("driver:" + driver);
          System.out.println("url:" + url);
          System.out.flush();


          if (this.passwordenc != null && this.passwordenc.equals("true")) {
            this.password = decryptPassword(password);
          }
          if (url.contains("Mondrian=4")) {
            url = url.replace("Mondrian=4; ", "");
            url = url.replace("jdbc:mondrian", "jdbc:mondrian4");
          }
          if (url.length() > 0 && url.charAt(url.length() - 1) != ';') {
            url += ";";
          }
          if (driver.equals("mondrian.olap4j.MondrianOlap4jDriver")) {
            if (username != null && username.length() > 0) {
              url += "JdbcUser=" + username + ";";
            }
            if (password != null && password.length() > 0) {
              url += "JdbcPassword=" + password + ";";
            }
          }

          Class.forName(driver);
          Connection connection = DriverManager.getConnection(url, username, password);

          if (connection != null) {
            final OlapWrapper wrapper = (OlapWrapper) connection;
            OlapConnection tmpolapConnection = wrapper.unwrap(OlapConnection.class);

            if (tmpolapConnection == null) {
              throw new Exception("Connection is null");
            }

            System.out.println("Catalogs:" + tmpolapConnection.getOlapCatalogs().size());
            olapConnection = tmpolapConnection;
            initialized = true;
            return true;
          }

          return false;
        }
  }

  public boolean clearCache() throws Exception {
    if ( olapConnection.isWrapperFor( RolapConnection.class ) ) {
      System.out.println( "Clearing cache" );
      RolapConnection rcon = olapConnection.unwrap( RolapConnection.class );
      rcon.getCacheControl( null ).flushSchemaCache();

    }
    return true;
  }


  public String getDatasourceType() {
    return ISaikuConnection.OLAP_DATASOURCE;
  }

  public boolean initialized() {
    return initialized;
  }

  public Connection getConnection() {
    return olapConnection;
  }

  public void setProperties( Properties props ) {
    properties = props;
  }

  public String getName() {
    return name;
  }

  public Properties getProperties() {
    return properties;
  }

}

@@ -731,57 +731,51 @@
 rg.s
-pringframework.beans.factory.annotation.Autowired
+lf4j.Logger;%0Aimport org.slf4j.LoggerFactory
 ;%0A%0Ai
@@ -866,46 +866,8 @@
 s;%0A%0A
-import javax.servlet.ServletContext;%0A%0A
 impo
@@ -1046,54 +1046,8 @@
  %7B%0A%0A
-  @Autowired%0A  ServletContext servletContext;%0A
 %0A%0A  
@@ -1272,193 +1272,93 @@
 %0A  p
-ublic ServletContext getServletContext() %7B%0A    return servletContext;%0A  %7D%0A%0A  public void setServletContext(ServletContext servletContext) %7B%0A    this.servletContext = servletContext;%0A  %7D
+rivate static final Logger log = LoggerFactory.getLogger(SaikuOlapConnection.class);%0A
 %0A%0A  
@@ -1952,24 +1952,26 @@
 n %7B%0A        
+  
 String safem
@@ -2029,41 +2029,34 @@
 e = 
-servletContext.getInitParameter(%22
+System.getProperty(%22saiku.
 safe
@@ -2063,17 +2063,16 @@
 mode%22);%0A
-%0A
         
@@ -2226,32 +2226,109 @@
 uals(%22true%22)) %7B%0A
+          log.debug(%22Not starting connection %22+name+%22, Saiku in safe mode%22);%0A
           return
@@ -2776,178 +2776,8 @@
 Y);%0A
-          System.out.println(%22name:%22 + name);%0A          System.out.println(%22driver:%22 + driver);%0A          System.out.println(%22url:%22 + url);%0A          System.out.flush();%0A
 %0A%0A  

fix safemode