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