001/*
002 * #%L
003 * HAPI FHIR JPA Model
004 * %%
005 * Copyright (C) 2014 - 2025 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.context.FhirVersionEnum;
023import ca.uhn.fhir.jpa.model.cross.IBasePersistedResource;
024import ca.uhn.fhir.jpa.model.dao.JpaPid;
025import ca.uhn.fhir.model.primitive.InstantDt;
026import jakarta.persistence.Column;
027import jakarta.persistence.EnumType;
028import jakarta.persistence.Enumerated;
029import jakarta.persistence.MappedSuperclass;
030import jakarta.persistence.Temporal;
031import jakarta.persistence.TemporalType;
032import org.hibernate.annotations.JdbcTypeCode;
033import org.hibernate.annotations.OptimisticLock;
034import org.hibernate.type.SqlTypes;
035
036import java.io.Serializable;
037import java.util.Collection;
038import java.util.Date;
039
040@MappedSuperclass
041public abstract class BaseHasResource<T>
042                implements IBaseResourceEntity<T>, IBasePersistedResource<JpaPid>, Serializable {
043
044        public static final String RES_PUBLISHED = "RES_PUBLISHED";
045        public static final String RES_UPDATED = "RES_UPDATED";
046
047        @Column(name = "RES_DELETED_AT", nullable = true)
048        @Temporal(TemporalType.TIMESTAMP)
049        private Date myDeleted;
050
051        @Column(name = "RES_VERSION", nullable = true, length = 7)
052        @Enumerated(EnumType.STRING)
053        @OptimisticLock(excluded = true)
054        @JdbcTypeCode(SqlTypes.VARCHAR)
055        private FhirVersionEnum myFhirVersion;
056
057        @Column(name = "HAS_TAGS", nullable = false)
058        @OptimisticLock(excluded = true)
059        private boolean myHasTags;
060
061        @Temporal(TemporalType.TIMESTAMP)
062        @Column(name = RES_PUBLISHED, nullable = false)
063        @OptimisticLock(excluded = true)
064        private Date myPublished;
065
066        @Temporal(TemporalType.TIMESTAMP)
067        @Column(name = RES_UPDATED, nullable = false)
068        @OptimisticLock(excluded = true)
069        private Date myUpdated;
070
071        public abstract BaseTag addTag(TagDefinition theDef);
072
073        @Override
074        public Date getDeleted() {
075                return cloneDate(myDeleted);
076        }
077
078        @Override
079        public FhirVersionEnum getFhirVersion() {
080                return myFhirVersion;
081        }
082
083        public void setFhirVersion(FhirVersionEnum theFhirVersion) {
084                myFhirVersion = theFhirVersion;
085        }
086
087        public void setDeleted(Date theDate) {
088                myDeleted = theDate;
089        }
090
091        @Override
092        public InstantDt getPublished() {
093                if (myPublished != null) {
094                        return new InstantDt(getPublishedDate());
095                } else {
096                        return null;
097                }
098        }
099
100        public Date getPublishedDate() {
101                return cloneDate(myPublished);
102        }
103
104        public void setPublished(Date thePublished) {
105                myPublished = thePublished;
106        }
107
108        public void setPublished(InstantDt thePublished) {
109                myPublished = thePublished.getValue();
110        }
111
112        public abstract Collection<? extends BaseTag> getTags();
113
114        @Override
115        public InstantDt getUpdated() {
116                return new InstantDt(getUpdatedDate());
117        }
118
119        @Override
120        public Date getUpdatedDate() {
121                return cloneDate(myUpdated);
122        }
123
124        public void setUpdated(Date theUpdated) {
125                myUpdated = theUpdated;
126        }
127
128        @Override
129        public boolean isHasTags() {
130                return myHasTags;
131        }
132
133        public void setHasTags(boolean theHasTags) {
134                myHasTags = theHasTags;
135        }
136
137        static Date cloneDate(Date theDate) {
138                Date retVal = theDate;
139                if (retVal != null) {
140                        retVal = new Date(retVal.getTime());
141                }
142                return retVal;
143        }
144}