Class | Line # | Actions | |||||
---|---|---|---|---|---|---|---|
HiddenPageFilter | 43 | 6 | 0% | 5 | 1 |
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.index.tree.internal.nestedpages.query; | |
21 | ||
22 | import javax.inject.Inject; | |
23 | import javax.inject.Named; | |
24 | import javax.inject.Singleton; | |
25 | ||
26 | import org.hibernate.dialect.Dialect; | |
27 | import org.hibernate.engine.SessionFactoryImplementor; | |
28 | import org.xwiki.component.annotation.Component; | |
29 | ||
30 | import com.xpn.xwiki.store.hibernate.HibernateSessionFactory; | |
31 | ||
32 | /** | |
33 | * Filters hidden pages. This filter works with the named <strong>native SQL</strong> queries declared in the | |
34 | * {@code queries.hbm.xml} mapping file. | |
35 | * | |
36 | * @version $Id: a6f551d8c4fced8506c21ae8b9525308de6502af $ | |
37 | * @since 8.3RC1 | |
38 | * @since 7.4.5 | |
39 | */ | |
40 | @Component | |
41 | @Named("hiddenPage/nestedPages") | |
42 | @Singleton | |
43 | public class HiddenPageFilter extends AbstractNestedPageFilter | |
44 | { | |
45 | @Inject | |
46 | private HibernateSessionFactory sessionFactory; | |
47 | ||
48 | 11 | @Override |
49 | protected String filterNestedPagesStatement(String statement) | |
50 | { | |
51 | // The constraint is different depending on whether we filter a native SQL query or an HQL query. | |
52 | 11 | String constraint = |
53 | 11 | statement.indexOf("XWS_REFERENCE") < 0 ? "hidden <> true " : getHiddenConstraint("XWS_HIDDEN"); |
54 | 11 | return insertWhereConstraint(statement, constraint); |
55 | } | |
56 | ||
57 | 7 | @Override |
58 | protected String filterTerminalPagesStatement(String statement) | |
59 | { | |
60 | 7 | return statement + " and " + getHiddenConstraint("doc.XWD_HIDDEN"); |
61 | } | |
62 | ||
63 | 18 | private String getHiddenConstraint(String field) |
64 | { | |
65 | // I don't know exactly why "field = false" is not enough since the hidden field is marked as non-null so it | |
66 | // should have only two values (true or false). I remember something related to Oracle but I don't know for | |
67 | // sure. Let's keep this for now, but the downside of using the <> (not-equal) operator instead of = (equal) is | |
68 | // that the database cannot use the index so the query is slower (which can be significant when we have | |
69 | // thousands of documents and spaces in the database). | |
70 | // | |
71 | // Note that we can't use the boolean literal here because Oracle doesn't support it (ORA-00904: "TRUE": invalid | |
72 | // identifier) and we can't use "1" (integer) also because PostgreSQL doesn't support automatic integer to | |
73 | // boolean conversion (PSQLException: ERROR: operator does not exist: boolean <> integer). We're forced to get | |
74 | // the boolean value from the SQL Dialect currently in use. | |
75 | 18 | return String.format("%s <> %s ", field, toBooleanValueString(true)); |
76 | } | |
77 | ||
78 | 18 | private String toBooleanValueString(boolean value) |
79 | { | |
80 | 18 | Dialect dialect = ((SessionFactoryImplementor) this.sessionFactory.getSessionFactory()).getDialect(); |
81 | 18 | return dialect.toBooleanValueString(value); |
82 | } | |
83 | } |