| Akron | c74dee0 | 2018-02-07 18:48:30 +0100 | [diff] [blame] | 1 | package de.ids_mannheim.korap.response; |
| 2 | |
| 3 | import org.slf4j.Logger; |
| 4 | import org.slf4j.LoggerFactory; |
| 5 | |
| 6 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 8 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 9 | import com.fasterxml.jackson.databind.JsonNode; |
| 10 | |
| 11 | import java.util.*; |
| 12 | |
| 13 | import org.apache.lucene.index.*; |
| 14 | |
| 15 | /** |
| 16 | * Class representing a meta field. |
| 17 | */ |
| 18 | public 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<>(); |
| Akron | c74dee0 | 2018-02-07 18:48:30 +0100 | [diff] [blame] | 26 | |
| 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 | |
| Akron | c74dee0 | 2018-02-07 18:48:30 +0100 | [diff] [blame] | 40 | // 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 | }; |