Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2003 Regents of the University of California.
4
 *
5
 * Author: John Harris
6
 * '$Date: 2006-02-19 21:11:53 -0800 (Sun, 19 Feb 2006) $'
7
 * '$Revision: 2910 $'
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 2 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
 */
23
package edu.ucsb.nceas.metacat.spatial;
24

    
25
import java.io.File;
26
import java.io.FileReader;
27
import java.io.PrintWriter;
28
import java.io.IOException;
29
import java.io.StringReader;
30
import java.io.BufferedReader;
31
import java.io.Reader;
32
import java.io.FileInputStream;
33
import java.io.BufferedInputStream;
34
import java.util.Enumeration;
35
import java.util.Hashtable;
36
import java.util.ResourceBundle;
37
import java.util.Random;
38
import java.util.StringTokenizer;
39
import java.util.Properties;
40
import java.util.PropertyResourceBundle;
41
import java.util.Vector;
42
import java.util.Map;
43

    
44
import edu.ucsb.nceas.metacat.client.Metacat;
45
import edu.ucsb.nceas.metacat.DBConnection;
46
import edu.ucsb.nceas.metacat.client.MetacatFactory;
47
import edu.ucsb.nceas.metacat.client.MetacatAuthException;
48
import edu.ucsb.nceas.metacat.client.MetacatException;
49
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
50
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
51
import edu.ucsb.nceas.utilities.IOUtil;
52

    
53
import java.sql.ResultSet;
54
import java.sql.PreparedStatement;
55

    
56
import org.apache.log4j.Logger;
57
import org.apache.log4j.BasicConfigurator;
58
import org.apache.log4j.PropertyConfigurator;
59

    
60
public class SpatialQueryProcessor {
61

    
62
  private String METACATURL =
63
      "http://nebulous.msi.ucsb.edu:9999/knp/metacat";
64
  private File QUERYFILE = null;
65
  private String MCUSER = "uid=harris,o=NCEAS,dc=ecoinformatics,dc=org";
66
  private String MCPASS = "yeahRight";
67

    
68

    
69

    
70

    
71
  private DBConnection dbconn;
72

    
73
  private static Logger log =
74
      Logger.getLogger(SpatialQueryProcessor.class.getName());
75

    
76
        /** constructor that uses the defaults **/
77
  public SpatialQueryProcessor() {
78
    try {
79
      dbconn = new DBConnection();
80
    } catch(java.sql.SQLException se) {
81
      log.fatal("Error connecting to the database ...");
82
      se.getMessage();
83
    }
84
  }
85

    
86
  /** call this after the processor is done **/
87
  protected void close() {
88
    try {
89
      dbconn.close();
90
    } catch(java.sql.SQLException se) {
91
      log.fatal("Error closing connecting to the database ...");
92
      se.getMessage();
93
    }
94
  }
95

    
96
  public SpatialQueryProcessor(String mcUser, String mcPass,
97
                               String metacatUrl, File queryFile) {
98
    log.debug("constructor called");
99
    this.METACATURL = metacatUrl;
100
    this.MCPASS = mcPass;
101
    this.MCUSER = mcUser;
102
    this.QUERYFILE = queryFile;
103
  }
104

    
105
  public String execute() throws IOException {
106
    FileReader queryFileReader = new FileReader(QUERYFILE);
107
    String res = runQuery((Reader) queryFileReader);
108
     return res;
109
  } protected MetacatSpatialDataset queryDatasetByCartesianBounds(float
110
                                                                  _xmin,
111
                                                                  float
112
                                                                  _ymin,
113
                                                                  float
114
                                                                  _xmax,
115
                                                                  float
116
                                                                  _ymax) {
117

    
118
    MetacatSpatialDataset _data = new MetacatSpatialDataset();
119

    
120
    // get all docid's 
121
    Vector _docs = queryAllDocids();
122

    
123
    // for each, see if they fall w/in bounds
124
    for (int i = 0; i < _docs.size(); i++) {
125
      float _docMaxY =
126
          queryNorthBoundingCoordinate((String) _docs.elementAt(i));
127
      float _docMinY =
128
          querySouthBoundingCoordinate((String) _docs.elementAt(i));
129
      float _docMaxX =
130
          queryEastBoundingCoordinate((String) _docs.elementAt(i));
131
      float _docMinX =
132
          queryWestBoundingCoordinate((String) _docs.elementAt(i));
133

    
134
      // check if inside the bounding box
135
      if (_docMaxX <= _xmax && _docMaxX >= _xmin) {
136
        MetacatSpatialDocument _doc =
137
            new MetacatSpatialDocument((String) _docs.elementAt(i));
138
        _doc.setDocid((String) _docs.elementAt(i));
139
        // set the extents
140
        _data.add(_doc);
141
      }
142
    }
143
    return _data;
144
  }
145

    
146
  /**
147
   * Returns a Vector of all the docids in the xml_nodes tables
148
   */
149
  protected Vector queryAllDocids() {
150
    Vector _docs = new Vector();
151
    PreparedStatement pstmt = null;
152
    ResultSet rs = null;
153
    String query = "select distinct docid from xml_nodes";
154

    
155
    try {
156
      pstmt = dbconn.prepareStatement(query);
157
      pstmt.execute();
158
      rs = pstmt.getResultSet();
159
      while (rs.next()) {
160
        String docid = rs.getString(1);
161
        log.fatal("adding docid: " + docid);
162
        _docs.add(docid);
163
      }
164
      rs.close();
165
      pstmt.close();
166
    }
167
    catch(Exception e) {
168
      log.error("Error getting docids from queryAllDocids");
169
      e.printStackTrace();
170
    }
171
    return _docs;
172
  }
173

    
174

    
175
  /**
176
   * returns the north bounding coordinate -- this is a single point
177
   */
178
  private float queryNorthBoundingCoordinate(String docid) {
179
    float _coord = MetacatSpatialConstants.FLOAT_NULL;
180
    PreparedStatement pstmt = null;
181
    ResultSet rs = null;
182
    String query = "select docid, nodedata, nodeid from xml_nodes where "
183
        + "nodeid =(select  nodeid from  xml_nodes where docid  like '"
184
        + docid.trim() + "' and nodename like 'north%')+1;";
185

    
186
    try {
187
      pstmt = dbconn.prepareStatement(query);
188
      pstmt.execute();
189
      rs = pstmt.getResultSet();
190
      if (rs.next())
191
        _coord = rs.getFloat(2);
192
      rs.close();
193
      pstmt.close();
194
    }
195
    catch(Exception e) {
196
      log.error("Error getting docids from queryExtentsByDocid");
197
      e.printStackTrace();
198
    }
199
    return _coord;
200
  }
201

    
202

    
203
  /**
204
   * returns the east bounding coordinate -- this is a single point
205
   */
206
  private float queryEastBoundingCoordinate(String docid) {
207
    float _coord = MetacatSpatialConstants.FLOAT_NULL;
208
    PreparedStatement pstmt = null;
209
    ResultSet rs = null;
210
    String query = "select docid, nodedata, nodeid from xml_nodes where "
211
        + "nodeid =(select  nodeid from  xml_nodes where docid  like '"
212
        + docid.trim() + "' and nodename like 'east%')+1;";
213

    
214
    try {
215
      pstmt = dbconn.prepareStatement(query);
216
      pstmt.execute();
217
      rs = pstmt.getResultSet();
218
      if (rs.next())
219
        _coord = rs.getFloat(2);
220
      rs.close();
221
      pstmt.close();
222
    }
223
    catch(Exception e) {
224
      log.error("Error getting docids from queryExtentsByDocid");
225
      e.printStackTrace();
226
    }
227
    return _coord;
228
  }
229

    
230

    
231
  /**
232
   * returns the south bounding coordinate -- this is a single point
233
   */
234
  private float querySouthBoundingCoordinate(String docid) {
235
    float _coord = MetacatSpatialConstants.FLOAT_NULL;
236
    PreparedStatement pstmt = null;
237
    ResultSet rs = null;
238
    String query = "select docid, nodedata, nodeid from xml_nodes where "
239
        + "nodeid =(select  nodeid from  xml_nodes where docid  like '"
240
        + docid.trim() + "' and nodename like 'south%')+1;";
241

    
242
    try {
243
      pstmt = dbconn.prepareStatement(query);
244
      pstmt.execute();
245
      rs = pstmt.getResultSet();
246
      if (rs.next())
247
        _coord = rs.getFloat(2);
248
      rs.close();
249
      pstmt.close();
250
    }
251
    catch(Exception e) {
252
      log.error("Error getting docids from queryExtentsByDocid");
253
      e.printStackTrace();
254
    }
255
    return _coord;
256
  }
257

    
258

    
259

    
260
  /**
261
   * returns the west bounding coordinate -- this is a single point
262
   */
263
  private float queryWestBoundingCoordinate(String docid) {
264
    float _coord = MetacatSpatialConstants.FLOAT_NULL;
265
    PreparedStatement pstmt = null;
266
    ResultSet rs = null;
267
    String query = "select docid, nodedata, nodeid from xml_nodes where "
268
        + "nodeid =(select  nodeid from  xml_nodes where docid  like '"
269
        + docid.trim() + "' and nodename like 'west%')+1;";
270

    
271
    try {
272
      pstmt = dbconn.prepareStatement(query);
273
      pstmt.execute();
274
      rs = pstmt.getResultSet();
275
      if (rs.next())
276
        _coord = rs.getFloat(2);
277
      rs.close();
278
      pstmt.close();
279
    }
280
    catch(Exception e) {
281
      log.error("Error getting docids from queryExtentsByDocid");
282
      e.printStackTrace();
283
    }
284
    return _coord;
285
  }
286

    
287

    
288

    
289

    
290
  /**
291
   * Performs a spatial query given the docid as an input
292
   * 
293
   * @param docid -- the docid for which the extents are to be queried
294
   * @returns spatialextents -- the MetcataSpatialExtents object for this query
295
   */
296
  protected MetacatSpatialExtents queryExtentsByDocid(String docid) {
297
    MetacatSpatialExtents _extents = new MetacatSpatialExtents();
298

    
299
    float _northbc = queryNorthBoundingCoordinate(docid);
300
    float _eastbc = queryEastBoundingCoordinate(docid);
301
    float _southbc = querySouthBoundingCoordinate(docid);
302
    float _westbc = queryWestBoundingCoordinate(docid);
303

    
304
    if ((_northbc == _southbc) && (_eastbc == _westbc)) {
305
      _extents.setIsPoint(true);
306
    } else {
307
      _extents.setIsPoint(false);
308
    }
309

    
310
    _extents.setYMin((_southbc < _northbc ? _southbc : _northbc));
311
    _extents.setYMax((_southbc > _northbc ? _southbc : _northbc));
312
    _extents.setXMin((_westbc < _eastbc ? _westbc : _eastbc));
313
    _extents.setXMax((_westbc > _eastbc ? _westbc : _eastbc));
314

    
315
    _extents.setDocid(docid);
316

    
317
    return _extents;
318
  }
319

    
320

    
321
        /**
322
		* Query the Metacat Server to get a list of documents, 
323
		* if it doesn't exist there, from the backend storage server.
324
		* 
325
		* @param query the xml description of the query to run
326
		* @return String the XML document string of the result set
327
		* @throws QueryFailedException when any thing happens to prevent a query
328
		*  returns
329
		*/
330
  private String runQuery(Reader query) {
331
    String document = null;
332

    
333
    Reader xdoc = null;
334
    String error = null;
335
    try {
336
      log.debug("creating metacat instance with: " + METACATURL);
337
      Metacat m = MetacatFactory.createMetacatConnection(METACATURL);
338
      m.login(MCUSER, MCPASS);
339
      log.debug("metacat instance: " + m);
340
      xdoc = m.query(query);
341
      document = IOUtil.getAsString(xdoc, true);
342
      m.logout();
343
      if (document == null) {
344
        throw new QueryFailedException("Document String Was null");
345
      }
346
    }
347
    catch(Exception e) {
348
      e.printStackTrace();
349
    }
350
    return document;
351
  }
352

    
353

    
354
}
(9-9/10)