Skip to content

Commit 0644433

Browse files
committed
GroupByParent.transformParticipants(): extract duplicated code from TransactionParticipantServiceImpl methods.
Also rename original methods. Addressed to #592 No functional changes.
1 parent d246e5b commit 0644433

File tree

6 files changed

+100
-87
lines changed

6 files changed

+100
-87
lines changed

src/main/config/checkstyle-suppressions.xml

+1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616

1717
<!-- Too long link -->
1818
<suppress checks="LineLength" files="src/main/java/ru/mystamps/web/dao/impl/package-info.java" />
19+
<suppress checks="LineLength" files="src/main/java/ru/mystamps/web/support/thymeleaf/GroupByParent.java" />
1920

2021
</suppressions>

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

+13-7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import ru.mystamps.web.dao.dto.LinkEntityDto;
6767
import ru.mystamps.web.dao.dto.PurchaseAndSaleDto;
6868
import ru.mystamps.web.dao.dto.SeriesInfoDto;
69+
import ru.mystamps.web.dao.dto.TransactionParticipantDto;
6970
import ru.mystamps.web.service.CategoryService;
7071
import ru.mystamps.web.service.CollectionService;
7172
import ru.mystamps.web.service.CountryService;
@@ -79,6 +80,7 @@
7980
import ru.mystamps.web.support.spring.security.Authority;
8081
import ru.mystamps.web.support.spring.security.CustomUserDetails;
8182
import ru.mystamps.web.support.spring.security.SecurityContextUtils;
83+
import ru.mystamps.web.support.thymeleaf.GroupByParent;
8284
import ru.mystamps.web.support.togglz.Features;
8385
import ru.mystamps.web.util.CatalogUtils;
8486
import ru.mystamps.web.util.LocaleUtils;
@@ -542,13 +544,17 @@ private void addSeriesSalesFormToModel(Model model) {
542544
model.addAttribute("addSeriesSalesForm", addSeriesSalesForm);
543545
}
544546

545-
List<GroupedTransactionParticipantDto> sellers =
546-
transactionParticipantService.findAllSellers();
547-
model.addAttribute("sellers", sellers);
548-
549-
List<GroupedTransactionParticipantDto> buyers =
550-
transactionParticipantService.findAllBuyers();
551-
model.addAttribute("buyers", buyers);
547+
List<TransactionParticipantDto> sellers =
548+
transactionParticipantService.findSellersWithParents();
549+
List<GroupedTransactionParticipantDto> groupedSellers =
550+
GroupByParent.transformParticipants(sellers);
551+
model.addAttribute("sellers", groupedSellers);
552+
553+
List<TransactionParticipantDto> buyers =
554+
transactionParticipantService.findBuyersWithParents();
555+
List<GroupedTransactionParticipantDto> groupedBuyers =
556+
GroupByParent.transformParticipants(buyers);
557+
model.addAttribute("buyers", groupedBuyers);
552558
}
553559

554560
// false positive on Travis CI

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

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

2222
import ru.mystamps.web.dao.dto.EntityWithIdDto;
23+
import ru.mystamps.web.dao.dto.TransactionParticipantDto;
2324
import ru.mystamps.web.service.dto.AddParticipantDto;
24-
import ru.mystamps.web.service.dto.GroupedTransactionParticipantDto;
2525

2626
public interface TransactionParticipantService {
2727
void add(AddParticipantDto dto);
28-
List<GroupedTransactionParticipantDto> findAllBuyers();
29-
List<GroupedTransactionParticipantDto> findAllSellers();
28+
List<TransactionParticipantDto> findBuyersWithParents();
29+
List<TransactionParticipantDto> findSellersWithParents();
3030
List<EntityWithIdDto> findAllGroups();
3131
}

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

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

20-
import java.util.ArrayList;
21-
import java.util.Collections;
2220
import java.util.List;
2321

2422
import org.apache.commons.lang3.Validate;
@@ -36,7 +34,6 @@
3634
import ru.mystamps.web.dao.dto.EntityWithIdDto;
3735
import ru.mystamps.web.dao.dto.TransactionParticipantDto;
3836
import ru.mystamps.web.service.dto.AddParticipantDto;
39-
import ru.mystamps.web.service.dto.GroupedTransactionParticipantDto;
4037
import ru.mystamps.web.support.spring.security.HasAuthority;
4138

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

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

15380
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.support.thymeleaf;
19+
20+
import java.util.ArrayList;
21+
import java.util.Collections;
22+
import java.util.List;
23+
24+
import ru.mystamps.web.dao.dto.TransactionParticipantDto;
25+
import ru.mystamps.web.service.dto.GroupedTransactionParticipantDto;
26+
27+
/**
28+
* Transforms flat list to hierarchical structure suitable for rendering a &lt;select&gt; tag
29+
* with &lt;optgroup&gt; in Thymeleaf.
30+
*
31+
* See also: <a href="https://gist.github.com/php-coder/d3020e4d8d00b8c5befe755c46f06f1b" target="_blank">gist with example</a>.
32+
*/
33+
public final class GroupByParent {
34+
35+
private GroupByParent() {
36+
}
37+
38+
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
39+
public static List<GroupedTransactionParticipantDto> transformParticipants(
40+
List<TransactionParticipantDto> participants) {
41+
42+
if (participants.isEmpty()) {
43+
return Collections.emptyList();
44+
}
45+
46+
List<GroupedTransactionParticipantDto> items = new ArrayList<>();
47+
String lastParent = null;
48+
GroupedTransactionParticipantDto lastItem = null;
49+
50+
for (TransactionParticipantDto participant : participants) {
51+
String name = participant.getName();
52+
Integer id = participant.getId();
53+
String parent = participant.getParentName();
54+
55+
boolean participantWithoutParent = parent == null;
56+
boolean createNewItem = participantWithoutParent || !parent.equals(lastParent);
57+
58+
if (createNewItem) {
59+
lastParent = parent;
60+
if (participantWithoutParent) {
61+
lastItem = new GroupedTransactionParticipantDto(id, name);
62+
} else {
63+
lastItem = new GroupedTransactionParticipantDto(parent);
64+
lastItem.addChild(id, name);
65+
}
66+
items.add(lastItem);
67+
} else {
68+
lastItem.addChild(id, name);
69+
}
70+
}
71+
72+
return items;
73+
}
74+
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* Integration with <a href="http://www.thymeleaf.org/" target="_blank">Thymeleaf</a>.
3+
*/
4+
package ru.mystamps.web.support.thymeleaf;

0 commit comments

Comments
 (0)