Clover icon

Magnolia REST Content Delivery 2.0-rc1

  1. Project Clover database Mon Oct 30 2017 16:36:57 CET
  2. Package info.magnolia.rest.delivery.jcr

File FilteringCondition.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

14
23
5
1
114
60
12
0.52
4.6
5
2.4

Classes

Class Line # Actions
FilteringCondition 47 23 0% 12 2
0.9523809695.2%
 

Contributing tests

This file is covered by 24 tests. .

Source view

1    /**
2    * This file Copyright (c) 2017 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.rest.delivery.jcr;
35   
36    import static java.util.stream.Collectors.joining;
37   
38    import java.time.LocalDate;
39    import java.time.ZoneId;
40    import java.time.ZonedDateTime;
41    import java.time.format.DateTimeFormatter;
42    import java.util.Arrays;
43   
44    /**
45    * This class is used to get filtering condition SQL.
46    */
 
47    public class FilteringCondition {
48    private static final String ISO_DATE_TIME_REGEX = "^(?:[1-9]\\d{3}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1\\d|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[1-9]\\d(?:0[48]|[2468][048]|[13579][26])|(?:[2468][048]|[13579][26])00)-02-29)T(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d.[0-9]{3}(?:Z|[+-][01]\\d:[0-5]\\d)$";
49    private static final String ISO_DATE_REGEX = "^(?:[1-9]\\d{3}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1\\d|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[1-9]\\d(?:0[48]|[2468][048]|[13579][26])|(?:[2468][048]|[13579][26])00)-02-29)$";
50    private static final String NUMBER_REGEX = "[+-]?(([1-9][0-9]*)|(0))([.,][0-9]+)?";
51    private static final String NAME_PROPERTY = "@name";
52    private static final String PARENT_PROPERTY = "@ancestor";
53   
54    static final DateTimeFormatter JCR_OFFSET_DATE_TIME = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
55   
56    private static final String VALUES_SPLITTER = "\\|";
57   
58    private static final String OPERATOR_OPEN = "[";
59    private static final String OPERATOR_CLOSE = "]";
60   
61    private final String property;
62    private final Operator operator;
63    private final String[] values;
64   
 
65  25 toggle public FilteringCondition(String property, String value) {
66    // TODO: Draft support for query parameter operators. This is prone to change! (see MGNLREST-120)
67  25 int openPosition = property.lastIndexOf(OPERATOR_OPEN);
68  25 int closePosition = property.lastIndexOf(OPERATOR_CLOSE);
69  25 this.property = openPosition == -1 ? property : property.substring(0, openPosition);
70  25 this.operator = openPosition == -1 ? Operator.EQ : Operator.from(property.substring(openPosition + 1, closePosition));
71   
72    // TODO: Draft support for multi-value query parameters. This is prone to change! (see MGNLREST-122)
73  25 this.values = value.split(VALUES_SPLITTER);
74    }
75   
 
76  25 toggle public String asSqlString() {
77  25 return Arrays.stream(values)
78    .map(this::getConditionPattern)
79    .collect(joining(" OR "));
80    }
81   
 
82  28 toggle private String getConditionPattern(String value) {
83  28 if (PARENT_PROPERTY.equalsIgnoreCase(property)) {
84  1 return String.format("ISDESCENDANTNODE('%s')", value);
85    }
86   
87  27 if (NAME_PROPERTY.equalsIgnoreCase(property)) {
88  2 return String.format("LOWER(NAME(t)) %s '%s'", operator.asSqlString(), value);
89    }
90   
91  25 return String.format("[%s] %s %s", property, operator.asSqlString(), resolveValue(value));
92    }
93   
 
94  25 toggle private String resolveValue(String value) {
95  25 if (value.matches(NUMBER_REGEX)) {
96  8 return value;
97  17 } else if (value.matches(ISO_DATE_TIME_REGEX)) {
98  2 return String.format("CAST('%s' AS DATE)", value);
99  15 } else if (value.matches(ISO_DATE_REGEX)) {
100  2 DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
101  2 LocalDate localDate = LocalDate.parse(value, dateTimeFormatter);
102  2 ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
103  2 String formattedDateTime = zonedDateTime.format(JCR_OFFSET_DATE_TIME);
104  2 return String.format("CAST('%s' AS DATE)", formattedDateTime);
105    }
106   
107  13 return String.format("'%s'", value);
108    }
109   
 
110  0 toggle @Override
111    public String toString() {
112  0 return asSqlString();
113    }
114    }