Project

General

Profile

1 6 jones
/**
2
 *        Name: DBElement.java
3
 *     Purpose: A Class that represents an XML element and can
4
 *		write its contents to a database connection
5
 * Institution: National Center for Ecological Analysis and Synthesis
6
 *   Copyright: 1998
7
 *     Authors: Matt Jones
8
 *
9
 *     Version: '$Id$'
10
 */
11
12
//package project;
13
14
import com.sun.xml.tree.ElementNode;
15
16
import org.w3c.dom.DOMException;
17
import org.w3c.dom.Element;
18 13 jones
import org.w3c.dom.Attr;
19 6 jones
import org.w3c.dom.Node;
20 8 jones
import org.w3c.dom.NodeList;
21 13 jones
import org.w3c.dom.NamedNodeMap;
22 6 jones
import org.w3c.dom.ProcessingInstruction;
23
import org.w3c.dom.Text;
24
25 8 jones
import java.sql.*;
26
import java.io.IOException;
27 6 jones
28 8 jones
29
public class DBElement extends ElementNode {
30
31 6 jones
    public DBElement () {
32
      super();
33
    };
34
35 8 jones
    /**
36
     * Writes this element and all of its children out to the
37
     * given database connection
38
     */
39 11 jones
    public void writeXmlToDB(Connection conn, int parentID)
40
		throws IOException {
41 13 jones
42 8 jones
        int maxid=0;
43 11 jones
        int newid=0;
44 8 jones
        Statement stmt;
45
        try {
46
          stmt = conn.createStatement();
47
          stmt.execute("SELECT MAX(nodeid) FROM xml_elements");
48
          try {
49
            ResultSet rs = stmt.getResultSet();
50
            try {
51
              boolean tableHasRows = rs.next();
52
              if (tableHasRows) {
53
                try {
54
                  maxid = rs.getInt(1);
55
                } catch (SQLException e) {
56
                  System.out.println("Error with getInt: " + e.getMessage());
57
                }
58
              }
59
            } catch (SQLException e) {
60
              System.out.println("Error with next: " + e.getMessage());
61
            }
62
          } catch (SQLException e) {
63
            System.out.println("Error with getrset: " + e.getMessage());
64
          }
65
          stmt.close();
66
        } catch (SQLException e) {
67
          System.out.println("Error getting id: " + e.getMessage());
68
        }
69 11 jones
70
        // assign a new ID number
71
        newid = maxid + 1;
72 8 jones
73
        try {
74 11 jones
          PreparedStatement pstmt;
75
          if (parentID != 0) {
76
            pstmt = conn.prepareStatement(
77
                "INSERT INTO xml_elements(nodeid, nodename, parentnodeid) " +
78
		"VALUES (?, ?, ?)");
79
          } else {
80
            pstmt = conn.prepareStatement(
81
                "INSERT INTO xml_elements(nodeid, nodename) " +
82
		"VALUES (?, ?)");
83
          }
84 8 jones
85 11 jones
          // Bind the values to the query
86
          pstmt.setInt(1, newid); // The first ? is for NODEID
87 8 jones
          pstmt.setString(2, getTagName());// The second ? is for NODENAME
88 11 jones
          if (parentID != 0) {
89
            pstmt.setInt(3, parentID);
90
          }
91 8 jones
          // Do the insertion
92
          pstmt.execute();
93
          pstmt.close();
94
95
        } catch (SQLException e) {
96
          System.out.println(e.getMessage());
97
        }
98
99 13 jones
        System.out.println("ID: " + newid + "\tTag: " + getTagName());
100
101
        // Now save this node's attributes
102
	writeAttributesToDB(conn, newid);
103
104
        // Now save this node's children
105
	if (hasChildNodes()) {
106 12 jones
            try {
107
	      writeChildrenXmlToDB(conn, newid);
108
            } catch (SQLException e) {
109
              System.out.println(e.getMessage());
110
            }
111 13 jones
        }
112 8 jones
113 6 jones
    }
114
115 11 jones
    public void writeChildrenXmlToDB(Connection conn, int parentID)
116 12 jones
		throws IOException, SQLException {
117 8 jones
118
      NodeList children = getChildNodes();
119
      if (children != null) {
120
        for (int i = 0; i < children.getLength(); i++) {
121
          if (children.item(i) instanceof ElementNode) {
122 11 jones
            ((DBElement)children.item(i)).writeXmlToDB(conn, parentID);
123 12 jones
          } else if (children.item(i) instanceof Text) {
124
             // Write the text to the now created parent node
125
             PreparedStatement pstmt = conn.prepareStatement(
126
                "UPDATE xml_elements SET nodedata = ? " +
127
		"WHERE nodeid = ?");
128
             pstmt.setString(1, ((Text)children.item(i)).getNodeValue());
129
             pstmt.setInt(2, parentID);
130
             pstmt.execute();
131
             pstmt.close();
132 8 jones
          } else {
133 12 jones
            System.out.println("    Other Node Type skipped.");
134 8 jones
          }
135
        }
136
      }
137
    }
138
139 13 jones
    /**
140
     * Writes this element's attributes out to the
141
     * given database connection
142
     */
143
    public void writeAttributesToDB(Connection conn, int nodeid)
144
		throws IOException {
145
        NamedNodeMap attributes = getAttributes();
146
147
        if (attributes != null) {
148
            for (int i=0; i < attributes.getLength(); i++) {
149
               String name = ((Attr)attributes.item(i)).getName();
150
               String value = ((Attr)attributes.item(i)).getValue();
151
               System.out.println("    Saving attribute: " + name + "=" + value);
152
153
               int maxid=0;
154
               int newid=0;
155
               Statement stmt;
156
               try {
157
                 stmt = conn.createStatement();
158
                 stmt.execute("SELECT MAX(attributeid) FROM xml_attributes");
159
                 try {
160
                   ResultSet rs = stmt.getResultSet();
161
                   try {
162
                     boolean tableHasRows = rs.next();
163
                     if (tableHasRows) {
164
                       try {
165
                         maxid = rs.getInt(1);
166
                       } catch (SQLException e) {
167
                         System.out.println("Error with getInt: " + e.getMessage());
168
                       }
169
              }
170
                   } catch (SQLException e) {
171
                     System.out.println("Error with next: " + e.getMessage());
172
                   }
173
                 } catch (SQLException e) {
174
                   System.out.println("Error with getrset: " + e.getMessage());
175
                 }
176
                 stmt.close();
177
               } catch (SQLException e) {
178
                 System.out.println("Error getting id: " + e.getMessage());
179
               }
180
181
               // assign a new ID number
182
               newid = maxid + 1;
183
184
               try {
185
                 PreparedStatement pstmt;
186
                 pstmt = conn.prepareStatement(
187
                  "INSERT INTO xml_attributes(attributeid, nodeid, " +
188
                  "attributenumber, attributename, attributevalue) " +
189
		   "VALUES (?, ?, ?, ?, ?)");
190
191
                 // Bind the values to the query
192
                 pstmt.setInt(1, newid); 		// ATTRIBUTEID
193
                 pstmt.setInt(2, nodeid);		// NODEID
194
                 pstmt.setInt(3, i+1); 			// ATTRIBUTENUMBER
195
                 pstmt.setString(4, name);		// ATTRIBUTENAME
196
                 pstmt.setString(5, value);		// ATTRIBUTEVALUE
197
198
                 // Do the insertion
199
                 pstmt.execute();
200
                 pstmt.close();
201
202
               } catch (SQLException e) {
203
                 System.out.println(e.getMessage());
204
               }
205
            }
206
        }
207
    }
208
209 6 jones
    // used by JTree to display this node
210
    public String toString ()
211
    {
212
	StringBuffer	value = new StringBuffer ();
213
	value.append ('<');
214
	value.append (getTagName ());
215
	value.append (getAttributes ().toString ());
216
	value.append ('>');
217
	return value.toString ();
218
    }
219
}