Project

General

Profile

1
/**
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: DBSAXElement.java 22 2000-04-11 20:26:57Z jones $'
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
public class DBSAXElement extends BasicElement {
19

    
20
    private Connection		conn;
21

    
22
    public DBSAXElement (Connection conn, String tagname, 
23
                         long parent_id) {
24

    
25
      super(tagname, parent_id);
26

    
27
      this.conn = conn;
28
      writeElementToDB();
29
    }
30
    
31
    /** 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
                "INSERT INTO xml_nodes (nodeid, nodetype, " +
39
                "nodename, parentnodeid) VALUES (null, ?, ?, ?)");
40
          } else {
41
            pstmt = conn.prepareStatement(
42
                "INSERT INTO xml_nodes (nodeid, nodetype, nodename) " +
43
                "VALUES (null, ?, ?)");
44
          }
45

    
46
          // Bind the values to the query
47
          pstmt.setString(1, "ELEMENT");
48
          pstmt.setString(2, getTagName());
49
          if (parent_id != 0) {
50
            pstmt.setLong(3, parent_id);
51
          }
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
        Statement stmt;
67
        try {
68
          stmt = conn.createStatement();
69
          stmt.execute("SELECT xml_nodes_id_seq.currval FROM dual");
70
          try {
71
            ResultSet rs = stmt.getResultSet();
72
            try {
73
              boolean tableHasRows = rs.next();
74
              if (tableHasRows) {
75
                try {
76
                  assigned_id = rs.getLong(1);
77
                } 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
        // assign the new ID number
93
        return assigned_id;
94
    }
95

    
96
    /** Add a new attribute to this element, or set its value */
97
    public void setAttribute(String attName, String attValue) {
98
      if (attName != null) {
99
        // Enter the attribute in the hash table
100
        attributes.put(attName, attValue);
101
 
102
        // And enter the attribute in the database
103
        try {
104
          PreparedStatement pstmt = conn.prepareStatement(
105
              "INSERT INTO xml_nodes (nodeid, nodetype, " +
106
              "nodename, nodedata, parentnodeid) VALUES (null, ?, ?, ?, ?)");
107

    
108
          // Bind the values to the query
109
          pstmt.setString(1, "ATTRIBUTE");
110
          pstmt.setString(2, attName);
111
          pstmt.setString(3, attValue);
112
          pstmt.setLong(4, getElementID());
113
 
114
          // Do the insertion
115
          pstmt.execute();
116
          pstmt.close();
117
        } catch (SQLException e) {
118
          System.out.println(e.getMessage());
119
        }
120

    
121
      } else {
122
        System.err.println("Attribute name must not be null!");
123
      }
124
    }
125

    
126
    /** Write the element content to the db connection */
127
    public void writeContentToDB() {
128
        try {
129
          PreparedStatement pstmt = conn.prepareStatement(
130
                "UPDATE xml_nodes SET nodedata = ? WHERE nodeid = ?");
131

    
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
}
(5-5/13)