1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Purpose: A Class that implements utility methods like:
|
4
|
* 1/ Reding all doctypes from db connection
|
5
|
* 2/ Reading Lore type Data Guide from db connection
|
6
|
* Copyright: 2000 Regents of the University of California and the
|
7
|
* National Center for Ecological Analysis and Synthesis
|
8
|
* Authors: Jivka Bojilova
|
9
|
*
|
10
|
* '$Author: bojilova $'
|
11
|
* '$Date: 2000-08-03 16:19:41 -0700 (Thu, 03 Aug 2000) $'
|
12
|
* '$Revision: 315 $'
|
13
|
*/
|
14
|
|
15
|
package edu.ucsb.nceas.metacat;
|
16
|
|
17
|
import java.sql.Connection;
|
18
|
import java.sql.SQLException;
|
19
|
import java.sql.PreparedStatement;
|
20
|
import java.sql.ResultSet;
|
21
|
import java.util.Hashtable;
|
22
|
import java.util.Enumeration;
|
23
|
import java.util.Vector;
|
24
|
|
25
|
/**
|
26
|
* A suite of utility classes for quering DB
|
27
|
*/
|
28
|
public class DBUtil {
|
29
|
|
30
|
private Connection conn = null;
|
31
|
|
32
|
/**
|
33
|
* main routine used for testing.
|
34
|
* <p>
|
35
|
* Usage: java DBUtil <-dt|-dg>
|
36
|
*
|
37
|
* @param -dt for selecting all doctypes
|
38
|
* -dg for selecting DataGuide
|
39
|
*/
|
40
|
static public void main(String[] args) {
|
41
|
|
42
|
if (args.length < 1)
|
43
|
{
|
44
|
System.err.println("Wrong number of arguments!!!");
|
45
|
System.err.println("USAGE: java DBUtil <-dt | -dg doctype>");
|
46
|
return;
|
47
|
} else {
|
48
|
try {
|
49
|
|
50
|
// Open a connection to the database
|
51
|
MetaCatUtil util = new MetaCatUtil();
|
52
|
Connection dbconn = util.openDBConnection();
|
53
|
|
54
|
DBUtil dbutil = new DBUtil( dbconn );
|
55
|
|
56
|
if ( args[0].equals("-dt") ) {
|
57
|
String doctypes = dbutil.readDoctypes();
|
58
|
System.out.println(doctypes);
|
59
|
} else if ( args[0].equals("-dg") ) {
|
60
|
String doctype = null;
|
61
|
if ( args.length == 2 ) { doctype = args[1]; }
|
62
|
String dataguide = dbutil.readDataGuide(doctype);
|
63
|
System.out.println(dataguide);
|
64
|
} else {
|
65
|
System.err.println("USAGE: java DBUtil <-dt | -dg doctype>");
|
66
|
}
|
67
|
|
68
|
} catch (Exception e) {
|
69
|
System.err.println("EXCEPTION HANDLING REQUIRED");
|
70
|
System.err.println(e.getMessage());
|
71
|
e.printStackTrace(System.err);
|
72
|
}
|
73
|
}
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* Construct an instance of the utility class
|
78
|
*/
|
79
|
public DBUtil( Connection conn ) {
|
80
|
this.conn = conn;
|
81
|
}
|
82
|
|
83
|
/**
|
84
|
* read all doctypes from db connection in XML format
|
85
|
* select all Public Id from xml_catalog table
|
86
|
*/
|
87
|
public String readDoctypes()
|
88
|
throws SQLException {
|
89
|
|
90
|
Vector doctypeList = new Vector();
|
91
|
|
92
|
try {
|
93
|
|
94
|
PreparedStatement pstmt =
|
95
|
conn.prepareStatement("SELECT public_id FROM xml_catalog");
|
96
|
|
97
|
pstmt.execute();
|
98
|
ResultSet rs = pstmt.getResultSet();
|
99
|
boolean tableHasRows = rs.next();
|
100
|
while (tableHasRows) {
|
101
|
doctypeList.addElement(rs.getString(1));
|
102
|
tableHasRows = rs.next();
|
103
|
}
|
104
|
|
105
|
pstmt.close();
|
106
|
|
107
|
} catch (SQLException e) {
|
108
|
System.out.println("DBUtil.readDoctypes(): " + e.getMessage());
|
109
|
throw e;
|
110
|
}
|
111
|
|
112
|
return formatToXML(doctypeList, "doctype");
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* read Data Guide for a given doctype from db connection in XML format
|
117
|
* select all distinct absolute paths from xml_index table
|
118
|
*/
|
119
|
public String readDataGuide( String doctype )
|
120
|
throws SQLException {
|
121
|
|
122
|
Vector dataguide = new Vector();
|
123
|
String path;
|
124
|
PreparedStatement pstmt;
|
125
|
|
126
|
try {
|
127
|
|
128
|
if ( doctype != null ) {
|
129
|
pstmt = conn.prepareStatement("SELECT distinct path, nodeid " +
|
130
|
"FROM xml_index " +
|
131
|
"WHERE path LIKE '/%' " +
|
132
|
"AND doctype LIKE ? " +
|
133
|
"ORDER BY nodeid");
|
134
|
pstmt.setString(1, doctype);
|
135
|
} else {
|
136
|
pstmt = conn.prepareStatement("SELECT distinct path, nodeid " +
|
137
|
"FROM xml_index " +
|
138
|
"WHERE path LIKE '/%' " +
|
139
|
"ORDER BY nodeid");
|
140
|
}
|
141
|
|
142
|
pstmt.execute();
|
143
|
ResultSet rs = pstmt.getResultSet();
|
144
|
boolean tableHasRows = rs.next();
|
145
|
while (tableHasRows) {
|
146
|
path = rs.getString(1);
|
147
|
if ( dataguide.indexOf(path) == -1 ) {
|
148
|
dataguide.addElement(path);
|
149
|
}
|
150
|
tableHasRows = rs.next();
|
151
|
}
|
152
|
|
153
|
pstmt.close();
|
154
|
|
155
|
} catch (SQLException e) {
|
156
|
System.out.println("DBUtil.readDoctypes(): " + e.getMessage());
|
157
|
throw e;
|
158
|
}
|
159
|
|
160
|
return formatToXML(dataguide, "path");
|
161
|
}
|
162
|
|
163
|
/**
|
164
|
* format the ResultSet to XML
|
165
|
*/
|
166
|
private String formatToXML(Vector resultset, String tag) {
|
167
|
|
168
|
String val = null;
|
169
|
String doctype = null;
|
170
|
StringBuffer result = new StringBuffer();
|
171
|
Enumeration rs = resultset.elements();
|
172
|
|
173
|
result.append("<?xml version=\"1.0\"?>\n");
|
174
|
result.append("<resultset>\n");
|
175
|
while (rs.hasMoreElements()) {
|
176
|
val = (String)rs.nextElement();
|
177
|
result.append(" <" + tag + ">" + val + "</" + tag + ">\n");
|
178
|
}
|
179
|
result.append("</resultset>\n");
|
180
|
|
181
|
return result.toString();
|
182
|
}
|
183
|
|
184
|
|
185
|
}
|