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 com.google.common.hash.Hasher;
023import com.google.common.hash.Hashing;
024import jakarta.persistence.Column;
025import jakarta.persistence.Entity;
026import jakarta.persistence.Id;
027import jakarta.persistence.Table;
028import jakarta.persistence.UniqueConstraint;
029import org.apache.commons.lang3.Validate;
030
031import java.nio.charset.StandardCharsets;
032
033/**
034 * Used for Identifier system values and potentially other system URLs in FHIR
035 */
036@Entity
037@Table(
038                name = "HFJ_RES_SYSTEM",
039                uniqueConstraints = {
040                        @UniqueConstraint(
041                                        name = "IDX_RESIDENT_SYS",
042                                        columnNames = {"SYSTEM_URL"})
043                })
044public class ResourceSystemEntity {
045
046        /**
047         * This column stores a hash of the system URL
048         */
049        @Column(name = "PID")
050        @Id
051        private Long myPid;
052
053        @Column(name = "SYSTEM_URL", length = 500, nullable = false)
054        private String mySystem;
055
056        public Long getPid() {
057                return myPid;
058        }
059
060        @SuppressWarnings("UnstableApiUsage")
061        public void setSystem(String theSystem) {
062                Validate.notBlank(theSystem, "System URL cannot be blank");
063                Validate.isTrue(theSystem.length() <= 500, "System URL cannot be longer than 500 characters: %s", theSystem);
064                mySystem = theSystem;
065
066                Hasher hasher = Hashing.murmur3_128(0).newHasher();
067                hasher.putBytes(theSystem.getBytes(StandardCharsets.UTF_8));
068                myPid = hasher.hash().asLong();
069        }
070}