001package ca.uhn.fhir.jpa.search.builder.predicate;
002
003/*-
004 * #%L
005 * HAPI FHIR JPA Server
006 * %%
007 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.jpa.api.config.DaoConfig;
024import ca.uhn.fhir.jpa.dao.predicate.SearchFilterParser;
025import ca.uhn.fhir.jpa.search.builder.sql.SearchQueryBuilder;
026import ca.uhn.fhir.model.api.IQueryParameterType;
027import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
028import ca.uhn.fhir.rest.param.DateParam;
029import ca.uhn.fhir.rest.param.DateRangeParam;
030import ca.uhn.fhir.rest.param.ParamPrefixEnum;
031import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
032import ca.uhn.fhir.util.DateUtils;
033
034import com.healthmarketscience.sqlbuilder.ComboCondition;
035import com.healthmarketscience.sqlbuilder.Condition;
036import com.healthmarketscience.sqlbuilder.dbspec.basic.DbColumn;
037import org.slf4j.Logger;
038import org.slf4j.LoggerFactory;
039import org.springframework.beans.factory.annotation.Autowired;
040
041import java.util.Date;
042
043public class DatePredicateBuilder extends BaseSearchParamPredicateBuilder {
044
045        private static final Logger ourLog = LoggerFactory.getLogger(DatePredicateBuilder.class);
046        private final DbColumn myColumnValueHigh;
047        private final DbColumn myColumnValueLow;
048        private final DbColumn myColumnValueLowDateOrdinal;
049        private final DbColumn myColumnValueHighDateOrdinal;
050
051        @Autowired
052        private DaoConfig myDaoConfig;
053
054        /**
055         * Constructor
056         */
057        public DatePredicateBuilder(SearchQueryBuilder theSearchSqlBuilder) {
058                super(theSearchSqlBuilder, theSearchSqlBuilder.addTable("HFJ_SPIDX_DATE"));
059
060                myColumnValueLow = getTable().addColumn("SP_VALUE_LOW");
061                myColumnValueHigh = getTable().addColumn("SP_VALUE_HIGH");
062                myColumnValueLowDateOrdinal = getTable().addColumn("SP_VALUE_LOW_DATE_ORDINAL");
063                myColumnValueHighDateOrdinal = getTable().addColumn("SP_VALUE_HIGH_DATE_ORDINAL");
064        }
065
066
067        public Condition createPredicateDateWithoutIdentityPredicate(IQueryParameterType theParam,
068                                                                                                                                                                         DatePredicateBuilder theFrom,
069                                                                                                                                                                         SearchFilterParser.CompareOperation theOperation) {
070
071                Condition p;
072                if (theParam instanceof DateParam) {
073                        DateParam date = (DateParam) theParam;
074                        if (!date.isEmpty()) {
075                                if (theOperation == SearchFilterParser.CompareOperation.ne) {
076                                        date = new DateParam(ParamPrefixEnum.EQUAL, date.getValueAsString());
077                                }
078                                DateRangeParam range = new DateRangeParam(date);
079                                p = createPredicateDateFromRange(theFrom, range, theOperation);
080                        } else {
081                                // TODO: handle missing date param?
082                                p = null;
083                        }
084                } else if (theParam instanceof DateRangeParam) {
085                        DateRangeParam range = (DateRangeParam) theParam;
086                        p = createPredicateDateFromRange(
087                                theFrom,
088                                range,
089                                theOperation);
090                } else {
091                        throw new IllegalArgumentException("Invalid token type: " + theParam.getClass());
092                }
093
094                return p;
095        }
096
097        private Condition createPredicateDateFromRange(DatePredicateBuilder theFrom,
098                                                                                                                                  DateRangeParam theRange,
099                                                                                                                                  SearchFilterParser.CompareOperation theOperation) {
100
101
102                Date lowerBoundInstant = theRange.getLowerBoundAsInstant();
103                Date upperBoundInstant = theRange.getUpperBoundAsInstant();
104
105                DateParam lowerBound = theRange.getLowerBound();
106                DateParam upperBound = theRange.getUpperBound();
107                Integer lowerBoundAsOrdinal = theRange.getLowerBoundAsDateInteger();
108                Integer upperBoundAsOrdinal = theRange.getUpperBoundAsDateInteger();
109                Comparable<?> genericLowerBound;
110                Comparable<?> genericUpperBound;
111
112                /*
113                 * If all present search parameters are of DAY precision, and {@link ca.uhn.fhir.jpa.model.entity.ModelConfig#getUseOrdinalDatesForDayPrecisionSearches()} is true,
114                 * then we attempt to use the ordinal field for date comparisons instead of the date field.
115                 */
116                boolean isOrdinalComparison = isNullOrDatePrecision(lowerBound) && isNullOrDatePrecision(upperBound) && myDaoConfig.getModelConfig().getUseOrdinalDatesForDayPrecisionSearches();
117
118                Condition lt;
119                Condition gt;
120                Condition lb = null;
121                Condition ub = null;
122                DatePredicateBuilder.ColumnEnum lowValueField;
123                DatePredicateBuilder.ColumnEnum highValueField;
124
125                if (isOrdinalComparison) {
126                        lowValueField = DatePredicateBuilder.ColumnEnum.LOW_DATE_ORDINAL;
127                        highValueField = DatePredicateBuilder.ColumnEnum.HIGH_DATE_ORDINAL;
128                        genericLowerBound = lowerBoundAsOrdinal;
129                        genericUpperBound = upperBoundAsOrdinal;
130                        if (upperBound != null && upperBound.getPrecision().ordinal() <= TemporalPrecisionEnum.MONTH.ordinal()) {
131                                genericUpperBound = DateUtils.getCompletedDate(upperBound.getValueAsString()).getRight().replace("-", "");
132                                
133                        }
134                } else {
135                        lowValueField = DatePredicateBuilder.ColumnEnum.LOW;
136                        highValueField = DatePredicateBuilder.ColumnEnum.HIGH;
137                        genericLowerBound = lowerBoundInstant;
138                        genericUpperBound = upperBoundInstant;
139                        if (upperBound != null && upperBound.getPrecision().ordinal() <= TemporalPrecisionEnum.MONTH.ordinal()) {
140                                String theCompleteDateStr =  DateUtils.getCompletedDate(upperBound.getValueAsString()).getRight().replace("-", "");
141                                genericUpperBound = DateUtils.parseDate(theCompleteDateStr);
142                        }
143                }
144
145                if (theOperation == SearchFilterParser.CompareOperation.lt || theOperation == SearchFilterParser.CompareOperation.le) {
146                        // use lower bound first
147                        if (lowerBoundInstant != null) {
148                                lb = theFrom.createPredicate(lowValueField, ParamPrefixEnum.LESSTHAN_OR_EQUALS, genericLowerBound);
149                                if (myDaoConfig.isAccountForDateIndexNulls()) {
150                                        lb = ComboCondition.or(lb, theFrom.createPredicate(highValueField, ParamPrefixEnum.LESSTHAN_OR_EQUALS, genericLowerBound));
151                                }
152                        } else if (upperBoundInstant != null) {
153                                ub = theFrom.createPredicate(lowValueField, ParamPrefixEnum.LESSTHAN_OR_EQUALS, genericUpperBound);
154                                if (myDaoConfig.isAccountForDateIndexNulls()) {
155                                        ub = ComboCondition.or(ub, theFrom.createPredicate(highValueField, ParamPrefixEnum.LESSTHAN_OR_EQUALS, genericUpperBound));
156                                }
157                        } else {
158                                throw new InvalidRequestException("lowerBound and upperBound value not correctly specified for comparing " + theOperation);
159                        }
160                } else if (theOperation == SearchFilterParser.CompareOperation.gt || theOperation == SearchFilterParser.CompareOperation.ge) {
161                        // use upper bound first, e.g value between 6 and 10
162                        if (upperBoundInstant != null) {
163                                ub = theFrom.createPredicate(highValueField, ParamPrefixEnum.GREATERTHAN_OR_EQUALS, genericUpperBound);
164                                if (myDaoConfig.isAccountForDateIndexNulls()) {
165                                        ub = ComboCondition.or(ub, theFrom.createPredicate(lowValueField, ParamPrefixEnum.GREATERTHAN_OR_EQUALS, genericUpperBound));
166                                }
167                        } else if (lowerBoundInstant != null) {
168                                lb = theFrom.createPredicate(highValueField, ParamPrefixEnum.GREATERTHAN_OR_EQUALS, genericLowerBound);
169                                if (myDaoConfig.isAccountForDateIndexNulls()) {
170                                        lb = ComboCondition.or(lb, theFrom.createPredicate(lowValueField, ParamPrefixEnum.GREATERTHAN_OR_EQUALS, genericLowerBound));
171                                }
172                        } else {
173                                throw new InvalidRequestException("upperBound and lowerBound value not correctly specified for compare theOperation");
174                        }
175                } else if (theOperation == SearchFilterParser.CompareOperation.ne) {
176                        if ((lowerBoundInstant == null) ||
177                                (upperBoundInstant == null)) {
178                                throw new InvalidRequestException("lowerBound and/or upperBound value not correctly specified for compare theOperation");
179                        }
180                        lt = theFrom.createPredicate(lowValueField, ParamPrefixEnum.LESSTHAN, genericLowerBound);
181                        gt = theFrom.createPredicate(highValueField, ParamPrefixEnum.GREATERTHAN, genericUpperBound);
182                        lb = ComboCondition.or(lt, gt);
183                } else if ((theOperation == SearchFilterParser.CompareOperation.eq)
184                        || (theOperation == SearchFilterParser.CompareOperation.sa)
185                        || (theOperation == SearchFilterParser.CompareOperation.eb)
186                        || (theOperation == null)) {
187                        if (lowerBoundInstant != null) {
188                                gt = theFrom.createPredicate(lowValueField, ParamPrefixEnum.GREATERTHAN_OR_EQUALS, genericLowerBound);
189                                lt = theFrom.createPredicate(highValueField, ParamPrefixEnum.GREATERTHAN_OR_EQUALS, genericLowerBound);
190
191                                if (lowerBound.getPrefix() == ParamPrefixEnum.STARTS_AFTER || lowerBound.getPrefix() == ParamPrefixEnum.EQUAL) {
192                                        lb = gt;
193                                } else {
194                                        lb = ComboCondition.or(gt, lt);
195                                }
196                        }
197
198                        if (upperBoundInstant != null) {
199                                gt = theFrom.createPredicate(lowValueField, ParamPrefixEnum.LESSTHAN_OR_EQUALS, genericUpperBound);
200                                lt = theFrom.createPredicate(highValueField, ParamPrefixEnum.LESSTHAN_OR_EQUALS, genericUpperBound);
201
202
203                                if (theRange.getUpperBound().getPrefix() == ParamPrefixEnum.ENDS_BEFORE || theRange.getUpperBound().getPrefix() == ParamPrefixEnum.EQUAL) {
204                                        ub = lt;
205                                } else {
206                                        ub = ComboCondition.or(gt, lt);
207                                }
208                        }
209                } else {
210                        throw new InvalidRequestException(String.format("Unsupported operator specified, operator=%s",
211                                theOperation.name()));
212                }
213                if (isOrdinalComparison) {
214                        ourLog.trace("Ordinal date range is {} - {} ", lowerBoundAsOrdinal, upperBoundAsOrdinal);
215                } else {
216                        ourLog.trace("Date range is {} - {}", lowerBoundInstant, upperBoundInstant);
217                }
218
219                if (lb != null && ub != null) {
220                        return (ComboCondition.and(lb, ub));
221                } else if (lb != null) {
222                        return (lb);
223                } else {
224                        return (ub);
225                }
226        }
227
228        public DbColumn getColumnValueLow() {
229                return myColumnValueLow;
230        }
231
232        private boolean isNullOrDatePrecision(DateParam theDateParam) {
233                return theDateParam == null || theDateParam.getPrecision().ordinal() <= TemporalPrecisionEnum.DAY.ordinal();
234        }
235
236        private Condition createPredicate(ColumnEnum theColumn, ParamPrefixEnum theComparator, Object theValue) {
237
238                DbColumn column;
239                switch (theColumn) {
240                        case LOW:
241                                column = myColumnValueLow;
242                                break;
243                        case LOW_DATE_ORDINAL:
244                                column = myColumnValueLowDateOrdinal;
245                                break;
246                        case HIGH:
247                                column = myColumnValueHigh;
248                                break;
249                        case HIGH_DATE_ORDINAL:
250                                column = myColumnValueHighDateOrdinal;
251                                break;
252                        default:
253                                throw new IllegalArgumentException();
254                }
255
256                return createConditionForValueWithComparator(theComparator, column, theValue);
257
258        }
259
260
261        public enum ColumnEnum {
262
263                LOW,
264                LOW_DATE_ORDINAL,
265                HIGH,
266                HIGH_DATE_ORDINAL
267
268        }
269
270}