-
-
Notifications
You must be signed in to change notification settings - Fork 357
/
Copy pathEvent.java
305 lines (253 loc) · 9.44 KB
/
Event.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/**
* 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.model;
import alfio.model.transaction.PaymentProxy;
import alfio.util.ClockProvider;
import alfio.util.EventUtil;
import alfio.util.MonetaryUtil;
import ch.digitalfondue.npjt.ConstructorAnnotationRowMapper.Column;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.flywaydb.core.api.MigrationVersion;
import java.math.BigDecimal;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;
import static java.time.temporal.ChronoField.OFFSET_SECONDS;
@Getter
public class Event extends EventAndOrganizationId implements EventHiddenFieldContainer, EventCheckInInfo, PurchaseContext {
private static final String VERSION_FOR_FIRST_AND_LAST_NAME = "15.1.8.8";
public enum Status {
DRAFT, PUBLIC, DISABLED
}
public enum EventFormat {
IN_PERSON, ONLINE, HYBRID
}
private final EventFormat format;
private final String shortName;
private final String displayName;
private final String websiteUrl;
private final String externalUrl;
private final String termsAndConditionsUrl;
private final String privacyPolicyUrl;
private final String imageUrl;
private final String fileBlobId;
private final String location;
private final String latitude;
private final String longitude;
private final ZonedDateTime begin;
private final ZonedDateTime end;
private final String currency;
private final boolean vatIncluded;
private final BigDecimal vat;
private final List<PaymentProxy> allowedPaymentProxies;
@JsonIgnore
private final String privateKey;
private final ZoneId timeZone;
private final int locales;
private final int srcPriceCts;
private final PriceContainer.VatStatus vatStatus;
private final String version;
private final Status status;
public Event(@Column("id") int id,
@Column("format") EventFormat format,
@Column("short_name") String shortName,
@Column("display_name") String displayName,
@Column("location") String location,
@Column("latitude") String latitude,
@Column("longitude") String longitude,
@Column("start_ts") ZonedDateTime begin,
@Column("end_ts") ZonedDateTime end,
@Column("time_zone") String timeZone,
@Column("website_url") String websiteUrl,
@Column("external_url") String externalUrl,
@Column("file_blob_id") String fileBlobId,
@Column("website_t_c_url") String termsAndConditionsUrl,
@Column("website_p_p_url") String privacyPolicyUrl,
@Column("image_url") String imageUrl,
@Column("currency") String currency,
@Column("vat") BigDecimal vat,
@Column("allowed_payment_proxies") String allowedPaymentProxies,
@Column("private_key") String privateKey,
@Column("org_id") int organizationId,
@Column("locales") int locales,
@Column("src_price_cts") int srcPriceInCents,
@Column("vat_status") PriceContainer.VatStatus vatStatus,
@Column("version") String version,
@Column("status") Status status) {
super(id, organizationId);
this.format = format;
this.displayName = displayName;
this.websiteUrl = websiteUrl;
this.externalUrl = externalUrl;
this.termsAndConditionsUrl = termsAndConditionsUrl;
this.privacyPolicyUrl = privacyPolicyUrl;
this.imageUrl = imageUrl;
this.fileBlobId = fileBlobId;
final ZoneId zoneId = TimeZone.getTimeZone(timeZone).toZoneId();
this.shortName = shortName;
this.location = location;
this.latitude = latitude;
this.longitude = longitude;
this.timeZone = zoneId;
this.begin = begin.withZoneSameInstant(zoneId);
this.end = end.withZoneSameInstant(zoneId);
this.currency = currency;
this.vatIncluded = vatStatus == PriceContainer.VatStatus.INCLUDED;
this.vat = vat;
this.privateKey = privateKey;
this.locales = locales;
this.allowedPaymentProxies = Arrays.stream(Optional.ofNullable(allowedPaymentProxies).orElse("").split(","))
.filter(StringUtils::isNotBlank)
.map(PaymentProxy::valueOf)
.collect(Collectors.toList());
this.vatStatus = vatStatus;
this.srcPriceCts = srcPriceInCents;
this.version = version;
this.status = status;
}
@Override
public Map<String, String> getTitle() {
return buildTitle(displayName, locales);
}
static Map<String, String> buildTitle(String displayName, int locales) {
return ContentLanguage.findAllFor(locales).stream()
.collect(Collectors.toMap(cl -> cl.getLocale().getLanguage(), cl -> displayName));
}
public BigDecimal getRegularPrice() {
return MonetaryUtil.centsToUnit(srcPriceCts, currency);
}
public boolean getSameDay() {
return begin.truncatedTo(ChronoUnit.DAYS).equals(end.truncatedTo(ChronoUnit.DAYS));
}
@Override
@JsonIgnore
public String getPrivateKey() {
return privateKey;
}
@Override
@JsonIgnore
public Pair<String, String> getLatLong() {
return Pair.of(latitude, longitude);
}
/**
* Returns the begin date in the event's timezone
* @return Date
*/
public ZonedDateTime getBegin() {
return begin;
}
/**
* Returns the end date in the event's timezone
* @return Date
*/
public ZonedDateTime getEnd() {
return end;
}
/**
* Returns a string representation of the event's time zone
* @return timeZone
*/
public String getTimeZone() {
return timeZone.toString();
}
@Override
@JsonIgnore
public ZoneId getZoneId() {
return timeZone;
}
@Override
public boolean isFreeOfCharge() {
return srcPriceCts == 0;
}
public boolean getFree() {
return isFreeOfCharge();
}
public boolean getImageIsPresent() {
return StringUtils.isNotBlank(imageUrl) || StringUtils.isNotBlank(fileBlobId);
}
public boolean getFileBlobIdIsPresent() {
return StringUtils.isNotBlank(fileBlobId);
}
public boolean getMultiplePaymentMethods() {
return allowedPaymentProxies.size() > 1;
}
public PaymentProxy getFirstPaymentMethod() {
return allowedPaymentProxies.isEmpty() ? null : allowedPaymentProxies.get(0);//it is guaranteed that this list is not null.
}
public boolean supportsPaymentMethod(PaymentProxy paymentProxy) {
return allowedPaymentProxies.contains(paymentProxy);
}
public List<ContentLanguage> getContentLanguages() {
return ContentLanguage.findAllFor(getLocales());
}
public boolean isOnline() {
return format == EventFormat.ONLINE;
}
// mustache
public boolean getOnline() {
return isOnline();
}
public boolean getUseFirstAndLastName() {
return mustUseFirstAndLastName();
}
public boolean mustUseFirstAndLastName() {
return mustUseFirstAndLastName(this);
}
public boolean supportsQRCodeCaseInsensitive() {
return EventUtil.supportsCaseInsensitiveQRCode(version);
}
private static boolean mustUseFirstAndLastName(Event event) {
return event.getVersion() != null
&& MigrationVersion.fromVersion(event.getVersion()).compareTo(MigrationVersion.fromVersion(VERSION_FOR_FIRST_AND_LAST_NAME)) >= 0;
}
public boolean expired() {
return expiredSince(0);
}
public boolean expiredSince(int days) {
return ZonedDateTime.now(ClockProvider.clock().withZone(getZoneId())).truncatedTo(ChronoUnit.DAYS).minusDays(days).isAfter(getEnd().truncatedTo(ChronoUnit.DAYS));
}
public String getPrivacyPolicyLinkOrNull() {
return StringUtils.trimToNull(privacyPolicyUrl);
}
public int getBeginTimeZoneOffset() {
return getBegin().getOffset().get(OFFSET_SECONDS);
}
public int getEndTimeZoneOffset() {
return getEnd().getOffset().get(OFFSET_SECONDS);
}
public boolean getIsOnline() {
return format == EventFormat.ONLINE;
}
@Override
public String getPublicIdentifier() {
return getShortName();
}
@Override
public PurchaseContextType getType() {
return PurchaseContextType.event;
}
@Override
@JsonIgnore
public Optional<Event> event() {
return Optional.of(this);
}
}