Project

General

Profile

« Previous | Next » 

Revision 5167

Added by daigle over 14 years ago

add method name to log statements

View differences:

DBUtil.java
29 29

  
30 30
package edu.ucsb.nceas.metacat;
31 31

  
32
import java.sql.Connection;
33 32
import java.sql.SQLException;
34 33
import java.sql.PreparedStatement;
35 34
import java.sql.ResultSet;
......
38 37
import java.io.InputStream;
39 38
import java.io.IOException;
40 39
import java.net.URL;
41
import java.net.URLConnection;
42 40
import java.net.MalformedURLException;
43 41

  
44 42
import java.util.Enumeration;
45 43
import java.util.Vector;
46
import java.util.Stack;
47 44

  
48 45
import org.apache.log4j.Logger;
49 46

  
......
55 52
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
56 53

  
57 54
/**
58
 * A suite of utility classes for quering DB
55
 * A suite of utility classes for querying DB
59 56
 */
60 57
public class DBUtil {
61 58

  
......
75 72
     if (args.length < 1)
76 73
     {
77 74
        System.err.println("Wrong number of arguments!!!");
78
        System.err.println(
79
        "USAGE: java DBUtil <-dt | -ds [doctype] | -dl user>");
75
        System.err.println("USAGE: java DBUtil <-dt | -ds [doctype] | -dl user>");
80 76
        return;
81 77
     } else {
82 78
        try {
......
126 122
  public String readDoctypes()
127 123
        throws SQLException  {
128 124

  
129
    Vector doctypeList = new Vector();
125
    Vector<String> doctypeList = new Vector<String>();
130 126
    DBConnection dbConn = null;
131 127
    int serialNumber = -1;
132 128
    PreparedStatement pstmt = null;
......
150 146
      pstmt.close();
151 147

  
152 148
    } catch (SQLException e) {
153
      throw new SQLException("DBUtil.readDoctypes(). " + e.getMessage());
149
      throw new SQLException("DBUtil.readDoctypes - SQL error: " + e.getMessage());
154 150
    }
155 151
    finally
156 152
    {
......
200 196
        	systemID = SystemUtil.getContextURL() + systemID;
201 197
        }
202 198
      } else {
203
        throw new SQLException("Non-registered doctype: " + doctype);
199
        throw new SQLException("DBUtil.readDTDSchema - Non-registered doctype: " + doctype);
204 200
      }
205 201
      pstmt.close();
206 202

  
207 203
    } catch (SQLException e) {
208
      throw new SQLException("DBUtil.readDTD(). " + e.getMessage());
204
      throw new SQLException("DBUtil.readDTDSchema - " + e.getMessage());
209 205
    }
210 206
    finally
211 207
    {
......
238 234
	    in.close();
239 235
	    
240 236
    } catch (MalformedURLException e) {
241
      throw new MalformedURLException
242
      ("DBUtil.readDTD(). " + e.getMessage());
237
    	throw new MalformedURLException("DBUtil.readDTDSchema - Malformed URL Error: " + e.getMessage());
243 238
    } catch (IOException e) {
244
      throw new IOException
245
      ("DBUtil.readDTD(). " + e.getMessage());
239
    	throw new IOException("DBUtil.readDTDSchema - I/O error: " + e.getMessage());
246 240
    } catch (SecurityException e) {
247
      throw new IOException
248
      ("DBUtil.readDTD(). " + e.getMessage());
241
    	throw new IOException("DBUtil.readDTDSchema - Security error: " + e.getMessage());
249 242
    }
250 243
    
251 244
   return cbuff.toString();
252 245
  }
253 246

  
254
  /**
255
   * format the DataGuide ResultSet to XML
256
   */
