001/*
002 * #%L
003 * HAPI FHIR JPA Model
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.model.entity;
021
022import ca.uhn.fhir.interceptor.model.RequestPartitionId;
023import ca.uhn.fhir.jpa.model.config.PartitionSettings;
024import jakarta.persistence.Column;
025import jakarta.persistence.MappedSuperclass;
026import org.apache.commons.lang3.builder.HashCodeBuilder;
027import org.hibernate.search.mapper.pojo.mapping.definition.annotation.FullTextField;
028
029@MappedSuperclass
030public abstract class BaseResourceIndexedSearchParamQuantity extends BaseResourceIndexedSearchParam {
031
032        private static final int MAX_LENGTH = 200;
033
034        private static final long serialVersionUID = 1L;
035
036        @Column(name = "SP_SYSTEM", nullable = true, length = MAX_LENGTH)
037        @FullTextField
038        public String mySystem;
039
040        @Column(name = "SP_UNITS", nullable = true, length = MAX_LENGTH)
041        @FullTextField
042        public String myUnits;
043
044        /**
045         * @since 3.5.0 - At some point this should be made not-null
046         */
047        @Column(name = "HASH_IDENTITY_AND_UNITS", nullable = true)
048        private Long myHashIdentityAndUnits;
049        /**
050         * @since 3.5.0 - At some point this should be made not-null
051         */
052        @Column(name = "HASH_IDENTITY_SYS_UNITS", nullable = true)
053        private Long myHashIdentitySystemAndUnits;
054        /**
055         * @since 3.5.0 - At some point this should be made not-null
056         */
057        @Column(name = "HASH_IDENTITY", nullable = true)
058        private Long myHashIdentity;
059
060        /**
061         * Constructor
062         */
063        public BaseResourceIndexedSearchParamQuantity() {
064                super();
065        }
066
067        @Override
068        public void clearHashes() {
069                myHashIdentity = null;
070                myHashIdentityAndUnits = null;
071                myHashIdentitySystemAndUnits = null;
072        }
073
074        @Override
075        public void calculateHashes() {
076                if (myHashIdentity != null || myHashIdentityAndUnits != null || myHashIdentitySystemAndUnits != null) {
077                        return;
078                }
079
080                String resourceType = getResourceType();
081                String paramName = getParamName();
082                String units = getUnits();
083                String system = getSystem();
084                setHashIdentity(calculateHashIdentity(getPartitionSettings(), getPartitionId(), resourceType, paramName));
085                setHashIdentityAndUnits(
086                                calculateHashUnits(getPartitionSettings(), getPartitionId(), resourceType, paramName, units));
087                setHashIdentitySystemAndUnits(calculateHashSystemAndUnits(
088                                getPartitionSettings(), getPartitionId(), resourceType, paramName, system, units));
089        }
090
091        public Long getHashIdentity() {
092                return myHashIdentity;
093        }
094
095        public void setHashIdentity(Long theHashIdentity) {
096                myHashIdentity = theHashIdentity;
097        }
098
099        public Long getHashIdentityAndUnits() {
100                return myHashIdentityAndUnits;
101        }
102
103        public void setHashIdentityAndUnits(Long theHashIdentityAndUnits) {
104                myHashIdentityAndUnits = theHashIdentityAndUnits;
105        }
106
107        public Long getHashIdentitySystemAndUnits() {
108                return myHashIdentitySystemAndUnits;
109        }
110
111        public void setHashIdentitySystemAndUnits(Long theHashIdentitySystemAndUnits) {
112                myHashIdentitySystemAndUnits = theHashIdentitySystemAndUnits;
113        }
114
115        public String getSystem() {
116                return mySystem;
117        }
118
119        public void setSystem(String theSystem) {
120                mySystem = theSystem;
121        }
122
123        public String getUnits() {
124                return myUnits;
125        }
126
127        public void setUnits(String theUnits) {
128                myUnits = theUnits;
129        }
130
131        @Override
132        public int hashCode() {
133                HashCodeBuilder b = new HashCodeBuilder();
134                b.append(getResourceType());
135                b.append(getParamName());
136                b.append(getHashIdentity());
137                b.append(getHashIdentityAndUnits());
138                b.append(getHashIdentitySystemAndUnits());
139                return b.toHashCode();
140        }
141
142        public static long calculateHashSystemAndUnits(
143                        PartitionSettings thePartitionSettings,
144                        PartitionablePartitionId theRequestPartitionId,
145                        String theResourceType,
146                        String theParamName,
147                        String theSystem,
148                        String theUnits) {
149                RequestPartitionId requestPartitionId = PartitionablePartitionId.toRequestPartitionId(theRequestPartitionId);
150                return calculateHashSystemAndUnits(
151                                thePartitionSettings, requestPartitionId, theResourceType, theParamName, theSystem, theUnits);
152        }
153
154        public static long calculateHashSystemAndUnits(
155                        PartitionSettings thePartitionSettings,
156                        RequestPartitionId theRequestPartitionId,
157                        String theResourceType,
158                        String theParamName,
159                        String theSystem,
160                        String theUnits) {
161                return hash(thePartitionSettings, theRequestPartitionId, theResourceType, theParamName, theSystem, theUnits);
162        }
163
164        public static long calculateHashUnits(
165                        PartitionSettings thePartitionSettings,
166                        PartitionablePartitionId theRequestPartitionId,
167                        String theResourceType,
168                        String theParamName,
169                        String theUnits) {
170                RequestPartitionId requestPartitionId = PartitionablePartitionId.toRequestPartitionId(theRequestPartitionId);
171                return calculateHashUnits(thePartitionSettings, requestPartitionId, theResourceType, theParamName, theUnits);
172        }
173
174        public static long calculateHashUnits(
175                        PartitionSettings thePartitionSettings,
176                        RequestPartitionId theRequestPartitionId,
177                        String theResourceType,
178                        String theParamName,
179                        String theUnits) {
180                return hash(thePartitionSettings, theRequestPartitionId, theResourceType, theParamName, theUnits);
181        }
182}