|
1 | | - |
2 | 1 | /* |
3 | 2 | * Copyright (C) 2023 Dynamia Soluciones IT S.A.S - NIT 900302344-1 |
4 | 3 | * Colombia / South America |
|
18 | 17 |
|
19 | 18 | package tools.dynamia.modules.email.services; |
20 | 19 |
|
21 | | -import org.springframework.transaction.annotation.Transactional; |
22 | 20 | import tools.dynamia.modules.email.EmailMessage; |
23 | 21 | import tools.dynamia.modules.email.EmailSendResult; |
24 | 22 | import tools.dynamia.modules.email.domain.EmailAccount; |
25 | 23 | import tools.dynamia.modules.email.domain.EmailAddress; |
26 | 24 | import tools.dynamia.modules.email.domain.EmailTemplate; |
27 | 25 |
|
28 | | -import java.util.concurrent.Future; |
| 26 | +import java.util.concurrent.CompletableFuture; |
29 | 27 |
|
30 | 28 | /** |
31 | | - * Email service for sending emails |
| 29 | + * Email service for sending emails and managing email-related resources. |
| 30 | + * <p> |
| 31 | + * This service defines the contract to send emails synchronously and asynchronously, resolve |
| 32 | + * preferred/notification accounts, manage templates, log addresses, and control internal caches. |
| 33 | + * Implementations may rely on Spring's async and scheduling infrastructure. |
| 34 | + * </p> |
32 | 35 | * |
33 | 36 | * @author Mario Serrano Leones |
34 | 37 | */ |
35 | 38 | public interface EmailService { |
36 | 39 |
|
37 | 40 | /** |
38 | | - * Send email message asynchronously. Default implementation use Spring Scheduling and Async API. Make sure your |
39 | | - * application has {@link org.springframework.scheduling.annotation.EnableAsync} and |
40 | | - * {@link org.springframework.scheduling.annotation.EnableScheduling} configured. |
| 41 | + * Sends an email message asynchronously. |
| 42 | + * <p> |
| 43 | + * Default implementations are expected to use Spring's {@code @EnableAsync} and scheduling facilities. |
| 44 | + * Ensure your application has {@link org.springframework.scheduling.annotation.EnableAsync} and |
| 45 | + * {@link org.springframework.scheduling.annotation.EnableScheduling} enabled so the task executor handles |
| 46 | + * background execution properly. |
| 47 | + * </p> |
41 | 48 | * |
42 | | - * @param message |
| 49 | + * @param message Fully built {@link EmailMessage} to be sent, including recipients, subject, content, |
| 50 | + * attachments, and any headers. |
| 51 | + * @return a {@link CompletableFuture} that completes with an {@link EmailSendResult} indicating success or failure. |
| 52 | + * The future is completed exceptionally if an unrecoverable error occurs during dispatch. |
43 | 53 | */ |
44 | | - Future<EmailSendResult> send(EmailMessage message); |
| 54 | + CompletableFuture<EmailSendResult> send(EmailMessage message); |
45 | 55 |
|
46 | 56 | /** |
47 | | - * Build and send email message asynchronously. See {@link EmailService#send(EmailMessage)} |
| 57 | + * Builds and sends an email message asynchronously based on simple inputs. |
| 58 | + * <p> |
| 59 | + * This is a convenience method that internally creates an {@link EmailMessage} with a single recipient, |
| 60 | + * subject, and content, and then delegates to {@link #send(EmailMessage)}. |
| 61 | + * </p> |
48 | 62 | * |
49 | | - * @param to |
50 | | - * @param subject |
51 | | - * @param content |
| 63 | + * @param to Recipient email address (e.g., "user@example.com"). Must be a valid RFC 5322 address. |
| 64 | + * @param subject Email subject line. |
| 65 | + * @param content Email body content. Implementations may treat this as plain text or HTML depending on configuration. |
| 66 | + * @return a {@link CompletableFuture} that completes with an {@link EmailSendResult} when sending finishes. |
52 | 67 | */ |
53 | | - Future<EmailSendResult> send(String to, String subject, String content); |
| 68 | + CompletableFuture<EmailSendResult> send(String to, String subject, String content); |
54 | 69 |
|
| 70 | + /** |
| 71 | + * Returns the default notification {@link EmailAccount} to be used for system-generated emails |
| 72 | + * in the current SaaS context. |
| 73 | + * |
| 74 | + * @return the configured notification email account, or {@code null} if none is configured. |
| 75 | + */ |
55 | 76 | EmailAccount getNotificationEmailAccount(); |
56 | 77 |
|
| 78 | + /** |
| 79 | + * Returns the notification {@link EmailAccount} associated with the given SaaS account id. |
| 80 | + * |
| 81 | + * @param accountId SaaS account identifier. |
| 82 | + * @return the notification email account for the provided {@code accountId}, or {@code null} if not found. |
| 83 | + */ |
57 | 84 | EmailAccount getNotificationEmailAccount(Long accountId); |
58 | 85 |
|
59 | 86 | /** |
60 | | - * Setup preferred email account in current SaaS account |
| 87 | + * Sets the preferred {@link EmailAccount} for the current SaaS account. |
| 88 | + * Implementations should persist this preference so subsequent calls to |
| 89 | + * {@link #getPreferredEmailAccount()} return this account. |
61 | 90 | * |
62 | | - * @param account |
| 91 | + * @param account The {@link EmailAccount} to set as preferred. Must be a valid, enabled account. |
63 | 92 | */ |
64 | 93 | void setPreferredEmailAccount(EmailAccount account); |
65 | 94 |
|
| 95 | + /** |
| 96 | + * Sends an email message synchronously and waits for the result. |
| 97 | + * <p> |
| 98 | + * Use this method when you need immediate feedback about delivery status. Consider timeouts and |
| 99 | + * potential blocking behavior in your calling thread. |
| 100 | + * </p> |
| 101 | + * |
| 102 | + * @param mailMessage Fully built {@link EmailMessage} to be sent. |
| 103 | + * @return the {@link EmailSendResult} containing delivery details, success flag, and error information if any. |
| 104 | + */ |
66 | 105 | EmailSendResult sendAndWait(EmailMessage mailMessage); |
67 | 106 |
|
68 | 107 | /** |
69 | | - * Get preferred email account in current SaaS account |
| 108 | + * Returns the preferred {@link EmailAccount} for the current SaaS account. |
70 | 109 | * |
71 | | - * @return |
| 110 | + * @return the preferred account, or {@code null} if none has been configured. |
72 | 111 | */ |
73 | 112 | EmailAccount getPreferredEmailAccount(); |
74 | 113 |
|
75 | 114 | /** |
76 | | - * Get preferred email account in SaaS account ID |
| 115 | + * Returns the preferred {@link EmailAccount} for the specified SaaS account id. |
77 | 116 | * |
78 | | - * @return |
| 117 | + * @param accountId SaaS account identifier. |
| 118 | + * @return the preferred email account for {@code accountId}, or {@code null} if not configured. |
79 | 119 | */ |
80 | 120 | EmailAccount getPreferredEmailAccount(Long accountId); |
81 | 121 |
|
82 | 122 | /** |
83 | | - * Find email template by name in current SaaS account. If autocreate is true a new blank email template is created |
| 123 | + * Finds an {@link EmailTemplate} by name within the current SaaS account. |
| 124 | + * If {@code autocreate} is {@code true} and the template does not exist, a new blank template will be created |
| 125 | + * and returned. |
84 | 126 | * |
85 | | - * @param name |
86 | | - * @param autocreate |
87 | | - * @return |
| 127 | + * @param name Template name to search for. |
| 128 | + * @param autocreate Whether to create a new template if one is not found. |
| 129 | + * @return the existing or newly created {@link EmailTemplate}, or {@code null} if not found and {@code autocreate} |
| 130 | + * is {@code false}. |
88 | 131 | */ |
89 | 132 | EmailTemplate getTemplateByName(String name, boolean autocreate); |
90 | 133 |
|
| 134 | + /** |
| 135 | + * Finds an {@link EmailTemplate} by name for a specific SaaS account id. |
| 136 | + * If {@code autocreate} is {@code true} and the template does not exist, a new blank template will be created |
| 137 | + * under that account. |
| 138 | + * |
| 139 | + * @param name Template name to search for. |
| 140 | + * @param autocreate Whether to create a new template if one is not found. |
| 141 | + * @param accountId SaaS account identifier. |
| 142 | + * @return the existing or newly created {@link EmailTemplate}, or {@code null} if not found and {@code autocreate} |
| 143 | + * is {@code false}. |
| 144 | + */ |
91 | 145 | EmailTemplate getTemplateByName(String name, boolean autocreate, Long accountId); |
92 | 146 |
|
93 | 147 | /** |
94 | | - * Find email template by name |
| 148 | + * Finds an {@link EmailTemplate} by name using default lookup rules for the current SaaS account. |
95 | 149 | * |
96 | | - * @param name |
97 | | - * @return |
| 150 | + * @param name Template name to search for. |
| 151 | + * @return the matching {@link EmailTemplate}, or {@code null} if none exists. |
98 | 152 | */ |
99 | 153 | EmailTemplate getTemplateByName(String name); |
100 | 154 |
|
101 | 155 | /** |
102 | | - * Log all email address from message |
| 156 | + * Logs all email addresses present in the given {@link EmailMessage}. |
| 157 | + * <p> |
| 158 | + * Implementations should inspect TO, CC, BCC, REPLY-TO (and possibly FROM) fields and persist or update |
| 159 | + * a registry of known {@link EmailAddress} entries associated with the supplied {@link EmailAccount}. |
| 160 | + * </p> |
103 | 161 | * |
104 | | - * @param message |
| 162 | + * @param account The {@link EmailAccount} context in which addresses will be logged. |
| 163 | + * @param message The {@link EmailMessage} whose addresses should be extracted and logged. |
105 | 164 | */ |
106 | 165 | void logEmailAddress(EmailAccount account, EmailMessage message); |
107 | 166 |
|
108 | 167 | /** |
109 | | - * Log email address |
| 168 | + * Logs a single email address with an optional tag for classification. |
110 | 169 | * |
111 | | - * @param address |
112 | | - * @param tag |
| 170 | + * @param account The {@link EmailAccount} context used to associate the address entry. |
| 171 | + * @param address A valid email address string to log. |
| 172 | + * @param tag A marker or label (e.g., "customer", "supplier", "notification") to categorize the address. |
113 | 173 | */ |
114 | 174 | void logEmailAddress(EmailAccount account, String address, String tag); |
115 | 175 |
|
116 | 176 | /** |
117 | | - * Find a logged email address |
| 177 | + * Retrieves a previously logged {@link EmailAddress} by its string representation. |
118 | 178 | * |
119 | | - * @param address |
120 | | - * @return |
| 179 | + * @param address Email address string to look up. |
| 180 | + * @return the {@link EmailAddress} entity if found; otherwise {@code null}. |
121 | 181 | */ |
122 | 182 | EmailAddress getEmailAddress(String address); |
123 | 183 |
|
124 | 184 | /** |
125 | | - * Clears the mail sender cache for the specified email account. |
| 185 | + * Clears the internal mail-sender cache for the specified {@link EmailAccount}. |
| 186 | + * <p> |
| 187 | + * Implementations that cache mail sender instances (e.g., JavaMailSender) should discard and recreate them |
| 188 | + * after configuration changes like credentials, host, or port updates. |
| 189 | + * </p> |
126 | 190 | * |
127 | | - * @param account The email account for which to clear the cache. |
| 191 | + * @param account The email account whose cache entries should be invalidated. Must not be {@code null}. |
128 | 192 | */ |
129 | 193 | void clearCache(EmailAccount account); |
130 | 194 | } |
0 commit comments