257
  private String formatToXML(Vector resultset) {
258
  
259
    String currPath = null;
260
    String currElement = null;
261
    String prevElement = null;
262
    StringBuffer result = new StringBuffer();
263
    Enumeration rs = resultset.elements(); 
264
    Stack st = new Stack();
265
    int i = 0;
247
//  /**
248
//   * format the DataGuide ResultSet to XML
249
//   */
250
//  private String formatToXML(Vector resultset) {
251
//  
252
//    String currPath = null;
253
//    String currElement = null;
254
//    String prevElement = null;
255
//    StringBuffer result = new StringBuffer();
256
//    Enumeration<String> rs = resultset.elements(); 
257
//    Stack st = new Stack();
258
//    int i = 0;
259
//
260
//    result.append("<?xml version=\"1.0\"?>\n");
261
//    result.append("<resultset>\n"); 
262
//    
263
//    while (rs.hasMoreElements()) {
264
//        currPath = (String)rs.nextElement();
265
//        while ( !In(prevElement, currPath) ) {
266
//            currElement = (String)st.pop();
267
//            result.append(pad(" ",i--) + "</" + currElement + ">\n");
268
//            if ( st.empty() ) 
269
//                prevElement = null;
270
//            else    
271
//                prevElement = (String)st.peek();
272
//        }    
273
//        currElement = getElementFromPath(currPath);
274
//        st.push(currElement);
275
//        result.append(pad(" ",++i) + "<" + currElement + ">\n");
276
//        prevElement = currElement;
277
//    }
278
//    while ( !st.empty() ) {
279
//        prevElement = (String)st.pop();
280
//        result.append(pad(" ",i--) + "</" + prevElement + ">\n");
281
//    }    
282
//    result.append("</resultset>\n"); 
283
//
284
//    return result.toString();
285
//  }
266 286

  
267
    result.append("<?xml version=\"1.0\"?>\n");
268
    result.append("<resultset>\n"); 
269
    
270
    while (rs.hasMoreElements()) {
271
        currPath = (String)rs.nextElement();
272
        while ( !In(prevElement, currPath) ) {
273
            currElement = (String)st.pop();
274
            result.append(pad(" ",i--) + "</" + currElement + ">\n");
275
            if ( st.empty() ) 
276
                prevElement = null;
277
            else    
278
                prevElement = (String)st.peek();
279
        }    
280
        currElement = getElementFromPath(currPath);
281
        st.push(currElement);
282
        result.append(pad(" ",++i) + "<" + currElement + ">\n");
283
        prevElement = currElement;
284
    }
285
    while ( !st.empty() ) {
286
        prevElement = (String)st.pop();
287
        result.append(pad(" ",i--) + "</" + prevElement + ">\n");
288
    }    
289
    result.append("</resultset>\n"); 
287
//  /**
288
//   * check if element is in path like /elem1/elem2/elemn3
289
//   */
290
//  private boolean In(String element, String path) {
291
//    
292
//    if ( element == null ) return true;
293
//    return ( path.indexOf(element) != -1 );
294
//  }
295
//
296
//  /**
297
//   * get last element from path like /elem1/elem2/elemn3
298
//   */
299
//  private String getElementFromPath(String path) {
300
//    
301
//    return ( path.substring(path.lastIndexOf("/")+1) );
302
//  }
303
//
304
//  /**
305
//   * repeates the str n-times
306
//   */
307
//  private String pad(String str, int n) {
308
//    
309
//    String result = "";
310
//    for ( int i = 0; i < n; i++ )
311
//        result = result.concat(str);
312
//        
313
//    return result;    
314
//  }
290 315

  
291
    return result.toString();
292
  }
293

  
294 316
  /**
295
   * check if element is in path like /elem1/elem2/elemn3
296
   */
297
  private boolean In(String element, String path) {
298
    
299
    if ( element == null ) return true;
300
    return ( path.indexOf(element) != -1 );
301
  }
302

  
303
  /**
304
   * get last element from path like /elem1/elem2/elemn3
305
   */
306
  private String getElementFromPath(String path) {
307
    
308
    return ( path.substring(path.lastIndexOf("/")+1) );
309
  }
310

  
311
  /**
312
   * repeates the str n-times
313
   */
314
  private String pad(String str, int n) {
315
    
316
    String result = "";
317
    for ( int i = 0; i < n; i++ )
318
        result = result.concat(str);
319
        
320
    return result;    
321
  }
322

  
323
  /**
324 317
   * format the ResultSet to XML
325 318
   */
