[Registry-dev] svn commit r9920 - in trunk/registry/modules: core/src/main/java/org/wso2/registry core/src/main/java/org/wso2/registry/jdbc/dao core/src/main/java/org/wso2/registry/jdbc/urlhandlers core/src/test/java/org/wso2/registry/jdbc webapps/src/main/java/org/wso2/registry/web/actions webapps/src/main/java/org/wso2/registry/web/actions/utils webapps/src/main/webapp/admin/tiles

svn at wso2.org svn at wso2.org
Mon Nov 19 21:10:13 PST 2007


Author: chathura
Date: Mon Nov 19 21:09:44 2007
New Revision: 9920

Added:
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/urlhandlers/RatingURLHandler.java
   trunk/registry/modules/webapps/src/main/java/org/wso2/registry/web/actions/utils/Permission.java
Modified:
   trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryConstants.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/RatingsDAO.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/urlhandlers/RatingsCollectionURLHandler.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/urlhandlers/URLHandlerManager.java
   trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/JDBCRegistryTest.java
   trunk/registry/modules/webapps/src/main/java/org/wso2/registry/web/actions/ResourceDetailsAction.java
   trunk/registry/modules/webapps/src/main/webapp/admin/tiles/resource_detail.jsp
Log:

Implemented handling of ratings as resources.
Now it is possible to get ratings of a resource as a collection using registry.get("/myResource;ratings");
Then individual ratings can be retrieved using registry.get("/myResource;ratings:userName");
This implementation is usefull for getting the results of dynamic queries which returns ratings.



Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryConstants.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryConstants.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryConstants.java	Mon Nov 19 21:09:44 2007
@@ -62,6 +62,8 @@
     public static final String DEFAULT_MEDIA_TYPE = "default";
     public static final String SQL_QUERY_MEDIA_TYPE = "sql-query";
     public static final String COMMENT_MEDIA_TYPE = "comment";    
+    public static final String RATING_MEDIA_TYPE = "rating";
+    
     public static final String SYNPASE_REPOSITORY_MEDIA_TYPE = "synapse-repo";
     public static final String SYNAPSE_CONF_COLLECTION_MEDIA_TYPE = "synapse-conf";
     public static final String SYNAPSE_SEQUENCE_COLLECTION_MEDIA_TYPE = "synapse-seq";

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/RatingsDAO.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/RatingsDAO.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/RatingsDAO.java	Mon Nov 19 21:09:44 2007
@@ -114,6 +114,25 @@
         return rating;
     }
 
