Skip to content

Commit 0ccfd7e

Browse files
committed
Show statistic about user's collection.
Fix GH #48
1 parent 3d66e6f commit 0ccfd7e

26 files changed

+304
-7
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholder
6969
PropertySourcesPlaceholderConfigurer configurer =
7070
new PropertySourcesPlaceholderConfigurer();
7171
configurer.setLocations(new Resource[] {
72+
new ClassPathResource("sql/category_dao_queries.properties"),
73+
new ClassPathResource("sql/country_dao_queries.properties"),
7274
new ClassPathResource("sql/series_dao_queries.properties")
7375
});
7476
return configurer;

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ public CountryController getCountryController() {
5353

5454
@Bean
5555
public CollectionController getCollectionController() {
56-
return new CollectionController(servicesConfig.getSeriesService());
56+
return new CollectionController(
57+
servicesConfig.getCategoryService(),
58+
servicesConfig.getCountryService(),
59+
servicesConfig.getSeriesService());
5760
}
5861

5962
@Bean

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

+14
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
import org.springframework.context.annotation.Bean;
2323
import org.springframework.context.annotation.Configuration;
2424

25+
import ru.mystamps.web.dao.JdbcCategoryDao;
26+
import ru.mystamps.web.dao.JdbcCountryDao;
2527
import ru.mystamps.web.dao.JdbcSeriesDao;
28+
import ru.mystamps.web.dao.impl.JdbcCategoryDaoImpl;
29+
import ru.mystamps.web.dao.impl.JdbcCountryDaoImpl;
2630
import ru.mystamps.web.dao.impl.JdbcSeriesDaoImpl;
2731

2832
@Configuration
@@ -31,6 +35,16 @@ public class DaoConfig {
3135
@Inject
3236
private DataSourceConfig dataSourceConfig;
3337

38+
@Bean
39+
public JdbcCategoryDao getJdbcCategoryDao() {
40+
return new JdbcCategoryDaoImpl(dataSourceConfig.getDataSource());
41+
}
42+
43+
@Bean
44+
public JdbcCountryDao getJdbcCountryDao() {
45+
return new JdbcCountryDaoImpl(dataSourceConfig.getDataSource());
46+
}
47+
3448
@Bean
3549
public JdbcSeriesDao getJdbcSeriesDao() {
3650
return new JdbcSeriesDaoImpl(dataSourceConfig.getDataSource());

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ public class ServicesConfig {
7878

7979
@Bean
8080
public CountryService getCountryService() {
81-
return new CountryServiceImpl(countryDao);
81+
return new CountryServiceImpl(countryDao, daoConfig.getJdbcCountryDao());
8282
}
8383

8484
@Bean
8585
public CategoryService getCategoryService() {
86-
return new CategoryServiceImpl(categoryDao);
86+
return new CategoryServiceImpl(categoryDao, daoConfig.getJdbcCategoryDao());
8787
}
8888

8989
@Bean

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,18 @@
2929

3030
import ru.mystamps.web.Url;
3131
import ru.mystamps.web.entity.Collection;
32+
import ru.mystamps.web.service.CategoryService;
33+
import ru.mystamps.web.service.CountryService;
3234
import ru.mystamps.web.service.SeriesService;
35+
import ru.mystamps.web.service.dto.SeriesInfoDto;
3336
import ru.mystamps.web.util.LocaleUtils;
3437

3538
@Controller
3639
@RequiredArgsConstructor
3740
public class CollectionController {
3841

42+
private final CategoryService categoryService;
43+
private final CountryService countryService;
3944
private final SeriesService seriesService;
4045

4146
@RequestMapping(value = Url.INFO_COLLECTION_PAGE, method = RequestMethod.GET)
@@ -51,7 +56,15 @@ public String showInfo(
5156
model.addAttribute("ownerName", collection.getOwner().getName());
5257

5358
String lang = LocaleUtils.getLanguageOrNull(userLocale);
54-
model.addAttribute("seriesOfCollection", seriesService.findBy(collection, lang));
59+
Iterable<SeriesInfoDto> seriesOfCollection = seriesService.findBy(collection, lang);
60+
model.addAttribute("seriesOfCollection", seriesOfCollection);
61+
62+
if (seriesOfCollection.iterator().hasNext()) {
63+
model.addAttribute("categoryCounter", categoryService.countCategoriesOf(collection));
64+
model.addAttribute("countryCounter", countryService.countCountriesOf(collection));
65+
model.addAttribute("seriesCounter", seriesService.countSeriesOf(collection));
66+
model.addAttribute("stampsCounter", seriesService.countStampsOf(collection));
67+
}
5568

5669
return "collection/info";
5770
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
public interface JdbcCategoryDao {
21+
long countCategoriesOfCollection(Integer collectionId);
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
public interface JdbcCountryDao {
21+
long countCountriesOfCollection(Integer collectionId);
22+
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,6 @@
2121

2222
public interface JdbcSeriesDao {
2323
Iterable<SeriesInfoDto> findLastAdded(int quantity, String lang);
24+
long countSeriesOfCollection(Integer collectionId);
25+
long countStampsOfCollection(Integer collectionId);
2426
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.impl;
19+
20+
import java.util.Collections;
21+
22+
import javax.sql.DataSource;
23+
24+
import org.springframework.beans.factory.annotation.Value;
25+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
26+
27+
import ru.mystamps.web.dao.JdbcCategoryDao;
28+
29+
public class JdbcCategoryDaoImpl implements JdbcCategoryDao {
30+
31+
private final NamedParameterJdbcTemplate jdbcTemplate;
32+
33+
@Value("${category.count_categories_of_collection}")
34+
private String countCategoriesOfCollectionSql;
35+
36+
public JdbcCategoryDaoImpl(DataSource dataSource) {
37+
jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
38+
}
39+
40+
@Override
41+
public long countCategoriesOfCollection(Integer collectionId) {
42+
return jdbcTemplate.queryForObject(
43+
countCategoriesOfCollectionSql,
44+
Collections.singletonMap("collection_id", collectionId),
45+
Long.class
46+
);
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.impl;
19+
20+
import java.util.Collections;
21+
22+
import javax.sql.DataSource;
23+
24+
import org.springframework.beans.factory.annotation.Value;
25+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
26+
27+
import ru.mystamps.web.dao.JdbcCountryDao;
28+
29+
public class JdbcCountryDaoImpl implements JdbcCountryDao {
30+
31+
private final NamedParameterJdbcTemplate jdbcTemplate;
32+
33+
@Value("${country.count_countries_of_collection}")
34+
private String countCountriesOfCollectionSql;
35+
36+
public JdbcCountryDaoImpl(DataSource dataSource) {
37+
jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
38+
}
39+
40+
@Override
41+
public long countCountriesOfCollection(Integer collectionId) {
42+
return jdbcTemplate.queryForObject(
43+
countCountriesOfCollectionSql,
44+
Collections.singletonMap("collection_id", collectionId),
45+
Long.class
46+
);
47+
}
48+
49+
}

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

+24
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ public class JdbcSeriesDaoImpl implements JdbcSeriesDao {
4141
@Value("${series.find_last_added_ru_sql}")
4242
private String findLastAddedSeriesRuSql;
4343

44+
@Value("${series.count_series_of_collection}")
45+
private String countSeriesOfCollectionSql;
46+
47+
@Value("${series.count_stamps_of_collection}")
48+
private String countStampsOfCollectionSql;
49+
4450
public JdbcSeriesDaoImpl(DataSource dataSource) {
4551
jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
4652
}
@@ -61,4 +67,22 @@ public Iterable<SeriesInfoDto> findLastAdded(int quantity, String lang) {
6167
);
6268
}
6369

70+
@Override
71+
public long countSeriesOfCollection(Integer collectionId) {
72+
return jdbcTemplate.queryForObject(
73+
countSeriesOfCollectionSql,
74+
Collections.singletonMap("collection_id", collectionId),
75+
Long.class
76+
);
77+
}
78+
79+
@Override
80+
public long countStampsOfCollection(Integer collectionId) {
81+
return jdbcTemplate.queryForObject(
82+
countStampsOfCollectionSql,
83+
Collections.singletonMap("collection_id", collectionId),
84+
Long.class
85+
);
86+
}
87+
6488
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package ru.mystamps.web.service;
1919

2020
import ru.mystamps.web.entity.Category;
21+
import ru.mystamps.web.entity.Collection;
2122
import ru.mystamps.web.entity.User;
2223
import ru.mystamps.web.service.dto.AddCategoryDto;
2324
import ru.mystamps.web.service.dto.EntityInfoDto;
@@ -26,6 +27,7 @@ public interface CategoryService {
2627
Category add(AddCategoryDto dto, User user);
2728
Iterable<EntityInfoDto> findAll(String lang);
2829
long countAll();
30+
long countCategoriesOf(Collection collection);
2931
int countByName(String name);
3032
int countByNameRu(String name);
3133
}

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

+12
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@
2929

3030
import lombok.RequiredArgsConstructor;
3131

32+
import ru.mystamps.web.dao.JdbcCategoryDao;
3233
import ru.mystamps.web.entity.Category;
34+
import ru.mystamps.web.entity.Collection;
3335
import ru.mystamps.web.entity.User;
3436
import ru.mystamps.web.dao.CategoryDao;
3537
import ru.mystamps.web.service.dto.AddCategoryDto;
@@ -40,6 +42,7 @@ public class CategoryServiceImpl implements CategoryService {
4042
private static final Logger LOG = LoggerFactory.getLogger(CategoryServiceImpl.class);
4143

4244
private final CategoryDao categoryDao;
45+
private final JdbcCategoryDao jdbcCategoryDao;
4346

4447
@Override
4548
@Transactional
@@ -79,6 +82,15 @@ public long countAll() {
7982
return categoryDao.count();
8083
}
8184

85+
@Override
86+
@Transactional(readOnly = true)
87+
public long countCategoriesOf(Collection collection) {
88+
Validate.isTrue(collection != null, "Collection must be non null");
89+
Validate.isTrue(collection.getId() != null, "Collection id must be non null");
90+
91+
return jdbcCategoryDao.countCategoriesOfCollection(collection.getId());
92+
}
93+
8294
@Override
8395
@Transactional(readOnly = true)
8496
public int countByName(String name) {

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

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package ru.mystamps.web.service;
1919

20+
import ru.mystamps.web.entity.Collection;
2021
import ru.mystamps.web.entity.Country;
2122
import ru.mystamps.web.entity.User;
2223
import ru.mystamps.web.service.dto.AddCountryDto;
@@ -26,6 +27,7 @@ public interface CountryService {
2627
Country add(AddCountryDto dto, User user);
2728
Iterable<EntityInfoDto> findAll(String lang);
2829
long countAll();
30+
long countCountriesOf(Collection collection);
2931
int countByName(String name);
3032
int countByNameRu(String name);
3133
}

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

+12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
import lombok.RequiredArgsConstructor;
3131

32+
import ru.mystamps.web.dao.JdbcCountryDao;
33+
import ru.mystamps.web.entity.Collection;
3234
import ru.mystamps.web.entity.Country;
3335
import ru.mystamps.web.entity.User;
3436
import ru.mystamps.web.dao.CountryDao;
@@ -40,6 +42,7 @@ public class CountryServiceImpl implements CountryService {
4042
private static final Logger LOG = LoggerFactory.getLogger(CountryServiceImpl.class);
4143

4244
private final CountryDao countryDao;
45+
private final JdbcCountryDao jdbcCountryDao;
4346

4447
@Override
4548
@Transactional
@@ -79,6 +82,15 @@ public long countAll() {
7982
return countryDao.count();
8083
}
8184

85+
@Override
86+
@Transactional(readOnly = true)
87+
public long countCountriesOf(Collection collection) {
88+
Validate.isTrue(collection != null, "Collection must be non null");
89+
Validate.isTrue(collection.getId() != null, "Collection id must be non null");
90+
91+
return jdbcCountryDao.countCountriesOfCollection(collection.getId());
92+
}
93+
8294
@Override
8395
@Transactional(readOnly = true)
8496
public int countByName(String name) {

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

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public interface SeriesService {
2929
Series add(AddSeriesDto dto, User user, boolean userCanAddComments);
3030
long countAll();
3131
long countAllStamps();
32+
long countSeriesOf(Collection collection);
33+
long countStampsOf(Collection collection);
3234
int countByMichelNumber(String michelNumberCode);
3335
int countByScottNumber(String scottNumberCode);
3436
int countByYvertNumber(String yvertNumberCode);

0 commit comments

Comments
 (0)