File: n_ds_report.sru
Size: 7207
Date: Sat, 08 Dec 2007 15:38:36 +0100
$PBExportHeader$n_ds_report.sru
$PBExportComments$Experimental Report Generating Class
forward
global type n_ds_report from datastore
end type
end forward

global type n_ds_report from datastore
string dataobject = "pwd_d_selectreport"
end type
global n_ds_report n_ds_report

type prototypes
Function Int GetTempFileName(Ref String ls_path,Ref String ls_filename,Int li_Seed,Ref String ls_result_name) ALIAS FOR  GetTempFileNameA LIBRARY "kernel32"
FUNCTION boolean SetEnvironmentVariable ( string szName, string szValue ) ALIAS FOR SetEnvironmentVariableA  LIBRARY "kernel32.dll" 

end prototypes

forward prototypes
public function string of_invokemethod (string as_method)
public function string of_getreport ()
end prototypes

public function string of_invokemethod (string as_method);
// Some JavaScript to always submit all 3 Columns 
// experimtal code
string ls_js_test
ls_js_test = "&
<SCRIPT Language=JavaScript>~r~n&
function forceAcceptText (HTMLDW , rowNum, colNum , colName )~r~n&
{~r~n&
// Workaround to force an AcceptText() on a HTMLDW Column~r~n&
// 21.09.2007 Arnd Schmidt pbwebeasy.org~r~n&
var control = HTMLDW.findControl(colName,rowNum -1  , true);~r~n&
  if (control != null)~r~n&
  {~r~n&
    if ( HTMLDW.SetRow(rowNum) == 1 )~r~n&
    {~r~n&
       if ( HTMLDW.SetColumn(colName) == 1 )~r~n&
       {~r~n&
          HTMLDW.currentControl = control;~r~n&
          eval('HTMLDW.itemGainFocus(rowNum - 1,colNum,control,HTMLDW.gobs.'+ colName +')');~r~n&
          HTMLDW.currentControl.bChanged=true;~r~n&
          return HTMLDW.AcceptText();~r~n&
       }~r~n&
    }~r~n&
  }~r~n&
  return -1;~r~n&
}~r~n&
function pwd_d_selectreport_UpdateStart ()~r~n&
{~r~n&
   forceAcceptText(this,1,1,'sdatawindow');~r~n&
   forceAcceptText(this,1,2,'soutputtype');~r~n&
   forceAcceptText(this,1,3,'sretrievalarguments');~r~n&
   return 0;~r~n&
}~r~n&
</SCRIPT>~r~n"

// With PB 9 the script is NOT WORKING!
ls_js_test  = ""

choose case as_method
   case "of_getreport"
      return of_getreport ()
      // Populate report (Tutorial Message)
      /*
      // Tutorial
      return '<html>~n<head>~n<title>PB Web Easy - Report Example</title>~n</head>~n<body>~n' + &
      '<h1>Your turn ;-)</h1>' +&
      +'~n</body>~n</html>'
      */
   case else
      // Show Form
      return '<html>~n<head>~n<title>PB Web Easy - Report Example</title>~n</head>~n<body>~n' &
            + ls_js_test +&
            +  This.Object.DataWindow.data.HTML + &
            +' ~n</body>~n</html>'

end choose

end function

public function string of_getreport ();string ls_mimetype   // Mime Type to be created html|pdf|png

string ls_tempdir    // Temp Directory as configured in the Application's section 
string ls_tempprefix // Temp Filenames Prefix
string ls_tempfile   // Generated Temp Filename

integer li_fileno // FileNumber folr openend output file
long ll_bytes     // Length in bytes of binary data output (pdf|png)
blob lbl_file     // Temp. Blob for file (sure, this can be "optimized"! for a direct binary filewrite to stdout

n_cst_runandwait lnv_run   // Custom class to run a command 
string ls_command          // Command line for a ps2pdf or ps2png
int li_rtc_run             // Return Code of the run

long ll_pos          // Some var to store results of Pos() calls

integer li_ret    // Return Value of SetHTMLAction ()
string ls_error   // Error Message 

datastore lds_report // Report DS 

// This one is for PB 9 and external Datawindows
// Do not use the 'Update' as Action-Parameter - it is not working in PB 9 !

