Project

General

Profile

Revision b1d7bf10

IDb1d7bf106124f9206ca6623b969a2f8ac4432540
Parent b49b7f6b
Child 7fcb1960

Added by Patrick Plitzner over 3 years ago

ref #7362 Refactor json parser + add term request

View differences:

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/GfBioTerminologyImportPresenter.java
35 35
import eu.etaxonomy.taxeditor.model.ColorResources;
36 36
import eu.etaxonomy.taxeditor.model.MessagingUtils;
37 37
import eu.etaxonomy.taxeditor.preference.Resources;
38
import eu.etaxonomy.taxeditor.view.webimport.termimport.parser.SuggestParser;
38
import eu.etaxonomy.taxeditor.view.webimport.termimport.parser.TermParser;
39 39
import eu.etaxonomy.taxeditor.view.webimport.termimport.parser.TerminologyParser;
40 40
import eu.etaxonomy.taxeditor.view.webimport.termimport.requests.RequestSearch;
41 41
import eu.etaxonomy.taxeditor.view.webimport.termimport.requests.RequestSuggest;
......
175 175
        if(searchString.equals(TXT_SEARCH_DEFAULT)){
176 176
            return;
177 177
        }
178
        String terminology = "";
178 179
        ISelection selection = composite.getComboTerminologies().getSelection();
179 180
        Collection<OntologyTermWrapper> wrapperList = new ArrayList<>();
180 181
        if(selection instanceof IStructuredSelection && !selection.isEmpty()){
181
            //TODO maybe add support for mutliple terminology selection
182
            //TODO maybe add support for multiple terminology selection
182 183
            Object next = ((IStructuredSelection) selection).iterator().next();
183 184
            if(next instanceof TerminologyWrapper){
184
                String response = new RequestSearch(searchString, ((TerminologyWrapper) next).getAcronym()).request();
185
                if(response==null){
186
                    MessagingUtils.informationDialog(NO_CONNECTION_TITLE, NO_CONNECTION_MESSAGE);
187
                    return;
188
                }
189
                wrapperList = SuggestParser.parse(response);
185
                terminology =((TerminologyWrapper) next).getAcronym();
186
            }
187
            String response = new RequestSearch(searchString, terminology).request();
188
            if(response==null){
189
                MessagingUtils.informationDialog(NO_CONNECTION_TITLE, NO_CONNECTION_MESSAGE);
190
                return;
190 191
            }
192
            wrapperList = TermParser.parse(response);
191 193
        }
192 194
        else{
193 195
            String response = new RequestSuggest(searchString).request();
......
195 197
                MessagingUtils.informationDialog(NO_CONNECTION_TITLE, NO_CONNECTION_MESSAGE);
196 198
                return;
197 199
            }
198
            wrapperList = SuggestParser.parse(response);
200
            wrapperList = TermParser.parse(response);
199 201
        }
