Author: sshafroi
Date: 2008-05-23 15:12:45 +0200 (Fri, 23 May 2008)
New Revision: 6635

Modified:
   
trunk/generic.sesam/search-command-config/src/main/java/no/sesat/search/mode/config/EspFastCommandConfig.java
   
trunk/generic.sesam/search-command-config/src/main/java/no/sesat/search/mode/config/FastCommandConfig.java
   
trunk/generic.sesam/search-command-control/fast/src/main/java/no/sesat/search/mode/command/AbstractSimpleFastSearchCommand.java
   
trunk/search-command-config-spi/src/main/java/no/sesat/search/mode/SearchModeFactory.java
   
trunk/search-command-config-spi/src/main/java/no/sesat/search/mode/config/AbstractSearchConfiguration.java
   
trunk/search-command-config-spi/src/main/java/no/sesat/search/mode/config/CommandConfig.java
Log:
Work on SKER4404
Summary: Automatically assign config settings in readSearchConfiguration where 
there is a setter

This is work in progress and is not enabled by default.



Modified: 
trunk/generic.sesam/search-command-config/src/main/java/no/sesat/search/mode/config/EspFastCommandConfig.java
===================================================================
--- 
trunk/generic.sesam/search-command-config/src/main/java/no/sesat/search/mode/config/EspFastCommandConfig.java
       2008-05-22 07:53:27 UTC (rev 6634)
+++ 
trunk/generic.sesam/search-command-config/src/main/java/no/sesat/search/mode/config/EspFastCommandConfig.java
       2008-05-23 13:12:45 UTC (rev 6635)
@@ -290,5 +290,24 @@
         return this;
     }
 
+    protected void readSearchConfigurationAfter(Element element, 
SearchConfiguration inherit) {
+        super.readSearchConfigurationAfter(element, inherit);
+        final EspFastCommandConfig efscInherit = inherit instanceof 
EspFastCommandConfig
+                ? (EspFastCommandConfig) inherit
+                : null;
 
+        if (efscInherit != null && efscInherit.getNavigators() != null) {
+
+            navigators.putAll(efscInherit.getNavigators());
+        }
+
+        final NodeList nList = element.getElementsByTagName("navigators");
+
+        for (int i = 0; i < nList.getLength(); ++i) {
+            final Collection<Navigator> navigators = parseNavigators((Element) 
nList.item(i));
+            for (Navigator navigator : navigators) {
+                addNavigator(navigator, navigator.getId());
+            }
+        }
+    }
 }

Modified: 
trunk/generic.sesam/search-command-config/src/main/java/no/sesat/search/mode/config/FastCommandConfig.java
===================================================================
--- 
trunk/generic.sesam/search-command-config/src/main/java/no/sesat/search/mode/config/FastCommandConfig.java
  2008-05-22 07:53:27 UTC (rev 6634)
+++ 
trunk/generic.sesam/search-command-config/src/main/java/no/sesat/search/mode/config/FastCommandConfig.java
  2008-05-23 13:12:45 UTC (rev 6635)
@@ -18,12 +18,8 @@
  */
 package no.sesat.search.mode.config;
 
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
+
 import no.sesat.search.mode.SearchModeFactory.Context;
 import no.sesat.search.mode.config.CommandConfig.Controller;
 import no.sesat.search.result.Navigator;
@@ -101,8 +97,10 @@
      *
      * @return
      */
