Project

General

Profile

« Previous | Next » 

Revision 86

Added by Matt Jones over 24 years ago

added XSL formatting to document display for metacat servlet

View differences:

src/edu/ucsb/nceas/metacat/DBReader.java
92 92
  }
93 93
  
94 94
  /**
95
   * Get the root node id for an XML document given a document id
96
   *
97
   * @param docid the document node contains the root of the document
98
   * @returns long the nodeid of the root node for this document
99
   */
100
  public long getRootNode(long docid) {
101
    // Now look up the root node id
102
    long rootnodeid = 0;
103

  
104
    try {
105
      PreparedStatement pstmt =
106
        conn.prepareStatement("SELECT rootnodeid " +
107
                "FROM xml_documents " +
108
                "WHERE docid = ?");
109
      // Bind the values to the query
110
      pstmt.setLong(1, docid);
111

  
112
      pstmt.execute();
113
      try {
114
        ResultSet rs = pstmt.getResultSet();
115
        try {
116
          boolean tableHasRows = rs.next();
117
          if (tableHasRows) {
118
            try {
119
              rootnodeid = rs.getLong(1);
120

  
121
            } catch (SQLException e) {
122
              System.out.println("Error with getLong: " + e.getMessage());
123
            }
124
          }
125
        } catch (SQLException e) {
126
          System.out.println("Error with next: " + e.getMessage());
127
        }
128
      } catch (SQLException e) {
129
        System.out.println("Error with getrset: " + e.getMessage());
130
      }
131
      pstmt.close();
132
    } catch (SQLException e) {
133
      System.out.println("Error getting id: " + e.getMessage());
134
    }
135

  
136
    return rootnodeid;
137
  }
138

  
139
  /**
95 140
   * Create an XML document from the database starting with the element 
96 141
   * having element_id nodeid
97 142
   *
98
   * @param nodeid the node that will represent the root of the document
143
   * @param docid the document that we want retrieved
99 144
   */
