view software/eXist/webapp/mpdl/interface/queryResult.xql @ 11:d6f528ad5d96

TEI Unterst?tzung, Fehlerbehebungen, externe Objekte
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Fri, 11 Mar 2011 13:34:02 +0100
parents 5589d865af7a
children
line wrap: on
line source

xquery version "1.0";

import module namespace functx = "http://www.functx.com" at "../util/functx.xql";
import module namespace mpdl-lucene = "http://www.mpiwg-berlin.mpg.de/ns/mpdl/lucene/search" at "../lucene/search.xql";

declare namespace request="http://exist-db.org/xquery/request";

declare namespace echo="http://www.mpiwg-berlin.mpg.de/ns/echo/1.0/";
declare namespace TEI="http://www.tei-c.org/ns/1.0";

declare namespace dc="http://purl.org/dc/elements/1.1/";
declare namespace dct="http://purl.org/dc/terms/1.0/";
declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";

let $queryType := request:get-parameter("queryType", "")
let $docbase := request:get-parameter("docbase", "")
let $output := request:get-parameter("output", "html")
let $attribute1 := request:get-parameter("attribute1", "")
let $relOp1 := request:get-parameter("relOp1", "")
let $attrQuery1 := request:get-parameter("attrQuery1", "")
let $boolOp := request:get-parameter("boolOp", "")
let $attribute2 := request:get-parameter("attribute2", "")
let $relOp2 := request:get-parameter("relOp2", "")
let $attrQuery2 := request:get-parameter("attrQuery2", "")
let $ftQuery := request:get-parameter("ftQuery", "")
let $ftMorphQuery := request:get-parameter("ftMorphQuery", "")
let $orderBy := request:get-parameter("orderBy", "")
let $language := request:get-parameter("language", "")
let $pn := number(request:get-parameter("pn", ""))
let $reqPageSize := request:get-parameter("pageSize", "")
let $pageSize := 
  if ($reqPageSize = '' or $reqPageSize = '0')
  then 20
  else number($reqPageSize)

let $docPathStandard := "/db/mpdl/documents/standard"
let $docPathMorph := "/db/mpdl/documents/morph"
let $docPath := 
  if($queryType = 'fulltextMorph')
  then $docPathMorph
  else $docPathStandard

let $docBaseArch := "archimedes"
let $docBaseEcho := "echo"
let $docBaseTei := "tei"
let $fulltextMorphArchDocPath := concat($docPathMorph, "/", $docBaseArch, "/", $language)
let $fulltextMorphEchoDocPath := concat($docPathMorph, "/", $docBaseEcho, "/", $language)
let $fulltextMorphTeiDocPath := concat($docPathMorph, "/", $docBaseTei, "/", $language)
let $fulltextMorphCollection := 
  if(contains($docbase, $docBaseArch) and contains($docbase, $docBaseEcho) and contains($docbase, $docBaseTei))
  then collection($fulltextMorphArchDocPath, $fulltextMorphEchoDocPath, $fulltextMorphTeiDocPath)
  else if(contains($docbase, $docBaseArch) and contains($docbase, $docBaseEcho) and not(contains($docbase, $docBaseTei)))
  then collection($fulltextMorphArchDocPath, $fulltextMorphEchoDocPath)
  else if(contains($docbase, $docBaseArch) and not(contains($docbase, $docBaseEcho)) and contains($docbase, $docBaseTei))
  then collection($fulltextMorphArchDocPath, $fulltextMorphTeiDocPath)
  else if(not(contains($docbase, $docBaseArch)) and contains($docbase, $docBaseEcho) and contains($docbase, $docBaseTei))
  then collection($fulltextMorphEchoDocPath, $fulltextMorphTeiDocPath)
  else if(contains($docbase, $docBaseArch) and not(contains($docbase, $docBaseEcho)) and not(contains($docbase, $docBaseTei)))
  then collection($fulltextMorphArchDocPath)
  else if(not(contains($docbase, $docBaseArch)) and contains($docbase, $docBaseEcho) and not(contains($docbase, $docBaseTei)))
  then collection($fulltextMorphEchoDocPath)
  else if(not(contains($docbase, $docBaseArch)) and not(contains($docbase, $docBaseEcho)) and contains($docbase, $docBaseTei))
  then collection($fulltextMorphTeiDocPath)
  else ()

