jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r724762 - in /jackrabbit/sandbox/jackrabbit-jdbc2jcr: ./ pom.xml src/main/java/org/apache/jackrabbit/jdbc/TpsReport.java
Date Tue, 09 Dec 2008 15:43:06 GMT
Author: jukka
Date: Tue Dec  9 07:43:05 2008
New Revision: 724762

URL: http://svn.apache.org/viewvc?rev=724762&view=rev
Log:
jdbc2jcr: Added Thomas' TpsReport class for H2.

Added:
    jackrabbit/sandbox/jackrabbit-jdbc2jcr/src/main/java/org/apache/jackrabbit/jdbc/TpsReport.java
  (with props)
Modified:
    jackrabbit/sandbox/jackrabbit-jdbc2jcr/   (props changed)
    jackrabbit/sandbox/jackrabbit-jdbc2jcr/pom.xml

Propchange: jackrabbit/sandbox/jackrabbit-jdbc2jcr/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Tue Dec  9 07:43:05 2008
@@ -0,0 +1,5 @@
+target
+.*
+*.iml
+*.ipr
+*.iws

Modified: jackrabbit/sandbox/jackrabbit-jdbc2jcr/pom.xml
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-jdbc2jcr/pom.xml?rev=724762&r1=724761&r2=724762&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-jdbc2jcr/pom.xml (original)
+++ jackrabbit/sandbox/jackrabbit-jdbc2jcr/pom.xml Tue Dec  9 07:43:05 2008
@@ -49,7 +49,7 @@
       <groupId>org.apache.jackrabbit</groupId>
       <artifactId>jackrabbit-core</artifactId>
       <version>1.5.0</version>
-      <scope>test</test>
+      <scope>test</scope>
     </dependency>
   </dependencies>
 

