/*
 * Copyright 2017 Axway Software
 * 
 * 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 com.axway.ats.core.dbaccess;

import java.lang.reflect.Array;
import java.sql.SQLException;

/**
 * Class representing a value in a recordset
 */
public class DbRecordValue {

    private DbColumn dbColumn;
    private Object   value;

    /**
     * @param tableName     name of the DB table
     * @param columnName    name of the column in that table
     * @param value         the value returned by the query
     */
    public DbRecordValue( String tableName,
                          String columnName,
                          Object value ) {

        this.dbColumn = new DbColumn( tableName, columnName );
        this.value = value;
    }

    /**
     * @param dbColumn      the db column that contains the value
     * @param value         the value returned by the query
     */
    public DbRecordValue( DbColumn dbColumn,
                          Object value ) {

        this.dbColumn = dbColumn;
        this.value = value;
    }

    public Object getValue() {

        return value;
    }

    public void setValue(
                          String value ) {

        this.value = value;
    }

    public String getValueAsString() {

        // handle null value
        if( value == null ) {
            return null;
        }

        // handle array
        if( value.getClass().isArray() ) {
            StringBuilder valueAsString = new StringBuilder();

            valueAsString.append( "[ " );
            for( int i = 0; i < Array.getLength( value ); i++ ) {
                valueAsString.append( Array.get( value, i ) );
                valueAsString.append( ", " );
            }

            if( valueAsString.toString().endsWith( ", " ) ) {
                valueAsString.delete( valueAsString.length() - 2, valueAsString.length() );
            }

            valueAsString.append( " ]" );

            return valueAsString.toString();
        }

        // handle oracle time stamp
        if( value instanceof oracle.sql.TIMESTAMP ) {
            try {
                return ( ( oracle.sql.TIMESTAMP ) value ).toJdbc().toString();
            } catch( SQLException e ) {
                // If get here, it is likely to break some functionality like Database Snapshots.
                // Then we will have to revise this logic.
                return value.toString();
            }
        }

        return value.toString();
    }

    public DbColumn getDbColumn() {

        return dbColumn;
    }
}

@@ -691,16 +691,49 @@
 ption;%0A%0A
+import org.apache.log4j.Logger;%0A%0A
 /**%0A * C
@@ -803,24 +803,128 @@
 ordValue %7B%0A%0A
+    private static final Logger log                         = Logger.getLogger( DbRecordValue.class );%0A%0A
     private 
@@ -924,32 +924,43 @@
 rivate DbColumn 
+           
 dbColumn;%0A    pr
@@ -974,22 +974,615 @@
 bject   
-value;
+           value;%0A%0A    // flag, used to determine whether ojdbc (Oracle JDBC) driver is in JAVA class path%0A    private static boolean      oracleJdbcDriverInClassPath = false;%0A%0A    static %7B%0A        try %7B%0A            Class%3C?%3E clazz = Class.forName( oracle.jdbc.OracleDriver.class.getName() );%0A            if( clazz != null ) %7B%0A                oracleJdbcDriverInClassPath = true;%0A            %7D else %7B%0A                oracleJdbcDriverInClassPath = false;%0A            %7D%0A        %7D catch( ClassNotFoundException %7C NoClassDefFoundError e ) %7B%0A            oracleJdbcDriverInClassPath = false;%0A        %7D%0A    %7D
 %0A%0A    /*
@@ -3216,32 +3216,80 @@
 g();%0A        %7D%0A%0A
+        if( oracleJdbcDriverInClassPath ) %7B%0A    
         // handl
@@ -3308,16 +3308,20 @@
 e stamp%0A
+    
         
@@ -3370,38 +3370,46 @@
 ) %7B%0A            
+   
+ 
 try %7B%0A
+    
                 
@@ -3463,32 +3463,36 @@
 c().toString();%0A
+    
             %7D ca
@@ -3531,16 +3531,20 @@
         
+    
 // If ge
@@ -3633,16 +3633,20 @@
         
+    
 // Then 
@@ -3680,16 +3680,216 @@
  logic.%0A
+                    log.warn( %22There was error while parsing value as oracle.sql.TIMESTAMP.%22%0A                              + %22We will instead return the value as %22 + value.toString() + %22 .%22, e );%0A    
         
@@ -3913,32 +3913,50 @@
 lue.toString();%0A
+                %7D%0A
             %7D%0A  

Check if oracle jdbc driver classes are in class path - This is related to a bug in DbRecordValue.getValuesAsString() where even if the test does not uses an Oracle Database, an exception is thrown for missing class (oracle.sql.TIMESTAMP)