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
 *   Copyright: 2000 Regents of the University of California and the
6
 *              National Center for Ecological Analysis and Synthesis
7
 *     Authors: Matt Jones
8
 *
9
 *     Version: '$Id: DBElement.java 52 2000-04-17 23:29:00Z jones $'
10
 */
11

    
12
import com.sun.xml.tree.ElementNode;
13

    
14
import org.w3c.dom.DOMException;
15
import org.w3c.dom.Element;
16
import org.w3c.dom.Attr;
17
import org.w3c.dom.Node;
18
import org.w3c.dom.NodeList;
19
import org.w3c.dom.NamedNodeMap;
20
import org.w3c.dom.ProcessingInstruction;
21
import org.w3c.dom.Text;
22

    
23
import java.sql.*;
24
import java.io.IOException;
25

    
26

    
27
public class DBElement extends ElementNode {
28

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

    
40
        int maxid=0;
41
        int newid=0;
42
        Statement stmt;
43
        try {
44
          stmt = conn.createStatement();
45
          stmt.execute("SELECT MAX(nodeid) FROM xml_elements");
46
          try {
47
            ResultSet rs = stmt.getResultSet();
48
            try {
49
              boolean tableHasRows = rs.next();
50
              if (tableHasRows) {
51
                try {
52
                  maxid = rs.getInt(1);
53
                } catch (SQLException e) {
54
                  System.out.println("Error with getInt: " + e.getMessage());
55
                }
56
              }
57
            } catch (SQLException e) {
58
              System.out.println("Error with next: " + e.getMessage());
59
            }
60
          } catch (SQLException e) {
61
            System.out.println("Error with getrset: " + e.getMessage());
62
          }
63
          stmt.close();
64
        } catch (SQLException e) {
65
          System.out.println("Error getting id: " + e.getMessage());
66
        }
67
        
68
        // assign a new ID number
69
        newid = maxid + 1;
70

    
71
        try {
72
          PreparedStatement pstmt;
73
          if (parentID != 0) {
74
            pstmt = conn.prepareStatement(
75
                "INSERT INTO xml_elements(nodeid, nodename, parentnodeid) " + 
76
		"VALUES (?, ?, ?)");
77
          } else {
78
            pstmt = conn.prepareStatement(
79
                "INSERT INTO xml_elements(nodeid, nodename) " + 
80
		"VALUES (?, ?)");
81
          }
82

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

    
93
        } catch (SQLException e) {
94
          System.out.println(e.getMessage());
95
        }
96

    
97
        System.out.println("ID: " + newid + "\tTag: " + getTagName());
98

    
99
        // Now save this node's attributes
100
	writeAttributesToDB(conn, newid);
101

    
102
        // Now save this node's children
103
	if (hasChildNodes()) {
104
            try {
105
	      writeChildrenXmlToDB(conn, newid);
106
            } catch (SQLException e) {
107
              System.out.println(e.getMessage());
108
            }
109
        }
110

    
111
    }
112

    
113
    public void writeChildrenXmlToDB(Connection conn, int parentID) 
114
		throws IOException, SQLException {
115

    
116
      NodeList children = getChildNodes();
117
      if (children != null) {
118
        for (int i = 0; i < children.getLength(); i++) {
119
          if (children.item(i) instanceof ElementNode) {
120
            ((DBElement)children.item(i)).writeXmlToDB(conn, parentID);
121
          } else if (children.item(i) instanceof Text) {
122
             // Write the text to the now created parent node
123
             PreparedStatement pstmt = conn.prepareStatement(
124
                "UPDATE xml_elements SET nodedata = ? " + 
125
		"WHERE nodeid = ?");
126
             pstmt.setString(1, ((Text)children.item(i)).getNodeValue()); 
127
             pstmt.setInt(2, parentID); 
128
             pstmt.execute();
129
             pstmt.close();
130
          } else {
131
            System.out.println("    Other Node Type skipped.");
132
          }
133
        }
134
      }
135
    }
136

    
137
    /**
138
     * Writes this element's attributes out to the 
139
     * given database connection 
140
     */
141
    public void writeAttributesToDB(Connection conn, int nodeid) 
142
		throws IOException {
143
        NamedNodeMap attributes = getAttributes();
144

    
145
        if (attributes != null) {
146
            for (int i=0; i < attributes.getLength(); i++) {
147
               String name = ((Attr)attributes.item(i)).getName();
148
               String value = ((Attr)attributes.item(i)).getValue();
149
               System.out.println("    Saving attribute: " + name + "=" + value);
150

    
151
               int maxid=0;
152
               int newid=0;
153
               Statement stmt;
154
               try {
155
                 stmt = conn.createStatement();
156
                 stmt.execute("SELECT MAX(attributeid) FROM xml_attributes");
157
                 try {
158
                   ResultSet rs = stmt.getResultSet();
159
                   try {
160
                     boolean tableHasRows = rs.next();
161
                     if (tableHasRows) {
162
                       try {
163
                         maxid = rs.getInt(1);
164
                       } catch (SQLException e) {
165
                         System.out.println("Error with getInt: " + e.getMessage());
166
                       }
167
              }
168
                   } catch (SQLException e) {
169
                     System.out.println("Error with next: " + e.getMessage());
170
                   }
171
                 } catch (SQLException e) {
172
                   System.out.println("Error with getrset: " + e.getMessage());
173
                 }
174
                 stmt.close();
175
               } catch (SQLException e) {
176
                 System.out.println("Error getting id: " + e.getMessage());
177
               }
178
        
179
               // assign a new ID number
180
               newid = maxid + 1;
181
       
182
               try {
183
                 PreparedStatement pstmt;
184
                 pstmt = conn.prepareStatement(
185
                  "INSERT INTO xml_attributes(attributeid, nodeid, " +
186
                  "attributenumber, attributename, attributevalue) " +
187
		   "VALUES (?, ?, ?, ?, ?)");
188
       
189
                 // Bind the values to the query
190
                 pstmt.setInt(1, newid); 		// ATTRIBUTEID
191
                 pstmt.setInt(2, nodeid);		// NODEID
192
                 pstmt.setInt(3, i+1); 			// ATTRIBUTENUMBER
193
                 pstmt.setString(4, name);		// ATTRIBUTENAME
194
                 pstmt.setString(5, value);		// ATTRIBUTEVALUE
195

    
196
                 // Do the insertion
197
                 pstmt.execute();
198
                 pstmt.close();
199
       
200
               } catch (SQLException e) {
201
                 System.out.println(e.getMessage());
202
               }
203
            }
204
        }
205
    }
206

    
207
    // used by JTree to display this node
208
    public String toString ()
209
    {
210
	StringBuffer	value = new StringBuffer ();
211
	value.append ('<');
212
	value.append (getTagName ());
213
	value.append (getAttributes ().toString ());
214
	value.append ('>');
215
	return value.toString ();
216
    }
217
}
(3-3/27)