001package ca.uhn.fhir.jpa.model.entity;
002
003/*-
004 * #%L
005 * HAPI FHIR JPA Model
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.interceptor.model.RequestPartitionId;
024import ca.uhn.fhir.jpa.model.config.PartitionSettings;
025import org.apache.commons.lang3.builder.ToStringBuilder;
026import org.apache.commons.lang3.builder.ToStringStyle;
027
028import javax.persistence.*;
029import java.io.Serializable;
030
031@Entity
032@Table(name = "HFJ_RES_PARAM_PRESENT", indexes = {
033        // We used to have a constraint named IDX_RESPARMPRESENT_SPID_RESID - Don't reuse
034        @Index(name = "IDX_RESPARMPRESENT_RESID", columnList = "RES_ID"),
035        @Index(name = "IDX_RESPARMPRESENT_HASHPRES", columnList = "HASH_PRESENCE")
036})
037public class SearchParamPresent extends BasePartitionable implements Serializable {
038
039        private static final long serialVersionUID = 1L;
040
041        @Id
042        @SequenceGenerator(name = "SEQ_RESPARMPRESENT_ID", sequenceName = "SEQ_RESPARMPRESENT_ID")
043        @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_RESPARMPRESENT_ID")
044        @Column(name = "PID")
045        private Long myId;
046        @Column(name = "SP_PRESENT", nullable = false)
047        private boolean myPresent;
048        @ManyToOne()
049        @JoinColumn(name = "RES_ID", referencedColumnName = "RES_ID", nullable = false, foreignKey = @ForeignKey(name = "FK_RESPARMPRES_RESID"))
050        private ResourceTable myResource;
051        @Column(name = "RES_ID", nullable = false, insertable = false, updatable = false)
052        private Long myResourcePid;
053        @Transient
054        private transient String myParamName;
055        @Column(name = "HASH_PRESENCE")
056        private Long myHashPresence;
057        @Transient
058        private transient PartitionSettings myPartitionSettings;
059
060        /**
061         * Constructor
062         */
063        public SearchParamPresent() {
064                super();
065        }
066
067        @SuppressWarnings("unused")
068        @PrePersist
069        public void calculateHashes() {
070                if (myHashPresence == null && getParamName() != null) {
071                        String resourceType = getResource().getResourceType();
072                        String paramName = getParamName();
073                        boolean present = myPresent;
074                        setHashPresence(calculateHashPresence(getPartitionSettings(), getPartitionId(), resourceType, paramName, present));
075                }
076        }
077
078        public Long getHashPresence() {
079                return myHashPresence;
080        }
081
082        public void setHashPresence(Long theHashPresence) {
083                myHashPresence = theHashPresence;
084        }
085
086        public String getParamName() {
087                return myParamName;
088        }
089
090        public void setParamName(String theParamName) {
091                myParamName = theParamName;
092        }
093
094        public ResourceTable getResource() {
095                return myResource;
096        }
097
098        public void setResource(ResourceTable theResourceTable) {
099                myResource = theResourceTable;
100        }
101
102        public boolean isPresent() {
103                return myPresent;
104        }
105
106        public void setPresent(boolean thePresent) {
107                myPresent = thePresent;
108        }
109
110        @Override
111        public String toString() {
112                ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
113
114                b.append("resPid", myResource.getIdDt().toUnqualifiedVersionless().getValue());
115                b.append("paramName", myParamName);
116                b.append("present", myPresent);
117                b.append("partition", getPartitionId());
118                return b.build();
119        }
120
121        public PartitionSettings getPartitionSettings() {
122                return myPartitionSettings;
123        }
124
125        public void setPartitionSettings(PartitionSettings thePartitionSettings) {
126                myPartitionSettings = thePartitionSettings;
127        }
128
129        public static long calculateHashPresence(PartitionSettings thePartitionSettings, PartitionablePartitionId theRequestPartitionId, String theResourceType, String theParamName, Boolean thePresent) {
130                RequestPartitionId requestPartitionId = PartitionablePartitionId.toRequestPartitionId(theRequestPartitionId);
131                return calculateHashPresence(thePartitionSettings, requestPartitionId, theResourceType, theParamName, thePresent);
132        }
133
134        public static long calculateHashPresence(PartitionSettings thePartitionSettings, RequestPartitionId theRequestPartitionId, String theResourceType, String theParamName, Boolean thePresent) {
135                String string = thePresent != null ? Boolean.toString(thePresent) : Boolean.toString(false);
136                return BaseResourceIndexedSearchParam.hash(thePartitionSettings, theRequestPartitionId, theResourceType, theParamName, string);
137        }
138
139}