Revision 2909
Added by harris almost 19 years ago
src/edu/ucsb/nceas/metacat/spatial/MetacatSpatialDataset.java | ||
---|---|---|
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: John Harris |
|
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 |
import java.util.Vector; |
|
28 |
|
|
29 |
import org.apache.log4j.Logger; |
|
30 |
|
|
31 |
/** |
|
32 |
* Class that reflects a dataset of Metacat Documents in a spatially represented |
|
33 |
* sense. This dataset class is comprised of multiple MetacatSpatialDocument |
|
34 |
* and uses the PersistentMetacatSpatialDataset class for IO with databases |
|
35 |
* and the file system |
|
36 |
*/ |
|
37 |
public class MetacatSpatialDataset { |
|
38 |
|
|
39 |
private boolean initialized = false; |
|
40 |
|
|
41 |
private static Logger log = Logger.getLogger(MetacatSpatialDataset.class.getName()); |
|
42 |
|
|
43 |
private Vector docs; // vector of MetacatSpatialDocument's |
|
44 |
|
|
45 |
|
|
46 |
/** empty constructor **/ |
|
47 |
public MetacatSpatialDataset() { |
|
48 |
this.initialized = true; |
|
49 |
docs = new Vector(); |
|
50 |
} |
|
51 |
|
|
52 |
/* |
|
53 |
* Adds a new MetacatSpatialDocument to this dataset |
|
54 |
*/ |
|
55 |
public void add(MetacatSpatialDocument msdoc) { |
|
56 |
docs.add(msdoc); |
|
57 |
} |
|
58 |
|
|
59 |
|
|
60 |
/** |
|
61 |
* returns the data set as a flat-ascii table like: |
|
62 |
* |
|
63 |
* x y z segid |
|
64 |
* |
|
65 |
* the data are delimited by spaces and each line is terminated bt '\n' |
|
66 |
*/ |
|
67 |
public StringBuffer getExtentsDataAsAscii() { |
|
68 |
StringBuffer _sb = new StringBuffer(); |
|
69 |
for (int i = 0; i < docs.size(); i++) { |
|
70 |
MetacatSpatialDocument _doc = (MetacatSpatialDocument)docs.elementAt(i); |
|
71 |
if ( ! _doc.getIsPoint() ) { |
|
72 |
_sb.append( _doc.getXMin()+ " " + _doc.getYMin() + " 0 " + _doc.getDocid()+"\n"); |
|
73 |
_sb.append( _doc.getXMax()+ " " + _doc.getYMin() + " 0 " + _doc.getDocid()+"\n"); |
|
74 |
_sb.append( _doc.getXMax()+ " " + _doc.getYMax() + " 0 " + _doc.getDocid()+"\n"); |
|
75 |
_sb.append( _doc.getXMin()+ " " + _doc.getYMax() + " 0 " + _doc.getDocid()+"\n"); |
|
76 |
} |
|
77 |
} |
|
78 |
return _sb; |
|
79 |
} |
|
80 |
|
|
81 |
|
|
82 |
/* |
|
83 |
* Writes the dataset to the file system |
|
84 |
*/ |
|
85 |
public void writeTextQueryData() { |
|
86 |
|
|
87 |
// write the data stored in this object using the persistent layer |
|
88 |
PersistentMetacatSpatialDataset _writer = new PersistentMetacatSpatialDataset(); |
|
89 |
_writer.writeTextQueryData(this); |
|
90 |
|
|
91 |
} |
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
} |
|
0 | 97 |
src/edu/ucsb/nceas/metacat/spatial/MetacatSpatialQuery.java | ||
---|---|---|
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: John Harris |
|
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 |
|
|
26 |
package edu.ucsb.nceas.metacat.spatial; |
|
27 |
|
|
28 |
import org.apache.log4j.Logger; |
|
29 |
|
|
30 |
/** |
|
31 |
* Class that reflects a query against Metacat to gatther spatial data -- this |
|
32 |
* could be a text query that returns a spatial dataset or a spatial query that |
|
33 |
* returns a spatially-referenced dataset. |
|
34 |
*/ |
|
35 |
public class MetacatSpatialQuery { |
|
36 |
|
|
37 |
private static Logger log = Logger.getLogger(MetacatSpatialQuery.class.getName()); |
|
38 |
|
|
39 |
private MetacatSpatialDocument msd; |
|
40 |
|
|
41 |
private SpatialQueryProcessor mqp; |
|
42 |
|
|
43 |
public MetacatSpatialQuery() { } |
|
44 |
|
|
45 |
/** |
|
46 |
* returns the metacat spatial document retrieved by this query |
|
47 |
*/ |
|
48 |
public MetacatSpatialDocument getSpatialDocument(String docid) { |
|
49 |
msd = new MetacatSpatialDocument(docid); |
|
50 |
|
|
51 |
log.info("creating spatial reference for docid: " + docid); |
|
52 |
|
|
53 |
/** all database querying is carried out in the Query Processor */ |
|
54 |
mqp = new SpatialQueryProcessor(); |
|
55 |
|
|
56 |
// this call will return the extents (a class) of the docid |
|
57 |
MetacatSpatialExtents _extents = mqp.queryExtentsByDocid(docid); |
|
58 |
|
|
59 |
//set the elements on the spatialdocument |
|
60 |
msd.setDocid(docid); |
|
61 |
msd.setExtents(_extents); |
|
62 |
|
|
63 |
// destroy the processor |
|
64 |
mqp.close(); |
|
65 |
|
|
66 |
return msd; |
|
67 |
} |
|
68 |
|
|
69 |
} |
|
0 | 70 |
src/edu/ucsb/nceas/metacat/spatial/XSLTransform.java | ||
---|---|---|
1 |
/** |
|
2 |
* '$RCSfile$' |
|
3 |
* Copyright: 2003 Regents of the University of California and the |
|
4 |
* National Center for Ecological Analysis and Synthesis |
|
5 |
* |
|
6 |
* Author: John Harris |
|
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 |
package edu.ucsb.nceas.metacat.spatial; |
|
25 |
|
|
26 |
import java.io.*; |
|
27 |
import java.net.URL; |
|
28 |
import java.net.MalformedURLException; |
|
29 |
import java.sql.*; |
|
30 |
import java.util.Enumeration; |
|
31 |
import java.util.Hashtable; |
|
32 |
import java.util.Stack; |
|
33 |
|
|
34 |
import javax.xml.transform.TransformerFactory; |
|
35 |
import javax.xml.transform.Transformer; |
|
36 |
import javax.xml.transform.stream.StreamSource; |
|
37 |
import javax.xml.transform.stream.StreamResult; |
|
38 |
import javax.xml.transform.TransformerException; |
|
39 |
import javax.xml.transform.TransformerConfigurationException; |
|
40 |
import javax.xml.transform.URIResolver; |
|
41 |
|
|
42 |
import org.apache.xerces.parsers.DOMParser; |
|
43 |
import org.w3c.dom.Attr; |
|
44 |
import org.w3c.dom.NamedNodeMap; |
|
45 |
import org.w3c.dom.NodeList; |
|
46 |
import org.w3c.dom.Document; |
|
47 |
import org.w3c.dom.Node; |
|
48 |
import org.w3c.dom.NodeList; |
|
49 |
import org.w3c.dom.DocumentType; |
|
50 |
import org.xml.sax.SAXException; |
|
51 |
import org.xml.sax.InputSource; |
|
52 |
import org.apache.xerces.dom.DocumentTypeImpl; |
|
53 |
import org.apache.xpath.XPathAPI; |
|
54 |
import org.w3c.dom.NamedNodeMap; |
|
55 |
|
|
56 |
import org.w3c.dom.Document; |
|
57 |
import org.w3c.dom.Node; |
|
58 |
import org.w3c.dom.Element; |
|
59 |
import org.xml.sax.SAXException; |
|
60 |
|
|
61 |
//import edu.ucsb.nceas.utilities.IOUtil; |
|
62 |
|
|
63 |
/** |
|
64 |
* A Class that transforms XML documents utitlizing XSL style sheets. This is |
|
65 |
* a convenience class that makes it easier to handle the location of |
|
66 |
* stylesheets and the mapping between a request and a particular stylesheet. |
|
67 |
*/ |
|
68 |
public class XSLTransform { |
|
69 |
|
|
70 |
/** |
|
71 |
* Private constructor because all methids are static and do not need |
|
72 |
* an instance. |
|
73 |
*/ |
|
74 |
public XSLTransform() |
|
75 |
{ |
|
76 |
} |
|
77 |
|
|
78 |
/** |
|
79 |
* Transform an XML document using an XSLT stylesheet to another format, |
|
80 |
* probably HTML or another XML document format. |
|
81 |
* |
|
82 |
* @param docString the document to be transformed |
|
83 |
* @param xslSystemId the system location of the stylesheet |
|
84 |
* @param pw the PrintWriter to which output is printed |
|
85 |
* @param params some parameters for inclusion to the transformation |
|
86 |
*/ |
|
87 |
public static void transform(String docString, String xslSystemId, |
|
88 |
PrintWriter pw, Hashtable param) |
|
89 |
{ |
|
90 |
transform(new StringReader(docString), xslSystemId, pw, param); |
|
91 |
} |
|
92 |
|
|
93 |
/** |
|
94 |
* Transform an XML document using an XSLT stylesheet to another format, |
|
95 |
* probably HTML or another XML document format. |
|
96 |
* |
|
97 |
* @param doc the document to be transformed |
|
98 |
* @param xslSystemId the system location of the stylesheet |
|
99 |
* @param pw the PrintWriter to which output is printed |
|
100 |
* @param params some parameters for inclusion to the transformation |
|
101 |
*/ |
|
102 |
public static void transform(Reader doc, String xslSystemId, |
|
103 |
PrintWriter pw, Hashtable param) |
|
104 |
{ |
|
105 |
try { |
|
106 |
|
|
107 |
StreamSource xslSource = |
|
108 |
new StreamSource(xslSystemId); |
|
109 |
xslSource.setSystemId(xslSystemId); |
|
110 |
// Create a stylesheet from the system id that was found |
|
111 |
TransformerFactory tFactory = TransformerFactory.newInstance(); |
|
112 |
Transformer transformer = tFactory.newTransformer(xslSource); |
|
113 |
|
|
114 |
// Set up parameters for transformation |
|
115 |
if ( param != null) { |
|
116 |
Enumeration en = param.keys(); |
|
117 |
while (en.hasMoreElements()) { |
|
118 |
String key =(String)en.nextElement(); |
|
119 |
String value = ((String)(param.get(key))); |
|
120 |
transformer.setParameter(key, value); |
|
121 |
} |
|
122 |
} |
|
123 |
|
|
124 |
// Run the transform engine |
|
125 |
StreamSource ss = new StreamSource(doc); |
|
126 |
StreamResult sr = new StreamResult(pw); |
|
127 |
transformer.transform(ss, sr); |
|
128 |
} catch (Exception e) { |
|
129 |
pw.println("Error transforming document in " + |
|
130 |
"XSLTransform.transform:\n" + e.getMessage()); |
|
131 |
e.printStackTrace(pw); |
|
132 |
} |
|
133 |
} |
|
134 |
|
|
135 |
/** |
|
136 |
* the main routine used to test the transform utility. |
|
137 |
* |
|
138 |
* Usage: java DBTransform |
|
139 |
*/ |
|
140 |
static public void main(String[] args) { |
|
141 |
|
|
142 |
if (args.length != 2) |
|
143 |
{ |
|
144 |
System.err.println("Wrong number of arguments!!!"); |
|
145 |
System.err.println("USAGE: java XSLTransform xml style"); |
|
146 |
return; |
|
147 |
} else { |
|
148 |
String xmlfile = args[0]; |
|
149 |
String xslfile = args[1]; |
|
150 |
try { |
|
151 |
Reader r = new FileReader(xmlfile); |
|
152 |
XSLTransform.transform( r, xslfile, |
|
153 |
new PrintWriter(System.out), null); |
|
154 |
} catch (Exception e) { |
|
155 |
System.err.println("EXCEPTION HANDLING REQUIRED"); |
|
156 |
System.err.println(e.getMessage()); |
|
157 |
e.printStackTrace(System.err); |
|
158 |
} |
|
159 |
} |
|
160 |
} |
|
161 |
} |
|
0 | 162 |
src/edu/ucsb/nceas/metacat/spatial/PersistentMetacatSpatialDataset.java | ||
---|---|---|
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: John Harris |
|
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 |
|
|
26 |
package edu.ucsb.nceas.metacat.spatial; |
|
27 |
|
|
28 |
import java.io.BufferedReader; |
|
29 |
import java.io.FileReader; |
|
30 |
import java.io.PrintStream; |
|
31 |
import java.io.OutputStream; |
|
32 |
import java.io.FileOutputStream; |
|
33 |
import java.io.IOException; |
|
34 |
import java.io.FileNotFoundException; |
|
35 |
|
|
36 |
|
|
37 |
import org.apache.log4j.Logger; |
|
38 |
|
|
39 |
/** |
|
40 |
* Class for doing IO of spatial data from. IO can be with the FS in the |
|
41 |
* case of a shapefile, and or with a DB (as in the case of a PostGIS |
|
42 |
* entry or a Metacat Entry) |
|
43 |
*/ |
|
44 |
public class PersistentMetacatSpatialDataset { |
|
45 |
|
|
46 |
private String textQueryAsciiFile = "/tmp/mso_metacat_textquery.ascii"; |
|
47 |
|
|
48 |
private static Logger log = Logger.getLogger(PersistentMetacatSpatialDataset.class.getName()); |
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
/* |
|
53 |
* Writes the dataset to the file system |
|
54 |
*/ |
|
55 |
public void writeTextQueryData(MetacatSpatialDataset dataset) { |
|
56 |
|
|
57 |
try { |
|
58 |
PrintStream out = new PrintStream(new FileOutputStream(textQueryAsciiFile)); |
|
59 |
|
|
60 |
out.println("#HEADER x y z segid"); |
|
61 |
|
|
62 |
out.println( dataset.getExtentsDataAsAscii()); |
|
63 |
|
|
64 |
out.close(); |
|
65 |
|
|
66 |
} catch (FileNotFoundException fnfe) { |
|
67 |
log.fatal("ERROR: could not find the file "); |
|
68 |
fnfe.printStackTrace(); |
|
69 |
} |
|
70 |
|
|
71 |
|
|
72 |
} |
|
73 |
|
|
74 |
} |
|
0 | 75 |
src/edu/ucsb/nceas/metacat/spatial/SpatialQueryProcessor.java | ||
---|---|---|
1 |
/** |
|
2 |
* '$RCSfile$' |
|
3 |
* Copyright: 2003 Regents of the University of California. |
|
4 |
* |
|
5 |
* Author: John Harris |
|
6 |
* '$Date$' |
|
7 |
* '$Revision$' |
|
8 |
* |
|
9 |
* This program is free software; you can redistribute it and/or modify |
|
10 |
* it under the terms of the GNU General Public License as published by |
|
11 |
* the Free Software Foundation; either version 2 of the License, or |
|
12 |
* (at your option) any later version. |
|
13 |
* |
|
14 |
* This program is distributed in the hope that it will be useful, |
|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 |
* GNU General Public License for more details. |
|
18 |
* |
|
19 |
* You should have received a copy of the GNU General Public License |
|
20 |
* along with this program; if not, write to the Free Software |
|
21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
22 |
*/ |
|
23 |
package edu.ucsb.nceas.metacat.spatial; |
|
24 |
|
|
25 |
import java.io.File; |
|
26 |
import java.io.FileReader; |
|
27 |
import java.io.PrintWriter; |
|
28 |
import java.io.IOException; |
|
29 |
import java.io.StringReader; |
|
30 |
import java.io.BufferedReader; |
|
31 |
import java.io.Reader; |
|
32 |
import java.io.FileInputStream; |
|
33 |
import java.io.BufferedInputStream; |
|
34 |
import java.util.Enumeration; |
|
35 |
import java.util.Hashtable; |
|
36 |
import java.util.ResourceBundle; |
|
37 |
import java.util.Random; |
|
38 |
import java.util.StringTokenizer; |
|
39 |
import java.util.Properties; |
|
40 |
import java.util.PropertyResourceBundle; |
|
41 |
import java.util.Vector; |
|
42 |
import java.util.Map; |
|
43 |
|
|
44 |
import edu.ucsb.nceas.metacat.client.Metacat; |
|
45 |
import edu.ucsb.nceas.metacat.DBConnection; |
|
46 |
import edu.ucsb.nceas.metacat.client.MetacatFactory; |
|
47 |
import edu.ucsb.nceas.metacat.client.MetacatAuthException; |
|
48 |
import edu.ucsb.nceas.metacat.client.MetacatException; |
|
49 |
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException; |
|
50 |
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException; |
|
51 |
import edu.ucsb.nceas.utilities.IOUtil; |
|
52 |
|
|
53 |
import java.sql.ResultSet; |
|
54 |
import java.sql.PreparedStatement; |
|
55 |
|
|
56 |
import org.apache.log4j.Logger; |
|
57 |
import org.apache.log4j.BasicConfigurator; |
|
58 |
import org.apache.log4j.PropertyConfigurator; |
|
59 |
|
|
60 |
public class SpatialQueryProcessor { |
|
61 |
|
|
62 |
private String METACATURL = "http://nebulous.msi.ucsb.edu:9999/knp/metacat"; |
|
63 |
private File QUERYFILE = null; |
|
64 |
private String MCUSER = "uid=harris,o=NCEAS,dc=ecoinformatics,dc=org"; |
|
65 |
private String MCPASS = "yeahRight"; |
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
private DBConnection dbconn; |
|
71 |
|
|
72 |
private static Logger log = Logger.getLogger(SpatialQueryProcessor.class.getName()); |
|
73 |
|
|
74 |
/** constructor that uses the defaults **/ |
|
75 |
public SpatialQueryProcessor() { |
|
76 |
try { |
|
77 |
dbconn = new DBConnection(); |
|
78 |
} catch ( java.sql.SQLException se) { |
|
79 |
log.fatal("Error connecting to the database ..."); |
|
80 |
se.getMessage(); |
|
81 |
} |
|
82 |
} |
|
83 |
|
|
84 |
/** call this after the processor is done **/ |
|
85 |
protected void close() { |
|
86 |
try { |
|
87 |
dbconn.close(); |
|
88 |
} catch ( java.sql.SQLException se) { |
|
89 |
log.fatal("Error closing connecting to the database ..."); |
|
90 |
se.getMessage(); |
|
91 |
} |
|
92 |
} |
|
93 |
|
|
94 |
public SpatialQueryProcessor(String mcUser, String mcPass, String metacatUrl, |
|
95 |
File queryFile) { |
|
96 |
log.debug("constructor called"); |
|
97 |
this.METACATURL = metacatUrl; |
|
98 |
this.MCPASS = mcPass; |
|
99 |
this.MCUSER = mcUser; |
|
100 |
this.QUERYFILE = queryFile; |
|
101 |
} |
|
102 |
|
|
103 |
public String execute() throws IOException { |
|
104 |
FileReader queryFileReader = new FileReader(QUERYFILE); |
|
105 |
String res = runQuery((Reader)queryFileReader); |
|
106 |
return res; |
|
107 |
} |
|
108 |
|
|
109 |
protected MetacatSpatialExtents queryExtentsForDataset() { |
|
110 |
|
|
111 |
|
|
112 |
String query = "<?xml version=\"1.0\" ?> <pathquery version=\"1.2\"> " |
|
113 |
+"<querytitle>Untitled-Search-2</querytitle> " |
|
114 |
+"<returndoctype>-//ecoinformatics.org//eml-dataset-2.0.0beta6//EN</returndoctype> " |
|
115 |
+"<returndoctype>-//NCEAS//eml-dataset-2.0//EN</returndoctype> " |
|
116 |
+"<returndoctype>eml://ecoinformatics.org/eml-2.0.0</returndoctype> " |
|
117 |
+"<returnfield>dataset/title</returnfield> " |
|
118 |
+"<returnfield>individualName/surName</returnfield> " |
|
119 |
+"<returnfield>keyword</returnfield><returnfield>westBoundingCoordinate</returnfield>" |
|
120 |
+"<returnfield>eastBoundingCoordinate</returnfield><returnfield>northBoundingCoordinate" |
|
121 |
+"</returnfield><returnfield>southBoundingCoordinate</returnfield>" |
|
122 |
+"<returnfield>westbc</returnfield><returnfield>eastbc</returnfield>" |
|
123 |
+"<returnfield>northbc</returnfield><returnfield>southbc</returnfield>" |
|
124 |
+"<querygroup operator=\"INTERSECT\"><querygroup operator=\"UNION\">" |
|
125 |
+"<querygroup operator=\"UNION\"><queryterm searchmode=\"contains\" casesensitive=\"false\">" |
|
126 |
+"<value>%</value></queryterm></querygroup></querygroup></querygroup></pathquery>"; |
|
127 |
|
|
128 |
StringReader _sr = new StringReader(query); |
|
129 |
String _result = runQuery(_sr); |
|
130 |
|
|
131 |
log.info("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$: result data: " + _result); |
|
132 |
|
|
133 |
|
|
134 |
return null; |
|
135 |
|
|
136 |
} |
|
137 |
|
|
138 |
/** |
|
139 |
* returns the north bounding coordinate -- this is a single point |
|
140 |
*/ |
|
141 |
private float queryNorthBoundingCoordinate(String docid) { |
|
142 |
float _coord = MetacatSpatialConstants.FLOAT_NULL; |
|
143 |
PreparedStatement pstmt = null; |
|
144 |
ResultSet rs = null; |
|
145 |
String query = "select docid, nodedata, nodeid from xml_nodes where " |
|
146 |
+"nodeid =(select nodeid from xml_nodes where docid like '" |
|
147 |
+docid.trim()+"' and nodename like 'north%')+1;"; |
|
148 |
|
|
149 |
try { |
|
150 |
pstmt = dbconn.prepareStatement(query); |
|
151 |
pstmt.execute(); |
|
152 |
rs = pstmt.getResultSet(); |
|
153 |
if ( rs.next() ) |
|
154 |
_coord = rs.getFloat(2); |
|
155 |
rs.close(); |
|
156 |
pstmt.close(); |
|
157 |
} catch (Exception e){ |
|
158 |
log.error("Error getting docids from queryExtentsByDocid"); |
|
159 |
e.printStackTrace(); |
|
160 |
} |
|
161 |
return _coord; |
|
162 |
} |
|
163 |
|
|
164 |
|
|
165 |
/** |
|
166 |
* returns the east bounding coordinate -- this is a single point |
|
167 |
*/ |
|
168 |
private float queryEastBoundingCoordinate(String docid) { |
|
169 |
float _coord = MetacatSpatialConstants.FLOAT_NULL; |
|
170 |
PreparedStatement pstmt = null; |
|
171 |
ResultSet rs = null; |
|
172 |
String query = "select docid, nodedata, nodeid from xml_nodes where " |
|
173 |
+"nodeid =(select nodeid from xml_nodes where docid like '" |
|
174 |
+docid.trim()+"' and nodename like 'east%')+1;"; |
|
175 |
|
|
176 |
try { |
|
177 |
pstmt = dbconn.prepareStatement(query); |
|
178 |
pstmt.execute(); |
|
179 |
rs = pstmt.getResultSet(); |
|
180 |
if ( rs.next() ) |
|
181 |
_coord = rs.getFloat(2); |
|
182 |
rs.close(); |
|
183 |
pstmt.close(); |
|
184 |
} catch (Exception e){ |
|
185 |
log.error("Error getting docids from queryExtentsByDocid"); |
|
186 |
e.printStackTrace(); |
|
187 |
} |
|
188 |
return _coord; |
|
189 |
} |
|
190 |
|
|
191 |
|
|
192 |
/** |
|
193 |
* returns the south bounding coordinate -- this is a single point |
|
194 |
*/ |
|
195 |
private float querySouthBoundingCoordinate(String docid) { |
|
196 |
float _coord = MetacatSpatialConstants.FLOAT_NULL; |
|
197 |
PreparedStatement pstmt = null; |
|
198 |
ResultSet rs = null; |
|
199 |
String query = "select docid, nodedata, nodeid from xml_nodes where " |
|
200 |
+"nodeid =(select nodeid from xml_nodes where docid like '" |
|
201 |
+docid.trim()+"' and nodename like 'south%')+1;"; |
|
202 |
|
|
203 |
try { |
|
204 |
pstmt = dbconn.prepareStatement(query); |
|
205 |
pstmt.execute(); |
|
206 |
rs = pstmt.getResultSet(); |
|
207 |
if ( rs.next() ) |
|
208 |
_coord = rs.getFloat(2); |
|
209 |
rs.close(); |
|
210 |
pstmt.close(); |
|
211 |
} catch (Exception e){ |
|
212 |
log.error("Error getting docids from queryExtentsByDocid"); |
|
213 |
e.printStackTrace(); |
|
214 |
} |
|
215 |
return _coord; |
|
216 |
} |
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
/** |
|
221 |
* returns the west bounding coordinate -- this is a single point |
|
222 |
*/ |
|
223 |
private float queryWestBoundingCoordinate(String docid) { |
|
224 |
float _coord = MetacatSpatialConstants.FLOAT_NULL; |
|
225 |
PreparedStatement pstmt = null; |
|
226 |
ResultSet rs = null; |
|
227 |
String query = "select docid, nodedata, nodeid from xml_nodes where " |
|
228 |
+"nodeid =(select nodeid from xml_nodes where docid like '" |
|
229 |
+docid.trim()+"' and nodename like 'west%')+1;"; |
|
230 |
|
|
231 |
try { |
|
232 |
pstmt = dbconn.prepareStatement(query); |
|
233 |
pstmt.execute(); |
|
234 |
rs = pstmt.getResultSet(); |
|
235 |
if ( rs.next() ) |
|
236 |
_coord = rs.getFloat(2); |
|
237 |
rs.close(); |
|
238 |
pstmt.close(); |
|
239 |
} catch (Exception e){ |
|
240 |
log.error("Error getting docids from queryExtentsByDocid"); |
|
241 |
e.printStackTrace(); |
|
242 |
} |
|
243 |
return _coord; |
|
244 |
} |
|
245 |
|
|
246 |
|
|
247 |
|
|
248 |
|
|
249 |
/** |
|
250 |
* Performs a spatial query given the docid as an input |
|
251 |
* |
|
252 |
* @param docid -- the docid for which the extents are to be queried |
|
253 |
* @returns spatialextents -- the MetcataSpatialExtents object for this query |
|
254 |
*/ |
|
255 |
protected MetacatSpatialExtents queryExtentsByDocid(String docid) { |
|
256 |
MetacatSpatialExtents _extents = new MetacatSpatialExtents(); |
|
257 |
|
|
258 |
float _northbc = queryNorthBoundingCoordinate(docid); |
|
259 |
float _eastbc = queryEastBoundingCoordinate(docid); |
|
260 |
float _southbc = querySouthBoundingCoordinate(docid); |
|
261 |
float _westbc = queryWestBoundingCoordinate(docid); |
|
262 |
|
|
263 |
if (( _northbc == _southbc) && (_eastbc == _westbc) ) { |
|
264 |
_extents.setIsPoint(true); } else { _extents.setIsPoint(false); } |
|
265 |
|
|
266 |
_extents.setYMin( (_southbc < _northbc? _southbc : _northbc) ); |
|
267 |
_extents.setYMax( (_southbc > _northbc? _southbc : _northbc) ); |
|
268 |
_extents.setXMin( (_westbc < _eastbc? _westbc : _eastbc) ); |
|
269 |
_extents.setXMax( (_westbc > _eastbc? _westbc : _eastbc) ); |
|
270 |
|
|
271 |
_extents.setDocid(docid); |
|
272 |
|
|
273 |
log.warn("**************************** > " + _extents.toString() ); |
|
274 |
|
|
275 |
|
|
276 |
return _extents; |
|
277 |
} |
|
278 |
|
|
279 |
|
|
280 |
/** |
|
281 |
* Query the Metacat Server to get a list of documents, |
|
282 |
* if it doesn't exist there, from the backend storage server. |
|
283 |
* |
|
284 |
* @param query the xml description of the query to run |
|
285 |
* @return String the XML document string of the result set |
|
286 |
* @throws QueryFailedException when any thing happens to prevent a query |
|
287 |
* returns |
|
288 |
*/ |
|
289 |
private String runQuery(Reader query) |
|
290 |
{ |
|
291 |
String document = null; |
|
292 |
|
|
293 |
Reader xdoc = null; |
|
294 |
String error = null; |
|
295 |
try { |
|
296 |
log.debug("creating metacat instance with: " + METACATURL); |
|
297 |
Metacat m = MetacatFactory.createMetacatConnection(METACATURL); |
|
298 |
m.login(MCUSER, MCPASS); |
|
299 |
log.debug("metacat instance: " + m); |
|
300 |
xdoc = m.query(query); |
|
301 |
document = IOUtil.getAsString(xdoc, true); |
|
302 |
m.logout(); |
|
303 |
if (document == null) { |
|
304 |
throw new QueryFailedException("Document String Was null"); |
|
305 |
} |
|
306 |
} |
|
307 |
catch(Exception e) { |
|
308 |
e.printStackTrace(); |
|
309 |
} |
|
310 |
return document; |
|
311 |
} |
|
312 |
|
|
313 |
|
|
314 |
} |
|
0 | 315 |
src/edu/ucsb/nceas/metacat/spatial/MetacatSpatialExtents.java | ||
---|---|---|
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: John Harris |
|
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 |
|
|
26 |
package edu.ucsb.nceas.metacat.spatial; |
|
27 |
|
|
28 |
import org.apache.log4j.Logger; |
|
29 |
|
|
30 |
/** |
|
31 |
* Class that reflects a query against Metacat to gatther spatial data -- this |
|
32 |
* could be a text query that returns a spatial dataset or a spatial query that |
|
33 |
* returns a spatially-referenced dataset. |
|
34 |
*/ |
|
35 |
public class MetacatSpatialExtents { |
|
36 |
|
|
37 |
/** true if the document is represented by a single point and |
|
38 |
* false if is represented by a bouding box |
|
39 |
*/ |
|
40 |
private boolean isPoint = true; |
|
41 |
|
|
42 |
/** the coordinates of the bounding box, or of the point **/ |
|
43 |
private float xmin, ymin, xmax, ymax; |
|
44 |
|
|
45 |
/** the docid of the extents **/ |
|
46 |
private String docid; |
|
47 |
|
|
48 |
private static Logger log = Logger.getLogger(MetacatSpatialExtents.class.getName()); |
|
49 |
|
|
50 |
public MetacatSpatialExtents() {} |
|
51 |
|
|
52 |
public MetacatSpatialExtents(String _docid ) { |
|
53 |
setDocid(_docid); |
|
54 |
} |
|
55 |
|
|
56 |
public void setIsPoint(boolean valid) { |
|
57 |
this.isPoint = valid; |
|
58 |
} |
|
59 |
|
|
60 |
public boolean getIsPoint() { |
|
61 |
return this.isPoint; |
|
62 |
} |
|
63 |
|
|
64 |
public void setDocid(String _docid) { |
|
65 |
this.docid = _docid; |
|
66 |
} |
|
67 |
|
|
68 |
public String getDocid() { |
|
69 |
return docid; |
|
70 |
} |
|
71 |
|
|
72 |
public void setXMin(float _xmin) { |
|
73 |
this.xmin = _xmin; |
|
74 |
} |
|
75 |
|
|
76 |
public float getXMin() { return xmin; } |
|
77 |
|
|
78 |
public void setYMin(float _ymin) { |
|
79 |
this.ymin = _ymin; |
|
80 |
} |
|
81 |
|
|
82 |
public float getYMin() { return ymin; } |
|
83 |
|
|
84 |
public void setXMax(float _xmax) { |
|
85 |
this.xmax = _xmax; |
|
86 |
} |
|
87 |
|
|
88 |
public float getXMax() { return xmax; } |
|
89 |
|
|
90 |
public void setYMax(float _ymax) { |
|
91 |
this.ymax = _ymax; |
|
92 |
} |
|
93 |
|
|
94 |
public float getYMax() { return ymax; } |
|
95 |
|
|
96 |
public String toString() { |
|
97 |
return ("\n\n***************************************************************" |
|
98 |
+"\n docid: " + docid + " \n is point: " + isPoint |
|
99 |
+"\n x min: " + getXMin() + " \n x max: " + getXMax() |
|
100 |
+"\n y min: " + getYMin() + " \n y max: " + getYMax() |
|
101 |
+"\n***************************************************************\n"); |
|
102 |
|
|
103 |
} |
|
104 |
|
|
105 |
|
|
106 |
} |
|
0 | 107 |
src/edu/ucsb/nceas/metacat/spatial/MetacatSpatialConstants.java | ||
---|---|---|
1 |
/** |
|
2 |
* '$RCSfile$' |
|
3 |
* Copyright: 2003 Regents of the University of California. |
|
4 |
* |
|
5 |
* Author: John Harris |
|
6 |
* '$Date$' |
|
7 |
* '$Revision$' |
|
8 |
* |
|
9 |
* This program is free software; you can redistribute it and/or modify |
|
10 |
* it under the terms of the GNU General Public License as published by |
|
11 |
* the Free Software Foundation; either version 2 of the License, or |
|
12 |
* (at your option) any later version. |
|
13 |
* |
|
14 |
* This program is distributed in the hope that it will be useful, |
|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 |
* GNU General Public License for more details. |
|
18 |
* |
|
19 |
* You should have received a copy of the GNU General Public License |
|
20 |
* along with this program; if not, write to the Free Software |
|
21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
22 |
*/ |
|
23 |
package edu.ucsb.nceas.metacat.spatial; |
|
24 |
|
|
25 |
public class MetacatSpatialConstants { |
|
26 |
|
|
27 |
public static final float FLOAT_NULL = (float)1e20; |
|
28 |
|
|
29 |
} |
|
0 | 30 |
src/edu/ucsb/nceas/metacat/spatial/QueryFailedException.java | ||
---|---|---|
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: John Harris |
|
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 |
import org.apache.log4j.Logger; |
|
28 |
|
|
29 |
/** |
|
30 |
* Exception thrown when an error occurs trying to query for the |
|
31 |
* list of meeting document |
|
32 |
*/ |
|
33 |
public class QueryFailedException extends Exception { |
|
34 |
|
|
35 |
|
|
36 |
/** |
|
37 |
* Create a new QueryFailedException. |
|
38 |
* |
|
39 |
* @param message The error or warning message. |
|
40 |
*/ |
|
41 |
public QueryFailedException(String message) { |
|
42 |
super(message); |
|
43 |
} |
|
44 |
} |
|
0 | 45 |
src/edu/ucsb/nceas/metacat/spatial/MetacatSpatialDocument.java | ||
---|---|---|
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: John Harris |
|
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 |
import org.apache.log4j.Logger; |
|
28 |
|
|
29 |
/** |
|
30 |
* Class that reflects a Metacat Document in a spatially represented sense. |
|
31 |
*/ |
|
32 |
public class MetacatSpatialDocument { |
|
33 |
|
|
34 |
private static Logger log = Logger.getLogger(MetacatSpatialDocument.class.getName()); |
|
35 |
|
|
36 |
private String docid; |
|
37 |
|
|
38 |
private MetacatSpatialExtents extents; |
|
39 |
|
|
40 |
public MetacatSpatialDocument(String docid) { |
|
41 |
setDocid(docid); |
|
42 |
} |
|
43 |
|
|
44 |
/** |
|
45 |
* function that registers the document with the Spatial system. This is |
|
46 |
* to be called when the document is inserted to the Metacat system. At |
|
47 |
* registration time, this the Metacat Spatial Elements are requeruried, |
|
48 |
* and the spatial database is repopulated |
|
49 |
*/ |
|
50 |
public void registerSpatialDocument() { |
|
51 |
log.info("Dataset " + docid + " is being registered with the metacat spatial system" ); |
|
52 |
} |
|
53 |
|
|
54 |
public void setDocid(String _docid) { |
|
55 |
this.docid = _docid; |
|
56 |
} |
|
57 |
|
|
58 |
public void setExtents(MetacatSpatialExtents _extents) { |
|
59 |
this.extents = _extents; |
|
60 |
} |
|
61 |
|
|
62 |
public float getXMin() { return extents.getXMin(); } |
|
63 |
public float getYMin() { return extents.getYMin(); } |
|
64 |
public float getXMax() { return extents.getXMax(); } |
|
65 |
public float getYMax() { return extents.getYMax(); } |
|
66 |
|
|
67 |
public String getDocid() { return this.docid; } |
|
68 |
|
|
69 |
public boolean getIsPoint() { return extents.getIsPoint(); } |
|
70 |
|
|
71 |
} |
|
0 | 72 |
src/edu/ucsb/nceas/metacat/spatial/SpatialQueryManager.java | ||
---|---|---|
1 |
/** |
|
2 |
* '$RCSfile$' |
|
3 |
* Copyright: 2003 Regents of the University of California. |
|
4 |
* |
|
5 |
* '$Author$' |
|
6 |
* '$Date$' |
|
7 |
* '$Revision$' |
|
8 |
* |
|
9 |
* This program is free software; you can redistribute it and/or modify |
|
10 |
* it under the terms of the GNU General Public License as published by |
|
11 |
* the Free Software Foundation; either version 2 of the License, or |
|
12 |
* (at your option) any later version. |
|
13 |
* |
|
14 |
* This program is distributed in the hope that it will be useful, |
|
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 |
* GNU General Public License for more details. |
|
18 |
* |
|
19 |
* You should have received a copy of the GNU General Public License |
|
20 |
* along with this program; if not, write to the Free Software |
|
21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
22 |
*/ |
|
23 |
package edu.ucsb.nceas.metacat.spatial; |
|
24 |
|
|
25 |
import java.io.File; |
|
26 |
import java.io.FileReader; |
|
27 |
import java.io.PrintWriter; |
|
28 |
import java.io.IOException; |
|
29 |
import java.io.StringReader; |
|
30 |
import java.io.BufferedReader; |
|
31 |
import java.io.Reader; |
|
32 |
import java.io.FileInputStream; |
|
33 |
import java.io.BufferedInputStream; |
|
34 |
import java.util.Enumeration; |
|
35 |
import java.util.Hashtable; |
|
36 |
import java.util.ResourceBundle; |
|
37 |
import java.util.Random; |
|
38 |
import java.util.StringTokenizer; |
|
39 |
import java.util.Properties; |
|
40 |
import java.util.PropertyResourceBundle; |
|
41 |
import java.util.Vector; |
|
42 |
import java.util.Map; |
|
43 |
|
|
44 |
import java.net.MalformedURLException; |
|
45 |
import java.sql.PreparedStatement; |
|
46 |
import java.sql.ResultSet; |
|
47 |
import java.sql.Connection; |
|
48 |
import java.sql.SQLException; |
|
49 |
import java.lang.reflect.*; |
|
50 |
import java.net.*; |
|
51 |
import java.util.zip.*; |
|
52 |
|
|
53 |
import javax.servlet.ServletConfig; |
|
54 |
import javax.servlet.ServletContext; |
|
55 |
import javax.servlet.ServletException; |
|
56 |
import javax.servlet.ServletInputStream; |
|
57 |
import javax.servlet.http.HttpServlet; |
|
58 |
import javax.servlet.http.HttpServletRequest; |
|
59 |
import javax.servlet.http.HttpServletResponse; |
|
60 |
import javax.servlet.http.HttpSession; |
|
61 |
import javax.servlet.http.HttpUtils; |
|
62 |
import javax.servlet.ServletOutputStream; |
|
63 |
|
|
64 |
import edu.ucsb.nceas.metacat.client.Metacat; |
|
65 |
import edu.ucsb.nceas.metacat.client.MetacatFactory; |
|
66 |
import edu.ucsb.nceas.metacat.client.MetacatAuthException; |
|
67 |
import edu.ucsb.nceas.metacat.client.MetacatException; |
|
68 |
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException; |
|
69 |
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException; |
|
70 |
import edu.ucsb.nceas.utilities.IOUtil; |
|
71 |
|
|
72 |
import org.apache.log4j.Logger; |
|
73 |
import org.apache.log4j.BasicConfigurator; |
|
74 |
import org.apache.log4j.PropertyConfigurator; |
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
/** |
|
79 |
* An application that manages the registration of ecological networks using the |
|
80 |
* metacat technology |
|
81 |
*/ |
|
82 |
public class SpatialQueryManager extends HttpServlet |
|
83 |
{ |
|
84 |
|
|
85 |
private ServletConfig config = null; |
|
86 |
private ServletContext context = null; |
|
87 |
private Properties appConfig = null; |
|
88 |
private static final String CONFIG_DIR = "WEB-INF"; |
|
89 |
private static final String CONFIG_NAME = "econet.properties"; |
|
90 |
private static final String STYLE_DIR = "WEB-INF/"; |
|
91 |
private String defaultStyle = null; |
|
92 |
private String metacatUrl = null; |
|
93 |
private String queryName = null; |
|
94 |
private String invalidRegistrationErrorPage = null; |
|
95 |
|
|
96 |
// private EcoNetwork network; |
|
97 |
|
|
98 |
private String mcUser = "uid=jones,o=NCEAS,dc=ecoinformatics,dc=org"; |
|
99 |
private String mcPass = "@mcpass@"; |
|
100 |
|
|
101 |
// private EconetCache cache; |
|
102 |
|
|
103 |
protected SpatialQueryProcessor queryProcessor; |
|
104 |
// this is the key val for the econet summary to be in the cache and |
|
105 |
// it must be the same value as the value in the manager |
|
106 |
// private static String ECONET_SUMMARY_KEY = "econet_summary"; |
|
107 |
|
|
108 |
private static Logger log = Logger.getLogger(SpatialQueryManager.class.getName()); |
|
109 |
|
|
110 |
/** |
|
111 |
* Initialize the servlet during container startup |
|
112 |
*/ |
|
113 |
public void init(ServletConfig config) throws ServletException |
|
114 |
{ |
|
115 |
try |
|
116 |
{ |
|
117 |
super.init(config); |
|
118 |
this.config = config; |
|
119 |
this.context = config.getServletContext(); |
|
120 |
|
|
121 |
//BasicConfigurator.configure(); |
|
122 |
String prefix = getServletContext().getRealPath("/"); |
|
123 |
System.out.println("SpatialQueryManager servlet context path ####################### >>> " + prefix); |
|
124 |
String file = getInitParameter("log4j-init-file"); |
|
125 |
System.out.println("SpatialQueryManager log4j-init-file ####################### >>> " + file); |
|
126 |
System.out.println("SpatialQueryManager econet - metacat acct: ####################### >>> " + mcUser); |
|
127 |
|
|
128 |
// if the log4j-init-file is not set, then no point in trying |
|
129 |
if (file != null) |
|
130 |
{ |
|
131 |
PropertyConfigurator.configure(prefix + file); |
|
132 |
} |
|
133 |
|
|
134 |
try |
|
135 |
{ |
|
136 |
// locate, open, and read the properties |
|
137 |
appConfig = new Properties(); |
|
138 |
String dirPath = context.getRealPath(CONFIG_DIR); |
|
139 |
File propertyFile = new File(dirPath, CONFIG_NAME); |
|
140 |
FileInputStream fis = new FileInputStream(propertyFile); |
|
141 |
appConfig.load(fis); |
|
142 |
fis.close(); |
|
143 |
defaultStyle = (String) appConfig.get("defaultStyle"); |
|
144 |
metacatUrl = (String) appConfig.get("metacatUrl"); |
|
145 |
System.out.println("SpatialQueryManager using metacat at url: ####################### >>> " + metacatUrl); |
|
146 |
queryName = (String)appConfig.get("queryName"); |
|
147 |
System.out.println("SpatialQueryManager query name : ####################### >>> " + queryName); |
|
148 |
invalidRegistrationErrorPage = (String) appConfig.get("invalidRegistrationErrorPage"); |
|
149 |
|
|
150 |
} |
|
151 |
catch(Throwable t) |
|
152 |
{ |
|
153 |
throw new ServletException(t.getMessage()); |
|
154 |
} |
|
155 |
} |
|
156 |
catch(ServletException ex) { |
|
157 |
throw ex; |
|
158 |
} |
|
159 |
} |
|
160 |
|
|
161 |
/** Handle "GET" method requests from HTTP clients */ |
|
162 |
public void doGet(HttpServletRequest request, HttpServletResponse response) |
|
163 |
throws ServletException, IOException |
|
164 |
{ |
|
165 |
handleGetOrPost(request, response); |
|
166 |
} |
|
167 |
|
|
168 |
/** Handle "POST" method requests from HTTP clients */ |
|
169 |
public void doPost(HttpServletRequest request, HttpServletResponse response) |
|
170 |
throws ServletException, IOException |
|
171 |
{ |
|
172 |
handleGetOrPost(request, response); |
|
173 |
} |
|
174 |
|
|
175 |
/** |
|
176 |
* Control servlet response depending on the action parameter specified |
|
177 |
*/ |
|
178 |
private void handleGetOrPost(HttpServletRequest request, HttpServletResponse response) |
|
179 |
throws ServletException, IOException |
|
180 |
{ |
|
181 |
Hashtable params = getParamHash(request); |
|
182 |
String action = getAction(request); |
|
183 |
log.debug(">> action " + action); |
|
184 |
|
|
185 |
if (action.equals("list")) { |
|
186 |
handleListAction(request, response); |
|
187 |
} else if (action.equals("register")) { |
|
188 |
//handleRegisterAction(request, response); |
|
189 |
} else if (action.equals("delete")) { |
|
190 |
handleDeleteAction(request, response); |
|
191 |
} else if (action.equals("displaydoc")) { |
|
192 |
handleDisplayDocAction(request, response); |
|
193 |
} else { |
|
194 |
handleListAction(request, response); |
|
195 |
} |
|
196 |
} |
|
197 |
|
|
198 |
/** |
|
199 |
* Reads a document from metacat and displays the information to the browser |
|
200 |
* @param request -- the HTTP request object |
|
201 |
* @param response -- the HTTP response object |
|
202 |
*/ |
|
203 |
private void handleDisplayDocAction(HttpServletRequest request, HttpServletResponse response) |
|
204 |
throws IOException |
|
205 |
{ |
|
206 |
Hashtable params = getParamHash(request); |
|
207 |
String contextPath = request.getContextPath(); |
|
208 |
String action = getAction(request); |
|
209 |
String styleSystemId = getStyleSystemId(action, defaultStyle); |
|
210 |
String docid = (String) getParamHash(request).get("docId"); |
|
211 |
PrintWriter out = response.getWriter(); |
|
212 |
ServletContext servletContext = getServletContext(); |
|
213 |
response.setContentType ("text/html"); |
|
214 |
if (docid != null && !docid.trim().equals("")) |
|
215 |
{ |
|
216 |
try { |
|
217 |
String doc = null; |
|
218 |
// if ( cache.recover(docid) == null ) { |
|
219 |
log.debug("docid in the cache is null ... going to the db"); |
|
220 |
Metacat m = MetacatFactory.createMetacatConnection(metacatUrl); |
|
221 |
m.login(mcUser, mcPass); |
|
222 |
Reader r = m.read(docid); |
|
223 |
doc = IOUtil.getAsString(r, true); |
|
224 |
r.close(); |
|
225 |
m.logout(); |
|
226 |
// adding this was giving a concurrent modification exception |
|
227 |
//cache.admit(docid, doc); |
|
228 |
// } else { |
|
229 |
// log.debug("docid in the cache is valid"); |
|
230 |
// doc = (String)cache.recover(docid); |
|
231 |
// } |
|
232 |
String dirPath = context.getRealPath(CONFIG_DIR); |
|
233 |
XSLTransform.transform(doc, styleSystemId, out, params); |
|
234 |
out.close(); |
|
235 |
} |
|
236 |
catch(Exception e) { e.printStackTrace(); } |
|
237 |
} else { |
|
238 |
out.println("no such docid!"); |
|
239 |
} |
|
240 |
out.close(); |
|
241 |
} |
|
242 |
|
|
243 |
/** |
|
244 |
* Deletes a network from the registration system. This is called by the |
|
245 |
* administrator to delete a registry from metacat. There is no response to |
|
246 |
* the calling device - instead details are written to the logger |
|
247 |
* @param request -- the HTTP request object |
|
248 |
* @param response -- the HTTP response object |
|
249 |
*/ |
|
250 |
private void handleDeleteAction(HttpServletRequest request, HttpServletResponse response) throws IOException |
|
251 |
{ |
|
252 |
Hashtable params = getParamHash(request); |
|
253 |
String docId = (String)params.get("docId"); |
|
254 |
// for the Metacat.delete request to be called the password needs to be the |
|
255 |
// same as the mcpass var. |
|
256 |
String pass = (String)params.get("password"); |
|
257 |
if ( params.containsKey("password") ) { |
|
258 |
if ( pass.equals(mcPass) ) { |
|
259 |
log.info(">> deleting docid: " + docId); |
|
260 |
// add a lookup for an issued password. |
|
261 |
try |
|
262 |
{ |
|
263 |
Metacat m = MetacatFactory.createMetacatConnection(metacatUrl); |
|
264 |
log.info(mcUser + " " + mcPass); |
|
265 |
m.login(mcUser, mcPass); |
|
266 |
String resp = m.delete(docId); |
|
267 |
log.info(">>deletion success is: " + resp); |
|
268 |
} |
|
269 |
catch(Exception e) { e.printStackTrace(); } |
|
270 |
} |
|
271 |
else { log.fatal("attempt to delete a registry with invalid pass"); } |
|
272 |
} |
|
273 |
else { log.fatal("attempt to delete a registry with NO pass"); } |
|
274 |
} |
|
275 |
|
|
276 |
/** |
|
277 |
* Display the registered networks. This is called when a list of |
|
278 |
* regestered networks is requested for display and a transformation |
|
279 |
* is needed. It queries the registered networks and then transforms the |
|
280 |
* network docs using XSLT and displays the networks. |
|
281 |
* @param request -- the HTTP request object |
|
282 |
* @param response -- the HTTP response object |
|
283 |
*/ |
|
284 |
private void handleListAction(HttpServletRequest request, HttpServletResponse response) throws IOException |
|
285 |
{ |
|
286 |
Hashtable params = getParamHash(request); |
|
287 |
String contextPath = request.getContextPath(); |
|
288 |
String action = getAction(request); |
|
289 |
String styleSystemId = getStyleSystemId(action, defaultStyle); |
|
290 |
|
|
291 |
//String style = (String)pathData.get(1); |
|
292 |
PrintWriter out = response.getWriter(); |
|
293 |
ServletContext servletContext = getServletContext(); |
|
294 |
response.setContentType ("text/html"); |
|
295 |
|
|
296 |
// get the query document |
|
297 |
String dirPath = context.getRealPath(CONFIG_DIR); |
|
298 |
File queryFile = new File(dirPath, queryName); |
|
299 |
// Query for the documents from the server |
|
300 |
String queryResult = null; |
|
301 |
try { |
|
302 |
// if the cache doesnt have the summary then get it from the database |
|
303 |
// if ( cache.recover(ECONET_SUMMARY_KEY) == null ) { |
|
304 |
// log.debug("summary in the cache is null ... going to the db"); |
|
305 |
queryResult = queryProcessor.execute(); |
|
306 |
// cache.admit(ECONET_SUMMARY_KEY, queryResult); |
|
307 |
// } else { |
|
308 |
// log.debug("summary in the cache is valid"); |
|
309 |
// queryResult = (String)cache.recover(ECONET_SUMMARY_KEY); |
|
310 |
// } |
|
311 |
} catch(Exception e) { e.printStackTrace(); } |
|
312 |
|
|
313 |
log.debug("query result >> " + queryResult); |
|
314 |
XSLTransform.transform(queryResult, styleSystemId, out, params); |
|
315 |
out.close(); |
|
316 |
} |
|
317 |
|
|
318 |
|
|
319 |
|
|
320 |
/** |
|
321 |
* Register the network. This is called when the attributes of a network |
|
322 |
* are submitted to the system. |
|
323 |
* |
|
324 |
* @param request -- the HTTP request object |
|
325 |
* @param response -- the HTTP response object |
|
326 |
*/ |
|
327 |
/** private void handleRegisterAction(HttpServletRequest request, HttpServletResponse response) throws IOException |
|
328 |
{ |
|
329 |
Map params = getParamHash(request); |
|
330 |
String contextPath = request.getContextPath(); |
|
331 |
String action = getAction(request); |
|
332 |
|
|
333 |
network = new EcoNetwork((Map) params); |
|
334 |
// add a validation step here |
|
335 |
if (network.isValid() == true) |
|
336 |
{ |
|
337 |
PrintWriter out = response.getWriter(); |
|
338 |
response.setContentType ("text/html"); |
|
339 |
log.info("about to insert: " + network.toXML()); |
|
340 |
String metacatResponse = insertEcoNetwork(network.toXML()); |
|
341 |
log.info("metacat response: " + metacatResponse); |
|
342 |
|
|
343 |
String styleSystemId = getStyleSystemId(action, defaultStyle); |
|
344 |
log.info("registration system id " + styleSystemId); |
|
345 |
XSLTransform.transform(metacatResponse, styleSystemId, out, (Hashtable) params); |
|
346 |
|
|
347 |
// clear the summary from the cache which will be recovered either next |
|
348 |
// time the show all docs function is called or the ttl in the cache is |
|
349 |
// achived. |
|
350 |
cache.clear(); |
|
351 |
|
|
352 |
out.close(); |
Also available in: Unified diff
metacat spatial option code -- code that produces layers depending on the
metacat transaction.