Author: chathura
Date: Thu Dec 13 04:45:35 2007
New Revision: 11087

Log:


Implement proper advanced search mechanism.
Now the queries are created on-the-fly according to the number of parameters.
But once a query is created it can be reused for another query with same 
pattern of paramters.
So, there is an upper bound on maximum number of queries that can be created 
for advance search functionality.



Modified:
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryConstants.java
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/jdbc/JDBCRegistry.java
   
trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java
   
trunk/registry/modules/webapps/src/main/java/org/wso2/registry/web/actions/AdvancedSearchAction.java
   
trunk/registry/modules/webapps/src/main/java/org/wso2/registry/web/actions/utils/AdvancedResourceQuery.java
   
trunk/registry/modules/webapps/src/main/java/org/wso2/registry/web/actions/utils/QueryManager.java

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
  Thu Dec 13 04:45:35 2007
@@ -49,7 +49,8 @@
 
     public static final String TAG_PROPERTY = "tag";
     
-    public static final String REGISTRY = "RegistryEngine";
+    public static final String REGISTRY = "CoreRegistry";
+    public static final String SYSTEM_REGISTRY = "SystemRegistry";
     public static final String USER_REGISTRY = "user_registry";
     public static final String REGISTRY_REALM = "registry_realm";
 

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 Dec 13 04:45:35 2007
@@ -150,6 +150,7 @@
                 root.setPath(RegistryConstants.ROOT_PATH);
                 root.setDirectory(true);
                 root.setAuthorUserName(RegistryConstants.SYSTEM_USER);
+                root.setLastUpdaterUserName(RegistryConstants.SYSTEM_USER);
                 resourceDAO.add(root, conn);
 
                 AuthorizationUtil.setDefaultAuthorizations(defaultRealm, 
RegistryConstants.ROOT_PATH, RegistryConstants.SYSTEM_USER);

Modified: 
trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java
==============================================================================
--- 
trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java
    (original)
+++ 
trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java
    Thu Dec 13 04:45:35 2007
@@ -25,6 +25,7 @@
 import org.wso2.registry.RegistryConstants;
 import org.wso2.registry.RegistryException;
 import org.wso2.registry.Resource;
+import org.wso2.registry.secure.SecureRegistry;
 import org.wso2.registry.i18n.Messages;
 import org.wso2.registry.jdbc.JDBCRegistry;
 import org.wso2.usermanager.Realm;
@@ -81,6 +82,18 @@
                 // todo: initialize core registry with a remote registry 
instance.
             }
 
+            // create a system registry and put it in the context
+            SecureRegistry systemRegistry =
+                    new SecureRegistry(RegistryConstants.SYSTEM_USER,
+                            RegistryConstants.SYSTEM_PASSWORD,
+                            coreRegistry,
+                            registryRealm);
+
+            
config.getServletContext().setAttribute(RegistryConstants.REGISTRY, 
coreRegistry);
+            
config.getServletContext().setAttribute(RegistryConstants.REGISTRY_REALM, 
registryRealm);
+            
System.getProperties().put(RegistryConstants.REGISTRY,coreRegistry);
+            System.getProperties().put(RegistryConstants.SYSTEM_REGISTRY, 
systemRegistry);
+
             this.editProcessor = new EditProcessor();
 
         } catch (RegistryException e) {
@@ -88,9 +101,6 @@
             throw new ServletException(e.getMessage());
         }
 
-        config.getServletContext().setAttribute(RegistryConstants.REGISTRY, 
coreRegistry);
-        
config.getServletContext().setAttribute(RegistryConstants.REGISTRY_REALM, 
registryRealm);
-         System.getProperties().put(RegistryConstants.REGISTRY,coreRegistry);
         log.info(Messages.getMessage("server.initalized"));
     }
 

Modified: 
trunk/registry/modules/webapps/src/main/java/org/wso2/registry/web/actions/AdvancedSearchAction.java
==============================================================================
--- 
trunk/registry/modules/webapps/src/main/java/org/wso2/registry/web/actions/AdvancedSearchAction.java
        (original)
