<?php

class MJasperReport extends MReport
{
    var $filetype; // pdf doc xls rtf htm rpt
    var $fileout;
    var $fileexp;
    var $objDb;
    var $db;
    var $connectionType;

    function __construct($db = 'admin', $connectionType = 'local')
    {
        $this->db = $db;
	    $this->connectionType=$connectionType;
    }

    function Execute($module, $name, $parameters = null, $rangeparam = null, $filetype = 'PDF', $save = false)
    {
        global $MIOLO, $page;

	//Caso o parâmetro filetype seja null (diferente de ter sido omitido)        
	$this->filetype = isset($filetype) ? $filetype : 'PDF';

    $filein = addslashes($MIOLO->GetModulePath($module, 'reports/' . $name . '.jasper'));

    $uniqid = uniqid(md5(uniqid("")));        
    $this->fileout= $uniqid . "." . strtolower($this->filetype);
    $pathout = $MIOLO->getConf("home.reports") .'/'.$this->fileout;
    
    $comando = 'echo $JAVA_HOME';
    $pathJava = trim(shell_exec($comando));
	
	$pathMJasper = $MIOLO->getConf("home.extensions")."/jasper";
	$pathScriptlets = $MIOLO->GetModulePath($module, 'reports/scriptlets.jar');
	$classPath = "$pathMJasper/lib/*:$pathMJasper/:$pathScriptlets:$pathJava/lib";
		
	$this->Fill($filein, $pathout, $filetype, $parameters, $classPath, $save);
	
   }
   
    function Fill($fileIn, $fileOut, $filetype, $parameters, $classPath, $save) {
    
        global $MIOLO, $page;
   
        if ($this->connectionType=='local') {
        	//Nova solução sem usar o TomCat	
		
            $param = "";
		    if (is_array($parameters))
	        {
	            foreach ($parameters as $pn => $pv)
	            {
				        $param .= '&' . $pn . "<-" . $pv;
	            }
            }
		
		    $this->objDb = $MIOLO->GetDatabase($this->db);
		    $dbUser = $this->objDb->user;
		    $dbPass = $this->objDb->pass;
		    $jdbcDriver = $this->objDb->jdbc_driver;
		    $jdbcDb =  $this->objDb->jdbc_db;
		
			$param = "relatorio<-$fileIn".$param."&fileout<-".$fileOut."&filetype<-".$this->filetype;
	
		    $comando = 'echo $JAVA_HOME';
		    $pathJava = trim(shell_exec($comando));
            $pathLog = $MIOLO->GetConf('home.logs');
            $uniqid = uniqid(md5(uniqid("")));
            $fileLog = $uniqid . ".txt";
			
            $cmd = $pathJava  . "/bin/java -classpath $classPath MJasper \"{$pathMJasper}\" \"{$param}\" \"{$dbUser}\" \"{$dbPass}\" \"{$jdbcDriver}\" \"{$jdbcDb}\"";
		    $cmd .= " 2> $pathLog/$fileLog";

            exec($cmd,$output);
mdump($cmd);
//            var_dump($output) ;

            //Aplicação JAVA rodou sem erros!
            if (trim($output[0])=="end") {
                //Resolve problema de incompatibilidade com quebra de linhas entre Windows e Unix
                if (strtoupper(trim($this->filetype))=="TXT") Mutil::unix2dos($fileOut);
                
                if ($save) {
                    $MIOLO->response->sendDownload($fileOut);
                } else {
                    $this->fileout = $MIOLO->getActionURL('miolo','reports:'.$this->fileout);
//mdump($this->fileout);
			        $MIOLO->getPage()->window($this->fileout);
                }
                return 1;
		    } else {
                
                $link = new MLink('','aqui',$MIOLO->GetActionURL('miolo',"logs:$fileLog"));
			    $detalhes = "<br>Para mais detalhes clique " . $link->generate();
			    throw new EControlException(implode("<br>",$output) . $detalhes);
		    }
	
        } else if ($this->connectionType=='remote') {
		    //Mantendo a possibilidade de usar outra máquina para geração de relatórios com o TomCat
		    $this->fileout = $MIOLO->getConf("home.url_jasper"). "?bd={$this->db}&relatorio=$filein" . $param;
		    $MIOLO->getPage()->window($this->fileout);
	    }
   
   
   }
   
   function ExecuteJRXML($fileIn, $parameters = null, $filetype = 'PDF', $save = false) {
        global $MIOLO;
   
        $this->filetype = isset($filetype) ? $filetype : 'PDF';

        $uniqid = uniqid(md5(uniqid("")));        
        $this->fileout = $uniqid . "." . strtolower($this->filetype);
        $pathout = $MIOLO->getConf("home.reports") .'/'.$this->fileout;
	
	    $pathMJasper = $MIOLO->getConf("home.extensions")."/jasper";
	    $classPath = "$pathMJasper/lib/*:$pathMJasper/";
		
	    $this->Fill($fileIn, $pathout, $this->filetype, $parameters, $classPath, $save);
   
   }
   
   
   
}
?>
