From f972bf71ce400c87177da1adfa4941b56b2d5927 Mon Sep 17 00:00:00 2001 From: Maxim Shestakov Date: Thu, 3 Aug 2017 11:12:54 +0300 Subject: [PATCH] refactoring_method --- .../web/config/ControllersConfig.java | 2 +- .../mystamps/web/config/ServicesConfig.java | 12 ++- .../web/controller/ReportController.java | 6 +- .../ru/mystamps/web/service/MailService.java | 1 - .../mystamps/web/service/MailServiceImpl.java | 38 +-------- .../mystamps/web/service/ReportService.java | 27 ++++++ .../web/service/ReportServiceImpl.java | 85 +++++++++++++++++++ 7 files changed, 131 insertions(+), 40 deletions(-) create mode 100644 src/main/java/ru/mystamps/web/service/ReportService.java create mode 100644 src/main/java/ru/mystamps/web/service/ReportServiceImpl.java diff --git a/src/main/java/ru/mystamps/web/config/ControllersConfig.java b/src/main/java/ru/mystamps/web/config/ControllersConfig.java index 45178ed849..fc520563d3 100644 --- a/src/main/java/ru/mystamps/web/config/ControllersConfig.java +++ b/src/main/java/ru/mystamps/web/config/ControllersConfig.java @@ -91,7 +91,7 @@ public RobotsTxtController getRobotsTxtController() { @Bean public ReportController getReportController() { return new ReportController( - servicesConfig.getMailService(), + servicesConfig.getReportService(), servicesConfig.getCronService() ); } diff --git a/src/main/java/ru/mystamps/web/config/ServicesConfig.java b/src/main/java/ru/mystamps/web/config/ServicesConfig.java index e9deec8e57..64f64352d5 100644 --- a/src/main/java/ru/mystamps/web/config/ServicesConfig.java +++ b/src/main/java/ru/mystamps/web/config/ServicesConfig.java @@ -91,12 +91,14 @@ public MailService getMailService() { boolean enableTestMode = !isProductionEnvironment; return new MailServiceImpl( + getReportService(), mailSender, messageSource, env.getProperty("app.mail.admin.email", "root@localhost"), new Locale(env.getProperty("app.mail.admin.lang", "en")), env.getRequiredProperty("app.mail.robot.email"), - enableTestMode); + enableTestMode + ); } @Bean @@ -104,6 +106,14 @@ public UsersActivationService getUsersActivationService() { return new UsersActivationServiceImpl(daoConfig.getUsersActivationDao(), getMailService()); } + @Bean + public ReportService getReportService() { + return new ReportServiceImpl( + messageSource, + new Locale(env.getProperty("app.mail.admin.lang", "en")) + ); + } + @Bean public SeriesService getSeriesService() { return new SeriesServiceImpl( diff --git a/src/main/java/ru/mystamps/web/controller/ReportController.java b/src/main/java/ru/mystamps/web/controller/ReportController.java index d2835de780..051f1f2352 100644 --- a/src/main/java/ru/mystamps/web/controller/ReportController.java +++ b/src/main/java/ru/mystamps/web/controller/ReportController.java @@ -25,7 +25,7 @@ import ru.mystamps.web.Url; import ru.mystamps.web.service.CronService; -import ru.mystamps.web.service.MailService; +import ru.mystamps.web.service.ReportService; /** * @author Maxim Shestakov @@ -34,13 +34,13 @@ @RequiredArgsConstructor public class ReportController { - private final MailService mailService; + private final ReportService reportService; private final CronService cronService; @GetMapping(path = Url.DAILY_STATISTICS, produces = "text/plain; charset=UTF-8") @ResponseBody public String showDailyReport() { - return mailService.prepareDailyStatistics( + return reportService.prepareDailyStatistics( cronService.getDailyReport() ); } diff --git a/src/main/java/ru/mystamps/web/service/MailService.java b/src/main/java/ru/mystamps/web/service/MailService.java index add8d93ede..10b90db247 100644 --- a/src/main/java/ru/mystamps/web/service/MailService.java +++ b/src/main/java/ru/mystamps/web/service/MailService.java @@ -23,5 +23,4 @@ public interface MailService { void sendActivationKeyToUser(SendUsersActivationDto activation); void sendDailyStatisticsToAdmin(AdminDailyReport report); - String prepareDailyStatistics(AdminDailyReport report); } diff --git a/src/main/java/ru/mystamps/web/service/MailServiceImpl.java b/src/main/java/ru/mystamps/web/service/MailServiceImpl.java index 190bc1d798..a28b02767c 100644 --- a/src/main/java/ru/mystamps/web/service/MailServiceImpl.java +++ b/src/main/java/ru/mystamps/web/service/MailServiceImpl.java @@ -39,13 +39,10 @@ import org.springframework.mail.javamail.MimeMessagePreparator; import org.springframework.scheduling.annotation.Async; -import org.springframework.security.access.prepost.PreAuthorize; - import ru.mystamps.web.Url; import ru.mystamps.web.service.dto.AdminDailyReport; import ru.mystamps.web.service.dto.SendUsersActivationDto; import ru.mystamps.web.service.exception.EmailSendingException; -import ru.mystamps.web.support.spring.security.HasAuthority; public class MailServiceImpl implements MailService { private static final Logger LOG = LoggerFactory.getLogger(MailServiceImpl.class); @@ -57,8 +54,10 @@ public class MailServiceImpl implements MailService { private final String robotEmail; private final boolean testMode; private final DatePrinter shortDatePrinter; + private final ReportService reportService; public MailServiceImpl( + ReportService reportService, JavaMailSender mailer, MessageSource messageSource, String adminEmail, @@ -66,6 +65,7 @@ public MailServiceImpl( String robotEmail, boolean testMode) { + this.reportService = reportService; this.mailer = mailer; this.messageSource = messageSource; this.adminEmail = adminEmail; @@ -104,7 +104,7 @@ public void sendDailyStatisticsToAdmin(AdminDailyReport report) { sendMail( adminEmail, getSubjectOfDailyStatisticsMail(report), - prepareDailyStatistics(report), + reportService.prepareDailyStatistics(report), "daily_statistics" ); @@ -118,36 +118,6 @@ public void sendDailyStatisticsToAdmin(AdminDailyReport report) { ); } - @Override - @PreAuthorize(HasAuthority.VIEW_DAILY_STATS) - public String prepareDailyStatistics(AdminDailyReport report) { - String template = messageSource.getMessage("daily_stat.text", null, adminLang); - String fromDate = shortDatePrinter.format(report.getStartDate()); - String tillDate = shortDatePrinter.format(report.getEndDate()); - - Map ctx = new HashMap<>(); - ctx.put("from_date", fromDate); - ctx.put("to_date", tillDate); - - put(ctx, "added_countries_cnt", report.getAddedCountriesCounter()); - put(ctx, "untranslated_countries_cnt", report.getUntranslatedCountriesCounter()); - put(ctx, "added_categories_cnt", report.getAddedCategoriesCounter()); - put(ctx, "untranslated_categories_cnt", report.getUntranslatedCategoriesCounter()); - put(ctx, "added_series_cnt", report.getAddedSeriesCounter()); - put(ctx, "updated_series_cnt", report.getUpdatedSeriesCounter()); - put(ctx, "updated_collections_cnt", report.getUpdatedCollectionsCounter()); - put(ctx, "registration_requests_cnt", report.getRegistrationRequestsCounter()); - put(ctx, "registered_users_cnt", report.getRegisteredUsersCounter()); - put(ctx, "events_cnt", report.countEvents()); - put(ctx, "not_found_cnt", report.getNotFoundCounter()); - put(ctx, "failed_auth_cnt", report.getFailedAuthCounter()); - put(ctx, "missing_csrf_cnt", report.getMissingCsrfCounter()); - put(ctx, "invalid_csrf_cnt", report.getInvalidCsrfCounter()); - put(ctx, "bad_request_cnt", -1L); // TODO: #122 - - return new StrSubstitutor(ctx).replace(template); - } - @SuppressWarnings("PMD.UseObjectForClearerAPI") private void sendMail( final String email, diff --git a/src/main/java/ru/mystamps/web/service/ReportService.java b/src/main/java/ru/mystamps/web/service/ReportService.java new file mode 100644 index 0000000000..a820b817b1 --- /dev/null +++ b/src/main/java/ru/mystamps/web/service/ReportService.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2009-2017 Slava Semushin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package ru.mystamps.web.service; + +import ru.mystamps.web.service.dto.AdminDailyReport; + +/** + *@author Maxim Shestakov + */ +public interface ReportService { + String prepareDailyStatistics(AdminDailyReport report); +} diff --git a/src/main/java/ru/mystamps/web/service/ReportServiceImpl.java b/src/main/java/ru/mystamps/web/service/ReportServiceImpl.java new file mode 100644 index 0000000000..391277fd59 --- /dev/null +++ b/src/main/java/ru/mystamps/web/service/ReportServiceImpl.java @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2009-2017 Slava Semushin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package ru.mystamps.web.service; + +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +import org.apache.commons.lang3.text.StrSubstitutor; +import org.apache.commons.lang3.time.DatePrinter; +import org.apache.commons.lang3.time.FastDateFormat; + +import org.springframework.context.MessageSource; + +import ru.mystamps.web.service.dto.AdminDailyReport; + +/** + * @author Maxim Shestakov + */ +public class ReportServiceImpl implements ReportService { + + private final MessageSource messageSource; + private final DatePrinter shortDatePrinter; + private final Locale adminLang; + + public ReportServiceImpl( + MessageSource messageSource, + Locale adminLang) { + + this.messageSource = messageSource; + this.adminLang = adminLang; + + this.shortDatePrinter = FastDateFormat.getInstance("dd.MM.yyyy", adminLang); + } + + // This method should have @PreAuthorize(VIEW_DAILY_STATS) but we can't put it here because it + // breaks MailServiceImpl.sendDailyStatisticsToAdmin() method that is being executed by cron. + @Override + public String prepareDailyStatistics(AdminDailyReport report) { + String template = messageSource.getMessage("daily_stat.text", null, adminLang); + String fromDate = shortDatePrinter.format(report.getStartDate()); + String tillDate = shortDatePrinter.format(report.getEndDate()); + + Map ctx = new HashMap<>(); + ctx.put("from_date", fromDate); + ctx.put("to_date", tillDate); + + put(ctx, "added_countries_cnt", report.getAddedCountriesCounter()); + put(ctx, "untranslated_countries_cnt", report.getUntranslatedCountriesCounter()); + put(ctx, "added_categories_cnt", report.getAddedCategoriesCounter()); + put(ctx, "untranslated_categories_cnt", report.getUntranslatedCategoriesCounter()); + put(ctx, "added_series_cnt", report.getAddedSeriesCounter()); + put(ctx, "updated_series_cnt", report.getUpdatedSeriesCounter()); + put(ctx, "updated_collections_cnt", report.getUpdatedCollectionsCounter()); + put(ctx, "registration_requests_cnt", report.getRegistrationRequestsCounter()); + put(ctx, "registered_users_cnt", report.getRegisteredUsersCounter()); + put(ctx, "events_cnt", report.countEvents()); + put(ctx, "not_found_cnt", report.getNotFoundCounter()); + put(ctx, "failed_auth_cnt", report.getFailedAuthCounter()); + put(ctx, "missing_csrf_cnt", report.getMissingCsrfCounter()); + put(ctx, "invalid_csrf_cnt", report.getInvalidCsrfCounter()); + put(ctx, "bad_request_cnt", -1L); // TODO: #122 + + return new StrSubstitutor(ctx).replace(template); + } + + private static void put(Map ctx, String key, long value) { + ctx.put(key, String.valueOf(value)); + } +}