Version 4.1 par Florent Charton le 2026/01/08 10:56

Afficher les derniers auteurs
1 {{velocity output="false"}}
2 #template('hierarchy_macros.vm')
3
4 #**
5 * Macro to get the results of a livetable data call.
6 * This page is called from live grids via Ajax with the argument xpage=plain. It returns a
7 * set of results serialized in JSON.
8 *#
9 #macro(gridresult $className $collist)
10 #gridresultwithfilter($className $collist '' '' {})
11 #end
12
13
14 #**
15 * Computes the query used to retrieve the results of a live table data call.
16 * NOTE: This macro is not fully encapsulated because it defines a few Velocity variables that are used in subsequent macros.
17 *#
18 #macro(gridresultwithfilter_buildQuery $className $collist $filterfrom $filterwhere $filterParams)
19 ## Additional columns; should *not* contain raw parameters, all added column names must be filtered
20 #set($fromSql = '')
21 ## Parametrized filter part of the query
22 #set($whereSql = '')
23 ## List of values to use with $whereSql
24 #if (!$filterParams)
25 #set($filterParams = {})
26 #end
27 #if ($filterParams.entrySet())
28 #set($whereParams = {})
29 #else
30 #set($whereParams = [])
31 #end
32 #set($class = $xwiki.getDocument($className).getxWikiClass())
33 ##
34 ## Add the columns needed for the actual data
35 ##
36 #set($tablelist = [])
37 #foreach($colname in $collist)
38 ## If a classname is defined and the class field corresponding to the column name,
39 ## we check the type of the field and skip it if it's Password.
40 #if ($className != '' && $class.get($colname))
41 #set ($isPasswordType = $class.get($colname).classType == 'Password')
42 #set ($isEmailType = $class.get($colname).classType == 'Email')
43 #set ($emailObfuscated = $services.mail.general.shouldObfuscate())
44 #if (!($isPasswordType || ($isEmailType && $emailObfuscated)))
45 #livetable_addColumnToQuery($colname)
46 #end
47 #else
48 #livetable_addColumnToQuery($colname)
49 #end
50 #end
51 ##
52 ## Tag filtering
53 ##
54 #if($request.tag)
55 #set($fromSql = "${fromSql} , BaseObject as tobject, DBStringListProperty as tagprop")
56 #set($whereSql = "${whereSql} and tobject.className='XWiki.TagClass' and tobject.name=doc.fullName and tobject.id=tagprop.id.id and tagprop.id.name='tags' and (")
57 #foreach($tag in $request.getParameterValues('tag'))
58 #if($foreach.count > 1) #set($whereSql = "${whereSql} and ") #end
59 ## Tags are case insensitive but they are stored unchanged which means we have to normalize them when performing
60 ## a query. Unfortunately there's no simple way to match multiple tags (AND operator). If we join the list of
61 ## tags in the FROM clause then we match at least one of the tags (OR operator). The only remaining option is to
62 ## check that the list of tags contains each of the desired tags. HQL doesn't help us to lower-case the entire
63 ## list of tags so we use an inner select for this.
64 #if ($whereParams.entrySet())
65 #set($whereSql = "${whereSql} lower(:wikitag${foreach.count}) in (select lower(tag) from tagprop.list tag)")
66 #set($discard = $whereParams.put("wikitag$foreach.count", "${tag}"))
67 #else
68 #set($whereSql = "${whereSql} lower(?) in (select lower(tag) from tagprop.list tag)")
69 #set($discard = $whereParams.add("${tag}"))
70 #end
71 #end
72 #set($whereSql = "${whereSql})")
73 #end
74 ##
75 ##
76 ## Order
77 ##
78 ## if the object for the classname of the order column is not already in the from sql, put it
79 #macro(addObjectClause $objectAlias)
80 #if($fromSql.indexOf($objectAlias) < 0)
81 #set($fromSql = "${fromSql}, BaseObject $objectAlias")
82 #if ($whereParams.entrySet())
83 #set($whereSql = "${whereSql} and ${objectAlias}.name = doc.fullName and ${objectAlias}.className = :${objectAlias}_className")
84 #set($discard = $whereParams.put("${objectAlias}_className", $propClassName))
85 #else
86 #set($whereSql = "${whereSql} and ${objectAlias}.name = doc.fullName and ${objectAlias}.className = ?")
87 #set($discard = $whereParams.add($propClassName))
88 #end
89 #end
90 #end
91 ## Set the order clause for a field. We first ignore the case using the lower function (so that e.g. 'aaa' equals 'AAA')
92 ## but then consider it only for equal values (so that e.g. 'AAA' comes before 'aaa').
93 #macro(setOrderClause $fieldName $direction $useRawValue)
94 #if ($useRawValue)
95 #set($orderSql = " order by ${fieldName} ${direction}")
96 #else
97 #set($orderSql = " order by lower(${fieldName}) ${direction}, ${fieldName} ${direction}")
98 #end
99 #end
100 #set($order = "$!request.sort")
101 #if ($order == 'doc.location')
102 #set ($order = 'doc.fullName')
103 #elseif ($order == 'email' && $services.mail.general.shouldObfuscate())
104 #set ($order = '')
105 #end
106 #set ($orderSql = '')
107 #if($order != '')
108 #set($orderDirection = "$!{request.get('dir').toLowerCase()}")
109 #if("$!orderDirection" != '' && "$!orderDirection" != 'asc')
110 #set($orderDirection = 'desc')
111 #end
112 #livetable_getTableAlias($order)
113 #if($order.startsWith('doc.'))
114 ## The column is a document field.
115 ##
116 ## These document fields need to be ordered as raw values and not as strings.
117 #set($rawDocumentFields = ['translation', 'date', 'contentUpdateDate', 'creationDate', 'elements', 'minorEdit1', 'hidden'])
118 #set($documentField = $stringtool.removeStart($order, 'doc.'))
119 #setOrderClause(${safe_tableAlias.replace('_','.')}, ${orderDirection}, $rawDocumentFields.contains($documentField))
120 #else
121 ## The column is an object property.
122 ##
123 ## Resolve the property.
124 #livetable_getPropertyClassAndType($order)
125 #set ($multiselect = "$!{propClass.get($order).getProperty('multiSelect').getValue()}")
126 ## We can only handle single values, not multiselect ones.
127 #if ($multiselect != '1')
128 ## Some property types do not need lowercasing since they have unique values by design, so we use the raw values to order.
129 #set($rawPropertyTypes = ['NumberClass', 'BooleanClass', 'DateClass', 'LevelsClass'])
130 ## If the order column is also a filer column, this means that it was already added to the query and all we need to do is to add it to the order clause.
131 #if(!$tablelist.contains($order))
132 ## The order column is not also a filter column, so not yet defined in the query.
133 ## We need to first define it (to the from and where clauses) before we can add it to the order clause.
134 ##
135 ## Resolve the table name of the property to be used in the from clause below.
136 #livetable_getTableName($order)
137 ## If the sort column has a _class specified, join that object in
138 #set($orderObjectAlias = 'obj')
139 #if($propClassName != '' && "$!propClass" != '')
140 ## prepare the alias of the BaseObject table that corresponds to the class of this column
141 #set($orderObjectAlias = "$!{propClassName.replaceAll('[^a-zA-Z0-9_]', '')}_obj")
142 #addObjectClause($orderObjectAlias)
143 #end
144 #set($fromSql = "${fromSql}, ${tableName} ${safe_tableAlias}")
145 ## FIXME: Check if this is indeed a property of the class. Hint: $propType can be used.
146 ## Conditions are put on the object coresponding to the column of the order ($orderObjectAlias), which depends on which is the class of the $order
147 #if ($whereParams.entrySet())
148 #set($whereSql = "${whereSql} and ${orderObjectAlias}.id=${safe_tableAlias}.id.id and ${safe_tableAlias}.name = :${safe_tableAlias}_name")
149 #set($discard = $whereParams.put("${safe_tableAlias}_name", "${order}"))
150 #else
151 #set($whereSql = "${whereSql} and ${orderObjectAlias}.id=${safe_tableAlias}.id.id and ${safe_tableAlias}.name = ?")
152 #set($discard = $whereParams.add("${order}"))
153 #end
154 #end
155 ## Add the column to the order clause.
156 #setOrderClause("${safe_tableAlias}.value", ${orderDirection}, $rawPropertyTypes.contains($propType))
157 #end
158 #end
159 #end
160 ##
161 ##
162 ## Compute the final queries
163 ##
164 #if ($filterParams.entrySet())
165 #set($sqlParams = {})
166 #set($tagsMatchingParams = {})
167 #set($allMatchingParams = {})
168 #else
169 #set($sqlParams = [])
170 #set($tagsMatchingParams = [])
171 #set($allMatchingParams = [])
172 #end
173 #if("$!className" != '')
174 ## Class query
175 #if ($sqlParams.entrySet())
176 #set($sql = ", BaseObject as obj $!fromSql $!filterfrom where obj.name=doc.fullName and obj.className = :className and doc.fullName not in (:classTemplate1, :classTemplate2) $!whereSql $!filterwhere")
177 #set($discard = $sqlParams.put('className', "${className}"))
178 #set($discard = $sqlParams.put('classTemplate1', "${className}Template"))
179 #set($discard = $sqlParams.put('classTemplate2', ${className.replaceAll('Class$', 'Template')}))
180 #set($discard = $sqlParams.putAll($whereParams))
181 #else
182 #set($sql = ", BaseObject as obj $!fromSql $!filterfrom where obj.name=doc.fullName and obj.className = ? and doc.fullName not in (?, ?) $!whereSql $!filterwhere")
183 #set($discard = $sqlParams.addAll(["${className}", "${className}Template", ${className.replaceAll('Class$', 'Template')}]))
184 #set($discard = $sqlParams.addAll($whereParams))
185 #end
186 ##
187 #set($tagsMatchingFiltersFrom = ", BaseObject as obj $!fromSql $!filterfrom")
188 #if ($tagsMatchingParams.entrySet())
189 #set($tagsMatchingFiltersWhere = "obj.name=doc.fullName and obj.className = :className and doc.fullName not in (:classTemplate1, :classTemplate2) $!whereSql $!filterwhere")
190 #set($discard = $tagsMatchingParams.put('className', "${className}"))
191 #set($discard = $tagsMatchingParams.put('classTemplate1', "${className}Template"))
192 #set($discard = $tagsMatchingParams.put('classTemplate2', ${className.replaceAll('Class$', 'Template')}))
193 #set($discard = $tagsMatchingParams.putAll($whereParams))
194 #else
195 #set($tagsMatchingFiltersWhere = "obj.name=doc.fullName and obj.className = ? and doc.fullName not in (?, ?) $!whereSql $!filterwhere")
196 #set($discard = $tagsMatchingParams.addAll(["${className}", "${className}Template", ${className.replaceAll('Class$', 'Template')}]))
197 #set($discard = $tagsMatchingParams.addAll($whereParams))
198 #end
199 ##
200 #set($allMatchingTagsFrom = ", BaseObject as obj $!filterfrom")
201 #if ($allMatchingParams.entrySet())
202 #set($allMatchingTagsWhere = "obj.name=doc.fullName and obj.className = :className and doc.fullName not in (:classTemplate1, :classTemplate2) $!filterwhere")
203 #set($discard = $allMatchingParams.put('className', "${className}"))
204 #set($discard = $allMatchingParams.put('classTemplate1', "${className}Template"))
205 #set($discard = $allMatchingParams.put('classTemplate2', ${className.replaceAll('Class$', 'Template')}))
206 #else
207 #set($allMatchingTagsWhere = "obj.name=doc.fullName and obj.className = ? and doc.fullName not in (?, ?) $!filterwhere")
208 #set($discard = $allMatchingParams.addAll(["${className}", "${className}Template", ${className.replaceAll('Class$', 'Template')}]))
209 #end
210 ##
211 #if($filterParams)
212 #if ($filterParams.entrySet())
213 #set($discard = $sqlParams.putAll($filterParams))
214 #set($discard = $tagsMatchingParams.putAll($filterParams))
215 #set($discard = $allMatchingParams.putAll($filterParams))
216 #else
217 #set($discard = $sqlParams.addAll($filterParams))
218 #set($discard = $tagsMatchingParams.addAll($filterParams))
219 #set($discard = $allMatchingParams.addAll($filterParams))
220 #end
221 #end
222 #else
223 ## Document query
224 #set($sql = "$!fromSql $!filterfrom where 1=1 $!whereSql $!filterwhere")
225 #if ($whereParams.entrySet())
226 #set($discard = $sqlParams.putAll($whereParams))
227 #else
228 #set($discard = $sqlParams.addAll($whereParams))
229 #end
230 ##
231 #set($tagsMatchingFiltersFrom = "$!fromSql $!filterfrom")
232 #set($tagsMatchingFiltersWhere = "1=1 $!whereSql $!filterwhere")
233 #if ($whereParams.entrySet())
234 #set($discard = $tagsMatchingParams.putAll($whereParams))
235 #else
236 #set($discard = $tagsMatchingParams.addAll($whereParams))
237 #end
238 ##
239 #set($allMatchingTagsFrom = "$!filterfrom")
240 #set($allMatchingTagsWhere = "1=1 $!filterwhere")
241 ##
242 #if($filterParams)
243 #if ($filterParams.entrySet())
244 #set($discard = $sqlParams.putAll($filterParams))
245 #set($discard = $tagsMatchingParams.putAll($filterParams))
246 #set($discard = $allMatchingParams.putAll($filterParams))
247 #else
248 #set($discard = $sqlParams.addAll($filterParams))
249 #set($discard = $tagsMatchingParams.addAll($filterParams))
250 #set($discard = $allMatchingParams.addAll($filterParams))
251 #end
252 #end
253 #end
254 #if($orderSql != '')
255 #set($sql = "$sql $!{orderSql}")
256 #end
257 #end
258 #**
259 * Adds TagCloud information to the JSON returned by a live table data call.
260 * NOTE: This macro uses Velocity variables defined by gridresultwithfilter_buildQuery.
261 *
262 * @param $map stores the JSON in memory so that it can be adjusted before serialization
263 *#
264 #macro(gridresult_buildTagCloudJSON $map)
265 ##
266 ## TagCloud matching the current filters
267 ##
268 #set($tagsMatchingFilters = $xwiki.tag.getTagCountForQuery($tagsMatchingFiltersFrom, $tagsMatchingFiltersWhere, $tagsMatchingParams))
269 ## FIXME: We use a map just because the client expects an object, but all we really need is a list..
270 #set($matchingTags = {})
271 #foreach($tag in $tagsMatchingFilters.keySet())
272 ## NOTE: The value doesn't have a special meaning. I've used 1 just because it takes less space when serialized.
273 #set($discard = $matchingTags.put($tag, 1))
274 #end
275 #set($discard = $map.put('matchingtags', $matchingTags))
276 ##
277 ## TagCloud matching all the documents used by the live table
278 ##
279 ## If all the query parameters are the same as for $tagsMatchingFilters, no need to run the query again.
280 ## This optimization allows to divide the time to compute the tagcloud by 2 when the table has no filters applied.
281 #if ($allMatchingTagsFrom.trim() != $tagsMatchingFiltersFrom.trim() || $allMatchingTagsWhere.trim() != $tagsMatchingFiltersWhere.trim() || $tagsMatchingParams != $allMatchingParams)
282 #set($allMatchingTags = $xwiki.tag.getTagCountForQuery($allMatchingTagsFrom, $allMatchingTagsWhere, $allMatchingParams))
283 #else
284 #set($allMatchingTags = $tagsMatchingFilters)
285 #end
286 ## FIXME: We use a list of maps just because the client expects an array, but we should simply return $allMatchingTags..
287 #set($tags = [])
288 #foreach($tag in $allMatchingTags.keySet())
289 #set($discard = $tags.add({'tag': $tag, 'count': $allMatchingTags.get($tag)}))
290 #end
291 #set($discard = $map.put('tags', $tags))
292 #end
293
294
295 #**
296 * Adds information about each live table row to the JSON returned by a live table data call.
297 * NOTE: This macro uses Velocity variables defined by gridresultwithfilter_buildQuery.
298 *
299 * @param $map stores the JSON in memory so that it can be adjusted before serialization
300 *#
301 #macro(gridresult_buildRowsJSON $map)
302 #set($offset = $numbertool.toNumber($request.get('offset')).intValue())
303 ## Offset starts from 0 in velocity and 1 in javascript
304 #set($offset = $offset - 1)
305 #if(!$offset || $offset < 0)
306 #set($offset = 0)
307 #end
308 #getAndValidateQueryLimitFromRequest('limit', 15, $limit)
309 #set($query = $services.query.hql($sql))
310 ## Apply query filters if defined. Otherwise use default.
311 #foreach ($queryFilter in $stringtool.split($!request.queryFilters, ', '))
312 #set ($query = $query.addFilter($queryFilter))
313 #end
314 #set ($query = $query.setLimit($limit).setOffset($offset).bindValues($sqlParams))
315 #set($items = $query.execute())
316 #set($discard = $map.put('totalrows', $query.count()))
317 #if ($limit > 0)
318 #set($discard = $map.put('returnedrows', $mathtool.min($items.size(), $limit)))
319 #else
320 ## When the limit is 0, it's equivalent to no limit at all and the actual number of returned results can be used.
321 #set($discard = $map.put('returnedrows', $items.size()))
322 #end
323 #set($discard = $map.put('offset', $mathtool.add($offset, 1)))
324 #set($rows = [])
325 #foreach($item in $items)
326 #gridresult_buildRowJSON($item $rows)
327 #end
328 #set ($discard = $map.put('rows', $rows))
329 #livetable_filterObfuscated($map)
330 #end
331
332
333 #**
334 * Adds information about the specified live table row to the JSON returned by a live table data call.
335 * NOTE: This macro uses Velocity variables available in gridresult_buildRowsJSON.
336 *
337 * @param $item the name of the document that feeds this live table row
338 * @param $rows stores the JSON in memory so that it can be adjusted before serialization
339 *#
340 #macro(gridresult_buildRowJSON $item $rows)
341 ## Handle both the case where the "language" filter is used and thus languages are returned too and the case where
342 ## only the document name is returned. When more than the document name is returned the $item variable is a list.
343 #if($item.size())
344 ## Extract doc name and doc language from $item
345 #set($docName = $item[0])
346 #set($docLanguage = $item[1])
347 #else
348 #set($docName = $item)
349 #set($docLanguage = '')
350 #end
351 #set ($docReference = $services.model.resolveDocument($docName))
352 #set ($isViewable = $services.security.authorization.hasAccess('view', $docReference))
353 #if ($isViewable)
354 #set ($row = {
355 'doc_viewable': $isViewable,
356 'doc_fullName': $services.model.serialize($docReference, 'local'),
357 'doc_space': $services.model.serialize($docReference.parent, 'local'),
358 'doc_location': "#hierarchy($docReference, {'limit': 5, 'plain': false, 'local': true, 'displayTitle': false})",
359 'doc_url': $xwiki.getURL($docReference),
360 'doc_space_url': $xwiki.getURL($docReference.parent),
361 'doc_wiki': $docReference.wikiReference.name,
362 'doc_wiki_url': $xwiki.getURL($docReference.wikiReference),
363 'doc_hasadmin': $xwiki.hasAdminRights(),
364 'doc_hasedit': $services.security.authorization.hasAccess('edit', $docReference),
365 'doc_hasdelete': $services.security.authorization.hasAccess('delete', $docReference),
366 'doc_edit_url': $xwiki.getURL($docReference, 'edit'),
367 'doc_copy_url': $xwiki.getURL($docReference, 'view', 'xpage=copy'),
368 'doc_delete_url': $xwiki.getURL($docReference, 'delete'),
369 'doc_rename_url': $xwiki.getURL($docReference, 'view', 'xpage=rename&step=1')
370 })
371 #set ($isTranslation = "$!docLanguage" != '' && $xwiki.getLanguagePreference() != $docLanguage)
372 ## Display the language after the document name so that not all translated documents have the same name displayed.
373 #set ($row.doc_name = "$docReference.name#if ($isTranslation) ($docLanguage)#end")
374 #set ($row.doc_hascopy = $row.doc_viewable)
375 #set ($row.doc_hasrename = $row.doc_hasdelete)
376 #set ($row.doc_hasrights = $row.doc_hasedit && $isAdvancedUser)
377 #if ($docReference.name == 'WebHome')
378
379 ## For nested pages, use the page administration.
380 #set ($webPreferencesReference = $services.model.createDocumentReference(
381 'WebPreferences', $docReference.lastSpaceReference))
382 #set ($row.doc_rights_url = $xwiki.getURL($webPreferencesReference, 'admin',
383 'editor=spaceadmin&section=PageRights'))
384 #else
385 ## For terminal pages, use the old rights editor.
386 ## TODO: We should create a page administration for terminal pages too.
387 #set ($row.doc_rights_url = $xwiki.getURL($docReference, 'edit', 'editor=rights'))
388 #end
389 #if ($row.doc_viewable)
390 #set ($itemDoc = $xwiki.getDocument($docReference))
391 ## Handle translations. We need to make sure we display the data associated to the correct document if the returned
392 ## result is a translation.
393 #if ($isTranslation)
394 #set ($translatedDoc = $itemDoc.getTranslatedDocument($docLanguage))
395 #else
396 #set ($translatedDoc = $itemDoc.translatedDocument)
397 #end
398 #set($discard = $itemDoc.use($className))
399 #set($discard = $row.put('doc_objectCount', $itemDoc.getObjectNumbers($className)))
400 #set($discard = $row.put('doc_edit_url', $itemDoc.getURL($itemDoc.defaultEditMode)))
401 #set($discard = $row.put('doc_date', $xwiki.formatDate($translatedDoc.date)))
402 #set($discard = $row.put('doc_title', $translatedDoc.plainTitle))
403 #set($rawTitle = $translatedDoc.title)
404 #if($rawTitle != $row['doc_title'])
405 #set($discard = $row.put('doc_title_raw', $rawTitle))
406 #end
407 #set ($metadataAuthor = $translatedDoc.authors.originalMetadataAuthor)
408 #if ($metadataAuthor == $services.user.getGuestUserReference())
409 ## Special handling for guest so that it displays unknown user.
410 #set($discard = $row.put('doc_author', $xwiki.getPlainUserName($NULL)))
411 #else
412 #set($discard = $row.put('doc_author', $xwiki.getPlainUserName($metadataAuthor)))
413 #end
414
415 #set($discard = $row.put('doc_author_url', $xwiki.getURL($metadataAuthor)))
416 #set($discard = $row.put('doc_creationDate', $xwiki.formatDate($translatedDoc.creationDate)))
417 #set($discard = $row.put('doc_creator', $xwiki.getPlainUserName($translatedDoc.creatorReference)))
418 #set($discard = $row.put('doc_hidden', $translatedDoc.isHidden()))
419 #foreach($colname in $collist)
420 #gridresult_buildColumnJSON($colname $row)
421 #end
422 #end
423 #else
424 #set ($row = {
425 'doc_viewable': $isViewable,
426 'doc_fullName': 'obfuscated'
427 })
428 #end
429 #set($discard = $rows.add($row))
430 #end
431
432
433 #**
434 * Adds information about the given column to the JSON returned by a live table data call.
435 * NOTE: This macro uses Velocity variables available in gridresult_buildRowJSON.
436 *
437 * @param $colname the name of the live table column for which to retrieve information
438 * @param $row stores the JSON in memory so that it can be adjusted before serialization
439 *#
440 #macro(gridresult_buildColumnJSON $colname $row)
441 #if($colname.startsWith('doc.'))
442 #elseif($colname == '_action')
443 #set($discard = $row.put($colname, $services.localization.render("${request.transprefix}actiontext")))
444 #elseif($colname == '_attachments')
445 #livetable_getAttachmentsList($translatedDoc)
446 #set($discard = $row.put($colname, $attachlist))
447 #elseif($colname == '_avatar')
448 #livetable_getAvatar($itemDoc)
449 #set($discard = $row.put($colname, $avatar))
450 #elseif($colname == '_images')
451 #livetable_getImagesList($itemDoc)
452 #set($discard = $row.put($colname, $imagesList))
453 ## Output likes if they are available.
454 #elseif($colname == '_likes' && "$!services.like" != "")
455 #set($likes = $services.like.getLikes($docReference))
456 #if ($likes.isPresent())
457 #set($discard = $row.put('_likes', $likes.get()))
458 #end
459 #else
460 #livetable_getPropertyClassAndType($colname)
461 #if(!$propClass.equals($class))
462 #set($discard = $itemDoc.use($propClassName))
463 #end
464 #set($fieldObject = $itemDoc.getFirstObject($colname))
465 #set($fieldProperty = $fieldObject.getProperty($colname))
466 #if ($fieldProperty.getPropertyClass().classType == 'Password')
467 #set($fieldValue = '********')
468 #elseif ($fieldProperty.getPropertyClass().classType == 'Email' && $services.mail.general.shouldObfuscate())
469 #set ($fieldValue = $services.mail.general.obfuscate("$!fieldProperty.getValue()"))
470 #else
471 #set($fieldValue = "$!fieldProperty.getValue()")
472 #end
473 #set($fieldDisplayValue = "#unwrapXPropertyDisplay($itemDoc.display($colname, 'view'))")
474 #if($fieldDisplayValue == '')
475 #set($fieldDisplayValue = $services.localization.render("${request.transprefix}emptyvalue"))
476 #end
477 #set($fieldUrl = '')
478 ## Only retrieve an URL for a DBListClass item
479 #if(($propType == 'DBListClass' || $propType == 'PageClass') && $propClass.get($colname).getProperty('multiSelect').value != 1)
480 #set($fieldUrl = $xwiki.getURL($fieldValue))
481 #if($fieldUrl == $xwiki.getURL($services.model.resolveDocument('', 'default', $doc.documentReference.extractReference('WIKI'))))
482 #set($fieldUrl = '')
483 #end
484 #end
485 #set($discard = $row.put($colname, $fieldDisplayValue))
486 #set($discard = $row.put("${colname}_value", $fieldValue))
487 #set($discard = $row.put("${colname}_url", $fieldUrl))
488 ## Reset to the default class
489 #set($discard = $itemDoc.use($className))
490 #end
491 #end
492
493
494 #**
495 * Builds the JSON response to a live table data call.
496 *
497 * @param $map stores the JSON in memory so that it can be adjusted before serialization
498 *#
499 #macro(gridresultwithfilter_buildJSON $className $collist $filterfrom $filterwhere $filterParams $map)
500 #gridresultwithfilter_buildQuery($className $collist $filterfrom $filterwhere $filterParams)
501 #if("$!request.sql" == '1')
502 #set($discard = $map.put('sql', $sql))
503 #set($discard = $map.put('params', $sqlParams))
504 #end
505 #set($discard = $map.put('reqNo', $numbertool.toNumber($request.reqNo).intValue()))
506 #if("$!request.tagcloud" == 'true')
507 #gridresult_buildTagCloudJSON($map)
508 #end
509 #gridresult_buildRowsJSON($map)
510 #end
511
512
513 #**
514 * Builds the JSON response to a live table data call.
515 *
516 * @param $map stores the JSON in memory so that it can be adjusted before serialization
517 *#
518 #macro(gridresult_buildJSON $className $collist $map)
519 #gridresultwithfilter_buildJSON($className $collist '' '' {} $map)
520 #end
521
522
523 #**
524 * Macro to get the results of a livetable data call.
525 * This page is called from live grids via Ajax with the argument xpage=plain. It returns a
526 * set of results serialized in JSON.
527 *#
528 #macro(gridresultwithfilter $className $collist $filterfrom $filterwhere $filterParams)
529 #if($xcontext.action == 'get' && "$!{request.outputSyntax}" == 'plain')
530 ## Build the JSON in memory (using basic Java data types) so that it can be adjusted before serialization.
531 #set($map = {})
532 #gridresultwithfilter_buildJSON($className $collist $filterfrom $filterwhere $filterParams $map)
533 #jsonResponse($map)
534 #end
535 #end
536
537
538 #**
539 * Get the name of the Property that should be used for a given livetable column.
540 * NOTE the resulting $tableName is safe to use inside SQL queries
541 *#
542 #macro(livetable_getTableName $colname)
543 #livetable_getPropertyClassAndType($colname)
544 #if($propType == 'NumberClass')
545 #set($numberType = $propClass.get($colname).getProperty('numberType').getValue())
546 #if($numberType == 'integer')
547 #set($tableName = 'IntegerProperty')
548 #elseif($numberType == 'float')
549 #set($tableName = 'FloatProperty')
550 #elseif($numberType == 'double')
551 #set($tableName = 'DoubleProperty')
552 #else
553 #set($tableName = 'LongProperty')
554 #end
555 #elseif($propType == 'BooleanClass')
556 #set($tableName = 'IntegerProperty')
557 #elseif($propType == 'DateClass')
558 #set($tableName = 'DateProperty')
559 #elseif($propType == 'TextAreaClass' || $propType == 'UsersClass' || $propType == 'GroupsClass')
560 #set($tableName = 'LargeStringProperty')
561 #elseif($propType == 'StaticListClass' || $propType == 'DBListClass' || $propType == 'DBTreeListClass' || $propType == 'PageClass')
562 ## The following logic is mirrored from ListClass and might need to be updated when the logic in ListClass changes.
563 #set($multiSelect = $propClass.get($colname).getProperty('multiSelect').getValue())
564 #set($relationalStorage = $propClass.get($colname).getProperty('relationalStorage').getValue())
565 #set($largeStorage = $propClass.get($colname).getProperty('largeStorage').getValue())
566 #if($multiSelect == 1)
567 #if($relationalStorage == 1)
568 #set($tableName = 'DBStringListProperty')
569 #else
570 #set($tableName = 'StringListProperty')
571 #end
572 #elseif($largeStorage == 1)
573 #set($tableName = 'LargeStringProperty')
574 #else
575 #set($tableName = 'StringProperty')
576 #end
577 #else
578 #set($tableName = 'StringProperty')
579 #end
580 #end
581
582 #**
583 * Get the property class and type for a given livetable column.
584 *#
585 #macro(livetable_getPropertyClassAndType $colname)
586 #set($propClassName = "$!request.get(${colname.concat('_class')})")
587 #if($propClassName != '')
588 #set($propClass = $xwiki.getDocument($propClassName).getxWikiClass())
589 #else
590 #set($propClass = $class)
591 #end
592 #set($propType = '')
593 #if($propClass.getPropertyNames().contains($colname))
594 #set($propType = "$!{propClass.get($colname).type}")
595 #end
596 #end
597
598 #**
599 * Old alias of the #livetable_getTableName macro.
600 * @deprecated since 2.2.3, use {@link #livetable_getTableName}
601 *#
602 #macro(grid_gettablename $colname)
603 #livetable_getTableName($colname)
604 #end
605
606
607
608 #**
609 * List attachments for a document, putting the result as HTML markup in the $attachlist variable.
610 *#
611 #macro(livetable_getAttachmentsList $itemDoc)
612 #set($attachlist = '')
613 #foreach($attachment in $itemDoc.attachmentList)
614 #set($attachmentUrl = $itemDoc.getAttachmentURL($attachment.filename))
615 #set($attachlist = "${attachlist}<a href='${attachmentUrl}'>$attachment.filename</a><br/>")
616 #end
617 #end
618
619 #**
620 * Old alias of the #livetable_getAttachmentsList macro.
621 * @deprecated since 2.2.3, use {@link #livetable_getAttachmentsList}
622 *#
623 #macro(grid_attachlist $itemDoc)
624 #livetable_getAttachmentsList($itemDoc)
625 #end
626
627
628
629 #**
630 * List image attachments for a document, putting the result as HTML markup in the $imagesList variable.
631 *#
632 #macro(livetable_getImagesList $itemDoc)
633 #set($imagesList = '')
634 #foreach ($attachment in $itemDoc.attachmentList)
635 #if($attachment.isImage())
636 ## Create a thumbnail by resizing the image on the server side, if needed, to fit inside a 50x50 pixel square.
637 #set($thumbnailURL = $itemDoc.getAttachmentURL($attachment.filename, 'download', "width=50&height=50&keepAspectRatio=true"))
638 #set($imageURL = $itemDoc.getAttachmentURL($attachment.filename))
639 #set($imagesList = "${imagesList}<a href=""$imageURL""><img src=""$thumbnailURL"" alt=""$attachment.filename"" title=""$attachment.filename"" /></a>")
640 #end
641 #end
642 #end
643
644 #**
645 * Old alias of the #livetable_getImagesList macro.
646 * @deprecated since 2.2.3, use {@link #livetable_getImagesList}
647 *#
648 #macro(grid_photolist $itemDoc)
649 #livetable_getImagesList($itemDoc)
650 #end
651
652
653 #**
654 * Generate the HTML code for a user avatar.
655 *#
656 #macro(livetable_getAvatar $itemDoc)
657 #set ($avatar = "#mediumUserAvatar($itemDoc.fullName)")
658 #set ($avatar = $avatar.trim())
659 #end
660
661 #**
662 * Old alias of the #livetable_getAvatar macro.
663 * @deprecated since 2.2.3, use {@link #livetable_getAvatar}
664 *#
665 #macro(grid_avatar $itemDoc)
666 #livetable_getAvatar($itemDoc)
667 #end
668
669
670
671 #**
672 * Macro to extend the query to select the properties for the livetable columns.
673 * NOTE $colName is filtered (all characters but [a-zA-Z0-9_.] are removed) before use
674 *#
675 #macro (livetable_addColumnToQuery $colName)
676 ## Safe because / is not allowed in property names
677 ## The $joinModeMarker is used in #livetable_filterDBStringListProperty.
678 #set ($joinModeMarker = "/join_mode")
679 #if (!$colName.endsWith($joinModeMarker))
680 #set ($filterValue = "$!request.getParameter($colName)")
681 #if ("$!filterValue" != '')
682 #set ($discard = $tablelist.add($colName))
683 ## Some columns may support filtering with multiple constraints (multiple filter values).
684 #set ($filterValues = $request.getParameterValues($colName))
685 #if ($colName.startsWith('doc.'))
686 #if ($colName == 'doc.location')
687 #set ($safeColName = 'doc.fullName')
688 ## Use filterLocation since addLivetableLocationFilter is buggy when called several times (it'll add the
689 ## same HQL binding name every time it's called! See https://jira.xwiki.org/browse/XWIKI-17463).
690 ## Also note that we don't call addLocationFilter since we use a Map for $params.
691 #filterLocation($whereSql, $whereParams, $filterValue, 'locationFilterValue2', true)
692 #elseif ($colName == 'doc.date' || $colName == 'doc.creationDate' || $colName == 'doc.contentUpdateDate')
693 #livetable_getTableAlias($colName)
694 #livetable_filterDateProperty()
695 #else
696 #set ($safeColName = $colName.replaceAll('[^a-zA-Z0-9_.]', '').replace('_', '.'))
697 #if ($whereParams.entrySet())
698 #set ($whereSql = "${whereSql} and upper(str($safeColName)) like upper(:${safeColName.replace('.', '_')}_filter)")
699 #set ($discard = $whereParams.put("${safeColName.replace('.', '_')}_filter", "%$filterValue%"))
700 #else
701 #set ($whereSql = "${whereSql} and upper(str($safeColName)) like upper(?)")
702 #set ($discard = $whereParams.add("%$filterValue%"))
703 #end
704 #end
705 #else
706 #livetable_filterProperty($colName)
707 #end
708 #end
709 #end
710 #end
711
712
713 #**
714 * Determine how the filter values should be matched against the stored values. This macro sets two variables:
715 * <ul>
716 * <li>$matchType: use this when the specified column supports only a single filter value</li>
717 * <li>$matchTypes: use this when the specified column supports multiple filter values.</li>
718 * </ul>
719 *
720 * @param column the column name; each column can have a different match type
721 * @param filterValueCount the number of filter values for which to determine the match type; each filter value can have
722 * a different match type
723 * @param defaultMatchType the default match type to use for the given column when the request doesn't specify one
724 *#
725 #macro (livetable_getMatchTypes $column $filterValueCount $defaultMatchType)
726 #set ($macro.matchTypes = $request.getParameterValues("${column}_match"))
727 #if (!$macro.matchTypes || $macro.matchTypes.isEmpty())
728 ## No match type specified for this column.
729 #set ($matchType = $defaultMatchType)
730 #set ($matchTypes = $stringtool.repeat($matchType, ',', $filterValueCount).split(','))
731 #else
732 ## At least one match type specified for this column.
733 #set ($matchType = $macro.matchTypes.get(0))
734 #set ($matchTypes = [])
735 #set ($discard = $matchTypes.addAll($macro.matchTypes.subList(0, $mathtool.min($macro.matchTypes.size(),
736 $filterValueCount))))
737 #if ($matchTypes.size() < $filterValueCount)
738 ## Add missing match types.
739 #set ($discard = $matchTypes.addAll($stringtool.repeat($matchType, ',', $mathtool.sub($filterValueCount,
740 $matchTypes.size())).split(',')))
741 #end
742 #end
743 #end
744
745
746 #macro (livetable_filterProperty $colname)
747 #livetable_getTableAlias($colname)
748 #livetable_getTableName($colname)
749 #set ($fromSql = "$fromSql, $tableName as $safe_tableAlias")
750 ##
751 ## If the column is not from $class, we need to make sure we join with the proper table.
752 #set ($filterObjectAlias = 'obj')
753 #set ($propClass = $class)
754 #set ($propClassName = $request.getParameter("${colname}_class"))
755 #if ("$!propClassName" != '')
756 #set ($propClass = $xwiki.getDocument($propClassName).getxWikiClass())
757 #if ("$!propClass" != '')
758 ## Prepare the alias of the BaseObject table that corresponds to the class of this column
759 ## Property table is to be joined with its object, determined depending on $propClassName.
760 #set ($filterObjectAlias = "$!{propClassName.replaceAll('[^a-zA-Z0-9_]', '')}_obj")
761 #addObjectClause($filterObjectAlias)
762 #end
763 #end
764 #if ($whereParams.entrySet())
765 #set ($joinObjectTable = "${filterObjectAlias}.id = ${safe_tableAlias}.id.id and ${safe_tableAlias}.id.name = :${safe_tableAlias}_id_name")
766 #set ($discard = $whereParams.put("${safe_tableAlias}_id_name", $colname))
767 #else
768 #set ($joinObjectTable = "${filterObjectAlias}.id = ${safe_tableAlias}.id.id and ${safe_tableAlias}.id.name = ?")
769 #set ($discard = $whereParams.add($colname))
770 #end
771 #set ($whereSql = "$whereSql and $joinObjectTable")
772 ##
773 ## We determine the default match type (when not specified) based on the property meta class (e.g. DateClass).
774 #set ($propMetaClass = $NULL)
775 #if ($propClass && $propClass.getPropertyNames().contains($colname))
776 #set ($propMetaClass = $propClass.get($colname).type)
777 #end
778 ##
779 #set ($numberProperties = ['IntegerProperty', 'LongProperty', 'FloatProperty', 'DoubleProperty'])
780 #if ($numberProperties.contains($tableName))
781 #livetable_filterNumberProperty()
782 #elseif ($tableName == 'DateProperty')
783 #livetable_filterDateProperty()
784 #elseif ($tableName == 'DBStringListProperty')
785 #livetable_filterDBStringListProperty()
786 #elseif ($tableName == 'StringListProperty')
787 #livetable_filterStringListProperty()
788 #else
789 ## StringProperty or LargeStringProperty
790 #livetable_filterStringProperty()
791 #end
792 #end
793
794
795 #**
796 * NOTE: This macro uses variables defined in livetable_filterProperty . It was not meant to be used alone.
797 *#
798 #macro (livetable_filterNumberProperty)
799 #set($numberValue = $numbertool.toNumber($filterValue))
800 #if($tableName == 'IntegerProperty' || $tableName == 'LongProperty')
801 #if($tableName == 'LongProperty')
802 #set($numberValue = $numberValue.longValue())
803 #else
804 ## IntegerProperty
805 #set($numberValue = $numberValue.intValue())
806 #end
807 #if ($whereParams.entrySet())
808 #set($whereSql = "${whereSql} and ${safe_tableAlias}.value = :${safe_tableAlias}_value")
809 #set($discard = $whereParams.put("${safe_tableAlias}_value", $numberValue))
810 #else
811 #set($whereSql = "${whereSql} and ${safe_tableAlias}.value = ?")
812 #set($discard = $whereParams.add($numberValue))
813 #end
814 #else
815 #if($tableName == 'FloatProperty')
816 #set($numberValue = $numberValue.floatValue())
817 #else
818 ## DoubleProperty
819 #set($numberValue = $numberValue.doubleValue())
820 #end
821 #set($precision = 0.000001)
822 #if ($whereParams.entrySet())
823 #set($whereSql = "${whereSql} and abs(:${safe_tableAlias}_value - ${safe_tableAlias}.value) <= ${precision}")
824 #set($discard = $whereParams.put("${safe_tableAlias}_value", $numberValue))
825 #else
826 #set($whereSql = "${whereSql} and abs(? - ${safe_tableAlias}.value) <= ${precision}")
827 #set($discard = $whereParams.add($numberValue))
828 #end
829 #end
830 #end
831
832
833 #**
834 * NOTE: This macro uses variables defined in livetable_filterProperty . It was not meant to be used alone.
835 *#
836 #macro (livetable_filterDateProperty)
837 #if ($safe_tableAlias.startsWith('doc.'))
838 #set ($dateProperty = $safe_tableAlias)
839 #else
840 #set ($dateProperty = "${safe_tableAlias}.value")
841 #end
842 #set ($safeDateProperty = $dateProperty.replace('.', '_'))
843 #set ($dateRange = {})
844 ## Perform partial string matching by default if no match type is specified.
845 ## Note that for the moment we support only one filter value (e.g. one date range) and thus only the first match type
846 ## is taken into account.
847 #livetable_getMatchTypes($colname $filterValues.size() 'partial')
848 #parseDateRange($matchType $filterValue $dateRange)
849 #if ($dateRange.start || $dateRange.end)
850 ## Date range.
851 #if ($dateRange.start)
852 #if ($whereParams.entrySet())
853 #set ($whereSql = "${whereSql} and $dateProperty >= :${safeDateProperty}1")
854 #set ($discard = $whereParams.put("${safeDateProperty}1", $dateRange.start))
855 #else
856 #set ($whereSql = "${whereSql} and $dateProperty >= ?")
857 #set ($discard = $whereParams.add($dateRange.start))
858 #end
859 #end
860 #if ($dateRange.end)
861 #if ($whereParams.entrySet())
862 #set ($whereSql = "${whereSql} and $dateProperty <= :${safeDateProperty}2")
863 #set ($discard = $whereParams.put("${safeDateProperty}2", $dateRange.end))
864 #else
865 #set ($whereSql = "${whereSql} and $dateProperty <= ?")
866 #set ($discard = $whereParams.add($dateRange.end))
867 #end
868 #end
869 #else
870 ## String matching (contains).
871 #if ($whereParams.entrySet())
872 #set ($whereSql = "${whereSql} and upper(str($dateProperty)) like upper(:$safeDateProperty)")
873 #set ($discard = $whereParams.put($safeDateProperty, "%$filterValue%"))
874 #else
875 #set ($whereSql = "${whereSql} and upper(str($dateProperty)) like upper(?)")
876 #set ($discard = $whereParams.add("%$filterValue%"))
877 #end
878 #end
879 #end
880
881
882 #**
883 * NOTE: This macro uses variables defined in livetable_filterProperty . It was not meant to be used alone.
884 *#
885 #macro (livetable_filterDBStringListProperty)
886 ## Perform exact matching by default if no match type is specified.
887 ## Note that for DBStringList properties we take into account only the first match type, even if multiple filter
888 ## values are specified. Basically the first match type is used for all filter values.
889 #livetable_getMatchTypes($colname $filterValues.size() 'exact')
890 #if ($matchType == 'partial' || $matchType == 'prefix')
891 ## We need to join with the list of values in order to be able to use the LIKE operator.
892 #set ($matchTarget = "${safe_tableAlias}_item")
893 #if ($whereParams.entrySet())
894 #set ($paramPrefix = "${safe_tableAlias}_item_")
895 #else
896 #set ($paramPrefix = $NULL)
897 #end
898 #set ($joinPos = $mathtool.add($fromSql.lastIndexOf(" $safe_tableAlias"), $mathtool.add($safe_tableAlias.length(), 1)))
899 #set ($fromSql = "$fromSql.substring(0, $joinPos) join ${safe_tableAlias}.list as $matchTarget $fromSql.substring($joinPos)")
900 #else
901 ## Fall-back on exact matching even if the match type is specified, when its value is not supported.
902 #set ($matchType = 'exact')
903 #set ($matchTarget = "${safe_tableAlias}.list")
904 #if ($whereParams.entrySet())
905 #set ($paramPrefix = "${safe_tableAlias}_list_")
906 #else
907 #set ($paramPrefix = $NULL)
908 #end
909 #end
910 #set ($filterQuery = "#livetable_getFilterQuery($matchTarget $matchType true $filterValues.size() $paramPrefix $NULL)")
911 #set ($whereSql = "$whereSql and ($filterQuery.trim())")
912 #foreach ($filterValue in $filterValues)
913 #livetable_addFilterParam($filterValue $matchType $whereParams "${paramPrefix}${foreach.count}")
914 #end
915 #end
916
917
918 #**
919 * NOTE: This macro uses variables defined in livetable_filterProperty . It was not meant to be used alone.
920 *#
921 #macro (livetable_filterStringListProperty)
922 ## From the user point of view we support only exact matching for StringList properties, due to the way the values of
923 ## these properties are stored (concatenated). But when building the actual query, the match type is in fact partial
924 ## because we have to use the like operator in order to match the concatenated list of values.
925 #livetable_getMatchTypes($colname $filterValues.size() 'exact')
926 #set ($matchTarget = "concat('|', concat(${safe_tableAlias}.textValue, '|'))")
927 #if ($whereParams.entrySet())
928 #set ($paramPrefix = "${safe_tableAlias}_textValue_")
929 #else
930 #set ($paramPrefix = $NULL)
931 #end
932 ## As noted above, we have to use the like operator because the list of values is saved concatenated, so from the
933 ## point of view of the query the match type is always partial.
934 #set ($filterQuery = "#livetable_getFilterQuery($matchTarget 'partial' false $filterValues.size() $paramPrefix $NULL)")
935 #set ($whereSql = "${whereSql} and ($filterQuery.trim())")
936 #foreach ($filterValue in $filterValues)
937 #if ($matchTypes.get($foreach.index) == 'empty')
938 ## The client side cannot pass an empty filter value so it specifies that the value is empty using the match type.
939 #set ($filterValue = '')
940 #end
941 ## As noted above, we can only perform exact matching due to the way the values are stored (concatenated).
942 #livetable_addFilterParam("%|$filterValue|%" 'exact' $whereParams "${paramPrefix}${foreach.count}")
943 #end
944 #end
945
946
947 #**
948 * NOTE: This macro uses variables defined in livetable_filterProperty . It was not meant to be used alone.
949 *#
950 #macro (livetable_filterStringProperty)
951 #if ($propMetaClass.endsWith('ListClass'))
952 ## Perform exact matching by default for StaticListClass, DBListClass and DBTreeListClass
953 ## when they are stored as StringProperty (i.e. single value and no relational storage).
954 #set ($defaultStringMatchType = 'exact')
955 #else
956 ## Perform partial matching by default otherwise.
957 #set ($defaultStringMatchType = 'partial')
958 #end
959 #livetable_getMatchTypes($colname $filterValues.size() $defaultStringMatchType)
960 ## Group the filter values by match type so that we cann optimize the query.
961 #livetable_groupFilterValuesByMatchType($matchTypes $filterValues)
962 #if ($whereParams.entrySet())
963 #set ($paramPrefix = "${safe_tableAlias}_value_")
964 #else
965 #set ($paramPrefix = $NULL)
966 #end
967 ## Note that unlike other property types, the String property supports different match types for different filter
968 ## values. This means we have to call livetable_getFilterQuery for each filter value and then join the constraints
969 ## ourselves.
970 #set ($constraints = [])
971 #set ($paramOffset = 1)
972 #foreach ($entry in $filterValuesByMatchType.entrySet())
973 #set ($matchType = $entry.key)
974 #set ($filterValues = $entry.value)
975 #set ($constraint = "#livetable_getFilterQuery(""${safe_tableAlias}.value"" $matchType false $filterValues.size() $paramPrefix $paramOffset)")
976 #set ($discard = $constraints.add($constraint.trim()))
977 #foreach ($filterValue in $filterValues)
978 #livetable_addFilterParam($filterValue $matchType $whereParams
979 "${paramPrefix}${mathtool.add($paramOffset, $foreach.index)}")
980 #end
981 #set ($paramOffset = $paramOffset + $filterValues.size())
982 #end
983 #set ($whereSql = "${whereSql} and ($stringtool.join($constraints, "" $joinOperator ""))")
984 #end
985
986 #macro (livetable_groupFilterValuesByMatchType $matchTypes $filterValues)
987 #set ($filterValuesByMatchType = {})
988 #foreach ($matchType in $matchTypes)
989 #set ($discard = $filterValuesByMatchType.putIfAbsent($matchType, []))
990 #set ($discard = $filterValuesByMatchType.get($matchType).add($filterValues.get($foreach.index)))
991 #end
992 #end
993
994 #macro (livetable_getJoinOperator $colName)
995 #set ($joinOperator = "$!{request.get(""${colName}${joinModeMarker}"").toUpperCase()}")
996 #if ($joinOperator != 'AND' && $joinOperator != 'OR')
997 #set ($joinOperator = 'AND')
998 #end
999 #end
1000
1001 #macro (livetable_getFilterQuery $column $matchType $isList $valueCount $paramPrefix $paramOffset)
1002 #livetable_getJoinOperator($colname)
1003 #if (!$paramOffset)
1004 #set ($paramOffset = 1)
1005 #end
1006 #if ($matchType == 'partial' || $matchType == 'prefix')
1007 #livetable_repeatParams("upper($column) like upper(?)", " $joinOperator ", $valueCount, $paramPrefix, $paramOffset)
1008 #elseif($matchType == 'empty')
1009 ## Check if the value of the column is like the empty parameter (which is often the empty string), or if the value
1010 ## of the column is null (to be compliant with Oracle which stores the empty string as a NULL value).
1011 #livetable_repeatParams("($column like ? or $column is null)", " $joinOperator ", $valueCount, $paramPrefix,
1012 $paramOffset)
1013 #elseif ($isList)
1014 #livetable_repeatParams("? in elements($column)", " $joinOperator ", $valueCount, $paramPrefix, $paramOffset)
1015 #elseif ($valueCount > 1 && $joinOperator == 'OR')
1016 $column in (#livetable_repeatParams('?', ', ', $valueCount, $paramPrefix, $paramOffset))
1017 #else
1018 #livetable_repeatParams("$column = ?", ' AND ', $valueCount, $paramPrefix, $paramOffset)
1019 #end
1020 #end
1021
1022 #macro (livetable_repeatParams $str $separator $valueCount $paramPrefix $paramOffset)
1023 #if ($valueCount > 0)
1024 #foreach ($count in [1..$valueCount])
1025 #if ($count > 1)
1026 $separator##
1027 #end
1028 #if ($paramPrefix)
1029 $str.replace('?', ":${paramPrefix}${mathtool.add($paramOffset, $foreach.index)}")##
1030 #else
1031 $str##
1032 #end
1033 #end
1034 #end
1035 #end
1036
1037 #macro (livetable_addFilterParam $filterValue $matchType $params $paramName)
1038 #if ($matchType == 'partial')
1039 #if ($params.entrySet())
1040 #set ($discard = $params.put($paramName, "%$!filterValue%"))
1041 #else
1042 #set ($discard = $params.add("%$!filterValue%"))
1043 #end
1044 #elseif ($matchType == 'prefix')
1045 #if ($params.entrySet())
1046 #set ($discard = $params.put($paramName, "$!filterValue%"))
1047 #else
1048 #set ($discard = $params.add("$!filterValue%"))
1049 #end
1050 #elseif ($matchType == 'empty')
1051 #if ($params.entrySet())
1052 #set ($discard = $params.put($paramName, ''))
1053 #else
1054 #set ($discard = $params.add(''))
1055 #end
1056 #else
1057 #if ($params.entrySet())
1058 #set ($discard = $params.put($paramName, $filterValue))
1059 #else
1060 #set ($discard = $params.add($filterValue))
1061 #end
1062 #end
1063 #end
1064
1065
1066 #**
1067 * Old alias of the #livetable_addColumnToQuery macro.
1068 * @deprecated since 2.2.3, use {@link #livetable_addColumnToQuery}
1069 *#
1070 #macro(grid_addcolumn $colname)
1071 #livetable_addColumnToQuery($colname)
1072 #end
1073
1074 #**
1075 * Generates a valid SQL table alias for the specified live table column.
1076 *#
1077 #macro (livetable_getTableAlias $columnName)
1078 #set ($prefix = 'doc.')
1079 #if ($columnName.startsWith($prefix))
1080 #set ($suffix = $stringtool.removeStart($columnName, $prefix))
1081 #else
1082 ## Force a prefix to avoid the cases when the column name is a reserved SQL keyword.
1083 #set ($prefix = 'prop_')
1084 #set ($suffix = $columnName)
1085 #end
1086 ## Remove non-word characters.
1087 #set ($safe_tableAlias = "$prefix$suffix.replaceAll('\W', '')")
1088 #end
1089 {{/velocity}}