+    public java.util.Date getRatedTime(String path, String userName, Connection conn) throws SQLException {
+
+        String sql = "SELECT R.RATED_TIME FROM RATINGS R, ARTIFACTS A WHERE A.PATH=? AND R.USER_ID=? AND A.AID=R.AID";
+
+        PreparedStatement s = conn.prepareStatement(sql);
+        s.setString(1, path);
+        s.setString(2, userName);
+
+        ResultSet result = s.executeQuery();
+
+        java.util.Date ratedTime = null;
+        if (result.next()) {
+            ratedTime = new java.util.Date(
+                    result.getTimestamp(DatabaseConstants.RATED_TIME_FIELD).getTime());
+        }
+
+        return ratedTime;
+    }
+
     public void removeRatings(long artifactID, Connection conn) throws SQLException {
 
         String sql = "DELETE FROM RATINGS WHERE AID=?";

Added: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/urlhandlers/RatingURLHandler.java
==============================================================================
--- (empty file)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/urlhandlers/RatingURLHandler.java	Mon Nov 19 21:09:44 2007
@@ -0,0 +1,89 @@
+/*
+ * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed 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.wso2.registry.jdbc.urlhandlers;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.wso2.registry.jdbc.ConnectionFactory;
+import org.wso2.registry.jdbc.dao.RatingsDAO;
+import org.wso2.registry.Resource;
+import org.wso2.registry.RegistryException;
+import org.wso2.registry.RegistryConstants;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Date;
+
+public class RatingURLHandler extends URLHandler {
+
+    private static final Log log = LogFactory.getLog(RatingURLHandler.class);
+
+    public RatingURLHandler(ConnectionFactory connectionFactory) {
+        super(connectionFactory);
+    }
+
+    public boolean handleURL(String url, Resource resource) throws RegistryException {
+
+        String[] parts = url.split(";");
+
+        if (parts.length != 2) {
+            return false;
+        }
+
+        if (parts[0].length() == 0) {
+            return false;
+        }
+
+        String[] details = parts[1].split(":");
+        if (details.length != 2 || !details[0].equals("ratings") || details[1].length() == 0) {
+            return false;
+        }
+
+        String resourcePath = parts[0];
+        String ratedUserName = details[1];
+
+        Connection conn = connectionFactory.getConnection();
+
+        try {
+            RatingsDAO ratingsDAO = new RatingsDAO();
+            int rating = ratingsDAO.getRating(resourcePath, ratedUserName, conn);
+            Date ratedTime = ratingsDAO.getRatedTime(resourcePath, ratedUserName, conn);
+
+            resource.setMediaType(RegistryConstants.RATING_MEDIA_TYPE);
+            resource.setContent(new Integer(rating));
+            resource.setAuthorUserName(ratedUserName);
+            resource.setPath(url);
+            resource.setCreatedTime(ratedTime);
+            resource.setProperty("resourcePath", resourcePath);
+
+        } catch (SQLException e) {
+            String msg = "Could not get the rating for resource at " + resourcePath +
+                    ". Caused by: " + e.getMessage();
+            log.error(msg, e);
+            throw new RegistryException(msg);
+
+        } finally {
+            if (conn != null) {
+                try {
+                    conn.close();
+                } catch (SQLException ignore) {}
+            }
+        }
+
+        return true;
+    }
+}

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/urlhandlers/RatingsCollectionURLHandler.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/urlhandlers/RatingsCollectionURLHandler.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/urlhandlers/RatingsCollectionURLHandler.java	Mon Nov 19 21:09:44 2007
@@ -39,7 +39,7 @@
 
     public boolean handleURL(String url, Resource resource) throws RegistryException {
 
-        String[] parts = url.split("\\?");
+        String[] parts = url.split(";");
 
         if (parts.length != 2) {
             return false;
@@ -69,7 +69,7 @@
             String[] ratedUserNames = ratingsDAO.getRatedUserNames(resourcePath, conn);
             List ratingPaths = new ArrayList();
             for (int i = 0; i < ratedUserNames.length; i++) {
-                String ratingPath = resourcePath + "?rating&user=" + ratedUserNames[i];
+                String ratingPath = resourcePath + ";ratings:" + ratedUserNames[i];
                 ratingPaths.add(ratingPath);
             }
 

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/urlhandlers/URLHandlerManager.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/urlhandlers/URLHandlerManager.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/urlhandlers/URLHandlerManager.java	Mon Nov 19 21:09:44 2007
@@ -32,6 +32,8 @@
         
         urlHandlers.add(new CommentURLHandler(connectionFactory));
         urlHandlers.add(new CommentCollectionURLHandler(connectionFactory));
+        urlHandlers.add(new RatingURLHandler(connectionFactory));
+        urlHandlers.add(new RatingsCollectionURLHandler(connectionFactory));
     }
 
     public Resource handleURL(String url)

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	Mon Nov 19 21:09:44 2007
@@ -708,7 +708,7 @@
 
         assertTrue("Comments are not retrieved properly as resources.",
                 commentStrings.contains("dummy resource."));
-        
+
         assertTrue("Comments are not retrieved properly as resources.",
                 commentStrings.contains("simple test resource."));
 
@@ -717,43 +717,42 @@
 
     public void testRatingsAsResources() {
 
-        //Resource r5 = new Resource();
-        //String r5Content = "this is r5 content";
-        //r5.setContent(r5Content.getBytes());
-        //r5.setDescription("production ready.");
-        //String r5Path = "/c1/r5";
-        //
-        //try {
-        //    registry.put(r5Path, r5);
-        //} catch (RegistryException e) {
-        //    fail("Valid put scenario failed.");
-        //}
-        //
-        //try {
-        //    registry.rateResource("/c1/r5", 3);
-        //} catch (RegistryException e) {
-        //    fail("Valid tagging scenario failed.");
-        //}
-        //
-        //Resource ratings = null;
-        //try {
-        //    ratings = registry.get("/c1/r5?ratings");
-        //} catch (RegistryException e) {
-        //    fail("Failed to get comments using URL.");
-        //}
-        //
-        //String[] ratingPaths = (String[]) ratings.getContent();
-        //
-        //    try {
-        //        Resource c1 = registry.get(ratingPaths[0]);
-        //        int rating = ((Integer) c1.getContent()).intValue();
-        //    } catch (RegistryException e) {
-        //        fail("Failed to get comment using a path.");
-        //    }
-        //
-        //assertEquals("Ratings are not retrieved properly as resources.", ratings, 3);
-        //
-        //int a = 1;
+        Resource r5 = new Resource();
+        String r5Content = "this is r5 content";
+        r5.setContent(r5Content.getBytes());
+        r5.setDescription("production ready.");
+        String r5Path = "/c1/r5";
+
+        try {
+            registry.put(r5Path, r5);
+        } catch (RegistryException e) {
+            fail("Valid put scenario failed.");
+        }
+
+        try {
+            registry.rateResource("/c1/r5", 3);
+        } catch (RegistryException e) {
+            fail("Valid tagging scenario failed.");
+        }
+
+        Resource ratings = null;
+        try {
+            ratings = registry.get("/c1/r5;ratings");
+        } catch (RegistryException e) {
+            fail("Failed to get ratings using URL.");
+        }
+
+        String[] ratingPaths = (String[]) ratings.getContent();
+
+        int rating = 0;
+        try {
+            Resource c1 = registry.get(ratingPaths[0]);
+            rating = ((Integer) c1.getContent()).intValue();
+        } catch (RegistryException e) {
+            fail("Failed to get rating using a path.");
+        }
+
+        assertEquals("Ratings are not retrieved properly as resources.", rating, 3);
     }
 
     public void testLogs() throws RegistryException {
@@ -778,7 +777,7 @@
 
         LogEntry l2 = logs[1];
         assertEquals("Log for rating /r200 is not added properly.", LogEntry.RATING, l2.getAction());
-        assertEquals("Log for rating /r200 is not added properly.", "5", l2.getActionData());        
+        assertEquals("Log for rating /r200 is not added properly.", "5", l2.getActionData());
     }
 
     public void testCombinedScenario() throws RegistryException {

Modified: trunk/registry/modules/webapps/src/main/java/org/wso2/registry/web/actions/ResourceDetailsAction.java
==============================================================================
--- trunk/registry/modules/webapps/src/main/java/org/wso2/registry/web/actions/ResourceDetailsAction.java	(original)
+++ trunk/registry/modules/webapps/src/main/java/org/wso2/registry/web/actions/ResourceDetailsAction.java	Mon Nov 19 21:09:44 2007
@@ -20,6 +20,7 @@
 import org.wso2.registry.web.actions.utils.VersionPath;
 import org.wso2.registry.secure.RegistryUserManager;
 import org.wso2.registry.secure.SecureRegistry;
+import org.wso2.usermanager.Realm;
 
 import java.util.Date;
 import java.util.Properties;
@@ -43,6 +44,7 @@
     private List comments = new ArrayList();
     private float averageRating;
     private List versionPaths = new ArrayList();
+    private List permissions = new ArrayList();
 
     private String parentPath;
 
@@ -128,6 +130,9 @@
             userNames.add(userArray[i]);
         }
 
+        Realm realm = userManager.getRealm();
+        //realm.getAuthorizer().getAuthorizedUsersForResource(path, )
+
         return ActionSupport.SUCCESS;
     }
 
@@ -242,4 +247,12 @@
     public void setUserNames(List userNames) {
         this.userNames = userNames;
     }
+
+    public List getPermissions() {
+        return permissions;
+    }
+
+    public void setPermissions(List permissions) {
+        this.permissions = permissions;
+    }
 }

Added: trunk/registry/modules/webapps/src/main/java/org/wso2/registry/web/actions/utils/Permission.java
==============================================================================
--- (empty file)
+++ trunk/registry/modules/webapps/src/main/java/org/wso2/registry/web/actions/utils/Permission.java	Mon Nov 19 21:09:44 2007
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed 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.wso2.registry.web.actions.utils;
+
+public class Permission {
+
+    private String userName;
+    private boolean readAllow;
+    private boolean readDeny;
+    private boolean writeAllow;
+    private boolean writeDeny;
+    private boolean deleteAllow;
+    private boolean deleteDeny;
+
+    public String getUserName() {
+        return userName;
+    }
+
+    public void setUserName(String userName) {
+        this.userName = userName;
+    }
+
+    public boolean isReadAllow() {
+        return readAllow;
+    }
+
+    public void setReadAllow(boolean readAllow) {
+        this.readAllow = readAllow;
+    }
+
+    public boolean isReadDeny() {
+        return readDeny;
+    }
+
+    public void setReadDeny(boolean readDeny) {
+        this.readDeny = readDeny;
+    }
+
+    public boolean isWriteAllow() {
+        return writeAllow;
+    }
+
+    public void setWriteAllow(boolean writeAllow) {
+        this.writeAllow = writeAllow;
+    }
+
+    public boolean isWriteDeny() {
+        return writeDeny;
+    }
+
+    public void setWriteDeny(boolean writeDeny) {
+        this.writeDeny = writeDeny;
+    }
+
+    public boolean isDeleteAllow() {
+        return deleteAllow;
+    }
+
+    public void setDeleteAllow(boolean deleteAllow) {
+        this.deleteAllow = deleteAllow;
+    }
+
+    public boolean isDeleteDeny() {
+        return deleteDeny;
+    }
+
+    public void setDeleteDeny(boolean deleteDeny) {
+        this.deleteDeny = deleteDeny;
+    }
+}

Modified: trunk/registry/modules/webapps/src/main/webapp/admin/tiles/resource_detail.jsp
==============================================================================
--- trunk/registry/modules/webapps/src/main/webapp/admin/tiles/resource_detail.jsp	(original)
+++ trunk/registry/modules/webapps/src/main/webapp/admin/tiles/resource_detail.jsp	Mon Nov 19 21:09:44 2007
@@ -167,12 +167,12 @@
   </tr>
   <tr>
     <td>&nbsp;</td>
-    <td width="100">&nbsp;</td>
-    <td width="100">&nbsp;</td>
-    <td width="100">&nbsp;</td>
-    <td width="100">&nbsp;</td>
-    <td width="100">&nbsp;</td>
-    <td width="100">&nbsp;</td>
+    <td width="100"><input type="checkbox" name="readAllow"/></td>
+    <td width="100"><input type="checkbox" name="readDeny"/></td>
+    <td width="100"><input type="checkbox" name="writeAllow"/></td>
+    <td width="100"><input type="checkbox" name="writeDeny"/></td>
+    <td width="100"><input type="checkbox" name="deleteAllow"/></td>
+    <td width="100"><input type="checkbox" name="deleteDeny"/></td>
   </tr>
   <tr>
     <td>&nbsp;</td>



More information about the Registry-dev mailing list