Skip to content

Commit a79306f

Browse files
committed
Merge branch 'gh357_collections_metadata'
Fix #357
2 parents 29f594f + 3199d98 commit a79306f

14 files changed

+187
-3
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public CronService getCronService() {
6767
return new CronServiceImpl(
6868
getCategoryService(),
6969
getCountryService(),
70+
getCollectionService(),
7071
getSeriesService(),
7172
getSuspiciousActivityService(),
7273
getUserService(),

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package ru.mystamps.web.dao;
1919

20+
import java.util.Date;
2021
import java.util.List;
2122

2223
import ru.mystamps.web.dao.dto.AddCollectionDbDto;
@@ -26,7 +27,9 @@
2627
public interface CollectionDao {
2728
List<LinkEntityDto> findLastCreated(int quantity);
2829
long countCollectionsOfUsers();
30+
long countUpdatedSince(Date date);
2931
Integer add(AddCollectionDbDto collection);
32+
void markAsModified(Integer userId, Date updatedAt);
3033
boolean isSeriesInUserCollection(Integer userId, Integer seriesId);
3134
void addSeriesToUserCollection(Integer userId, Integer seriesId);
3235
void removeSeriesFromUserCollection(Integer userId, Integer seriesId);

src/main/java/ru/mystamps/web/dao/dto/AddCollectionDbDto.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
package ru.mystamps.web.dao.dto;
1919

20+
import java.util.Date;
21+
2022
import lombok.Getter;
2123
import lombok.Setter;
2224
import lombok.ToString;
@@ -27,4 +29,5 @@
2729
public class AddCollectionDbDto {
2830
private Integer ownerId;
2931
private String slug;
32+
private Date updatedAt;
3033
}

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package ru.mystamps.web.dao.impl;
1919

2020
import java.util.Collections;
21+
import java.util.Date;
2122
import java.util.HashMap;
2223
import java.util.List;
2324
import java.util.Map;
@@ -54,8 +55,14 @@ public class JdbcCollectionDao implements CollectionDao {
5455
@Value("${collection.count_collections_of_users}")
5556
private String countCollectionsOfUsersSql;
5657

58+
@Value("${collection.count_updated_since}")
59+
private String countUpdatedSinceSql;
60+
5761
@Value("${collection.create}")
5862
private String addCollectionSql;
63+
64+
@Value("${collection.mark_as_modified}")
65+
private String markAsModifiedSql;
5966

6067
@Value("${collection.is_series_in_collection}")
6168
private String isSeriesInUserCollectionSql;
@@ -87,11 +94,22 @@ public long countCollectionsOfUsers() {
8794
);
8895
}
8996

97+
@Override
98+
public long countUpdatedSince(Date date) {
99+
return jdbcTemplate.queryForObject(
100+
countUpdatedSinceSql,
101+
Collections.singletonMap("date", date),
102+
Long.class
103+
);
104+
}
105+
90106
@Override
91107
public Integer add(AddCollectionDbDto collection) {
92108
Map<String, Object> params = new HashMap<>();
93109
params.put("user_id", collection.getOwnerId());
94110
params.put("slug", collection.getSlug());
111+
params.put("updated_at", collection.getUpdatedAt());
112+
params.put("updated_by", collection.getOwnerId());
95113

96114
KeyHolder holder = new GeneratedKeyHolder();
97115

@@ -110,6 +128,29 @@ public Integer add(AddCollectionDbDto collection) {
110128

111129
return Integer.valueOf(holder.getKey().intValue());
112130
}
131+
132+
/**
133+
* @author John Shkarin
134+
* @author Slava Semushin
135+
*/
136+
@Override
137+
public void markAsModified(Integer userId, Date updatedAt) {
138+
Map<String, Object> params = new HashMap<>();
139+
params.put("user_id", userId);
140+
params.put("updated_at", updatedAt);
141+
params.put("updated_by", userId);
142+
143+
int affected = jdbcTemplate.update(
144+
markAsModifiedSql,
145+
params
146+
);
147+
148+
Validate.validState(
149+
affected == 1,
150+
"Unexpected number of affected rows after updating collection: %d",
151+
affected
152+
);
153+
}
113154

114155
@Override
115156
public boolean isSeriesInUserCollection(Integer userId, Integer seriesId) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package ru.mystamps.web.service;
1919

20+
import java.util.Date;
2021
import java.util.List;
2122

2223
import ru.mystamps.web.dao.dto.CollectionInfoDto;
@@ -28,6 +29,7 @@ public interface CollectionService {
2829
void removeFromCollection(Integer userId, Integer seriesId);
2930
boolean isSeriesInCollection(Integer userId, Integer seriesId);
3031
long countCollectionsOfUsers();
32+
long countUpdatedSince(Date date);
3133
List<LinkEntityDto> findRecentlyCreated(int quantity);
3234
CollectionInfoDto findBySlug(String slug);
3335
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package ru.mystamps.web.service;
1919

20+
import java.util.Date;
2021
import java.util.List;
2122

2223
import org.apache.commons.lang3.StringUtils;
@@ -59,6 +60,7 @@ public void createCollection(Integer ownerId, String ownerLogin) {
5960
"Slug for string '%s' must be non empty", ownerLogin
6061
);
6162
collection.setSlug(slug);
63+
collection.setUpdatedAt(new Date());
6264

6365
Integer id = collectionDao.add(collection);
6466

@@ -73,6 +75,7 @@ public void addToCollection(Integer userId, Integer seriesId) {
7375
Validate.isTrue(seriesId != null, "Series id must be non null");
7476

7577
collectionDao.addSeriesToUserCollection(userId, seriesId);
78+
collectionDao.markAsModified(userId, new Date());
7679

7780
LOG.info("Series #{} has been added to collection of user #{}", seriesId, userId);
7881
}
@@ -85,6 +88,7 @@ public void removeFromCollection(Integer userId, Integer seriesId) {
8588
Validate.isTrue(seriesId != null, "Series id must be non null");
8689

8790
collectionDao.removeSeriesFromUserCollection(userId, seriesId);
91+
collectionDao.markAsModified(userId, new Date());
8892

8993
LOG.info("Series #{} has been removed from collection of user #{}", seriesId, userId);
9094
}
@@ -117,6 +121,14 @@ public long countCollectionsOfUsers() {
117121
return collectionDao.countCollectionsOfUsers();
118122
}
119123

124+
@Override
125+
@Transactional(readOnly = true)
126+
public long countUpdatedSince(Date date) {
127+
Validate.isTrue(date != null, "Date must be non null");
128+
129+
return collectionDao.countUpdatedSince(date);
130+
}
131+
120132
@Override
121133
@Transactional(readOnly = true)
122134
public List<LinkEntityDto> findRecentlyCreated(int quantity) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class CronServiceImpl implements CronService {
4444

4545
private final CategoryService categoryService;
4646
private final CountryService countryService;
47+
private final CollectionService collectionService;
4748
private final SeriesService seriesService;
4849
private final SuspiciousActivityService suspiciousActivityService;
4950
private final UserService userService;
@@ -71,6 +72,7 @@ public void sendDailyStatistics() {
7172

7273
report.setAddedSeriesCounter(seriesService.countAddedSince(yesterday));
7374
report.setUpdatedSeriesCounter(seriesService.countUpdatedSince(yesterday));
75+
report.setUpdatedCollectionsCounter(collectionService.countUpdatedSince(yesterday));
7476
report.setRegistrationRequestsCounter(usersActivationService.countCreatedSince(yesterday));
7577
report.setRegisteredUsersCounter(userService.countRegisteredSince(yesterday));
7678

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ private String getTextOfDailyStatisticsMail(AdminDailyReport report) {
197197
put(ctx, "untranslated_categories_cnt", report.getUntranslatedCategoriesCounter());
198198
put(ctx, "added_series_cnt", report.getAddedSeriesCounter());
199199
put(ctx, "updated_series_cnt", report.getUpdatedSeriesCounter());
200-
put(ctx, "updated_collections_cnt", -1L); // TODO: #357
200+
put(ctx, "updated_collections_cnt", report.getUpdatedCollectionsCounter());
201201
put(ctx, "registration_requests_cnt", report.getRegistrationRequestsCounter());
202202
put(ctx, "registered_users_cnt", report.getRegisteredUsersCounter());
203203
put(ctx, "events_cnt", report.countEvents());

src/main/java/ru/mystamps/web/service/dto/AdminDailyReport.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class AdminDailyReport {
3333
private long untranslatedCountriesCounter;
3434
private long addedSeriesCounter;
3535
private long updatedSeriesCounter;
36+
private long updatedCollectionsCounter;
3637
private long registrationRequestsCounter;
3738
private long registeredUsersCounter;
3839
private long notFoundCounter;
@@ -57,6 +58,7 @@ public long countTotalChanges() {
5758
totalChanges = Math.addExact(totalChanges, untranslatedCountriesCounter);
5859
totalChanges = Math.addExact(totalChanges, addedSeriesCounter);
5960
totalChanges = Math.addExact(totalChanges, updatedSeriesCounter);
61+
totalChanges = Math.addExact(totalChanges, updatedCollectionsCounter);
6062
totalChanges = Math.addExact(totalChanges, registrationRequestsCounter);
6163
totalChanges = Math.addExact(totalChanges, registeredUsersCounter);
6264
totalChanges = Math.addExact(totalChanges, countEvents());

src/main/resources/liquibase/version/0.4.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@
2525
<include file="0.4/2016-12-05--make_name_ru_optional.xml" relativeToChangelogFile="true" />
2626
<include file="0.4/2017-01-06--top_categories.xml" relativeToChangelogFile="true" />
2727
<include file="0.4/2017-01-25--united_kingdom_country.xml" relativeToChangelogFile="true" />
28+
<include file="0.4/2017-01-29--add_updater_data_to_collections.xml" relativeToChangelogFile="true" />
2829

2930
</databaseChangeLog>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<databaseChangeLog
3+
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
6+
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd">
7+
8+
<changeSet id="add-updater-data-to-collections-table" author="john" context="scheme">
9+
<!-- Since MySQL 5.6.5 it's possible to use `NOW()` as default value for a column
10+
but we're doing the same in 3 steps to support also old versions. -->
11+
<addColumn tableName="collections">
12+
<column name="updated_at" type="DATETIME" />
13+
<column name="updated_by" type="INTEGER" />
14+
</addColumn>
15+
16+
<update tableName="collections">
17+
<column name="updated_at" type="DATETIME" valueComputed="${NOW}" />
18+
</update>
19+
20+
<update tableName="collections">
21+
<column name="updated_by"
22+
type="INTEGER"
23+
valueComputed="(SELECT id FROM users WHERE role = 'ADMIN' ORDER BY id LIMIT 1)" />
24+
</update>
25+
26+
<addNotNullConstraint tableName="collections" columnName="updated_at" columnDataType="DATETIME" />
27+
28+
<addNotNullConstraint tableName="collections" columnName="updated_by" columnDataType="INTEGER" />
29+
30+
<addForeignKeyConstraint
31+
baseTableName="collections"
32+
baseColumnNames="updated_by"
33+
constraintName="fk_collections_users_id"
34+
referencedTableName="users"
35+
referencedColumnNames="id" />
36+
37+
</changeSet>
38+
39+
</databaseChangeLog>

src/main/resources/sql/collection_dao_queries.properties

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,32 @@ LEFT JOIN series s \
1919
ON s.id = cs.series_id \
2020
WHERE s.id IS NOT NULL
2121

22+
collection.count_updated_since = \
23+
SELECT COUNT(*) \
24+
FROM collections \
25+
WHERE updated_at >= :date
26+
2227
collection.create = \
2328
INSERT \
2429
INTO collections \
2530
( user_id \
2631
, slug \
32+
, updated_at \
33+
, updated_by \
2734
) \
2835
VALUES \
2936
( :user_id \
3037
, :slug \
38+
, :updated_at \
39+
, :updated_by \
3140
)
3241

42+
collection.mark_as_modified = \
43+
UPDATE collections c \
44+
SET c.updated_at = :updated_at \
45+
, c.updated_by = :updated_by \
46+
WHERE c.user_id = :user_id
47+
3348
collection.is_series_in_collection = \
3449
SELECT COUNT(*) \
3550
FROM collections c \

0 commit comments

Comments
 (0)