326
  private String formatToXML(Vector resultset, String tag) {
319
  private String formatToXML(Vector<String> resultset, String tag) {
327 320
  
328 321
    String val = null;
329 322
    StringBuffer result = new StringBuffer();
330
    Enumeration rs = resultset.elements(); 
323
    Enumeration<String> rs = resultset.elements(); 
331 324

  
332 325
    result.append("<?xml version=\"1.0\"?>\n");
333 326
    result.append("<resultset>\n"); 
334 327
    while (rs.hasMoreElements()) {
335
        val = (String)rs.nextElement();
328
        val = rs.nextElement();
336 329
        result.append("   <" + tag + ">" + val + "</" + tag + ">\n");
337 330
    }
338 331
    result.append("</resultset>\n"); 
......
341 334
  }
342 335

  
343 336
  /**
344
   * get the lastest Accession Number from a particular scope
337
   * get the latest Accession Number from a particular scope
345 338
   */
346 339
  public String getMaxDocid(String scope)
347 340
        throws SQLException  {
......
351 344
    try {
352 345
    	PropertyService.getProperty("document.accNumSeparator");
353 346
    } catch (PropertyNotFoundException pnfe) {
354
    	logMetacat.error("could not get property 'accNumSeparator'.  setting to '.': " 
355
    			+ pnfe.getMessage());  	
347
    	logMetacat.error("DBUtil.getMaxDocid - could not get property " + 
348
    			"'accNumSeparator'.  setting to '.': " + pnfe.getMessage());  	
356 349
    }
357 350
    PreparedStatement pstmt = null;
358 351
    DBConnection dbConn = null;
......
427 420
  public boolean idExists(String docid)
428 421
    throws SQLException
429 422
  {
430
    Vector v = getAllDocids(null);
423
    Vector<String> v = getAllDocids(null);
431 424
    for(int i=0; i<v.size(); i++)
432 425
    {
433 426
      String id = (String)v.elementAt(i);
......
442 435
  /**
443 436
   * return all docids with a given doctype
444 437
   */
445
  public Vector getAllDocidsByType(String doctype, boolean includeRevs) throws SQLException {
438
  public Vector<String> getAllDocidsByType(String doctype, boolean includeRevs) throws SQLException {
446 439
		Vector<String> resultVector = new Vector<String>();
447
		String accnum = null;
448 440
		String sep = ".";
449 441
	    try {
450 442
	    	PropertyService.getProperty("document.accNumSeparator");
451 443
	    } catch (PropertyNotFoundException pnfe) {
452
	    	logMetacat.error("could not get property 'accNumSeparator'.  setting to '.': " 
453
	    			+ pnfe.getMessage());  	
444
	    	logMetacat.error("DBUtil.getAllDocidsByType - could not get property " + 
445
	    			"'accNumSeparator'.  setting to '.': " + pnfe.getMessage());  	
454 446
	    }
455 447
		PreparedStatement pstmt = null;
456 448
		DBConnection dbConn = null;
......
510 502
	}
511 503
  
512 504
  /**
513
   * get the lastest Accession Number from a particular scope
505
   * get the latest Accession Number from a particular scope
514 506
   */
515 507
  public Vector<String> getAllDocids(String scope)
516 508
        throws SQLException  {
517 509
    Vector<String> resultVector = new Vector<String>();
518
    String accnum = null;
510
//    String accnum = null;
519 511
    String sep = ".";
520 512
    try {
521 513
    	PropertyService.getProperty("document.accNumSeparator");
......
559 551
      pstmt.execute();
560 552
      ResultSet rs = pstmt.getResultSet();
561 553
      
562
      long max = 0;
554
//      long max = 0;
563 555
      String id = null;
564 556
      String rev = null;
565 557
      while(rs.next()){
......
574 566
      pstmt.close();
575 567

  
576 568
    } catch (SQLException e) {
577
      throw new SQLException("DBUtil.getAllDocids(). " + e.getMessage());
569
      throw new SQLException("DBUtil.getAllDocids - SQL error:  " + e.getMessage());
578 570
    }
579 571
    finally
580 572
    {
......
592 584
  }
593 585
  
594 586
  /**
595
   * To a given docid, found a dataset docid which conatains the the given doicd
587
   * To a given docid, found a dataset docid which contains the the given docid
596 588
   * This will be done by searching xml_relation table
597 589
   * If couldn't find, null will be return
598 590
   * @param givenDocId, the docid which we want to find
......
616 608
      dbConn=DBConnectionPool.
617 609
                  getDBConnection("DBUtil.findDataSetDocIdForGivenDocument");
618 610
      serialNumber=dbConn.getCheckOutSerialNumber();
619
      // Sql command to chose a docid from xm_relation table
611
      // SQL command to chose a docid from xm_relation table
620 612
      sqlCommand = "select docid from xml_relation where object like ? or " 
621 613
                                                    + "subject like ?";
622 614
      // Prepared statement
......
624 616
      // Bind variable
625 617
      pStmt.setString(1, givenDocId);
626 618
      pStmt.setString(2, givenDocId);
627
      // Excute prepared statement
619
      // Execute prepared statement
628 620
      pStmt.execute();
629 621
      // Get result set
630 622
      resultSet = pStmt.getResultSet();
......
645 637
    }//try
646 638
    catch ( SQLException e)
647 639
    {
648
      // Print out excepition
649
      logMetacat.error("Error in DBUil.findDataSEtDocIdForGivenDocument"
650
                                +e.getMessage());
640
      // Print out exception
641
      logMetacat.error("DBUtil.findDataSetDocIdForGivenDocument - " +
642
    		  "SQL error: " + e.getMessage());
651 643
      // return null
652 644
      return dataSetDocId;
653 645
     
......
663 655
      }//try
664 656
      catch ( SQLException e)
665 657
      {
666
        // Print out excepition
667
    	  logMetacat.error("Error in DBUil.findDataSetDocIdForGivenDoc"
668
                                + e.getMessage());
658
        // Print out exception
659
    	  logMetacat.error("DBUtil.findDataSetDocIdForGivenDocument - " +
660
    			  "error closing db resources: "  + e.getMessage());
669 661
     
670 662
      }//catch
671 663
      finally
......
692 684
    PreparedStatement pstmt = null;
693 685
    // String to store a docid without rev
694 686
    String docIdWithoutRevision = null;
695
    // SQL comand
687
    // SQL command
696 688
    String sqlCommand = null;
697
    // Resulst set
689
    // Result set
698 690
    ResultSet rs = null;
699 691
    // String to store the revision
700 692
    String revision = null;
......
703 695
    
704 696
    // Get docid without rev
705 697
    docIdWithoutRevision = DocumentUtil.getDocIdFromString(givenDocId);
706
    // SQL comand is:
698
    // SQL command is:
707 699
    sqlCommand = "select rev, doctype from xml_documents where docid like ?";
708 700
    
709 701
    try
......
715 707
      
716 708
      // Prepare the sql command
717 709
      pstmt = dbConn.prepareStatement(sqlCommand);
718
      // Bin vairable
710
      // Bin variable
719 711
      pstmt.setString(1, docIdWithoutRevision);
720
      // Excute the prepared statement
712
      // Execute the prepared statement
721 713
      pstmt.execute();
722 714
      // Get result set
723 715
      rs = pstmt.getResultSet();
......
731 723
      {
732 724
        // No record, throw a exception
733 725
        throw new 
734
              SQLException("There is not record for given docid:"+givenDocId);
726
              SQLException("DBUtil.getCurrentRevisionAndDocTypeForGivenDocument - " + 
727
            		  "There is no record for given docid: " + givenDocId);
735 728
      }//else
736 729
        
737 730
    }
......
746 739
      }//try
747 740
      catch ( SQLException e)
748 741
      {
749
        // Print out excepition
750
    	  logMetacat.error("Error in DBUil.getCurrentRevisionAndDocType"
751
                                + e.getMessage());
742
        // Print out exception
743
    	  logMetacat.error("DBUtil.getCurrentRevisionAndDocTypeForGivenDocument - " + 
744
    			  "Error closing db resources: " + e.getMessage());
752 745
     
753 746
      }//catch
754 747
      finally
......
790 783
          boolean hasRow = rs.next();
791 784
          while (hasRow) {
792 785
              rev = rs.getInt(1);
793
              logMetacat.warn("rev "+ rev +" is added to list");
786
              logMetacat.info("DBUtil.getRevListFromRevisionTable - rev: " + rev + 
787
            		  " is added to list for docid: " + docIdWithoutRev);
794 788
              list.add(new Integer(rev));
795 789
              hasRow = rs.next();
796 790
              
......
801 795
          try {
802 796
              pStmt.close();
803 797
          } catch (Exception ee) {
804
        	  logMetacat.error("Error in DocumentImpl."
805
                      + "getLatestRevisionNumber: " + ee.getMessage());
798
        	  logMetacat.error("DBUtil.getRevListFromRevisionTable - Error closing " + 
799
        			  "prepared statement: " + ee.getMessage());
806 800
          } finally {
807 801
              DBConnectionPool.returnDBConnection(dbConn, serialNumber);
808 802
          }
......
850 844
          try {
851 845
              pStmt.close();
852 846
          } catch (Exception ee) {
853
        	  logMetacat.error("Error in DBUtil."
854
                      + "getLatestRevisionInDocumentTable: " + ee.getMessage());
847
        	  logMetacat.error("DBUtil.getLatestRevisionInDocumentTable - Error closing " + 
848
        			  " prepared statement: " + ee.getMessage());
855 849
          } finally {
856 850
              DBConnectionPool.returnDBConnection(dbConn, serialNumber);
857 851
          }
858 852
      }//finally
859 853

  
860 854
      return rev;
861
  }//getLatestRevisionNumber
855
  }
862 856
   
863 857
}

Also available in: Unified diff