Skip to content

Commit c430bcc

Browse files
committed
use entity graph and rework attachment file reference
1 parent 6f49809 commit c430bcc

12 files changed

Lines changed: 48 additions & 42 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.springframework.boot</groupId>
88
<artifactId>spring-boot-starter-parent</artifactId>
9-
<version>3.1.1</version>
9+
<version>3.1.4</version>
1010
<relativePath/> <!-- lookup parent from repository -->
1111
</parent>
1212

src/main/java/it/aboutbits/springboot/emailservice/lib/AttachmentDataSource.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import java.io.InputStream;
66

77
public interface AttachmentDataSource {
8-
InputStream getAttachmentPayload(AttachmentReference reference) throws AttachmentException;
8+
InputStream getAttachmentPayload(long fileReference) throws AttachmentException;
99

10-
AttachmentReference storeAttachmentPayload(InputStream payload) throws AttachmentException;
10+
long storeAttachmentPayload(InputStream payload) throws AttachmentException;
1111

12-
void releaseAttachment(AttachmentReference reference) throws AttachmentException;
12+
void releaseAttachment(long fileReference) throws AttachmentException;
1313
}

src/main/java/it/aboutbits/springboot/emailservice/lib/AttachmentReference.java

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/main/java/it/aboutbits/springboot/emailservice/lib/EmailAttachmentDto.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ public record EmailAttachmentDto(
1111

1212
String contentType,
1313

14-
AttachmentReference reference
14+
long fileReference
1515
) {
1616
}
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package it.aboutbits.springboot.emailservice.lib.application;
22

3-
import it.aboutbits.springboot.emailservice.lib.AttachmentReference;
43
import it.aboutbits.springboot.emailservice.lib.EmailAttachmentDto;
54
import it.aboutbits.springboot.emailservice.lib.model.EmailAttachment;
65
import org.mapstruct.Mapper;
@@ -11,9 +10,5 @@
1110
public interface EmailAttachmentMapper {
1211
EmailAttachmentDto toDto(EmailAttachment model);
1312

14-
default AttachmentReference map(String value) {
15-
return new AttachmentReference(value);
16-
}
17-
1813
List<EmailAttachmentDto> toDto(List<EmailAttachment> model);
1914
}

src/main/java/it/aboutbits/springboot/emailservice/lib/application/EmailServiceMigrator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ updated_at timestamp with time zone default now() not null,
4444
4545
create index if not exists email_service_emails_scheduled_at_index
4646
on email_service_emails (scheduled_at);
47-
47+
4848
create index if not exists email_service_emails_attachments_cleaned_index
4949
on email_service_emails (attachments_cleaned);
5050
@@ -57,7 +57,7 @@ updated_at timestamp with time zone default now() not null,
5757
references email_service_emails,
5858
file_name text not null,
5959
content_type text not null,
60-
reference text not null
60+
file_reference bigint not null
6161
);
6262
6363
create index if not exists email_service_email_attachments_email_id_index

src/main/java/it/aboutbits/springboot/emailservice/lib/application/ManageEmail.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33

