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-12-07 12:18:24 -0800 (Wed, 07 Dec 2011) $'
|
8
|
* '$Revision: 6744 $'
|
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.io.IOException;
|
28
|
import java.sql.PreparedStatement;
|
29
|
import java.sql.ResultSet;
|
30
|
import java.sql.SQLException;
|
31
|
import java.util.Vector;
|
32
|
|
33
|
import org.apache.log4j.Logger;
|
34
|
import org.opengis.feature.simple.SimpleFeature;
|
35
|
|
36
|
import edu.ucsb.nceas.metacat.database.DBConnection;
|
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( SQLException 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 current versions of public docids
|
75
|
*/
|
76
|
protected Vector<String> queryAllDocids() {
|
77
|
Vector<String> _docs = new Vector<String>();
|
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 id.docid " +
|
87
|
"from xml_access xa, identifier id, xml_documents xd " +
|
88
|
"where xa.guid = id.guid " +
|
89
|
"and id.docid = xd.docid " +
|
90
|
"and id.rev = xd.rev " +
|
91
|
"and xa.principal_name = 'public' " +
|
92
|
"and xa.perm_type = 'allow'";
|
93
|
|
94
|
try {
|
95
|
pstmt = dbconn.prepareStatement(query);
|
96
|
pstmt.execute();
|
97
|
rs = pstmt.getResultSet();
|
98
|
while (rs.next()) {
|
99
|
String docid = rs.getString(1);
|
100
|
if (docid != null) {
|
101
|
//log.fatal("adding docid: " + docid);
|
102
|
_docs.add(docid);
|
103
|
}
|
104
|
}
|
105
|
rs.close();
|
106
|
pstmt.close();
|
107
|
}
|
108
|
catch(Exception e) {
|
109
|
log.error("Error getting docids from queryAllDocids");
|
110
|
e.printStackTrace();
|
111
|
}
|
112
|
return _docs;
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* Currently just a wrapper around the harvestDocument method.
|
117
|
* Eventually we can use this method as a
|
118
|
* timed que like the indexing process.
|
119
|
*
|
120
|
* @param docid The docid to be added to the spatial cache.
|
121
|
*/
|
122
|
public void addToUpdateQue( String docid ) {
|
123
|
harvestDocument(docid);
|
124
|
}
|
125
|
|
126
|
/**
|
127
|
* Delete a given document from spatial cache.
|
128
|
* Just a wrapper around deleteDocument method for now.
|
129
|
*
|
130
|
* @param docid The docid to be deleted from the spatial cache.
|
131
|
*/
|
132
|
public void addToDeleteQue( String docid ) {
|
133
|
deleteDocument(docid);
|
134
|
}
|
135
|
|
136
|
/**
|
137
|
* Given a docid, will attempt to delete it from spatial cache
|
138
|
*
|
139
|
* @param docid The docid to be deleted from the spatial cache.
|
140
|
*/
|
141
|
public void deleteDocument( String docid ) {
|
142
|
|
143
|
|
144
|
|
145
|
try {
|
146
|
// Read the existing spatial dataset cache
|
147
|
SpatialDataset sds = new SpatialDataset();
|
148
|
// Delete both the polygon(s) and point(s)
|
149
|
sds.delete( "polygon" , docid );
|
150
|
sds.delete( "point" , docid );
|
151
|
} catch (IOException e) {
|
152
|
log.error("IOException while deleting from spatial cache");
|
153
|
}
|
154
|
|
155
|
log.info(" --------- Spatial Harvester - Deleted from spatial cache : " + docid );
|
156
|
}
|
157
|
|
158
|
|
159
|
/**
|
160
|
* Given a new or updated docid,
|
161
|
* will update the spatial cache accordingly.
|
162
|
*
|
163
|
* @param docid The docid to be updated or inserted into the spatial cache.
|
164
|
*/
|
165
|
public void harvestDocument( String docid ) {
|
166
|
|
167
|
long before = System.currentTimeMillis();
|
168
|
|
169
|
try {
|
170
|
//Read the existing spatial dataset cache
|
171
|
SpatialDataset sds = new SpatialDataset();
|
172
|
|
173
|
// insert OR update the spatial cache
|
174
|
// SpatialDataset.insertOrUpdate takes care of the difference
|
175
|
SpatialDocument sdoc = new SpatialDocument( docid, dbconn );
|
176
|
|
177
|
SimpleFeature polygonFeature = sdoc.getPolygonFeature();
|
178
|
sds.insertOrUpdate("polygon", polygonFeature, docid );
|
179
|
|
180
|
SimpleFeature pointFeature = sdoc.getPointFeature();
|
181
|
sds.insertOrUpdate("point", pointFeature, docid );
|
182
|
long after = System.currentTimeMillis();
|
183
|
log.info(" ------- Spatial Harvester - spatial cache updated for : " + docid + ".... Time " + (after - before) + "ms");
|
184
|
} catch (IOException e) {
|
185
|
log.error("IOException while performing spatial harvest ");
|
186
|
}
|
187
|
}
|
188
|
|
189
|
/**
|
190
|
* Completely regenerates the spatial cache.
|
191
|
* This can take a long time, especially with lots of documents.
|
192
|
*/
|
193
|
public void regenerate() throws IOException{
|
194
|
|
195
|
// Create new Spatial Dataset
|
196
|
SpatialDataset sds = new SpatialDataset();
|
197
|
|
198
|
// Get list of all docids in the database
|
199
|
Vector<String> docids = queryAllDocids();
|
200
|
|
201
|
for (int i = 0; i < docids.size(); i++) {
|
202
|
SpatialDocument sdoc = new SpatialDocument( (String)docids.elementAt(i), dbconn );
|
203
|
|
204
|
// Get the polygon representation of SpatialDocument
|
205
|
// and add it to the spatial dataset
|
206
|
SimpleFeature polygonFeature = sdoc.getPolygonFeature();
|
207
|
sds.add("polygon", polygonFeature );
|
208
|
|
209
|
// Get the point representation of SpatialDocument
|
210
|
// and add it to the spatial dataset
|
211
|
SimpleFeature pointFeature = sdoc.getPointFeature();
|
212
|
sds.add("point", pointFeature );
|
213
|
|
214
|
log.info(" ****** Spatial harvest of docid " + docids.elementAt(i) );
|
215
|
}
|
216
|
|
217
|
// save SpatialDataset
|
218
|
sds.save();
|
219
|
|
220
|
}
|
221
|
|
222
|
}
|