Project

General

Profile

1
/**
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: DBElement.java 13 1999-09-19 23:01:18Z jones $'
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
import org.w3c.dom.Attr;
19
import org.w3c.dom.Node;
20
import org.w3c.dom.NodeList;
21
import org.w3c.dom.NamedNodeMap;
22
import org.w3c.dom.ProcessingInstruction;
23
import org.w3c.dom.Text;
24

    
25
import java.sql.*;
26
import java.io.IOException;
27

    
28

    
29
public class DBElement extends ElementNode {
30

    
31
    public DBElement () {
32
      super();
33
    };
34
    
35
    /**
36
     * Writes this element and all of its children out to the 
37
     * given database connection 
38
     */
39
    public void writeXmlToDB(Connection conn, int parentID) 
40
		throws IOException {
41

    
42
        int maxid=0;
43
        int newid=0;
44
        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
        
70
        // assign a new ID number
71
        newid = maxid + 1;
72

    
73
        try {
74
          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

    
85
          // Bind the values to the query
86
          pstmt.setInt(1, newid); // The first ? is for NODEID
87
          pstmt.setString(2, getTagName());// The second ? is for NODENAME
88
          if (parentID != 0) {
89
            pstmt.setInt(3, parentID);
90
          }
91
          // Do the insertion
92
          pstmt.execute();
93
          pstmt.close();
94

    
95
        } catch (SQLException e) {
96
          System.out.println(e.getMessage());
97
        }
98

    
99
        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
            try {
107
	      writeChildrenXmlToDB(conn, newid);
108
            } catch (SQLException e) {
109
              System.out.println(e.getMessage());
110
            }
111
        }
112

    
113
    }
114

    
115
    public void writeChildrenXmlToDB(Connection conn, int parentID) 
116
		throws IOException, SQLException {
117

    
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
            ((DBElement)children.item(i)).writeXmlToDB(conn, parentID);
123
          } 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
          } else {
133
            System.out.println("    Other Node Type skipped.");
134
          }
135
        }
136
      }
137
    }
138

    
139
    /**
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
    // 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
}
(2-2/12)