Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
LanguageQueryFilter | 41 | 6 | 0% | 5 | 0 |
1 | /* | |
2 | * See the NOTICE file distributed with this work for additional | |
3 | * information regarding copyright ownership. | |
4 | * | |
5 | * This is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU Lesser General Public License as | |
7 | * published by the Free Software Foundation; either version 2.1 of | |
8 | * the License, or (at your option) any later version. | |
9 | * | |
10 | * This software is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this software; if not, write to the Free | |
17 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
18 | * 02110-1301 USA, or see the FSF site: http://www.fsf.org. | |
19 | */ | |
20 | package org.xwiki.query.internal; | |
21 | ||
22 | import java.util.List; | |
23 | ||
24 | import javax.inject.Named; | |
25 | import javax.inject.Singleton; | |
26 | ||
27 | import org.xwiki.component.annotation.Component; | |
28 | import org.xwiki.query.Query; | |
29 | import org.xwiki.query.QueryFilter; | |
30 | ||
31 | /** | |
32 | * Query filter adding a select clause to also return the document's language. This is useful if you need to adapt | |
33 | * your display based on the document's language (for example in search results). | |
34 | * | |
35 | * @version $Id: d8e8ad0d6721f23c6a34dd209d4a7940c98a02fc $ | |
36 | * @since 5.1M2 | |
37 | */ | |
38 | @Component | |
39 | @Named("language") | |
40 | @Singleton | |
41 | public class LanguageQueryFilter implements QueryFilter | |
42 | { | |
43 | /** | |
44 | * The select clause to extend in order to also return the document's language. | |
45 | */ | |
46 | private static final String SELECT_CLAUSE_TO_EXTEND = "select doc.fullName"; | |
47 | ||
48 | /** | |
49 | * @param statement statement to filter. | |
50 | * @return true if the filter can be applied to the passed statement, false otherwise. | |
51 | */ | |
52 | 2 | private boolean isFilterable(String statement) |
53 | { | |
54 | 2 | return statement.startsWith(SELECT_CLAUSE_TO_EXTEND); |
55 | } | |
56 | ||
57 | 3 | @Override |
58 | public String filterStatement(String statement, String language) | |
59 | { | |
60 | 3 | String result = statement.trim(); |
61 | ||
62 | 3 | if (Query.HQL.equals(language) && isFilterable(result)) { |
63 | 1 | result = SELECT_CLAUSE_TO_EXTEND + ", doc.language" + result.substring(SELECT_CLAUSE_TO_EXTEND.length()); |
64 | } | |
65 | ||
66 | 3 | return result; |
67 | } | |
68 | ||
69 | 1 | @Override |
70 | public List filterResults(List results) | |
71 | { | |
72 | 1 | return results; |
73 | } | |
74 | } |