Skip to content

Commit 85d47cc

Browse files
committed
refactoring_method
1 parent eb57501 commit 85d47cc

File tree

7 files changed

+134
-40
lines changed

7 files changed

+134
-40
lines changed

src/main/java/ru/mystamps/web/config/ControllersConfig.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public RobotsTxtController getRobotsTxtController() {
9191
@Bean
9292
public ReportController getReportController() {
9393
return new ReportController(
94-
servicesConfig.getMailService(),
94+
servicesConfig.getReportService(),
9595
servicesConfig.getCronService()
9696
);
9797
}

src/main/java/ru/mystamps/web/config/ServicesConfig.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,24 @@ public MailService getMailService() {
9696
env.getProperty("app.mail.admin.email", "root@localhost"),
9797
new Locale(env.getProperty("app.mail.admin.lang", "en")),
9898
env.getRequiredProperty("app.mail.robot.email"),
99-
enableTestMode);
99+
enableTestMode,
100+
getReportService()
101+
);
100102
}
101103

102104
@Bean
103105
public UsersActivationService getUsersActivationService() {
104106
return new UsersActivationServiceImpl(daoConfig.getUsersActivationDao(), getMailService());
105107
}
106108

109+
@Bean
110+
public ReportService getReportService() {
111+
return new ReportServiceImpl(
112+
messageSource,
113+
new Locale(env.getProperty("app.mail.admin.lang", "en"))
114+
);
115+
}
116+
107117
@Bean
108118
public SeriesService getSeriesService() {
109119
return new SeriesServiceImpl(

src/main/java/ru/mystamps/web/controller/ReportController.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
import ru.mystamps.web.Url;
2727
import ru.mystamps.web.service.CronService;
28-
import ru.mystamps.web.service.MailService;
28+
import ru.mystamps.web.service.ReportService;
2929

3030
/**
3131
* @author Maxim Shestakov
@@ -34,13 +34,13 @@
3434
@RequiredArgsConstructor
3535
public class ReportController {
3636

37-
private final MailService mailService;
37+
private final ReportService reportService;
3838
private final CronService cronService;
3939

4040
@GetMapping(path = Url.DAILY_STATISTICS, produces = "text/plain; charset=UTF-8")
4141
@ResponseBody
4242
public String showDailyReport() {
43-
return mailService.prepareDailyStatistics(
43+
return reportService.prepareDailyStatistics(
4444
cronService.getDailyReport()
4545
);
4646
}

src/main/java/ru/mystamps/web/service/MailService.java

-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,4 @@
2323
public interface MailService {
2424
void sendActivationKeyToUser(SendUsersActivationDto activation);
2525
void sendDailyStatisticsToAdmin(AdminDailyReport report);
26-
String prepareDailyStatistics(AdminDailyReport report);
2726
}

src/main/java/ru/mystamps/web/service/MailServiceImpl.java

+4-34
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,10 @@
3939
import org.springframework.mail.javamail.MimeMessagePreparator;
4040
import org.springframework.scheduling.annotation.Async;
4141

42-
import org.springframework.security.access.prepost.PreAuthorize;
43-
4442
import ru.mystamps.web.Url;
4543
import ru.mystamps.web.service.dto.AdminDailyReport;
4644
import ru.mystamps.web.service.dto.SendUsersActivationDto;
4745
import ru.mystamps.web.service.exception.EmailSendingException;
48-
import ru.mystamps.web.support.spring.security.HasAuthority;
4946

5047
public class MailServiceImpl implements MailService {
5148
private static final Logger LOG = LoggerFactory.getLogger(MailServiceImpl.class);
@@ -57,15 +54,18 @@ public class MailServiceImpl implements MailService {
5754
private final String robotEmail;
5855
private final boolean testMode;
5956
private final DatePrinter shortDatePrinter;
57+
private final ReportService reportService;
6058

6159
public MailServiceImpl(
60+
ReportService reportService,
6261
JavaMailSender mailer,
6362
MessageSource messageSource,
6463
String adminEmail,
6564
Locale adminLang,
6665
String robotEmail,
6766
boolean testMode) {
6867

68+
this.reportService = reportService;
6969
this.mailer = mailer;
7070
this.messageSource = messageSource;
7171
this.adminEmail = adminEmail;
@@ -104,7 +104,7 @@ public void sendDailyStatisticsToAdmin(AdminDailyReport report) {
104104
sendMail(
105105
adminEmail,
106106
getSubjectOfDailyStatisticsMail(report),
107-
prepareDailyStatistics(report),
107+
reportService.prepareDailyStatistics(report),
108108
"daily_statistics"
109109
);
110110

@@ -118,36 +118,6 @@ public void sendDailyStatisticsToAdmin(AdminDailyReport report) {
118118
);
119119
}
120120

121-
@Override
122-
@PreAuthorize(HasAuthority.VIEW_DAILY_STATS)
123-
public String prepareDailyStatistics(AdminDailyReport report) {
124-
String template = messageSource.getMessage("daily_stat.text", null, adminLang);
125-
String fromDate = shortDatePrinter.format(report.getStartDate());
126-
String tillDate = shortDatePrinter.format(report.getEndDate());
127-
128-
Map<String, String> ctx = new HashMap<>();
129-
ctx.put("from_date", fromDate);
130-
ctx.put("to_date", tillDate);
131-
132-
put(ctx, "added_countries_cnt", report.getAddedCountriesCounter());
133-
put(ctx, "untranslated_countries_cnt", report.getUntranslatedCountriesCounter());
134-
put(ctx, "added_categories_cnt", report.getAddedCategoriesCounter());
135-
put(ctx, "untranslated_categories_cnt", report.getUntranslatedCategoriesCounter());
136-
put(ctx, "added_series_cnt", report.getAddedSeriesCounter());
137-
put(ctx, "updated_series_cnt", report.getUpdatedSeriesCounter());
138-
put(ctx, "updated_collections_cnt", report.getUpdatedCollectionsCounter());
139-
put(ctx, "registration_requests_cnt", report.getRegistrationRequestsCounter());
140-
put(ctx, "registered_users_cnt", report.getRegisteredUsersCounter());
141-
put(ctx, "events_cnt", report.countEvents());
142-
put(ctx, "not_found_cnt", report.getNotFoundCounter());
143-
put(ctx, "failed_auth_cnt", report.getFailedAuthCounter());
144-
put(ctx, "missing_csrf_cnt", report.getMissingCsrfCounter());
145-
put(ctx, "invalid_csrf_cnt", report.getInvalidCsrfCounter());
146-
put(ctx, "bad_request_cnt", -1L); // TODO: #122
147-
148-
return new StrSubstitutor(ctx).replace(template);
149-
}
150-
151121
@SuppressWarnings("PMD.UseObjectForClearerAPI")
152122
private void sendMail(
153123
final String email,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (C) 2009-2017 Slava Semushin <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.service;
19+
20+
import ru.mystamps.web.service.dto.AdminDailyReport;
21+
22+
/**
23+
*@author Maxim Shestakov
24+
*/
25+
public interface ReportService {
26+
String prepareDailyStatistics(AdminDailyReport report);
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (C) 2009-2017 Slava Semushin <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
package ru.mystamps.web.service;
19+
20+
import java.util.HashMap;
21+
import java.util.Locale;
22+
import java.util.Map;
23+
24+
import org.apache.commons.lang3.text.StrSubstitutor;
25+
import org.apache.commons.lang3.time.DatePrinter;
26+
import org.apache.commons.lang3.time.FastDateFormat;
27+
28+
import org.springframework.context.MessageSource;
29+
30+
import org.springframework.security.access.prepost.PreAuthorize;
31+
32+
import ru.mystamps.web.service.dto.AdminDailyReport;
33+
import ru.mystamps.web.support.spring.security.HasAuthority;
34+
35+
/**
36+
* @author Maxim Shestakov
37+
*/
38+
public class ReportServiceImpl implements ReportService {
39+
40+
private final MessageSource messageSource;
41+
private final DatePrinter shortDatePrinter;
42+
private final Locale adminLang;
43+
44+
public ReportServiceImpl(
45+
MessageSource messageSource,
46+
Locale adminLang) {
47+
48+
this.messageSource = messageSource;
49+
this.adminLang = adminLang;
50+
51+
this.shortDatePrinter = FastDateFormat.getInstance("dd.MM.yyyy", adminLang);
52+
}
53+
54+
// This method should have @PreAuthorize(VIEW_DAILY_STATS) but we can't put it here because it
55+
// breaks MailServiceImpl.sendDailyStatisticsToAdmin() method that is being executed by cron.
56+
@Override
57+
public String prepareDailyStatistics(AdminDailyReport report) {
58+
String template = messageSource.getMessage("daily_stat.text", null, adminLang);
59+
String fromDate = shortDatePrinter.format(report.getStartDate());
60+
String tillDate = shortDatePrinter.format(report.getEndDate());
61+
62+
Map<String, String> ctx = new HashMap<>();
63+
ctx.put("from_date", fromDate);
64+
ctx.put("to_date", tillDate);
65+
66+
put(ctx, "added_countries_cnt", report.getAddedCountriesCounter());
67+
put(ctx, "untranslated_countries_cnt", report.getUntranslatedCountriesCounter());
68+
put(ctx, "added_categories_cnt", report.getAddedCategoriesCounter());
69+
put(ctx, "untranslated_categories_cnt", report.getUntranslatedCategoriesCounter());
70+
put(ctx, "added_series_cnt", report.getAddedSeriesCounter());
71+
put(ctx, "updated_series_cnt", report.getUpdatedSeriesCounter());
72+
put(ctx, "updated_collections_cnt", report.getUpdatedCollectionsCounter());
73+
put(ctx, "registration_requests_cnt", report.getRegistrationRequestsCounter());
74+
put(ctx, "registered_users_cnt", report.getRegisteredUsersCounter());
75+
put(ctx, "events_cnt", report.countEvents());
76+
put(ctx, "not_found_cnt", report.getNotFoundCounter());
77+
put(ctx, "failed_auth_cnt", report.getFailedAuthCounter());
78+
put(ctx, "missing_csrf_cnt", report.getMissingCsrfCounter());
79+
put(ctx, "invalid_csrf_cnt", report.getInvalidCsrfCounter());
80+
put(ctx, "bad_request_cnt", -1L); // TODO: #122
81+
82+
return new StrSubstitutor(ctx).replace(template);
83+
}
84+
85+
private static void put(Map<String, String> ctx, String key, long value) {
86+
ctx.put(key, String.valueOf(value));
87+
}
88+
}

0 commit comments

Comments
 (0)