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.jpa.model.config.PartitionSettings;
023import ca.uhn.fhir.jpa.model.listener.IndexStorageOptimizationListener;
024import ca.uhn.fhir.model.api.IQueryParameterType;
025import jakarta.annotation.Nullable;
026import jakarta.persistence.Column;
027import jakarta.persistence.Embeddable;
028import jakarta.persistence.Entity;
029import jakarta.persistence.EntityListeners;
030import jakarta.persistence.FetchType;
031import jakarta.persistence.ForeignKey;
032import jakarta.persistence.GeneratedValue;
033import jakarta.persistence.GenerationType;
034import jakarta.persistence.Id;
035import jakarta.persistence.Index;
036import jakarta.persistence.JoinColumn;
037import jakarta.persistence.ManyToOne;
038import jakarta.persistence.Table;
039import org.apache.commons.lang3.builder.EqualsBuilder;
040import org.apache.commons.lang3.builder.HashCodeBuilder;
041import org.apache.commons.lang3.builder.ToStringBuilder;
042import org.apache.commons.lang3.builder.ToStringStyle;
043import org.hibernate.annotations.GenericGenerator;
044
045@Embeddable
046@EntityListeners(IndexStorageOptimizationListener.class)
047@Entity
048@Table(
049                name = "HFJ_SPIDX_COORDS",
050                indexes = {
051                        @Index(
052                                        name = "IDX_SP_COORDS_HASH_V2",
053                                        columnList = "HASH_IDENTITY,SP_LATITUDE,SP_LONGITUDE,RES_ID,PARTITION_ID"),
054                        @Index(name = "IDX_SP_COORDS_UPDATED", columnList = "SP_UPDATED"),
055                        @Index(name = "IDX_SP_COORDS_RESID", columnList = "RES_ID")
056                })
057public class ResourceIndexedSearchParamCoords extends BaseResourceIndexedSearchParam {
058
059        public static final int MAX_LENGTH = 100;
060
061        private static final long serialVersionUID = 1L;
062
063        @Column(name = "SP_LATITUDE", nullable = true)
064        public Double myLatitude;
065
066        @Column(name = "SP_LONGITUDE", nullable = true)
067        public Double myLongitude;
068
069        @Id
070        @GenericGenerator(name = "SEQ_SPIDX_COORDS", type = ca.uhn.fhir.jpa.model.dialect.HapiSequenceStyleGenerator.class)
071        @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_SPIDX_COORDS")
072        @Column(name = "SP_ID")
073        private Long myId;
074
075        @ManyToOne(
076                        optional = false,
077                        fetch = FetchType.LAZY,
078                        cascade = {})
079        @JoinColumn(
080                        foreignKey = @ForeignKey(name = "FKC97MPK37OKWU8QVTCEG2NH9VN"),
081                        name = "RES_ID",
082                        referencedColumnName = "RES_ID",
083                        nullable = false)
084        private ResourceTable myResource;
085
086        public ResourceIndexedSearchParamCoords() {}
087
088        public ResourceIndexedSearchParamCoords(
089                        PartitionSettings thePartitionSettings,
090                        String theResourceType,
091                        String theParamName,
092                        double theLatitude,
093                        double theLongitude) {
094                setPartitionSettings(thePartitionSettings);
095                setResourceType(theResourceType);
096                setParamName(theParamName);
097                setLatitude(theLatitude);
098                setLongitude(theLongitude);
099                calculateHashes();
100        }
101
102        @Override
103        public void clearHashes() {
104                myHashIdentity = null;
105        }
106
107        @Override
108        public void calculateHashes() {
109                if (myHashIdentity != null) {
110                        return;
111                }
112
113                String resourceType = getResourceType();
114                String paramName = getParamName();
115                setHashIdentity(calculateHashIdentity(getPartitionSettings(), getPartitionId(), resourceType, paramName));
116        }
117
118        @Override
119        public boolean equals(Object theObj) {
120                if (this == theObj) {
121                        return true;
122                }
123                if (theObj == null) {
124                        return false;
125                }
126                if (!(theObj instanceof ResourceIndexedSearchParamCoords)) {
127                        return false;
128                }
129                ResourceIndexedSearchParamCoords obj = (ResourceIndexedSearchParamCoords) theObj;
130                EqualsBuilder b = new EqualsBuilder();
131                b.append(getHashIdentity(), obj.getHashIdentity());
132                b.append(getLatitude(), obj.getLatitude());
133                b.append(getLongitude(), obj.getLongitude());
134                b.append(isMissing(), obj.isMissing());
135                return b.isEquals();
136        }
137
138        @Override
139        public <T extends BaseResourceIndex> void copyMutableValuesFrom(T theSource) {
140                super.copyMutableValuesFrom(theSource);
141                ResourceIndexedSearchParamCoords source = (ResourceIndexedSearchParamCoords) theSource;
142                myLatitude = source.getLatitude();
143                myLongitude = source.getLongitude();
144                myHashIdentity = source.myHashIdentity;
145        }
146
147        @Override
148        public Long getId() {
149                return myId;
150        }
151
152        @Override
153        public void setId(Long theId) {
154                myId = theId;
155        }
156
157        @Nullable
158        public Double getLatitude() {
159                return myLatitude;
160        }
161
162        public ResourceIndexedSearchParamCoords setLatitude(double theLatitude) {
163                myLatitude = theLatitude;
164                return this;
165        }
166
167        @Nullable
168        public Double getLongitude() {
169                return myLongitude;
170        }
171
172        public ResourceIndexedSearchParamCoords setLongitude(double theLongitude) {
173                myLongitude = theLongitude;
174                return this;
175        }
176
177        @Override
178        public int hashCode() {
179                HashCodeBuilder b = new HashCodeBuilder();
180                b.append(getHashIdentity());
181                b.append(getLatitude());
182                b.append(getLongitude());
183                b.append(isMissing());
184                return b.toHashCode();
185        }
186
187        @Override
188        public IQueryParameterType toQueryParameterType() {
189                return null;
190        }
191
192        @Override
193        public String toString() {
194                ToStringBuilder b = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
195                b.append("paramName", getParamName());
196                b.append("resourceId", getResourcePid());
197                if (isMissing()) {
198                        b.append("missing", isMissing());
199                } else {
200                        b.append("lat", getLatitude());
201                        b.append("lon", getLongitude());
202                }
203                return b.build();
204        }
205
206        @Override
207        public ResourceTable getResource() {
208                return myResource;
209        }
210
211        @Override
212        public BaseResourceIndexedSearchParam setResource(ResourceTable theResource) {
213                myResource = theResource;
214                setResourceType(theResource.getResourceType());
215                return this;
216        }
217}