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.interceptor.model.RequestPartitionId;
024import org.apache.commons.lang3.builder.EqualsBuilder;
025import org.apache.commons.lang3.builder.HashCodeBuilder;
026
027import javax.annotation.Nonnull;
028import javax.annotation.Nullable;
029import javax.persistence.Column;
030import javax.persistence.Embeddable;
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        @Column(name = "PARTITION_DATE", nullable = true, insertable = true, updatable = false)
041        private LocalDate myPartitionDate;
042
043        /**
044         * Constructor
045         */
046        public PartitionablePartitionId() {
047                super();
048        }
049
050        /**
051         * Constructor
052         */
053        public PartitionablePartitionId(@Nullable Integer thePartitionId, @Nullable LocalDate thePartitionDate) {
054                setPartitionId(thePartitionId);
055                setPartitionDate(thePartitionDate);
056        }
057
058        @Nullable
059        public Integer getPartitionId() {
060                return myPartitionId;
061        }
062
063        public PartitionablePartitionId setPartitionId(@Nullable Integer thePartitionId) {
064                myPartitionId = thePartitionId;
065                return this;
066        }
067
068        @Override
069        public boolean equals(Object theO) {
070                if (!(theO instanceof PartitionablePartitionId)) {
071                        return false;
072                }
073
074                PartitionablePartitionId that = (PartitionablePartitionId) theO;
075                return new EqualsBuilder().append(myPartitionId, that.myPartitionId).append(myPartitionDate, that.myPartitionDate).isEquals();
076        }
077
078        @Override
079        public int hashCode() {
080                return new HashCodeBuilder(17, 37).append(myPartitionId).append(myPartitionDate).toHashCode();
081        }
082
083        @Nullable
084        public LocalDate getPartitionDate() {
085                return myPartitionDate;
086        }
087
088        public PartitionablePartitionId setPartitionDate(@Nullable LocalDate thePartitionDate) {
089                myPartitionDate = thePartitionDate;
090                return this;
091        }
092
093        @SuppressWarnings({"CloneDoesntDeclareCloneNotSupportedException", "MethodDoesntCallSuperMethod"})
094        @Override
095        protected PartitionablePartitionId clone() {
096                return new PartitionablePartitionId()
097                        .setPartitionId(getPartitionId())
098                        .setPartitionDate(getPartitionDate());
099        }
100
101        public RequestPartitionId toPartitionId() {
102                return RequestPartitionId.fromPartitionId(getPartitionId(), getPartitionDate());
103        }
104
105        @Override
106        public String toString() {
107                return "[" +
108                        getPartitionId() +
109                        "]";
110        }
111
112        @Nonnull
113        public static RequestPartitionId toRequestPartitionId(@Nullable PartitionablePartitionId theRequestPartitionId) {
114                if (theRequestPartitionId != null) {
115                        return theRequestPartitionId.toPartitionId();
116                } else {
117                        return RequestPartitionId.defaultPartition();
118                }
119        }
120}