Project

General

Profile

1 3035 perry
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2000 Regents of the University of California and the
4
 *             National Center for Ecological Analysis and Synthesis
5
 *
6
 * Author: Matthew Perry
7
 * '$Date$'
8
 * '$Revision$'
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
 */
24
25
package edu.ucsb.nceas.metacat.spatial;
26
27 5915 jones
import java.io.IOException;
28
import java.sql.PreparedStatement;
29 3035 perry
import java.sql.ResultSet;
30 5915 jones
import java.sql.SQLException;
31 3035 perry
import java.util.Vector;
32
33
import org.apache.log4j.Logger;
34 5915 jones
import org.opengis.feature.simple.SimpleFeature;
35 3035 perry
36 5015 daigle
import edu.ucsb.nceas.metacat.database.DBConnection;
37 4080 daigle
38 3040 perry
/**
39
 * Harvests spatial data from metacat database
40
 * and saves to persistent cache
41 3035 perry
 */
42
public class SpatialHarvester {
43
44
  private static Logger log = Logger.getLogger(SpatialHarvester.class.getName());
45
46
  private DBConnection dbconn;
47
48 3040 perry
  /**
49
   * constructor to initialize db connection
50
   */
51 3035 perry
  public SpatialHarvester() {
52
      try {
53
          dbconn = new DBConnection();
54
      } catch( Exception e ) {
55
          log.error("Error getting docids from queryAllDocids");
56
          e.printStackTrace();
57
      }
58
  }
59
60 3040 perry
  /**
61
   * Closes the database connection.
62
   * Should be called after you're done with the SpatialHarvester
63 3035 perry
   */
64
  public void destroy() {
65
      try {
66
          dbconn.close();
67 5915 jones
      } catch( SQLException e ) {
68 3040 perry
          log.error("Error closing out dbconn in spatial harvester");
69 3035 perry
          e.printStackTrace();
70
      }
71
  }
72
73
  /**
74
   * Returns a Vector of all the docids in the xml_path_index tables
75
   */
76 5915 jones
  protected Vector<String> queryAllDocids() {
77
    Vector<String> _docs = new Vector<String>();
78 3035 perry
    PreparedStatement pstmt = null;
79
    ResultSet rs = null;
80
81 3043 perry
    /*
82
     * For now, only allow publically readable docids
83
     * to be considered for the spatial cache
84
     */
85
    //String query = "select distinct docid from xml_path_index";
86
    String query = "select distinct docid from xml_access where principal_name = 'public' and perm_type = 'allow'";
87 3035 perry
88
    try {
89
      pstmt = dbconn.prepareStatement(query);
90
      pstmt.execute();
91
      rs = pstmt.getResultSet();
92
      while (rs.next()) {
93
        String docid = rs.getString(1);
94 6305 leinfelder
        if (docid != null) {
95
	        //log.fatal("adding docid: " + docid);
96
	        _docs.add(docid);
97
        }
98 3035 perry
    }
99
      rs.close();
100
      pstmt.close();
101
    }
102
    catch(Exception e) {
103
      log.error("Error getting docids from queryAllDocids");
104
      e.printStackTrace();
105
    }
106
    return _docs;
107
  }
108
109 3040 perry
  /**
110
   * Currently just a wrapper around the harvestDocument method.
111 3035 perry
   * Eventually we can use this method as a
112 3040 perry
   * timed que like the indexing process.
113
   *
114
   * @param docid The docid to be added to the spatial cache.
115 3035 perry
   */
116
  public void addToUpdateQue( String docid ) {
117
      harvestDocument(docid);
118
  }
119
120 3040 perry
  /**
121
   * Delete a given document from spatial cache.
122
   * Just a wrapper around deleteDocument method for now.
123
   *
124
   * @param docid The docid to be deleted from the spatial cache.
125 3035 perry
   */
126
  public void addToDeleteQue( String docid ) {
127
      deleteDocument(docid);
128
  }
129
130 3040 perry
  /**
131
   * Given a docid, will attempt to delete it from spatial cache
132
   *
133
   * @param docid The docid to be deleted from the spatial cache.
134 3035 perry
   */
135
  public void deleteDocument( String docid ) {
136
137 3201 tao
138 3035 perry
139
     try {
140 3201 tao
//    	 Read the existing spatial dataset cache
141
         SpatialDataset sds = new SpatialDataset();
142 3035 perry
         // Delete both the polygon(s) and point(s)
143
         sds.delete( "polygon" , docid );
144
         sds.delete( "point" , docid );
145
     } catch (IOException e) {
146
         log.error("IOException while deleting from spatial cache");
147
     }
148
149
     log.info(" --------- Spatial Harvester - Deleted from spatial cache : " + docid );
150
  }
151
152
153 3040 perry
  /**
154
   * Given a new or updated docid,
155
   * will update the spatial cache accordingly.
156
   *
157
   * @param docid The docid to be updated or inserted into the spatial cache.
158 3035 perry
   */
159
  public void harvestDocument( String docid ) {
160
161 3039 perry
     long before = System.currentTimeMillis();
162 3201 tao
163
     try {
164
         //Read the existing spatial dataset cache
165
         SpatialDataset sds = new SpatialDataset();
166 3035 perry
167 3201 tao
         // insert OR update the spatial cache
168
         // SpatialDataset.insertOrUpdate takes care of the difference
169
         SpatialDocument sdoc = new SpatialDocument( docid, dbconn );
170 3035 perry
171 5829 leinfelder
         SimpleFeature polygonFeature = sdoc.getPolygonFeature();
172 3035 perry
         sds.insertOrUpdate("polygon", polygonFeature, docid );
173
174 5829 leinfelder
         SimpleFeature pointFeature = sdoc.getPointFeature();
175 3035 perry
         sds.insertOrUpdate("point", pointFeature, docid );
176 3039 perry
         long after = System.currentTimeMillis();
177
         log.info(" ------- Spatial Harvester - spatial cache updated for : " + docid + ".... Time  " + (after - before) + "ms");
178 3035 perry
     } catch (IOException e) {
179
         log.error("IOException while performing spatial harvest ");
180
     }
181
  }
182
183 3040 perry
  /**
184
   * Completely regenerates the spatial cache.
185
   * This can take a long time, especially with lots of documents.
186 3035 perry
   */
187 3201 tao
  public void regenerate() throws IOException{
188 3035 perry
189
      // Create new Spatial Dataset
190
      SpatialDataset sds = new SpatialDataset();
191
192
      // Get list of all docids in the database
193 5915 jones
      Vector<String> docids = queryAllDocids();
194 3035 perry
195
      for (int i = 0; i < docids.size(); i++) {
196
          SpatialDocument sdoc = new SpatialDocument( (String)docids.elementAt(i), dbconn );
197
198
          // Get the polygon representation of SpatialDocument
199
          // and add it to the spatial dataset
200 5829 leinfelder
         SimpleFeature polygonFeature = sdoc.getPolygonFeature();
201 3035 perry
          sds.add("polygon", polygonFeature );
202
203
          // Get the point representation of SpatialDocument
204
          // and add it to the spatial dataset
205 5829 leinfelder
          SimpleFeature pointFeature = sdoc.getPointFeature();
206 3035 perry
          sds.add("point", pointFeature );
207
208
          log.info(" ****** Spatial harvest of docid " + docids.elementAt(i) );
209
      }
210
211
      // save SpatialDataset
212
      sds.save();
213
214
  }
215
216
}