1
|
/**
|
2
|
* Name: DBSAXDocument.java
|
3
|
* Purpose: A Class that writes an XML document main data
|
4
|
* Copyright: 2000 Regents of the University of California and the
|
5
|
* National Center for Ecological Analysis and Synthesis
|
6
|
* Authors: Jivka Bojilova
|
7
|
*
|
8
|
* Version: '$Id: DBSAXDocument.java 148 2000-06-12 16:08:35Z bojilova $'
|
9
|
*/
|
10
|
|
11
|
package edu.ucsb.nceas.metacat;
|
12
|
|
13
|
import java.sql.*;
|
14
|
import java.io.IOException;
|
15
|
import java.util.Hashtable;
|
16
|
import java.util.Enumeration;
|
17
|
|
18
|
/**
|
19
|
* A Class that writes an XML document main data to a database connection
|
20
|
*/
|
21
|
public class DBSAXDocument {
|
22
|
|
23
|
private Connection conn;
|
24
|
private long rootnodeid;
|
25
|
private String docname;
|
26
|
private String doctype;
|
27
|
private String doctitle;
|
28
|
private long docid;
|
29
|
|
30
|
/**
|
31
|
* Construct a new document instance
|
32
|
*
|
33
|
* @param conn the JDBC Connection to which all information is written
|
34
|
* @param rootnodeid - sequence id of the root node in the document
|
35
|
* @param docname - the name of DTD, i.e. the name immediately following
|
36
|
* the DOCTYPE keyword ( should be the root element name ) or
|
37
|
* the root element name if no DOCTYPE declaration provided
|
38
|
* (Oracle's and IBM parsers are not aware if it is not the
|
39
|
* root element name)
|
40
|
* @param doctype - Public ID of the DTD, i.e. the name immediately
|
41
|
* following the PUBLIC keyword in DOCTYPE declaration or
|
42
|
* the docname if no Public ID provided or
|
43
|
* null if no DOCTYPE declaration provided
|
44
|
*
|
45
|
*/
|
46
|
public DBSAXDocument (Connection conn, long rootnodeid, String docname,
|
47
|
String doctype)
|
48
|
{
|
49
|
this.conn = conn;
|
50
|
this.rootnodeid = rootnodeid;
|
51
|
this.docname = docname;
|
52
|
this.doctype = doctype;
|
53
|
writeDocumentToDB();
|
54
|
}
|
55
|
|
56
|
/** creates SQL code and inserts new document into DB connection */
|
57
|
private void writeDocumentToDB() {
|
58
|
try {
|
59
|
PreparedStatement pstmt;
|
60
|
pstmt = conn.prepareStatement(
|
61
|
"INSERT INTO xml_documents " +
|
62
|
"(docid, rootnodeid, docname, doctype) " +
|
63
|
"VALUES (null, ?, ?, ?)");
|
64
|
|
65
|
// Bind the values to the query
|
66
|
pstmt.setLong(1, rootnodeid);
|
67
|
pstmt.setString(2, docname);
|
68
|
pstmt.setString(3, doctype);
|
69
|
// Do the insertion
|
70
|
pstmt.execute();
|
71
|
pstmt.close();
|
72
|
|
73
|
long assigned_id=0;
|
74
|
Statement stmt;
|
75
|
stmt = conn.createStatement();
|
76
|
stmt.execute("SELECT xml_documents_id_seq.currval FROM dual");
|
77
|
ResultSet rs = stmt.getResultSet();
|
78
|
boolean tableHasRows = rs.next();
|
79
|
if (tableHasRows) {
|
80
|
assigned_id = rs.getLong(1);
|
81
|
this.docid = assigned_id;
|
82
|
}
|
83
|
stmt.close();
|
84
|
|
85
|
} catch (SQLException e) {
|
86
|
System.out.println(e.getMessage());
|
87
|
}
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* Get the document title
|
92
|
*/
|
93
|
public String getTitle() {
|
94
|
return doctitle;
|
95
|
}
|
96
|
|
97
|
/**
|
98
|
* Set the document title
|
99
|
*
|
100
|
* @param title the new title for the document
|
101
|
*/
|
102
|
public void setTitle( String title ) {
|
103
|
this.doctitle = title;
|
104
|
try {
|
105
|
PreparedStatement pstmt;
|
106
|
pstmt = conn.prepareStatement(
|
107
|
"UPDATE xml_documents " +
|
108
|
" SET doctitle = ? " +
|
109
|
"WHERE docid = ?");
|
110
|
|
111
|
// Bind the values to the query
|
112
|
pstmt.setString(1, doctitle);
|
113
|
pstmt.setLong(2, docid);
|
114
|
|
115
|
// Do the insertion
|
116
|
pstmt.execute();
|
117
|
pstmt.close();
|
118
|
} catch (SQLException e) {
|
119
|
System.out.println(e.getMessage());
|
120
|
}
|
121
|
}
|
122
|
|
123
|
/**
|
124
|
* Look up the title of the first child element named "title"
|
125
|
* and record it as the document title
|
126
|
*/
|
127
|
public void setTitleFromChildElement() {
|
128
|
String title = null;
|
129
|
long assigned_id=0;
|
130
|
PreparedStatement pstmt;
|
131
|
try {
|
132
|
pstmt = conn.prepareStatement(
|
133
|
"SELECT nodedata FROM xml_nodes " +
|
134
|
"WHERE nodetype = 'TEXT' " +
|
135
|
"AND parentnodeid IN " +
|
136
|
"(SELECT nodeid FROM xml_nodes " +
|
137
|
"WHERE nodename = 'title' " +
|
138
|
"START WITH nodeid = ? " +
|
139
|
"CONNECT BY PRIOR nodeid = parentnodeid)");
|
140
|
/*
|
141
|
"SELECT nodeid " +
|
142
|
"FROM xml_nodes " +
|
143
|
"START WITH nodeid = ? " +
|
144
|
"CONNECT BY PRIOR nodeid = parentnodeid " +
|
145
|
"AND PRIOR nodename = 'title' " );
|
146
|
|
147
|
pstmt = conn.prepareStatement("SELECT nodedata " +
|
148
|
"FROM xml_nodes " +
|
149
|
"WHERE nodetype = 'TEXT' " +
|
150
|
" AND nodeid in " +
|
151
|
"(SELECT nodeid FROM xml_nodes " +
|
152
|
" WHERE nodename = 'title' " +
|
153
|
" START WITH nodeid = ? " +
|
154
|
" CONNECT BY PRIOR nodeid = parentnodeid ");
|
155
|
*/
|
156
|
|
157
|
// Bind the values to the query
|
158
|
pstmt.setLong(1, rootnodeid);
|
159
|
|
160
|
pstmt.execute();
|
161
|
ResultSet rs = pstmt.getResultSet();
|
162
|
boolean tableHasRows = rs.next();
|
163
|
if (tableHasRows) {
|
164
|
title = rs.getString(1);
|
165
|
}
|
166
|
pstmt.close();
|
167
|
} catch (SQLException e) {
|
168
|
System.out.println("Error getting id: " + e.getMessage());
|
169
|
}
|
170
|
|
171
|
// assign the new title
|
172
|
this.setTitle(title);
|
173
|
}
|
174
|
}
|