[Registry-dev] svn commit r10131 - in trunk/registry/modules/core/src: main/java/org/wso2/registry/jdbc main/java/org/wso2/registry/jdbc/dao main/java/org/wso2/registry/jdbc/mediatypes/builtin main/java/org/wso2/registry/secure test/java/org/wso2/registry/jdbc

svn at wso2.org svn at wso2.org
Fri Nov 23 04:03:09 PST 2007


Author: deepal
Date: Fri Nov 23 04:02:46 2007
New Revision: 10131

Modified:
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/VersionedResourceDAO.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/DefaultMediaTypeHandler.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/HSQLDBInitializer.java
   trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/JDBCRegistryTest.java
Log:
- Now we handle resource delete correctly 
  - One can add a resource called "foo/r1" then delete that and add a resource withe the same  name. And he can restore to any Version 
- Code cleanup , removed most of the unwanted code
- Btw test is failing for me I think that is due to some UM code

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java	Fri Nov 23 04:02:46 2007
@@ -306,11 +306,11 @@
 
     public void restoreVersion(String versionPath) throws RegistryException {
         String plainPath ;
-        long versionNumber = -1;
+        long versionNumber;
         if (versionPath.indexOf("?v=") != -1) {
             String[] parts = versionPath.split("\\?v=");
             plainPath = parts[0];
-            versionNumber = new Long(parts[1]).longValue();
+            versionNumber = Long.parseLong(parts[1]);
         } else {
             String msg = "Given path is not a path of a versioned resource.";
             log.error(msg);
@@ -659,7 +659,7 @@
                 }
 
                 logsDAO.addLog(resource.getId(),
-                        userID, LogEntry.RATING, new Integer(rating).toString(), conn);
+                        userID, LogEntry.RATING, Integer.toString(rating), conn);
 
             } else {
                 String msg = Messages.getMessage("rate.on.null.artfact", resourcePath);

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/VersionedResourceDAO.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/VersionedResourceDAO.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/VersionedResourceDAO.java	Fri Nov 23 04:02:46 2007
@@ -21,6 +21,8 @@
 import org.wso2.registry.RegistryConstants;
 import org.wso2.registry.RegistryException;
 import org.wso2.registry.jdbc.DatabaseConstants;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 
 import java.sql.*;
 import java.util.ArrayList;
@@ -30,13 +32,16 @@
 
 public class VersionedResourceDAO {
 
+    private static final Log log = LogFactory.getLog(VersionedResourceDAO.class);
     /**
      * Returns the given version of the artifact. If artifact is a collection all child paths will
      * be added as versioned paths in the form /a/b?v=12
      *
      * @param path : path of the artifact
      * @return Resource for the given version number
-     * @throws SQLException
+     * @throws SQLException : If something went wrong
+     * @param versionNumber  : Version number
+     * @param conn : Connection to DB
      */
     public Resource get(String path, long versionNumber, Connection conn) throws SQLException {
 
@@ -192,6 +197,11 @@
      * @throws SQLException : if somthing went wrong
      */
     public Resource getLatestVersion(String path, Connection conn) throws SQLException {
+        /*
+        * when someone give path like "root/wso2/wsas/r1" , then need to figure out the latest
+        * version of the root and from the need to figure out the version of wso2 and so so
+        * so when list outing the nodes need to give them correctly.
+        * */
 
         Resource resource = null;
 
@@ -225,6 +235,7 @@
 
         long latestVersionNumber = getLatestVerisonNumber(resource.getId(), conn);
 
+
         String sqlVersionedFields = "SELECT * FROM VERSIONS WHERE AID=? AND VN=?";
         PreparedStatement versionQuery = conn.prepareStatement(sqlVersionedFields);
         versionQuery.setLong(1, resource.getId());
@@ -239,8 +250,8 @@
         }
         // get resource chidren
         if (resource.isDirectory()) {
-
-            String[] children = getLatestChildPaths(resource.getId(), latestVersionNumber, conn);
+            String[] children = getLatestChildPaths(resource.getId(),
+                    getCurrentVerisonNumber(path, conn), conn);
             resource.setContent(children);
         }
 
@@ -260,6 +271,46 @@
         return resource;
     }
 
+    /**
+     * To get the currenet version for a give resource , current version may not be the latest
+     * version in all the caess. If someone retore a resource then the child nodes will remain same
+     * But the parent version will be changed. In that case the parent [foo/bar] verion will be the latest
+     * version but the child node verion [foo/bar/v1] will be an old verion . So when some one ask for
+     * the resource value foo/bar/v1 then we need to calculate them correctly and give that.
+     * @param path : Resource ID
+     * @param conn : Connection to DB
+     * @return : verion number of the resource user requested
+     */
+    private long getCurrentVerisonNumber(String  path, Connection conn) {
+        if (path != null) {
+            try {
+                int index = path.lastIndexOf(RegistryConstants.PATH_SEPARATOR);
+                if (index >0) {
+                    String parentPath = path.substring(0,index);
+                    long parentId = getResourceID(parentPath,conn);
+                    long latestVersion = getLatestVerisonNumber(parentId,conn);
+                    long childID = getResourceID(path,conn);
+                    String SQL = "SELECT DVN FROM DEPENDENCY WHERE AID=? AND VN=? AND DAID=?";
+                    PreparedStatement s = conn.prepareStatement(SQL);
+                    s.setLong(1, parentId);
+                    s.setLong(2, latestVersion);
+                    s.setLong(3, childID);
+                    ResultSet result = s.executeQuery();
+                    if (result.next()) {
+                        return result.getLong(1);
+                    }
+                } else {
+                   return getLatestVerisonNumber(getResourceID(path,conn),conn);
+                }
+            } catch (SQLException e) {
+                log.info("Something went wrong while calculating the current " +
+                        "version for the path : " + path+ " and the error us " + e);
+                return -1;
+            }
+        }
+        return -1;
+    }
+
     public String[] getChildPaths(long artifcatID, long versionNumber,
                                   Connection connection) throws SQLException {
 
@@ -387,6 +438,7 @@
      * @param path     Path of the new resource
      * @param resource New resource
      * @param conn     : Connection to Db
+     * @throws java.sql.SQLException : if something went wrong
      */
     public void add(String path, Resource resource, Connection conn) throws SQLException {
 
@@ -892,6 +944,35 @@
         return ps;
     }
 
+    /**
+     * This method will check whether a given resource is delete or not ,
+     * there can be mutiple resource with the same path and thier ID will
+     * vary from one to another. So when we list out the resources by
+     * giving the path then we will have one or more resources.
+     * Then we go though the list and figure out whether there is
+     * any resource which is in active state if so return true else return false
+     * @param resourcePath : reosurce path
+     * @param connection : Connection to DB
+     * @return : return true if there is a resource for the given path in
+     * the active state else false
+     */
+    public boolean isResourceActive(String resourcePath ,Connection connection ){
+        try {
+            String SQL = "SELECT AID FROM ARTIFACTS WHERE PATH=? AND STATE=?";
+            PreparedStatement selectps = connection.prepareStatement(SQL);
+            selectps.setString(1, resourcePath);
+            selectps.setInt(2, RegistryConstants.ACTIVE_STATE);
+            ResultSet childResult = selectps.executeQuery();
+            if (childResult.next()) {
+                return true;
+            }
+        } catch (SQLException e) {
+            log.info("Something went wrong when calling isResourceActive for the path " +
+                    resourcePath + " and the error message is " + e.getMessage());
+        }
+        return false;
+    }
+
     private String getParentPath(String path) {
 
         String[] parts = path.split(RegistryConstants.PATH_SEPARATOR);

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/DefaultMediaTypeHandler.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/DefaultMediaTypeHandler.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/mediatypes/builtin/DefaultMediaTypeHandler.java	Fri Nov 23 04:02:46 2007
@@ -50,7 +50,7 @@
         if (path.indexOf("?v=") != -1) {
             String[] parts = path.split("\\?v=");
             plainPath = parts[0];
-            versionNumber = new Long(parts[1]).longValue();
+            versionNumber = Long.parseLong(parts[1]);
         }
 
         Connection conn = connectionFactory.getConnection();

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/HSQLDBInitializer.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/HSQLDBInitializer.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/HSQLDBInitializer.java	Fri Nov 23 04:02:46 2007
@@ -79,7 +79,9 @@
         } finally {
             try {
                 conn.close();
-            } catch (SQLException ignore) {}
+            } catch (SQLException ignore) {
+                log.info(ignore);
+            }
         }
     }
 }

