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: 2011-01-21 15:08:31 -0800 (Fri, 21 Jan 2011) $'
8
 * '$Revision: 5829 $'
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.database.DBConnection;
35
import edu.ucsb.nceas.metacat.util.MetacatUtil;
36

    
37
import org.opengis.feature.simple.SimpleFeature;
38

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

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

    
82
    /*
83
     * For now, only allow publically readable docids
84
     * to be considered for the spatial cache
85
     */
86
    //String query = "select distinct docid from xml_path_index";
87
    String query = "select distinct docid from xml_access where principal_name = 'public' and perm_type = 'allow'";
88

    
89
    try {
90
      pstmt = dbconn.prepareStatement(query);
91
      pstmt.execute();
92
      rs = pstmt.getResultSet();
93
      while (rs.next()) {
94
        String docid = rs.getString(1);
95
        //log.fatal("adding docid: " + docid);
96
        _docs.add(docid);
97
    }
98
      rs.close();
99
      pstmt.close();
100
    }
101
    catch(Exception e) {
102
      log.error("Error getting docids from queryAllDocids");
103
      e.printStackTrace();
104
    }
105
    return _docs;
106
  }
107

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

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

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

    
136
     
137

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

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

    
151

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

    
160
     long before = System.currentTimeMillis();
161
     
162
     try {
163
         //Read the existing spatial dataset cache
164
         SpatialDataset sds = new SpatialDataset();
165

    
166
         // insert OR update the spatial cache
167
         // SpatialDataset.insertOrUpdate takes care of the difference 
168
         SpatialDocument sdoc = new SpatialDocument( docid, dbconn );
169

    
170
         SimpleFeature polygonFeature = sdoc.getPolygonFeature();
171
         sds.insertOrUpdate("polygon", polygonFeature, docid );
172

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

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

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

    
197
          // Get the polygon representation of SpatialDocument
198
          // and add it to the spatial dataset
199
         SimpleFeature polygonFeature = sdoc.getPolygonFeature();
200
          sds.add("polygon", polygonFeature );
201

    
202
          // Get the point representation of SpatialDocument
203
          // and add it to the spatial dataset
204
          SimpleFeature pointFeature = sdoc.getPointFeature();
205
          sds.add("point", pointFeature );
206

    
207
          log.info(" ****** Spatial harvest of docid " + docids.elementAt(i) );
208
      }
209

    
210
      // save SpatialDataset
211
      sds.save();
212

    
213
  }
214

    
215
}
(5-5/8)