Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 00d021f

Browse files
bodom91php-coder
authored andcommittedAug 7, 2017
ReportService.prepareDailyStatistics(): move method from MailService to a dedicated class.
Fix #620 No functional changes.
1 parent d55ae61 commit 00d021f

File tree

7 files changed

+131
-40
lines changed

7 files changed

+131
-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
@@ -91,19 +91,29 @@ public MailService getMailService() {
9191
boolean enableTestMode = !isProductionEnvironment;
9292

9393
return new MailServiceImpl(
94+
getReportService(),
9495
mailSender,
9596
messageSource,
9697
env.getProperty("app.mail.admin.email", "root@localhost"),
9798
new Locale(env.getProperty("app.mail.admin.lang", "en")),
9899
env.getRequiredProperty("app.mail.robot.email"),
99-
enableTestMode);
100+
enableTestMode
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 <slava.semushin@gmail.com>
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,85 @@
1+
/*
2+
* Copyright (C) 2009-2017 Slava Semushin <slava.semushin@gmail.com>
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 ru.mystamps.web.service.dto.AdminDailyReport;
31+
32+
/**
33+
* @author Maxim Shestakov
34+
*/
35+
public class ReportServiceImpl implements ReportService {
36+
37+
private final MessageSource messageSource;
38+
private final DatePrinter shortDatePrinter;
39+
private final Locale adminLang;
40+
41+
public ReportServiceImpl(
42+
MessageSource messageSource,
43+
Locale adminLang) {
44+
45+
this.messageSource = messageSource;
46+
this.adminLang = adminLang;
47+
48+
this.shortDatePrinter = FastDateFormat.getInstance("dd.MM.yyyy", adminLang);
49+
}
50+
51+
// This method should have @PreAuthorize(VIEW_DAILY_STATS) but we can't put it here because it
52+
// breaks MailServiceImpl.sendDailyStatisticsToAdmin() method that is being executed by cron.
53+
@Override
54+
public String prepareDailyStatistics(AdminDailyReport report) {
55+
String template = messageSource.getMessage("daily_stat.text", null, adminLang);
56+
String fromDate = shortDatePrinter.format(report.getStartDate());
57+
String tillDate = shortDatePrinter.format(report.getEndDate());
58+
59+
Map<String, String> ctx = new HashMap<>();
60+
ctx.put("from_date", fromDate);
61+
ctx.put("to_date", tillDate);
62+
63+
put(ctx, "added_countries_cnt", report.getAddedCountriesCounter());
64+
put(ctx, "untranslated_countries_cnt", report.getUntranslatedCountriesCounter());
65+
put(ctx, "added_categories_cnt", report.getAddedCategoriesCounter());
66+
put(ctx, "untranslated_categories_cnt", report.getUntranslatedCategoriesCounter());
67+
put(ctx, "added_series_cnt", report.getAddedSeriesCounter());
68+
put(ctx, "updated_series_cnt", report.getUpdatedSeriesCounter());
69+
put(ctx, "updated_collections_cnt", report.getUpdatedCollectionsCounter());
70+
put(ctx, "registration_requests_cnt", report.getRegistrationRequestsCounter());
71+
put(ctx, "registered_users_cnt", report.getRegisteredUsersCounter());
72+
put(ctx, "events_cnt", report.countEvents());
73+
put(ctx, "not_found_cnt", report.getNotFoundCounter());
74+
put(ctx, "failed_auth_cnt", report.getFailedAuthCounter());
75+
put(ctx, "missing_csrf_cnt", report.getMissingCsrfCounter());
76+
put(ctx, "invalid_csrf_cnt", report.getInvalidCsrfCounter());
77+
put(ctx, "bad_request_cnt", -1L); // TODO: #122
78+
79+
return new StrSubstitutor(ctx).replace(template);
80+
}
81+
82+
private static void put(Map<String, String> ctx, String key, long value) {
83+
ctx.put(key, String.valueOf(value));
84+
}
85+
}

0 commit comments

Comments
 (0)
Please sign in to comment.