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.annotation.Nonnull;
025import jakarta.annotation.Nullable;
026import jakarta.persistence.Column;
027import jakarta.persistence.Embeddable;
028import org.apache.commons.lang3.builder.EqualsBuilder;
029import org.apache.commons.lang3.builder.HashCodeBuilder;
030
031import java.time.LocalDate;
032
033@Embeddable
034public class PartitionablePartitionId implements Cloneable {
035
036        static final String PARTITION_ID = "PARTITION_ID";
037
038        @Column(name = PARTITION_ID, nullable = true, insertable = true, updatable = false)
039        private Integer myPartitionId;
040
041        @Column(name = "PARTITION_DATE", nullable = true, insertable = true, updatable = false)
042        private LocalDate myPartitionDate;
043
044        /**
045         * Constructor
046         */
047        public PartitionablePartitionId() {
048                super();
049        }
050
051        /**
052         * Constructor
053         */
054        public PartitionablePartitionId(@Nullable Integer thePartitionId, @Nullable LocalDate thePartitionDate) {
055                setPartitionId(thePartitionId);
056                setPartitionDate(thePartitionDate);
057        }
058
059        @Nullable
060        public Integer getPartitionId() {
061                return myPartitionId;
062        }
063
064        public PartitionablePartitionId setPartitionId(@Nullable Integer thePartitionId) {
065                myPartitionId = thePartitionId;
066                return this;
067        }
068
069        @Override
070        public boolean equals(Object theO) {
071                if (!(theO instanceof PartitionablePartitionId)) {
072                        return false;
073                }
074
075                PartitionablePartitionId that = (PartitionablePartitionId) theO;
076                return new EqualsBuilder()
077                                .append(myPartitionId, that.myPartitionId)
078                                .append(myPartitionDate, that.myPartitionDate)
079                                .isEquals();
080        }
081
082        @Override
083        public int hashCode() {
084                return new HashCodeBuilder(17, 37)
085                                .append(myPartitionId)
086                                .append(myPartitionDate)
087                                .toHashCode();
088        }
089
090        @Nullable
091        public LocalDate getPartitionDate() {
092                return myPartitionDate;
093        }
094
095        public PartitionablePartitionId setPartitionDate(@Nullable LocalDate thePartitionDate) {
096                myPartitionDate = thePartitionDate;
097                return this;
098        }
099
100        @SuppressWarnings({"CloneDoesntDeclareCloneNotSupportedException", "MethodDoesntCallSuperMethod"})
101        @Override
102        protected PartitionablePartitionId clone() {
103                return new PartitionablePartitionId().setPartitionId(getPartitionId()).setPartitionDate(getPartitionDate());
104        }
105
106        public RequestPartitionId toPartitionId() {
107                return RequestPartitionId.fromPartitionId(getPartitionId(), getPartitionDate());
108        }
109
110        @Override
111        public String toString() {
112                return "PartitionablePartitionId{" + "myPartitionId="
113                                + myPartitionId + ", myPartitionDate="
114                                + myPartitionDate + '}';
115        }
116
117        @Nonnull
118        public static RequestPartitionId toRequestPartitionId(@Nullable PartitionablePartitionId theRequestPartitionId) {
119                if (theRequestPartitionId != null) {
120                        return theRequestPartitionId.toPartitionId();
121                } else {
122                        return RequestPartitionId.defaultPartition();
123                }
124        }
125
126        @Nonnull
127        public static PartitionablePartitionId toStoragePartition(
128                        @Nonnull RequestPartitionId theRequestPartitionId, @Nonnull PartitionSettings thePartitionSettings) {
129                Integer partitionId = theRequestPartitionId.getFirstPartitionIdOrNull();
130                if (partitionId == null) {
131                        partitionId = thePartitionSettings.getDefaultPartitionId();
132                }
133                return new PartitionablePartitionId(partitionId, theRequestPartitionId.getPartitionDate());
134        }
135}