Project

General

Profile

« Previous | Next » 

Revision 793

Added by bojilova about 23 years ago

http://bugzilla.ecoinformatics.org/show_bug.cgi?id=248
Included utility function about gettig the latest docid for a user:
DBUtil.getLastDocid(username).
Metacat parameters:
action='getlastdocid'
username

View differences:

src/edu/ucsb/nceas/metacat/MetaCatServlet.java
326 326
    } else if (action.equals("getdataguide")) {
327 327
      PrintWriter out = response.getWriter();
328 328
      handleGetDataGuideAction(out, params, response);  
329
    } else if (action.equals("getlastdocid")) {
330
      PrintWriter out = response.getWriter();
331
      handleGetLastDocidAction(out, params, response);  
329 332
    } else if (action.equals("login") || action.equals("logout")) {
330 333
    } else if (action.equals("protocoltest")) {
331 334
      String testURL = "metacat://dev.nceas.ucsb.edu/NCEAS.897766.9";
......
1466 1469
    
1467 1470
  }
1468 1471

  
1472
  /** 
1473
   * Handle the "getlastdocid" action.
1474
   * Get the latest docid with rev number from db connection in XML format
1475
   */
1476
  private void handleGetLastDocidAction(PrintWriter out, Hashtable params, 
1477
                                        HttpServletResponse response) {
1478

  
1479
    Connection conn = null;
1480
    String username = ((String[])params.get("username"))[0];
1481

  
1482
    try {
1483

  
1484
        // get connection from the pool
1485
        conn = util.getConnection();
1486
        DBUtil dbutil = new DBUtil(conn);
1487
        String lastDocid = dbutil.getLastDocid(username);
1488
        out.println("<?xml version=\"1.0\"?>");
1489
        out.println("<lastDocid>");
1490
        out.println("  <username>" + username + "</username>");
1491
        out.println("  <docid>" + lastDocid + "</docid>");
1492
        out.println("</lastDocid>");
1493

  
1494
    } catch (Exception e) {
1495
      out.println("<?xml version=\"1.0\"?>");
1496
      out.println("<error>");
1497
      out.println(e.getMessage());
1498
      out.println("</error>");
1499
    } finally {
1500
      util.returnConnection(conn);
1501
    }  
1502
    
1503
  }
1504

  
1469 1505
}
src/edu/ucsb/nceas/metacat/DBUtil.java
67 67
     {
68 68
        System.err.println("Wrong number of arguments!!!");
69 69
        System.err.println(
70
        "USAGE: java DBUtil <-dt | -ds [doctype] | -dg [doctype]>");
70
        "USAGE: java DBUtil <-dt | -ds [doctype] | -dg [doctype] | -dl user>");
71 71
        return;
72 72
     } else {
73 73
        try {
......
91 91
            if ( args.length == 2 ) { doctype = args[1]; }
92 92
            String dtdschema = dbutil.readDTDSchema(doctype);
93 93
            System.out.println(dtdschema);
94
          } else if ( args[0].equals("-dl") ) {
95
            String username = "";
96
            if ( args.length == 2 ) { username = args[1]; }
97
            String docid = dbutil.getLastDocid(username);
98
            System.out.println(docid);
94 99
          } else {
95 100
            System.err.println(
96 101
            "USAGE: java DBUtil <-dt | -ds [doctype] | -dg [doctype]>");
......
342 347
    return result.toString();
343 348
  }
344 349

  
350
  /**
351
   * get the lastest Accession Number used by user
352
   */
353
  public String getLastDocid(String user)
354
        throws SQLException  {
345 355

  
356
    String accnum = null;
357
    String sep = MetaCatUtil.getOption("accNumSeparator");
358

  
359
    try {
360

  
361
      PreparedStatement pstmt =
362
        conn.prepareStatement("SELECT docid, rev, date_updated " +
363
                              "FROM xml_documents " +
364
                              "WHERE user_owner = ? " +
365
                              "AND date_updated = (SELECT MAX(date_updated) " +
366
                                                   "FROM xml_documents " +
367
                                                   "WHERE user_owner = ?) " +
368
                              "UNION " + 
369
                              "SELECT docid, rev, date_updated " +
370
                              "FROM xml_revisions " +
371
                              "WHERE user_owner = ? " +
372
                              "AND date_updated = (SELECT MAX(date_updated) " +
373
                                                   "FROM xml_revisions " +
374
                                                   "WHERE user_owner = ?) " +
375
                              "ORDER BY date_updated");
376
      pstmt.setString(1,user);
377
      pstmt.setString(2,user);
378
      pstmt.setString(3,user);
379
      pstmt.setString(4,user);
380
      pstmt.execute();
381
      ResultSet rs = pstmt.getResultSet();
382
      boolean tableHasRows = rs.next(); //0, 1 or 2 possible num of rows
383
      // get the last one which is the latest accnum
384
      while (tableHasRows) {
385
        accnum = rs.getString(1) + sep + rs.getString(2);
386
        tableHasRows = rs.next();
387
      }
388
      
389
      pstmt.close();
390

  
391
    } catch (SQLException e) {
392
      throw new SQLException("DBUtil.readLastDocid(). " + e.getMessage());
393
    }
394

  
395
    return accnum;
396
  }
397

  
346 398
}

Also available in: Unified diff