Project

General

Profile

1
package org.kepler.web.service;
2

    
3
import com.google.gwt.user.client.rpc.*;
4
import com.google.gwt.user.server.rpc.*;
5

    
6
import org.kepler.web.client.*;
7

    
8
import edu.ucsb.nceas.metacat.client.*;
9

    
10
import org.w3c.dom.Document;
11
import org.w3c.dom.Node;
12
import org.w3c.dom.NodeList;
13
import org.apache.xpath.XPathAPI;
14
import javax.xml.parsers.DocumentBuilder;
15
import javax.xml.parsers.DocumentBuilderFactory;
16
import org.xml.sax.InputSource;
17

    
18
import java.io.*;
19
import java.util.*;
20

    
21
public class KeplerServiceServlet extends RemoteServiceServlet implements KeplerService
22
{
23
  private String url = "http://library.kepler-project.org/kepler/metacat";
24
  
25
  /**
26
   * takes in a query string and a sessionid and returns a resultset
27
   */
28
  public MetacatQueryResult[] query(String query, String sessionid)
29
  {
30
    try
31
    { //query metacat
32
      MetacatClient client = (MetacatClient)MetacatFactory.createMetacatConnection(url);
33
      String queryDoc = createQueryDocument(query);
34
      Reader queryResultReader = client.query(new StringReader(queryDoc));
35
      //now we have the result document, parse it and return it as MQR[]
36
      InputSource is = new InputSource(queryResultReader);
37
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
38
      DocumentBuilder builder = factory.newDocumentBuilder();
39
      Document doc = builder.parse(is);
40
      NodeList docs = XPathAPI.selectNodeList(doc, "/resultset/document");
41
      MetacatQueryResult[] mqr = new MetacatQueryResult[docs.getLength()];
42
      for(int i=0; i<docs.getLength(); i++)
43
      {
44
        Node docNode = docs.item(i);
45
        //parse each document result into an MQR
46
        mqr[i] = new MetacatQueryResult();
47
        String docid = XPathAPI.selectSingleNode(docNode, "docid").getFirstChild().getNodeValue();
48
        String name = XPathAPI.selectSingleNode(docNode, "param[@name='/entity/@name']").getFirstChild().getNodeValue();
49
        
50
        mqr[i].setDocid(docid);
51
        mqr[i].setName(name);
52
        mqr[i].setDescription("");
53
      }
54
      
55
      return mqr;
56
    }
57
    catch(Exception e)
58
    {
59
      return null;
60
    }
61
  }
62
  
63
  /**
64
   * takes in credentials and returns a sessionid
65
   */
66
  public String login(String user, String pass)
67
  {
68
    try
69
    {
70
      MetacatClient client = (MetacatClient)MetacatFactory.createMetacatConnection(url);
71
      return client.login("uid=kepler,o=unaffiliated,dc=ecoinformatics,dc=org", "kepler");
72
    }
73
    catch(Exception e)
74
    {
75
      return "error: " + e.getMessage();
76
    }
77
  }
78
  
79
  public String logout()
80
  {
81
    return "logout";
82
  }
83
  
84
  /**
85
   * takes in a docid and a sessionid and returns a document
86
   */
87
  public String read(String docid, String sessionid)
88
  {
89
    return "read";
90
  }
91
  
92
  private String createQueryDocument(String queryString)
93
  {
94
    String query = "<?xml version=\"1.0\"?>" +
95
      "<pathquery version=\"1.2\">" +
96
      "<querytitle>Untitled-Search-1</querytitle>" +
97
      "<returndoctype>entity</returndoctype>" +
98
      "<returndoctype>eml://ecoinformatics.org//eml-2.0.0</returndoctype>" +
99
      "<returndoctype>-//ecoinformatics.org//eml-dataset-2.0.0beta4//EN</returndoctype>" +
100
      "<returndoctype>-//ecoinformatics.org//eml-dataset-2.0.0beta6//EN</returndoctype>" +
101
      "<returndoctype>-//NCEAS//eml-dataset-2.0//EN</returndoctype>" +
102
      "<returndoctype>-//NCEAS//resource//EN</returndoctype>" +
103
      "<returnfield>/entity/@name</returnfield>" +
104
      "<querygroup operator=\"INTERSECT\">" +
105
        "<querygroup operator=\"UNION\">" +
106
            "<queryterm searchmode=\"contains\" casesensitive=\"false\">" +
107
              "<value>" + queryString + "</value>" +
108
            "</queryterm>" +
109
        "</querygroup>" +
110
      "</querygroup>" +
111
      "</pathquery>";
112
    return query;
113
  }
114
}
    (1-1/1)