Project

General

Profile

« Previous | Next » 

Revision 747

Added by bojilova over 23 years ago

- changed to use the DBAdapter uniform interface for all apperance of "sysdate"
and uniqueid generation from sequence
MetaCatUtil:
- reads from metacat.properties the dbadapter class implemented the interface
and creates an instance of that class (in static public variable named dbAdapter) for use.
So all the classes in the application can access the dbadapter obj by:
MetaCatUtil.dbAdapter

View differences:

src/edu/ucsb/nceas/metacat/MetaCatUtil.java
37 37
import java.util.Hashtable;
38 38
import java.util.Enumeration;
39 39

  
40
import edu.ucsb.nceas.dbadapter.DBAdapter;
41

  
40 42
/**
41 43
 * A suite of utility classes for the metadata catalog server
42 44
 */
43 45
public class MetaCatUtil {
44 46

  
45
  private PropertyResourceBundle options = null;
46
  private String propertiesFile = "edu.ucsb.nceas.metacat.metacat";
47
  private Hashtable connectionPool = new Hashtable();
47
  public static DBAdapter dbAdapter;
48
  private static PropertyResourceBundle options = null;
49
  private static String propertiesFile = "edu.ucsb.nceas.metacat.metacat";
48 50
  private static boolean debug = false;
49 51

  
52
  private Hashtable connectionPool = new Hashtable();
53

  
54
  /** 
55
   * Determine our db adapter class and create an instance of that class
56
   */
57
  static {
58
    try {
59
      dbAdapter = (DBAdapter)createObject(getOption("dbAdapter"));
60
    } catch (Exception e) {
61
      System.err.println("Error in MetaCatUtil static block:" + e.getMessage());
62
    }
63
  }
64

  
65
// CONSTRUCTORS NOT NEEDED
66
//  /**
67
//   * Construct an instance of the utility class
68
//   */
69
//  public MetaCatUtil() {
70
//
71
//    options = (PropertyResourceBundle)
72
//          PropertyResourceBundle.getBundle(propertiesFile);
73
//  }
74
//  
75
//  /**
76
//   * This constructor allows the usage of a different properties file
77
//   * @param propFile the properties file that you wish to use.
78
//   */
79
//  public MetaCatUtil(String propFile)  {
80
//
81
//    propertiesFile = propFile;
82
//  }
83

  
50 84
  /**
51
   * Construct an instance of the utility class
85
   * Instantiate a class using the name of the class at runtime
86
   *
87
   * @param className the fully qualified name of the class to instantiate
52 88
   */
53
  public MetaCatUtil() {
54
    options = (PropertyResourceBundle)
89
  public static Object createObject(String className) throws Exception {
90
 
91
    Object object = null;
92
    try {
93
      Class classDefinition = Class.forName(className);
94
      object = classDefinition.newInstance();
95
    } catch (InstantiationException e) {
96
      throw e;
97
    } catch (IllegalAccessException e) {
98
      throw e;
99
    } catch (ClassNotFoundException e) {
100
      throw e;
101
    }
102
    return object;
103
  }
104

  
105
  /** 
106
   * Utility method to get an option value from the properties file
107
   *
108
   * @param optionName the name of the option requested
109
   */
110
  public static String getOption(String optionName) {
111
      // Get the configuration file information
112
      if (options == null) {
113
        options = (PropertyResourceBundle)
55 114
          PropertyResourceBundle.getBundle(propertiesFile);
115
      }
116
      String value = (String)options.handleGetObject(optionName);
117
      return value;
56 118
  }
57
  
58
  /**
59
   * This constructor allows the usage of a different properties file
60
   * @param propFile the properties file that you wish to use.
119

  
120
  /** 
121
   * Utility method to get an option value from a properties file
122
   *
123
   * @param optionName the name of the option requested
124
   * @param propFile the name of the file where to get the properties from
61 125
   */
62
  public MetaCatUtil(String propFile)
63
  {
64
    propertiesFile = propFile;
126
  public String getOption(String optionName, String propFile) {
127
    // Get the configuration file information
128
    PropertyResourceBundle options = (PropertyResourceBundle)
129
                                     PropertyResourceBundle.getBundle(propFile);
130
    String value = (String)options.handleGetObject(optionName);
131
    return value;
65 132
  }
66 133

  
67 134
  /** 
......
95 162
     return conn;
96 163
  }
97 164

  
98
  /** 
99
   * Utility method to get an option value from the properties file
100
   *
101
   * @param option_name the name of the option requested
102
   */
103
  public String getOption(String option_name) {
104
      // Get the configuration file information
105
      if (options == null) {
106
        options = (PropertyResourceBundle)
107
          PropertyResourceBundle.getBundle(propertiesFile);
108
      }
109
      String value = (String)options.handleGetObject(option_name);
110
      return value;
111
  }
112

  
113 165
  /* Utility method to create and return a pool of Connection objects */
114 166
  public Hashtable getConnectionPool()
115 167
                throws SQLException, ClassNotFoundException {
src/edu/ucsb/nceas/metacat/DocumentImpl.java
55 55

  
56 56
import java.net.URL;
57 57

  
58
import edu.ucsb.nceas.dbadapter.DBAdapter;
59

  
58 60
/**
59 61
 * A class that represents an XML document. It can be created with a simple
60 62
 * document identifier from a database connection.  It also will write an
......
65 67
  static final int ALL = 1;
66 68
  static final int WRITE = 2;
67 69
  static final int READ = 4;
70
  private static final DBAdapter dbAdapter = MetaCatUtil.dbAdapter;
68 71

  
69 72
  private Connection conn = null;
70 73
  private String docid = null;
......
637 640
  private void writeDocumentToDB(String action, String user, String pub, 
638 641
                                 String catalogid, int serverCode) 
639 642
               throws SQLException, Exception {
643

  
644
    String sysdate = dbAdapter.getDateString();
645

  
640 646
    try {
641 647
      PreparedStatement pstmt = null;
642 648

  
......
648 654
                "(docid, rootnodeid, docname, doctype, " + 
649 655
                "user_owner, user_updated, date_created, date_updated, " + 
650 656
                "public_access, catalog_id, server_location) " +
651
                "VALUES (?, ?, ?, ?, ?, ?, sysdate, sysdate, ?, ?, ?)");
657
                "VALUES (?, ?, ?, ?, ?, ?, " + sysdate + ", " + sysdate + 
658
                ", ?, ?, ?)");
652 659
        //note that the server_location is set to 1. 
653 660
        //this means that "localhost" in the xml_replication table must
654 661
        //always be the first entry!!!!!
......
687 694
        pstmt = conn.prepareStatement(
688 695
            "UPDATE xml_documents " +
689 696
            "SET rootnodeid = ?, docname = ?, doctype = ?, " +
690
            "user_updated = ?, date_updated = sysdate, " +
697
            "user_updated = ?, date_updated = " + sysdate + ", " +
691 698
            "server_location = ?, rev = ?, public_access = ?, catalog_id = ? " +
692 699
            "WHERE docid LIKE ?");
693 700
        // Bind the values to the query
......
1078 1085
  private static void archiveDocRevision(Connection conn, String docid,
1079 1086
                                         String user) 
1080 1087
                                         throws SQLException {
1088
    String sysdate = dbAdapter.getDateString();
1089
    
1081 1090
    // create a record in xml_revisions table 
1082 1091
    // for that document as selected from xml_documents
1083 1092
    PreparedStatement pstmt = conn.prepareStatement(
......
1086 1095
        "user_owner, user_updated, date_created, date_updated, " +
1087 1096
        "server_location, rev, public_access, catalog_id) " +
1088 1097
      "SELECT null, ?, rootnodeid, docname, doctype, " + 
1089
        "user_owner, ?, sysdate, sysdate, server_location, rev, " +
1090
        "public_access, catalog_id " +
1098
        "user_owner, ?, " + sysdate + ", " + sysdate + ", "+
1099
        "server_location, rev, public_access, catalog_id " +
1091 1100
      "FROM xml_documents " +
1092 1101
      "WHERE docid = ?");
1093 1102
    // Bind the values to the query and execute it
......
1228 1237
    } catch (McdbException me) {
1229 1238
      me.toXml(new PrintWriter(System.err));
1230 1239
    } catch (AccessionNumberException ane) {
1231
      System.out.println("ERROR: Couldn't delete document!!! ");
1232 1240
      System.out.println(ane.getMessage());
1233 1241
    } catch (Exception e) {
1234 1242
      System.err.println("EXCEPTION HANDLING REQUIRED");
src/edu/ucsb/nceas/metacat/DBSAXNode.java
34 34
import oracle.jdbc.driver.*;
35 35
import org.xml.sax.SAXException;
36 36

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

  
37 39
/** 
38 40
 * A Class that represents an XML node and its contents and
39 41
 * can write its own representation to a database connection
......
42 44

  
43 45
  private Connection	conn;
44 46
  private DBSAXNode	parentNode;
47
  private static final DBAdapter dbAdapter = MetaCatUtil.dbAdapter;
45 48

  
46 49
  /** 
47 50
   * Construct a new node instance for DOCUMENT nodes
......
49 52
   * @param conn the JDBC Connection to which all information is written
50 53
   */
51 54
  public DBSAXNode (Connection conn, String docid) throws SAXException {
55

  
52 56
    super();
53 57
    this.conn = conn;
54 58
    this.parentNode = null;
......
67 71
                    throws SAXException {
68 72

  
69 73
    super(tagname);
74
    this.conn = conn;
75
    this.parentNode = parentNode;
70 76
    setParentID(parentNode.getNodeID());
71 77
    setRootNodeID(rootnodeid);
72 78
    setDocID(docid);
73 79
    setNodeIndex(parentNode.incChildNum());
74
    this.conn = conn;
75
    this.parentNode = parentNode;
76 80
    writeChildNodeToDB("ELEMENT", getTagName(), null, docid);
77 81
    //No writing XML Index from here. New Thread used instead.
78 82
    //updateNodeIndex(docid, doctype);
......
99 103
      }
100 104

  
101 105
      // Bind the values to the query
102
      long nid = generateNodeID();
106
      // NOT USED; USED DBAdapter.getUniqueID() instead
107
      //long nid = generateNodeID();
108
      long nid = dbAdapter.getUniqueID(conn, "xml_nodes");
103 109
      pstmt.setLong(1, nid);
104 110
      pstmt.setString(2, nodetype);
105 111
      pstmt.setString(3, nodename);
src/edu/ucsb/nceas/metacat/AccessionNumber.java
31 31
import java.net.*;
32 32
import java.sql.*;
33 33

  
34
import edu.ucsb.nceas.dbadapter.DBAdapter;
35

  
34 36
/**
35 37
 * (on insert of XML document)
36 38
 * Generates a unique Accession Number or if provided check it 
......
41 43
 */
42 44
public class AccessionNumber  {
43 45
  
46
  private static final DBAdapter dbAdapter = MetaCatUtil.dbAdapter;
47

  
44 48
  private Connection conn = null;
45 49
  private String sitecode = null;
46 50
  private String sep = null;
......
104 108
        // get a new docid
105 109
        if ( docid == null ) {
106 110
          String sitecode = getSitecode();
107
          String uniqueid = getUniqueID();
111
          // NOT USED; USED DBAdapter.getUniqueID() instead
112
          //String uniqueid = getUniqueID();
113
          String uniqueid = "" + dbAdapter.getUniqueID(conn,"xml_documents");
108 114

  
109 115
          return sitecode + sep + uniqueid;
110 116

  

Also available in: Unified diff