|
| 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 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