|
18 | 18 | package ru.mystamps.web.controller;
|
19 | 19 |
|
20 | 20 | import java.io.IOException;
|
| 21 | +import java.util.List; |
21 | 22 | import java.util.Locale;
|
22 | 23 |
|
23 | 24 | import javax.servlet.http.HttpServletResponse;
|
|
38 | 39 |
|
39 | 40 | import ru.mystamps.web.Url;
|
40 | 41 | import ru.mystamps.web.controller.converter.annotation.CurrentUser;
|
| 42 | +import ru.mystamps.web.controller.dto.FirstLevelCategoryDto; |
| 43 | +import ru.mystamps.web.controller.dto.ImportSeriesForm; |
41 | 44 | import ru.mystamps.web.controller.dto.RequestImportForm;
|
42 | 45 | import ru.mystamps.web.controller.event.ImportRequestCreated;
|
| 46 | +import ru.mystamps.web.dao.dto.CategoryDto; |
43 | 47 | import ru.mystamps.web.dao.dto.ImportRequestDto;
|
| 48 | +import ru.mystamps.web.dao.dto.LinkEntityDto; |
44 | 49 | import ru.mystamps.web.dao.dto.ParsedDataDto;
|
| 50 | +import ru.mystamps.web.service.CategoryService; |
| 51 | +import ru.mystamps.web.service.CountryService; |
45 | 52 | import ru.mystamps.web.service.SeriesImportService;
|
| 53 | +import ru.mystamps.web.service.SeriesService; |
| 54 | +import ru.mystamps.web.support.thymeleaf.GroupByParent; |
46 | 55 | import ru.mystamps.web.util.LocaleUtils;
|
47 | 56 |
|
48 | 57 | import static ru.mystamps.web.controller.ControllerUtils.redirectTo;
|
|
51 | 60 | @RequiredArgsConstructor
|
52 | 61 | public class SeriesImportController {
|
53 | 62 |
|
| 63 | + private final CategoryService categoryService; |
| 64 | + private final CountryService countryService; |
| 65 | + private final SeriesService seriesService; |
54 | 66 | private final SeriesImportService seriesImportService;
|
55 | 67 | private final ApplicationEventPublisher eventPublisher;
|
56 | 68 |
|
@@ -107,10 +119,97 @@ public String showRequestAndImportSeriesForm(
|
107 | 119 |
|
108 | 120 | String lang = LocaleUtils.getLanguageOrNull(userLocale);
|
109 | 121 | ParsedDataDto parsedData = seriesImportService.getParsedData(requestId, lang);
|
110 |
| - model.addAttribute("parsedData", parsedData); |
| 122 | + |
| 123 | + ImportSeriesForm form = new ImportSeriesForm(); |
| 124 | + form.setPerforated(Boolean.TRUE); |
| 125 | + |
| 126 | + boolean hasParsedData = parsedData != null; |
| 127 | + if (hasParsedData) { |
| 128 | + if (parsedData.getCategory() != null) { |
| 129 | + form.setCategory(parsedData.getCategory()); |
| 130 | + } |
| 131 | + if (parsedData.getCountry() != null) { |
| 132 | + form.setCountry(parsedData.getCountry()); |
| 133 | + } |
| 134 | + if (parsedData.getImageUrl() != null) { |
| 135 | + form.setImageUrl(parsedData.getImageUrl()); |
| 136 | + } |
| 137 | + if (parsedData.getIssueYear() != null) { |
| 138 | + form.setYear(parsedData.getIssueYear()); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + model.addAttribute("importSeriesForm", form); |
| 143 | + model.addAttribute("showForm", hasParsedData); |
| 144 | + |
| 145 | + // @todo #709 SeriesImportController.showRequestAndImportSeriesForm(): |
| 146 | + // extract a method for adding shared attributes to the model |
| 147 | + List<CategoryDto> categories = |
| 148 | + categoryService.findCategoriesWithParents(lang); |
| 149 | + List<FirstLevelCategoryDto> groupedCategories = |
| 150 | + GroupByParent.transformCategories(categories); |
| 151 | + model.addAttribute("categories", groupedCategories); |
| 152 | + |
| 153 | + List<LinkEntityDto> countries = countryService.findAllAsLinkEntities(lang); |
| 154 | + model.addAttribute("countries", countries); |
| 155 | + |
| 156 | + model.addAttribute("years", SeriesController.YEARS); |
111 | 157 |
|
112 | 158 | return "series/import/info";
|
113 | 159 | }
|
114 | 160 |
|
| 161 | + @PostMapping(Url.REQUEST_IMPORT_PAGE) |
| 162 | + public String processImportSeriesForm( |
| 163 | + @PathVariable("id") Integer requestId, |
| 164 | + Model model, |
| 165 | + @Valid ImportSeriesForm form, |
| 166 | + BindingResult result, |
| 167 | + @CurrentUser Integer currentUserId, |
| 168 | + Locale userLocale, |
| 169 | + HttpServletResponse response) |
| 170 | + throws IOException { |
| 171 | + |
| 172 | + if (requestId == null) { |
| 173 | + response.sendError(HttpServletResponse.SC_NOT_FOUND); |
| 174 | + return null; |
| 175 | + } |
| 176 | + |
| 177 | + ImportRequestDto request = seriesImportService.findById(requestId); |
| 178 | + if (request == null) { |
| 179 | + response.sendError(HttpServletResponse.SC_NOT_FOUND); |
| 180 | + return null; |
| 181 | + } |
| 182 | + |
| 183 | + // @todo #709 SeriesImportController.processImportSeriesForm(): |
| 184 | + // handle errors from interceptor |
| 185 | + |
| 186 | + model.addAttribute("request", request); |
| 187 | + |
| 188 | + String lang = LocaleUtils.getLanguageOrNull(userLocale); |
| 189 | + |
| 190 | + ParsedDataDto parsedData = seriesImportService.getParsedData(requestId, lang); |
| 191 | + boolean hasParsedData = parsedData != null; |
| 192 | + model.addAttribute("showForm", hasParsedData); |
| 193 | + |
| 194 | + List<CategoryDto> categories = |
| 195 | + categoryService.findCategoriesWithParents(lang); |
| 196 | + List<FirstLevelCategoryDto> groupedCategories = |
| 197 | + GroupByParent.transformCategories(categories); |
| 198 | + model.addAttribute("categories", groupedCategories); |
| 199 | + |
| 200 | + List<LinkEntityDto> countries = countryService.findAllAsLinkEntities(lang); |
| 201 | + model.addAttribute("countries", countries); |
| 202 | + |
| 203 | + model.addAttribute("years", SeriesController.YEARS); |
| 204 | + |
| 205 | + if (result.hasErrors()) { |
| 206 | + return "series/import/info"; |
| 207 | + } |
| 208 | + |
| 209 | + Integer seriesId = seriesService.add(form, currentUserId, false); |
| 210 | + |
| 211 | + return redirectTo(Url.INFO_SERIES_PAGE, seriesId); |
| 212 | + } |
| 213 | + |
115 | 214 | }
|
116 | 215 |
|
0 commit comments