# SearchDocs - search screen for keyword searching of
# the S2K database
#
proc SearchDocs {{w .search}} {
     catch {destroy $w}
     toplevel $w
     dpos $w
     global LASTSEARCH
     global BIGFONT
     wm title $w "Search Window"
     wm iconname $w "Search"
     set c $w.c

    set font *helvetica-medium-r-*-140-*

    frame $w.frame1 -relief raised -bd 2
    text $c -relief sunken -width 60 -height 20 -wrap word -font $font -padx 20 -pady 20 -borderwidth 2
     message $w.frame1.msg -aspect {900}  -font $BIGFONT  \
       -justify {center}  -pady {5}  -relief {raised}  \
       -text {Type Search Request below (use plain English)}


    pack append $w $w.frame1 {top expand fill} $w.c {expand fill}
    pack append $w.frame1 $w.frame1.msg {top frame center expand fillx filly}


     # Give the text item some other  bindings for ease in editing.

    bind $c <Right> "$c mark set insert {insert + 1 chars}"
    bind $c <Left> "$c mark set insert {insert - 1 chars}"
    bind $c <Up> "$c mark set insert {insert - 1 lines}"
    bind $c <Down> "$c mark set insert {insert + 1 lines}"



 frame $w.bot -borderwidth {2}  -geometry {20x20}  -relief {raised}

 button $w.bot.butt11  -text {Cancel} -pady 10 \
	       -command "destroy $w"

 button $w.bot.butt10  -text {Help} \
	      -command "createHelpListBox Search_Help searchdocs.help"

 button $w.bot.butt9  -text {Retrieve}\
	      -command "dosearch $w"

 button $w.bot.butt8  -text {Last Search}\
	      -command "$w.c delete 1.0 end ; $w.c insert end \"$LASTSEARCH\" "


 pack append $w.bot \
	       $w.bot.butt8 {left frame center expand fillx filly} \
	       $w.bot.butt9 {left frame center expand fillx filly} \
	       $w.bot.butt10 {left frame center  expand fillx filly} \
	       $w.bot.butt11 {left frame center expand fillx filly}



    pack append $w $w.bot {bottom fill}

    focus $w.c

 }

proc dosearch {w} {
    global LASTSEARCH

    set c $w.c
    set temp [$c get 1.0 end]
    set LASTSEARCH $temp
    $w config -cursor watch
    update
    WaitWindow {retrieving from POSTGRES}

    find $w [concat [string trim $temp]]

    $w config -cursor top_left_arrow
    update

}


proc find {w args} {
    global LASTSEARCH
    global SEARCHRES1
    global QUERYIDNUM
    catch {unset SEARCHRES1}
    catch {unset QUERYIDNUM}
    set args2 [concat [lindex $args 0]]

    PQexec "begin"

    set cmd "retrieve (qid = kwsearch(\"$args2\"::text)) "
    PQexec $cmd

    # get the queryid result
    PQtuplearray blank QUERYIDNUM

    PQexec "end"


    set queryid $QUERYIDNUM(000.000000)

#    puts stdout "query id is $queryid"

#    puts stdout "search done..."
    rankresults $w $queryid

}


proc rankresults {w queryid} {
    global SEARCHRES1 RANKVALS
    set totfreq 0
    set totcount 0

    catch unset RANKVALS


    set query1 [retrieve (d.doc_prob_rel,d.tuple_id,b.title) \
       from d in kw_retrieval, b in TECH_REPORT_BIB \
       where d.query_id = $queryid and d.tuple_id = b.oid ]

#    puts stdout $query1

    set ntuples [lindex [lindex $query1 0] 0]

    set query2 [retrieve (d.doc_prob_rel,d.tuple_id,b.title) \
       from d in kw_retrieval, b in TECH_REPORT_BIB, a in DOC_TEXT \
       where d.query_id = $queryid and d.tuple_id = a.oid \
       and a.reference = b.oid]

    incr ntuples [lindex [lindex $query1 0] 0]

    if {$ntuples==0} {
        WaitWindow {No documents retrieved
  click here to remove window }
        normalcursor
        return
    }
    
    set tmpres [lsort [concat [lrange $query1 1 end] [lrange $query2 1 end]]]

# the following reverses the sort -- not needed?
#    set results ""
#    foreach i $tmpres {
#	set results [linsert $results 0 $i]
#    }

  #
  # Load the tuples into the listbox
  #
    set RANKVALS $tmpres

    browsedoclist $w "$RANKVALS" "Ranked Search Results"
}