200 202
        if(wrapperList.isEmpty()){
201 203
            MessagingUtils.informationDialog(Messages.GfBioTerminologyImportPresenter_NO_RESULTS_TITLE, Messages.GfBioTerminologyImportPresenter_NO_RESULTS_MESSAGE);
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/parser/HierarchyParser.java
27 27
        LinkedList<HierarchyTermWrapper> wrapperList = new LinkedList<>();
28 28
        try {
29 29
            JSONObject jsonResponse = new JSONObject(response);
30
            JSONArray responseArray = jsonResponse.getJSONArray("results");
30
            JSONArray responseArray = ParserUtil.parseResults(jsonResponse);
31 31
            HierarchyTermWrapper childTerm = null;
32 32
            for(int i=0;i<responseArray.length();i++){
33
                JSONObject terminology = responseArray.getJSONObject(i);
34
                String label = terminology.getString("label");
35
                String uri = terminology.getString("uri");
33
                JSONObject jsonObject = responseArray.getJSONObject(i);
34
                String label = ParserUtil.parseLabel(jsonObject);
35
                String uri= ParserUtil.parseUri(jsonObject);
36 36
                HierarchyTermWrapper hierarchyTermWrapper = new HierarchyTermWrapper(uri, label);
37 37
                wrapperList.add(hierarchyTermWrapper);
38 38
                if(childTerm!=null){
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/parser/OntologyTermParser.java
28 28
        List<OntologyTermWrapper> wrapperList = new ArrayList<>();
29 29
        try {
30 30
            JSONObject jsonResponse = new JSONObject(response);
31
            JSONArray responseArray = jsonResponse.getJSONArray("results");
31
            JSONArray responseArray = ParserUtil.parseResults(jsonResponse);
32 32
            for(int i=0;i<responseArray.length();i++){
33 33
                JSONObject jsonObject = responseArray.getJSONObject(i);
34
                String label = jsonObject.getString("label");
35
                String uriString = jsonObject.getString("uri");
34
                String label = ParserUtil.parseLabel(jsonObject);
35
                String uriString = ParserUtil.parseUri(jsonObject);
36 36
                wrapperList.add(new OntologyTermWrapper(uriString, label, terminology));
37 37
            }
38 38
        } catch (JSONException e) {
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 org.json.JSONArray;
12
import org.json.JSONException;
13
import org.json.JSONObject;
14

  
15
/**
16
 * @author pplitzner
17
 * @since May 31, 2018
18
 *
19
 */
20
public class ParserUtil {
21

  
22
    public static JSONArray parseResults(JSONObject jsonObject) throws JSONException{
23
        return jsonObject.getJSONArray("results");
24
    }
25

  
26
    public static String parseUri(JSONObject jsonObject) throws JSONException{
27
        return jsonObject.getString("uri");
28
    }
29

  
30
    public static String parseLabel(JSONObject jsonObject) throws JSONException{
31
        return jsonObject.getString("label");
32
    }
33

  
34
    public static String parseDescription(JSONObject jsonObject) throws JSONException{
35
        return jsonObject.getString("description");
36
    }
37

  
38
    public static String parseTerminology(JSONObject jsonObject) throws JSONException {
39
        return jsonObject.getString("sourceTerminology");
40
    }
41
}
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/parser/SuggestParser.java
16 16
import org.json.JSONException;
17 17
import org.json.JSONObject;
18 18

  
19
import eu.etaxonomy.taxeditor.view.webimport.termimport.requests.RequestTerm;
19 20
import eu.etaxonomy.taxeditor.view.webimport.termimport.wrapper.OntologyTermWrapper;
20 21

  
21 22
/**
......
28 29
        List<OntologyTermWrapper> wrapperList = new ArrayList<>();
29 30
        try {
30 31
            JSONObject jsonResponse = new JSONObject(response);
31
            JSONArray responseArray = jsonResponse.getJSONArray("results");
32
            JSONArray responseArray = ParserUtil.parseResults(jsonResponse);
32 33
            for(int i=0;i<responseArray.length();i++){
33
                JSONObject terminology = responseArray.getJSONObject(i);
34
                String label = terminology.getString("label");
35
                String uri = terminology.getString("uri");
36
                String sourceTerminology = terminology.getString("sourceTerminology");
37
                wrapperList.add(new OntologyTermWrapper(uri, label, sourceTerminology));
34
                JSONObject jsonObject = responseArray.getJSONObject(i);
35
                String label = ParserUtil.parseLabel(jsonObject);
36
                String uri= ParserUtil.parseUri(jsonObject);
37
                String sourceTerminology = ParserUtil.parseTerminology(jsonObject);
38
                String request = new RequestTerm(uri, sourceTerminology).request();
39
                //load term description
40
                String description = TermParser.parseSingleTerm(request).getDescription();
41
                OntologyTermWrapper wrapper = new OntologyTermWrapper(uri, label, sourceTerminology);
42
                wrapper.setDescription(description);
43
                wrapperList.add(wrapper);
38 44
            }
39 45
        } catch (JSONException jsonException) {
40 46
            jsonException.printStackTrace();
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/parser/TermParser.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.Collection;
13
import java.util.List;
14

  
15
import org.json.JSONArray;
16
import org.json.JSONException;
17
import org.json.JSONObject;
18

  
19
import eu.etaxonomy.taxeditor.view.webimport.termimport.wrapper.OntologyTermWrapper;
20

  
21
/**
22
 * @author pplitzner
23
 * @since May 9, 2018
24
 *
25
 */
26
public class TermParser {
27
    public static OntologyTermWrapper parseSingleTerm(String response){
28
        try {
29
            JSONObject jsonResponse = new JSONObject(response);
30
            JSONArray responseArray = ParserUtil.parseResults(jsonResponse);
31
            for(int i=0;i<responseArray.length();i++){
32
                JSONObject jsonObject = responseArray.getJSONObject(i);
33
                return createWrapper(jsonObject);
34
            }
35
        } catch (JSONException e) {
36
            e.printStackTrace();
37
        }
38
        return null;
39
    }
40

  
41
    public static Collection<OntologyTermWrapper> parse(String response){
42
        List<OntologyTermWrapper> wrapperList = new ArrayList<>();
43
        try {
44
            JSONObject jsonResponse = new JSONObject(response);
45
            JSONArray responseArray = ParserUtil.parseResults(jsonResponse);
46
            for(int i=0;i<responseArray.length();i++){
47
                JSONObject jsonObject = responseArray.getJSONObject(i);
48
                wrapperList.add(createWrapper(jsonObject));
49
            }
50
        } catch (JSONException e) {
51
            e.printStackTrace();
52
        }
53
        return wrapperList;
54
    }
55

  
56
    private static OntologyTermWrapper createWrapper(JSONObject jsonObject) throws JSONException{
57
        String description = ParserUtil.parseDescription(jsonObject);
58
        String label = ParserUtil.parseLabel(jsonObject);
59
        String uri= ParserUtil.parseUri(jsonObject);
60
        String terminology= ParserUtil.parseTerminology(jsonObject);
61
        OntologyTermWrapper wrapper = new OntologyTermWrapper(uri, label, terminology);
62
        wrapper.setDescription(description);
63
        return wrapper;
64
    }
65

  
66
}
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/parser/TerminologyParser.java
30 30
            JSONObject jsonResponse = new JSONObject(response);
31 31
            JSONArray responseArray = jsonResponse.getJSONArray("results");
32 32
            for(int i=0;i<responseArray.length();i++){
33
                JSONObject terminology = responseArray.getJSONObject(i);
34
                String name = terminology.getString("name");
35
                String acronym = terminology.getString("acronym");
36
                String description = terminology.getString("description");
37
                String uriString = terminology.getString("uri");
38
                wrapperList.add(new TerminologyWrapper(name, acronym, description, uriString));
33
                JSONObject jsonObject = responseArray.getJSONObject(i);
34
                String name = jsonObject.getString("name");
35
                String acronym = jsonObject.getString("acronym");
36
                String description = ParserUtil.parseDescription(jsonObject);
37
                String uri = ParserUtil.parseUri(jsonObject);
38
                wrapperList.add(new TerminologyWrapper(name, acronym, description, uri));
39 39
            }
40 40
        } catch (JSONException e) {
41 41
            e.printStackTrace();
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/view/webimport/termimport/requests/RequestTerm.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.requests;
10

  
11
import java.util.ArrayList;
12
import java.util.List;
13

  
14
/**
15
 * @author pplitzner
16
 * @since Apr 23, 2018
17
 *
18
 */
19
public class RequestTerm extends AbstractTerminologyServiceRequest{
20

  
21
    private String termUri;
22
    private String ontologyLabel;
23

  
24
    public RequestTerm(String termUri, String ontologyLabel) {
25
        super();
26
        this.termUri = termUri;
27
        this.ontologyLabel = ontologyLabel;
28
    }
29

  
30
    @Override
31
    protected String getServiceUri() {
32
        return "term";
33
    }
34

  
35
    @Override
36
    protected String getTerminologyId() {
37
        return ontologyLabel;
38
    }
39

  
40
    @Override
41
    protected List<RequestParameter> getServiceParameters() {
42
        List<RequestParameter> params = new ArrayList<>();
43
        params.add(new RequestParameter(TerminologyServiceConstants.ATTR_URI, termUri));
44
        return params;
45
    }
46

  
47
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)