Author: chathura
Date: Thu Jan 10 20:48:58 2008
New Revision: 12124

Log:


Implemented the media type based search as a method of the Registry interface.

Added a test case to test media type based search.
(This test case is commented till the media type search is implemented in the 
remote registry)



Added:
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/MediaTypeDAO.java
Modified:
   trunk/registry/modules/core/src/main/java/org/wso2/registry/Registry.java
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RemoteRegistry.java
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/inmemory/InMemoryRegistry.java
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/SecureRegistry.java
   
trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/JDBCRegistryTest.java

Modified: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/Registry.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/Registry.java   
(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/Registry.java   
Thu Jan 10 20:48:58 2008
@@ -196,6 +196,15 @@
     int getRating(String path, String userName) throws RegistryException;
 
     /**
+     * Returns a collection resource containing the paths of the resources of 
the given media type.
+     *
+     * @param mediaType Media type to search for (e.g. image/gif, text/html)
+     * @return Collection resource containing the resources matching the given 
media type
+     * @throws RegistryException
+     */
+    Resource getResourcesOfMediaType(String mediaType) throws 
RegistryException;
+
+    /**
      * Executes a custom query defined by users. Returned RowSet contains 
fields of tables specified
      * by the custom query.
      *

Modified: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RemoteRegistry.java
==============================================================================
--- 
trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RemoteRegistry.java
 (original)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/app/RemoteRegistry.java
 Thu Jan 10 20:48:58 2008
@@ -446,6 +446,10 @@
         return Integer.parseInt(intvalue);
     }
 
+    public Resource getResourcesOfMediaType(String mediaType) throws 
RegistryException {
+        // TODO: implement media type based search in APP
+        return null;
+    }
 
     public Resource executeQuery(String path, Object[] parameters) throws 
RegistryException {
         Abdera abdera = new Abdera();

Modified: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/inmemory/InMemoryRegistry.java
==============================================================================
--- 
trunk/registry/modules/core/src/main/java/org/wso2/registry/inmemory/InMemoryRegistry.java
  (original)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/inmemory/InMemoryRegistry.java
  Thu Jan 10 20:48:58 2008
@@ -245,6 +245,11 @@
                                                                     "User 
level rating"));
     }
 
+    public Resource getResourcesOfMediaType(String mediaType) throws 
RegistryException {
+        throw new 
UnsupportedOperationException(Messages.getMessage("unsupported.exception",
+                                                                    "Search 
for resources of media type"));
+    }
+
     public Resource executeQuery(String path, Object[] parameters) throws 
RegistryException {
         throw new 
UnsupportedOperationException(Messages.getMessage("unsupported.exception",
                                                                     "Custom 
queries"));

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
  Thu Jan 10 20:48:58 2008
@@ -59,6 +59,7 @@
     private TagsDAO tagsDAO = null;
     private CommentsDAO commentsDAO = null;
     private RatingsDAO ratingsDAO = null;
+    private MediaTypeDAO mediaTypeDAO = null;
     private LogsDAO logsDAO = null;
 
     private Realm defaultRealm = null;
@@ -120,10 +121,11 @@
         urlHandlerManager = new URLHandlerManager(dataSource);
         mediaTypeManager = new MediaTypeManager(dataSource, realm);
 
-        this.resourceDAO = new VersionedResourceDAO();
-        this.tagsDAO = new TagsDAO();
-        this.commentsDAO = new CommentsDAO();
-        this.ratingsDAO = new RatingsDAO();
+        resourceDAO = new VersionedResourceDAO();
+        tagsDAO = new TagsDAO();
+        commentsDAO = new CommentsDAO();
+        ratingsDAO = new RatingsDAO();
+        mediaTypeDAO = new MediaTypeDAO();
         logsDAO = new LogsDAO();
 
         // check if root is added. if not add it.
@@ -955,6 +957,32 @@
 
     }
 
+    public Resource getResourcesOfMediaType(String mediaType) throws 
RegistryException {
+
+        Connection conn = getConnection();
+
+        Resource resultCollection = new Resource();
+        resultCollection.setDirectory(true);
+
+        try {
+            String[] resultPaths = mediaTypeDAO.getPathsOfMediaType(mediaType, 
conn);
+            resultCollection.setContent(resultPaths);
+
+        } catch (SQLException e) {
+
+            String msg = "Could not get the resources of media type: " + 
mediaType;
+            log.error(msg, e);
+            throw new RegistryException(msg, e);
+
+        } finally {
+            try {
+                conn.close();
+            } catch (SQLException ignore) {}
+        }
+
+        return resultCollection;
+    }
+
     public Resource executeQuery(String path, Object[] parameters) throws 
RegistryException {
 
         Connection conn = getConnection();

Added: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/MediaTypeDAO.java
==============================================================================
--- (empty file)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/dao/MediaTypeDAO.java
      Thu Jan 10 20:48:58 2008
@@ -0,0 +1,45 @@
+/*
+ * 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.dao;
+
+import org.wso2.registry.jdbc.DatabaseConstants;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.sql.ResultSet;
+import java.util.List;
+import java.util.ArrayList;
+
+public class MediaTypeDAO {
+
+    public String[] getPathsOfMediaType(String mediaType, Connection conn) 
throws SQLException {
+
+        String sql = "SELECT PATH FROM ARTIFACTS WHERE MEDIA_TYPE=?";
+
+        PreparedStatement s = conn.prepareStatement(sql);
+        s.setString(1, mediaType);
+
+        List pathList = new ArrayList();
+        ResultSet results = s.executeQuery();
+        while(results.next()) {
+            pathList.add(results.getString(DatabaseConstants.PATH_FIELD));
+        }
+
+        return (String[]) pathList.toArray(new String[pathList.size()]);
+    }
+}

Modified: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/SecureRegistry.java
==============================================================================
--- 
trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/SecureRegistry.java
      (original)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/SecureRegistry.java
      Thu Jan 10 20:48:58 2008
@@ -623,6 +623,11 @@
         return registry.getRating(resourcePath, userName);
     }
 
+    public Resource getResourcesOfMediaType(String mediaType) throws 
RegistryException {
+        User.setCurrentUser(userID);
+        return registry.getResourcesOfMediaType(mediaType);
+    }
+
     public Resource executeQuery(String path, Object[] parameters) throws 
RegistryException {
         User.setCurrentUser(userID);
         return registry.executeQuery(path, parameters);

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
      Thu Jan 10 20:48:58 2008
@@ -943,6 +943,61 @@
 
     }
 
+    // TODO: uncomment this test after media type search is implemented in the 
remote registry
+    //public void testMediaTypeSearch() {
+    //
+    //    String r1Content = "this is the r1 content.";
+    //    Resource r1 = new Resource();
+    //    r1.setContent(r1Content.getBytes());
+    //    try {
+    //        registry.put("/r101", r1);
+    //    } catch (RegistryException e) {
+    //        fail("Couldn't put a content resource in to path /r101");
+    //    }
+    //
+    //    String r2Content = "this is the r2 content.";
+    //    Resource r2 = new Resource();
+    //    r2.setMediaType("text/plain");
+    //    r2.setContent(r2Content.getBytes());
+    //    try {
+    //        registry.put("/r102", r2);
+    //    } catch (RegistryException e) {
+    //        fail("Couldn't put a content resource in to path /r102");
+    //    }
+    //
+    //    String r3Content = "this is the r3 content.";
+    //    Resource r3 = new Resource();
+    //    r3.setContent(r3Content.getBytes());
+    //    r3.setMediaType("text/plain");
+    //    try {
+    //        registry.put("/c10022/r3", r3);
+    //    } catch (RegistryException e) {
+    //        fail("Couldn't put a content resource in to path /c10022/r3");
+    //    }
+    //
+    //    String r4Content = "this is the r4 content.";
+    //    Resource r4 = new Resource();
+    //    r4.setContent(r4Content.getBytes());
+    //    try {
+    //        registry.put("/c1222/r4", r4);
+    //    } catch (RegistryException e) {
+    //        fail("Couldn't put a content resource in to path /c1222/r4");
+    //    }
+    //
+    //    try {
+    //        Resource results = 
registry.getResourcesOfMediaType("text/plain");
+    //        String[] resultPaths = (String[]) results.getContent();
+    //
+    //        assertTrue(containsString(resultPaths, "/r102"));
+    //        assertTrue(containsString(resultPaths, "/c10022/r3"));
+    //        assertTrue(!containsString(resultPaths, "/r101"));
+    //        assertTrue(!containsString(resultPaths, "/c1222/r4"));
+    //
+    //    } catch (RegistryException e) {
+    //        e.printStackTrace();
+    //    }
+    //}
+
     public void testCombinedScenario() throws RegistryException {
         // put a content resource in to the root
         String r1Content = "this is the r1 content.";

_______________________________________________
Registry-dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/registry-dev

Reply via email to