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.i18n.Msg;
024import ca.uhn.fhir.interceptor.model.RequestPartitionId;
025import ca.uhn.fhir.jpa.model.config.PartitionSettings;
026import org.apache.commons.lang3.builder.CompareToBuilder;
027import org.apache.commons.lang3.builder.EqualsBuilder;
028import org.apache.commons.lang3.builder.HashCodeBuilder;
029import org.apache.commons.lang3.builder.ToStringBuilder;
030
031import javax.persistence.Column;
032import javax.persistence.Entity;
033import javax.persistence.ForeignKey;
034import javax.persistence.GeneratedValue;
035import javax.persistence.GenerationType;
036import javax.persistence.Id;
037import javax.persistence.Index;
038import javax.persistence.JoinColumn;
039import javax.persistence.ManyToOne;
040import javax.persistence.SequenceGenerator;
041import javax.persistence.Table;
042import javax.persistence.Transient;
043
044import static ca.uhn.fhir.jpa.model.entity.BaseResourceIndexedSearchParam.hash;
045
046@Entity
047@Table(name = "HFJ_IDX_CMB_TOK_NU", indexes = {
048        @Index(name = "IDX_IDXCMBTOKNU_STR", columnList = "IDX_STRING", unique = false),
049        @Index(name = "IDX_IDXCMBTOKNU_RES", columnList = "RES_ID", unique = false)
050})
051public class ResourceIndexedComboTokenNonUnique extends BaseResourceIndex implements Comparable<ResourceIndexedComboTokenNonUnique> {
052
053        @SequenceGenerator(name = "SEQ_IDXCMBTOKNU_ID", sequenceName = "SEQ_IDXCMBTOKNU_ID")
054        @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_IDXCMBTOKNU_ID")
055        @Id
056        @Column(name = "PID")
057        private Long myId;
058
059        @ManyToOne
060        @JoinColumn(name = "RES_ID", referencedColumnName = "RES_ID", foreignKey = @ForeignKey(name = "FK_IDXCMBTOKNU_RES_ID"))
061        private ResourceTable myResource;
062
063        @Column(name = "RES_ID", insertable = false, updatable = false)
064        private Long myResourceId;
065
066        @Column(name = "HASH_COMPLETE", nullable = false)
067        private Long myHashComplete;
068
069        @Column(name = "IDX_STRING", nullable = false, length = ResourceIndexedComboStringUnique.MAX_STRING_LENGTH)
070        private String myIndexString;
071
072        @Transient
073        private transient PartitionSettings myPartitionSettings;
074
075        /**
076         * Constructor
077         */
078        public ResourceIndexedComboTokenNonUnique() {
079                super();
080        }
081
082        public ResourceIndexedComboTokenNonUnique(PartitionSettings thePartitionSettings, ResourceTable theEntity, String theQueryString) {
083                myPartitionSettings = thePartitionSettings;
084                myResource = theEntity;
085                myIndexString = theQueryString;
086                calculateHashes();
087        }
088
089        public String getIndexString() {
090                return myIndexString;
091        }
092
093        public void setIndexString(String theIndexString) {
094                myIndexString = theIndexString;
095        }
096
097        @Override
098        public boolean equals(Object theO) {
099                if (this == theO) {
100                        return true;
101                }
102
103                if (theO == null || getClass() != theO.getClass()) {
104                        return false;
105                }
106
107                ResourceIndexedComboTokenNonUnique that = (ResourceIndexedComboTokenNonUnique) theO;
108
109                return new EqualsBuilder()
110                        .append(myResource, that.myResource)
111                        .append(myHashComplete, that.myHashComplete)
112                        .isEquals();
113        }
114
115        @Override
116        public <T extends BaseResourceIndex> void copyMutableValuesFrom(T theSource) {
117                throw new IllegalStateException(Msg.code(1528));
118        }
119
120        @Override
121        public Long getId() {
122                return myId;
123        }
124
125        @Override
126        public void setId(Long theId) {
127                myId = theId;
128        }
129
130        @Override
131        public void clearHashes() {
132                myHashComplete = null;
133        }
134
135        @Override
136        public void calculateHashes() {
137                if (myHashComplete != null) {
138                        return;
139                }
140
141                PartitionSettings partitionSettings = getPartitionSettings();
142                PartitionablePartitionId partitionId = getPartitionId();
143                String queryString = myIndexString;
144                setHashComplete(calculateHashComplete(partitionSettings, partitionId, queryString));
145        }
146
147        @Override
148        public int hashCode() {
149                return new HashCodeBuilder(17, 37)
150                        .append(myResource)
151                        .append(myHashComplete)
152                        .toHashCode();
153        }
154
155        public PartitionSettings getPartitionSettings() {
156                return myPartitionSettings;
157        }
158
159        public ResourceTable getResource() {
160                return myResource;
161        }
162
163        public void setResource(ResourceTable theResource) {
164                myResource = theResource;
165        }
166
167        public Long getHashComplete() {
168                return myHashComplete;
169        }
170
171        public void setHashComplete(Long theHashComplete) {
172                myHashComplete = theHashComplete;
173        }
174
175        @Override
176        public int compareTo(ResourceIndexedComboTokenNonUnique theO) {
177                CompareToBuilder b = new CompareToBuilder();
178                b.append(myHashComplete, theO.getHashComplete());
179                return b.toComparison();
180        }
181
182        @Override
183        public String toString() {
184                return new ToStringBuilder(this)
185                        .append("id", myId)
186                        .append("resourceId", myResourceId)
187                        .append("hashComplete", myHashComplete)
188                        .append("indexString", myIndexString)
189                        .toString();
190        }
191
192        public static long calculateHashComplete(PartitionSettings partitionSettings, PartitionablePartitionId thePartitionId, String queryString) {
193                RequestPartitionId requestPartitionId = PartitionablePartitionId.toRequestPartitionId(thePartitionId);
194                return hash(partitionSettings, requestPartitionId, queryString);
195        }
196
197        public static long calculateHashComplete(PartitionSettings partitionSettings, RequestPartitionId partitionId, String queryString) {
198                return hash(partitionSettings, partitionId, queryString);
199        }
200
201}