/
SpecialPriceTokenGenerator.java
122 lines (107 loc) · 4.95 KB
/
SpecialPriceTokenGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* This file is part of alf.io.
*
* alf.io is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* alf.io is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with alf.io. If not, see <http://www.gnu.org/licenses/>.
*/
package alfio.manager;
import alfio.manager.system.ConfigurationLevel;
import alfio.manager.system.ConfigurationManager;
import alfio.model.EventAndOrganizationId;
import alfio.model.SpecialPrice;
import alfio.model.TicketCategory;
import alfio.model.system.ConfigurationKeys;
import alfio.repository.EventRepository;
import alfio.repository.SpecialPriceRepository;
import alfio.repository.TicketCategoryRepository;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.time.StopWatch;
import org.apache.commons.text.RandomStringGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.security.SecureRandom;
/**
* Class SpecialPriceTokenGenerator.
* This class generates a bunch of tokens which will be used for
* granting a special price to a specific user category.
*/
@Component
@Transactional
public class SpecialPriceTokenGenerator {
private static final Logger log = LoggerFactory.getLogger(SpecialPriceTokenGenerator.class);
private static final SecureRandom RANDOM = new SecureRandom();
private static final char[] ADMITTED_CHARACTERS = new char[]{
'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'J', 'K', 'M', 'N',
'P', 'Q', 'R', 'S', 'T', 'W',
'X', 'Y', 'Z', '2', '3', '4',
'5', '6', '7', '8', '9'
};
private static final RandomStringGenerator RANDOM_STRING_GENERATOR = new RandomStringGenerator.Builder()
.selectFrom(ADMITTED_CHARACTERS)
.usingRandom(RANDOM::nextInt)
.get();
private final SpecialPriceRepository specialPriceRepository;
private final TicketCategoryRepository ticketCategoryRepository;
private final EventRepository eventRepository;
private final ConfigurationManager configurationManager;
public SpecialPriceTokenGenerator(ConfigurationManager configurationManager,
SpecialPriceRepository specialPriceRepository,
TicketCategoryRepository ticketCategoryRepository,
EventRepository eventRepository) {
this.specialPriceRepository = specialPriceRepository;
this.configurationManager = configurationManager;
this.ticketCategoryRepository = ticketCategoryRepository;
this.eventRepository = eventRepository;
}
public void generatePendingCodes() {
StopWatch stopWatch = new StopWatch();
log.trace("start pending codes generation");
stopWatch.start();
specialPriceRepository.findWaitingElements().forEach(this::generateCode);
stopWatch.stop();
log.trace("end. Took {} ms", stopWatch.getTime());
}
public void generatePendingCodesForCategory(int categoryId) {
specialPriceRepository.findWaitingElementsForCategory(categoryId).forEach(this::generateCode);
}
private void generateCode(SpecialPrice.SpecialPriceTicketCategoryId specialPrice) {
TicketCategory ticketCategory = ticketCategoryRepository.getByIdAndActive(specialPrice.getTicketCategoryId()).orElseThrow(IllegalStateException::new);
EventAndOrganizationId event = eventRepository.findEventAndOrganizationIdById(ticketCategory.getEventId());
int maxLength = configurationManager.getFor(ConfigurationKeys.SPECIAL_PRICE_CODE_LENGTH, ConfigurationLevel.ticketCategory(event, ticketCategory.getId())).getValueAsIntOrDefault(6);
while (true) {
try {
log.trace("generate code for special price with id {}", specialPrice.getId());
specialPriceRepository.updateCode(nextValidCode(maxLength), specialPrice.getId());
log.trace("done.");
return;
} catch (DataAccessException e) {
log.warn("got a duplicate. Retrying...", e);
}
}
}
private String nextValidCode(int maxLength) {
while (true) {
String code = generateRandomCode(maxLength);
if (specialPriceRepository.countByCode(code) == 0) {
return code;
}
}
}
private String generateRandomCode(int maxLength) {
return RANDOM_STRING_GENERATOR.generate(maxLength);
}
}