/*
 * 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.facebook.presto.hive;

import com.facebook.presto.spi.ConnectorSession;
import com.facebook.presto.spi.security.ConnectorIdentity;

import java.util.Optional;

import static com.google.common.base.MoreObjects.toStringHelper;
import static java.util.Objects.requireNonNull;

public class HdfsContext
{
    private final ConnectorIdentity identity;
    private final Optional<String> source;
    private final Optional<String> queryId;
    private final Optional<String> schemaName;
    private final Optional<String> tableName;
    private final Optional<String> tablePath;
    // true if the table already exist in the metastore, false if the table is about to be created in the current transaction
    private final Optional<Boolean> isNewTable;
    private final Optional<String> clientInfo;

    /**
     *  Table information is expected to be provided when accessing a storage.
     *  Do not use this constructor.
     */
    @Deprecated
    public HdfsContext(ConnectorIdentity identity)
    {
        this.identity = requireNonNull(identity, "identity is null");
        this.source = Optional.empty();
        this.queryId = Optional.empty();
        this.schemaName = Optional.empty();
        this.tableName = Optional.empty();
        this.clientInfo = Optional.empty();
        this.tablePath = Optional.empty();
        this.isNewTable = Optional.empty();
    }

    /**
     *  Table information is expected to be provided when accessing a storage.
     *  Do not use this constructor.
     */
    @Deprecated
    public HdfsContext(ConnectorSession session)
    {
        this(session, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty());
    }

    /**
     *  Table information is expected to be provided when accessing a storage.
     *  Currently the only legit use case for this constructor is the schema
     *  level operations (e.g.: create/drop schema) or drop a view
     *  Do not use this constructor for any other use cases.
     */
    @Deprecated
    public HdfsContext(ConnectorSession session, String schemaName)
    {
        this(session, Optional.of(requireNonNull(schemaName, "schemaName is null")), Optional.empty(), Optional.empty(), Optional.empty());
    }

    /**
     *  Table information is expected to be provided when accessing a storage.
     *  Do not use this constructor.
     */
    @Deprecated
    public HdfsContext(ConnectorSession session, String schemaName, String tableName)
    {
        this(
                session,
                Optional.of(requireNonNull(schemaName, "schemaName is null")),
                Optional.of(requireNonNull(tableName, "tableName is null")),
                Optional.empty(),
                Optional.empty());
    }

    public HdfsContext(
            ConnectorSession session,
            String schemaName,
            String tableName,
            String tablePath,
            boolean isNewTable)
    {
        this(
                session,
                Optional.of(requireNonNull(schemaName, "schemaName is null")),
                Optional.of(requireNonNull(tableName, "tableName is null")),
                Optional.of(requireNonNull(tablePath, "tablePath is null")),
                Optional.of(isNewTable));
    }

    private HdfsContext(
            ConnectorSession session,
            Optional<String> schemaName,
            Optional<String> tableName,
            Optional<String> tablePath,
            Optional<Boolean> isNewTable)
    {
        requireNonNull(session, "session is null");
        this.identity = requireNonNull(session.getIdentity(), "session.getIdentity() is null");
        this.source = requireNonNull(session.getSource(), "session.getSource() is null");
        this.queryId = Optional.of(session.getQueryId());
        this.schemaName = requireNonNull(schemaName, "schemaName is null");
        this.tableName = requireNonNull(tableName, "tableName is null");
        this.clientInfo = session.getClientInfo();
        this.tablePath = requireNonNull(tablePath, "tablePath is null");
        this.isNewTable = requireNonNull(isNewTable, "isNewTable is null");
    }

    public ConnectorIdentity getIdentity()
    {
        return identity;
    }

    public Optional<String> getSource()
    {
        return source;
    }

    public Optional<String> getQueryId()
    {
        return queryId;
    }

    public Optional<String> getSchemaName()
    {
        return schemaName;
    }

    public Optional<String> getTableName()
    {
        return tableName;
    }

    public Optional<String> getTablePath()
    {
        return tablePath;
    }

    public Optional<Boolean> isNewTable()
    {
        return isNewTable;
    }

    public Optional<String> getClientInfo()
    {
        return clientInfo;
    }

    @Override
    public String toString()
    {
        return toStringHelper(this)
                .omitNullValues()
                .add("user", identity)
                .add("source", source.orElse(null))
                .add("queryId", queryId.orElse(null))
                .add("schemaName", schemaName.orElse(null))
                .add("tableName", tableName.orElse(null))
                .add("tablePath", tablePath.orElse(null))
                .add("isNewTable", isNewTable.orElse(null))
                .add("clientInfo", clientInfo.orElse(null))
                .toString();
    }
}

@@ -1360,16 +1360,70 @@
 entInfo;
+%0A    private final Optional%3CConnectorSession%3E session;
 %0A%0A    /*
@@ -1986,24 +1986,65 @@
 al.empty();%0A
+        this.session = Optional.empty();%0A
     %7D%0A%0A    /
@@ -4143,16 +4143,43 @@
         
+this.session = Optional.of(
 requireN
@@ -4204,32 +4204,33 @@
 ession is null%22)
+)
 ;%0A        this.i
@@ -5460,24 +5460,112 @@
 nfo;%0A    %7D%0A%0A
+    public Optional%3CConnectorSession%3E getSession()%0A    %7B%0A        return session;%0A    %7D%0A%0A
     @Overrid
@@ -6044,32 +6044,32 @@
 e.orElse(null))%0A
-
                 
@@ -6104,32 +6104,86 @@
 o.orElse(null))%0A
+                .add(%22session%22, session.orElse(null))%0A
                 

Add ability to read session properties from HdfsContext