Skip to content

Commit 7cdcbcc

Browse files
committed
/series/{id}: group buyers and sellers.
Fix #598
1 parent 3e7355e commit 7cdcbcc

13 files changed

+323
-70
lines changed

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
import ru.mystamps.web.controller.dto.AddSeriesSalesForm;
6565
import ru.mystamps.web.controller.dto.NullableImageUrl;
6666
import ru.mystamps.web.controller.interceptor.DownloadImageInterceptor;
67-
import ru.mystamps.web.dao.dto.EntityWithIdDto;
6867
import ru.mystamps.web.dao.dto.LinkEntityDto;
6968
import ru.mystamps.web.dao.dto.PurchaseAndSaleDto;
7069
import ru.mystamps.web.dao.dto.SeriesInfoDto;
@@ -76,6 +75,7 @@
7675
import ru.mystamps.web.service.TransactionParticipantService;
7776
import ru.mystamps.web.service.dto.DownloadResult;
7877
import ru.mystamps.web.service.dto.FirstLevelCategoryDto;
78+
import ru.mystamps.web.service.dto.GroupedTransactionParticipantDto;
7979
import ru.mystamps.web.service.dto.SeriesDto;
8080
import ru.mystamps.web.support.spring.security.Authority;
8181
import ru.mystamps.web.support.spring.security.CustomUserDetails;
@@ -543,10 +543,12 @@ private void addSeriesSalesFormToModel(Model model) {
543543
model.addAttribute("addSeriesSalesForm", addSeriesSalesForm);
544544
}
545545

546-
List<EntityWithIdDto> sellers = transactionParticipantService.findAllSellers();
546+
List<GroupedTransactionParticipantDto> sellers =
547+
transactionParticipantService.findAllSellers();
547548
model.addAttribute("sellers", sellers);
548549

549-
List<EntityWithIdDto> buyers = transactionParticipantService.findAllBuyers();
550+
List<GroupedTransactionParticipantDto> buyers =
551+
transactionParticipantService.findAllBuyers();
550552
model.addAttribute("buyers", buyers);
551553
}
552554

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
import java.util.List;
2121

2222
import ru.mystamps.web.dao.dto.AddParticipantDbDto;
23-
import ru.mystamps.web.dao.dto.EntityWithIdDto;
23+
import ru.mystamps.web.dao.dto.TransactionParticipantDto;
2424

