Skip to content

Commit 9828d8b

Browse files
committed
improve user notification impl
* separate user notifications from others: introduction of NotificationServiceForUser * simplify and dedup user notification logic in NotificationServiceForUser * refactor MailService: async not needed for mail for user notifications, because the whole user notification processing is done in async * UserRepository.getDetails(String ocppTag) is not needed. the use cases can be achieved via getOverview(..)
1 parent d45ced3 commit 9828d8b

File tree

7 files changed

+282
-327
lines changed

7 files changed

+282
-327
lines changed

src/main/java/de/rwth/idsg/steve/config/BeanConfiguration.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.format.support.FormattingConversionService;
4444
import org.springframework.http.converter.HttpMessageConverter;
4545
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
46+
import org.springframework.scheduling.annotation.EnableAsync;
4647
import org.springframework.scheduling.annotation.EnableScheduling;
4748
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
4849
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
@@ -67,6 +68,7 @@
6768
@Configuration
6869
@EnableWebMvc
6970
@EnableScheduling
71+
@EnableAsync
7072
@ComponentScan("de.rwth.idsg.steve")
7173
public class BeanConfiguration implements WebMvcConfigurer {
7274

src/main/java/de/rwth/idsg/steve/repository/UserRepository.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
public interface UserRepository {
3232
List<User.Overview> getOverview(UserQueryForm form);
3333
User.Details getDetails(int userPk);
34-
User.Details getDetails(String ocppTag);
3534

3635
void add(UserForm form);
3736
void update(UserForm form);

src/main/java/de/rwth/idsg/steve/repository/impl/UserRepositoryImpl.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import java.util.HashMap;
4646
import java.util.List;
4747
import java.util.Map;
48-
import java.util.Optional;
4948

5049
import static de.rwth.idsg.steve.utils.CustomDSL.includes;
5150
import static jooq.steve.db.Tables.USER_OCPP_TAG;
@@ -120,30 +119,6 @@ public User.Details getDetails(int userPk) {
120119
.build();
121120
}
122121

123-
@Override
124-
public User.Details getDetails(String ocppIdTag) {
125-
Map<Integer, List<User.OcppTagEntry>> ocppTagEntries = getOcppTagsInternal(null, ocppIdTag);
126-
if (CollectionUtils.isEmpty(ocppTagEntries)) {
127-
throw new SteveException("There is no user with OcppTag '%s'", ocppIdTag);
128-
}
129-
130-
Integer userPk = ocppTagEntries.keySet().iterator().next();
131-
132-
UserRecord ur = ctx.selectFrom(USER)
133-
.where(USER.USER_PK.equal(userPk))
134-
.fetchOne();
135-
136-
if (ur == null) {
137-
throw new SteveException("There is no user with id '%s'", userPk);
138-
}
139-
140-
return User.Details.builder()
141-
.userRecord(ur)
142-
.address(addressRepository.get(ctx, ur.getAddressPk()))
143-
.ocppTagEntries(ocppTagEntries.getOrDefault(userPk, List.of()))
144-
.build();
145-
}
146-
147122
@Override
148123
public void add(UserForm form) {
149124
ctx.transaction(configuration -> {

src/main/java/de/rwth/idsg/steve/service/MailService.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import de.rwth.idsg.steve.web.dto.SettingsForm.MailSettings;
2222

23+
import jakarta.mail.MessagingException;
24+
2325
import java.util.List;
2426

2527
/**
@@ -34,5 +36,7 @@ public interface MailService {
3436

3537
void sendAsync(String subject, String body);
3638

37-
void sendAsync(String subject, String body, List<String> eMailAddresses);
39+
void send(String subject, String body) throws MessagingException;
40+
41+
void send(String subject, String body, List<String> eMailAddresses);
3842
}

src/main/java/de/rwth/idsg/steve/service/MailServiceDefault.java

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
* You should have received a copy of the GNU General Public License
1717
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
*/
19-
2019
package de.rwth.idsg.steve.service;
2120

2221
import com.google.common.base.Strings;
@@ -37,9 +36,9 @@
3736
import jakarta.mail.internet.InternetAddress;
3837
import jakarta.mail.internet.MimeMessage;
3938

40-
import java.util.ArrayList;
4139
import java.util.List;
4240
import java.util.Properties;
41+
import java.util.function.Function;
4342

4443
/**
4544
* @author Sevket Goekay <[email protected]>
@@ -61,8 +60,7 @@ public MailSettings getSettings() {
6160
@Override
6261
public void sendTestMail() {
6362
try {
64-
List<String> noAddress = new ArrayList();
65-
send("Test", "Test", noAddress);
63+
send("Test", "Test");
6664
} catch (MessagingException e) {
6765
throw new SteveException("Failed to send mail", e);
6866
}
@@ -72,26 +70,28 @@ public void sendTestMail() {
7270
public void sendAsync(String subject, String body) {
7371
asyncTaskExecutor.execute(() -> {
7472
try {
75-
List<String> noAddress = new ArrayList();
76-
send(subject, body, noAddress);
73+
send(subject, body);
7774
} catch (MessagingException e) {
7875
log.error("Failed to send mail", e);
7976
}
8077
});
8178
}
8279

8380
@Override
84-
public void sendAsync(String subject, String body, List<String> eMailAddresses) {
85-
asyncTaskExecutor.execute(() -> {
86-
try {
87-
send(subject, body, eMailAddresses);
88-
} catch (MessagingException e) {
89-
log.error("Failed to send mail", e);
90-
}
91-
});
81+
public void send(String subject, String body) throws MessagingException {
82+
sendInternal(subject, body, MailSettings::getRecipients);
83+
}
84+
85+
@Override
86+
public void send(String subject, String body, List<String> eMailAddresses) {
87+
try {
88+
sendInternal(subject, body, mailSettings -> eMailAddresses);
89+
} catch (MessagingException e) {
90+
log.error("Failed to send mail", e);
91+
}
9292
}
9393

94-
private void send(String subject, String body, List<String> eMailAddresses) throws MessagingException {
94+
public void sendInternal(String subject, String body, Function<MailSettings, List<String>> emailAddressProvider) throws MessagingException {
9595
MailSettings settings = getSettings();
9696
Session session = createSession(settings);
9797

@@ -100,21 +100,14 @@ private void send(String subject, String body, List<String> eMailAddresses) thro
100100
mail.setContent(body, "text/plain");
101101
mail.setFrom(new InternetAddress(settings.getFrom()));
102102

103-
if (eMailAddresses.isEmpty()) {
104-
eMailAddresses = settings.getRecipients();
105-
}
106-
107-
for (String rep : eMailAddresses) {
103+
for (String rep : emailAddressProvider.apply(settings)) {
108104
mail.addRecipient(Message.RecipientType.TO, new InternetAddress(rep));
109105
}
110106

111107
try (Transport transport = session.getTransport()) {
112108
transport.connect();
113109
transport.sendMessage(mail, mail.getAllRecipients());
114110
}
115-
catch (Exception e) {
116-
log.error("Failed to send mail(s)! ", e);
117-
}
118111
}
119112

120113
// -------------------------------------------------------------------------

0 commit comments

Comments
 (0)