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
public class DBSAXElement {
19
20
    private long		element_id;
21
    private String		tagname;
22
    private StringBuffer	content;
23
    private long		parent_id;
24
    private Hashtable		attributes;
25 16 jones
    private Connection		conn;
26 15 jones
27 18 jones
    public DBSAXElement (Connection conn, String tagname,
28
                         long parent_id) {
29 16 jones
      this.conn = conn;
30
31 15 jones
      this.tagname = tagname;
32
      this.parent_id = parent_id;
33
      content = new StringBuffer();
34
      attributes = new Hashtable();
35 16 jones
      writeElementToDB();
36 15 jones
    };
37
38 19 jones
    /** creates SQL code and inserts new element into DB connection */
39
    private void writeElementToDB() {
40
        try {
41
          conn.setAutoCommit(false);
42
          PreparedStatement pstmt;
43
          if (parent_id != 0) {
44
            pstmt = conn.prepareStatement(
45 20 jones
                "INSERT INTO xml_nodes (nodeid, nodetype, " +
46
                "nodename, parentnodeid) VALUES (null, ?, ?, ?)");
47 19 jones
          } else {
48
            pstmt = conn.prepareStatement(
49 20 jones
                "INSERT INTO xml_nodes (nodeid, nodetype, nodename) " +
50
                "VALUES (null, ?, ?)");
51 19 jones
          }
52
53
          // Bind the values to the query
54 20 jones
          pstmt.setString(1, "ELEMENT");
55
          pstmt.setString(2, getTagName());
56 19 jones
          if (parent_id != 0) {
57 20 jones
            pstmt.setLong(3, parent_id);
58 19 jones
          }
59
          // Do the insertion
60
          pstmt.execute();
61
          pstmt.close();
62
          this.element_id = getAssignedElementID();
63
          conn.commit();
64
          conn.setAutoCommit(true);
65
        } catch (SQLException e) {
66
          System.out.println(e.getMessage());
67
        }
68
    }
69
70
    /** look up the assigned element id from DB connection */
71
    private long getAssignedElementID() {
72
        long assigned_id=0;
73 16 jones
        Statement stmt;
74
        try {
75
          stmt = conn.createStatement();
76 20 jones
          stmt.execute("SELECT xml_nodes_id_seq.currval FROM dual");
77 16 jones
          try {
78
            ResultSet rs = stmt.getResultSet();
79
            try {
80
              boolean tableHasRows = rs.next();
81
              if (tableHasRows) {
82
                try {
83 19 jones
                  assigned_id = rs.getLong(1);
84 16 jones
                } catch (SQLException e) {
85
                  System.out.println("Error with getInt: " + e.getMessage());
86
                }
87
              }
88
            } catch (SQLException e) {
89
              System.out.println("Error with next: " + e.getMessage());
90
            }
91
          } catch (SQLException e) {
92
            System.out.println("Error with getrset: " + e.getMessage());
93
          }
94
          stmt.close();
95
        } catch (SQLException e) {
96
          System.out.println("Error getting id: " + e.getMessage());
97
        }
98
99 19 jones
        // assign the new ID number
100
        return assigned_id;
101 16 jones
    }
102
103 15 jones
    // used by JTree to display this node
104
    public String toString ()
105
    {
106
	StringBuffer	value = new StringBuffer ();
107
	value.append ('<');
108
	value.append (getTagName ());
109
	value.append (getAttributes ().toString ());
110
	value.append ('>');
111
	return value.toString ();
112
    }
113
114 18 jones
    /** Get the id of this element */
115
    public long  getElementID() { return element_id; }
116
117 15 jones
    /** Get the name of this element */
118
    public String  getTagName() { return tagname; }
119
120
    /** Get the attributes as a string */
121
    public String getAttributes() {
122
      StringBuffer buf = new StringBuffer();
123
      String attName = null;
124
      String attValue = null;
125
126
      Enumeration attList = attributes.keys();
127
      while (attList.hasMoreElements()) {
128
        attName = (String)attList.nextElement();
129
        attValue = (String)attributes.get(attName);
130
        buf.append(" ").append(attName).append("=").append(attValue);
131
      }
132
      return buf.toString();
133
    }
134
135
    /** Add a new attribute to this element, or set its value */
136
    public void setAttribute(String attName, String attValue) {
137
      if (attName != null) {
138 19 jones
        // Enter the attribute in the hash table
139 15 jones
        attributes.put(attName, attValue);
140 19 jones
141
        // And enter the attribute in the database
142
        try {
143
          PreparedStatement pstmt = conn.prepareStatement(
144 20 jones
              "INSERT INTO xml_nodes (nodeid, nodetype, " +
145
              "nodename, nodedata, parentnodeid) VALUES (null, ?, ?, ?, ?)");
146 19 jones
147
          // Bind the values to the query
148 20 jones
          pstmt.setString(1, "ATTRIBUTE");
149 19 jones
          pstmt.setString(2, attName);
150
          pstmt.setString(3, attValue);
151 20 jones
          pstmt.setLong(4, getElementID());
152 19 jones
153
          // Do the insertion
154
          pstmt.execute();
155
          pstmt.close();
156
        } catch (SQLException e) {
157
          System.out.println(e.getMessage());
158
        }
159
160 15 jones
      } else {
161
        System.err.println("Attribute name must not be null!");
162
      }
163
    }
164
165
    /** Get an attribute value by name */
166
    public String getAttribute(String attName) {
167
      return (String)attributes.get(attName);
168
    }
169
170
    /** Append to the content of the element */
171
    public void appendContent(char[] cbuf, int start, int len) {
172
      this.content.append( cbuf, start, len );
173
    }
174
175
    /** Append to the content of the element */
176
    public void appendContent(String new_content) {
177
      this.content.append( new_content );
178
    }
179
180
    /** Get the content of the element */
181
    public String getContent() {
182
      return this.content.toString();
183
    }
184 18 jones
185
    /** Write the element content to the db connection */
186
    public void writeContentToDB() {
187
        try {
188
          PreparedStatement pstmt = conn.prepareStatement(
189 20 jones
                "UPDATE xml_nodes SET nodedata = ? WHERE nodeid = ?");
190 18 jones
191
          // Bind the values to the query
192
          pstmt.setString(1, getContent());// The first ? is for NODEDATA
193
          pstmt.setLong(2, element_id); // The second ? is for NODEID
194
195
          // Do the update
196
          pstmt.execute();
197
          pstmt.close();
198
199
        } catch (SQLException e) {
200
          System.out.println(e.getMessage());
201
        }
202
    }
203
204 15 jones
}