Modified: trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/JDBCRegistryTest.java
==============================================================================
--- trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/JDBCRegistryTest.java	(original)
+++ trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/JDBCRegistryTest.java	Fri Nov 23 04:02:46 2007
@@ -22,6 +22,7 @@
 import junit.framework.TestCase;
 import org.wso2.registry.*;
 import org.wso2.registry.secure.RegistryRealm;
+import org.wso2.registry.secure.HSQLDBInitializer;
 import org.wso2.usermanager.Realm;
 
 import java.util.ArrayList;
@@ -49,7 +50,6 @@
     }
 
     public void testFlatResourceHandling() {
-
         Resource r1 = new Resource();
         r1.setAuthorUserName("Chathura");
         r1.setDescription("This is a test resource used for registry testing.");
@@ -170,7 +170,6 @@
     }
 
     public void testResourceVersioning() {
-
         Resource r1 = new Resource();
         r1.setAuthorUserName("writer1");
         byte[] r1Content = "R1 content".getBytes();
@@ -293,7 +292,6 @@
     //}
 
     public void testCollectionVersioning() {
-
         String r1Content = "r1 content1";
         Resource r1 = new Resource();
         r1.setAuthorUserName("Chathura");
@@ -358,7 +356,6 @@
     }
 
     public void testTagging() {
-
         // add a resource
 
         try {
@@ -366,28 +363,28 @@
             r1.setAuthorUserName("Author R1");
             byte[] r1content = "R1 content".getBytes();
             r1.setContent(r1content);
-            registry.put("/d1/r1", r1);
+            registry.put("/d11/r1", r1);
 
             Resource r2 = new Resource();
             r2.setAuthorUserName("Author R2");
             byte[] r2content = "R2 content".getBytes();
             r2.setContent(r2content);
-            registry.put("/d1/r2", r2);
+            registry.put("/d11/r2", r2);
 
             Resource r3 = new Resource();
             r3.setAuthorUserName("Author R3");
             byte[] r3content = "R3 content".getBytes();
             r3.setContent(r3content);
-            registry.put("/d1/r3", r3);
+            registry.put("/d11/r3", r3);
 
         } catch (RegistryException e) {
             fail("Failed to put test resources.");
         }
 
         try {
-            registry.applyTag("/d1/r1", "jsp");
-            registry.applyTag("/d1/r2", "jsp");
-            registry.applyTag("/d1/r3", "java");
+            registry.applyTag("/d11/r1", "jsp");
+            registry.applyTag("/d11/r2", "jsp");
+            registry.applyTag("/d11/r3", "java");
         } catch (RegistryException e) {
             fail("Valid tagging scenario failed");
         }
@@ -400,18 +397,18 @@
         }
         boolean artifactFound = false;
         for (int i = 0; i < paths.length; i++) {
-            if (paths[i].getResourcePath().equals("/d1/r1")) {
+            if (paths[i].getResourcePath().equals("/d11/r1")) {
                 artifactFound = true;
                 break;
             }
         }
-        assertTrue("/d1/r1 is not tagged with the tag \"jsp\"", artifactFound);
+        assertTrue("/d11/r1 is not tagged with the tag \"jsp\"", artifactFound);
 
         Tag[] tags = null;
         try {
-            tags = registry.getTags("/d1/r1");
+            tags = registry.getTags("/d11/r1");
         } catch (RegistryException e) {
-            fail("Failed to get tags for the resource /d1/r1");
+            fail("Failed to get tags for the resource /d11/r1");
         }
 
         boolean tagFound = false;
@@ -421,10 +418,10 @@
                 break;
             }
         }
