-
Notifications
You must be signed in to change notification settings - Fork 7
/
LookupValues.java
129 lines (113 loc) · 4.23 KB
/
LookupValues.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
/*
* Made with all the love in the world
* by scireum in Remshalden, Germany
*
* Copyright by scireum GmbH
* http://www.scireum.de - [email protected]
*/
package sirius.biz.codelists;
import sirius.db.mixing.types.StringList;
import sirius.kernel.commons.Tuple;
import sirius.kernel.di.std.Part;
import java.util.List;
/**
* Represents a string list value backed by a {@link LookupTable} to be used in database entities.
* Note that for single strings, {@link LookupValue} can be used. Also note that internally the values are stored
* as {@link StringList}.
*/
public class LookupValues extends StringList {
private final String lookupTableName;
private LookupTable table;
private final LookupValue.Display display;
private final LookupValue.Display extendedDisplay;
private final LookupValue.Export export;
private final LookupValue.CustomValues customValues;
@Part
private static LookupTables lookupTables;
/**
* Creates a new list with the given settings.
* <p>
* Note that when using the list in database entities, the field has to be final, as the actual values
* is stored internally.
*
* @param lookupTableName the lookup table used to draw metadata from
* @param customValues determines if custom values are supported
* @param display determines how values are rendered in the UI
* @param extendedDisplay determines how values are rendered in the UI in cases where we want to be more verbose
* @param export determines how values are rendered in exports
*/
public LookupValues(String lookupTableName,
LookupValue.CustomValues customValues,
LookupValue.Display display,
LookupValue.Display extendedDisplay,
LookupValue.Export export) {
this.lookupTableName = lookupTableName;
this.customValues = customValues;
this.display = display;
this.extendedDisplay = extendedDisplay;
this.export = export;
}
/**
* Creates a new list using default settings.
* <p>
* Note that when using the list in database entities, the field has to be final, as the actual values
* are stored internally.
* <p>
* By default, <tt>LookupValues</tt> doesn't support custom values, shows the name in the UI and exports the
* normalized code.
*
* @param lookupTableName the lookup table used to draw metadata from
*/
public LookupValues(String lookupTableName) {
this(lookupTableName,
LookupValue.CustomValues.REJECT,
LookupValue.Display.NAME,
LookupValue.Display.NAME,
LookupValue.Export.CODE);
}
/**
* Provides access to the underlying {@link LookupTable}.
*
* @return the lookup table responsible for this field
*/
public LookupTable getTable() {
if (table == null) {
table = lookupTables.fetchTable(lookupTableName);
}
return table;
}
/**
* Resolves a string to present to the user for each value according to {@link #display}.
*
* @return a list of tuples containing the value codes, along with their display strings
* @see LookupValue.Display#resolveDisplayString()
*/
public List<Tuple<String, String>> resolveDisplayStrings() {
return data().stream()
.map(code -> Tuple.create(code, display.resolveDisplayString(getTable(), code)))
.toList();
}
/**
* Provides access to {@link #customValues} as a simple boolean.
*
* @return true if the field {@link sirius.biz.codelists.LookupValue.CustomValues#ACCEPT accepts} custom values, false otherwise
*/
public boolean acceptsCustomValues() {
return customValues == LookupValue.CustomValues.ACCEPT;
}
public LookupValue.Display getDisplay() {
return display;
}
public LookupValue.Display getExtendedDisplay() {
return extendedDisplay;
}
public LookupValue.Export getExport() {
return export;
}
public LookupValue.CustomValues getCustomValues() {
return customValues;
}
public String getTableName() {
return lookupTableName;
}
}