Skip to content

Commit a5098d7

Browse files
committed
Introduce SelectItem/SelectOption as unified successor of GroupedTransactionParticipantDto.
Addressed to #592 No functional changes.
1 parent 57e4cc1 commit a5098d7

File tree

5 files changed

+67
-35
lines changed

5 files changed

+67
-35
lines changed

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@
6262
import ru.mystamps.web.controller.dto.AddSeriesForm;
6363
import ru.mystamps.web.controller.dto.AddSeriesSalesForm;
6464
import ru.mystamps.web.controller.dto.FirstLevelCategoryDto;
65-
import ru.mystamps.web.controller.dto.GroupedTransactionParticipantDto;
6665
import ru.mystamps.web.controller.dto.NullableImageUrl;
66+
import ru.mystamps.web.controller.dto.SelectItem;
6767
import ru.mystamps.web.controller.interceptor.DownloadImageInterceptor;
6868
import ru.mystamps.web.dao.dto.CategoryDto;
6969
import ru.mystamps.web.dao.dto.LinkEntityDto;
@@ -587,14 +587,12 @@ private void addSeriesSalesFormToModel(Model model) {
587587

588588
List<TransactionParticipantDto> sellers =
589589
transactionParticipantService.findSellersWithParents();
590-
List<GroupedTransactionParticipantDto> groupedSellers =
591-
GroupByParent.transformParticipants(sellers);
590+
List<SelectItem> groupedSellers = GroupByParent.transformParticipants(sellers);
592591
model.addAttribute("sellers", groupedSellers);
593592

594593
List<TransactionParticipantDto> buyers =
595594
transactionParticipantService.findBuyersWithParents();
596-
List<GroupedTransactionParticipantDto> groupedBuyers =
597-
GroupByParent.transformParticipants(buyers);
595+
List<SelectItem> groupedBuyers = GroupByParent.transformParticipants(buyers);
598596
model.addAttribute("buyers", groupedBuyers);
599597
}
600598

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,32 @@
1818
package ru.mystamps.web.controller.dto;
1919

2020
import java.util.ArrayList;
21+
import java.util.Collections;
2122
import java.util.List;
2223

2324
import lombok.Getter;
24-
import lombok.ToString;
25-
26-
import ru.mystamps.web.dao.dto.EntityWithIdDto;
2725

