Skip to content

Commit 5254bb1

Browse files
committed
Guess Country
Guess Country gh571_prediction_country add guess country fixes after merge fix query cleaning and return String check style check style fixes fixes fixes remarks style add try togglz for link SuggestionController add HasAuthority remarks rename
1 parent 74f8b18 commit 5254bb1

18 files changed

+156
-5
lines changed

src/main/java/ru/mystamps/web/Url.java

+2
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public final class Url {
5151
public static final String INFO_SERIES_PAGE = "/series/{id}";
5252
public static final String ADD_IMAGE_SERIES_PAGE = "/series/{id}/image";
5353
public static final String SEARCH_SERIES_BY_CATALOG = "/series/search/by_catalog";
54+
public static final String INFO_COUNTRY_SERIES_PAGE = "/suggest/series_country";
5455

5556
public static final String ADD_CATEGORY_PAGE = "/category/add";
5657
public static final String LIST_CATEGORIES_PAGE = "/category/list";
@@ -125,6 +126,7 @@ public static Map<String, String> asMap(boolean serveContentFromSingleHost) {
125126
map.put("INFO_SERIES_PAGE", INFO_SERIES_PAGE);
126127
map.put("ADD_IMAGE_SERIES_PAGE", ADD_IMAGE_SERIES_PAGE);
127128
map.put("SEARCH_SERIES_BY_CATALOG", SEARCH_SERIES_BY_CATALOG);
129+
map.put("INFO_COUNTRY_SERIES_PAGE", INFO_COUNTRY_SERIES_PAGE);
128130
map.put("ADD_CATEGORY_PAGE", ADD_CATEGORY_PAGE);
129131
map.put("INFO_CATEGORY_PAGE", INFO_CATEGORY_PAGE);
130132
map.put("LIST_CATEGORIES_PAGE", LIST_CATEGORIES_PAGE);

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

+7
Original file line numberDiff line numberDiff line change
@@ -110,5 +110,12 @@ public SiteController getSiteController() {
110110
public SitemapController getSitemapController() {
111111
return new SitemapController(servicesConfig.getSeriesService());
112112
}
113+
114+
@Bean
115+
public SuggestionController getSuggestionController() {
116+
return new SuggestionController(
117+
servicesConfig.getCountryService()
118+
);
119+
}
113120

114121
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public void showForm(
133133
model.addAttribute("countries", countries);
134134

135135
model.addAttribute("years", YEARS);
136+
model.addAttribute("suggestCountryUrl", Url.INFO_COUNTRY_SERIES_PAGE);
136137

137138
AddSeriesForm addSeriesForm = new AddSeriesForm();
138139
addSeriesForm.setPerforated(true);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package ru.mystamps.web.controller;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.springframework.security.access.prepost.PreAuthorize;
5+
import org.springframework.stereotype.Controller;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.ResponseBody;
8+
import ru.mystamps.web.Url;
9+
import ru.mystamps.web.controller.converter.annotation.CurrentUser;
10+
import ru.mystamps.web.service.CountryService;
11+
import ru.mystamps.web.support.spring.security.HasAuthority;
12+
13+
@Controller
14+
@RequiredArgsConstructor
15+
@SuppressWarnings({ "PMD.AvoidDuplicateLiterals", "PMD.TooManyMethods", "PMD.GodClass" })
16+
public class SuggestionController {
17+
18+
private final CountryService countryService;
19+
/**
20+
* @author John Shkarin
21+
*/
22+
@ResponseBody
23+
@GetMapping(Url.INFO_COUNTRY_SERIES_PAGE)
24+
@PreAuthorize(HasAuthority.CREATE_SERIES)
25+
public String suggestCountryForUser(@CurrentUser Integer currentUserId) {
26+
return countryService.suggestCountryForUser(currentUserId);
27+
}
28+
29+
}

src/main/java/ru/mystamps/web/dao/CountryDao.java

+2
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ public interface CountryDao {
3535
List<Object[]> getStatisticsOf(Integer collectionId, String lang);
3636
List<LinkEntityDto> findAllAsLinkEntities(String lang);
3737
LinkEntityDto findOneAsLinkEntity(String slug, String lang);
38+
39+
String suggestCountryForUser(Integer userId);
3840
}

src/main/java/ru/mystamps/web/dao/SeriesDao.java

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
@SuppressWarnings("PMD.TooManyMethods")
3131
public interface SeriesDao {
3232
Integer add(AddSeriesDbDto series);
33+
3334
void markAsModified(Integer seriesId, Date updateAt, Integer updatedBy);
3435
List<SitemapInfoDto> findAllForSitemap();
3536
List<SeriesInfoDto> findLastAdded(int quantity, String lang);

src/main/java/ru/mystamps/web/dao/impl/JdbcCountryDao.java

+28-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ public class JdbcCountryDao implements CountryDao {
7676

7777
@Value("${country.find_country_link_info_by_slug}")
7878
private String findCountryLinkEntityBySlugSql;
79+
80+
@Value("${country.find_last_country_by_id}")
81+
private String findLastCountryByIdSql;
82+
83+
@Value("${country.find_popular_country}")
84+
private String findPopularCountrySql;
7985

8086
@Override
8187
public Integer add(AddCountryDbDto country) {
@@ -206,5 +212,26 @@ public LinkEntityDto findOneAsLinkEntity(String slug, String lang) {
206212
return null;
207213
}
208214
}
209-
215+
216+
@Override
217+
public String suggestCountryForUser(Integer userId) {
218+
219+
try {
220+
return jdbcTemplate.queryForObject(
221+
findLastCountryByIdSql,
222+
Collections.singletonMap("created_by", userId),
223+
String.class);
224+
} catch (EmptyResultDataAccessException ignored) {
225+
try {
226+
return jdbcTemplate.queryForObject(
227+
findPopularCountrySql,
228+
Collections.<String, Object>emptyMap(),
229+
String.class
230+
);
231+
232+
} catch (EmptyResultDataAccessException ex) {
233+
return null;
234+
}
235+
}
236+
}
210237
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import ru.mystamps.web.dao.dto.LinkEntityDto;
2424
import ru.mystamps.web.service.dto.AddCountryDto;
2525

26+
@SuppressWarnings("PMD.TooManyMethods")
2627
public interface CountryService {
2728
String add(AddCountryDto dto, Integer userId);
29+
String suggestCountryForUser(Integer userId);
2830
List<LinkEntityDto> findAllAsLinkEntities(String lang);
2931
LinkEntityDto findOneAsLinkEntity(String slug, String lang);
3032
long countAll();

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,15 @@ public List<Object[]> getStatisticsOf(Integer collectionId, String lang) {
162162

163163
return countryDao.getStatisticsOf(collectionId, lang);
164164
}
165-
165+
166+
/**
167+
* @author Shkarin John
168+
*/
169+
@Override
170+
@Transactional(readOnly = true)
171+
public String suggestCountryForUser(Integer userId) {
172+
Validate.isTrue(userId != null, "UserId must be non null");
173+
174+
return countryDao.suggestCountryForUser(userId);
175+
}
166176
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
@SuppressWarnings("PMD.TooManyMethods")
3232
public interface SeriesService {
3333
Integer add(AddSeriesDto dto, Integer userId, boolean userCanAddComments);
34+
3435
void addImageToSeries(AddImageDto dto, Integer seriesId, Integer userId);
3536
long countAll();
3637
long countAllStamps();

src/main/java/ru/mystamps/web/support/togglz/Features.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ public enum Features implements Feature {
7474

7575
@Label("View site events")
7676
@EnabledByDefault
77-
VIEW_SITE_EVENTS;
77+
VIEW_SITE_EVENTS,
78+
79+
@Label("/series/add: show link with auto-suggestions")
80+
@EnabledByDefault
81+
INFO_COUNTRY_SERIES;
7882

7983
public boolean isActive() {
8084
return FeatureContext.getFeatureManager().isActive(this);

src/main/javascript/series/add.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// IMPORTANT:
33
// You have to update Url.RESOURCES_VERSION each time whenever you're modified this file!
44
//
5-
function initPage() {
5+
function initPage(suggestCountryUrl) {
66
$('#country').selectize();
77

88
$('.js-catalog-numbers').on('blur', function() {
@@ -21,4 +21,19 @@ function initPage() {
2121
$('.js-with-tooltip').tooltip({
2222
'placement': 'right'
2323
});
24+
25+
$.get(suggestCountryUrl, function (slug){
26+
if (slug == null)
27+
return;
28+
29+
var country = $("#guess_country");
30+
country.show();
31+
country.click(function() {
32+
$(this).hide();
33+
34+
var select_country = $("#country").selectize();
35+
var selectize = select_country[0].selectize;
36+
selectize.setValue(slug);
37+
});
38+
});
2439
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--
2+
-- Auto-generated by Maven, based on values from src/main/resources/test/spring/test-data.properties
3+
--
4+
5+
INSERT INTO countries(name, created_at, created_by, updated_at, updated_by, name_ru, slug) VALUES
6+
('Russia', NOW(), 3, NOW(), 3, 'Россия', 'russia');
7+
8+
INSERT INTO countries(name, created_at, created_by, updated_at, updated_by, name_ru, slug) VALUES
9+
('Germany', NOW(), 3, NOW(), 3, 'Германия', 'germany');
10+
11+
INSERT INTO countries(name, created_at, created_by, updated_at, updated_by, name_ru, slug) VALUES
12+
('France', NOW(), 3, NOW(), 3, 'Франция', 'france');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--
2+
-- Auto-generated by Maven, based on values from src/main/resources/test/spring/test-data.properties
3+
--
4+
5+
INSERT INTO series(id, quantity, perforated, country_id, created_at, created_by, updated_at, updated_by, category_id) VALUES
6+
(2, 1, 1, 2, NOW() + 1, 1, NOW(), 4, 1);
7+
8+
INSERT INTO series(id, quantity, perforated, country_id, created_at, created_by, updated_at, updated_by, category_id) VALUES
9+
(3, 1, 1, 2, NOW() + 2, 1, NOW(), 4, 1);
10+
11+
INSERT INTO series(id, quantity, perforated, country_id, created_at, created_by, updated_at, updated_by, category_id) VALUES
12+
(4, 1, 1, 2, NOW() + 3, 1, NOW(), 4, 1);
13+
14+
INSERT INTO series(id, quantity, perforated, country_id, created_at, created_by, updated_at, updated_by, category_id) VALUES
15+
(5, 1, 1, 2, NOW() + 4, 1, NOW(), 4, 1);
16+
17+
INSERT INTO series(id, quantity, perforated, country_id, created_at, created_by, updated_at, updated_by, category_id) VALUES
18+
(6, 1, 1, 4, NOW() + 5, 1, NOW(), 4, 1);

src/main/resources/ru/mystamps/i18n/Messages.properties

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ t_add_more_images_hint = Later you will be able to add additional images
121121
t_not_chosen = Not chosen
122122
t_create_category_hint = You can also <a tabindex="-1" href="{0}">add a new category</a>
123123
t_create_country_hint = You can also <a tabindex="-1" href="{0}">add a new country</a>
124+
t_guess_country = Guess a country
124125

125126
# series/info.html
126127
t_series_info = Info about series

src/main/resources/ru/mystamps/i18n/Messages_ru.properties

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ t_add_more_images_hint = Вы сможете добавить дополните
121121
t_not_chosen = Не выбрана
122122
t_create_category_hint = Вы также можете <a tabindex="-1" href="{0}">добавить новую категорию</a>
123123
t_create_country_hint = Вы также можете <a tabindex="-1" href="{0}">добавить новую страну</a>
124+
t_guess_country = Угадать страну
124125

125126
# series/info.html
126127
t_series_info = Информация о серии

src/main/resources/sql/country_dao_queries.properties

+14
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,17 @@ country.find_country_link_info_by_slug = \
8181
FROM countries c \
8282
WHERE c.slug = :slug \
8383
ORDER BY CASE WHEN 'ru' = :lang THEN COALESCE(c.name_ru, c.name) ELSE c.name END
84+
85+
country.find_last_country_by_id = \
86+
SELECT slug \
87+
FROM series s \
88+
LEFT JOIN countries c ON c.id = s.country_id \
89+
WHERE series.created_by = :created_by \
90+
ORDER BY series.created_at DESC LIMIT 1
91+
92+
country.find_popular_country = \
93+
SELECT slug \
94+
FROM series s \
95+
LEFT JOIN countries c ON c.id = s.country_id \
96+
GROUP BY country_id \
97+
ORDER BY COUNT(*) DESC LIMIT 1

src/main/webapp/WEB-INF/views/series/add.html

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<link rel="stylesheet" href="../../static/styles/main.css" th:href="${MAIN_CSS}" />
1616
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/selectize.js/0.12.3/css/selectize.bootstrap3.min.css" th:href="${SELECTIZE_CSS}" />
1717
</head>
18-
<body onload="initPage()">
18+
<body onload="initPage(${suggestCountryUrl})">
1919
<div class="container-fluid">
2020
<div class="row" id="header">
2121
<div id="logo" class="col-sm-9 vcenter">
@@ -202,6 +202,10 @@ <h3 th:text="${#strings.capitalize(add_series)}">
202202
<span id="country.errors" class="help-block" th:if="${#fields.hasErrors('country')}" th:each="error : ${#fields.errors('country')}" th:text="${error}"></span>
203203
/*/-->
204204
</div>
205+
206+
<small togglz:active="INFO_COUNTRY_SERIES" id="guess_country" style="display: none;">
207+
<a tabindex="-1" th:text="#{t_guess_country}" href="javascript:void(0)">Guess a country</a>
208+
</small>
205209
</div>
206210

207211
<div class="form-group form-group-sm" th:classappend="${#fields.hasErrors('quantity') ? 'has-error' : ''}">

0 commit comments

Comments
 (0)