Project

General

Profile

1
/**
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: 2006-08-31 16:54:32 -0700 (Thu, 31 Aug 2006) $'
8
 * '$Revision: 3035 $'
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
import java.sql.ResultSet;
28
import java.sql.PreparedStatement;
29
import java.util.Vector;
30
import java.io.IOException;
31

    
32
import org.apache.log4j.Logger;
33

    
34
import edu.ucsb.nceas.metacat.MetaCatUtil;
35
import edu.ucsb.nceas.metacat.DBConnection;
36
import org.geotools.feature.Feature;
37

    
38
/* 
39
 * Harvests spatial data from metacat and saves to persistent cache
40
 */
41
public class SpatialHarvester {
42
 
43
  private static Logger log = Logger.getLogger(SpatialHarvester.class.getName());
44
 
45
  private DBConnection dbconn;
46
 
47
  /** constructor to initialize db connection **/
48
  public SpatialHarvester() {
49
      try {
50
          dbconn = new DBConnection();
51
      } catch( Exception e ) {
52
          log.error("Error getting docids from queryAllDocids");
53
          e.printStackTrace();
54
      }
55
  }
56
 
57
  /*
58
   * Closes the database connection
59
   * MUST be called after you're done with
60
   * the SpatialHarvester
61
   */
62
  public void destroy() {
63
      try {
64
          dbconn.close();
65
      } catch( Exception e ) {
66
          log.error("Error closing out dbconn");
67
          e.printStackTrace();
68
      }
69
  }
70

    
71
  /**
72
   * Returns a Vector of all the docids in the xml_path_index tables
73
   */
74
  protected Vector queryAllDocids() {
75
    Vector _docs = new Vector();
76
    PreparedStatement pstmt = null;
77
    ResultSet rs = null;
78

    
79
    String query = "select distinct docid from xml_path_index";
80

    
81
    try {
82
      pstmt = dbconn.prepareStatement(query);
83
      pstmt.execute();
84
      rs = pstmt.getResultSet();
85
      while (rs.next()) {
86
        String docid = rs.getString(1);
87
        //log.fatal("adding docid: " + docid);
88
        _docs.add(docid);
89
    }
90
      rs.close();
91
      pstmt.close();
92
    }
93
    catch(Exception e) {
94
      log.error("Error getting docids from queryAllDocids");
95
      e.printStackTrace();
96
    }
97
    return _docs;
98
  }
99

    
100
  /*
101
   * Currently just a wrapper around the harvester
102
   * Eventually we can use this method as a
103
   * timed que like the indexing process
104
   */                   
105
  public void addToUpdateQue( String docid ) {
106
      harvestDocument(docid);
107
  }
108

    
109
  /*
110
   * Delete from spatial cache
111
   * Just a wrapper around delete for now
112
   */
113
  public void addToDeleteQue( String docid ) {
114
      deleteDocument(docid);
115
  }
116

    
117
  /*
118
   * Given a docid, will attempt to delete
119
   * from spatial cache
120
   */
121
  public void deleteDocument( String docid ) {
122

    
123
     // Read the existing spatial dataset cache
124
     SpatialDataset sds = new SpatialDataset();
125

    
126
     try {
127
         // Delete both the polygon(s) and point(s)
128
         sds.delete( "polygon" , docid ); 
129
         sds.delete( "point" , docid ); 
130
     } catch (IOException e) {
131
         log.error("IOException while deleting from spatial cache");
132
     }
133

    
134
     log.info(" --------- Spatial Harvester - Deleted from spatial cache : " + docid );
135
  }      
136

    
137

    
138
  /*
139
   * Given a docid, will update the spatial cache accordingly
140
   */
141
  public void harvestDocument( String docid ) {
142

    
143
     // Read the existing spatial dataset cache
144
     SpatialDataset sds = new SpatialDataset();
145

    
146
     // insert OR update the spatial cache
147
     // SpatialDataset.insertOrUpdate takes care of the difference 
148
     SpatialDocument sdoc = new SpatialDocument( docid, dbconn );
149

    
150
     try {
151
         Feature polygonFeature = sdoc.getPolygonFeature();
152
         sds.insertOrUpdate("polygon", polygonFeature, docid );
153

    
154
         Feature pointFeature = sdoc.getPointFeature();
155
         sds.insertOrUpdate("point", pointFeature, docid );
156
         log.info(" --------- Spatial Harvester - spatial cache updated for : " + docid );
157
     } catch (IOException e) {
158
         log.error("IOException while performing spatial harvest ");
159
     }
160
  }
161

    
162
  /*
163
   * Completely regenerates the spatial cache
164
   */
165
  public void regenerate() {
166
      
167
      // Create new Spatial Dataset
168
      SpatialDataset sds = new SpatialDataset();
169

    
170
      // Get list of all docids in the database
171
      Vector docids = queryAllDocids(); 
172
       
173
      for (int i = 0; i < docids.size(); i++) {
174
          SpatialDocument sdoc = new SpatialDocument( (String)docids.elementAt(i), dbconn );
175

    
176
          // Get the polygon representation of SpatialDocument
177
          // and add it to the spatial dataset
178
          Feature polygonFeature = sdoc.getPolygonFeature();
179
          sds.add("polygon", polygonFeature );
180

    
181
          // Get the point representation of SpatialDocument
182
          // and add it to the spatial dataset
183
          Feature pointFeature = sdoc.getPointFeature();
184
          sds.add("point", pointFeature );
185

    
186
          log.info(" ****** Spatial harvest of docid " + docids.elementAt(i) );
187
      }
188

    
189
      // save SpatialDataset
190
      sds.save();
191

    
192
  }
193

    
194
}
(12-12/16)