001package ca.uhn.fhir.jpa.search.builder.predicate;
002
003/*-
004 * #%L
005 * HAPI FHIR JPA Server
006 * %%
007 * Copyright (C) 2014 - 2022 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.search.builder.sql.SearchQueryBuilder;
024import com.healthmarketscience.sqlbuilder.BinaryCondition;
025import com.healthmarketscience.sqlbuilder.Condition;
026import com.healthmarketscience.sqlbuilder.NotCondition;
027import com.healthmarketscience.sqlbuilder.UnaryCondition;
028import com.healthmarketscience.sqlbuilder.dbspec.basic.DbColumn;
029
030import java.util.Set;
031
032import static ca.uhn.fhir.jpa.search.builder.QueryStack.toAndPredicate;
033import static ca.uhn.fhir.jpa.search.builder.QueryStack.toEqualToOrInPredicate;
034
035public class ResourceTablePredicateBuilder extends BaseJoiningPredicateBuilder {
036        private final DbColumn myColumnResId;
037        private final DbColumn myColumnResDeletedAt;
038        private final DbColumn myColumnResType;
039        private final DbColumn myColumnLastUpdated;
040        private final DbColumn myColumnLanguage;
041
042        /**
043         * Constructor
044         */
045        public ResourceTablePredicateBuilder(SearchQueryBuilder theSearchSqlBuilder) {
046                super(theSearchSqlBuilder, theSearchSqlBuilder.addTable("HFJ_RESOURCE"));
047                myColumnResId = getTable().addColumn("RES_ID");
048                myColumnResType = getTable().addColumn("RES_TYPE");
049                myColumnResDeletedAt = getTable().addColumn("RES_DELETED_AT");
050                myColumnLastUpdated = getTable().addColumn("RES_UPDATED");
051                myColumnLanguage = getTable().addColumn("RES_LANGUAGE");
052        }
053
054
055        @Override
056        public DbColumn getResourceIdColumn() {
057                return myColumnResId;
058        }
059
060        public Condition createResourceTypeAndNonDeletedPredicates() {
061                BinaryCondition typePredicate = null;
062                if (getResourceType() != null) {
063                        typePredicate = BinaryCondition.equalTo(myColumnResType, generatePlaceholder(getResourceType()));
064                }
065                return toAndPredicate(
066                        typePredicate,
067                        UnaryCondition.isNull(myColumnResDeletedAt)
068                );
069        }
070
071        public DbColumn getLastUpdatedColumn() {
072                return myColumnLastUpdated;
073        }
074
075        public Condition createLanguagePredicate(Set<String> theValues, boolean theNegated) {
076                Condition condition = toEqualToOrInPredicate(myColumnLanguage, generatePlaceholders(theValues));
077                if (theNegated) {
078                        condition = new NotCondition(condition);
079                }
080                return condition;
081        }
082
083        public DbColumn getColumnLastUpdated() {
084                return myColumnLastUpdated;
085        }
086}