[dna-commits] DNA SVN: r816 - in trunk/dna-jcr/src: test/java/org/jboss/dna/jcr and 1 other directory.

dna-commits at lists.jboss.org dna-commits at lists.jboss.org
Mon Apr 13 15:20:51 EDT 2009


Author: rhauch
Date: 2009-04-13 15:20:51 -0400 (Mon, 13 Apr 2009)
New Revision: 816

Modified:
   trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrQueryManager.java
   trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSession.java
   trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrWorkspaceTest.java
Log:
DNA-347 JcrQuery.storeAsNode Is Not Implemented

Applied the patch, which implements the JcrQuery.storeAsNode(...) method.

Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrQueryManager.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrQueryManager.java	2009-04-13 19:13:43 UTC (rev 815)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrQueryManager.java	2009-04-13 19:20:51 UTC (rev 816)
@@ -26,8 +26,9 @@
 import java.util.Arrays;
 import javax.jcr.ItemNotFoundException;
 import javax.jcr.Node;
+import javax.jcr.PathNotFoundException;
 import javax.jcr.RepositoryException;
-import javax.jcr.UnsupportedRepositoryOperationException;
+import javax.jcr.nodetype.ConstraintViolationException;
 import javax.jcr.query.InvalidQueryException;
 import javax.jcr.query.Query;
 import javax.jcr.query.QueryManager;
