#!/usr/local/bin/oraperl 
#
# Copyright (C) 1992 by Gustaf Neumann, Stefan Nusser
#
#      Wirtschaftsuniversitaet Wien,
#      Abteilung fuer Wirtschaftsinformatik
#      Augasse 2-6,
#      A-1090 Vienna, Austria
#      neumann@wu-wien.ac.at, nusser@wu-wien.ac.at
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted, provided
# that the above copyright notice appears in all copies and that both that
# copyright notice and this permission notice appear in all supporting
# documentation.  This software is provided "as is" without expressed or
# implied warranty.
#
# Date: Mon, Apr 13 1992
# Author: Gustaf Neumann
# Version: 0.9
#


%privOptions = (
	"d", "database: form 'userid/passwd@T:machine:oracleId",
	"a", ": alternative oracle server",
	"v", ": verbous mode, displaying SQL commands",
	);

$WafeLib = $ENV{'WAFELIB'} || "/usr/lib/X11/wafe";
require "$WafeLib/perl/wafe.pl";


$database = $opt_d ||  ($opt_a ? $wafetelDBalt : $wafetelDB);

@sqlField = ('TEL_TEILNEHMER', 'TEL_VZ_VORNAME', 'TEL_VZ_KLAPPE');
@sqlLabel = ('Familienname', 'Vorname', 'Klappe');
$sqlTable =  "TEL_VZ_EINTRAG";

$oraChar = 'ÌÎÏØÚÛÞ';
$isoChar = 'äöüÄÖÜß';

%extraResources = (
		   '*Text', "$roColors displayCaret false $normalFont "
		           .'left chainLeft right chainLeft editType edit',
	'*Label', "$boldFont $backGround "
		           .'left chainLeft right chainLeft borderWidth 0',
);

&wafe'setResources("",%textResources);
&wafe'setResources("",%extraResources);


foreach ($[..$#sqlField) {
    $tcl .= "textField $sqlField[$_] {$sqlLabel[$_]} ";
    $tcl .= "fromVert $last" if $last;
    $tcl .= "\n";
    $last = $sqlField[$_];
}

$labelAtts = "justify right width 120";
&UI(<<"End of Wafe");
proc textField {name label args} {\\
    eval Label lab\$name form label {\$label:} $labelAtts \$args; \\
    eval Text \$name form fromHoriz lab\$name \$args width 200; \\
    action \$name override {<Key>Return: exec(echo read %w [gV %w string])}; \\
    action \$name override {Ctrl<Key>s: exec(echo read %w [gV %w string])}; \\
    action \$name override {<Key>Tab: exec(echo field)}; \\
    action \$name override {<Key>Down: exec(echo next)}; \\
    action \$name override {<Key>Up: exec(echo last)}; \\
}

Form form topLevel $backGround
$tcl

  Command quit form label Ende $buttonAtts \\
      callback {echo %w} fromVert $sqlField[$#sqlField]

  Label info form fromVert $sqlField[$#sqlField] \\
      fromHoriz quit label {} width 270

 realize; deleteWindowProtocol quit
 setKeyboardFocus form $sqlField[0]
End of Wafe

foreach (@sqlField) {
    &wafe'applyActions($_,@genericTextActions);
    &Xui("action $_ override {<Key>Next: exec(echo next)};"
        ."action $_ override {<Key>Prior: exec(echo last)}");
}


#
# fetch $i'th solution either from 
# the caching array or from oracle
# maintaining global variables $current and $max

sub fetchValue {
    local($i) = @_;
    return () if $i == 0;

    # fetching from cache
    if ($i <= $max) {
	$current = $i;
	return split(/$;/,$solution[$i]);
    }

    local(@result) = &ora_fetch($cursor) if $cursor;
    $cursor ="", return @result if !@result;
    foreach ($[..$#result) { 
	eval '$result[$_] =~ '. "tr/$oraChar/$isoChar/";
    }
    
    $solution[$i] = join($;,@result);
    $current = $i; 
    $max = $i;
    return @result;
}

sub displayValue {
    local($index) = @_;
    local(@result) = &fetchValue($index);
    if (!@result) {
	&info("Das waren alle!") if $max > 0;
	&info("Keiner gefunden") if $max == 0;
	return;
    }
    foreach ($[..$#sqlField) {&Xui("sV $sqlField[$_] string {$result[$_]}");}
    &info("");
}


$connection = &ora_login("", $database, "") || die $ora_errstr;
&info("connected to ". ((split(/:/,$database))[1]));

$currentField = 0;
&Xui("sV $sqlField[$currentField] $highLight displayCaret true");

while ($_=&wafe'read) {
    chop;
    if (/^read\s+(\S+)\s+(.*)/ ) {
	($att,$key) = ($1,$2);
	eval '$key =~ '. "tr/$isoChar/$oraChar/";
	&ora_close($cursor) if $cursor;
	$max = 0; 

	local($sqlQuery) = 
	    "select ".join(',',@sqlField)." from $sqlTable "
  	   ."where upper($att) like upper('$key%') order by $sqlField[0] ";
	print "SQL: $sqlQuery\n" if $opt_v;
	$cursor = &ora_open($connection, $sqlQuery);
	&displayValue(1);
    }

    if (/^field/) {
	&Xui("sV $sqlField[$currentField] $roColors displayCaret false");
	$currentField = ($currentField +1)%@sqlField;
	&Xui("sV $sqlField[$currentField] $highLight displayCaret true;"
	    ."setKeyboardFocus form $sqlField[$currentField]");
    }
    &displayValue($current+1) if /^next/;
    &displayValue($current-1) if /^last/;
    last if /^quit/;

#   print "<$_> $currentField\n";
}

&ora_close($cursor) if $cursor;
&ora_logoff($connection) || die "can't log off Oracle";
&Xui("quit");

