Project

General

Profile

« Previous | Next » 

Revision 847

Added by Matt Jones over 22 years ago

Fixed the function to return the max id for a given scope. Now the function
takes a parameter named 'scope' and returns the largest docid that has been
used under that scope in this metacat instance (it used to return the most
recently created docid, which is clearly different). For compatibility with
older versions of metacat the routine falls back to using the username
parameter if scope is not provided, but this is deprecated in favor of using
the scope parameter.

View differences:

DBUtil.java
92 92
            String dtdschema = dbutil.readDTDSchema(doctype);
93 93
            System.out.println(dtdschema);
94 94
          } else if ( args[0].equals("-dl") ) {
95
            String username = "";
96
            if ( args.length == 2 ) { username = args[1]; }
97
            String docid = dbutil.getLastDocid(username);
95
            String scope = "";
96
            if ( args.length == 2 ) { scope = args[1]; }
97
            String docid = dbutil.getMaxDocid(scope);
98 98
            System.out.println(docid);
99 99
          } else {
100 100
            System.err.println(
......
348 348
  }
349 349

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

  
356 356
    String accnum = null;
......
359 359
    try {
360 360

  
361 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);
362
        conn.prepareStatement(
363
            "SELECT docid, rev, acc FROM " +
364
            "( " +
365
            "SELECT docid, rev, acc FROM " +
366
                "(" +
367
                "SELECT docid, rev, " + 
368
                "SUBSTR(docid, INSTR(docid, '" + sep + "', 1)+1)+0 acc " +
369
                "FROM xml_documents " +
370
                "WHERE docid LIKE ? " +
371
                "ORDER BY acc DESC " +
372
                ") " +
373
            "WHERE rownum = 1 " +
374
            "UNION " + 
375
            "SELECT docid, rev, acc FROM " +
376
                "(" +
377
                "SELECT docid, rev, " + 
378
                "SUBSTR(docid, INSTR(docid, '" + sep + "', 1)+1)+0 acc " +
379
                "FROM xml_revisions " +
380
                "WHERE docid LIKE ? " +
381
                "ORDER BY acc DESC " +
382
                ") " +
383
            "WHERE rownum = 1 " +
384
            ") " +
385
            "ORDER BY acc DESC"
386
            );
387

  
388
      pstmt.setString(1,scope + sep + "%");
389
      pstmt.setString(2,scope + sep + "%");
380 390
      pstmt.execute();
381 391
      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) {
392
      boolean tableHasRows = rs.next(); 
393
      // 0, 1 or 2 possible num of rows
394
      // get the first one which is the max accnum
395
      if (tableHasRows) {
385 396
        accnum = rs.getString(1) + sep + rs.getString(2);
386
        tableHasRows = rs.next();
397
        //tableHasRows = rs.next();
387 398
      }
388 399
      
389 400
      pstmt.close();
390 401

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

  
395 406
    return accnum;

Also available in: Unified diff