<?php
/**
 * gtcTask business
 *
 * @author Luiz Gilberto Gregory F [luiz@solis.coop.br]
 *
 * @version $Id$
 *
 * \b Maintainers \n
 * Eduardo Bonfandini [eduardo@solis.coop.br]
 * Jamiel Spezia [jamiel@solis.coop.br]
 * Luiz Gregory Filho [luiz@solis.coop.br]
 * Moises Heberle [moises@solis.coop.br]
 * Sandro Roberto Weisheimer [sandrow@solis.coop.br]
 *
 * @since
 * Class created on 06/08/2009
 *
 * \b Organization: \n
 * SOLIS - Cooperativa de Solucoes Livres \n
 * The Gnuteca3 Development Team
 *
 * \b CopyLeft: \n
 * CopyLeft (L) 2008 SOLIS - Cooperativa de Solucoes Livres \n
 *
 * \b License: \n
 * Licensed under GPL (for further details read the COPYING file or http://www.gnu.org/copyleft/gpl.html )
 *
 * \b History: \n
 * See history in SVN repository: http://gnuteca.solis.coop.br
 */


class BusinessGnuteca3BusScheduleTaskLog extends GnutecaBusiness
{
    public $scheduleTaskId,//"    integer,
           $log,//"               text,
           $date;//"              datetime

    public $columns,
           $table       = 'gtcScheduleTaskLog',
           $pkeys       = 'scheduleTaskId',
           $cols        = 'log, date';


    public function __construct()
    {
        $this->columns = "{$this->pkeys}, {$this->cols}";
        parent::__construct($this->table, $this->pkeys, $this->cols);

        $this->MIOLO->getClass($this->module, 'GnutecaDate2');
    }


    public function insertScheduleTaskLog($scheduleTaskId = null, $log = null)
    {
        $this->scheduleTaskId   = is_null($scheduleTaskId)  ? $this->scheduleTaskId : $scheduleTaskId;
        $this->log              = is_null($log)             ? $this->log            : $log;
        $now                    = new GnutecaDate2();
        $this->date             = $now->getDBDate();

        return $this->autoInsert();
    }


    public function updateScheduleTaskLog()
    {
        return $this->autoUpdate();
    }


    public function deleteScheduleTaskLog($taskId)
    {
        return $this->autoDelete($taskId);
    }


    public function getScheduleTaskLog($taskId)
    {
        $this->clear();
        return $this->autoGet($taskId);
    }

    /**
     * Check se uma tarefa ja foi executada.
     *
     * @param db date $startDate
     * @param db date $endDate
     * @param string $logMsg
     * @return boolean
     */
    public function checkRun($scheduleTaskId, $startDate, $endDate, $logMsg)
    {
        $this->clear();
        $this->setColumns("1");
        $this->setTables($this->table);
        $this->setWhere(" scheduleTaskId = ? AND (date >= ? AND date <= ?) AND log = ?");
        $sql = $this->select(array($scheduleTaskId, $startDate, $endDate, $logMsg));
        $rs  = $this->query($sql);
        return (!$rs) ? false : true;
    }

    public function searchScheduleTaskLog($orderBy='date desc', $limit=50)
    {
    	if ($this->scheduleTaskId)
    	{
    		$this->setWhere('scheduletaskid = ?');
    		$data[] = $this->scheduleTaskId;
    	}
    	if ( $this->log )
    	{
    		$this->setWhere('log = ?');
            $data[] = $this->log;
    	}
    	
        if ( $this->date )
        {
            $this->setWhere('date = ?');
            $data[] = $this->date;
        }
        
        $this->setTables('gtcscheduletasklog');
        
        $this->setColumns('scheduletaskid, 
                           log,
                           date');
        $this->setOrderBy($orderBy);
        $this->setLimit($limit);    
        $sql = $this->select($data);
        $rs  = $this->query($sql);
    	
        return $rs;
    }
    
    
}
?>