package com.utic.center.utic.ptis.server.config;

import oracle.jdbc.OracleConnection;
import org.springframework.jdbc.datasource.DelegatingDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class OracleAwareDataSource extends DelegatingDataSource {

    private final String moduleName;
    private final String actionName;
    private final String clientIdentifier;
    private final String clientInfo;

    public OracleAwareDataSource(DataSource delegate, String moduleName, String actionName,
                                 String clientIdentifier, String clientInfo) {
        super(delegate);
        this.moduleName = moduleName;
        this.actionName = actionName;
        this.clientIdentifier = clientIdentifier;
        this.clientInfo = clientInfo;
    }

    @Override
    public Connection getConnection() throws SQLException {
        Connection conn = super.getConnection();
        if (conn.isWrapperFor(OracleConnection.class)) {
            OracleConnection oracleConn = conn.unwrap(OracleConnection.class);
            oracleConn.setClientInfo("MODULE", moduleName);
            oracleConn.setClientInfo("ACTION", actionName);
            oracleConn.setClientInfo("CLIENT_IDENTIFIER", clientIdentifier);
            oracleConn.setClientInfo("CLIENT_INFO", clientInfo);
        }
        return conn;
    }

}
