ref #7362 Refactor json parser + add term request
authorPatrick Plitzner <p.plitzner@bgbm.org>
Thu, 31 May 2018 11:51:38 +0000 (13:51 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Thu, 31 May 2018 11:51:38 +0000 (13:51 +0200)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/GfBioTerminologyImportPresenter.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/parser/HierarchyParser.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/parser/OntologyTermParser.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/parser/ParserUtil.java [new file with mode: 0644]
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/parser/SuggestParser.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/parser/TermParser.java [new file with mode: 0644]
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/parser/TerminologyParser.java
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/requests/RequestTerm.java [new file with mode: 0644]

index 99b1ab28720de78547f6e153b02ef04250c11b33..1e02f03b9e42001eed8474ddca8cd5b147b09b0c 100644 (file)
@@ -35,7 +35,7 @@ import eu.etaxonomy.taxeditor.l10n.Messages;
 import eu.etaxonomy.taxeditor.model.ColorResources;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.preference.Resources;
-import eu.etaxonomy.taxeditor.view.webimport.termimport.parser.SuggestParser;
+import eu.etaxonomy.taxeditor.view.webimport.termimport.parser.TermParser;
 import eu.etaxonomy.taxeditor.view.webimport.termimport.parser.TerminologyParser;
 import eu.etaxonomy.taxeditor.view.webimport.termimport.requests.RequestSearch;
 import eu.etaxonomy.taxeditor.view.webimport.termimport.requests.RequestSuggest;
@@ -175,19 +175,21 @@ public class GfBioTerminologyImportPresenter {
         if(searchString.equals(TXT_SEARCH_DEFAULT)){
             return;
         }
+        String terminology = "";
         ISelection selection = composite.getComboTerminologies().getSelection();
         Collection<OntologyTermWrapper> wrapperList = new ArrayList<>();
         if(selection instanceof IStructuredSelection && !selection.isEmpty()){
-            //TODO maybe add support for mutliple terminology selection
+            //TODO maybe add support for multiple terminology selection
             Object next = ((IStructuredSelection) selection).iterator().next();
             if(next instanceof TerminologyWrapper){
-                String response = new RequestSearch(searchString, ((TerminologyWrapper) next).getAcronym()).request();
-                if(response==null){
-                    MessagingUtils.informationDialog(NO_CONNECTION_TITLE, NO_CONNECTION_MESSAGE);
-                    return;
-                }
-                wrapperList = SuggestParser.parse(response);
+                terminology =((TerminologyWrapper) next).getAcronym();
+            }
+            String response = new RequestSearch(searchString, terminology).request();
+            if(response==null){
+                MessagingUtils.informationDialog(NO_CONNECTION_TITLE, NO_CONNECTION_MESSAGE);
+                return;
             }
+            wrapperList = TermParser.parse(response);
         }
         else{
             String response = new RequestSuggest(searchString).request();
@@ -195,7 +197,7 @@ public class GfBioTerminologyImportPresenter {
                 MessagingUtils.informationDialog(NO_CONNECTION_TITLE, NO_CONNECTION_MESSAGE);
                 return;
             }
-            wrapperList = SuggestParser.parse(response);
+            wrapperList = TermParser.parse(response);
         }
         if(wrapperList.isEmpty()){
             MessagingUtils.informationDialog(Messages.GfBioTerminologyImportPresenter_NO_RESULTS_TITLE, Messages.GfBioTerminologyImportPresenter_NO_RESULTS_MESSAGE);
index edff1bcf220d0bc9ef115758bb04bd8e199c037a..11df63631e451d8ede6b4e26cba809d6fdd07e89 100644 (file)
@@ -27,12 +27,12 @@ public class HierarchyParser {
         LinkedList<HierarchyTermWrapper> wrapperList = new LinkedList<>();
         try {
             JSONObject jsonResponse = new JSONObject(response);
-            JSONArray responseArray = jsonResponse.getJSONArray("results");
+            JSONArray responseArray = ParserUtil.parseResults(jsonResponse);
             HierarchyTermWrapper childTerm = null;
             for(int i=0;i<responseArray.length();i++){
-                JSONObject terminology = responseArray.getJSONObject(i);
-                String label = terminology.getString("label");
-                String uri = terminology.getString("uri");
+                JSONObject jsonObject = responseArray.getJSONObject(i);
+                String label = ParserUtil.parseLabel(jsonObject);
+                String uri= ParserUtil.parseUri(jsonObject);
                 HierarchyTermWrapper hierarchyTermWrapper = new HierarchyTermWrapper(uri, label);
                 wrapperList.add(hierarchyTermWrapper);
                 if(childTerm!=null){
index 7bd89196f7180716b4c98abb8476983926876682..c9f5fbe56bbcf85f4d9b15ab2668a73e8d61ce54 100644 (file)
@@ -28,11 +28,11 @@ public class OntologyTermParser {
         List<OntologyTermWrapper> wrapperList = new ArrayList<>();
         try {
             JSONObject jsonResponse = new JSONObject(response);
-            JSONArray responseArray = jsonResponse.getJSONArray("results");
+            JSONArray responseArray = ParserUtil.parseResults(jsonResponse);
             for(int i=0;i<responseArray.length();i++){
                 JSONObject jsonObject = responseArray.getJSONObject(i);
-                String label = jsonObject.getString("label");
-                String uriString = jsonObject.getString("uri");
+                String label = ParserUtil.parseLabel(jsonObject);
+                String uriString = ParserUtil.parseUri(jsonObject);
                 wrapperList.add(new OntologyTermWrapper(uriString, label, terminology));
             }
         } catch (JSONException e) {
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/parser/ParserUtil.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/parser/ParserUtil.java
new file mode 100644 (file)
index 0000000..a635426
--- /dev/null
@@ -0,0 +1,41 @@
+/**
+* Copyright (C) 2018 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.taxeditor.view.webimport.termimport.parser;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+/**
+ * @author pplitzner
+ * @since May 31, 2018
+ *
+ */
+public class ParserUtil {
+
+    public static JSONArray parseResults(JSONObject jsonObject) throws JSONException{
+        return jsonObject.getJSONArray("results");
+    }
+
+    public static String parseUri(JSONObject jsonObject) throws JSONException{
+        return jsonObject.getString("uri");
+    }
+
+    public static String parseLabel(JSONObject jsonObject) throws JSONException{
+        return jsonObject.getString("label");
+    }
+
+    public static String parseDescription(JSONObject jsonObject) throws JSONException{
+        return jsonObject.getString("description");
+    }
+
+    public static String parseTerminology(JSONObject jsonObject) throws JSONException {
+        return jsonObject.getString("sourceTerminology");
+    }
+}
index f985e90811387040d291f6c0389a519f20a87bb5..a968a85695162d14ff423dbea3d5678053f0338d 100644 (file)
@@ -16,6 +16,7 @@ import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 
+import eu.etaxonomy.taxeditor.view.webimport.termimport.requests.RequestTerm;
 import eu.etaxonomy.taxeditor.view.webimport.termimport.wrapper.OntologyTermWrapper;
 
 /**
@@ -28,13 +29,18 @@ public class SuggestParser {
         List<OntologyTermWrapper> wrapperList = new ArrayList<>();
         try {
             JSONObject jsonResponse = new JSONObject(response);
-            JSONArray responseArray = jsonResponse.getJSONArray("results");
+            JSONArray responseArray = ParserUtil.parseResults(jsonResponse);
             for(int i=0;i<responseArray.length();i++){
-                JSONObject terminology = responseArray.getJSONObject(i);
-                String label = terminology.getString("label");
-                String uri = terminology.getString("uri");
-                String sourceTerminology = terminology.getString("sourceTerminology");
-                wrapperList.add(new OntologyTermWrapper(uri, label, sourceTerminology));
+                JSONObject jsonObject = responseArray.getJSONObject(i);
+                String label = ParserUtil.parseLabel(jsonObject);
+                String uri= ParserUtil.parseUri(jsonObject);
+                String sourceTerminology = ParserUtil.parseTerminology(jsonObject);
+                String request = new RequestTerm(uri, sourceTerminology).request();
+                //load term description
+                String description = TermParser.parseSingleTerm(request).getDescription();
+                OntologyTermWrapper wrapper = new OntologyTermWrapper(uri, label, sourceTerminology);
+                wrapper.setDescription(description);
+                wrapperList.add(wrapper);
             }
         } catch (JSONException jsonException) {
             jsonException.printStackTrace();
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/parser/TermParser.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/parser/TermParser.java
new file mode 100644 (file)
index 0000000..a400871
--- /dev/null
@@ -0,0 +1,66 @@
+/**
+* Copyright (C) 2018 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.taxeditor.view.webimport.termimport.parser;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import eu.etaxonomy.taxeditor.view.webimport.termimport.wrapper.OntologyTermWrapper;
+
+/**
+ * @author pplitzner
+ * @since May 9, 2018
+ *
+ */
+public class TermParser {
+    public static OntologyTermWrapper parseSingleTerm(String response){
+        try {
+            JSONObject jsonResponse = new JSONObject(response);
+            JSONArray responseArray = ParserUtil.parseResults(jsonResponse);
+            for(int i=0;i<responseArray.length();i++){
+                JSONObject jsonObject = responseArray.getJSONObject(i);
+                return createWrapper(jsonObject);
+            }
+        } catch (JSONException e) {
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+    public static Collection<OntologyTermWrapper> parse(String response){
+        List<OntologyTermWrapper> wrapperList = new ArrayList<>();
+        try {
+            JSONObject jsonResponse = new JSONObject(response);
+            JSONArray responseArray = ParserUtil.parseResults(jsonResponse);
+            for(int i=0;i<responseArray.length();i++){
+                JSONObject jsonObject = responseArray.getJSONObject(i);
+                wrapperList.add(createWrapper(jsonObject));
+            }
+        } catch (JSONException e) {
+            e.printStackTrace();
+        }
+        return wrapperList;
+    }
+
+    private static OntologyTermWrapper createWrapper(JSONObject jsonObject) throws JSONException{
+        String description = ParserUtil.parseDescription(jsonObject);
+        String label = ParserUtil.parseLabel(jsonObject);
+        String uri= ParserUtil.parseUri(jsonObject);
+        String terminology= ParserUtil.parseTerminology(jsonObject);
+        OntologyTermWrapper wrapper = new OntologyTermWrapper(uri, label, terminology);
+        wrapper.setDescription(description);
+        return wrapper;
+    }
+
+}
index fefb6fd45f64adeaa580894afe943b51744ed7b3..77a8b7d3389e8de6027bc4ad03103710a40870d8 100644 (file)
@@ -30,12 +30,12 @@ public class TerminologyParser {
             JSONObject jsonResponse = new JSONObject(response);
             JSONArray responseArray = jsonResponse.getJSONArray("results");
             for(int i=0;i<responseArray.length();i++){
-                JSONObject terminology = responseArray.getJSONObject(i);
-                String name = terminology.getString("name");
-                String acronym = terminology.getString("acronym");
-                String description = terminology.getString("description");
-                String uriString = terminology.getString("uri");
-                wrapperList.add(new TerminologyWrapper(name, acronym, description, uriString));
+                JSONObject jsonObject = responseArray.getJSONObject(i);
+                String name = jsonObject.getString("name");
+                String acronym = jsonObject.getString("acronym");
+                String description = ParserUtil.parseDescription(jsonObject);
+                String uri = ParserUtil.parseUri(jsonObject);
+                wrapperList.add(new TerminologyWrapper(name, acronym, description, uri));
             }
         } catch (JSONException e) {
             e.printStackTrace();
diff --git a/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/requests/RequestTerm.java b/eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/requests/RequestTerm.java
new file mode 100644 (file)
index 0000000..2a528ad
--- /dev/null
@@ -0,0 +1,47 @@
+/**
+* Copyright (C) 2018 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+package eu.etaxonomy.taxeditor.view.webimport.termimport.requests;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author pplitzner
+ * @since Apr 23, 2018
+ *
+ */
+public class RequestTerm extends AbstractTerminologyServiceRequest{
+
+    private String termUri;
+    private String ontologyLabel;
+
+    public RequestTerm(String termUri, String ontologyLabel) {
+        super();
+        this.termUri = termUri;
+        this.ontologyLabel = ontologyLabel;
+    }
+
+    @Override
+    protected String getServiceUri() {
+        return "term";
+    }
+
+    @Override
+    protected String getTerminologyId() {
+        return ontologyLabel;
+    }
+
+    @Override
+    protected List<RequestParameter> getServiceParameters() {
+        List<RequestParameter> params = new ArrayList<>();
+        params.add(new RequestParameter(TerminologyServiceConstants.ATTR_URI, termUri));
+        return params;
+    }
+
+}