Project

General

Profile

« Previous | Next » 

Revision 2358

Added by sgarg almost 20 years ago

Modified 'insert xml_nodes...' so that numerical data is added into numericalnodedata also.

View differences:

src/edu/ucsb/nceas/metacat/DBSAXNode.java
36 36

  
37 37
import edu.ucsb.nceas.dbadapter.AbstractDatabase;
38 38

  
39
/** 
39
/**
40 40
 * A Class that represents an XML node and its contents and
41 41
 * can write its own representation to a database connection
42 42
 */
......
46 46
  private DBSAXNode	parentNode;
47 47
  private static final AbstractDatabase dbAdapter = MetaCatUtil.dbAdapter;
48 48

  
49
  /** 
49
  /**
50 50
   * Construct a new node instance for DOCUMENT nodes
51 51
   *
52 52
   * @param conn the JDBC Connection to which all information is written
......
60 60
    updateRootNodeID(getNodeID());
61 61
  }
62 62

  
63
  /** 
63
  /**
64 64
   * Construct a new node instance for ELEMENT nodes
65 65
   *
66 66
   * @param conn the JDBC Connection to which all information is written
......
68 68
   * @param parentNode the parent node for this node being created
69 69
   */
70 70
  public DBSAXNode (DBConnection conn, String qName, String lName,
71
                    DBSAXNode parentNode, long rootnodeid, 
72
                    String docid, String doctype) 
71
                    DBSAXNode parentNode, long rootnodeid,
72
                    String docid, String doctype)
73 73
                                               throws SAXException {
74 74

  
75 75
    super(lName);
......
83 83
    //No writing XML Index from here. New Thread used instead.
84 84
    //updateNodeIndex(docid, doctype);
85 85
  }
86
  
87
  /** 
86

  
87
  /**
88 88
   * Construct a new node instance for DTD nodes
89 89
   * This Node will write docname, publicId and systemId into db. Only
90 90
   * handle systemid  existed.(external dtd)
......
93 93
   * @param tagname the name of the node
94 94
   * @param parentNode the parent node for this node being created
95 95
   */
96
  public DBSAXNode (DBConnection conn, String docName, String publicId, 
97
                    String systemId, DBSAXNode parentNode, long rootnodeid, 
98
                    String docid) throws SAXException 
96
  public DBSAXNode (DBConnection conn, String docName, String publicId,
97
                    String systemId, DBSAXNode parentNode, long rootnodeid,
98
                    String docid) throws SAXException
99 99
  {
100 100

  
101 101
    super();
......
124 124
      writeDTDNodeToDB(DocumentImpl.SYSTEMID, systemId, docid);
125 125
    }
126 126
  }
127
    
127

  
128 128
  /** creates SQL code and inserts new node into DB connection */
129 129
  public long writeChildNodeToDB(String nodetype, String nodename,
130
                                 String data, String docid) 
131
                                 throws SAXException 
130
                                 String data, String docid)
131
                                 throws SAXException