100
  public String readXMLDocument(long nodeid) {
145
  public String readXMLDocument(long docid) {
101 146
    StringBuffer doc = new StringBuffer();
102 147

  
103
    ReaderElement element = new ReaderElement(conn, nodeid);
148
    ReaderElement element = new ReaderElement(conn, getRootNode(docid));
104 149
    doc.append("<?xml version=\"1.0\"?>\n");
105 150
    doc.append(element.toString());
106 151

  
src/edu/ucsb/nceas/metacat/MetaCatServlet.java
170 170
      Hashtable nodelist = null;
171 171
      String query = ((String[])params.get("query"))[0]; 
172 172
      if (queryobj != null) {
173
        nodelist = queryobj.findRootNodes(query);
173
        nodelist = queryobj.findDocuments(query);
174 174
      } else {
175 175
        out.println("Query Object Init failed.");
176 176
	/*
......
247 247
          pstmt =
248 248
            conn.prepareStatement("SELECT system_id " +
249 249
                    "FROM xml_catalog_entities " +
250
                    "WHERE source_doctype LIKE ? " +
250
                    "WHERE source_doctype LIKE " +
251
                    "  (SELECT doctype from xml_documents " +
252
                    "    WHERE docid = ? ) " +
251 253
                    "AND target_doctype LIKE ?");
252 254
          // Bind the values to the query
253
          pstmt.setString(1, "-//NCEAS//eml-dataset//EN");
255
          //pstmt.setString(1, "-//NCEAS//eml-dataset//EN");
256
          pstmt.setLong(1, new Long(docid).longValue());
254 257
          pstmt.setString(2, "-//W3C//HTML//EN");
255 258
  
256 259
          pstmt.execute();
......
276 279
          System.out.println("Error getting id: " + e.getMessage());
277 280
        }
278 281
 
282
        //out.println(xsl_system_id);
283

  
279 284
        // Try to apply the style
280 285
        try {
281 286
          XSLStylesheet style = new XSLStylesheet(new URL(xsl_system_id), null);
......
283 288
                     (Reader)(new StringReader(doc)),null);
284 289
          htmldoc.print(out);
285 290
        } catch (Exception e) {
286
          out.println("Error transforming document:\n" + e.getMessage());
291
          //out.println("Error transforming document:\n" + e.getMessage());
292
          // set content type and other response header fields first
293
          response.setContentType("text/xml");
294
          out.println(doc);
287 295
        }
288 296
      }
289 297
  }
src/edu/ucsb/nceas/metacat/DBSimpleQuery.java
69 69
                              dbstring, user, password);
70 70
          // Execute the simple query
71 71
          DBSimpleQuery rd = new DBSimpleQuery(dbconn);
72
          Hashtable nodelist = rd.findRootNodes(query);
72
          Hashtable nodelist = rd.findDocuments(query);
73 73

  
74 74
          // Print the reulting root nodes
75 75
          StringBuffer result = new StringBuffer();
76
          long nodeid;
76
          long docid = 0;
77 77
          result.append("<?xml version=\"1.0\"?>\n");
78 78
          result.append("<resultset>\n");
79
          Enumeration rootlist = nodelist.keys(); 
80
          while (rootlist.hasMoreElements()) {
81
            nodeid = ((Long)rootlist.nextElement()).longValue();
82
            result.append("  <nodeid>").append(nodeid).append("</nodeid>\n");
79
          Enumeration doclist = nodelist.keys(); 
80
          while (doclist.hasMoreElements()) {
81
            docid = ((Long)doclist.nextElement()).longValue();
82
            result.append("  <docid>").append(docid).append("</docid>\n");
83 83
          }
84 84
          result.append("</resultset>");
85 85

  
......
96 96
  /**
97 97
   * construct an instance of the DBSimpleQuery class 
98 98
   *
99
   * <p>Generally, one would call the findRootNodes() routine after creating 
99
   * <p>Generally, one would call the findDocuments() routine after creating 
100 100
   * an instance to specify the search query</p>
101 101
   *
102 102
   * @param conn the JDBC connection that we use for the query
......
114 114
   *
115 115
   * @param query the text to search for
116 116
   */
117
  public Hashtable findRootNodes(String query) {
117
  public Hashtable findDocuments(String query) {
118 118
      Hashtable	nodeListResult = new Hashtable();
119 119
      Hashtable	rootListResult = new Hashtable();
120
      Hashtable	 docListResult = new Hashtable();
120 121

  
121 122
      PreparedStatement pstmt;
122 123
      long nodeid;
......
200 201
        rootListResult.put(new Long(nodeid),new Long(nodeid));
201 202
      } 
202 203

  
203
      return rootListResult;
204
      // Now look up the document id
205
      long docid = 0;
206
      Enumeration rootlist = rootListResult.keys();
207
      while (rootlist.hasMoreElements()) {
208
        nodeid = ((Long)rootlist.nextElement()).longValue();
209

  
210
        try {
211
          pstmt =
212
            conn.prepareStatement("SELECT docid " +
213
                    "FROM xml_documents " +
214
                    "WHERE rootnodeid = ?");
215
          // Bind the values to the query
216
          pstmt.setLong(1, nodeid);
217
 
218
          pstmt.execute();
219
          try {
220
            ResultSet rs = pstmt.getResultSet();
221
            try {
222
              boolean tableHasRows = rs.next();
223
              while (tableHasRows) {
224
                try {
225
                  docid = rs.getLong(1);
226

  
227
                } catch (SQLException e) {
228
                  System.out.println("Error with getLong: " + e.getMessage());
229
                }
230

  
231
                // Advance to the next record in the cursor
232
                tableHasRows = rs.next();
233

  
234
              }
235
            } catch (SQLException e) {
236
              System.out.println("Error with next: " + e.getMessage());
237
            }
238
          } catch (SQLException e) {
239
            System.out.println("Error with getrset: " + e.getMessage());
240
          }
241
          pstmt.close();
242
        } catch (SQLException e) {
243
          System.out.println("Error getting id: " + e.getMessage());
244
        }
245

  
246
        // Store the document id and the root node id
247
        docListResult.put(new Long(docid),new Long(nodeid));
248
      }
249

  
250
      return docListResult;
204 251
  }
205 252
}
DBReader.java
92 92
  }
93 93
  
94 94
  /**
95
   * Get the root node id for an XML document given a document id
96
   *
97
   * @param docid the document node contains the root of the document
98
   * @returns long the nodeid of the root node for this document
99
   */
100
  public long getRootNode(long docid) {
101
    // Now look up the root node id
102
    long rootnodeid = 0;
103

  
104
    try {
105
      PreparedStatement pstmt =
106
        conn.prepareStatement("SELECT rootnodeid " +
107
                "FROM xml_documents " +
108
                "WHERE docid = ?");
109
      // Bind the values to the query
110
      pstmt.setLong(1, docid);
111

  
112
      pstmt.execute();
113
      try {
114
        ResultSet rs = pstmt.getResultSet();
115
        try {
116
          boolean tableHasRows = rs.next();
117
          if (tableHasRows) {
118
            try {
119
              rootnodeid = rs.getLong(1);
120

  
121
            } catch (SQLException e) {
122
              System.out.println("Error with getLong: " + e.getMessage());
123
            }
124
          }
125
        } catch (SQLException e) {
126
          System.out.println("Error with next: " + e.getMessage());
127
        }
128
      } catch (SQLException e) {
129
        System.out.println("Error with getrset: " + e.getMessage());
130
      }
131
      pstmt.close();
132
    } catch (SQLException e) {
133
      System.out.println("Error getting id: " + e.getMessage());
134
    }
135

  
136
    return rootnodeid;
137
  }
138

  
139
  /**
95 140
   * Create an XML document from the database starting with the element 
96 141
   * having element_id nodeid
97 142
   *
98
   * @param nodeid the node that will represent the root of the document
143
   * @param docid the document that we want retrieved
99 144
   */
100
  public String readXMLDocument(long nodeid) {
145
  public String readXMLDocument(long docid) {
101 146
    StringBuffer doc = new StringBuffer();
102 147

  
103
    ReaderElement element = new ReaderElement(conn, nodeid);
148
    ReaderElement element = new ReaderElement(conn, getRootNode(docid));
104 149
    doc.append("<?xml version=\"1.0\"?>\n");
105 150
    doc.append(element.toString());
106 151

  
MetaCatServlet.java
170 170
      Hashtable nodelist = null;
171 171
      String query = ((String[])params.get("query"))[0]; 
172 172
      if (queryobj != null) {
173
        nodelist = queryobj.findRootNodes(query);
173
        nodelist = queryobj.findDocuments(query);
174 174
      } else {
175 175
        out.println("Query Object Init failed.");
176 176
	/*
......
247 247
          pstmt =
248 248
            conn.prepareStatement("SELECT system_id " +
249 249
                    "FROM xml_catalog_entities " +
250
                    "WHERE source_doctype LIKE ? " +
250
                    "WHERE source_doctype LIKE " +
251
                    "  (SELECT doctype from xml_documents " +
252
                    "    WHERE docid = ? ) " +
251 253
                    "AND target_doctype LIKE ?");
252 254
          // Bind the values to the query
253
          pstmt.setString(1, "-//NCEAS//eml-dataset//EN");
255
          //pstmt.setString(1, "-//NCEAS//eml-dataset//EN");
256
          pstmt.setLong(1, new Long(docid).longValue());
254 257
          pstmt.setString(2, "-//W3C//HTML//EN");
255 258
  
256 259
          pstmt.execute();
......
276 279
          System.out.println("Error getting id: " + e.getMessage());
277 280
        }
278 281
 
282
        //out.println(xsl_system_id);
283

  
279 284
        // Try to apply the style
280 285
        try {
281 286
          XSLStylesheet style = new XSLStylesheet(new URL(xsl_system_id), null);
......
283 288
                     (Reader)(new StringReader(doc)),null);
284 289
          htmldoc.print(out);
285 290
        } catch (Exception e) {
286
          out.println("Error transforming document:\n" + e.getMessage());
291
          //out.println("Error transforming document:\n" + e.getMessage());
292
          // set content type and other response header fields first
293
          response.setContentType("text/xml");
294
          out.println(doc);
287 295
        }
288 296
      }
289 297
  }
lib/resultset.xsl
45 45
        <th><xsl:text>Title</xsl:text></th>
46 46
        </tr>
47 47

  
48
        <xsl:for-each select="resultset/nodeid">
48
        <xsl:for-each select="resultset/docid">
49 49
          <tr valign="top">
50 50
            <xsl:attribute name="class">
51 51
              <xsl:choose>
lib/style/resultset.xsl
45 45
        <th><xsl:text>Title</xsl:text></th>
46 46
        </tr>
47 47

  
48
        <xsl:for-each select="resultset/nodeid">
48
        <xsl:for-each select="resultset/docid">
49 49
          <tr valign="top">
50 50
            <xsl:attribute name="class">
51 51
              <xsl:choose>
Makefile
64 64
	java -cp $(CPATH) edu.ucsb.nceas.metacat.DBReader 1 $(USER) $(PW) 
65 65

  
66 66
rdtest1:
67
	java -cp $(CPATH) edu.ucsb.nceas.metacat.DBReader 31 $(USER) $(PW) 
67
	java -cp $(CPATH) edu.ucsb.nceas.metacat.DBReader 11 $(USER) $(PW) 
68 68

  
69 69
qtest:
70 70
	java -cp $(CPATH) edu.ucsb.nceas.metacat.DBSimpleQuery \
resultset.xsl
45 45
        <th><xsl:text>Title</xsl:text></th>
46 46
        </tr>
47 47

  
48
        <xsl:for-each select="resultset/nodeid">
48
        <xsl:for-each select="resultset/docid">
49 49
          <tr valign="top">
50 50
            <xsl:attribute name="class">
51 51
              <xsl:choose>
DBSimpleQuery.java
69 69
                              dbstring, user, password);
70 70
          // Execute the simple query
71 71
          DBSimpleQuery rd = new DBSimpleQuery(dbconn);
72
          Hashtable nodelist = rd.findRootNodes(query);
72
          Hashtable nodelist = rd.findDocuments(query);
73 73

  
74 74
          // Print the reulting root nodes
75 75
          StringBuffer result = new StringBuffer();
76
          long nodeid;
76
          long docid = 0;
77 77
          result.append("<?xml version=\"1.0\"?>\n");
78 78
          result.append("<resultset>\n");
79
          Enumeration rootlist = nodelist.keys(); 
80
          while (rootlist.hasMoreElements()) {
81
            nodeid = ((Long)rootlist.nextElement()).longValue();
82
            result.append("  <nodeid>").append(nodeid).append("</nodeid>\n");
79
          Enumeration doclist = nodelist.keys(); 
80
          while (doclist.hasMoreElements()) {
81
            docid = ((Long)doclist.nextElement()).longValue();
82
            result.append("  <docid>").append(docid).append("</docid>\n");
83 83
          }
84 84
          result.append("</resultset>");
85 85

  
......
96 96
  /**
97 97
   * construct an instance of the DBSimpleQuery class 
98 98
   *
99
   * <p>Generally, one would call the findRootNodes() routine after creating 
99
   * <p>Generally, one would call the findDocuments() routine after creating 
100 100
   * an instance to specify the search query</p>
101 101
   *
102 102
   * @param conn the JDBC connection that we use for the query
......
114 114
   *
115 115
   * @param query the text to search for
116 116
   */
117
  public Hashtable findRootNodes(String query) {
117
  public Hashtable findDocuments(String query) {
118 118
      Hashtable	nodeListResult = new Hashtable();
119 119
      Hashtable	rootListResult = new Hashtable();
120
      Hashtable	 docListResult = new Hashtable();
120 121

  
121 122
      PreparedStatement pstmt;
122 123
      long nodeid;
......
200 201
        rootListResult.put(new Long(nodeid),new Long(nodeid));
201 202
      } 
202 203

  
203
      return rootListResult;
204
      // Now look up the document id
205
      long docid = 0;
206
      Enumeration rootlist = rootListResult.keys();
207
      while (rootlist.hasMoreElements()) {
208
        nodeid = ((Long)rootlist.nextElement()).longValue();
209

  
210
        try {
211
          pstmt =
212
            conn.prepareStatement("SELECT docid " +
213
                    "FROM xml_documents " +
214
                    "WHERE rootnodeid = ?");
215
          // Bind the values to the query
216
          pstmt.setLong(1, nodeid);
217
 
218
          pstmt.execute();
219
          try {
220
            ResultSet rs = pstmt.getResultSet();
221
            try {
222
              boolean tableHasRows = rs.next();
223
              while (tableHasRows) {
224
                try {
225
                  docid = rs.getLong(1);
226

  
227
                } catch (SQLException e) {
228
                  System.out.println("Error with getLong: " + e.getMessage());
229
                }
230

  
231
                // Advance to the next record in the cursor
232
                tableHasRows = rs.next();
233

  
234
              }
235
            } catch (SQLException e) {
236
              System.out.println("Error with next: " + e.getMessage());
237
            }
238
          } catch (SQLException e) {
239
            System.out.println("Error with getrset: " + e.getMessage());
240
          }
241
          pstmt.close();
242
        } catch (SQLException e) {
243
          System.out.println("Error getting id: " + e.getMessage());
244
        }
245

  
246
        // Store the document id and the root node id
247
        docListResult.put(new Long(docid),new Long(nodeid));
248
      }
249

  
250
      return docListResult;
204 251
  }
205 252
}

Also available in: Unified diff