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-09-13 12:24:37 -0700 (Wed, 13 Sep 2006) $'
8
 * '$Revision: 3043 $'
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 database
40
 * and saves to persistent cache
41
 */
42
public class SpatialHarvester {
43
 
44
  private static Logger log = Logger.getLogger(SpatialHarvester.class.getName());
45
 
46
  private DBConnection dbconn;
47
 
48
  /** 
49
   * constructor to initialize db connection 
50
   */
51
  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
  /**
61
   * Closes the database connection. 
62
   * Should be called after you're done with the SpatialHarvester
63
   */
64
  public void destroy() {
65
      try {
66
          dbconn.close();
67
      } catch( Exception e ) {
68
          log.error("Error closing out dbconn in spatial harvester");
69
          e.printStackTrace();
70
      }
71
  }
72

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

    
81
    /*
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

    
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
        //log.fatal("adding docid: " + docid);
95
        _docs.add(docid);
96
    }
97
      rs.close();
98
      pstmt.close();
99
    }
100
    catch(Exception e) {
101
      log.error("Error getting docids from queryAllDocids");
102
      e.printStackTrace();
103
    }
104
    return _docs;
105
  }
106

    
107
  /**
108
   * Currently just a wrapper around the harvestDocument method.
109
   * Eventually we can use this method as a
110
   * timed que like the indexing process.
111
   *
112
   * @param docid The docid to be added to the spatial cache.
113
   */                   
114
  public void addToUpdateQue( String docid ) {
115
      harvestDocument(docid);
116
  }
117

    
118
  /**
119
   * Delete a given document from spatial cache.
120
   * Just a wrapper around deleteDocument method for now.
121
   *
122
   * @param docid The docid to be deleted from the spatial cache.
123
   */
124
  public void addToDeleteQue( String docid ) {
125
      deleteDocument(docid);
126
  }
127

    
128
  /**
129
   * Given a docid, will attempt to delete it from spatial cache
130
   *
131
   * @param docid The docid to be deleted from the spatial cache.
132
   */
133
  public void deleteDocument( String docid ) {
134

    
135
     // Read the existing spatial dataset cache
136
     SpatialDataset sds = new SpatialDataset();
137

    
138
     try {
139
         // Delete both the polygon(s) and point(s)
140
         sds.delete( "polygon" , docid ); 
141
         sds.delete( "point" , docid ); 
142
     } catch (IOException e) {
143
         log.error("IOException while deleting from spatial cache");
144
     }
145

    
146
     log.info(" --------- Spatial Harvester - Deleted from spatial cache : " + docid );
147
  }      
148

    
149

    
150
  /**
151
   * Given a new or updated docid, 
152
   * will update the spatial cache accordingly.
153
   *
154
   * @param docid The docid to be updated or inserted into the spatial cache.
155
   */
156
  public void harvestDocument( String docid ) {
157

    
158
     long before = System.currentTimeMillis();
159
     // Read the existing spatial dataset cache
160
     SpatialDataset sds = new SpatialDataset();
161

    
162
     // insert OR update the spatial cache
163
     // SpatialDataset.insertOrUpdate takes care of the difference 
164
     SpatialDocument sdoc = new SpatialDocument( docid, dbconn );
165

    
166
     try {
167
         Feature polygonFeature = sdoc.getPolygonFeature();
168
         sds.insertOrUpdate("polygon", polygonFeature, docid );
169

    
170
         Feature pointFeature = sdoc.getPointFeature();
171
         sds.insertOrUpdate("point", pointFeature, docid );
172
         long after = System.currentTimeMillis();
173
         log.info(" ------- Spatial Harvester - spatial cache updated for : " + docid + ".... Time  " + (after - before) + "ms");
174
     } catch (IOException e) {
175
         log.error("IOException while performing spatial harvest ");
176
     }
177
  }
178

    
179
  /**
180
   * Completely regenerates the spatial cache. 
181
   * This can take a long time, especially with lots of documents.
182
   */
183
  public void regenerate() {
184
      
185
      // Create new Spatial Dataset
186
      SpatialDataset sds = new SpatialDataset();
187

    
188
      // Get list of all docids in the database
189
      Vector docids = queryAllDocids(); 
190
       
191
      for (int i = 0; i < docids.size(); i++) {
192
          SpatialDocument sdoc = new SpatialDocument( (String)docids.elementAt(i), dbconn );
193

    
194
          // Get the polygon representation of SpatialDocument
195
          // and add it to the spatial dataset
196
          Feature polygonFeature = sdoc.getPolygonFeature();
197
          sds.add("polygon", polygonFeature );
198

    
199
          // Get the point representation of SpatialDocument
200
          // and add it to the spatial dataset
201
          Feature pointFeature = sdoc.getPointFeature();
202
          sds.add("point", pointFeature );
203

    
204
          log.info(" ****** Spatial harvest of docid " + docids.elementAt(i) );
205
      }
206

    
207
      // save SpatialDataset
208
      sds.save();
209

    
210
  }
211

    
212
}
(5-5/8)