<?php

require_once 'common/Object.php';
require_once 'dsn/Dsn.php';

/**
 * This is a common base class for Core and Workers.
 *
 * @abstract
 * @author  $Author: pruna $
 * @package orm
 * @version $Revision: 1.3 $
 * @created 19-Oct-2005
 * @modified $Date: 2005/12/06 02:56:45 $
 */
abstract class OrmBase extends Object {
    
    /**
     * @const
     */
    const PK_NULL_VALUE = null;
    
    /**
     * Internal holder for auxiliar <em>BeanUtils</em>.
     *
     * @var      BeanUtils
     * @access   protected
     */
    protected $bu;
    
    /**
     * Internal holder for database connection object.
     *
     * @var      object
     * @access   protected
     */
    protected $db;
    
    /**
     * Internal holder for database connection.
     *
     * @var      Dsn
     * @access   protected
     */
    protected $dsn;
    
    /**
     * Internal holder for <em>OrmMap</em> object for Bean.
     * 
     * @var      OrmMap
     * @access   protected
     */
    protected $map;
    
    /**
     * Auxiliar method to create a Bean.
     * 
     * @access   public
     * @param    OrmMap       $map         Map to be used for bean operations
     * @param    string       $beanName    Bean name to be instantiated
     * @access   protected
     */
    public function createBean($map, $beanName = null) {
        // @todo this method is intended to be refactored to use some external 
        // factory or similar
        if (is_null($beanName)) {
            $beanName = $map->getBeanName();
        }
        return new $beanName();
    }
    
    /**
     * Returns stored <em>BeanUtils</em> auxiliar object.
     *
     * @access  public
     * @return  BeanUtils
     */
    public function getBu() {
        return $this->bu;
    }
    
    /**
     * Returns stored <em>OrmIDb</em> object.
     *
     * @access  public
     * @return  OrmIDb
     */
    function getDb() {
        return $this->db;
    }
    
    /**
     * Returns stored <em>Dsn</em>.
     *
     * @access  public
     * @return  Dsn
     */
    function getDsn() {
        return $this->dsn;
    }
    
    /**
     * Returns stored <em>OrmMap</em> object.
     *
     * @access  public
     * @return  OrmMap
     */
    public function getMap() {
        return $this->map;
    }
    
    /**
     * Check if value is null, as defined for const <em>PK_NULL_VALUE</em>
     * 
     * @see     self::PK_NULL_VALUE
     * @param   mixed                $value   Value to check if null
     * @access  public
     * @return  boolean
     */
    public function isNullValue($value) {
        return $value === self::PK_NULL_VALUE;
    }
    
    /**
     * Stores <em>BeanUtils</em> auxiliar object.
     *
     * @access  public
     * @param   BeanUtils  $bu       BeanUtils for auxiliar operations
     */
    public function setBu($bu) {
        $this->bu = $bu;
    }
    
    /**
     * Stores <em>OrmIDb</em> object.
     * Also call <em>setDsn</em> to store dsn string.
     *
     * @access  public
     * @param   OrmIDb      $db       OrmIDb for database operations
     */
    function setDb($db) {
        $this->db = $db;
        $this->setDsn($db->getDsn());
    }
    
    /**
     * Stores <em>Dsn</em>.
     * 
     * If have no connection, or current connection has a different DSN, 
     * it creates a new <em>OrmIDb</em> to this DSN.
     * Default <em>OrmIDb</em> object that creates is a <em>OrmDbPearMdb2</em>.
     * 
     * @access  public
     * @param   string     $dsn      Dsn for database operations
     */
    function setDsn($dsn) {
        if (is_string($dsn)) {
            $str = $dsn;
            $dsn = new Dsn();
            $dsn->explode($str);
        }
        if (is_null($this->getDb()) || ($this->getDb()->getDsn()->implode() 
                != $dsn->implode())) {
            require_once 'orm/db/OrmDbPearMdb2.php';
            $this->db = new OrmDbPearMdb2($dsn);
        }
        $this->dsn = $dsn;
    }
    
    /**
     * Stores <em>OrmMap</em> to be used.
     * 
     * <strong>Important:</strong> note that if map is a string, it will 
     * create a new <em>OrmMapReader</em> and read it, to get the 
     * <em>OrmMap</em> that will store.
     * 
     * @access  public
     * @param   mixed  $map  Map to use for bean operations, as string or OrmMap
     */
    public function setMap($map) {
        // @todo See if this method could accept a xml as string, or a dsn as 
        // object and/or string.
        if (is_string($map)) {
            $mapper = new OrmMapReader();
            $this->map = $mapper->loadMap($map);
        } else {
            $this->map = $map;
        }
    }
    
}

?>