132 132
  {
133 133
    long nid = -1;
134
    try 
134
    try
135 135
    {
136
 
136

  
137 137
      PreparedStatement pstmt;
138

  
138 139
      if (nodetype == "DOCUMENT") {
139 140
        pstmt = connection.prepareStatement(
140 141
            "INSERT INTO xml_nodes " +
141 142
            "(nodetype, nodename, nodeprefix, docid) " +
142 143
            "VALUES (?, ?, ?, ?)");
143
        
144

  
144 145
        // Increase DBConnection usage count
145 146
        connection.increaseUsageCount(1);
146 147
        MetaCatUtil.debugMessage("INSERTING DOCNAME: " + nodename, 35);
147 148
      } else {
148
        pstmt = connection.prepareStatement(
149
            "INSERT INTO xml_nodes " +
150
            "(nodetype, nodename, nodeprefix, docid, " +
151
            "rootnodeid, parentnodeid, nodedata, nodeindex) " +
152
            "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
149
          if(data != null && !data.trim().equals("")){
150
              try{
151
                  double numberData = Double.parseDouble(data);
152
                  pstmt = connection.prepareStatement(
153
                      "INSERT INTO xml_nodes " +
154
                      "(nodetype, nodename, nodeprefix, docid, " +
155
                      "rootnodeid, parentnodeid, nodedata, nodeindex, nodedatanumerical) " +
156
                      "VALUES (?, ?, ?, ?, ?, ?, ?, ?, "+ numberData +")");
157
              } catch (NumberFormatException nfe) {
158
                  pstmt = connection.prepareStatement(
159
                      "INSERT INTO xml_nodes " +
160
                      "(nodetype, nodename, nodeprefix, docid, " +
161
                      "rootnodeid, parentnodeid, nodedata, nodeindex) " +
162
                      "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
163
              }
164
          } else {
165
              pstmt = connection.prepareStatement(
166
                  "INSERT INTO xml_nodes " +
167
                  "(nodetype, nodename, nodeprefix, docid, " +
168
                  "rootnodeid, parentnodeid, nodedata, nodeindex) " +
169
                  "VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
170
          }
153 171
        // Increase DBConnection usage count
154 172
        connection.increaseUsageCount(1);
155 173
      }
......
188 206
      // get the generated unique id afterward
189 207
      nid = dbAdapter.getUniqueID(connection.getConnections(), "xml_nodes");
190 208
      //should incease connection usage!!!!!!
191
     
192
      
209

  
210

  
193 211
      if (nodetype.equals("DOCUMENT")) {
194 212
        // Record the root node id that was generated from the database
195 213
        setRootNodeID(nid);
......
208 226
    } catch (SQLException e) {
209 227
      System.out.println("Error in DBSaxNode.writeChildNodeToDB");
210 228
      System.err.println("Error inserting node: (" + nodetype + ", " +
211
                                                     nodename + ", " + 
229
                                                     nodename + ", " +
212 230
                                                     data + ")" );
213 231
      System.err.println(e.getMessage());
214 232
      e.printStackTrace(System.err);
......
217 235
    return nid;
218 236
  }
219 237

  
220
  /** 
238
  /**
221 239
   * update rootnodeid=nodeid for 'DOCUMENT' type of nodes only
222 240
   */
223 241
  public void updateRootNodeID(long nodeid) throws SAXException {
......
236 254
        pstmt.execute();
237 255
        pstmt.close();
238 256
      } catch (SQLException e) {
239
        System.out.println("Error in DBSaxNode.updateRootNodeID: " + 
257
        System.out.println("Error in DBSaxNode.updateRootNodeID: " +
240 258
                           e.getMessage());
241 259
        throw new SAXException(e.getMessage());
242 260
      }
243 261
  }
244 262

  
245
  /** 
246
   * creates SQL code to put nodename for the document node 
247
   * into DB connection 
263
  /**
264
   * creates SQL code to put nodename for the document node
265
   * into DB connection
248 266
   */
249 267
  public void writeNodename(String nodename) throws SAXException {
250 268
      try {
......
262 280
        pstmt.execute();
263 281
        pstmt.close();
264 282
      } catch (SQLException e) {
265
        System.out.println("Error in DBSaxNode.writeNodeName: " + 
283
        System.out.println("Error in DBSaxNode.writeNodeName: " +
266 284
                           e.getMessage());
267 285
        throw new SAXException(e.getMessage());
268 286
      }
269 287
  }
270 288

  
271 289
 /** creates SQL code and inserts new node into DB connection */
272
  public long writeDTDNodeToDB(String nodename, String data, String docid) 
273
                                 throws SAXException 
290
  public long writeDTDNodeToDB(String nodename, String data, String docid)
291
                                 throws SAXException
274 292
  {
275 293
    long nid = -1;
276
    try 
294
    try
277 295
    {
278
 
296

  
279 297
      PreparedStatement pstmt;
280 298
      MetaCatUtil.debugMessage("Insert dtd into db: "+nodename +" "+data, 45);
281
      pstmt = connection.prepareStatement(
282
            "INSERT INTO xml_nodes " +
283
            "(nodetype, nodename, docid, " +
284
            "rootnodeid, parentnodeid, nodedata, nodeindex) " +
285
            "VALUES (?, ?, ?, ?, ?, ?, ?)");
299
      if(data != null && !data.trim().equals("")){
300
            try{
301
                double numberData = Double.parseDouble(data);
302
                pstmt = connection.prepareStatement(
303
                    "INSERT INTO xml_nodes " +
304
                    "(nodetype, nodename, docid, " +
305
                    "rootnodeid, parentnodeid, nodedata, nodeindex, nodedatanumerical) " +
306
                    "VALUES (?, ?, ?, ?, ?, ?, ?, "+ numberData +")");
307
            } catch (NumberFormatException nfe) {
308
                pstmt = connection.prepareStatement(
309
                    "INSERT INTO xml_nodes " +
310
                    "(nodetype, nodename,  docid, " +
311
                    "rootnodeid, parentnodeid, nodedata, nodeindex) " +
312
                    "VALUES (?, ?, ?, ?, ?, ?, ?)");
313
            }
314
        } else {
315
            pstmt = connection.prepareStatement(
316
                  "INSERT INTO xml_nodes " +
317
                  "(nodetype, nodename, docid, " +
318
                  "rootnodeid, parentnodeid, nodedata, nodeindex) " +
319
                  "VALUES (?, ?, ?, ?, ?, ?, ?)");
320
        }
321

  
286 322
       // Increase DBConnection usage count
287 323
      connection.increaseUsageCount(1);
288
      
324

  
289 325
      // Bind the values to the query
290 326
      pstmt.setString(1, DocumentImpl.DTD);
291 327
      pstmt.setString(2, nodename);
......
294 330
      pstmt.setLong(5, getParentID());
295 331
      pstmt.setString(6, data);
296 332
      pstmt.setInt(7, getNodeIndex());
297
    
333

  
298 334
      // Do the insertion
299 335
      pstmt.execute();
300 336
      pstmt.close();
......
302 338
      // get the generated unique id afterward
303 339
      nid = dbAdapter.getUniqueID(connection.getConnections(), "xml_nodes");
304 340
      //should incease connection usage!!!!!!
305
     
341

  
306 342
    } catch (SQLException e) {
307 343
      System.out.println("Error in DBSaxNode.writeDTDNodeToDB");
308 344
      System.err.println("Error inserting node: (" + DocumentImpl.DTD + ", " +
309
                                                     nodename + ", " + 
345
                                                     nodename + ", " +
310 346
                                                     data + ")" );
311 347
      System.err.println(e.getMessage());
312 348
      e.printStackTrace(System.err);
......
314 350
    }
315 351
    return nid;
316 352
  }
317
 
318
  
353

  
354

  
319 355
  /** get next node id from DB connection */
320 356
  private long generateNodeID() throws SAXException {
321 357
      long nid=0;
......
335 371
        }
336 372
        stmt.close();
337 373
      } catch (SQLException e) {
338
        System.out.println("Error in DBSaxNode.generateNodeID: " + 
374
        System.out.println("Error in DBSaxNode.generateNodeID: " +
339 375
                            e.getMessage());
340 376
        throw new SAXException(e.getMessage());
341 377
      }
......
350 386

  
351 387
  /** Add a new attribute to this node, or set its value */
352 388
  public long setAttribute(String attName, String attValue, String docid)
353
              throws SAXException 
389
              throws SAXException
354 390
  {
355 391
    long nodeId = -1;
356
    if (attName != null) 
392
    if (attName != null)
357 393
    {
358 394
      // Enter the attribute in the hash table
359 395
      super.setAttribute(attName, attValue);
360 396

  
361 397
      // And enter the attribute in the database
362 398
      nodeId = writeChildNodeToDB("ATTRIBUTE", attName, attValue, docid);
363
    } 
364
    else 
399
    }
400
    else
365 401
    {
366 402
      System.err.println("Attribute name must not be null!");
367 403
      throw new SAXException("Attribute name must not be null!");
......
371 407

  
372 408
  /** Add a namespace to this node */
373 409
  public long setNamespace(String prefix, String uri, String docid)
374
              throws SAXException 
410
              throws SAXException
375 411
  {
376 412
    long nodeId = -1;
377
    if (prefix != null) 
413
    if (prefix != null)
378 414
    {
379 415
      // Enter the namespace in a hash table
380 416
      super.setNamespace(prefix, uri);
381 417
      // And enter the namespace in the database
382 418
      nodeId = writeChildNodeToDB("NAMESPACE", prefix, uri, docid);
383
    } 
384
    else 
419
    }
420
    else
385 421
    {
386 422
      System.err.println("Namespace prefix must not be null!");
387 423
      throw new SAXException("Namespace prefix must not be null!");
......
389 425
    return nodeId;
390 426
  }
391 427

  
392
 
393 428

  
394
  /** 
429

  
430
  /**
395 431
   * USED FROM SEPARATE THREAD RUNNED from DBSAXHandler on endDocument()
396 432
   * Update the node index (xml_index) for this node by generating
397 433
   * test strings that represent all of the relative and absolute
398 434
   * paths through the XML tree from document root to this node
399 435
   */
400
  public void updateNodeIndex(DBConnection conn, String docid, String doctype) 
436
  public void updateNodeIndex(DBConnection conn, String docid, String doctype)
401 437
               throws SAXException
402 438
  {
403 439
    Hashtable pathlist = new Hashtable();
......
432 468
        pathlist.put(currentPath.toString(), new Long(getNodeID()));
433 469
        counter++;
434 470
        atRootDocumentNode = true;
435
      } 
471
      }
436 472
    }
437 473

  
438 474
    try {
439 475
      // Create an insert statement to reuse for all of the path insertions
440 476
      PreparedStatement pstmt = conn.prepareStatement(
441
              "INSERT INTO xml_index (nodeid, path, docid, doctype, " + 
442
               "parentnodeid) " + 
477
              "INSERT INTO xml_index (nodeid, path, docid, doctype, " +
478
               "parentnodeid) " +
443 479
              "VALUES (?, ?, ?, ?, ?)");
444 480
      // Increase usage count
445 481
      conn.increaseUsageCount(1);
446
  
482

  
447 483
      pstmt.setString(3, docid);
448 484
      pstmt.setString(4, doctype);
449 485
      pstmt.setLong(5, getParentID());
450
      
486

  
451 487
      // Step through the hashtable and insert each of the path values
452 488
      Enumeration en = pathlist.keys();
453 489
      while (en.hasMoreElements()) {
......
455 491
        Long nodeid = (Long)pathlist.get(path);
456 492
        pstmt.setLong(1, nodeid.longValue());
457 493
        pstmt.setString(2, path);
458
        
494

  
459 495
        pstmt.executeUpdate();
460 496
      }
461 497
      // Close the database statement
......
467 503
      throw new SAXException(sqe.getMessage());
468 504
    }
469 505
  }
470
 
506

  
471 507
  /** get the parent of this node */
472 508
  public DBSAXNode getParentNode() {
473 509
    return parentNode;

Also available in: Unified diff