-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLookupTableEntry.java
104 lines (88 loc) · 3.12 KB
/
LookupTableEntry.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
/*
* 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 com.fasterxml.jackson.databind.node.ObjectNode;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import sirius.kernel.commons.Json;
import sirius.kernel.commons.Limit;
import sirius.kernel.commons.Strings;
import sirius.web.controller.AutocompleteHelper;
import javax.annotation.Nullable;
/**
* Represents an entry within a {@link LookupTable}.
* <p>
* This is mainly used by {@link LookupTable#suggest(String)}, {@link LookupTable#search(String, Limit)} and
* {@link LookupTable#scan(String, Limit)}.
*/
public class LookupTableEntry {
private final String code;
private final String name;
private final String description;
private boolean deprecated;
private String source;
/**
* Creates a new entry.
*
* @param code the code of the entry
* @param name the name of the entry
* @param description a description of the entry
*/
public LookupTableEntry(String code, String name, String description) {
this.code = code;
this.name = Strings.isEmpty(name) ? null : name;
this.description = Strings.isEmpty(description) ? null : description;
}
protected LookupTableEntry markDeprecated() {
this.deprecated = true;
return this;
}
protected LookupTableEntry withSource(ObjectNode source) {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setPrettyFlow(true);
Yaml yaml = new Yaml(options);
this.source = yaml.dump(Json.convertToMap(source));
return this;
}
public String getCode() {
return code;
}
@Nullable
public String getName() {
return name;
}
@Nullable
public String getDescription() {
return description;
}
public boolean isDeprecated() {
return deprecated;
}
public String getSource() {
return source;
}
@Override
public String toString() {
return code + " (" + name + ")";
}
/**
* Transforms this entry into a completion to be supplied to a {@link AutocompleteHelper}.
*
* @param fieldDisplay the {@link sirius.biz.codelists.LookupValue.Display display mode} for the field label
* @param completionDisplay the {@link sirius.biz.codelists.LookupValue.Display display mode} for the completion label
* @return the completion which represents this entry
*/
public AutocompleteHelper.Completion toAutocompletion(LookupValue.Display fieldDisplay,
LookupValue.Display completionDisplay) {
return AutocompleteHelper.suggest(code)
.withFieldLabel(fieldDisplay.makeDisplayString(this))
.withCompletionLabel(completionDisplay.makeDisplayString(this))
.withCompletionDescription(description);
}
}