-    public List<String> getCollections() {
-        return collections;
+    public String[] getCollections() {
+        String[] res = new String[collections.size()];
+        collections.toArray(res);
+        return res;
     }
 
     /**
@@ -111,7 +109,8 @@
      */
     public void addCollections(final String[] collectionArray) {
         for (String string : collectionArray) {
-            collections.add(string);
+            if (!string.equals(""))
+                collections.add(string);
         }
     }
 
@@ -175,7 +174,7 @@
      *
      * @return
      */
-    public Map<String,String> getSearchParameters() {
+    public Map<String,String> getSearchParameterMap() {
         return searchParameters;
     }
 
@@ -192,6 +191,20 @@
 
     /**
      *
+     * @return Array of search parameters.
+     */
+    public String[] getSearchParameters() {
+        String[] res = new String[searchParameters.size()];
+        int index = 0;
+        for (String key : searchParameters.keySet()) {
+            res[index] = key + "=" + searchParameters.get(key);
+            index ++;
+        }
+        return res;
+    }
+
+    /**
+     *
      * @return
      */
     public boolean isLemmatise() {
@@ -464,14 +477,6 @@
     }
 
     /**
-     * Setter for property collectionFilterString.
-     * @param collectionFilterString New value of property 
collectionFilterString.
-     */
-    public void setCollectionFilterString(final String collectionFilterString) 
{
-        this.collectionString = collectionFilterString;
-    }
-
-    /**
      * Setter for property filter.
      * @param filter New value of property filter.
      */
@@ -537,12 +542,14 @@
         if (element.getAttribute("collections").length() > 0) {
             addCollections(element.getAttribute("collections").split(","));
         }else if(null != fscInherit){
-            collections.addAll(fscInherit.getCollections());
+            for(String collection : fscInherit.getCollections()) {
+                collections.add(collection);
+            }
         }
 
         // search parameters
         if(null != fscInherit){
-            searchParameters.putAll(fscInherit.getSearchParameters());
+            searchParameters.putAll(fscInherit.getSearchParameterMap());
         }
         if (element.getAttribute("search-parameters").length() > 0) {
             
setSearchParameters(element.getAttribute("search-parameters").split(","));
@@ -587,5 +594,27 @@
         return this;
     }
 
+    protected void readSearchConfigurationAfter(Element element, 
SearchConfiguration inherit) {
+        super.readSearchConfigurationAfter(element, inherit);
+        final FastCommandConfig fscInherit = inherit instanceof 
FastCommandConfig
+                ? (FastCommandConfig) inherit
+                : null;
+        if (getQueryServerUrl() == null || "".equals(getQueryServerUrl())) {
+            LOG.debug("queryServerURL is empty for " + getId());
+        }
 
+        if (fscInherit != null && fscInherit.getNavigators() != null) {
+
+            navigators.putAll(fscInherit.getNavigators());
+        }
+
+        final NodeList nList = element.getElementsByTagName("navigators");
+
+        for (int i = 0; i < nList.getLength(); ++i) {
+            final Collection<Navigator> navigators = parseNavigators((Element) 
nList.item(i));
+            for (Navigator navigator : navigators) {
+                addNavigator(navigator, navigator.getId());
+            }
+        }
+    }
 }

Modified: 
trunk/generic.sesam/search-command-control/fast/src/main/java/no/sesat/search/mode/command/AbstractSimpleFastSearchCommand.java
===================================================================
--- 
trunk/generic.sesam/search-command-control/fast/src/main/java/no/sesat/search/mode/command/AbstractSimpleFastSearchCommand.java
     2008-05-22 07:53:27 UTC (rev 6634)
+++ 
trunk/generic.sesam/search-command-control/fast/src/main/java/no/sesat/search/mode/command/AbstractSimpleFastSearchCommand.java
     2008-05-23 13:12:45 UTC (rev 6635)
@@ -416,7 +416,7 @@
      */
     protected void setAdditionalParameters(final ISearchParameters params) {
 
-        for(Map.Entry<String,String> entry : 
getSearchConfiguration().getSearchParameters().entrySet()){
+        for(Map.Entry<String,String> entry : 
getSearchConfiguration().getSearchParameterMap().entrySet()){
             params.setParameter(new SearchParameter(entry.getKey(), 
entry.getValue()));
         }
     }

Modified: 
trunk/search-command-config-spi/src/main/java/no/sesat/search/mode/SearchModeFactory.java
===================================================================
--- 
trunk/search-command-config-spi/src/main/java/no/sesat/search/mode/SearchModeFactory.java
   2008-05-22 07:53:27 UTC (rev 6634)
+++ 
trunk/search-command-config-spi/src/main/java/no/sesat/search/mode/SearchModeFactory.java
   2008-05-23 13:12:45 UTC (rev 6635)
@@ -549,6 +549,7 @@
             else {
                 sc.readSearchConfiguration(element, inherit, context);
             }
+            LOG.info("ParsedSearchConfiguration: " + sc);
             return sc;
         }
 

Modified: 
trunk/search-command-config-spi/src/main/java/no/sesat/search/mode/config/AbstractSearchConfiguration.java
===================================================================
--- 
trunk/search-command-config-spi/src/main/java/no/sesat/search/mode/config/AbstractSearchConfiguration.java
  2008-05-22 07:53:27 UTC (rev 6634)
+++ 
trunk/search-command-config-spi/src/main/java/no/sesat/search/mode/config/AbstractSearchConfiguration.java
  2008-05-23 13:12:45 UTC (rev 6635)
@@ -29,6 +29,9 @@
     private static final Logger LOG = 
Logger.getLogger(AbstractSearchConfiguration.class);
     private final static Map<Class, Map<String, MethodWrapper>> ClassMethodMap 
= new HashMap<Class, Map<String, MethodWrapper>>();
 
+    private static final String[] getters = {"get", "is"};
+    private static final String[] setters = {"set", "add"};
+
     /**
      * This method will be called before settings from the modes.xml files 
will be applied.
      * Default implementation is empty.
@@ -60,7 +63,7 @@
      */
     public final void readSearchConfiguration(final Element element, final 
SearchConfiguration inherit) {
         readSearchConfigurationBefore(element, inherit);
-        Set<String> methods = getMethodNames();
+        Set<String> methods = getMethodNames(setters);
 
         NamedNodeMap attribs = element.getAttributes();
         for (int i = 0; i < attribs.getLength(); i++) {
@@ -94,6 +97,14 @@
         readSearchConfigurationAfter(element, inherit);
     }
 
+    private static boolean startsWith(String string, String[] prefixes) {
+        for( String p : prefixes) {
+            if (string.startsWith(p))
+                return true;
+        }
+        return false;
+    }
+
     /**
      * Fetch the method map for klass, if it has not been initialized this will
      * be done first.
@@ -115,34 +126,32 @@
         for (int i = 0; i < methods.length; i++) {
             Method method = methods[i];
             String name = method.getName();
-            String shortName = name.substring(3);
-            if (name.startsWith("set") || name.startsWith("add")) {
+            if (startsWith(name, setters)) {
                 Class[] paramTypes = method.getParameterTypes();
                 if (paramTypes.length == 1) {
                     MethodWrapper.Type type = 
MethodWrapper.getType(paramTypes[0]);
-
-
                     if (type == MethodWrapper.Type.Unsupported) {
-                        LOG.info("Unsupported type for: " + name + " " + 
method.getParameterTypes()[0].getSimpleName());
+                        LOG.trace("Unsupported type for: " + name + " " + 
method.getParameterTypes()[0].getSimpleName());
                     } else {
 
-                        if (!methodMap.containsKey(shortName)) {
-                            methodMap.put(shortName, new MethodWrapper(type));
+                        if (!methodMap.containsKey(name)) {
+                            methodMap.put(name, new MethodWrapper(type, 
method));
+                        } else {
+                            LOG.error("Method already in map: " + name);
                         }
-
-                        methodMap.get(shortName).setMethod = method;
                     }
                 }
-            } else if (name.startsWith("get") && 
method.getParameterTypes().length == 0) {
-
+            } else if (startsWith(name, getters) && 
method.getParameterTypes().length == 0) {
                 MethodWrapper.Type type = 
MethodWrapper.getType(method.getReturnType());
-
-                if (!methodMap.containsKey(shortName)) {
-                    methodMap.put(shortName, new MethodWrapper(type));
+                if (type == MethodWrapper.Type.Unsupported) {
+                    LOG.trace("Unsupported type for: " + name + " " + 
method.getReturnType().getSimpleName());
+                } else {
+                    if (!methodMap.containsKey(name)) {
+                        methodMap.put(name, new MethodWrapper(type, method));
+                    } else {
+                        LOG.error("Method already in map: " + name);
+                    }
                 }
-
-                methodMap.get(shortName).getMethod = method;
-
             }
         }
         getMethodMap(klass.getSuperclass());
@@ -154,12 +163,19 @@
      *
      * @return Set with all method wrappers found for this class.
      */
-    private Set<String> getMethodNames() {
+    private Set<String> getMethodNames(String[] prefix) {
         Set<String> result = new HashSet<String>();
         Class klass = getClass();
-        Map map = getMethodMap(klass);
+        Map<String, MethodWrapper> map = getMethodMap(klass);
         while (map != null) {
-            result.addAll(map.keySet());
+            for (String key : map.keySet()) {
+                for (String s : prefix) {
+                    if (key.startsWith(s)) {
+                        result.add(key.substring(s.length()));
+                    }
+                }
+            }
+
             klass = klass.getSuperclass();
             map = getMethodMap(klass);
         }
@@ -168,38 +184,64 @@
 
     private void setAttribute(final String name, final Object value) {
         Class klass = getClass();
-        MethodWrapper method = getMethodWrapper(klass, name);
+        MethodWrapper method = getMethodWrapper(klass, name, setters);
 
         if (method != null) {
-            if (method.setValue(this, value)) {
-                LOG.info("Setting value on " + getClass() + " " + name + "==" 
+ value);
-            } else {
-                LOG.info("Setting value on " + getClass() + " " + name + "==" 
+ value
+            if (!method.setValue(this, value)) {
+                LOG.error("Setting value on " + getClass().getSimpleName() + " 
" + name + "==" + value
                         + " failed.");
             }
+        } else {
+            LOG.error("Setting value on " + getClass().getSimpleName() + " " + 
name + "==" + value
+                    + " failed because method was not found.");
         }
-        return;
+
     }
 
     private static Object getAttribute(final Object object, final String name) 
{
-        MethodWrapper method = getMethodWrapper(object.getClass(), name);
+        MethodWrapper method = getMethodWrapper(object.getClass(), name, 
getters);
 
         return method != null ? method.getValue(object) : null;
     }
 
-    private static MethodWrapper getMethodWrapper(Class klass, final String 
name) {
+    private static MethodWrapper getMethodWrapper(Class klass, final String 
name, final String[] pre) {
         Map<String, MethodWrapper> map = getMethodMap(klass);
         while (map != null) {
-            if (map.containsKey(name)) {
-                return map.get(name);
+            for(String s : pre) {
+                String g = s + name.substring(0, 1).toUpperCase() + 
name.substring(1);
+                if (map.containsKey(g)) {
+                    return map.get(g);
+                }
             }
             klass = klass.getSuperclass();
             map = getMethodMap(klass);
         }
-        LOG.info("Could not find method with name: " + name + " in class " + 
klass.getSimpleName());
         return null;
     }
 
+    public String toString() {
+        String res = getClass().getSimpleName() + " ";
+        Set<String> methods = getMethodNames(getters);
+        for (String s : methods) {
+            res += s + " == ";
+            Object o = getAttribute(this, s);
+            if (o instanceof String[]) {
+                res += "[";
+                for (String a : (String[])o) {
+                    res += "\"" + a + "\" ";
+                }
+                res += "] ";
+            } else if (o instanceof String) {
+                res += "\"" + o + "\" ";
+            } else {
+                res += o + " ";
+            }
+        }
+        return res;
+    }
+
+
+
     /**
      * Helper class to encapsulate a method.
      */
@@ -209,66 +251,53 @@
             String, StringArray, Integer, Boolean, Char, Unsupported
         }
 
-        private Method setMethod;
-        private Method getMethod;
+        private Method method;
         private final Type type;
 
-        private MethodWrapper(Type type) {
+        private MethodWrapper(Type type, Method method) {
             this.type = type;
+            this.method = method;
         }
 
         private boolean setValue(Object obj, Object value) {
-            if (setMethod != null) {
-                if (value instanceof String) {
-                    String valueString = (String) value;
-                    switch (type) {
-                        case String:
-                            break;
-                        case StringArray:
-                            value = valueString.split(",");
-                            break;
-                        case Integer:
-                            value = Integer.parseInt(valueString);
-                            break;
-                        case Boolean:
-                            value = Boolean.parseBoolean(valueString);
-                            break;
-                        case Char:
-                            value = valueString.charAt(0);
-                            if (valueString.length() > 1)
-                                LOG.error("Setting char attribute where input 
was more then a character long");
-                            break;
-                        default:
-                            LOG.error("Failed to set attribute " + 
setMethod.getName() + ", unnsuported type.");
-                            return false;
+            if (value instanceof String) {
+                String valueString = (String) value;
+                switch (type) {
+                    case String:
+                        break;
+                    case StringArray:
+                        value = valueString.split(",");
+                        break;
+                    case Integer:
+                        value = Integer.parseInt(valueString);
+                        break;
+                    case Boolean:
+                        value = Boolean.parseBoolean(valueString);
+                        break;
+                    case Char:
+                        value = valueString.charAt(0);
+                        if (valueString.length() > 1)
+                            LOG.error("Setting char attribute where input was 
more then a character long");
+                        break;
+                    default:
+                        LOG.error("Failed to set attribute " + 
method.getName() + ", unnsuported type.");
+                        return false;
                     }
                 }
                 try {
-                    setMethod.invoke(obj, value);
+                    method.invoke(obj, value);
                 } catch (Exception e) {
-                    LOG.info("Failed to set attribute with name: " + 
setMethod.getName() + "(" + type + ").");
+                    LOG.info("Failed to set attribute with name: " + 
method.getName() + "(" + type + ").");
                     return false;
                 }
-            } else {
-                LOG.info("Failed to set attribute, set method == null.");
-
-                return false;
-            }
             return true;
         }
 
         private Object getValue(Object obj) {
-
-            if (getMethod != null) {
-
-                try {
-                    Object res = getMethod.invoke(obj);
-                    LOG.info("getting " + getMethod.getName() + " --> " + res);
-                    return res;
-                } catch (Exception e) {
-                    LOG.error("Failed to get attribute with name: " + type + " 
" + setMethod.getName() + "(). " + e.getMessage(), e);
-                }
-
+            try {
+                return method.invoke(obj);
+            } catch (Exception e) {
+                LOG.error("Failed to get attribute with name: " + type + " " + 
method.getName() + "(). " + e.getMessage(), e);
             }
             return null;
         }

Modified: 
trunk/search-command-config-spi/src/main/java/no/sesat/search/mode/config/CommandConfig.java
===================================================================
--- 
trunk/search-command-config-spi/src/main/java/no/sesat/search/mode/config/CommandConfig.java
        2008-05-22 07:53:27 UTC (rev 6634)
+++ 
trunk/search-command-config-spi/src/main/java/no/sesat/search/mode/config/CommandConfig.java
        2008-05-23 13:12:45 UTC (rev 6635)
@@ -171,7 +171,9 @@
         String [] res = new String[resultFields.size()];
         int index = 0;
         for (String key : resultFields.keySet()) {
-            res[index] = key + " AS " + resultFields.get(key);
+            String value = resultFields.get(key);
+            res[index] = (key.equals(value)) ? key :  key + " AS " + value;
+            index ++;
         }
         return res;
     }
@@ -257,13 +259,6 @@
         this.asynchronous = asynchronous;
     }
 
-
-    /** [EMAIL PROTECTED] **/
-    @Override
-    public String toString(){
-        return getClass().getSimpleName() + " [" + name + "]";
-    }
-
     /**
      * @param fieldAndFilter
      *            String containing name of field and filter seperated with ' 
AS '.

_______________________________________________
Kernel-commits mailing list
[email protected]
http://sesat.no/mailman/listinfo/kernel-commits

Reply via email to