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 org.apache.commons.lang3.Validate;
024import org.apache.commons.lang3.builder.CompareToBuilder;
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;
029import org.hl7.fhir.instance.model.api.IIdType;
030
031import javax.persistence.*;
032
033@Entity()
034@Table(name = "HFJ_IDX_CMP_STRING_UNIQ", indexes = {
035        @Index(name = ResourceIndexedComboStringUnique.IDX_IDXCMPSTRUNIQ_STRING, columnList = "IDX_STRING", unique = true),
036        @Index(name = ResourceIndexedComboStringUnique.IDX_IDXCMPSTRUNIQ_RESOURCE, columnList = "RES_ID", unique = false)
037})
038public class ResourceIndexedComboStringUnique extends BasePartitionable implements Comparable<ResourceIndexedComboStringUnique> {
039
040        public static final int MAX_STRING_LENGTH = 500;
041        public static final String IDX_IDXCMPSTRUNIQ_STRING = "IDX_IDXCMPSTRUNIQ_STRING";
042        public static final String IDX_IDXCMPSTRUNIQ_RESOURCE = "IDX_IDXCMPSTRUNIQ_RESOURCE";
043
044        @SequenceGenerator(name = "SEQ_IDXCMPSTRUNIQ_ID", sequenceName = "SEQ_IDXCMPSTRUNIQ_ID")
045        @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_IDXCMPSTRUNIQ_ID")
046        @Id
047        @Column(name = "PID")
048        private Long myId;
049        @ManyToOne
050        @JoinColumn(name = "RES_ID", referencedColumnName = "RES_ID", foreignKey = @ForeignKey(name = "FK_IDXCMPSTRUNIQ_RES_ID"))
051        private ResourceTable myResource;
052        @Column(name = "RES_ID", insertable = false, updatable = false)
053        private Long myResourceId;
054        @Column(name = "IDX_STRING", nullable = false, length = MAX_STRING_LENGTH)
055        private String myIndexString;
056
057        /**
058         * This is here to support queries only, do not set this field directly
059         */
060        @SuppressWarnings("unused")
061        @Column(name = PartitionablePartitionId.PARTITION_ID, insertable = false, updatable = false, nullable = true)
062        private Integer myPartitionIdValue;
063        @Transient
064        private IIdType mySearchParameterId;
065
066        /**
067         * Constructor
068         */
069        public ResourceIndexedComboStringUnique() {
070                super();
071        }
072
073        /**
074         * Constructor
075         */
076        public ResourceIndexedComboStringUnique(ResourceTable theResource, String theIndexString, IIdType theSearchParameterId) {
077                setResource(theResource);
078                setIndexString(theIndexString);
079                setPartitionId(theResource.getPartitionId());
080                setSearchParameterId(theSearchParameterId);
081        }
082
083        @Override
084        public int compareTo(ResourceIndexedComboStringUnique theO) {
085                CompareToBuilder b = new CompareToBuilder();
086                b.append(myIndexString, theO.getIndexString());
087                return b.toComparison();
088        }
089
090        @Override
091        public boolean equals(Object theO) {
092                if (this == theO) return true;
093
094                if (!(theO instanceof ResourceIndexedComboStringUnique)) {
095                        return false;
096                }
097
098                ResourceIndexedComboStringUnique that = (ResourceIndexedComboStringUnique) theO;
099
100                return new EqualsBuilder()
101                        .append(myIndexString, that.myIndexString)
102                        .isEquals();
103        }
104
105        public String getIndexString() {
106                return myIndexString;
107        }
108
109        public void setIndexString(String theIndexString) {
110                myIndexString = theIndexString;
111        }
112
113        public ResourceTable getResource() {
114                return myResource;
115        }
116
117        public void setResource(ResourceTable theResource) {
118                Validate.notNull(theResource);
119                myResource = theResource;
120        }
121
122        @Override
123        public int hashCode() {
124                return new HashCodeBuilder(17, 37)
125                        .append(myIndexString)
126                        .toHashCode();
127        }
128
129        @Override
130        public String toString() {
131                return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
132                        .append("id", myId)
133                        .append("resourceId", myResourceId)
134                        .append("indexString", myIndexString)
135                        .append("partition", getPartitionId())
136                        .toString();
137        }
138
139        /**
140         * Note: This field is not persisted, so it will only be populated for new indexes
141         */
142        public void setSearchParameterId(IIdType theSearchParameterId) {
143                mySearchParameterId = theSearchParameterId;
144        }
145
146        /**
147         * Note: This field is not persisted, so it will only be populated for new indexes
148         */
149        public IIdType getSearchParameterId() {
150                return mySearchParameterId;
151        }
152}