Project

General

Profile

« Previous | Next » 

Revision 5363

Added by berkley over 14 years ago

implementing getObjects in CrudService.

View differences:

src/edu/ucsb/nceas/metacat/MetacatHandler.java
910 910
     * 
911 911
     * @return
912 912
     */
913
    public void query(String metacatUrl, PrintWriter out, Hashtable<String, String[]>params, 
913
    public MetacatResultSet query(String metacatUrl, Hashtable<String, String[]>params, 
914 914
            String username, String[] groups, String sessionid)
915 915
      throws Exception
916 916
    {
917
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
918
        PrintWriter out = new PrintWriter(baos);
917 919
        handleQuery(out, params, null, username, groups, sessionid);
920
        out.flush();
921
        baos.flush();
922
        //baos.close(); 
923
        //System.out.println("result from query: " + baos.toString());
924
        MetacatResultSet rs = new MetacatResultSet(baos.toString());
925
        return rs;
918 926
    }
919 927
    
920 928
    /**
src/edu/ucsb/nceas/metacat/DBQuery.java
370 370
            response.setContentType("text/xml");
371 371
        StringBuffer sb = createResultDocument(xmlquery, qspec, out, user, groups, useXMLIndex, 
372 372
          pagesize, pagestart, sessionid);
373
        System.out.println("result from createResultDocument: " + sb.toString());
373
        //System.out.println("result from createResultDocument: " + sb.toString());
374 374
      }//if
375 375
      else
376 376
      {
src/edu/ucsb/nceas/metacat/MetacatResultSet.java
1
/**
2
 * 
3
 */
4
package edu.ucsb.nceas.metacat;
5

  
6
import java.io.*;
7
import java.util.*;
8

  
9
import org.xml.sax.helpers.DefaultHandler;
10
import org.xml.sax.Attributes;
11
import org.xml.sax.SAXException;
12
import javax.xml.parsers.SAXParserFactory;
13
import javax.xml.parsers.SAXParser;
14
import org.xml.sax.InputSource;
15

  
16
/**
17
 * @author berkley
18
 * A class to parse, then encapsulate a metacat result set
19
 */
20
public class MetacatResultSet extends DefaultHandler
21
{    
22
    private Vector<Document> documents;
23
    private Document document;
24
    private String currentElement;
25
    private String paramName;
26
    private boolean inDocument = false;
27
    
28
    public MetacatResultSet(String s)
29
      throws Exception
30
    {
31
        try
32
        {
33
            documents = new Vector();
34
            SAXParserFactory factory = SAXParserFactory.newInstance();
35
            factory.setValidating(false);
36
            SAXParser parser = factory.newSAXParser();
37
            parser.parse(new InputSource(new StringReader(s)), this);
38
        }
39
        catch(Exception e)
40
        {
41
            e.printStackTrace();
42
            throw new Exception("Could not parse the resultset: " + e.getMessage());
43
        }
44
    }
45
    
46
    public void characters(char[] ch, int start, int length) throws SAXException
47
    {
48
        String content = new String(ch, start, length);
49
        //System.out.println("currentElement: " + currentElement);
50
        //System.out.println("content: " + content);
51

  
52
        if(currentElement.equals("document"))
53
        {
54

  
55
        }
56
        else if(currentElement.equals("docid"))
57
        {
58
            document.docid = content;
59
        }
60
        else if(currentElement.equals("docname"))
61
        {
62
            document.docname = content;
63
        }
64
        else if(currentElement.equals("doctype"))
65
        {
66
            document.doctype = content;
67
        }
68
        else if(currentElement.equals("createdate"))
69
        {
70
            document.createdate = content;
71
        }
72
        else if(currentElement.equals("updatedate"))
73
        {
74
            document.updatedate = content;
75
        }
76
        else if(currentElement.equals("param"))
77
        { 
78
            //System.out.println("setting param: " + paramName + " with value " + content);
79
            document.setField(paramName, content);
80
        }
81
    }
82
    
83
    public void startDocument()
84
    {
85
        
86
    }
87
    
88
    public void endDocument()
89
    {
90
        
91
    }
92
    
93
    public void startElement(String uri, String localName, String qName, Attributes attributes)
94
    {
95
        currentElement = qName;
96
        //System.out.println("currentElement: " + currentElement);
97
        //System.out.println("inDocument: " + inDocument);
98
        if(qName.equals("document"))
99
        {
100
            document = new Document();
101
            inDocument = true;
102
        }
103
        
104
        if(qName.equals("param"))
105
        {
106
            paramName = attributes.getValue("name");
107
        }
108
    }
109
    
110
    public void endElement(String uri, String localName, String qName)
111
    {
112
        if(qName.equals("document"))
113
        {
114
            documents.add(document);
115
        }
116
    }
117
    
118
    public List getDocuments()
119
    {
120
        return documents;
121
    }
122
    
123
    public class Document
124
    {
125
        private Hashtable<String, String> fields = new Hashtable();
126
        public String docid;
127
        public String docname;
128
        public String doctype;
129
        public String createdate;
130
        public String updatedate;
131
        
132
        public Document()
133
        {
134
            docid = null;
135
            docname = null;
136
            doctype = null;
137
            createdate = null;
138
            updatedate = null;
139
        }
140
        
141
        public String getField(String name)
142
        {
143
            return fields.get(name);
144
        }
145
        
146
        public void setField(String name, String value)
147
        {
148
            String val = fields.get(name);
149
            if(val != null)
150
            {
151
                val += value;
152
            }
153
            else 
154
            {
155
                val = value;
156
            }
157
            fields.put(name, val);
158
        }
159
        
160
        public String toString()
161
        {
162
            String s = new String();
163
            s = "{docid=" + docid.trim() + ", " +
164
                "docname=" + docname.trim() + ", " +
165
                "doctype=" + doctype.trim() + ", " +
166
                "createdate=" + createdate.trim() + ", " +
167
                "updatedate=" + updatedate.trim();
168
            Enumeration keys = fields.keys();
169
            while(keys.hasMoreElements())
170
            {
171
                s += ", ";
172
                String name = (String)keys.nextElement();
173
                String value = fields.get(name);
174
                s += name.trim() + "=" + value.trim();
175
            }
176
            s += "}";
177
            return s;
178
        }
179
    }
180
    
181
    
182
}
src/edu/ucsb/nceas/metacat/dataone/CrudService.java
71 71
import com.gc.iotools.stream.is.InputStreamFromOutputStream;
72 72

  
73 73
import edu.ucsb.nceas.metacat.AccessionNumberException;
74
import edu.ucsb.nceas.metacat.MetacatResultSet;
75
import edu.ucsb.nceas.metacat.MetacatResultSet.Document;
74 76
import edu.ucsb.nceas.metacat.DocumentImpl;
75 77
import edu.ucsb.nceas.metacat.EventLog;
76 78
import edu.ucsb.nceas.metacat.IdentifierManager;
......
354 356
          params.clear();
355 357
          params.put("returndoctype", new String[] {"http://dataone.org/service/types/SystemMetadata/0.1"});
356 358
          params.put("qformat", new String[] {"xml"});
357
          //params.put("returnvalue", new String[] {"dateSysMetadataModified", "identifier", "objectFormat"});
358
          //params.put("casesensitive", new String[] {"false"});
359
          params.put("returnfield", new String[] {"dateUploaded", "originMemberNode", 
360
                  "identifier", "objectFormat", "dateSysMetadataModified"});
359 361
          params.put("anyfield", new String[] {"%"});
360
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
361
          PrintWriter out = new PrintWriter(baos);
362
          
362 363
          System.out.println("listing objects");
363
          handler.query(metacatUrl, out, params, sessionData.getUserName(), 
364
          MetacatResultSet rs = handler.query(metacatUrl, params, sessionData.getUserName(), 
364 365
                  sessionData.getGroupNames(), sessionData.getId());
365
          baos.close();
366
          System.out.println("result from query: " + baos.toString());
366
          List docs = rs.getDocuments();
367
          System.out.println("docs size: " + docs.size());
368
          for(int i=0; i<docs.size(); i++)
369
          {
370
              Document d = (Document)docs.get(i);
371
              //System.out.println(d.toString());
372
          }
367 373
          
368 374
          //do an squery on metacat to return systemMetadata docs where 
369 375
          //sm.dateSysMetadataModified >= startTime

Also available in: Unified diff