let $fulltextStandardArchDocPath := concat($docPathStandard, "/", $docBaseArch)
let $fulltextStandardEchoDocPath := concat($docPathStandard, "/", $docBaseEcho)
let $fulltextStandardTeiDocPath := concat($docPathStandard, "/", $docBaseTei)
let $fulltextStandardCollectionStr := 
  if(contains($docbase, $docBaseArch) and contains($docbase, $docBaseEcho) and contains($docbase, $docBaseTei))
  then concat("collection('", $fulltextStandardArchDocPath, "', '", $fulltextStandardEchoDocPath, "', '", $fulltextStandardTeiDocPath, "')")
  else if(contains($docbase, $docBaseArch) and contains($docbase, $docBaseEcho) and not(contains($docbase, $docBaseTei)))
  then concat("collection('", $fulltextStandardArchDocPath, "', '", $fulltextStandardEchoDocPath, "')")
  else if(contains($docbase, $docBaseArch) and not(contains($docbase, $docBaseEcho)) and contains($docbase, $docBaseTei))
  then concat("collection('", $fulltextStandardArchDocPath, "', '", $fulltextStandardTeiDocPath, "')")
  else if(not(contains($docbase, $docBaseArch)) and contains($docbase, $docBaseEcho) and contains($docbase, $docBaseTei))
  then concat("collection('", $fulltextStandardEchoDocPath, "', '", $fulltextStandardTeiDocPath, "')")
  else if(contains($docbase, $docBaseArch) and not(contains($docbase, $docBaseEcho)) and not(contains($docbase, $docBaseTei)))
  then concat("collection('", $fulltextStandardArchDocPath, "')")
  else if(not(contains($docbase, $docBaseArch)) and contains($docbase, $docBaseEcho) and not(contains($docbase, $docBaseTei)))
  then concat("collection('", $fulltextStandardEchoDocPath, "')")
  else if(not(contains($docbase, $docBaseArch)) and not(contains($docbase, $docBaseEcho)) and contains($docbase, $docBaseTei))
  then concat("collection('", $fulltextStandardTeiDocPath, "')")
  else ""
let $metadataStr := concat("(", $fulltextStandardCollectionStr, "/archimedes/info", "|" , $fulltextStandardCollectionStr, "/echo:echo/echo:metadata", "|" , $fulltextStandardCollectionStr, "/TEI:TEI/TEI:teiHeader", ")")
let $fulltextStandardCollection := 
  if($fulltextStandardCollectionStr != "")
  then util:eval($fulltextStandardCollectionStr)
  else ()

let $isAttributeSearch :=
  if ($queryType = "attribute")
  then true()
  else false()
let $isSimpleAttributeSearch :=
  if ($queryType = "attribute" and $attrQuery2 = "")
  then true()
  else false()
let $isBooleanAttributeSearch :=
  if ($queryType = "attribute" and $attrQuery2 != "")
  then true()
  else false()

(:  TODO:  performance improvement: at this time the result is fully scanned 3 times (query, ordering, presentation)   :)
let $attrQueryResult := 
  if ($queryType = "browse")
  then $fulltextStandardCollection
  else if ($queryType = "fulltext")
  then mpdl-lucene:search($fulltextStandardCollection, $ftQuery)
  else if ($queryType = "fulltextMorph")
  then mpdl-lucene:search($fulltextMorphCollection, $ftMorphQuery)
  else if ($isAttributeSearch)
  then mpdl-lucene:attrSearch($metadataStr, $attribute1, $attrQuery1, $boolOp, $attribute2, $attrQuery2)
  else ()
  
let $orderedAttrQueryResult := 
  if ($queryType = "fulltext" or $queryType = "fulltextMorph")
  then
  (for $attrElem in $attrQueryResult
   order by ft:score($attrElem) descending
   return $attrElem)
  else mpdl-lucene:order($attrQueryResult, $orderBy)

let $countResult := count($orderedAttrQueryResult)
let $countPagesTemp := $countResult idiv $pageSize + 1
let $countPages :=
  if((($countResult - 1) idiv $pageSize + 1) = ($countPagesTemp - 1))   
  then $countPagesTemp - 1   (: if countResult is exactly 10, 20, 30, ... then 1 has to be subtracted   :)
  else $countPagesTemp
let $pnLeftNumber :=
  if ($pn > 1)
  then $pn - 1
  else $pn
let $pnRightNumber :=
  if ($pn < $countPages)
  then $pn + 1
  else $pn
let $positionFrom := (($pn - 1) * $pageSize) + 1
let $positionTo := 
  if ($pn = $countPages)
  then $countResult
  else $pn * $pageSize

let $mode :=
  if ($queryType = "fulltext" or $queryType = "fulltextMorph")
  then "text"
  else "image"

