ref #7362 Show synonyms for term details
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / webimport / termimport / parser / ParserUtil.java
1 /**
2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.view.webimport.termimport.parser;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import org.json.JSONArray;
15 import org.json.JSONException;
16 import org.json.JSONObject;
17
18 import eu.etaxonomy.taxeditor.view.webimport.termimport.requests.RequestTerm;
19 import eu.etaxonomy.taxeditor.view.webimport.termimport.wrapper.OntologyTermWrapper;
20
21 /**
22 * @author pplitzner
23 * @since May 31, 2018
24 *
25 */
26 public class ParserUtil {
27
28 public static JSONArray parseResults(JSONObject jsonObject) throws JSONException{
29 return jsonObject.getJSONArray("results");
30 }
31
32 public static String parseUri(JSONObject jsonObject) throws JSONException{
33 return jsonObject.getString("uri");
34 }
35
36 public static String parseLabel(JSONObject jsonObject) throws JSONException{
37 return jsonObject.getString("label");
38 }
39
40 public static String parseDescription(JSONObject jsonObject) throws JSONException{
41 String descriptionKey = "description";
42 if(jsonObject.has(descriptionKey)){
43 return jsonObject.getString(descriptionKey);
44 }
45 return null;
46 }
47
48 public static List<String> parseSynonyms(JSONObject jsonObject) throws JSONException{
49 List<String> synonyms = new ArrayList<>();
50 String synomymKey = "synonyms";
51 if(jsonObject.has(synomymKey)){
52 JSONArray jsonArray = jsonObject.getJSONArray(synomymKey);
53 for(int i=0;i<jsonArray.length();i++){
54 synonyms.add(jsonArray.getString(i));
55 }
56 }
57 return synonyms;
58 }
59
60 public static String parseTerminology(JSONObject jsonObject) throws JSONException {
61 return jsonObject.getString("sourceTerminology");
62 }
63
64 /**
65 * Loads and sets detail information for the given wrapper
66 * @param wrapper the wrapper for which the detailed information will be loaded and set
67 */
68 public static void loadDetails(OntologyTermWrapper wrapper) {
69 String request = new RequestTerm(wrapper.getUri(), wrapper.getSourceTerminology()).request();
70 OntologyTermWrapper detailTerm = TermParser.parseSingleTerm(request);
71 wrapper.setDescription(detailTerm.getDescription());
72 wrapper.setSynonyms(detailTerm.getSynonyms());
73 }
74 }