Skip to content
98 changes: 98 additions & 0 deletions src/main/java/org/example/vet1177/entities/Attachment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.example.vet1177.entities;

import jakarta.persistence.*;
import org.hibernate.annotations.OnDelete;
import org.hibernate.annotations.OnDeleteAction;

import java.time.Instant;
import java.util.UUID;

@Entity
@Table(name = "attachment")
public class Attachment {

@Id
@GeneratedValue(strategy = GenerationType.UUID)
private UUID id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "record_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
private MedicalRecord medicalRecord;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "uploaded_by", nullable = false)
@OnDelete(action = OnDeleteAction.SET_NULL)
private User uploadedBy;
Comment thread
johanbriger marked this conversation as resolved.
Comment thread
coderabbitai[bot] marked this conversation as resolved.

@Column(name = "file_name", nullable = false, length = 500)
private String fileName;

// Den unika nyckeln/sökvägen i S3-bucket
@Column(name = "s3_key", nullable = false, unique = true, length = 1000)
private String s3Key;

@Column(name = "s3_bucket", nullable = false, length = 255)
private String s3Bucket;

@Column(name = "file_type", length = 100)
private String fileType;

@Column(name = "file_size_bytes")
private Long fileSizeBytes;

@Column(name = "uploaded_at", nullable = false, updatable = false)
private Instant uploadedAt;

public Attachment() {
}

@PrePersist
protected void onCreate() {
this.uploadedAt = Instant.now();
}

// Getters och Setters
public UUID getId() { return id; }

public MedicalRecord getMedicalRecord() { return medicalRecord; }

public void setMedicalRecord(MedicalRecord medicalRecord) {
this.medicalRecord = medicalRecord; }

public User getUploadedBy() {
return uploadedBy; }

public void setUploadedBy(User uploadedBy) {
this.uploadedBy = uploadedBy; }

public String getFileName() {
return fileName; }

public void setFileName(String fileName) {
this.fileName = fileName; }

public String getS3Key() {
return s3Key; }

public void setS3Key(String s3Key) {
this.s3Key = s3Key; }

public String getS3Bucket() {
return s3Bucket; }

public void setS3Bucket(String s3Bucket) {
this.s3Bucket = s3Bucket; }

public String getFileType() { return fileType; }
public void setFileType(String fileType) { this.fileType = fileType; }

public Long getFileSizeBytes() {
return fileSizeBytes; }

public void setFileSizeBytes(Long fileSizeBytes) {
this.fileSizeBytes = fileSizeBytes; }

public Instant getUploadedAt() {
return uploadedAt; }
}
37 changes: 37 additions & 0 deletions src/main/java/org/example/vet1177/entities/MedicalRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import jakarta.persistence.*;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Entity
Expand Down Expand Up @@ -47,6 +49,9 @@ public class MedicalRecord {
@JoinColumn(name = "updated_by")
private User updatedBy;

@OneToMany(mappedBy = "medicalRecord", cascade = CascadeType.ALL, orphanRemoval = true)
private java.util.List<Attachment> attachments = new ArrayList<>();

// Timestamps
@Column(name = "created_at", updatable = false)
private Instant createdAt;
Expand Down Expand Up @@ -109,6 +114,38 @@ public MedicalRecord() {}
public User getUpdatedBy() { return updatedBy; }
public void setUpdatedBy(User updatedBy) { this.updatedBy = updatedBy; }


public List<Attachment> getAttachments() {
return java.util.Collections.unmodifiableList(attachments);
}

public void setAttachments(List<Attachment> newAttachments) {
this.attachments.clear();
if (newAttachments != null) {
for (Attachment attachment : newAttachments) {
this.addAttachment(attachment);
}
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.


public void addAttachment(Attachment attachment) {
if (attachment != null) {
this.attachments.add(attachment);
// Sätt baksidan av relationen om den inte redan är satt
if (attachment.getMedicalRecord() != this) {
attachment.setMedicalRecord(this);
}
}
}

public void removeAttachment(Attachment attachment) {
if (attachment != null) {
this.attachments.remove(attachment);
attachment.setMedicalRecord(null);
}
}

public Instant getCreatedAt() { return createdAt; }
public Instant getUpdatedAt() { return updatedAt; }
public Instant getClosedAt() { return closedAt; }
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/org/example/vet1177/entities/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import jakarta.persistence.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Entity
Expand All @@ -24,6 +26,11 @@ public class User {
@Enumerated(EnumType.STRING)
private Role role; //role använder enum OWNER, VET, ADMIN

// Koppling till bilagor som användaren laddat upp
// Vi använder inte CascadeType.REMOVE för att skydda medicinsk data om en användare raderas
@OneToMany(mappedBy = "uploadedBy", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private List<Attachment> uploadedAttachments = new ArrayList<>();
Comment thread
johanbriger marked this conversation as resolved.
Outdated

public User() {
} //tom konsturktor för JPA

Expand Down Expand Up @@ -69,4 +76,33 @@ public Role getRole() {
public void setRole(Role role) {
this.role = role;
}


public List<Attachment> getUploadedAttachments() {
return java.util.Collections.unmodifiableList(uploadedAttachments);
}

public void setUploadedAttachments(List<Attachment> attachments) {
this.uploadedAttachments.clear();
if (attachments != null) {
attachments.forEach(this::addAttachment);
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

public void addAttachment(Attachment attachment) {
if (attachment != null) {
this.uploadedAttachments.add(attachment);
// Säkerställ att bilagan pekar på denna användare
if (attachment.getUploadedBy() != this) {
attachment.setUploadedBy(this);
}
}
}

public void removeAttachment(Attachment attachment) {
if (attachment != null) {
this.uploadedAttachments.remove(attachment);
attachment.setUploadedBy(null);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.example.vet1177.repository;

import org.example.vet1177.entities.Attachment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Repository
public interface AttachmentRepository extends JpaRepository<Attachment, UUID> {

List<Attachment> findByMedicalRecordId(UUID recordId);

List<Attachment> findByUploadedById(UUID userId);

Optional<Attachment> findByS3Key(String s3Key);
}
4 changes: 2 additions & 2 deletions src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ CREATE TABLE IF NOT EXISTS comment (

CREATE TABLE IF NOT EXISTS attachment (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
record_id UUID NOT NULL REFERENCES medical_record(id),
uploaded_by UUID NOT NULL REFERENCES users(id),
record_id UUID NOT NULL REFERENCES medical_record(id) ON DELETE CASCADE,
uploaded_by UUID REFERENCES users(id) ON DELETE SET NULL,
file_name VARCHAR(500) NOT NULL,
s3_key VARCHAR(1000) NOT NULL UNIQUE,
s3_bucket VARCHAR(255) NOT NULL,
Expand Down