1 | package de.uni_leipzig.asv.clarin.webservices.pidservices2; |
---|
2 | |
---|
3 | import java.util.HashMap; |
---|
4 | import java.util.Iterator; |
---|
5 | import java.util.Map; |
---|
6 | |
---|
7 | import net.sf.json.JSONArray; |
---|
8 | |
---|
9 | import com.jayway.jsonpath.JsonPath; |
---|
10 | import com.jayway.jsonpath.PathNotFoundException; |
---|
11 | |
---|
12 | /** |
---|
13 | * Stores most(!) relevant information of a PID JSON object retrieved from the GWDG |
---|
14 | * |
---|
15 | * @author Thomas Eckart |
---|
16 | */ |
---|
17 | public class PidObject { |
---|
18 | private final String handleIdentifier; |
---|
19 | private final Map<HandleField, String> fieldMap; |
---|
20 | |
---|
21 | public PidObject(String pid, JSONArray pidJsonArray) { |
---|
22 | this.handleIdentifier = pid; |
---|
23 | |
---|
24 | fieldMap = new HashMap<HandleField, String>(); |
---|
25 | String jsonPath; |
---|
26 | for (HandleField fieldName : HandleField.values()) { |
---|
27 | try { |
---|
28 | jsonPath = "$..[?(@.type=='" + fieldName + "')].parsed_data[0]"; |
---|
29 | fieldMap.put(fieldName, JsonPath.read(pidJsonArray, jsonPath).toString()); |
---|
30 | } catch (PathNotFoundException pnfe) { |
---|
31 | fieldMap.put(fieldName, null); |
---|
32 | } |
---|
33 | } |
---|
34 | } |
---|
35 | |
---|
36 | /** |
---|
37 | * Returns handle identifier |
---|
38 | * |
---|
39 | * @return handle identifier |
---|
40 | */ |
---|
41 | public String getHandleIdentifier() { |
---|
42 | return handleIdentifier; |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * Returns stored value in EPIC handle for a specific field |
---|
47 | * |
---|
48 | * @param field |
---|
49 | * name of the stored field |
---|
50 | * @return value of the stored field ('parsed_data'), may be null |
---|
51 | */ |
---|
52 | public String getValue(HandleField fieldName) { |
---|
53 | return fieldMap.get(fieldName); |
---|
54 | } |
---|
55 | |
---|
56 | @Override |
---|
57 | public String toString() { |
---|
58 | StringBuilder sb = new StringBuilder(handleIdentifier); |
---|
59 | sb.append(" (Values: "); |
---|
60 | Iterator<HandleField> fieldIterator = fieldMap.keySet().iterator(); |
---|
61 | while (fieldIterator.hasNext()) { |
---|
62 | HandleField field = fieldIterator.next(); |
---|
63 | sb.append(" " + field + "=\"" + fieldMap.get(field) + "\""); |
---|
64 | } |
---|
65 | sb.append(")"); |
---|
66 | return sb.toString(); |
---|
67 | } |
---|
68 | } |
---|