Skip to content

Commit 00968c1

Browse files
committed
refactor: introduce UserService
and encapsulate UserRepository calls
1 parent 053e4db commit 00968c1

File tree

3 files changed

+117
-56
lines changed

3 files changed

+117
-56
lines changed

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

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import com.google.common.base.Strings;
2222
import de.rwth.idsg.steve.NotificationFeature;
23-
import de.rwth.idsg.steve.repository.UserRepository;
2423
import de.rwth.idsg.steve.repository.dto.Transaction;
2524
import de.rwth.idsg.steve.repository.dto.User;
2625
import de.rwth.idsg.steve.service.notification.OcppStationStatusFailure;
@@ -51,7 +50,7 @@ public class NotificationServiceForUser {
5150

5251
private final MailService mailService;
5352
private final TransactionService transactionService;
54-
private final UserRepository userRepository;
53+
private final UserService userService;
5554

5655
@Async
5756
@EventListener
@@ -63,7 +62,7 @@ public void ocppStationStatusFailure(OcppStationStatusFailure event) {
6362
return;
6463
}
6564

66-
var user = getUserForMail(transaction.getOcppIdTag(), NotificationFeature.OcppStationStatusFailure);
65+
var user = userService.getUserForMail(transaction.getOcppIdTag(), NotificationFeature.OcppStationStatusFailure);
6766
if (user == null) {
6867
return;
6968
}
@@ -90,7 +89,7 @@ public void ocppStationStatusFailure(OcppStationStatusFailure event) {
9089
public void ocppTransactionStarted(OcppTransactionStarted event) {
9190
log.debug("Processing: {}", event);
9291

93-
var user = getUserForMail(event.getParams().getIdTag(), NotificationFeature.OcppTransactionStarted);
92+
var user = userService.getUserForMail(event.getParams().getIdTag(), NotificationFeature.OcppTransactionStarted);
9493
if (user == null) {
9594
return;
9695
}
@@ -123,7 +122,7 @@ public void ocppStationStatusSuspendedEV(OcppStationStatusSuspendedEV event) {
123122
return;
124123
}
125124

126-
var user = getUserForMail(transaction.getOcppIdTag(), NotificationFeature.OcppStationStatusSuspendedEV);
125+
var user = userService.getUserForMail(transaction.getOcppIdTag(), NotificationFeature.OcppStationStatusSuspendedEV);
127126
if (user == null) {
128127
return;
129128
}
@@ -151,7 +150,7 @@ public void ocppTransactionEnded(OcppTransactionEnded event) {
151150

152151
var transaction = transactionService.getTransaction(event.getParams().getTransactionId());
153152

154-
var user = getUserForMail(transaction.getOcppIdTag(), NotificationFeature.OcppTransactionEnded);
153+
var user = userService.getUserForMail(transaction.getOcppIdTag(), NotificationFeature.OcppTransactionEnded);
155154
if (user == null) {
156155
return;
157156
}
@@ -168,48 +167,6 @@ public void ocppTransactionEnded(OcppTransactionEnded event) {
168167
// Private helpers
169168
// -------------------------------------------------------------------------
170169

171-
private User.Overview getUserForMail(String ocppIdTag, NotificationFeature feature) {
172-
UserQueryForm form = new UserQueryForm();
173-
form.setOcppIdTag(ocppIdTag);
174-
175-
List<User.Overview> overview = userRepository.getOverview(form);
176-
if (overview.isEmpty()) {
177-
return null;
178-
} else if (overview.size() > 1) {
179-
// should not happen
180-
log.warn("Multiple users found for OcppTag {}", ocppIdTag);
181-
return null;
182-
}
183-
184-
var user = overview.get(0);
185-
if (!hasOcppTag(user, ocppIdTag)) {
186-
return null;
187-
}
188-
189-
String eMailAddress = user.getEmail();
190-
if (Strings.isNullOrEmpty(eMailAddress)) {
191-
return null;
192-
}
193-
194-
if (!user.getNotificationFeatures().contains(feature)) {
195-
return null;
196-
}
197-
198-
return user;
199-
}
200-
201-
/**
202-
* We check this here again, because userRepository.getOverview(..) also returns partial match OcppTags.
203-
*/
204-
private static boolean hasOcppTag(User.Overview user, String ocppIdTag) {
205-
for (User.OcppTagEntry ocppTagEntry : user.getOcppTagEntries()) {
206-
if (ocppTagEntry.getIdTag().equals(ocppIdTag)) {
207-
return true;
208-
}
209-
}
210-
return false;
211-
}
212-
213170
private static String createContent(Transaction params, User.Overview user) {
214171
Double consumption = TransactionStopServiceHelper.calculateEnergyConsumptionInKWh(params);
215172
String strMeterValueDiff = (consumption == null) ? "- kWh" : consumption + " kWh";
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* SteVe - SteckdosenVerwaltung - https://github.com/steve-community/steve
3+
* Copyright (C) 2013-2025 SteVe Community Team
4+
* All Rights Reserved.
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
package de.rwth.idsg.steve.service;
20+
21+
import com.google.common.base.Strings;
22+
import de.rwth.idsg.steve.NotificationFeature;
23+
import de.rwth.idsg.steve.repository.UserRepository;
24+
import de.rwth.idsg.steve.repository.dto.User;
25+
import de.rwth.idsg.steve.web.dto.UserForm;
26+
import de.rwth.idsg.steve.web.dto.UserQueryForm;
27+
import lombok.RequiredArgsConstructor;
28+
import lombok.extern.slf4j.Slf4j;
29+
import org.springframework.stereotype.Service;
30+
31+
import java.util.List;
32+
33+
/**
34+
* @author Sevket Goekay <[email protected]>
35+
* @since 29.10.2025
36+
*/
37+
@Slf4j
38+
@Service
39+
@RequiredArgsConstructor
40+
public class UserService {
41+
42+
private final UserRepository userRepository;
43+
44+
public User.Overview getUserForMail(String ocppIdTag, NotificationFeature feature) {
45+
UserQueryForm form = new UserQueryForm();
46+
form.setOcppIdTag(ocppIdTag);
47+
48+
List<User.Overview> overview = this.getOverview(form);
49+
if (overview.isEmpty()) {
50+
return null;
51+
} else if (overview.size() > 1) {
52+
// should not happen
53+
log.warn("Multiple users found for OcppTag {}", ocppIdTag);
54+
return null;
55+
}
56+
57+
var user = overview.get(0);
58+
if (!hasOcppTag(user, ocppIdTag)) {
59+
return null;
60+
}
61+
62+
String eMailAddress = user.getEmail();
63+
if (Strings.isNullOrEmpty(eMailAddress)) {
64+
return null;
65+
}
66+
67+
if (!user.getNotificationFeatures().contains(feature)) {
68+
return null;
69+
}
70+
71+
return user;
72+
}
73+
74+
public List<User.Overview> getOverview(UserQueryForm form) {
75+
return userRepository.getOverview(form);
76+
}
77+
78+
public User.Details getDetails(int userPk) {
79+
return userRepository.getDetails(userPk);
80+
}
81+
82+
public void add(UserForm form) {
83+
userRepository.add(form);
84+
}
85+
86+
public void update(UserForm form) {
87+
userRepository.update(form);
88+
}
89+
90+
public void delete(int userPk) {
91+
userRepository.delete(userPk);
92+
}
93+
94+
/**
95+
* We check this here again, because userRepository.getOverview(..) also returns partial match OcppTags.
96+
*/
97+
private static boolean hasOcppTag(User.Overview user, String ocppIdTag) {
98+
for (User.OcppTagEntry ocppTagEntry : user.getOcppTagEntries()) {
99+
if (ocppTagEntry.getIdTag().equals(ocppIdTag)) {
100+
return true;
101+
}
102+
}
103+
return false;
104+
}
105+
}

src/main/java/de/rwth/idsg/steve/web/controller/UsersController.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
package de.rwth.idsg.steve.web.controller;
2020

2121
import de.rwth.idsg.steve.NotificationFeature;
22-
import de.rwth.idsg.steve.repository.UserRepository;
2322
import de.rwth.idsg.steve.repository.dto.User;
2423
import de.rwth.idsg.steve.service.OcppTagService;
24+
import de.rwth.idsg.steve.service.UserService;
2525
import de.rwth.idsg.steve.utils.ControllerHelper;
2626
import de.rwth.idsg.steve.utils.mapper.UserFormMapper;
2727
import de.rwth.idsg.steve.web.dto.UserForm;
@@ -36,7 +36,6 @@
3636
import org.springframework.web.bind.annotation.RequestMethod;
3737

3838
import jakarta.validation.Valid;
39-
4039
import java.util.ArrayList;
4140
import java.util.List;
4241

@@ -50,7 +49,7 @@
5049
public class UsersController {
5150

5251
private final OcppTagService ocppTagService;
53-
private final UserRepository userRepository;
52+
private final UserService userService;
5453

5554
private static final String PARAMS = "params";
5655

@@ -83,13 +82,13 @@ public String getQuery(@ModelAttribute(PARAMS) UserQueryForm params, Model model
8382

8483
private void initList(Model model, UserQueryForm params) {
8584
model.addAttribute(PARAMS, params);
86-
model.addAttribute("userList", userRepository.getOverview(params));
85+
model.addAttribute("userList", userService.getOverview(params));
8786
model.addAttribute("features", NotificationFeature.getUserValues());
8887
}
8988

9089
@RequestMapping(value = DETAILS_PATH, method = RequestMethod.GET)
9190
public String getDetails(@PathVariable("userPk") int userPk, Model model) {
92-
User.Details details = userRepository.getDetails(userPk);
91+
User.Details details = userService.getDetails(userPk);
9392
UserForm form = UserFormMapper.toForm(details);
9493

9594
model.addAttribute("userForm", form);
@@ -112,7 +111,7 @@ public String addPost(@Valid @ModelAttribute("userForm") UserForm userForm,
112111
return "data-man/userAdd";
113112
}
114113

115-
userRepository.add(userForm);
114+
userService.add(userForm);
116115
return toOverview();
117116
}
118117

@@ -124,13 +123,13 @@ public String update(@Valid @ModelAttribute("userForm") UserForm userForm,
124123
return "data-man/userDetails";
125124
}
126125

127-
userRepository.update(userForm);
126+
userService.update(userForm);
128127
return toOverview();
129128
}
130129

131130
@RequestMapping(value = DELETE_PATH, method = RequestMethod.POST)
132131
public String delete(@PathVariable("userPk") int userPk) {
133-
userRepository.delete(userPk);
132+
userService.delete(userPk);
134133
return toOverview();
135134
}
136135

0 commit comments

Comments
 (0)