blob: 095dd8947f1f9696d35e64580b5fb2db9cb145df [file] [log] [blame]
Akronc74dee02018-02-07 18:48:30 +01001package de.ids_mannheim.korap.response;
2
3import org.slf4j.Logger;
4import org.slf4j.LoggerFactory;
5
6import com.fasterxml.jackson.databind.ObjectMapper;
7import com.fasterxml.jackson.databind.node.ObjectNode;
8import com.fasterxml.jackson.databind.node.ArrayNode;
9import com.fasterxml.jackson.databind.JsonNode;
10
11import java.util.*;
12
13import org.apache.lucene.index.*;
14
15/**
16 * Class representing a meta field.
17 */
18public class MetaField {
19
20 // Mapper for JSON serialization
21 ObjectMapper mapper = new ObjectMapper();
22
23 public String type = "type:string";
24 public String key;
25 public List<String> values = new ArrayList<>();
Akronc74dee02018-02-07 18:48:30 +010026
27 public MetaField (String key) {
28 this.key = key;
29 };
30
31 /**
32 * Create JsonNode
33 */
34 public JsonNode toJsonNode () {
35 ObjectNode json = mapper.createObjectNode();
36 json.put("@type", "koral:field");
37 json.put("type", this.type);
38 json.put("key", this.key);
39
Akronc74dee02018-02-07 18:48:30 +010040 // Value is numerical
41 if (this.type.equals("type:number")) {
42
43 // Value is a list
44 if (this.values.size() > 1) {
45 ArrayNode list = json.putArray("value");
46
47 Iterator vIter = this.values.iterator();
48 while (vIter.hasNext()) {
49 list.add((int) Integer.parseInt((String) vIter.next()));
50 };
51 }
52
53 // Value is a single
54 else {
55 json.put("value", Integer.parseInt(this.values.get(0)));
56 };
57 }
58
59 // Value is textual
60 else {
61 // Value is a list
62 if (this.values.size() > 1) {
63 ArrayNode list = json.putArray("value");
64
65 Iterator vIter = this.values.iterator();
66 while (vIter.hasNext()) {
67 list.add((String) vIter.next());
68 };
69 }
70
71 // Value is a single
72 else {
73 json.put("value", this.values.get(0));
74 };
75 };
76
77 return (JsonNode) json;
78 };
79};