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.dao;
021
022import jakarta.persistence.Column;
023import jakarta.persistence.Embeddable;
024import jakarta.persistence.Transient;
025
026import java.io.Serializable;
027import java.util.List;
028import java.util.Objects;
029import java.util.stream.Collectors;
030
031/**
032 * This is a version of {@link JpaPid} which can be used as a FK, since it doesn't include
033 * the {@literal @Id} or related annotations.
034 */
035@Embeddable
036public class JpaPidFk implements Serializable {
037
038        @Column(name = "RES_ID", nullable = false)
039        private Long myId;
040
041        @Transient
042        private Integer myPartitionIdValue;
043
044        /**
045         * Note that equals and hashCode for this object only consider the ID and Partition ID because
046         * this class gets used as cache keys
047         */
048        @Override
049        public boolean equals(Object theO) {
050                if (this == theO) {
051                        return true;
052                }
053                if (!(theO instanceof JpaPidFk)) {
054                        return false;
055                }
056                JpaPidFk jpaPid = (JpaPidFk) theO;
057                return Objects.equals(myId, jpaPid.myId) && Objects.equals(myPartitionIdValue, jpaPid.myPartitionIdValue);
058        }
059
060        /**
061         * Note that equals and hashCode for this object only consider the ID and Partition ID because
062         * this class gets used as cache keys
063         */
064        @Override
065        public int hashCode() {
066                return Objects.hash(myId, myPartitionIdValue);
067        }
068
069        public JpaPid toJpaPid() {
070                return JpaPid.fromId(myId, myPartitionIdValue);
071        }
072
073        public void setId(Long theId) {
074                myId = theId;
075        }
076
077        public void setPartitionId(Integer thePartitionId) {
078                myPartitionIdValue = thePartitionId;
079        }
080
081        public static List<JpaPidFk> fromPids(List<JpaPid> thePids) {
082                return thePids.stream().map(JpaPid::toFk).collect(Collectors.toList());
083        }
084
085        public static JpaPidFk fromPid(JpaPid thePid) {
086                JpaPidFk retVal = new JpaPidFk();
087                retVal.setId(thePid.getId());
088                retVal.setPartitionId(thePid.getPartitionId());
089                return retVal;
090        }
091
092        public static JpaPidFk fromId(Long theId, Integer thePartitionId) {
093                JpaPidFk retVal = new JpaPidFk();
094                retVal.setId(theId);
095                retVal.setPartitionId(thePartitionId);
096                return retVal;
097        }
098
099        public static JpaPidFk fromId(Long theId) {
100                return fromId(theId, null);
101        }
102}