Skip to content

Commit

Permalink
connect patient to postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Chin committed Jun 22, 2021
1 parent edc7b5f commit 0f71662
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Expand Up @@ -34,6 +34,13 @@
<scope>runtime</scope>
</dependency>>

<!-- Support for JSON columns in hibernate-->
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>2.11.1</version>
</dependency>

<!-- This dependency includes the core HAPI-FHIR classes -->
<dependency>
<groupId>ca.uhn.hapi.fhir</groupId>
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/com/example/fhirserver/entities/PatientEntity.java
@@ -0,0 +1,39 @@
package com.example.fhirserver.entities;

import com.vladmihalcea.hibernate.type.json.JsonType;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;
import org.hl7.fhir.r4.model.Patient;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;

@TypeDefs({ @TypeDef(name = "json", typeClass = JsonType.class) })
@Entity
@Table(name = "fhir_patient")
@EntityListeners(AuditingEntityListener.class)
public class PatientEntity implements Serializable {

// we need _id to be generated safely in conjunction with other clients of
// the postgres table.
// To find your table's sequence, run:
// select c.relname from pg_class c where c.relkind = 'S';
@Id
@Column(name = "_id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "patient_id_generator")
@SequenceGenerator(name = "patient_id_generator", sequenceName = "fhir_patient__id_seq", allocationSize = 1)
private long id;

@Type(type = "json")
@Column(name = "resource", nullable = false, columnDefinition = "jsonb")
private Patient resource;

public long getId() {
return id;
}

public Patient getResource() {
return resource;
}
}
Expand Up @@ -2,26 +2,32 @@

import ca.uhn.fhir.rest.annotation.*;
import ca.uhn.fhir.rest.server.IResourceProvider;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import com.example.fhirserver.repositories.PatientRepository;
import com.example.fhirserver.entities.PatientEntity;
import org.hl7.fhir.r4.model.IdType;
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.ResourceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PatientProvider implements IResourceProvider {

@Autowired
private PatientRepository patientRepository;

@Override
public Class<Patient> getResourceType() {
return Patient.class;
}

@Read()
public Patient read(@IdParam IdType theId) {

// Create a dummy Patient resource to return
// in reality, we'd use theId to query the database!
Patient patient = new Patient();
patient.addName().setFamily("Lord").addGiven("Farquaad");
return patient;
Long resourceId = theId.getIdPartAsLong();
return this.patientRepository.findById(resourceId).map(PatientEntity::getResource)
.orElseThrow(() -> new ResourceNotFoundException(
String.format("Could not find %s with id=%d", ResourceType.Patient.name(), resourceId)));
}

}
@@ -0,0 +1,6 @@
package com.example.fhirserver.repositories;

import com.example.fhirserver.entities.PatientEntity;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PatientRepository extends JpaRepository<PatientEntity, Long> {}

0 comments on commit 0f71662

Please sign in to comment.