/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (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.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001-02 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.core.internals;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import org.apache.log4j.*;
import com.silverwrist.venice.core.SecurityInfo;
import com.silverwrist.venice.db.*;
import com.silverwrist.venice.except.AccessError;
import com.silverwrist.venice.security.*;
import com.silverwrist.venice.svc.ServiceControl;

public class EnvEngine
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static Category logger = Category.getInstance(EnvEngine.class);

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private EngineBackend engine;       // the engine
  private DataPool datapool;          // the database pool
  private ServiceControl scon;        // the service controller

  /*--------------------------------------------------------------------------------
   * Constructors
   *--------------------------------------------------------------------------------
   */

  public EnvEngine(EngineBackend engine, DataPool datapool, ServiceControl scon)
  {
    this.engine = engine;
    this.datapool = datapool;
    this.scon = scon;

  } // end constructor

  protected EnvEngine(EnvEngine other)
  {
    this.engine = other.engine;
    this.datapool = other.datapool;
    this.scon = other.scon;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Internal operations
   *--------------------------------------------------------------------------------
   */

  protected SecurityMonitor getStaticMonitor()
  {
    return engine.env_getSecurityMonitor(EngineBackend.SM_GLOBAL);

  } // end getStaticMonitor

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  public final EngineBackend getEngine()
  {
    return engine;

  } // end getEngine

  public final DataPool getDataPool()
  {
    return datapool;

  } // end getDataPool

  public final ServiceControl getSCM()
  {
    return scon;

  } // end getSCM

  public final Connection getConnection() throws SQLException
  {
    return datapool.getConnection();

  } // end getConnection

  public final void releaseConnection(Connection c)
  {
    datapool.releaseConnection(c);

  } // end releaseConnection

  public final boolean permissionDefined(String symbol, boolean no_follow)
  {
    return getStaticMonitor().permissionDefined(symbol,no_follow);

  } // end permissionDefined

  public final List getRoleList(String symbol)
  {
    return getStaticMonitor().getRoleList(symbol);

  } // end getRoleList

  public final Role getRole(String symbol)
  {
    return getStaticMonitor().getRole(symbol);

  } // end getRole

  public final Role getRoleForLevel(int level)
  {
    return getStaticMonitor().getRoleForLevel(level);

  } // end getRoleForLevel

  public final Role getDefaultRole(String symbol)
  {
    return getStaticMonitor().getDefaultRole(symbol);

  } // end getDefaultRole

  public final SecurityInfo getSecurityInfo()
  {
    return new SecurityInfoWrapper(getStaticMonitor());

  } // end getSecurityInfo

  public final void saveAuditRecord(AuditRecord ar)
  {
    Connection conn = null;

    if (ar==null)
      return;  // don't store a null record

    try
    { // get a connection and use it to store the audit record
      conn = datapool.getConnection();
      ar.store(conn);

    } // end try
    catch (SQLException e)
    { // just log an error if we screwed up
      logger.error("DB error saving audit record: " + e.getMessage(),e);

    } // end catch
    finally
    { // make sure the connection is released before we go
      datapool.releaseConnection(conn);

    } // end finally

  } // end saveAuditRecord

  public final String getStockMessage(String key)
  {
    return engine.getStockMessage(key);

  } // end getStockMessage

} // end class EnvEngine
