Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements utility methods like:
4
 *             1/ Reding all doctypes from db connection
5
 *             2/ Reading Lore type Data Guide from db connection
6
 *  Copyright: 2000 Regents of the University of California and the
7
 *             National Center for Ecological Analysis and Synthesis
8
 *    Authors: Jivka Bojilova
9
 *    Release: @release@
10
 * 
11
 *   '$Author: bojilova $'
12
 *     '$Date: 2000-08-17 11:06:27 -0700 (Thu, 17 Aug 2000) $'
13
 * '$Revision: 374 $'
14
 */
15

    
16
package edu.ucsb.nceas.metacat;
17

    
18
import java.sql.Connection;
19
import java.sql.SQLException;
20
import java.sql.PreparedStatement;
21
import java.sql.ResultSet;
22
import java.util.Enumeration;
23
import java.util.Vector;
24
import java.util.Stack;
25

    
26
/**
27
 * A suite of utility classes for quering DB
28
 */
29
public class DBUtil {
30

    
31
  private Connection	conn = null;
32

    
33
  /**
34
   * main routine used for testing.
35
   * <p>
36
   * Usage: java DBUtil <-dt|-dg>
37
   *
38
   * @param -dt for selecting all doctypes
39
   *        -dg for selecting DataGuide
40
   */
41
  static public void main(String[] args) {
42
     
43
     if (args.length < 1)
44
     {
45
        System.err.println("Wrong number of arguments!!!");
46
        System.err.println("USAGE: java DBUtil <-dt | -dg [doctype]>");
47
        return;
48
     } else {
49
        try {
50
                    
51
          // Open a connection to the database
52
          MetaCatUtil   util = new MetaCatUtil();
53
          Connection dbconn = util.openDBConnection();
54

    
55
          DBUtil dbutil = new DBUtil( dbconn );
56
          
57
          if ( args[0].equals("-dt") ) {
58
            String doctypes = dbutil.readDoctypes();
59
            System.out.println(doctypes);
60
          } else if ( args[0].equals("-dg") ) {
61
            String doctype = null;
62
            if ( args.length == 2 ) { doctype = args[1]; }
63
            String dataguide = dbutil.readDataGuide(doctype);
64
            System.out.println(dataguide);
65
          } else {
66
            System.err.println("USAGE: java DBUtil <-dt | -dg [doctype]>");
67
          }  
68

    
69
        } catch (Exception e) {
70
          System.err.println("EXCEPTION HANDLING REQUIRED");
71
          System.err.println(e.getMessage());
72
          e.printStackTrace(System.err);
73
        }
74
     }
75
  }
76
  
77
  /**
78
   * Construct an instance of the utility class
79
   */
80
  public DBUtil( Connection conn ) {
81
    this.conn = conn;
82
  }
83

    
84
  /**
85
   * read all doctypes from db connection in XML format
86
   * select all Public Id from xml_catalog table
87
   */
88
  public String readDoctypes()
89
        throws SQLException  {
90

    
91
    Vector doctypeList = new Vector();
92

    
93
    try {
94

    
95
      PreparedStatement pstmt =
96
        conn.prepareStatement("SELECT public_id FROM xml_catalog");
97

    
98
      pstmt.execute();
99
      ResultSet rs = pstmt.getResultSet();
100
      boolean tableHasRows = rs.next();
101
      while (tableHasRows) {
102
           doctypeList.addElement(rs.getString(1));
103
           tableHasRows = rs.next();
104
      }
105
      
106
      pstmt.close();
107

    
108
    } catch (SQLException e) {
109
      System.out.println("DBUtil.readDoctypes(): " + e.getMessage());
110
      throw e;
111
    }
112

    
113
    return formatToXML(doctypeList, "doctype");
114
  }
115

    
116
  /**
117
   * read Data Guide for a given doctype from db connection in XML format
118
   * select all distinct absolute paths from xml_index table
119
   */
120
  public String readDataGuide( String doctype )
121
        throws SQLException  {
122

    
123
    Vector dataguide = new Vector();
124
    String path;
125
    PreparedStatement pstmt;
126
    
127
    try {
128

    
129
        if ( doctype != null ) {
130
            pstmt = conn.prepareStatement("SELECT distinct path, nodeid " + 
131
                                          "FROM xml_index " +
132
                                          "WHERE path LIKE '/%' " + 
133
                                          "AND doctype LIKE ? " +
134
                                          "ORDER BY nodeid");
135
            pstmt.setString(1, doctype);
136
        } else {
137
            pstmt = conn.prepareStatement("SELECT distinct path, doctype, nodeid " + 
138
                                          "FROM xml_index " +
139
                                          "WHERE path LIKE '/%' " + 
140
                                          "ORDER BY doctype, nodeid");
141
        }
142

    
143
        pstmt.execute();
144
        ResultSet rs = pstmt.getResultSet();
145
        boolean tableHasRows = rs.next();
146
        while (tableHasRows) {
147
            path = rs.getString(1);
148
            if ( dataguide.indexOf(path) == -1 ) {
149
                dataguide.addElement(path);
150
            }    
151
            tableHasRows = rs.next();
152
        }
153
      
154
        pstmt.close();
155

    
156
    } catch (SQLException e) {
157
      System.out.println("DBUtil.readDataGuide(): " + e.getMessage());
158
      throw e;
159
    }
160

    
161
    return formatToXML(dataguide);
162
  }
163

    
164
  /**
165
   * format the DataGuide ResultSet to XML
166
   */
167
  private String formatToXML(Vector resultset) {
168
  
169
    String currPath = null;
170
    String currElement = null;
171
    String prevElement = null;
172
    StringBuffer result = new StringBuffer();
173
    Enumeration rs = resultset.elements(); 
174
    Stack st = new Stack();
175
    int i = 0;
176

    
177
    result.append("<?xml version=\"1.0\"?>\n");
178
    result.append("<resultset>\n"); 
179
    
180
    while (rs.hasMoreElements()) {
181
        currPath = (String)rs.nextElement();
182
        while ( !In(prevElement, currPath) ) {
183
            currElement = (String)st.pop();
184
            result.append(pad(" ",i--) + "</" + currElement + ">\n");
185
            if ( st.empty() ) 
186
                prevElement = null;
187
            else    
188
                prevElement = (String)st.peek();
189
        }    
190
        currElement = getElementFromPath(currPath);
191
        st.push(currElement);
192
        result.append(pad(" ",++i) + "<" + currElement + ">\n");
193
        prevElement = currElement;
194
    }
195
    while ( !st.empty() ) {
196
        prevElement = (String)st.pop();
197
        result.append(pad(" ",i--) + "</" + prevElement + ">\n");
198
    }    
199
    result.append("</resultset>\n"); 
200

    
201
    return result.toString();
202
  }
203

    
204
  /**
205
   * check if element is in path like /elem1/elem2/elemn3
206
   */
207
  private boolean In(String element, String path) {
208
    
209
    if ( element == null ) return true;
210
    return ( path.indexOf(element) != -1 );
211
  }
212

    
213
  /**
214
   * get last element from path like /elem1/elem2/elemn3
215
   */
216
  private String getElementFromPath(String path) {
217
    
218
    return ( path.substring(path.lastIndexOf("/")+1) );
219
  }
220

    
221
  /**
222
   * repeates the str n-times
223
   */
224
  private String pad(String str, int n) {
225
    
226
    String result = "";
227
    for ( int i = 0; i < n; i++ )
228
        result = result.concat(str);
229
        
230
    return result;    
231
  }
232

    
233
  /**
234
   * format the ResultSet to XML
235
   */
236
  private String formatToXML(Vector resultset, String tag) {
237
  
238
    String val = null;
239
    StringBuffer result = new StringBuffer();
240
    Enumeration rs = resultset.elements(); 
241

    
242
    result.append("<?xml version=\"1.0\"?>\n");
243
    result.append("<resultset>\n"); 
244
    while (rs.hasMoreElements()) {
245
        val = (String)rs.nextElement();
246
        result.append("   <" + tag + ">" + val + "</" + tag + ">\n");
247
    }
248
    result.append("</resultset>\n"); 
249
    
250
    return result.toString();
251
  }
252

    
253

    
254
}
(15-15/27)