li_ret = This.setHTMLAction( &
   ''  /* (cgienv.of_getParam( DataObject+'_action' ) */, &
   cgienv.of_getParam( DataObject+'_context' ) )

lds_report = create datastore
lds_report.dataobject = this.GetItemString(1,"sdatawindow")

lds_report.SetTransObject(SQLCA)

// This is brute force with one string retrieval argument :-(
lds_report.Retrieve( this.GetItemString(1,"sretrievalarguments") )

ls_mimetype = Lower(this.GetItemString(1,"soutputtype"))

choose case ls_mimetype
   case "html"
      return &
         '<html>~n<head>~n<title>' + this.GetItemString(1,"sdatawindow") + '</title>~n</head>~n<body>' + &
         lds_report.Object.DataWindow.data.HTML+ &
         '</body>~n</html>'

   case "pdf","png"
      ls_tempprefix = "PWE"         // PWE is a fine filename prefix
      ls_tempfile = space ( 255 )   // This should be long enough...
      ls_tempdir = ProfileString(cgienv.of_getINIFileName(), getApplication().AppName , "tempdir", "" )
      If GetTempFileName ( ls_tempdir , ls_tempprefix , 0 , ls_tempfile )<> 0 Then
         // Get PostScript Printername from INI File
         lds_report.Modify("DataWindow.Print.FileName='"+ls_tempfile+"'")
         lds_report.Object.DataWindow.Printer =  &
                     ProfileString(cgienv.of_getINIFileName(), "config" , "PSPrintername", "Sybase DataWindow PS")

         if lds_report.Print(false) = 1 then

            ls_command = ProfileString(cgienv.of_getINIFileName(), "config" , "ps2"+ls_mimetype, "" )
            IF Len ( ls_command ) > 0 Then
               lnv_run = CREATE n_cst_runandwait
               
               ll_pos = Pos ( ls_command , "$INPUT" )
               if ll_pos > 0 Then ls_command = Replace ( ls_command, ll_pos , 6, ls_tempfile )
               
               ll_pos = Pos ( ls_command , "$OUTPUT" )
               if ll_pos > 0 Then ls_command = Replace ( ls_command, ll_pos , 7, ls_tempfile + "."+ls_mimetype )
               
               if ll_pos > 0 then
                  li_rtc_run = lnv_run.of_run( ls_command )
                                    
                  li_fileno = FileOpen(ls_tempfile + "."+ls_mimetype, StreamMode!)
                  If li_fileno <> -1 Then 
                     ll_bytes = FileReadEx(li_fileno, lbl_file)
                     FileClose ( li_fileno )
                     FileDelete ( ls_tempfile )
                     FileDelete ( ls_tempfile + "."+ls_mimetype )

                     stdout.of_Write ("Content-Length: "+string (ll_bytes)+"~r~n")
                     choose case ls_mimetype 
                        case 'png' ; stdout.of_Write ("Content-Type: image/"+ls_mimetype+"~r~n~r~n")
                        case 'pdf' ; stdout.of_Write ("Content-Type: application/"+ls_mimetype+"~r~n~r~n")
                     end choose
                     stdout.of_WriteBinary ( lbl_file , ll_bytes)
                     // No logging to standard out allowed!
                     log.il_loglevel=0
                     // Return Empty Content, so that the main application's routine won't output any other data
                     return ""
               
                  else
                     ls_error = "FileOpen() of Output failed"
                  end if   
                  
                  FileDelete ( ls_tempfile )
                  
               else
                  ls_error = "Could not find $INPUT $OUPUT "
               end if
         
            else
               ls_error = "ps2"+ls_mimetype + " is not configured."
            end if
      
         else
            ls_error = "Print to PS FILE '"+ ls_tempfile + "' failed."
         end if
      End if      
      
   case else
      ls_error = "Unknown Outputtype specified!"
end choose

if Len (ls_error ) > 0 then
         return '<html>~n<head>~n<title>PB Web Easy Gen Report Error</title>~n</head>~n<body>' +&
            '<H1>Error</h1>'+ls_error+ &
            '</body>~n</html>'
end if

// Return empty string here so that no further output will be processed in the main routine!
return ""
end function

on n_ds_report.create
call super::create
TriggerEvent( this, "constructor" )
end on

on n_ds_report.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on