2525
public interface TransactionParticipantDao {
2626
void add(AddParticipantDbDto participant);
27-
List<EntityWithIdDto> findAllBuyers();
28-
List<EntityWithIdDto> findAllSellers();
27+
List<TransactionParticipantDto> findBuyersWithParents();
28+
List<TransactionParticipantDto> findSellersWithParents();
2929
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (C) 2009-2017 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.dto;
19+
20+
import lombok.Getter;
21+
import lombok.RequiredArgsConstructor;
22+
import lombok.ToString;
23+
24+
@Getter
25+
@ToString
26+
@RequiredArgsConstructor
27+
public class TransactionParticipantDto {
28+
private final Integer id;
29+
private final String name;
30+
private final String parentName;
31+
}

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

+15-9
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
import ru.mystamps.web.dao.TransactionParticipantDao;
3232
import ru.mystamps.web.dao.dto.AddParticipantDbDto;
33-
import ru.mystamps.web.dao.dto.EntityWithIdDto;
33+
import ru.mystamps.web.dao.dto.TransactionParticipantDto;
3434

3535
@RequiredArgsConstructor
3636
public class JdbcTransactionParticipantDao implements TransactionParticipantDao {
@@ -40,11 +40,11 @@ public class JdbcTransactionParticipantDao implements TransactionParticipantDao
4040
@Value("${transaction_participant.create}")
4141
private String addParticipantSql;
4242

43-
@Value("${transaction_participants.find_all_buyers}")
44-
private String findAllBuyersSql;
43+
@Value("${transaction_participant.find_buyers_with_parent_names}")
44+
private String findBuyersWithParentNamesSql;
4545

46-
@Value("${transaction_participants.find_all_sellers}")
47-
private String findAllSellersSql;
46+
@Value("${transaction_participant.find_sellers_with_parent_names}")
47+
private String findSellersWithParentNamesSql;
4848

4949
@Override
5050
public void add(AddParticipantDbDto participant) {
@@ -64,13 +64,19 @@ public void add(AddParticipantDbDto participant) {
6464
}
6565

6666
@Override
67-
public List<EntityWithIdDto> findAllBuyers() {
68-
return jdbcTemplate.query(findAllBuyersSql, RowMappers::forEntityWithIdDto);
67+
public List<TransactionParticipantDto> findBuyersWithParents() {
68+
return jdbcTemplate.query(
69+
findBuyersWithParentNamesSql,
70+
RowMappers::forTransactionParticipantDto
71+
);
6972
}
7073

7174
@Override
72-
public List<EntityWithIdDto> findAllSellers() {
73-
return jdbcTemplate.query(findAllSellersSql, RowMappers::forEntityWithIdDto);
75+
public List<TransactionParticipantDto> findSellersWithParents() {
76+
return jdbcTemplate.query(
77+
findSellersWithParentNamesSql,
78+
RowMappers::forTransactionParticipantDto
79+
);
7480
}
7581

7682
}

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

+10
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,16 @@ public static CategoryDto forCategoryDto(ResultSet rs, int i) throws SQLExceptio
261261
);
262262
}
263263

264+
public static TransactionParticipantDto forTransactionParticipantDto(ResultSet rs, int i)
265+
throws SQLException {
266+
267+
return new TransactionParticipantDto(
268+
rs.getInt("id"),
269+
rs.getString("name"),
270+
rs.getString("parent_name")
271+
);
272+
}
273+
264274
public static ImportRequestDto forImportRequestDto(ResultSet rs, int i) throws SQLException {
265275
return new ImportRequestDto(
266276
rs.getString("url"),

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919

2020
import java.util.List;
2121

22-
import ru.mystamps.web.dao.dto.EntityWithIdDto;
2322
import ru.mystamps.web.service.dto.AddParticipantDto;
23+
import ru.mystamps.web.service.dto.GroupedTransactionParticipantDto;
2424

2525
public interface TransactionParticipantService {
2626
void add(AddParticipantDto dto);
27-
List<EntityWithIdDto> findAllBuyers();
28-
List<EntityWithIdDto> findAllSellers();
27+
List<GroupedTransactionParticipantDto> findAllBuyers();
28+
List<GroupedTransactionParticipantDto> findAllSellers();
2929
}

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

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

20+
import java.util.ArrayList;
21+
import java.util.Collections;
2022
import java.util.List;
2123

2224
import org.apache.commons.lang3.Validate;
@@ -31,8 +33,9 @@
3133

3234
import ru.mystamps.web.dao.TransactionParticipantDao;
3335
import ru.mystamps.web.dao.dto.AddParticipantDbDto;
34-
import ru.mystamps.web.dao.dto.EntityWithIdDto;
36+
import ru.mystamps.web.dao.dto.TransactionParticipantDto;
3537
import ru.mystamps.web.service.dto.AddParticipantDto;
38+
import ru.mystamps.web.service.dto.GroupedTransactionParticipantDto;
3639
import ru.mystamps.web.support.spring.security.HasAuthority;
3740

3841
@RequiredArgsConstructor
@@ -64,15 +67,85 @@ public void add(AddParticipantDto dto) {
6467
@Override
6568
@Transactional(readOnly = true)
6669
@PreAuthorize(HasAuthority.ADD_SERIES_SALES)
67-
public List<EntityWithIdDto> findAllBuyers() {
68-
return transactionParticipantDao.findAllBuyers();
70+
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
71+
public List<GroupedTransactionParticipantDto> findAllBuyers() {
72+
List<TransactionParticipantDto> participants =
73+
transactionParticipantDao.findBuyersWithParents();
74+
if (participants.isEmpty()) {
75+
return Collections.emptyList();
76+
}
77+
78+
// Because of Thymeleaf's restrictions we can't return participants as-is and need this
79+
// transformation
80+
List<GroupedTransactionParticipantDto> items = new ArrayList<>();
81+
String lastParent = null;
82+
GroupedTransactionParticipantDto lastItem = null;
83+
84+
for (TransactionParticipantDto participant : participants) {
85+
String name = participant.getName();
86+
Integer id = participant.getId();
87+
String parent = participant.getParentName();
88+
89+
boolean participantWithoutParent = parent == null;
90+
boolean createNewItem = participantWithoutParent || !parent.equals(lastParent);
91+
92+
if (createNewItem) {
93+
lastParent = parent;
94+
if (participantWithoutParent) {
95+
lastItem = new GroupedTransactionParticipantDto(id, name);
96+
} else {
97+
lastItem = new GroupedTransactionParticipantDto(parent);
98+
lastItem.addChild(id, name);
99+
}
100+
items.add(lastItem);
101+
} else {
102+
lastItem.addChild(id, name);
103+
}
104+
}
105+
106+
return items;
69107
}
70108

71109
@Override
72110
@Transactional(readOnly = true)
73111
@PreAuthorize(HasAuthority.ADD_SERIES_SALES)
74-
public List<EntityWithIdDto> findAllSellers() {
75-
return transactionParticipantDao.findAllSellers();
112+
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
113+
public List<GroupedTransactionParticipantDto> findAllSellers() {
114+
List<TransactionParticipantDto> participants =
115+
transactionParticipantDao.findSellersWithParents();
116+
if (participants.isEmpty()) {
117+
return Collections.emptyList();
118+
}
119+
120+
// Because of Thymeleaf's restrictions we can't return participants as-is and need this
121+
// transformation
122+
List<GroupedTransactionParticipantDto> items = new ArrayList<>();
123+
String lastParent = null;
124+
GroupedTransactionParticipantDto lastItem = null;
125+
126+
for (TransactionParticipantDto participant : participants) {
127+
String name = participant.getName();
128+
Integer id = participant.getId();
129+
String parent = participant.getParentName();
130+
131+
boolean participantWithoutParent = parent == null;
132+
boolean createNewItem = participantWithoutParent || !parent.equals(lastParent);
133+
134+
if (createNewItem) {
135+
lastParent = parent;
136+
if (participantWithoutParent) {
137+
lastItem = new GroupedTransactionParticipantDto(id, name);
138+
} else {
139+
lastItem = new GroupedTransactionParticipantDto(parent);
140+
lastItem.addChild(id, name);
141+
}
142+
items.add(lastItem);
143+
} else {
144+
lastItem.addChild(id, name);
145+
}
146+
}
147+
148+
return items;
76149
}
77150

78151
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (C) 2009-2017 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.service.dto;
19+
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
import lombok.Getter;
24+
import lombok.ToString;
25+
26+
import ru.mystamps.web.dao.dto.EntityWithIdDto;
27+
28+
@Getter
29+
@ToString
30+
public class GroupedTransactionParticipantDto {
31+
private final Integer id;
32+
private final String name;
33+
private final List<EntityWithIdDto> children = new ArrayList<>();
34+
35+
public GroupedTransactionParticipantDto(Integer id, String name) {
36+
this.id = id;
37+
this.name = name;
38+
}
39+
40+
public GroupedTransactionParticipantDto(String name) {
41+
this(null, name);
42+
}
43+
44+
public void addChild(Integer id, String name) {
45+
children.add(new EntityWithIdDto(id, name));
46+
}
47+
48+
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@
3838
<include file="0.4/2017-11-09--categories_aliases.xml" relativeToChangelogFile="true" />
3939
<include file="0.4/2017-11-09--countries_aliases.xml" relativeToChangelogFile="true" />
4040
<include file="0.4/2017-11-14--separate_buyers_and_sellers.xml" relativeToChangelogFile="true" />
41+
<include file="0.4/2017-11-15--group_participants.xml" relativeToChangelogFile="true" />
4142

4243
</databaseChangeLog>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.1.xsd">
7+
8+
<changeSet id="create-transaction_participant_groups-table" author="php-coder" context="scheme">
9+
10+
<createTable tableName="transaction_participant_groups">
11+
<column name="id" type="INTEGER" autoIncrement="true">
12+
<constraints primaryKey="true" />
13+
</column>
14+
<column name="name" type="VARCHAR(50)">
15+
<constraints unique="true" uniqueConstraintName="uc_transaction_participant_groups_name" nullable="false" />
16+
</column>
17+
<column name="created_at" type="DATETIME">
18+
<constraints nullable="false" />
19+
</column>
20+
<column name="created_by" type="INTEGER">
21+
<constraints nullable="false" references="users(id)" foreignKeyName="fk_transaction_participant_groups_created_by" />
22+
</column>
23+
<column name="updated_at" type="DATETIME">
24+
<constraints nullable="false" />
25+
</column>
26+
<column name="updated_by" type="INTEGER">
27+
<constraints nullable="false" references="users(id)" foreignKeyName="fk_transaction_participant_groups_updated_by" />
28+
</column>
29+
</createTable>
30+
31+
</changeSet>
32+
33+
<changeSet id="add-transaction_participant-group_id-field" author="php-coder" context="scheme">
34+
35+
<addColumn tableName="transaction_participants">
36+
<column name="group_id" type="INTEGER">
37+
<constraints references="transaction_participant_groups(id)" foreignKeyName="fk_transaction_participants_group_id" />
38+
</column>
39+
</addColumn>
40+
41+
</changeSet>
42+
43+
<changeSet id="add-movies-characters-participant-group" author="php-coder" context="test-data">
44+
45+
<insert tableName="transaction_participant_groups">
46+
<column name="name" value="Movies characters" />
47+
<column name="created_at" valueComputed="${NOW}" />
48+
<column name="created_by" valueComputed="(SELECT id FROM users WHERE role = 'ADMIN' ORDER by id LIMIT 1)" />
49+
<column name="updated_at" valueComputed="${NOW}" />
50+
<column name="updated_by" valueComputed="(SELECT id FROM users WHERE role = 'ADMIN' ORDER by id LIMIT 1)" />
51+
</insert>
52+
53+
</changeSet>
54+
55+
<changeSet id="update-italy-country-name" author="php-coder" context="test-data">
56+
57+
<update tableName="transaction_participants">
58+
<column name="group_id" valueNumeric="1" />
59+
<where>name = :value</where>
60+
<whereParams>
61+
<param value="John Connor" />
62+
</whereParams>
63+
</update>
64+
65+
<update tableName="transaction_participants">
66+
<column name="group_id" valueNumeric="1" />
67+
<where>name = :value</where>
68+
<whereParams>
69+
<param value="Duncan MacLeod" />
70+
</whereParams>
71+
</update>
72+
73+
</changeSet>
74+
75+
</databaseChangeLog>

0 commit comments

Comments
 (0)