Skip to content

Commit 54c9ab9

Browse files
committed
Added categories to series.
1 parent a7a0942 commit 54c9ab9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1580
-27
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public final class Url {
4646
public static final String ADD_SERIES_PAGE = "/series/add";
4747
public static final String INFO_SERIES_PAGE = "/series/{id}";
4848

49+
public static final String ADD_CATEGORY_PAGE = "/category/add";
50+
public static final String INFO_CATEGORY_PAGE = "/category/{id}";
51+
4952
public static final String ADD_COUNTRY_PAGE = "/country/add";
5053
public static final String INFO_COUNTRY_PAGE = "/country/{id}";
5154

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

+7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public AccountController getAccountController() {
3535
return new AccountController(servicesConfig.getUserService());
3636
}
3737

38+
@Bean
39+
public CategoryController getCategoryController() {
40+
return new CategoryController(servicesConfig.getCategoryService());
41+
}
42+
3843
@Bean
3944
public CountryController getCountryController() {
4045
return new CountryController(servicesConfig.getCountryService());
@@ -53,6 +58,7 @@ public NotFoundErrorController getNotFoundErrorController() {
5358
@Bean
5459
public SeriesController getSeriesController() {
5560
return new SeriesController(
61+
servicesConfig.getCategoryService(),
5662
servicesConfig.getCountryService(),
5763
servicesConfig.getSeriesService()
5864
);
@@ -61,6 +67,7 @@ public SeriesController getSeriesController() {
6167
@Bean
6268
public SiteController getSiteController() {
6369
return new SiteController(
70+
servicesConfig.getCategoryService(),
6471
servicesConfig.getCountryService(),
6572
servicesConfig.getSeriesService()
6673
);

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

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public class ServicesConfig {
3131
@Inject
3232
private CountryDao countryDao;
3333

34+
@Inject
35+
private CategoryDao categoryDao;
36+
3437
@Inject
3538
private SecurityConfig securityConfig;
3639

@@ -60,6 +63,11 @@ public CountryService getCountryService() {
6063
return new CountryServiceImpl(countryDao);
6164
}
6265

66+
@Bean
67+
public CategoryService getCategoryService() {
68+
return new CategoryServiceImpl(categoryDao);
69+
}
70+
6371
@Bean
6472
public CronService getCronService() {
6573
return new CronServiceImpl(usersActivationDao);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright (C) 2009-2014 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.controller;
19+
20+
import javax.inject.Inject;
21+
import javax.validation.Valid;
22+
23+
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
24+
import org.springframework.stereotype.Controller;
25+
import org.springframework.ui.Model;
26+
import org.springframework.web.bind.annotation.InitBinder;
27+
import org.springframework.web.bind.annotation.PathVariable;
28+
import org.springframework.web.bind.annotation.RequestMapping;
29+
import org.springframework.web.bind.annotation.RequestMethod;
30+
import org.springframework.web.bind.WebDataBinder;
31+
import org.springframework.web.util.UriComponentsBuilder;
32+
import org.springframework.validation.BindingResult;
33+
34+
import ru.mystamps.web.Url;
35+
import ru.mystamps.web.entity.Category;
36+
import ru.mystamps.web.entity.User;
37+
import ru.mystamps.web.model.AddCategoryForm;
38+
import ru.mystamps.web.service.CategoryService;
39+
40+
@Controller
41+
public class CategoryController {
42+
43+
private final CategoryService categoryService;
44+
45+
@Inject
46+
public CategoryController(CategoryService categoryService) {
47+
this.categoryService = categoryService;
48+
}
49+
50+
@InitBinder("addCategoryForm")
51+
protected void initBinder(WebDataBinder binder) {
52+
StringTrimmerEditor editor = new StringTrimmerEditor(false);
53+
binder.registerCustomEditor(String.class, "name", editor);
54+
binder.registerCustomEditor(String.class, "nameRu", editor);
55+
}
56+
57+
@RequestMapping(value = Url.ADD_CATEGORY_PAGE, method = RequestMethod.GET)
58+
public AddCategoryForm showForm() {
59+
return new AddCategoryForm();
60+
}
61+
62+
@RequestMapping(value = Url.ADD_CATEGORY_PAGE, method = RequestMethod.POST)
63+
public String processInput(
64+
@Valid AddCategoryForm form,
65+
BindingResult result,
66+
User currentUser) {
67+
68+
if (result.hasErrors()) {
69+
return null;
70+
}
71+
72+
Category category = categoryService.add(form, currentUser);
73+
74+
String dstUrl = UriComponentsBuilder.fromUriString(Url.INFO_CATEGORY_PAGE)
75+
.buildAndExpand(category.getId())
76+
.toString();
77+
78+
return "redirect:" + dstUrl;
79+
}
80+
81+
@RequestMapping(value = Url.INFO_CATEGORY_PAGE, method = RequestMethod.GET)
82+
public String showInfo(@PathVariable("id") Category category, Model model) {
83+
84+
if (category == null) {
85+
throw new NotFoundException();
86+
}
87+
88+
model.addAttribute("category", category);
89+
return "category/info";
90+
}
91+
92+
}
93+

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.springframework.web.util.UriComponentsBuilder;
4141

4242
import ru.mystamps.web.Url;
43+
import ru.mystamps.web.entity.Category;
4344
import ru.mystamps.web.entity.User;
4445
import ru.mystamps.web.model.AddSeriesForm;
4546
import ru.mystamps.web.model.AddSeriesForm.ScottCatalogChecks;
@@ -49,6 +50,7 @@
4950
import ru.mystamps.web.model.AddSeriesForm.YvertCatalogChecks;
5051
import ru.mystamps.web.entity.Country;
5152
import ru.mystamps.web.entity.Series;
53+
import ru.mystamps.web.service.CategoryService;
5254
import ru.mystamps.web.service.CountryService;
5355
import ru.mystamps.web.service.SeriesService;
5456
import ru.mystamps.web.support.spring.security.SecurityContextUtils;
@@ -62,6 +64,7 @@ public class SeriesController {
6264

6365
private static final Map<Integer, Integer> YEARS;
6466

67+
private final CategoryService categoryService;
6568
private final CountryService countryService;
6669
private final SeriesService seriesService;
6770

@@ -73,7 +76,12 @@ public class SeriesController {
7376
}
7477

7578
@Inject
76-
public SeriesController(CountryService countryService, SeriesService seriesService) {
79+
public SeriesController(
80+
CategoryService categoryService,
81+
CountryService countryService,
82+
SeriesService seriesService) {
83+
84+
this.categoryService = categoryService;
7785
this.countryService = countryService;
7886
this.seriesService = seriesService;
7987
}
@@ -93,6 +101,11 @@ public Map<Integer, Integer> getYears() {
93101
return YEARS;
94102
}
95103

104+
@ModelAttribute("categories")
105+
public Iterable<Category> getCategories() {
106+
return categoryService.findAll();
107+
}
108+
96109
@ModelAttribute("countries")
97110
public Iterable<Country> getCountries() {
98111
return countryService.findAll();

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,30 @@
2323
import org.springframework.web.bind.annotation.RequestMethod;
2424

2525
import ru.mystamps.web.Url;
26+
import ru.mystamps.web.service.CategoryService;
2627
import ru.mystamps.web.service.CountryService;
2728
import ru.mystamps.web.service.SeriesService;
2829

2930
@Controller
3031
public class SiteController {
3132

33+
private final CategoryService categoryService;
3234
private final CountryService countryService;
3335
private final SeriesService seriesService;
3436

35-
public SiteController(CountryService countryService, SeriesService seriesService) {
37+
public SiteController(
38+
CategoryService categoryService,
39+
CountryService countryService,
40+
SeriesService seriesService) {
41+
42+
this.categoryService = categoryService;
3643
this.countryService = countryService;
3744
this.seriesService = seriesService;
3845
}
3946

4047
@RequestMapping(value = Url.INDEX_PAGE, method = RequestMethod.GET)
4148
public String showIndexPage(Model model) {
49+
model.addAttribute("categoryCounter", categoryService.countAll());
4250
model.addAttribute("countryCounter", countryService.countAll());
4351
model.addAttribute("seriesCounter", seriesService.countAll());
4452
model.addAttribute("stampsCounter", seriesService.countAllStamps());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (C) 2009-2014 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.dao;
19+
20+
import org.springframework.data.repository.PagingAndSortingRepository;
21+
22+
import ru.mystamps.web.entity.Category;
23+
24+
public interface CategoryDao extends PagingAndSortingRepository<Category, Integer> {
25+
int countByName(String name);
26+
int countByNameRu(String name);
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (C) 2009-2014 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.entity;
19+
20+
import javax.persistence.Column;
21+
import javax.persistence.Embedded;
22+
import javax.persistence.Entity;
23+
import javax.persistence.GeneratedValue;
24+
import javax.persistence.Id;
25+
import javax.persistence.Table;
26+
27+
import lombok.Getter;
28+
import lombok.Setter;
29+
import lombok.ToString;
30+
31+
@Entity
32+
@Table(name = "categories")
33+
@Getter
34+
@Setter
35+
@ToString(exclude = "metaInfo")
36+
public class Category {
37+
38+
public static final int NAME_LENGTH = 50;
39+
40+
@Id
41+
@GeneratedValue
42+
private Integer id;
43+
44+
@Column(length = NAME_LENGTH, unique = true, nullable = false)
45+
private String name;
46+
47+
@Column(name = "name_ru", length = NAME_LENGTH, unique = true, nullable = false)
48+
private String nameRu;
49+
50+
@Embedded
51+
private MetaInfo metaInfo; // NOPMD
52+
53+
public Category() {
54+
metaInfo = new MetaInfo();
55+
}
56+
57+
}

src/main/java/ru/mystamps/web/entity/Series.java

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public class Series {
5555
@GeneratedValue
5656
private Integer id;
5757

58+
@ManyToOne(optional = false)
59+
private Category category;
60+
5861
@ManyToOne
5962
private Country country;
6063

0 commit comments

Comments
 (0)