2826
@Getter
29-
@ToString
30-
public class GroupedTransactionParticipantDto {
31-
private final Integer id;
27+
public class SelectItem {
3228
private final String name;
33-
private final List<EntityWithIdDto> children = new ArrayList<>();
29+
private final String value;
30+
31+
private List<SelectOption> children = Collections.emptyList();
3432

35-
public GroupedTransactionParticipantDto(Integer id, String name) {
36-
this.id = id;
33+
public SelectItem(String name, String value) {
3734
this.name = name;
35+
this.value = value;
3836
}
3937

40-
public GroupedTransactionParticipantDto(String name) {
41-
this(null, name);
38+
public SelectItem(String name) {
39+
this(name, null);
4240
}
4341

44-
public void addChild(Integer id, String name) {
45-
children.add(new EntityWithIdDto(id, name));
42+
public void addChild(String name, String value) {
43+
if (children.isEmpty()) {
44+
children = new ArrayList<>();
45+
}
46+
children.add(new SelectOption(name, value));
4647
}
4748

4849
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (C) 2009-2017 Slava Semushin <slava.semushin@gmail.com>
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.dto;
19+
20+
import lombok.Getter;
21+
import lombok.RequiredArgsConstructor;
22+
23+
@Getter
24+
@RequiredArgsConstructor
25+
public class SelectOption {
26+
private final String name;
27+
private final String value;
28+
}

src/main/java/ru/mystamps/web/support/thymeleaf/GroupByParent.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import java.util.List;
2323

2424
import ru.mystamps.web.controller.dto.FirstLevelCategoryDto;
25-
import ru.mystamps.web.controller.dto.GroupedTransactionParticipantDto;
25+
import ru.mystamps.web.controller.dto.SelectItem;
2626
import ru.mystamps.web.dao.dto.CategoryDto;
2727
import ru.mystamps.web.dao.dto.TransactionParticipantDto;
2828

@@ -34,24 +34,27 @@
3434
*/
3535
public final class GroupByParent {
3636

37+
// @todo #592 GroupByParent: add unit tests
3738
private GroupByParent() {
3839
}
3940

41+
// @todo #592 GroupByParent.transformParticipants(): introduce unified class for representing entity with parent
42+
// @todo #592 GroupByParent: merge transformCategories() and transformParticipants() methods
4043
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
41-
public static List<GroupedTransactionParticipantDto> transformParticipants(
44+
public static List<SelectItem> transformParticipants(
4245
List<TransactionParticipantDto> participants) {
4346

4447
if (participants.isEmpty()) {
4548
return Collections.emptyList();
4649
}
4750

48-
List<GroupedTransactionParticipantDto> items = new ArrayList<>();
51+
List<SelectItem> items = new ArrayList<>();
4952
String lastParent = null;
50-
GroupedTransactionParticipantDto lastItem = null;
53+
SelectItem lastItem = null;
5154

5255
for (TransactionParticipantDto participant : participants) {
5356
String name = participant.getName();
54-
Integer id = participant.getId();
57+
String value = participant.getId().toString();
5558
String parent = participant.getParentName();
5659

5760
boolean participantWithoutParent = parent == null;
@@ -60,20 +63,22 @@ public static List<GroupedTransactionParticipantDto> transformParticipants(
6063
if (createNewItem) {
6164
lastParent = parent;
6265
if (participantWithoutParent) {
63-
lastItem = new GroupedTransactionParticipantDto(id, name);
66+
lastItem = new SelectItem(name, value);
6467
} else {
65-
lastItem = new GroupedTransactionParticipantDto(parent);
66-
lastItem.addChild(id, name);
68+
lastItem = new SelectItem(parent);
69+
lastItem.addChild(name, value);
6770
}
6871
items.add(lastItem);
6972
} else {
70-
lastItem.addChild(id, name);
73+
lastItem.addChild(name, value);
7174
}
7275
}
7376

7477
return items;
7578
}
7679

80+
// @todo #592 GroupByParent.transformCategories(): replace FirstLevelCategoryDto by SelectItem
81+
// @todo #592 GroupByParent.transformCategories(): use unified class that represents entity with parent
7782
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
7883
public static List<FirstLevelCategoryDto> transformCategories(List<CategoryDto> categories) {
7984
if (categories.isEmpty()) {

src/main/webapp/WEB-INF/views/series/info.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -404,15 +404,15 @@ <h5 th:text="|#{t_add_info_who_selling_series}:|">Add info about selling/buying
404404
<th:block th:each="item : ${sellers}">
405405
<optgroup th:if="${not #lists.isEmpty(item.children)}" th:label="${item.name}">
406406
<option th:each="seller : ${item.children}"
407-
th:value="${seller.id}"
407+
th:value="${seller.value}"
408408
th:text="${seller.name}"
409-
th:selected="${addSeriesSalesForm.sellerId != seller.id}">
409+
th:selected="${addSeriesSalesForm.sellerId != seller.value}">
410410
</option>
411411
</optgroup>
412412
<option th:if="${#lists.isEmpty(item.children)}"
413-
th:value="${item.id}"
413+
th:value="${item.value}"
414414
th:text="${item.name}"
415-
th:selected="${addSeriesSalesForm.sellerId != item.id}">
415+
th:selected="${addSeriesSalesForm.sellerId != item.value}">
416416
</option>
417417
</th:block>
418418
/*/-->
@@ -515,15 +515,15 @@ <h5 th:text="|#{t_add_info_who_selling_series}:|">Add info about selling/buying
515515
<th:block th:each="item : ${buyers}">
516516
<optgroup th:if="${not #lists.isEmpty(item.children)}" th:label="${item.name}">
517517
<option th:each="buyer : ${item.children}"
518-
th:value="${buyer.id}"
518+
th:value="${buyer.value}"
519519
th:text="${buyer.name}"
520-
th:selected="${addSeriesSalesForm.buyerId != buyer.id}">
520+
th:selected="${addSeriesSalesForm.buyerId != buyer.value}">
521521
</option>
522522
</optgroup>
523523
<option th:if="${#lists.isEmpty(item.children)}"
524-
th:value="${item.id}"
524+
th:value="${item.value}"
525525
th:text="${item.name}"
526-
th:selected="${addSeriesSalesForm.buyerId != item.id}">
526+
th:selected="${addSeriesSalesForm.buyerId != item.value}">
527527
</option>
528528
</th:block>
529529
/*/-->

0 commit comments

Comments
 (0)