44
import it.aboutbits.springboot.emailservice.lib.AttachmentDataSource;
5-
import it.aboutbits.springboot.emailservice.lib.AttachmentReference;
65
import it.aboutbits.springboot.emailservice.lib.EmailDto;
76
import it.aboutbits.springboot.emailservice.lib.EmailState;
87
import it.aboutbits.springboot.emailservice.lib.exception.AttachmentException;
@@ -98,7 +97,7 @@ Email send(Email email) {
9897

9998
void cleanupAttachments(final Email email) throws AttachmentException {
10099
for (var attachment : email.getAttachments()) {
101-
attachmentDataSource.releaseAttachment(new AttachmentReference(attachment.getReference()));
100+
attachmentDataSource.releaseAttachment(attachment.getFileReference());
102101
}
103102
email.setAttachmentsCleaned(true);
104103
emailRepository.save(email);
@@ -123,9 +122,9 @@ private Email fromParameter(EmailParameter parameter) throws AttachmentException
123122

124123
var emailAttachment = new EmailAttachment();
125124
emailAttachment.setEmail(email);
126-
emailAttachment.setReference(reference.value());
127125
emailAttachment.setContentType(attachment.contentType());
128126
emailAttachment.setFileName(attachment.fileName());
127+
emailAttachment.setFileReference(reference);
129128

130129
attachments.add(emailAttachment);
131130
}
@@ -162,7 +161,7 @@ private void sendMail(String fromAddress, String fromName, List<String> recipien
162161
}
163162

164163
for (var attachment : attachments) {
165-
var payload = attachmentDataSource.getAttachmentPayload(new AttachmentReference(attachment.getReference()));
164+
var payload = attachmentDataSource.getAttachmentPayload(attachment.getFileReference());
166165
helper.addAttachment(attachment.getFileName(), new ByteArrayResource(payload.readAllBytes()));
167166
payload.close();
168167
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
package it.aboutbits.springboot.emailservice.lib.application;
22

33
import it.aboutbits.springboot.emailservice.lib.AttachmentDataSource;
4-
import it.aboutbits.springboot.emailservice.lib.AttachmentReference;
54
import it.aboutbits.springboot.emailservice.lib.exception.AttachmentException;
65

76
import java.io.InputStream;
87

98
public final class UnavailableAttachmentDataSource implements AttachmentDataSource {
109
@Override
11-
public InputStream getAttachmentPayload(AttachmentReference reference) throws AttachmentException {
10+
public InputStream getAttachmentPayload(long fileReference) throws AttachmentException {
1211
throw new AttachmentException("attachments not available");
1312
}
1413

1514
@Override
16-
public AttachmentReference storeAttachmentPayload(InputStream payload) throws AttachmentException {
15+
public long storeAttachmentPayload(InputStream payload) throws AttachmentException {
1716
throw new AttachmentException("attachments not available");
1817
}
1918

2019
@Override
21-
public void releaseAttachment(AttachmentReference reference) throws AttachmentException {
20+
public void releaseAttachment(long fileReference) throws AttachmentException {
2221
throw new AttachmentException("attachments not available");
2322
}
2423
}

src/main/java/it/aboutbits/springboot/emailservice/lib/jpa/EmailRepository.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,33 @@
55
import it.aboutbits.springboot.emailservice.lib.model.Email;
66
import org.springframework.data.domain.Page;
77
import org.springframework.data.domain.PageRequest;
8+
import org.springframework.data.jpa.repository.EntityGraph;
89
import org.springframework.data.jpa.repository.JpaRepository;
10+
import org.springframework.lang.NonNull;
911

1012
import java.util.Collection;
1113
import java.util.List;
14+
import java.util.Optional;
1215

1316
public interface EmailRepository extends JpaRepository<Email, Long> {
17+
@Override
18+
@NonNull
19+
@EntityGraph(value = Email.DEFAULT_ENTITY_GRAPH)
20+
Optional<Email> findById(@NonNull Long id);
21+
22+
@Override
23+
@NonNull
24+
@EntityGraph(value = Email.DEFAULT_ENTITY_GRAPH)
25+
List<Email> findAllById(@NonNull Iterable<Long> ids);
26+
27+
@Override
28+
@NonNull
29+
@EntityGraph(value = Email.DEFAULT_ENTITY_GRAPH)
30+
List<Email> findAll();
31+
32+
@EntityGraph(value = Email.DEFAULT_ENTITY_GRAPH)
1433
Page<Email> findByState(EmailState state, PageRequest pageRequest);
1534

35+
@EntityGraph(value = Email.DEFAULT_ENTITY_GRAPH)
1636
List<Email> findByIdIn(Collection<Long> ids);
1737
}

src/main/java/it/aboutbits/springboot/emailservice/lib/model/Email.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import jakarta.persistence.Entity;
66
import jakarta.persistence.EnumType;
77
import jakarta.persistence.Enumerated;
8-
import jakarta.persistence.FetchType;
98
import jakarta.persistence.GeneratedValue;
109
import jakarta.persistence.GenerationType;
1110
import jakarta.persistence.Id;
@@ -27,6 +26,8 @@
2726
import java.util.List;
2827
import java.util.Set;
2928

29+
import static it.aboutbits.springboot.emailservice.lib.model.Email.DEFAULT_ENTITY_GRAPH;
30+
3031
@NamedEntityGraph(
3132
name = "email_service_emails-entity-graph",
3233
attributeNodes = {
@@ -40,7 +41,10 @@
4041
@AllArgsConstructor
4142
@NoArgsConstructor
4243
@Table(name = "email_service_emails")
44+
@NamedEntityGraph(name = DEFAULT_ENTITY_GRAPH, attributeNodes = @NamedAttributeNode("attachments"))
4345
public class Email {
46+
public static final String DEFAULT_ENTITY_GRAPH = "graph.EmailServiceEmail.default";
47+
4448
@Id
4549
@GeneratedValue(strategy = GenerationType.IDENTITY)
4650
private Long id;
@@ -59,7 +63,7 @@ public class Email {
5963
private String textBody;
6064
private String htmlBody;
6165

62-
@OneToMany(cascade = CascadeType.PERSIST, mappedBy = "email", orphanRemoval = true, fetch = FetchType.EAGER)
66+
@OneToMany(cascade = CascadeType.PERSIST, mappedBy = "email", orphanRemoval = true)
6367
private Set<EmailAttachment> attachments;
6468

6569
@Builder.Default

0 commit comments

Comments
 (0)