Added: jackrabbit/sandbox/jackrabbit-jdbc2jcr/src/main/java/org/apache/jackrabbit/jdbc/TpsReport.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-jdbc2jcr/src/main/java/org/apache/jackrabbit/jdbc/TpsReport.java?rev=724762&view=auto
==============================================================================
--- jackrabbit/sandbox/jackrabbit-jdbc2jcr/src/main/java/org/apache/jackrabbit/jdbc/TpsReport.java
(added)
+++ jackrabbit/sandbox/jackrabbit-jdbc2jcr/src/main/java/org/apache/jackrabbit/jdbc/TpsReport.java
Tue Dec  9 07:43:05 2008
@@ -0,0 +1,166 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.jdbc;
+
+import org.apache.jackrabbit.core.TransientRepository;
+
+import java.sql.*;
+import javax.jcr.*;
+import javax.jcr.query.*;
+
+/**
+ * This example application shows how to query a JCR repository using the JDBC
+ * API. It uses a user defined Java function that is used as a table. This
+ * method may be used to query any JCR compliant repository using any
+ * application that uses the JDBC API, including a report generator such as
+ * Crystal Reports.
+ */
+public class TpsReport {
+
+    /**
+     * This method is called when executing this sample application from the
+     * command line.
+     * 
+     * @param args the command line parameters
+     */
+    public static void main(String[] args) throws Exception {
+        new TpsReport().run();
+    }
+    
+    /**
+     * Run the sample application. This will initialize the repository,
+     * initialize the database, and then run the SQL query against the database,
+     * which in turn returns the result of the JCR query.
+     */
+    void run() throws Exception {
+        initJcrRepository();
+        initDatabase();
+        runQuery();
+    }
+    
+    /**
+     * Initialize the JCR repository. This will create a repository with one
+     * node 'test' that has a property 'text'.
+     */
+    void initJcrRepository() throws Exception {
+        Repository rep = new TransientRepository();
+        Session session = rep.login(new SimpleCredentials("admin", "admin".toCharArray()));
+        Node root = session.getRootNode();
+        if (root.hasNode("test")) {
+            root.getNode("test").remove();
+        }
+        Node n = root.addNode("test");
+        n.setProperty("text", "Hello");
+        session.save();
+        session.logout();
+    }
+    
+    /**
+     * Initialize the database. This will create a database called 'jcr' that
+     * contains a Java function 'TPS_REPORT'. This only needs to be done once,
+     * before running the report. The term TPS_REPORT is used as a generic term
+     * for any kind of report.
+     */
+    void initDatabase() throws Exception {
+        Connection conn = openConnection();
+        Statement stat = conn.createStatement();
+        stat.execute("CREATE ALIAS IF NOT EXISTS " + 
+            "TPS_REPORT FOR \"" + getClass().getName() + ".getTpsReport\"");
+    }
+    
+    /**
+     * Run the query SELECT * FROM TPS_REPORT against the database. This will
+     * call the Java function getTpsReport(). The result of
+     * the query is printed to system out. The query can also be run using a
+     * reporting tool such as Crystal Reports.
+     */
+    void runQuery() throws Exception {
+        Connection conn = openConnection();
+        Statement stat = conn.createStatement();
+        ResultSet rs = stat.executeQuery(
+            "SELECT * FROM TPS_REPORT('admin', 'admin')");
+        while(rs.next()) {
+            System.out.print(rs.getString(1));
+        }
+        conn.close();
+    }
+    
+    /**
+     * Open a connection to a temporary database.
+     * 
+     * @return the connection
+     */
+    private static Connection openConnection() throws Exception {
+        Class.forName("org.h2.Driver");
+        return DriverManager.getConnection("jdbc:h2:~/jcr", "sa", "sa");
+    }
+    
+    /**
+     * This method opens a JCR session and run the XPath query '//test' against
+     * the JCR repository. This method will also convert the JCR QueryResult to
+     * a SQL ResultSet, which is then returned. Internally, this method is
+     * actually called twice: first to get the column list of the result set,
+     * and then to get the data.
+     * 
+     * @param conn
+     *            the database connection
+     * @param jcrUser
+     *            the JCR user name as set when calling the database function
+     * @param jcrPassword
+     *            the JCR password as set when calling the database function
+     * @return the SQL result set
+     */
+    public static ResultSet getTpsReport(Connection conn, String jcrUser,
+            String jcrPassword) throws Exception {
+        Statement stat = conn.createStatement();
+        stat.execute("DROP TABLE IF EXISTS TEMP");
+        stat.execute("CREATE TABLE TEMP(TEXT VARCHAR)");
+        if (!conn.getMetaData().getURL().startsWith("jdbc:columnlist:")) {
+            // first, the method is called to get the column list of the result
+            // (to parse and prepare the query and resolve column names),
+            // but here the method is called to get the actual data
+            Repository rep = new TransientRepository();
+            Session session = rep.login(new SimpleCredentials(jcrUser, jcrPassword
+                    .toCharArray()));
+            generateReportData(session, conn);
+            session.logout();
+        }
+        return stat.executeQuery("SELECT * FROM TEMP");
+    }
+    
+    /**
+     * Read the resport data from the JCR repository and insert it into the
+     * temporary table.
+     * 
+     * @param session the JCR session (source)
+     * @param conn the database connection (target)
+     */
+    private static void generateReportData(Session session, Connection conn) 
+            throws Exception {
+        QueryManager qm = session.getWorkspace().getQueryManager();
+        QueryResult result = qm.createQuery("//test", Query.XPATH).execute();
+        NodeIterator it = result.getNodes();
+        PreparedStatement prep = conn.prepareStatement(
+            "INSERT INTO TEMP VALUES(?)");
+        while (it.hasNext()) {
+            Node n = it.nextNode();
+            prep.setString(1, n.getProperty("text").getString());
+            prep.execute();
+        }
+    }
+
+}

Propchange: jackrabbit/sandbox/jackrabbit-jdbc2jcr/src/main/java/org/apache/jackrabbit/jdbc/TpsReport.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message