let $pageResult := 
  for $elem at $pos in $orderedAttrQueryResult
    let $doc := $elem/fn:root()
    let $documentUriOrig := document-uri($doc)
    let $documentUri := substring-after($documentUriOrig, $docPath)
    let $docBase := substring-before(substring-after($documentUri, "/"), "/")
    let $metadata := mpdl-lucene:getMetadata($docBase, $doc)
    (: Performance: following is slow: why  (would be better structured code) ?
      let $attrAuthorStr := mpdl-lucene:getElemNameByAttr($docBase, "author")
      let $author := mpdl-lucene:getElemDynamic($metadataElem, $attrAuthorStr)
    :)    
    let $authorElems := mpdl-lucene:getElementsByAttr($metadata, $docBase, "author") 
    let $titleElems := mpdl-lucene:getElementsByAttr($metadata, $docBase, "title")
    let $placeElems := mpdl-lucene:getElementsByAttr($metadata, $docBase, "place")
    let $dateElems := mpdl-lucene:getElementsByAttr($metadata, $docBase, "date")
    let $authors := string-join($authorElems, ', ')
    let $titles := string-join($titleElems, ', ')
    let $places := string-join($placeElems, ', ')
    let $dates := string-join($dateElems, ', ')
    let $resultElem := 
      if ($output = "html")
      then 
        <tr>
          <td valign="top" style="padding-left:5px;">{$pos}.</td>
          <td valign="top" style="padding-left:7px;"><a href="echo/echoDocuView.xql?document={$documentUri}"><img src="../images/book.png" width="15" height="15" border="0"/></a> </td>
          <td valign="top"><a href="/exist/rest{$docPathStandard}{$documentUri}" target="_blank"><img src="../images/download.png" width="15" height="15" border="0" alt="Download"/></a></td>
          <td valign="top" style="padding-left:5px;"><i>{$authors}</i></td>
          <td valign="top" style="padding-left:5px;">{$titles}</td>
          <td valign="top" style="padding-left:5px;">{$places}</td>
          <td valign="top" style="padding-left:5px;">{$dates}</td>
        </tr>
      else if ($output = "xml")
      then 
        <document>
          <position>{$pos}</position>
          <uri>{$documentUri}</uri>
          <author>{$authors}</author>
          <title>{$titles}</title>
          <place>{$places}</place>
          <date>{$dates}</date>
        </document>
      else ()
  where $pos >= $positionFrom and $pos <= $positionTo
  return $resultElem

let $attrQueryResultError := string($attrQueryResult/error)

let $resultPageBody := 
  if ($attrQueryResultError = '' and $countResult > 0)
  then 
    <form action="queryResult.xql" method="get">
      <input type="hidden" name="docbase" value="{$docbase}"/>
      <input type="hidden" name="queryType" value="{$queryType}"/>
      <input type="hidden" name="attribute1" value="{$attribute1}"/>
      <input type="hidden" name="relOp1" value="{$relOp1}"/>
      <input type="hidden" name="attrQuery1" value="{$attrQuery1}"/>
      <input type="hidden" name="boolOp" value="{$boolOp}"/>
      <input type="hidden" name="attribute2" value="{$attribute2}"/>
      <input type="hidden" name="relOp2" value="{$relOp2}"/>
      <input type="hidden" name="attrQuery2" value="{$attrQuery2}"/>
      <input type="hidden" name="ftQuery" value="{$ftQuery}"/>
      <input type="hidden" name="ftMorphQuery" value="{$ftMorphQuery}"/>
      <input type="hidden" name="language" value="{$language}"/>
      <input type="hidden" name="pn" value="{$pn}"/>
      <input type="hidden" name="pageSize" value="{$pageSize}"/>
    <table width="100%" border="2" rules="groups">
    <colgroup>
      <col width="3%"/>
      <col width="3%"/>
      <col width="3%"/>
      <col width="15%"/>
      <col width="51%"/>
      <col width="15%"/>
      <col width="6%"/>
    </colgroup>
    <thead>
    <tr>
      <th align="left" valign="top">
        <button id="dummy" name="orderBy" value="{$orderBy}" style="padding:0px;font-weight:bold;font-size:14px;background:none;border:none;">No.</button>
      </th>
      <th align="left" valign="top"><button id="dummy" name="orderBy" value="{$orderBy}" style="padding:0px;font-weight:bold;font-size:14px;background:none;border:none;">Echo</button></th>
      <th></th>
      <th align="left" valign="top">
        <button name="orderBy" value="author" style="padding:0px;font-weight:bold;font-size:14px;background:none;border:none;">Author</button>
      </th>
      <th align="left" valign="top">
        <button name="orderBy" value="title" style="padding:0px;font-weight:bold;font-size:14px;background:none;border:none;">Title</button>
      </th>
      <th align="left" valign="top">
        <button name="orderBy" value="place" style="padding:0px;font-weight:bold;font-size:14px;background:none;border:none;">Place</button>
      </th>
      <th align="left" valign="top">
        <button name="orderBy" value="date" style="padding:0px;font-weight:bold;font-size:14px;background:none;border:none;">Year</button>
      </th>
    </tr>
    </thead>
    <tbody>
    <tr/>
      {$pageResult}
    </tbody>
    </table>
    </form>
  else if ($attrQueryResultError != '')
  then (<b>Your query delivers an error: </b>, $attrQueryResultError)
  else (<b>Your query delivers no result</b>)

let $resultPage :=
  if ($output = "html")
  then 
    <div class="queryResult">
      <div class="queryResultHits" style="visibility:hidden">{$countResult}</div>
      <div class="queryResultPage">
        {$resultPageBody}
      </div>
    </div>  
  else if ($output = "xml")
  then
    <result>
      <query>
        <type>{$queryType}</type>
        <language>{$language}</language>
        <docbase>{$docbase}</docbase>
        <ftQuery>{$ftQuery}</ftQuery>
        <ftMorphQuery>{$ftMorphQuery}</ftMorphQuery>
      </query>
      <queryResult>
        <size>{$countResult}</size>
        <page>
          <size>{$pageSize}</size>
          <pageNumber>{$pn}</pageNumber>
          <content>{$pageResult}</content>
        </page>
      </queryResult>
    </result>
  else ()   
return $resultPage