Project

General

Profile

1 15 jones
/**
2
 *        Name: DBSAXElement.java
3
 *     Purpose: A Class that represents an XML element and its contents
4
 * Institution: National Center for Ecological Analysis and Synthesis
5
 *   Copyright: 2000
6
 *     Authors: Matt Jones
7
 *
8
 *     Version: '$Id$'
9
 */
10
11
//package project;
12
13
import java.sql.*;
14
import java.io.IOException;
15
import java.util.Hashtable;
16
import java.util.Enumeration;
17
18 21 jones
public class DBSAXElement extends BasicElement {
19 15 jones
20 16 jones
    private Connection		conn;
21 15 jones
22 18 jones
    public DBSAXElement (Connection conn, String tagname,
23
                         long parent_id) {
24 21 jones
25
      super(tagname, parent_id);
26
27 16 jones
      this.conn = conn;
28
      writeElementToDB();
29 22 jones
    }
30 15 jones
31 19 jones
    /** creates SQL code and inserts new element into DB connection */
32
    private void writeElementToDB() {
33
        try {
34
          conn.setAutoCommit(false);
35
          PreparedStatement pstmt;
36
          if (parent_id != 0) {
37
            pstmt = conn.prepareStatement(
38 20 jones
                "INSERT INTO xml_nodes (nodeid, nodetype, " +
39
                "nodename, parentnodeid) VALUES (null, ?, ?, ?)");
40 19 jones
          } else {
41
            pstmt = conn.prepareStatement(
42 20 jones
                "INSERT INTO xml_nodes (nodeid, nodetype, nodename) " +
43
                "VALUES (null, ?, ?)");
44 19 jones
          }
45
46
          // Bind the values to the query
47 20 jones
          pstmt.setString(1, "ELEMENT");
48
          pstmt.setString(2, getTagName());
49 19 jones
          if (parent_id != 0) {
50 20 jones
            pstmt.setLong(3, parent_id);
51 19 jones
          }
52
          // Do the insertion
53
          pstmt.execute();
54
          pstmt.close();
55
          this.element_id = getAssignedElementID();
56
          conn.commit();
57
          conn.setAutoCommit(true);
58
        } catch (SQLException e) {
59
          System.out.println(e.getMessage());
60
        }
61
    }
62
63
    /** look up the assigned element id from DB connection */
64
    private long getAssignedElementID() {
65
        long assigned_id=0;
66 16 jones
        Statement stmt;
67
        try {
68
          stmt = conn.createStatement();
69 20 jones
          stmt.execute("SELECT xml_nodes_id_seq.currval FROM dual");
70 16 jones
          try {
71
            ResultSet rs = stmt.getResultSet();
72
            try {
73
              boolean tableHasRows = rs.next();
74
              if (tableHasRows) {
75
                try {
76 19 jones
                  assigned_id = rs.getLong(1);
77 16 jones
                } catch (SQLException e) {
78
                  System.out.println("Error with getInt: " + e.getMessage());
79
                }
80
              }
81
            } catch (SQLException e) {
82
              System.out.println("Error with next: " + e.getMessage());
83
            }
84
          } catch (SQLException e) {
85
            System.out.println("Error with getrset: " + e.getMessage());
86
          }
87
          stmt.close();
88
        } catch (SQLException e) {
89
          System.out.println("Error getting id: " + e.getMessage());
90
        }
91
92 19 jones
        // assign the new ID number
93
        return assigned_id;
94 16 jones
    }
95
96 15 jones
    /** Add a new attribute to this element, or set its value */
97
    public void setAttribute(String attName, String attValue) {
98
      if (attName != null) {
99 19 jones
        // Enter the attribute in the hash table
100 15 jones
        attributes.put(attName, attValue);
101 19 jones
102
        // And enter the attribute in the database
103
        try {
104
          PreparedStatement pstmt = conn.prepareStatement(
105 20 jones
              "INSERT INTO xml_nodes (nodeid, nodetype, " +
106
              "nodename, nodedata, parentnodeid) VALUES (null, ?, ?, ?, ?)");
107 19 jones
108
          // Bind the values to the query
109 20 jones
          pstmt.setString(1, "ATTRIBUTE");
110 19 jones
          pstmt.setString(2, attName);
111
          pstmt.setString(3, attValue);
112 20 jones
          pstmt.setLong(4, getElementID());
113 19 jones
114
          // Do the insertion
115
          pstmt.execute();
116
          pstmt.close();
117
        } catch (SQLException e) {
118
          System.out.println(e.getMessage());
119
        }
120
121 15 jones
      } else {
122
        System.err.println("Attribute name must not be null!");
123
      }
124
    }
125
126 18 jones
    /** Write the element content to the db connection */
127
    public void writeContentToDB() {
128
        try {
129
          PreparedStatement pstmt = conn.prepareStatement(
130 20 jones
                "UPDATE xml_nodes SET nodedata = ? WHERE nodeid = ?");
131 18 jones
132
          // Bind the values to the query
133
          pstmt.setString(1, getContent());// The first ? is for NODEDATA
134
          pstmt.setLong(2, element_id); // The second ? is for NODEID
135
136
          // Do the update
137
          pstmt.execute();
138
          pstmt.close();
139
140
        } catch (SQLException e) {
141
          System.out.println(e.getMessage());
142
        }
143
    }
144 15 jones
}