-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDefaultFields.java
229 lines (187 loc) · 7.22 KB
/
DefaultFields.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
package datawave.webservice.dictionary.data;
import java.io.IOException;
import java.util.List;
import java.util.Map.Entry;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import datawave.webservice.HtmlProvider;
import datawave.webservice.result.TotalResultsAware;
import io.protostuff.Input;
import io.protostuff.Message;
import io.protostuff.Output;
import io.protostuff.Schema;
@XmlRootElement(name = "DefaultFieldsResponse")
@XmlAccessorType(XmlAccessType.NONE)
public class DefaultFields extends FieldsBase<DefaultFields,DefaultDictionaryField,DefaultDescription>
implements TotalResultsAware, Message<DefaultFields>, HtmlProvider {
private static final long serialVersionUID = 1L;
private static final String TITLE = "Field Descriptions", EMPTY = "";
@XmlElementWrapper(name = "Fields")
@XmlElement(name = "Field")
private List<DefaultDictionaryField> fields = Lists.newArrayList();
@XmlElement(name = "TotalResults")
private Long totalResults = 0L;
public DefaultFields() {}
public DefaultFields(Multimap<Entry<String,String>,DefaultDescription> descriptions) {
this.fields = transform(descriptions);
this.totalResults = (long) this.fields.size();
this.setHasResults(this.totalResults > 0);
}
private List<DefaultDictionaryField> transform(Multimap<Entry<String,String>,DefaultDescription> descriptions) {
List<DefaultDictionaryField> list = Lists.newArrayListWithCapacity(descriptions.size());
for (Entry<Entry<String,String>,DefaultDescription> entry : descriptions.entries()) {
list.add(new DefaultDictionaryField(entry.getKey().getKey(), entry.getKey().getValue(), entry.getValue()));
}
return list;
}
public List<DefaultDictionaryField> getFields() {
return fields;
}
public void setFields(List<DefaultDictionaryField> fields) {
this.fields = fields;
}
public void setDescriptions(Multimap<Entry<String,String>,DefaultDescription> descriptions) {
this.fields = transform(descriptions);
}
/*
* (non-Javadoc)
*
* @see datawave.webservice.result.TotalResultsAware#setTotalResults(long)
*/
@Override
public void setTotalResults(long totalResults) {
this.totalResults = totalResults;
}
/*
* (non-Javadoc)
*
* @see datawave.webservice.result.TotalResultsAware#getTotalResults()
*/
@Override
public long getTotalResults() {
return this.totalResults;
}
public static Schema<DefaultFields> getSchema() {
return SCHEMA;
}
@Override
/*
* (non-Javadoc)
*
* @see com.dyuproject.protostuff.Message#cachedSchema()
*/
public Schema<DefaultFields> cachedSchema() {
return SCHEMA;
}
@XmlTransient
private static final Schema<DefaultFields> SCHEMA = new Schema<DefaultFields>() {
public DefaultFields newMessage() {
return new DefaultFields();
}
public Class<DefaultFields> typeClass() {
return DefaultFields.class;
}
public String messageName() {
return DefaultFields.class.getSimpleName();
}
public String messageFullName() {
return DefaultFields.class.getName();
}
public boolean isInitialized(DefaultFields message) {
return true;
}
public void writeTo(Output output, DefaultFields message) throws IOException {
if (message.totalResults != null) {
output.writeUInt64(1, message.totalResults, false);
}
if (message.fields != null) {
for (DictionaryFieldBase fd : message.fields) {
output.writeObject(2, (DefaultDictionaryField) fd, DefaultDictionaryField.getSchema(), true);
}
}
}
public void mergeFrom(Input input, DefaultFields message) throws IOException {
int number;
while ((number = input.readFieldNumber(this)) != 0) {
switch (number) {
case 1:
message.setTotalResults(input.readUInt64());
break;
case 2:
if (message.fields == null) {
message.fields = Lists.newArrayList();
}
message.fields.add(input.mergeObject(null, DefaultDictionaryField.getSchema()));
break;
default:
input.handleUnknownField(number, this);
break;
}
}
}
public String getFieldName(int number) {
switch (number) {
case 1:
return "totalResults";
case 2:
return "descriptions";
default:
return null;
}
}
public int getFieldNumber(String name) {
final Integer number = fieldMap.get(name);
return number == null ? 0 : number;
}
final java.util.HashMap<String,Integer> fieldMap = new java.util.HashMap<>();
{
fieldMap.put("totalResults", 1);
fieldMap.put("descriptions", 2);
}
};
@Override
public String getTitle() {
return TITLE;
}
@Override
public String getPageHeader() {
return getTitle();
}
@Override
public String getHeadContent() {
return EMPTY;
}
@Override
public String getMainContent() {
StringBuilder builder = new StringBuilder();
builder.append("<table>\n");
builder.append("<tr><th>Datatype</th><th>FieldName</th><th>Description</th></tr>");
int x = 0;
for (DefaultDictionaryField field : this.getFields()) {
for (DefaultDescription desc : field.getDescriptions()) {
addDescriptionRow(field, desc, x, builder);
x++;
}
}
builder.append("</table>\n");
return builder.toString();
}
public static void addDescriptionRow(DictionaryFieldBase<?,? extends DescriptionBase> field, DescriptionBase desc, int rowNum, StringBuilder builder) {
// highlight alternating rows
if (rowNum % 2 == 0) {
builder.append("<tr class=\"highlight\">");
} else {
builder.append("<tr>");
}
builder.append("<td>").append(field.getDatatype()).append("</td>");
builder.append("<td>").append(field.getFieldName()).append("</td>");
builder.append("<td>").append(desc.getDescription()).append("</td>");
builder.append("</tr>");
}
}