@@ -64,11 +65,11 @@
      * {@code InvalidQueryException} is thrown. The language must be a string from among those returned by {@code
      * QueryManager#getSupportedQueryLanguages()}; if it is not, then an {@code InvalidQueryException} is thrown.
      * 
-     * @param statement 
+     * @param statement
      * @param language
      * @param storedNode
      * @return A {@code Query} object
-     * @throws InvalidQueryException if statement is invalid or language is unsupported. 
+     * @throws InvalidQueryException if statement is invalid or language is unsupported.
      * @see javax.jcr.query.QueryManager#createQuery(java.lang.String, java.lang.String)
      */
     private Query createQuery( String statement,
@@ -179,8 +180,21 @@
          * 
          * @see javax.jcr.query.Query#storeAsNode(java.lang.String)
          */
-        public Node storeAsNode( java.lang.String absPath ) throws UnsupportedRepositoryOperationException {
-            throw new UnsupportedRepositoryOperationException();
+        public Node storeAsNode( java.lang.String absPath )
+            throws PathNotFoundException, ConstraintViolationException, RepositoryException {
+            NamespaceRegistry namespaces = this.session.namespaces();
+            
+            Path path = session.getExecutionContext().getValueFactories().getPathFactory().create(absPath);
+            Path parentPath = path.getParent();
+
+            Node parentNode = session.getNode(parentPath);
+            Node queryNode = parentNode.addNode(path.relativeTo(parentPath).getString(namespaces),
+                               JcrNtLexicon.QUERY.getString(namespaces));
+            
+            queryNode.setProperty(JcrLexicon.LANGUAGE.getString(namespaces), this.language);
+            queryNode.setProperty(JcrLexicon.STATEMENT.getString(namespaces), this.statement);
+            
+            return queryNode;
         }
 
     }

Modified: trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSession.java
===================================================================
--- trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSession.java	2009-04-13 19:13:43 UTC (rev 815)
+++ trunk/dna-jcr/src/main/java/org/jboss/dna/jcr/JcrSession.java	2009-04-13 19:20:51 UTC (rev 816)
@@ -406,6 +406,7 @@
      * @throws RepositoryException if there is a problem
      */
     Node getNode( Path path ) throws RepositoryException, PathNotFoundException {
+        if (path.isRoot()) return cache.findJcrRootNode();
         return cache.findJcrNode(null, path.relativeTo(rootPath));
     }
 
@@ -452,9 +453,7 @@
         return new ValueFactory() {
 
             public Value createValue( String value,
-                                      int propertyType ) 
-                throws ValueFormatException
-            {
+                                      int propertyType ) throws ValueFormatException {
                 return new JcrValue(valueFactories, sessionCache, propertyType, convertValueToType(value, propertyType));
             }
 
@@ -488,8 +487,9 @@
             public Value createValue( String value ) {
                 return new JcrValue(valueFactories, sessionCache, PropertyType.STRING, value);
             }
-            
-            Object convertValueToType(Object value, int toType) throws ValueFormatException { 
+
+            Object convertValueToType( Object value,
+                                       int toType ) throws ValueFormatException {
                 switch (toType) {
                     case PropertyType.BOOLEAN:
                         try {

Modified: trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrWorkspaceTest.java
===================================================================
--- trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrWorkspaceTest.java	2009-04-13 19:13:43 UTC (rev 815)
+++ trunk/dna-jcr/src/test/java/org/jboss/dna/jcr/JcrWorkspaceTest.java	2009-04-13 19:20:51 UTC (rev 816)
@@ -30,6 +30,9 @@
 import java.util.HashMap;
 import java.util.Map;
 import javax.jcr.NamespaceRegistry;
+import javax.jcr.Node;
+import javax.jcr.query.Query;
+import javax.jcr.query.QueryManager;
 import org.jboss.dna.graph.ExecutionContext;
 import org.jboss.dna.graph.Graph;
 import org.jboss.dna.graph.JcrLexicon;
@@ -56,7 +59,7 @@
     @Mock
     private JcrRepository repository;
     private RepositoryNodeTypeManager repoManager;
-    
+
     @Before
     public void beforeEach() throws Exception {
         final String repositorySourceName = "repository";
@@ -94,12 +97,12 @@
 
         // Stub out the repository, since we only need a few methods ...
         MockitoAnnotations.initMocks(this);
-        
+
         JcrNodeTypeSource source = null;
         source = new JcrBuiltinNodeTypeSource(context, source);
         source = new DnaBuiltinNodeTypeSource(context, source);
         repoManager = new RepositoryNodeTypeManager(context, source);
-        
+
         stub(repository.getRepositorySourceName()).toReturn(repositorySourceName);
         stub(repository.getRepositoryTypeManager()).toReturn(repoManager);
         stub(repository.getConnectionFactory()).toReturn(connectionFactory);
@@ -174,6 +177,49 @@
     }
 
     @Test
+    public void shouldCreateQuery() throws Exception {
+        String statement = "Some query syntax";
+
+        QueryManager queryManager = workspace.getQueryManager();
+        Query query = queryManager.createQuery(statement, Query.XPATH);
+
+        assertThat(query, is(notNullValue()));
+        assertThat(query.getLanguage(), is(Query.XPATH));
+        assertThat(query.getStatement(), is(statement));
+    }
+
+    @Test
+    public void shouldStoreQueryAsNode() throws Exception {
+        String statement = "Some query syntax";
+
+        QueryManager queryManager = workspace.getQueryManager();
+        Query query = queryManager.createQuery(statement, Query.XPATH);
+
+        Node node = query.storeAsNode("/storedQuery");
+        assertThat(node, is(notNullValue()));
+        assertThat(node.getPrimaryNodeType().getName(), is("nt:query"));
+        assertThat(node.getProperty("jcr:language").getString(), is(Query.XPATH));
+        assertThat(node.getProperty("jcr:statement").getString(), is(statement));
+    }
+
+    @Test
+    public void shouldLoadStoredQuery() throws Exception {
+        String statement = "Some query syntax";
+
+        QueryManager queryManager = workspace.getQueryManager();
+        Query query = queryManager.createQuery(statement, Query.XPATH);
+
+        Node node = query.storeAsNode("/storedQuery");
+
+        Query loaded = queryManager.getQuery(node);
+
+        assertThat(loaded, is(notNullValue()));
+        assertThat(loaded.getLanguage(), is(Query.XPATH));
+        assertThat(loaded.getStatement(), is(statement));
+        assertThat(loaded.getStoredQueryPath(), is(node.getPath()));
+    }
+
+    @Test
     public void shouldProvideSession() throws Exception {
         assertThat(workspace.getSession(), is(notNullValue()));
     }




More information about the dna-commits mailing list