-        assertTrue("tag 'jsp' is not associated with the artifact /d1/r1", tagFound);
+        assertTrue("tag 'jsp' is not associated with the artifact /d11/r1", tagFound);
 
         try {
-            registry.delete("/d1");
+            registry.delete("/d11");
         } catch (RegistryException e) {
             fail("Failed to delete test resources.");
         }
@@ -437,17 +434,16 @@
         }
         boolean artifactFound2 = false;
         for (int i = 0; i < paths2.length; i++) {
-            if (paths2[i].getResourcePath().equals("/d1/r1")) {
+            if (paths2[i].getResourcePath().equals("/d11/r1")) {
                 artifactFound2 = true;
                 break;
             }
         }
-        assertTrue("Tagging 'jsp' should still be associated with the deleted resource /d1/r1",
+        assertTrue("Tagging 'jsp' should still be associated with the deleted resource /d11/r1",
                 artifactFound2);
     }
 
     public void testComments() {
-
         // add a resource
 
         try {
@@ -455,7 +451,7 @@
             r1.setAuthorUserName("Author R1");
             byte[] r1content = "R1 content".getBytes();
             r1.setContent(r1content);
-            registry.put("/d1/r1", r1);
+            registry.put("/d12/r1", r1);
 
         } catch (RegistryException e) {
             fail("Failed to put test resources.");
@@ -464,17 +460,17 @@
         String comment1 = "this can be used as a test resource.";
         String comment2 = "I like this";
         try {
-            registry.addComment("/d1/r1", new Comment(comment1));
-            registry.addComment("/d1/r1", new Comment(comment2));
+            registry.addComment("/d12/r1", new Comment(comment1));
+            registry.addComment("/d12/r1", new Comment(comment2));
         } catch (RegistryException e) {
             fail("Valid tagging scenario failed");
         }
 
         Comment[] comments = null;
         try {
-            comments = registry.getComments("/d1/r1");
+            comments = registry.getComments("/d12/r1");
         } catch (RegistryException e) {
-            fail("Failed to get comments for the resource /d1/r1");
+            fail("Failed to get comments for the resource /d12/r1");
         }
 
         boolean commentFound = false;
@@ -485,10 +481,10 @@
             }
         }
         assertTrue("comment '" + comment1 +
-                "' is not associated with the artifact /d1/r1", commentFound);
+                "' is not associated with the artifact /d12/r1", commentFound);
 
         try {
-            Resource commentsResource = registry.get("/d1/r1?comments");
+            Resource commentsResource = registry.get("/d12/r1?comments");
             assertTrue("Comment collection resource should be a directory.",
                     commentsResource.isDirectory());
             String[] commentPaths = (String[]) commentsResource.getContent();
@@ -499,22 +495,21 @@
                 commentTexts.add(commentResource.getContent());
             }
 