+++ 
trunk/registry/modules/webapps/src/main/java/org/wso2/registry/web/actions/AdvancedSearchAction.java
        Thu Dec 13 04:45:35 2007
@@ -18,6 +18,7 @@
 
 import org.wso2.registry.*;
 import org.wso2.registry.web.actions.utils.ResourceData;
+import org.wso2.registry.web.actions.utils.AdvancedResourceQuery;
 import org.wso2.registry.secure.SecureRegistry;
 import org.wso2.registry.secure.RegistryUserManager;
 import org.wso2.registry.secure.AuthorizationFailedException;
@@ -34,7 +35,7 @@
     private String tags;
     private String commentWords;
     private String content;
-    private String resultType;
+    private String resultType = "Resources";
 
     private List resourceDataList = new ArrayList();
 
@@ -42,6 +43,59 @@
 
         setRequest(request);
 
+        Registry registry = getRegistry();
+
+        if (resultType.equals("Resources")) {
+
+            AdvancedResourceQuery query = new AdvancedResourceQuery();
+            query.setResourcePath(resourcePath);
+            query.setAuthorName(authorName);
+            query.setUpdaterName(updaterName);
+            query.setCommentWords(commentWords);
+            query.setTags(tags);
+
+            Resource qResults = query.execute(registry);
+
+            String[] childPaths = (String[]) qResults.getContent();
+            for (int i = 0; i < childPaths.length; i++) {
+
+                ResourceData resourceData = new ResourceData();
+                resourceData.setResourcePath(childPaths[i]);
+
+                if (childPaths[i] != null) {
+                    if (RegistryConstants.ROOT_PATH.equals(childPaths[i])) {
+                        resourceData.setName("root");
+                    } else {
+                        String[] parts = 
childPaths[i].split(RegistryConstants.PATH_SEPARATOR);
+                        resourceData.setName(parts[parts.length - 1]);
+                    }
+                }
+
+                try {
+                    Resource child = registry.get(childPaths[i]);
+
+                    resourceData.setResourceType(
+                            child.isDirectory()? ResourceData.COLLECTION : 
ResourceData.RESOURCE);
+                    resourceData.setAuthorUserName(child.getAuthorUserName());
+                    resourceData.setDescription(child.getDescription());
+                    
resourceData.setAverageRating(registry.getAverageRating(child.getPath()));
+                    resourceData.setCreatedOn(child.getCreatedTime());
+
+                } catch (AuthorizationFailedException e) {
+
+                    resourceData.setResourceType(ResourceData.UNKNOWN);
+                }
+
+                resourceDataList.add(resourceData);
+            }
+        }
+
+    }
+
+    public void executeDeprecated(HttpServletRequest request) throws 
RegistryException {
+
+        setRequest(request);
+
         // contructing sqls is prefered here, as otherwise we have to create 
and store very large
         // number of query resources to support combined criterias.
 

Modified: 
trunk/registry/modules/webapps/src/main/java/org/wso2/registry/web/actions/utils/AdvancedResourceQuery.java
==============================================================================
--- 
trunk/registry/modules/webapps/src/main/java/org/wso2/registry/web/actions/utils/AdvancedResourceQuery.java
 (original)
+++ 
trunk/registry/modules/webapps/src/main/java/org/wso2/registry/web/actions/utils/AdvancedResourceQuery.java
 Thu Dec 13 04:45:35 2007
@@ -19,6 +19,8 @@
 import org.wso2.registry.Resource;
 import org.wso2.registry.Registry;
 import org.wso2.registry.RegistryException;
+import org.wso2.registry.RegistryConstants;
+import org.wso2.registry.secure.SecureRegistry;
 
 import java.util.Map;
 import java.util.HashMap;
@@ -38,9 +40,13 @@
     private String resourcePath;
     private String authorName;
     private String updaterName;
-    private String tags;
     private String commentWords;
-    private String content;
+    private String tag1;
+    private String tag2;
+    private String tag3;
+
+    private String tags;
+    private String queryPath;
 
     public Resource execute(Registry registry) throws RegistryException {
 
@@ -49,26 +55,41 @@
         // else, create new query and execute
 
         String queryPath = computeQueryPath();
-        if (registry.resourceExists(queryPath)) {
+        if (!registry.resourceExists(queryPath)) {
+            defineQuery();
+        }
 
-            List params = new ArrayList();
-            if (resourcePath != null && resourcePath.length() > 0) {
-                params.add(resourcePath);
-            }
+        List params = new ArrayList();
+        if (resourcePath != null && resourcePath.length() > 0) {
+            params.add(resourcePath);
+        }
 
-            if (authorName != null && authorName.length() > 0) {
-                params.add(authorName);
-            }
+        if (authorName != null && authorName.length() > 0) {
+            params.add(authorName);
+        }
 
-            if (updaterName != null && updaterName.length() > 0) {
-                params.add(updaterName);
-            }
+        if (updaterName != null && updaterName.length() > 0) {
+            params.add(updaterName);
+        }
+
+        if (commentWords != null && commentWords.length() > 0) {
+            params.add("%" + commentWords + "%");
+        }
+
+        if (tag1 != null && tag1.length() > 0) {
+            params.add(tag1);
+        }
 
-            Resource resultCollection = registry.executeQuery(queryPath, 
params.toArray());
-            return resultCollection;
+        if (tag2 != null && tag2.length() > 0) {
+            params.add(tag2);
         }
 
-        return null;
+        if (tag3 != null && tag3.length() > 0) {
+            params.add(tag3);
+        }
+
+        Resource resultCollection = registry.executeQuery(queryPath, 
params.toArray());
+        return resultCollection;
     }
 
     public String getResourcePath() {
@@ -101,6 +122,19 @@
 
     public void setTags(String tags) {
         this.tags = tags;
+
+        String[] tagParts = tags.split(" ");
+        if (tagParts.length > 0) {
+            tag1 = tagParts[0];
+        }
+
+        if (tagParts.length > 1) {
+            tag2 = tagParts[1];
+        }
+
+        if (tagParts.length > 2) {
+            tag3 = tagParts[2];
+        }
     }
 
     public String getCommentWords() {
@@ -111,19 +145,145 @@
         this.commentWords = commentWords;
     }
 
-    public String getContent() {
-        return content;
-    }
+    private void defineQuery() throws RegistryException {
 
-    public void setContent(String content) {
-        this.content = content;
-    }
+        SecureRegistry systemRegistry =
+                (SecureRegistry) 
System.getProperties().get(RegistryConstants.SYSTEM_REGISTRY);
 
-    private void defineQuery() {
+        String queryContent = generateSQL();
 
+        Resource q1 = new Resource();
+        q1.setContent(queryContent);
+        q1.setMediaType(RegistryConstants.SQL_QUERY_MEDIA_TYPE);
+        q1.setProperty(RegistryConstants.RESULT_TYPE_PROPERTY_NAME,
+                RegistryConstants.RESOURCES_RESULT_TYPE);
+        String qPath = computeQueryPath();
+        systemRegistry.put(qPath, q1);
     }
 
     private String computeQueryPath() {
-        return null;
+
+        if (queryPath == null) {
+
+            StringBuffer buf = new StringBuffer("/queries/advanced");
+
+            if (resourcePath != null && resourcePath.length() > 0) {
+                buf.append("1");
+            } else {
+                buf.append("0");
+            }
+
+            if (authorName != null && authorName.length() > 0) {
+                buf.append("1");
+            } else {
+                buf.append("0");
+            }
+
+            if (updaterName != null && updaterName.length() > 0) {
+                buf.append("1");
+            } else {
+                buf.append("0");
+            }
+
+            if (commentWords != null && commentWords.length() > 0) {
+                buf.append("1");
+            } else {
+                buf.append("0");
+            }
+
+            if (tag1 != null && tag1.length() > 0) {
+                buf.append("1");
+            } else {
+                buf.append("0");
+            }
+
+            if (tag2 != null && tag2.length() > 0) {
+                buf.append("1");
+            } else {
+                buf.append("0");
+            }
+
+            if (tag3 != null && tag3.length() > 0) {
+                buf.append("1");
+            } else {
+                buf.append("0");
+            }
+
+            queryPath = buf.toString();
+        }
+
+        return queryPath;
+    }
+
+    private String generateSQL() {
+
+        boolean queryStarted = false;
+
+        String pathQuery = "";
+        if (resourcePath != null && resourcePath.length() != 0) {
+            pathQuery = " WHERE R.PATH=?";
+            queryStarted = true;
+        }
+
+        String authorQuery = "";
+        if (authorName != null && authorName.length() != 0) {
+            if (queryStarted) {
+                authorQuery = " AND R.AUTHOR=?";
+            } else {
+                authorQuery = " WHERE R.AUTHOR=?";
+                queryStarted = true;
+            }
+        }
+
+        //String sql = "SELECT A.AID FROM ARTIFACTS A, VERSIONS V, TAGS T, 
COMMENTS C WHERE A.AID=V.AID AND V.VN IN (SELECT MAX(V2.VN) FROM VERSIONS V2 
WHERE A.AID=V2.AID) AND A.AID=T.AID AND A.AID=C.AID AND A.PATH='c1200/r4'";
+        String fromVersions = "";
+        String versionQuery = "";
+        if (updaterName != null && updaterName.length() != 0) {
+            fromVersions = ", VERSIONS V";
+            if (queryStarted) {
+                versionQuery = " AND V.AID=R.AID AND V.VN IN (SELECT 
MAX(V2.VN) FROM VERSIONS V2 WHERE R.AID=V2.AID) AND V.AUTHOR=?";
+            } else {
+                versionQuery = " WHERE V.AID=R.AID AND V.VN IN (SELECT 
MAX(V2.VN) FROM VERSIONS V2 WHERE R.AID=V2.AID) AND V.AUTHOR=?";
+            }
+        }
+
+        String fromComments = "";
+        String commentsQuery = "";
+        if (commentWords != null && commentWords.length() != 0) {
+            fromComments = ", COMMENTS C";
+            if (queryStarted) {
+                commentsQuery = " AND C.AID=R.AID AND C.COMMENT_TEXT LIKE ?";
+            } else {
+                commentsQuery = " WHERE C.AID=R.AID AND C.COMMENT_TEXT LIKE ?";
+                queryStarted = true;
+            }
+        }
+
+        String fromTags = "";
+        String tagsQuery = "";
+        if (tag1 != null && tag1.length() > 0) {
+            String[] tag = new String[] {tag1, tag2, tag3};
+            if (tag.length > 0) {
+                fromTags = ", TAGS T";
+
+                // todo: complete with OR tags
+                if (queryStarted) {
+                    tagsQuery = " AND T.AID=R.AID AND";
+                } else {
+                    tagsQuery = " WHERE T.AID=R.AID AND";
+                    queryStarted = true;
+                }
+
+                tagsQuery = tagsQuery + " (T.TAG_NAME=?";
+                for (int i = 1; i < tag.length; i++) {
+                    tagsQuery = tagsQuery + " OR T.TAG_NAME=?";
+                }
+                tagsQuery = tagsQuery + ")";
+            }
+        }
+
+        String sql = "SELECT R.PATH FROM ARTIFACTS R" + fromVersions + 
fromComments + fromTags + pathQuery + authorQuery + versionQuery + 
commentsQuery + tagsQuery;
+
+        return sql;
     }
 }

Modified: 
trunk/registry/modules/webapps/src/main/java/org/wso2/registry/web/actions/utils/QueryManager.java
==============================================================================
--- 
trunk/registry/modules/webapps/src/main/java/org/wso2/registry/web/actions/utils/QueryManager.java
  (original)
+++ 
trunk/registry/modules/webapps/src/main/java/org/wso2/registry/web/actions/utils/QueryManager.java
  Thu Dec 13 04:45:35 2007
@@ -16,6 +16,8 @@
 
 package org.wso2.registry.web.actions.utils;
 
+import org.wso2.registry.Resource;
+
 import java.util.Map;
 import java.util.Collection;
 

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

Reply via email to