/
FieldConfigurationDescriptionAndValue.java
149 lines (130 loc) · 5.46 KB
/
FieldConfigurationDescriptionAndValue.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
/**
* 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.util.Json;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.experimental.Delegate;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Triple;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@AllArgsConstructor
public class FieldConfigurationDescriptionAndValue {
@Delegate
private final PurchaseContextFieldConfiguration purchaseContextFieldConfiguration;
@Delegate
private final PurchaseContextFieldDescription purchaseContextFieldDescription;
private final int count;
private final String value;
private static final List<String> TEXT_FIELD_TYPES = List.of(
"text",
"tel",
"textarea",
"vat:eu",
"dateOfBirth"
);
private static final Pattern CHECKBOX_VALUES_PATTERN = Pattern.compile("\"(.*?)\",?");
public String getTranslatedValue() {
if(StringUtils.isBlank(value)) {
return value;
}
if(isSelectField()) {
return purchaseContextFieldDescription.getRestrictedValuesDescription().getOrDefault(value, "MISSING_DESCRIPTION");
}
return value;
}
public List<Triple<String, String, Boolean>> getTranslatedRestrictedValue() {
Map<String, String> description = purchaseContextFieldDescription.getRestrictedValuesDescription();
return purchaseContextFieldConfiguration.getRestrictedValues()
.stream()
.map(val -> Triple.of(val, description.getOrDefault(val, "MISSING_DESCRIPTION"), isFieldValueEnabled(purchaseContextFieldConfiguration, val)))
.collect(Collectors.toList());
}
public List<TicketFieldValue> getFields() {
if(count == 1) {
return Collections.singletonList(new TicketFieldValue(0, 1, value, isAcceptingValues()));
}
List<String> values = StringUtils.isBlank(value) ? Collections.emptyList() : Json.fromJson(value, new TypeReference<>() {});
return IntStream.range(0, count)
.mapToObj(i -> new TicketFieldValue(i, i+1, i < values.size() ? values.get(i) : "", isAcceptingValues()))
.collect(Collectors.toList());
}
private boolean isText() {
return purchaseContextFieldConfiguration.isTextField()
|| TEXT_FIELD_TYPES.contains(purchaseContextFieldConfiguration.getType());
}
public String getValueDescription() {
if(isText()) {
return value;
} else if(isCheckboxField()) {
var matches = new ArrayList<String>();
var matcher = CHECKBOX_VALUES_PATTERN.matcher(value);
while(matcher.find()) {
matches.add(matcher.group(1));
}
var restrictedValues = getTranslatedRestrictedValue();
return matches.stream()
.map(v -> findValueDescription(restrictedValues, v))
.filter(StringUtils::isNotBlank)
.collect(Collectors.joining(", "));
} else {
return findValueDescription(getTranslatedRestrictedValue(), value);
}
}
private String findValueDescription(List<Triple<String, String, Boolean>> translateRestrictedValues,
String value) {
return translateRestrictedValues.stream()
.filter(t -> StringUtils.equals(t.getLeft(), value))
.map(Triple::getMiddle)
.findFirst()
.orElse("");
}
public String getValue() {
return value;
}
private boolean isAcceptingValues() {
return isEditable() || StringUtils.isBlank(value);
}
public boolean isBeforeStandardFields() {
return isBeforeStandardFields(purchaseContextFieldConfiguration);
}
private static boolean isFieldValueEnabled(PurchaseContextFieldConfiguration purchaseContextFieldConfiguration, String value) {
return !purchaseContextFieldConfiguration.isSelectField()
|| CollectionUtils.isEmpty(purchaseContextFieldConfiguration.getDisabledValues())
|| !purchaseContextFieldConfiguration.getDisabledValues().contains(value);
}
@RequiredArgsConstructor
@Getter
public static class TicketFieldValue {
private final int fieldIndex;
private final int fieldCounter;
private final String fieldValue;
private final Boolean editable;
}
public static boolean isBeforeStandardFields(PurchaseContextFieldConfiguration purchaseContextFieldConfiguration) {
return purchaseContextFieldConfiguration.getOrder() < 0;
}
}