|
17 | 17 | */
|
18 | 18 | package ru.mystamps.web.service;
|
19 | 19 |
|
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collections; |
20 | 22 | import java.util.List;
|
21 | 23 |
|
22 | 24 | import org.apache.commons.lang3.Validate;
|
|
31 | 33 |
|
32 | 34 | import ru.mystamps.web.dao.TransactionParticipantDao;
|
33 | 35 | import ru.mystamps.web.dao.dto.AddParticipantDbDto;
|
34 |
| -import ru.mystamps.web.dao.dto.EntityWithIdDto; |
| 36 | +import ru.mystamps.web.dao.dto.TransactionParticipantDto; |
35 | 37 | import ru.mystamps.web.service.dto.AddParticipantDto;
|
| 38 | +import ru.mystamps.web.service.dto.GroupedTransactionParticipantDto; |
36 | 39 | import ru.mystamps.web.support.spring.security.HasAuthority;
|
37 | 40 |
|
38 | 41 | @RequiredArgsConstructor
|
@@ -64,15 +67,85 @@ public void add(AddParticipantDto dto) {
|
64 | 67 | @Override
|
65 | 68 | @Transactional(readOnly = true)
|
66 | 69 | @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; |
69 | 107 | }
|
70 | 108 |
|
71 | 109 | @Override
|
72 | 110 | @Transactional(readOnly = true)
|
73 | 111 | @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; |
76 | 149 | }
|
77 | 150 |
|
78 | 151 | }
|
0 commit comments