jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r735668 - in /jackrabbit/sandbox/jackrabbit-jdbc2jcr/src/main/java/org/apache/jackrabbit/jdbc: JCRColumn.java JCRConnection.java JCRView.java
Date Mon, 19 Jan 2009 11:49:07 GMT
Author: jukka
Date: Mon Jan 19 03:49:03 2009
New Revision: 735668

URL: http://svn.apache.org/viewvc?rev=735668&view=rev
Log:
jdbc2jcr: Automatically generate views for all registered node types

Modified:
    jackrabbit/sandbox/jackrabbit-jdbc2jcr/src/main/java/org/apache/jackrabbit/jdbc/JCRColumn.java
    jackrabbit/sandbox/jackrabbit-jdbc2jcr/src/main/java/org/apache/jackrabbit/jdbc/JCRConnection.java
    jackrabbit/sandbox/jackrabbit-jdbc2jcr/src/main/java/org/apache/jackrabbit/jdbc/JCRView.java

Modified: jackrabbit/sandbox/jackrabbit-jdbc2jcr/src/main/java/org/apache/jackrabbit/jdbc/JCRColumn.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-jdbc2jcr/src/main/java/org/apache/jackrabbit/jdbc/JCRColumn.java?rev=735668&r1=735667&r2=735668&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-jdbc2jcr/src/main/java/org/apache/jackrabbit/jdbc/JCRColumn.java
(original)
+++ jackrabbit/sandbox/jackrabbit-jdbc2jcr/src/main/java/org/apache/jackrabbit/jdbc/JCRColumn.java
Mon Jan 19 03:49:03 2009
@@ -32,8 +32,6 @@
 
 class JCRColumn {
 
-    private final String name;
-
     private final String path;
 
     private final int type;
@@ -44,8 +42,7 @@
 
     private final boolean isName;
 
-    public JCRColumn(String name, String path, int type, boolean nullable) {
-        this.name = name;
+    public JCRColumn(String path, int type, boolean nullable) {
         this.path = path;
         this.type = type;
         this.nullable = nullable;
@@ -55,10 +52,6 @@
 
     //-----------------------------------------------------< Column metadata >
 
-    public String getColumnName() {
-        return name;
-    }
-
     public int getColumnType() {
         return type;
     }

Modified: jackrabbit/sandbox/jackrabbit-jdbc2jcr/src/main/java/org/apache/jackrabbit/jdbc/JCRConnection.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-jdbc2jcr/src/main/java/org/apache/jackrabbit/jdbc/JCRConnection.java?rev=735668&r1=735667&r2=735668&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-jdbc2jcr/src/main/java/org/apache/jackrabbit/jdbc/JCRConnection.java
(original)
+++ jackrabbit/sandbox/jackrabbit-jdbc2jcr/src/main/java/org/apache/jackrabbit/jdbc/JCRConnection.java
Mon Jan 19 03:49:03 2009
@@ -29,6 +29,8 @@
 import javax.jcr.NodeIterator;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
+import javax.jcr.nodetype.NodeType;
+import javax.jcr.nodetype.NodeTypeIterator;
 import javax.jcr.query.Query;
 import javax.jcr.query.QueryManager;
 
@@ -75,7 +77,32 @@
     }
 
     private void createViews() throws SQLException {
-        Statement statement = getDelegate().createStatement();
+        createNodeTypeViews();
+        createCustomViews();
+    }
+
+    private void createNodeTypeViews() throws SQLException {
+        try {
+            NodeTypeIterator iterator =
+                session.getWorkspace().getNodeTypeManager().getAllNodeTypes();
+            while (iterator.hasNext()) {
+                NodeType type = iterator.nextNodeType();
+                try {
+                    createView(new JCRView(session, type));
+                } catch (RepositoryException e) {
+                    System.err.println(
+                            "Failed to create view for JCR type: "
+                            + type.getName());
+                    e.printStackTrace();
+                }
+            }
+        } catch (RepositoryException e) {
+            System.err.println("Failed to create view for JCR types");
+            e.printStackTrace();
+        }
+    }
+
+    private void createCustomViews() throws SQLException {
         try {
             String jdbc = session.getNamespacePrefix(JCRDriver.JDBC_NAMESPACE);
             QueryManager manager = session.getWorkspace().getQueryManager();
@@ -85,29 +112,33 @@
             while (nodes.hasNext()) {
                 Node node = nodes.nextNode();
                 try {
-                    createView(statement, node);
+                    createView(new JCRView(node));
                 } catch (RepositoryException e) {
-                    // TODO: better handling of the error
+                    System.err.println(
+                            "Failed to create custom view: "
+                            + node.getPath());
                     e.printStackTrace();
                 }
             }
         } catch (NamespaceException e) {
-            System.out.println("No JCR views found.");
+            System.out.println("No custom views found");
         } catch (RepositoryException e) {
-            throw new SQLExceptionWithCause(
-                    "Failed to initialize JCR views", e);
-        } finally {
-            statement.close();
+            System.err.println("Failed to create custom views");
+            e.printStackTrace();
         }
     }
 
-    private void createView(Statement statement, Node node)
+    private void createView(JCRView view)
             throws SQLException, RepositoryException {
-        JCRView view = new JCRView(node);
-        views.put(view.getName(), view);
-        statement.executeUpdate(view.getFunctionSQL());
-        statement.executeUpdate(view.getViewSQL(
-                tmp.getPath(), view.getName()));
+        Statement statement = getDelegate().createStatement();
+        try {
+            views.put(view.getName(), view);
+            statement.executeUpdate(view.getFunctionSQL());
+            statement.executeUpdate(
+                    view.getViewSQL(tmp.getPath(), view.getName()));
+        } finally {
+            statement.close();
+        }
     }
 
     public JCRView getView(String key) throws SQLException {

Modified: jackrabbit/sandbox/jackrabbit-jdbc2jcr/src/main/java/org/apache/jackrabbit/jdbc/JCRView.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/jackrabbit-jdbc2jcr/src/main/java/org/apache/jackrabbit/jdbc/JCRView.java?rev=735668&r1=735667&r2=735668&view=diff
==============================================================================
--- jackrabbit/sandbox/jackrabbit-jdbc2jcr/src/main/java/org/apache/jackrabbit/jdbc/JCRView.java
(original)
+++ jackrabbit/sandbox/jackrabbit-jdbc2jcr/src/main/java/org/apache/jackrabbit/jdbc/JCRView.java
Mon Jan 19 03:49:03 2009
@@ -23,15 +23,17 @@
 import java.sql.SQLException;
 import java.sql.Timestamp;
 import java.sql.Types;
-import java.util.ArrayList;
 import java.util.Calendar;
-import java.util.List;
+import java.util.LinkedHashMap;
+import java.util.Map;
 
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
 import javax.jcr.PropertyType;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
+import javax.jcr.nodetype.NodeType;
+import javax.jcr.nodetype.PropertyDefinition;
 import javax.jcr.query.Query;
 import javax.jcr.query.QueryManager;
 
@@ -45,89 +47,137 @@
 
     private final String language;
 
-    private final List<JCRColumn> columns = new ArrayList<JCRColumn>();
+    private final Map<String, JCRColumn> columns =
+        new LinkedHashMap<String, JCRColumn>();
 
-    public JCRView(Node node)
-            throws RepositoryException {
-        this.session = node.getSession();
+    private JCRView(
+            Session session, String name, String query, String language) {
+        this.session = session;
+        this.name = name;
+        this.query = query;
+        this.language = language;
+
+        columns.put(
+                "jcr_path",
+                new JCRColumn("jcr:path()", Types.VARCHAR, false));
+        columns.put(
+                "jcr_name",
+                new JCRColumn("jcr:name()", Types.VARCHAR, false));
+        columns.put(
+                "jcr_primaryType",
+                new JCRColumn("jcr:primaryType", Types.VARCHAR, false));
+        columns.put(
+                "jcr_mixinTypes",
+                new JCRColumn("jcr:mixinTypes", Types.ARRAY, false));
+    }
+
+    public JCRView(Session session, NodeType type) throws RepositoryException {
+        this(session, type.getName().replace(':', '_'),
+                "//element(*," + type.getName() + ")", Query.XPATH);
+
+        for (PropertyDefinition def : type.getPropertyDefinitions()) {
+            String label = def.getName().replace(':', '_');
+            if (!columns.containsKey(label) && !label.equals("*")) {
+                int ptype = getType(def.getRequiredType());
+                if (def.isMultiple()) {
+                    ptype = Types.ARRAY;
+                }
 
-        if (node.hasProperty("name")) {
-            this.name = node.getProperty("name").getString();
-        } else {
-            this.name = node.getName();
-        }
+                boolean nullable = !def.isMandatory();
 
-        if (node.hasProperty("query")) {
-            this.query = node.getProperty("query").getString();
-        } else {
-            this.query = "//*";
-        }
-
-        if (node.hasProperty("language")) {
-            this.language = node.getProperty("language").getString();
-        } else {
-            this.language = Query.XPATH;
+                columns.put(
+                        label, new JCRColumn(def.getName(), ptype, nullable));
+            }
         }
+    }
 
-        columns.add(new JCRColumn(
-                "jcr_path", "jcr:path()", Types.VARCHAR, false));
-        columns.add(new JCRColumn(
-                "jcr_name", "jcr:name()", Types.VARCHAR, false));
-        columns.add(new JCRColumn(
-                "jcr_primaryType", "jcr:primaryType", Types.VARCHAR, false));
-        columns.add(new JCRColumn(
-                "jcr_mixinTypes", "jcr:mixinTypes", Types.ARRAY, false));
+    public JCRView(Node node) throws RepositoryException {
+        this(node.getSession(), getName(node),
+                getQuery(node), getLanguage(node));
 
         String jdbc = session.getNamespacePrefix(JCRDriver.JDBC_NAMESPACE);
         NodeIterator nodes = node.getNodes();
         while (nodes.hasNext()) {
             Node column = nodes.nextNode();
             if (column.isNodeType(jdbc + ":column")) {
-                String name = column.getName();
- 
-                boolean multiple = false;
-                if (node.hasProperty("multiple")) {
-                    multiple = node.getProperty("multiple").getBoolean();
-                }
+                String label = column.getName().replace(':', '_');
+                if (!columns.containsKey(label)) {
+                    boolean multiple = false;
+                    if (node.hasProperty("multiple")) {
+                        multiple = node.getProperty("multiple").getBoolean();
+                    }
 
-                int type = Types.VARCHAR;
-                if (multiple) {
-                    type = Types.ARRAY;
-                } else if (node.hasProperty("type")) {
-                    String typeName = node.getProperty("type").getString();
-                    if (typeName.equalsIgnoreCase(
-                            PropertyType.TYPENAME_BINARY)) {
-                        type = Types.BLOB;
-                    } else if (typeName.equalsIgnoreCase(
-                            PropertyType.TYPENAME_BOOLEAN)) {
-                        type = Types.BOOLEAN;
-                    } else if (typeName.equalsIgnoreCase(
-                            PropertyType.TYPENAME_DATE)) {
-                        type = Types.TIMESTAMP;
-                    } else if (typeName.equalsIgnoreCase(
-                            PropertyType.TYPENAME_DOUBLE)) {
-                        type = Types.DOUBLE;
-                    } else if (typeName.equalsIgnoreCase(
-                            PropertyType.TYPENAME_LONG)) {
-                        type = Types.BIGINT;
+                    int type = Types.VARCHAR;
+                    if (multiple) {
+                        type = Types.ARRAY;
+                    } else if (node.hasProperty("type")) {
+                        type = getType(PropertyType.valueFromName(
+                                node.getProperty("type").getString()));
                     }
-                }
 
-                String path = name;
-                if (node.hasProperty("path")) {
-                    path = node.getProperty("path").getString();
-                }
+                    String path = column.getName();
+                    if (node.hasProperty("path")) {
+                        path = node.getProperty("path").getString();
+                    }
 
-                columns.add(new JCRColumn(name, path, type, true));
+                    columns.put(label, new JCRColumn(path, type, true));
+                }
             }
         }
     }
 
+    private static String getName(Node node) throws RepositoryException {
+        if (node.hasProperty("name")) {
+            return node.getProperty("name").getString();
+        } else {
+            return node.getName();
+        }
+    }
+
+    private static String getQuery(Node node) throws RepositoryException {
+        if (node.hasProperty("query")) {
+            return node.getProperty("query").getString();
+        } else {
+            return "//*";
+        }
+    }
+
+    private static String getLanguage(Node node) throws RepositoryException {
+        if (node.hasProperty("query") && node.hasProperty("language")) {
+            return node.getProperty("language").getString();
+        } else {
+            return Query.XPATH;
+        }
+    }
+
+    private static int getType(int type) {
+        switch (type) {
+        case PropertyType.BINARY:
+            return Types.BLOB;
+        case PropertyType.DATE:
+            return Types.TIMESTAMP;
+        case PropertyType.DOUBLE:
+            return Types.DOUBLE;
+        case PropertyType.LONG:
+            return Types.BIGINT;
+        default:
+            return Types.VARCHAR;
+        }
+    }
+
+    private String getLabel(int index) throws SQLException {
+        if (index < 1 || index > columns.size()) {
+            throw new SQLException("Invalid column index: " + index);
+        } else {
+            return columns.keySet().toArray(new String[0])[index - 1];
+        }
+    }
+
     private JCRColumn getColumn(int index) throws SQLException {
         if (index < 1 || index > columns.size()) {
             throw new SQLException("Invalid column index: " + index);
         } else {
-            return columns.get(index - 1);
+            return columns.values().toArray(new JCRColumn[0])[index - 1];
         }
     }
 
@@ -142,14 +192,14 @@
         builder.append(" RETURNS TABLE (");
 
         int i = 0;
-        for (JCRColumn column : columns) {
+        for (Map.Entry<String, JCRColumn> entry : columns.entrySet()) {
             if (i++ > 0) {
                 builder.append(",");
             }
             builder.append(" ");
-            builder.append(column.getColumnName()); // TODO escape
+            builder.append(entry.getKey()); // TODO escape
             builder.append(" ");
-            builder.append(column.getColumnTypeName());
+            builder.append(entry.getValue().getColumnTypeName());
         }
 
         builder.append(" )");
@@ -179,8 +229,8 @@
 
     public int findColumn(String columnLabel) throws SQLException {
         int index = 1;
-        for (JCRColumn column : columns) {
-            if (column.getColumnName().equalsIgnoreCase(columnLabel)) {
+        for (String name : columns.keySet()) {
+            if (name.equalsIgnoreCase(columnLabel)) {
                 return index;
             } else {
                 index++;
@@ -246,7 +296,7 @@
     }
 
     public String getColumnName(int column) throws SQLException {
-        return getColumn(column).getColumnName();
+        return getLabel(column);
     }
 
     public int getColumnType(int column) throws SQLException {



Mime
View raw message