Mercurial > hg > mpdl-group
comparison software/eXist/webapp/mpdl/attribute-query-result.xql @ 7:5589d865af7a
Erstellung XQL/XSL Applikation
author | Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de> |
---|---|
date | Tue, 08 Feb 2011 15:16:46 +0100 |
parents | |
children | d6f528ad5d96 |
comparison
equal
deleted
inserted
replaced
6:2396a569e446 | 7:5589d865af7a |
---|---|
1 xquery version "1.0"; | |
2 | |
3 import module namespace functx = "http://www.functx.com" at "util/functx.xql"; | |
4 import module namespace mpdl-time = "http://www.mpiwg-berlin.mpg.de/ns/mpdl/util/time" at "util/time.xql"; | |
5 import module namespace mpdl-lucene = "http://www.mpiwg-berlin.mpg.de/ns/mpdl/lucene/search" at "lucene/search.xql"; | |
6 | |
7 declare namespace request="http://exist-db.org/xquery/request"; | |
8 | |
9 declare namespace echo="http://www.mpiwg-berlin.mpg.de/ns/echo/1.0/"; | |
10 declare namespace dc="http://purl.org/dc/elements/1.1/"; | |
11 declare namespace dct="http://purl.org/dc/terms/1.0/"; | |
12 declare namespace rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"; | |
13 | |
14 | |
15 let $queryType := request:get-parameter("query-type", "") | |
16 let $docbase := request:get-parameter("docbase", "") | |
17 let $attribute1 := request:get-parameter("attribute1", "") | |
18 let $relOp1 := request:get-parameter("relOp1", "") | |
19 let $attrQuery1 := request:get-parameter("attr-query1", "") | |
20 let $boolOp := request:get-parameter("boolOp", "") | |
21 let $attribute2 := request:get-parameter("attribute2", "") | |
22 let $relOp2 := request:get-parameter("relOp2", "") | |
23 let $attrQuery2 := request:get-parameter("attr-query2", "") | |
24 let $ftQuery := request:get-parameter("ft-query", "") | |
25 let $ftMorphQuery := request:get-parameter("ft-morph-query", "") | |
26 let $orderBy := request:get-parameter("order-by", "") | |
27 let $language := request:get-parameter("language", "") | |
28 let $pn := number(request:get-parameter("pn", "")) | |
29 let $reqPageSize := request:get-parameter("pageSize", "") | |
30 let $pageSize := | |
31 if ($reqPageSize = '' or $reqPageSize = '0') | |
32 then 20 | |
33 else number($reqPageSize) | |
34 | |
35 let $currentTimeBegin := util:system-time() | |
36 | |
37 let $docPathStandard := "/db/mpdl/documents/standard" | |
38 let $docPathMorph := "/db/mpdl/documents/morph" | |
39 let $docPath := | |
40 if($queryType = 'fulltextMorph') | |
41 then $docPathMorph | |
42 else $docPathStandard | |
43 | |
44 let $docBaseArch := "archimedes" | |
45 let $docBaseEcho := "echo" | |
46 let $fulltextMorphArchDocPath := concat($docPathMorph, "/", $docBaseArch, "/", $language) | |
47 let $fulltextMorphEchoDocPath := concat($docPathMorph, "/", $docBaseEcho, "/", $language) | |
48 let $fulltextMorphCollection := | |
49 if(contains($docbase, $docBaseArch) and contains($docbase, $docBaseEcho)) | |
50 then collection($fulltextMorphArchDocPath, $fulltextMorphEchoDocPath) | |
51 else if(contains($docbase, $docBaseArch) and not(contains($docbase, $docBaseEcho))) | |
52 then collection($fulltextMorphArchDocPath) | |
53 else if(not(contains($docbase, $docBaseArch)) and contains($docbase, $docBaseEcho)) | |
54 then collection($fulltextMorphEchoDocPath) | |
55 else () | |
56 | |
57 let $fulltextStandardArchDocPath := concat($docPathStandard, "/", $docBaseArch) | |
58 let $fulltextStandardEchoDocPath := concat($docPathStandard, "/", $docBaseEcho) | |
59 let $fulltextStandardCollectionStr := | |
60 if(contains($docbase, $docBaseArch) and contains($docbase, $docBaseEcho)) | |
61 then concat("collection('", $fulltextStandardArchDocPath, "', '", $fulltextStandardEchoDocPath, "')") | |
62 else if(contains($docbase, $docBaseArch) and not(contains($docbase, $docBaseEcho))) | |
63 then concat("collection('", $fulltextStandardArchDocPath, "')") | |
64 else if(not(contains($docbase, $docBaseArch)) and contains($docbase, $docBaseEcho)) | |
65 then concat("collection('", $fulltextStandardEchoDocPath, "')") | |
66 else "" | |
67 let $metadataStr := concat("(", $fulltextStandardCollectionStr, "/archimedes/info", "|" , $fulltextStandardCollectionStr, "/echo:echo/echo:metadata", ")") | |
68 let $fulltextStandardCollection := | |
69 if($fulltextStandardCollectionStr != "") | |
70 then util:eval($fulltextStandardCollectionStr) | |
71 else () | |
72 | |
73 let $isAttributeSearch := | |
74 if ($queryType = "attribute") | |
75 then true() | |
76 else false() | |
77 let $isSimpleAttributeSearch := | |
78 if ($queryType = "attribute" and $attrQuery2 = "") | |
79 then true() | |
80 else false() | |
81 let $isBooleanAttributeSearch := | |
82 if ($queryType = "attribute" and $attrQuery2 != "") | |
83 then true() | |
84 else false() | |
85 let $errorAttributeSearch := | |
86 if ($docbase = "") | |
87 then "No document check box is selected. Please select at least one of the document check boxes." | |
88 else if ($queryType = "attribute" and $attrQuery1 = "" and $attrQuery2 != "") | |
89 then "Your first attribute field is empty but your second not. Please use the first attribute field for a simple attribute query." | |
90 else if ($queryType = "attribute" and $attrQuery1 = "" and $attrQuery2 = "") | |
91 then "Your attribute fields are both empty. Please fill at least one attribute field." | |
92 else if ($queryType = "fulltext" and $ftQuery = "") | |
93 then "Your fulltext query field is empty. Please fill this field." | |
94 else if ($queryType = "fulltextMorph" and $ftMorphQuery = "") | |
95 then "Your fulltext query field is empty. Please fill this field." | |
96 else "" | |
97 | |
98 (: TODO: performance improvement: at this time the result is fully scanned 3 times (query, ordering, presentation) :) | |
99 let $attrQueryResult := | |
100 if ($queryType = "browse" and $errorAttributeSearch = "") | |
101 then $fulltextStandardCollection | |
102 else if ($queryType = "fulltext" and $errorAttributeSearch = "") | |
103 then mpdl-lucene:search($fulltextStandardCollection, $ftQuery) | |
104 else if ($queryType = "fulltextMorph" and $errorAttributeSearch = "") | |
105 then mpdl-lucene:search($fulltextMorphCollection, $ftMorphQuery) | |
106 else if ($isAttributeSearch and $errorAttributeSearch = "") | |
107 then mpdl-lucene:attrSearch($metadataStr, $attribute1, $attrQuery1, $boolOp, $attribute2, $attrQuery2) | |
108 else () | |
109 | |
110 let $orderedAttrQueryResult := | |
111 if ($queryType = "fulltext" or $queryType = "fulltextMorph") | |
112 then | |
113 (for $attrElem in $attrQueryResult | |
114 order by ft:score($attrElem) descending | |
115 return $attrElem) | |
116 else mpdl-lucene:order($attrQueryResult, $orderBy) | |
117 | |
118 let $countResult := count($orderedAttrQueryResult) | |
119 let $countPagesTemp := $countResult idiv $pageSize + 1 | |
120 let $countPages := | |
121 if((($countResult - 1) idiv $pageSize + 1) = ($countPagesTemp - 1)) | |
122 then $countPagesTemp - 1 (: if countResult is exactly 10, 20, 30, ... then 1 has to be subtracted :) | |
123 else $countPagesTemp | |
124 let $pnLeftNumber := | |
125 if ($pn > 1) | |
126 then $pn - 1 | |
127 else $pn | |
128 let $pnRightNumber := | |
129 if ($pn < $countPages) | |
130 then $pn + 1 | |
131 else $pn | |
132 let $positionFrom := (($pn - 1) * $pageSize) + 1 | |
133 let $positionTo := | |
134 if ($pn = $countPages) | |
135 then $countResult | |
136 else $pn * $pageSize | |
137 | |
138 let $queryUrlPart := | |
139 if ($queryType = "fulltext") | |
140 then concat("&query-type=", $queryType, "&query=", $ftQuery) | |
141 else if ($queryType = "fulltextMorph") | |
142 then concat("&query-type=", $queryType, "&query=", $ftMorphQuery) | |
143 else "" | |
144 | |
145 let $mode := | |
146 if ($queryType = "fulltext" or $queryType = "fulltextMorph") | |
147 then "text" | |
148 else "image" | |
149 | |
150 let $pageResult := | |
151 for $elem at $pos in $orderedAttrQueryResult | |
152 let $doc := $elem/fn:root() | |
153 let $documentUriOrig := document-uri($doc) | |
154 let $documentUri := substring-after($documentUriOrig, $docPath) | |
155 let $documentUriWithoutExtension := substring-before($documentUri, ".") | |
156 let $docBase := substring-before(substring-after($documentUri, "/"), "/") | |
157 let $metadata := mpdl-lucene:getMetadata($docBase, $doc) | |
158 (: Performance: following is slow: why (would be better structured code) ? | |
159 let $attrAuthorStr := mpdl-lucene:getElemNameByAttr($docBase, "author") | |
160 let $author := mpdl-lucene:getElemDynamic($metadataElem, $attrAuthorStr) | |
161 :) | |
162 let $authorElems := mpdl-lucene:getElementsByAttr($metadata, $docBase, "author") | |
163 let $titleElems := mpdl-lucene:getElementsByAttr($metadata, $docBase, "title") | |
164 let $placeElems := mpdl-lucene:getElementsByAttr($metadata, $docBase, "place") | |
165 let $dateElems := mpdl-lucene:getElementsByAttr($metadata, $docBase, "date") | |
166 let $authors := string-join($authorElems, ', ') | |
167 let $titles := string-join($titleElems, ', ') | |
168 let $places := string-join($placeElems, ', ') | |
169 let $dates := string-join($dateElems, ', ') | |
170 let $resultElem := | |
171 <tr> | |
172 <td valign="top" style="padding-left:5px;">{$pos}.</td> | |
173 <td align="middle" valign="top" style="padding-left:7px;"><a href="interface/echo/echoDocuView.xql?document={$documentUri}"><img src="images/book.png" width="15" height="15" border="0"/></a> </td> | |
174 <td align="middle" valign="top" style="padding-left:7px;"><a href="page-query-result.xql?document={$documentUri}&mode={$mode}{$queryUrlPart}"><img src="images/book.png" width="15" height="15" border="0"/></a> </td> | |
175 <td align="middle" valign="top"><a href="getDoc?doc={$documentUri}" target="_blank"><img src="images/download.png" width="15" height="15" border="0" alt="Download"/></a></td> | |
176 <td align="middle" valign="top"><a href="getDoc?doc={$documentUriWithoutExtension}.pdf"><img src="images/download.png" width="15" height="15" border="0" alt="Download"/></a></td> | |
177 <td align="middle" valign="top"><a href="getDoc?doc={$documentUriWithoutExtension}.html" target="_blank"><img src="images/download.png" width="15" height="15" border="0" alt="Download"/></a></td> | |
178 <td valign="top" style="padding-left:5px;"><i>{$authors}</i></td> | |
179 <td valign="top" style="padding-left:5px;">{$titles}</td> | |
180 <td valign="top" style="padding-left:5px;">{$places}</td> | |
181 <td valign="top" style="padding-left:5px;">{$dates}</td> | |
182 </tr> | |
183 where $pos >= $positionFrom and $pos <= $positionTo | |
184 return $resultElem | |
185 | |
186 let $docBases := string-join($docbase, ';') | |
187 let $title := | |
188 if ($queryType = "browse") | |
189 then concat("Result of your query: documentBases=", $docBases, ", browse all, orderBy=", $orderBy) | |
190 else if ($queryType = "attribute" and $isSimpleAttributeSearch) | |
191 then concat("Result of your query: documentBases=", $docBases, ', "', $attribute1, '"', " ", $relOp1, " ", '"', $attrQuery1, '"', ", orderBy=", '"', $orderBy, '"') | |
192 else if ($queryType = "attribute" and $isBooleanAttributeSearch) | |
193 then concat("Result of your query: documentBases=", $docBases, ', "', $attribute1, '"', " ", $relOp1, " ", '"', $attrQuery1, '"', $boolOp, '"', $attribute2, '"', " ", $relOp2, " ", '"', $attrQuery2, '"', ", orderBy=", '"', $orderBy, '"') | |
194 else if ($queryType = "fulltext") | |
195 then concat("Result of your fulltext query: documentBases=", $docBases, '"', $ftQuery, ', "', ", orderBy=", '"', "relevance", '"') | |
196 else if ($queryType = "fulltextMorph") | |
197 then concat("Result of your morphological fulltext query: documentBases=", $docBases, ', "', $ftMorphQuery, '"', ", language=", '"', $language, '"', ", orderBy=", '"', "relevance", '"') | |
198 else () | |
199 | |
200 let $resultHeaderTable := | |
201 <table width="100%"> | |
202 <colgroup> | |
203 <col width="85%"/> | |
204 <col width="3%"/> | |
205 <col width="3%"/> | |
206 <col width="3%"/> | |
207 <col width="3%"/> | |
208 </colgroup> | |
209 <tr> | |
210 <td align="left">{$positionFrom} - {$positionTo} of {$countResult} documents</td> | |
211 <form action="attribute-query-result.xql" method="get"> | |
212 <input type="hidden" name="docbase" value="{$docbase}"/> | |
213 <input type="hidden" name="query-type" value="{$queryType}"/> | |
214 <input type="hidden" name="attribute1" value="{$attribute1}"/> | |
215 <input type="hidden" name="relOp1" value="{$relOp1}"/> | |
216 <input type="hidden" name="attr-query1" value="{$attrQuery1}"/> | |
217 <input type="hidden" name="boolOp" value="{$boolOp}"/> | |
218 <input type="hidden" name="attribute2" value="{$attribute2}"/> | |
219 <input type="hidden" name="relOp2" value="{$relOp2}"/> | |
220 <input type="hidden" name="attr-query2" value="{$attrQuery2}"/> | |
221 <input type="hidden" name="order-by" value="{$orderBy}"/> | |
222 <input type="hidden" name="ft-query" value="{$ftQuery}"/> | |
223 <input type="hidden" name="ft-morph-query" value="{$ftMorphQuery}"/> | |
224 <input type="hidden" name="language" value="{$language}"/> | |
225 <td><button id="buttonLeftNumber" name="pn" value="{$pnLeftNumber}" style="background:none;border:none;"><img src="images/left.gif"/></button></td> | |
226 <td nowrap="true">{$pn} / {$countPages}</td> | |
227 <td><button id="buttonRightNumber" name="pn" value="{$pnRightNumber}" style="background:none;border:none;"><img src="images/right.gif"/></button></td> | |
228 </form> | |
229 <form action="attribute-query-result.xql" method="get"> | |
230 <input type="hidden" name="docbase" value="{$docbase}"/> | |
231 <input type="hidden" name="query-type" value="{$queryType}"/> | |
232 <input type="hidden" name="attribute1" value="{$attribute1}"/> | |
233 <input type="hidden" name="relOp1" value="{$relOp1}"/> | |
234 <input type="hidden" name="attr-query1" value="{$attrQuery1}"/> | |
235 <input type="hidden" name="boolOp" value="{$boolOp}"/> | |
236 <input type="hidden" name="attribute2" value="{$attribute2}"/> | |
237 <input type="hidden" name="relOp2" value="{$relOp2}"/> | |
238 <input type="hidden" name="attr-query2" value="{$attrQuery2}"/> | |
239 <input type="hidden" name="order-by" value="{$orderBy}"/> | |
240 <input type="hidden" name="ft-query" value="{$ftQuery}"/> | |
241 <input type="hidden" name="ft-morph-query" value="{$ftMorphQuery}"/> | |
242 <input type="hidden" name="language" value="{$language}"/> | |
243 <td nowrap="true">Page: <input type="text" size="3" name="pn" value="{$pn}"/></td> | |
244 </form> | |
245 </tr> | |
246 </table> | |
247 | |
248 let $attrQueryResultError := string($attrQueryResult/error) | |
249 let $resultHeader := | |
250 if ($attrQueryResultError = '' and $countResult > 0) | |
251 then $resultHeaderTable | |
252 else if ($attrQueryResultError != '') | |
253 then (<b>Your query delivers an error: </b>, $attrQueryResultError) | |
254 else if ($errorAttributeSearch != '') | |
255 then (<b>Your query delivers an error: </b>, $errorAttributeSearch) | |
256 else (<b>Your query delivers no result</b>) | |
257 | |
258 let $resultPageBody := | |
259 if ($attrQueryResultError = '' and $countResult > 0) | |
260 then | |
261 <form action="attribute-query-result.xql" method="get"> | |
262 <input type="hidden" name="docbase" value="{$docbase}"/> | |
263 <input type="hidden" name="query-type" value="{$queryType}"/> | |
264 <input type="hidden" name="attribute1" value="{$attribute1}"/> | |
265 <input type="hidden" name="relOp1" value="{$relOp1}"/> | |
266 <input type="hidden" name="attr-query1" value="{$attrQuery1}"/> | |
267 <input type="hidden" name="boolOp" value="{$boolOp}"/> | |
268 <input type="hidden" name="attribute2" value="{$attribute2}"/> | |
269 <input type="hidden" name="relOp2" value="{$relOp2}"/> | |
270 <input type="hidden" name="attr-query2" value="{$attrQuery2}"/> | |
271 <input type="hidden" name="ft-query" value="{$ftQuery}"/> | |
272 <input type="hidden" name="ft-morph-query" value="{$ftMorphQuery}"/> | |
273 <input type="hidden" name="language" value="{$language}"/> | |
274 <input type="hidden" name="pn" value="{$pn}"/> | |
275 <table width="100%" border="2" rules="groups"> | |
276 <colgroup> | |
277 <col width="2%"/> | |
278 <col width="3%"/> | |
279 <col width="3%"/> | |
280 <col width="3%"/> | |
281 <col width="3%"/> | |
282 <col width="3%"/> | |
283 <col width="15%"/> | |
284 <col width="42%"/> | |
285 <col width="15%"/> | |
286 <col width="6%"/> | |
287 </colgroup> | |
288 <thead> | |
289 <tr> | |
290 <th align="left" valign="top"> | |
291 <button id="dummy" name="order-by" value="{$orderBy}" style="padding:0px;font-weight:bold;font-size:14px;background:none;border:none;">No.</button> | |
292 </th> | |
293 <th align="left" valign="top"><button id="dummy" name="order-by" value="{$orderBy}" style="padding:0px;font-weight:bold;font-size:14px;background:none;border:none;">Full view</button></th> | |
294 <th align="left" valign="top"><button id="dummy" name="order-by" value="{$orderBy}" style="padding:0px;font-weight:bold;font-size:14px;background:none;border:none;">Lite view</button></th> | |
295 <th align="left" valign="top"><button id="dummy" name="order-by" value="author" style="padding:0px;font-weight:bold;font-size:14px;background:none;border:none;">Xml</button></th> | |
296 <th align="left" valign="top"><button id="dummy" name="order-by" value="author" style="padding:0px;font-weight:bold;font-size:14px;background:none;border:none;">Pdf</button></th> | |
297 <th align="left" valign="top"><button id="dummy" name="order-by" value="author" style="padding:0px;font-weight:bold;font-size:14px;background:none;border:none;">Html</button></th> | |
298 <th align="left" valign="top"> | |
299 <button name="order-by" value="author" style="padding:0px;font-weight:bold;font-size:14px;background:none;border:none;">Author</button> | |
300 </th> | |
301 <th align="left" valign="top"> | |
302 <button name="order-by" value="title" style="padding:0px;font-weight:bold;font-size:14px;background:none;border:none;">Title</button> | |
303 </th> | |
304 <th align="left" valign="top"> | |
305 <button name="order-by" value="place" style="padding:0px;font-weight:bold;font-size:14px;background:none;border:none;">Place</button> | |
306 </th> | |
307 <th align="left" valign="top"> | |
308 <button name="order-by" value="date" style="padding:0px;font-weight:bold;font-size:14px;background:none;border:none;">Year</button> | |
309 </th> | |
310 </tr> | |
311 </thead> | |
312 <tbody> | |
313 <tr/> | |
314 {$pageResult} | |
315 </tbody> | |
316 </table> | |
317 </form> | |
318 else () | |
319 | |
320 let $currentTimeEnd := util:system-time() | |
321 let $neededTime := mpdl-time:duration-as-ms($currentTimeEnd - $currentTimeBegin) | |
322 | |
323 return | |
324 <html> | |
325 <head> | |
326 <title>{$title}</title> | |
327 </head> | |
328 <body> | |
329 <p style="font-weight:bold;font-size:20px">{$title}</p> | |
330 {$resultHeader} | |
331 {$resultPageBody} | |
332 <p/> | |
333 Elapsed time: {$neededTime} ms, Back to <a href="query.xql">query page</a>, see the <a href="attribute-query-result.xql?_source=yes">XQuery source</a> of this page | |
334 <p/> | |
335 </body> | |
336 </html> |