Project

General

Profile

« Previous | Next » 

Revision 203

Merged in substantial changes to DBWriter and associated classes and to
the MetaCatServlet in order to accomodate the new UPDATE and DELETE
functions. The command line tools and the parameters for the
servlet have changed substantially.

View differences:

MetaCatServlet.java
1 1
/**
2
 *      Name: MetaCatServlet.java
3
 *   Purpose: A Class that implements a metadata catalog as a java Servlet
4
 * Copyright: 2000 Regents of the University of California and the
5
 *            National Center for Ecological Analysis and Synthesis
6
 *   Authors: Matt Jones, Dan Higgins
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements a metadata catalog as a java Servlet
4
 *  Copyright: 2000 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Matt Jones, Dan Higgins
7 7
 *
8
 *   Version: '$Id$'
8
 *   '$Author$'
9
 *     '$Date$'
10
 * '$Revision$'
9 11
 */
10 12

  
11 13
package edu.ucsb.nceas.metacat;
......
215 217
      } catch (SQLException se) {
216 218
        out.println(se.getMessage());
217 219
      }
218
    } else if (action.equals("putdocument")) {
219
      handlePutDocumentAction(out, params, response);
220
    } else if (action.equals("insert") || action.equals("update")) {
221
      handleInsertOrUpdateAction(out, params, response);
222
    } else if (action.equals("delete")) {
223
      handleDeleteAction(out, params, response);
220 224
    } else if (action.equals("validate")) {
221 225
      handleValidateAction(out, params, response);  
222 226
    } else if (action.equals("getdatadoc")) {
......
356 360
   * Handle the database putdocument request and write an XML document 
357 361
   * to the database connection
358 362
   */
359
  private void handlePutDocumentAction(PrintWriter out, Hashtable params, 
363
  private void handleInsertOrUpdateAction(PrintWriter out, Hashtable params, 
360 364
               HttpServletResponse response) {
361 365

  
362
      // Get the document indicated
363
      String[] doctext = (String[])params.get("doctext");
364
      StringReader xml = new StringReader(doctext[0]);
366
    // Get the document indicated
367
    String[] doctext = (String[])params.get("doctext");
368
    StringReader xml = new StringReader(doctext[0]);
365 369

  
366
      // write the document to the database
370
    String[] action = (String[])params.get("action");
371
    String[] docid = (String[])params.get("docid");
372
    String newdocid = null;
373

  
374
    String doAction = null;
375
    if (action[0].equals("insert")) {
376
      doAction = "INSERT";
377
    } else if (action[0].equals("update")) {
378
      doAction = "UPDATE";
379
    }
380

  
381
    // write the document to the database
382
    try {
383
      DBWriter dbw = new DBWriter(conn, saxparser);
384
                                      // NOTE -- NEED TO TEST HERE
385
                                      // FOR EXISTENCE OF PARAM
386
                                      // BEFORE ACCESSING ARRAY
367 387
      try {
368
        DBWriter dbw = new DBWriter(xml, conn, saxparser);
369
      } catch (SQLException e1) {
370
          out.println("Error 1 loading document:<p>\n" + e1.getMessage());
371
      }catch (IOException e2) {
372
          out.println("Error 2 loading document:<p>\n" + e2.getMessage());
373
      }catch (ClassNotFoundException e3) {
374
          out.println("Error 3 loading document:<p>\n" + e3.getMessage());
388
        String accNumber = docid[0];
389
        response.setContentType("text/plain");
390
        out.println("Accnumber param is: " + accNumber);
391
        out.println("doAction param is: " + doAction);
392
        if (accNumber.equals("")) {
393
          accNumber = null;
394
          out.println("AccNumber now set to null.");
395
        }
396
        newdocid = dbw.write(xml, doAction, accNumber);  
397
      } catch (NullPointerException npe) {
398
        newdocid = dbw.write(xml, doAction, null);  
375 399
      }
376 400

  
377
      // set content type and other response header fields first
378
      response.setContentType("text/xml");
379
  
380
      out.println(doctext[0]);
401
    } catch (SQLException e1) {
402
        out.println("Error 1 loading document:\n" + e1.getMessage());
403
    }catch (IOException e2) {
404
        out.println("Error 2 loading document:\n" + e2.getMessage());
405
    }catch (ClassNotFoundException e3) {
406
        out.println("Error 3 loading document:\n" + e3.getMessage());
407
    }catch (Exception e4) {
408
        out.println("Error 4 loading document:\n" + e4.getMessage());
409
        e4.printStackTrace(out);
410
    }
411

  
412
    // set content type and other response header fields first
413
    //response.setContentType("text/xml");
414
    out.println("<docid>" + newdocid + "</docid>"); 
381 415
  }
416

  
417
  /** 
418
   * Handle the database delete request and delete an XML document 
419
   * from the database connection
420
   */
421
  private void handleDeleteAction(PrintWriter out, Hashtable params, 
422
               HttpServletResponse response) {
423

  
424
    String[] docid = (String[])params.get("docid");
425

  
426
    // delete the document from the database
427
    try {
428
      DBWriter dbw = new DBWriter(conn, saxparser);
429
                                      // NOTE -- NEED TO TEST HERE
430
                                      // FOR EXISTENCE OF PARAM
431
                                      // BEFORE ACCESSING ARRAY
432
      try {
433
        dbw.delete(docid[0]);
434
        response.setContentType("text/html");
435
        out.println("Document deleted."); 
436
      } catch (AccessionNumberException ane) {
437
        response.setContentType("text/html");
438
        out.println("ERROR deleting document!!!<br />");
439
        out.println(ane.getMessage()); 
440
      }
441
    } catch (SQLException e1) {
442
        out.println("Error 1 loading document:<p>\n" + e1.getMessage());
443
    }catch (IOException e2) {
444
        out.println("Error 2 loading document:<p>\n" + e2.getMessage());
445
    }catch (ClassNotFoundException e3) {
446
        out.println("Error 3 loading document:<p>\n" + e3.getMessage());
447
    }
448
  }
382 449
  
383 450
  /** 
384 451
   * Handle the validtion request and return the results to the requestor
......
486 553
    } // end defaultdatapath not null if
487 554
  }
488 555
}
556

  
557
/**
558
 * '$Log$
559
 * 'Revision 1.30.2.6  2000/06/26 10:18:06  jones
560
 * 'Partial fix for MetaCatServlet INSERT?UPDATE bug.  Only will work on
561
 * 'the first call to the servlet.  Subsequent calls fail.  Seems to be
562
 * 'related to exception handling.  Multiple successive DELETE actions
563
 * 'work fine.
564
 * '
565
 * 'Revision 1.30.2.5  2000/06/26 09:09:53  jones
566
 * 'Modified MetaCatServlet and associated files to handle the UPDATE
567
 * 'and DELETE actions for DBWriter.
568
 * '
569
 * 'Revision 1.30.2.4  2000/06/26 00:51:06  jones
570
 * 'If docid passed to DBWriter.write() is not unique, classes now generate
571
 * 'an AccessionNumberException containing the new docid generated as a
572
 * 'replacement.  The docid is then extracted from the exception and
573
 * 'returned to the calling application for user feedback or client processing.
574
 * '
575
 * 'Revision 1.30.2.3  2000/06/25 23:38:17  jones
576
 * 'Added RCSfile keyword
577
 * '
578
 * 'Revision 1.30.2.2  2000/06/25 23:34:18  jones
579
 * 'Changed documentation formatting, added log entries at bottom of source files
580
 * ''
581
 */

Also available in: Unified diff