-            assertTrue(comment1 + " is not associated for resource /d1/r1.", commentTexts.contains(comment1));
-            assertTrue(comment2 + " is not associated for resource /d1/r1.", commentTexts.contains(comment2));
+            assertTrue(comment1 + " is not associated for resource /d12/r1.", commentTexts.contains(comment1));
+            assertTrue(comment2 + " is not associated for resource /d12/r1.", commentTexts.contains(comment2));
 
         } catch (RegistryException e) {
-            fail("Failed to get comments form URL: /d1/r1?comments");
+            fail("Failed to get comments form URL: /d12/r1?comments");
         }
 
         try {
-            registry.delete("/d1");
+            registry.delete("/d12");
         } catch (RegistryException e) {
             fail("Failed to delete test resources.");
         }
     }
 
     public void testRatings() {
-
         // add a resource
 
         try {
@@ -522,36 +517,35 @@
             r1.setAuthorUserName("Author R1");
             byte[] r1content = "R1 content".getBytes();
             r1.setContent(r1content);
-            registry.put("/d1/r1", r1);
+            registry.put("/d13/r1", r1);
 
         } catch (RegistryException e) {
             fail("Failed to put test resources.");
         }
 
         try {
-            registry.rateResource("/d1/r1", 4);
+            registry.rateResource("/d13/r1", 4);
         } catch (RegistryException e) {
-            fail("Couldn't rate the resource /d1/r1");
+            fail("Couldn't rate the resource /d13/r1");
         }
 
         float rating = 0;
         try {
-            rating = registry.getAverageRating("/d1/r1");
+            rating = registry.getAverageRating("/d13/r1");
         } catch (RegistryException e) {
-            fail("Couldn't get the rating of the resource /d1/r1");
+            fail("Couldn't get the rating of the resource /d13/r1");
         }
 
-        assertEquals("Rating of the resource /d1/r1 should be 4.", rating, (float) 4.0, (float) 0.01);
+        assertEquals("Rating of the resource /d13/r1 should be 4.", rating, (float) 4.0, (float) 0.01);
 
         try {
-            registry.delete("/d1");
+            registry.delete("/d13");
         } catch (RegistryException e) {
             fail("Failed to delete test resources.");
         }
     }
 
     public void testUserDefinedResourceQuery() {
-
         try {
 
             Resource r1 = new Resource();
@@ -608,7 +602,6 @@
     }
 
     public void testUserDefinedRatingsQuery() {
-
         try {
 
             Resource r1 = new Resource();
@@ -671,7 +664,6 @@
     }
 
     public void testUserDefinedTagsQuery() {
-
         try {
 
             Resource r1 = new Resource();
@@ -734,7 +726,6 @@
     }
 
     public void testUserDefinedCommentsQuery() {
-
         try {
 
             Resource r1 = new Resource();
@@ -797,7 +788,6 @@
     }
 
     public void testTagsAsResources() {
-
         //Resource r1 = new Resource();
         //String r1Content = "this is r1 content";
         //r1.setContent(r1Content.getBytes());
@@ -851,7 +841,6 @@
     }
 
     public void testCommentsAsResources() {
-
         Resource r1 = new Resource();
         String r1Content = "this is r1 content";
         r1.setContent(r1Content.getBytes());
@@ -905,7 +894,6 @@
     }
 
     public void testRatingsAsResources() {
-
         Resource r5 = new Resource();
         String r5Content = "this is r5 content";
         r5.setContent(r5Content.getBytes());
@@ -945,7 +933,6 @@
     }
 
     public void testLogs() throws RegistryException {
-
         String r1Content = "this is the r200 content.";
         Resource r1 = new Resource();
         r1.setContent(r1Content.getBytes());
@@ -978,9 +965,9 @@
         r1.setContent(r1Content.getBytes());
 
         try {
-            registry.put("/r1", r1);
+            registry.put("/r11", r1);
         } catch (RegistryException e) {
-            fail("Couldn't put a content resource in to path /r1");
+            fail("Couldn't put a content resource in to path /r11");
         }
 
         // put a collection in to the root
@@ -1062,9 +1049,9 @@
         // rate some artifacts
 
         try {
-            registry.rateResource("/r1", 3);
-            registry.rateResource("/r1", 4);
-            registry.rateResource("/r1", 5);
+            registry.rateResource("/r11", 3);
+            registry.rateResource("/r11", 4);
+            registry.rateResource("/r11", 5);
             registry.rateResource("/c1", 2);
             registry.rateResource("/c1/r2", 5);
             registry.rateResource("/c2/r4", 1);
@@ -1074,7 +1061,7 @@
             fail("Valid rating scenario failed.");
         }
 
-        registry.delete("/r1");
+        registry.delete("/r11");
         registry.delete("/c1");
         registry.delete("/c2");
     }



More information about the Registry-dev mailing list