001/*-
002 * #%L
003 * HAPI FHIR JPA Server
004 * %%
005 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.jpa.search.builder.predicate;
021
022import ca.uhn.fhir.jpa.model.entity.ResourceTable;
023import ca.uhn.fhir.jpa.search.builder.sql.SearchQueryBuilder;
024import ca.uhn.fhir.jpa.util.QueryParameterUtils;
025import com.healthmarketscience.sqlbuilder.BinaryCondition;
026import com.healthmarketscience.sqlbuilder.Condition;
027import com.healthmarketscience.sqlbuilder.NotCondition;
028import com.healthmarketscience.sqlbuilder.UnaryCondition;
029import com.healthmarketscience.sqlbuilder.dbspec.basic.DbColumn;
030
031import java.util.Set;
032
033public class ResourceTablePredicateBuilder extends BaseJoiningPredicateBuilder {
034        private final DbColumn myColumnResId;
035        private final DbColumn myColumnResDeletedAt;
036        private final DbColumn myColumnResType;
037        private final DbColumn myColumnLastUpdated;
038        private final DbColumn myColumnLanguage;
039        private final DbColumn myColumnFhirId;
040
041        /**
042         * Constructor
043         */
044        public ResourceTablePredicateBuilder(SearchQueryBuilder theSearchSqlBuilder) {
045                super(theSearchSqlBuilder, theSearchSqlBuilder.addTable("HFJ_RESOURCE"));
046                myColumnResId = getTable().addColumn("RES_ID");
047                myColumnResType = getTable().addColumn(ResourceTable.RES_TYPE);
048                myColumnResDeletedAt = getTable().addColumn("RES_DELETED_AT");
049                myColumnLastUpdated = getTable().addColumn("RES_UPDATED");
050                myColumnLanguage = getTable().addColumn("RES_LANGUAGE");
051                myColumnFhirId = getTable().addColumn(ResourceTable.FHIR_ID);
052        }
053
054        @Override
055        public DbColumn getResourceIdColumn() {
056                return myColumnResId;
057        }
058
059        public Condition createResourceTypeAndNonDeletedPredicates() {
060                BinaryCondition typePredicate = null;
061                if (getResourceType() != null) {
062                        typePredicate = BinaryCondition.equalTo(myColumnResType, generatePlaceholder(getResourceType()));
063                }
064                return QueryParameterUtils.toAndPredicate(typePredicate, UnaryCondition.isNull(myColumnResDeletedAt));
065        }
066
067        public DbColumn getLastUpdatedColumn() {
068                return myColumnLastUpdated;
069        }
070
071        public Condition createLanguagePredicate(Set<String> theValues, boolean theNegated) {
072                Condition condition =
073                                QueryParameterUtils.toEqualToOrInPredicate(myColumnLanguage, generatePlaceholders(theValues));
074                if (theNegated) {
075                        condition = new NotCondition(condition);
076                }
077                return condition;
078        }
079
080        public DbColumn getColumnLastUpdated() {
081                return myColumnLastUpdated;
082        }
083
084        public DbColumn getColumnFhirId() {
085                return myColumnFhirId;
086        }
087}