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 ca.uhn.fhir.jpa.model.entity.PartitionablePartitionId;
023import ca.uhn.fhir.rest.api.server.storage.BaseResourcePersistentId;
024
025import java.util.ArrayList;
026import java.util.Arrays;
027import java.util.Collection;
028import java.util.HashSet;
029import java.util.List;
030import java.util.Objects;
031import java.util.Set;
032
033/**
034 * JPA implementation of IResourcePersistentId.  JPA uses a Long as the primary key.  This class should be used in any
035 * context where the pid is known to be a Long.
036 */
037public class JpaPid extends BaseResourcePersistentId<Long> {
038        private final Long myId;
039        private PartitionablePartitionId myPartitionablePartitionId;
040
041        private JpaPid(Long theId) {
042                super(null);
043                myId = theId;
044        }
045
046        private JpaPid(Long theId, Long theVersion) {
047                super(theVersion, null);
048                myId = theId;
049        }
050
051        private JpaPid(Long theId, String theResourceType) {
052                super(theResourceType);
053                myId = theId;
054        }
055
056        private JpaPid(Long theId, Long theVersion, String theResourceType) {
057                super(theVersion, theResourceType);
058                myId = theId;
059        }
060
061        public PartitionablePartitionId getPartitionablePartitionId() {
062                return myPartitionablePartitionId;
063        }
064
065        public JpaPid setPartitionablePartitionId(PartitionablePartitionId thePartitionablePartitionId) {
066                myPartitionablePartitionId = thePartitionablePartitionId;
067                return this;
068        }
069
070        public static List<Long> toLongList(JpaPid[] thePids) {
071                return toLongList(Arrays.asList(thePids));
072        }
073
074        public static List<Long> toLongList(Collection<JpaPid> thePids) {
075                List<Long> retVal = new ArrayList<>(thePids.size());
076                for (JpaPid next : thePids) {
077                        retVal.add(next.getId());
078                }
079                return retVal;
080        }
081
082        public static Set<Long> toLongSet(Collection<JpaPid> thePids) {
083                Set<Long> retVal = new HashSet<>(thePids.size());
084                for (JpaPid next : thePids) {
085                        retVal.add(next.getId());
086                }
087                return retVal;
088        }
089
090        public static List<JpaPid> fromLongList(Collection<Long> theResultList) {
091                List<JpaPid> retVal = new ArrayList<>(theResultList.size());
092                for (Long next : theResultList) {
093                        retVal.add(fromId(next));
094                }
095                return retVal;
096        }
097
098        public static JpaPid fromId(Long theId) {
099                return new JpaPid(theId);
100        }
101
102        public static JpaPid fromIdAndVersion(Long theId, Long theVersion) {
103                return new JpaPid(theId, theVersion);
104        }
105
106        public static JpaPid fromIdAndResourceType(Long theId, String theResourceType) {
107                return new JpaPid(theId, theResourceType);
108        }
109
110        public static JpaPid fromIdAndVersionAndResourceType(Long theId, Long theVersion, String theResourceType) {
111                return new JpaPid(theId, theVersion, theResourceType);
112        }
113
114        @Override
115        public boolean equals(Object theO) {
116                if (this == theO) return true;
117                if (theO == null || getClass() != theO.getClass()) return false;
118                if (!super.equals(theO)) return false;
119                JpaPid jpaPid = (JpaPid) theO;
120                return myId.equals(jpaPid.myId);
121        }
122
123        @Override
124        public int hashCode() {
125                return Objects.hash(super.hashCode(), myId);
126        }
127
128        @Override
129        public Long getId() {
130                return myId;
131        }
132
133        @Override
134        public String toString() {
135                return myId.toString();
136        }
137
138        public Integer getPartitionId() {
139                // wipmb should we return null instead?
140                assert getPartitionablePartitionId() != null;
141                return getPartitionablePartitionId().getPartitionId();
142        }
143}