Author: sshafroi
Date: 2008-05-07 11:41:20 +0200 (Wed, 07 May 2008)
New Revision: 6586

Added:
   
trunk/generic.sesam/view-config/src/main/java/no/sesat/search/view/navigation/TreeNavigationConfig.java
   
trunk/generic.sesam/view-control/src/main/java/no/sesat/search/view/navigation/TreeNavigationController.java
   
trunk/generic.sesam/view-control/src/main/java/no/sesat/search/view/navigation/TreeUrlGenerator.java
Modified:
   
trunk/generic.sesam/view-control/src/main/java/no/sesat/search/view/navigation/AbstractUrlGenerator.java
   
trunk/generic.sesam/view-control/src/main/java/no/sesat/search/view/navigation/BasicUrlGenerator.java
   
trunk/result-spi/src/main/java/no/sesat/search/result/BasicNavigationItem.java
Log:
SEARCH-3427 - Remove AddGeographicNavigationResultHandler

Implementation of a generic tree navigator that can be used to replace the 
AddGeographicNavigationResultHandler.



Added: 
trunk/generic.sesam/view-config/src/main/java/no/sesat/search/view/navigation/TreeNavigationConfig.java
===================================================================
--- 
trunk/generic.sesam/view-config/src/main/java/no/sesat/search/view/navigation/TreeNavigationConfig.java
                             (rev 0)
+++ 
trunk/generic.sesam/view-config/src/main/java/no/sesat/search/view/navigation/TreeNavigationConfig.java
     2008-05-07 09:41:20 UTC (rev 6586)
@@ -0,0 +1,141 @@
+/**
+ * Copyright (2008) Schibsted Søk AS
+ * This file is part of SESAT.
+ *
+ *   SESAT is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU Affero General Public License as published 
by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   SESAT is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU Affero General Public License for more details.
+ *
+ *   You should have received a copy of the GNU Affero General Public License
+ *   along with SESAT.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+package no.sesat.search.view.navigation;
+
+import org.w3c.dom.Element;
+import no.sesat.search.site.config.AbstractDocumentFactory;
+import static 
no.sesat.search.site.config.AbstractDocumentFactory.fillBeanProperty;
+
+import java.util.Set;
+import java.util.HashSet;
+
+/**
+ * This class represent a tree element in the views.xml file.
+ *
+ * Usage:
+ *  <navigation 
url-generator="no.sesat.search.view.navigation.TreeUrlGenerator" 
prefix="/search/">
+ *              <tree id="geonav" hide-parameter="True" name="Hele norge">
+ *                  <tree field="countryregion" name="Nord-Norge">
+ *                      <tree field="county" name="Finnmark"/>
+ *                      <tree field="county" name="Nordland"/>
+ *                      <tree field="county" name="Svalbard"/>
+ *                      <tree field="county" name="Troms"/>
+ *                  </tree>
+ *                  <tree field="countryregion" name="Sørlandet">
+ *                      <tree field="county" name="Aust-Agder"/>
+ *                      <tree field="county" name="Vest-Agder"/>
+ *                  </tree>
+ *                  ...
+ */
[EMAIL 
PROTECTED]("no.sesat.search.view.navigation.TreeNavigationController$Factory")
+public class TreeNavigationConfig extends NavigationConfig.Nav {
+
+    private String name;
+    private String value;
+    private boolean hideParameter;
+    private Set resetParameter;
+
+    /**
+     *
+     * @param parent The parent
+     * @param navigation The navigation
+     * @param navElement
+     */
+    public TreeNavigationConfig(
+            final NavigationConfig.Nav parent,
+            final NavigationConfig.Navigation navigation,
+            final Element navElement) {
+        super(parent, navigation, navElement);
+
+        fillBeanProperty(this, navigation, "name", 
AbstractDocumentFactory.ParseType.String, navElement, null);
+        fillBeanProperty(this, navigation, "value", 
AbstractDocumentFactory.ParseType.String, navElement, name);
+        fillBeanProperty(this, navigation, "hideParameter", 
AbstractDocumentFactory.ParseType.Boolean, navElement, "false");
+
+        if (parent == null) {
+            resetParameter = new HashSet<String>();
+        } else {
+           // if(parent instanceof TreeNavigationConfig) {
+                resetParameter = ((TreeNavigationConfig)parent).resetParameter;
+           // }
+        }
+        resetParameter.add(getField());        
+    }
+
+    /**
+     *
+     * @return name
+     */
+    public String getName() {
+        return name;
+    }
+
+    /**
+     *
+     * @param name  Name used when displaying this element.
+     */
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    /**
+     * If not specified the name will be used as default.
+     *
+     * @return Value used for parameter
+     */
+    public String getValue() {
+        return value;
+    }
+
+    /**
+     *  If not specified the name will be used as default.
+     *
+     * @param value Value used for parameter.
+     */
+    public void setValue(String value) {
+        this.value = value;
+    }
+
+    /**
+     *
+     * @return Set of parameter names that should be reset when generating url
+     */
+    public Set getResetParameter() {
+        return resetParameter;
+    }
+
+    /**
+     * If this element should hide it's parameter or not. Default is false.
+     *
+     * @return true if this element should not generate a parameter.
+     */
+    public boolean isHideParameter() {
+        return hideParameter;
+    }
+
+    /**
+     * Set if this element should hide it's parameter. If this is true,
+     * then the elements field and value will be skipped when generating
+     * url.
+     *
+     * @param hideParameter
+     */
+    public void setHideParameter(boolean hideParameter) {
+        this.hideParameter = hideParameter;
+    }
+}
\ No newline at end of file

Modified: 
trunk/generic.sesam/view-control/src/main/java/no/sesat/search/view/navigation/AbstractUrlGenerator.java
===================================================================
--- 
trunk/generic.sesam/view-control/src/main/java/no/sesat/search/view/navigation/AbstractUrlGenerator.java
    2008-05-07 08:23:44 UTC (rev 6585)
+++ 
trunk/generic.sesam/view-control/src/main/java/no/sesat/search/view/navigation/AbstractUrlGenerator.java
    2008-05-07 09:41:20 UTC (rev 6586)
@@ -22,7 +22,11 @@
 
 import java.util.Map;
 import java.util.Set;
+import java.net.URLEncoder;
+import java.io.UnsupportedEncodingException;
 
+import org.apache.log4j.Logger;
+
 /**
  * The basics of a UrlGenerator.
  *
@@ -35,6 +39,8 @@
  */
 public abstract class AbstractUrlGenerator implements UrlGenerator {
 
+    private static final Logger LOG = 
Logger.getLogger(AbstractUrlGenerator.class);
+
     private final DataModel dataModel;
     private final NavigationState state;
     private final NavigationConfig.Navigation navigation;
@@ -136,4 +142,12 @@
         return dataModel;
     }
 
+    protected static String enc(final String str) {
+        try {
+            return str != null ? URLEncoder.encode(str, "UTF-8") : null;
+        } catch (UnsupportedEncodingException e) {
+            LOG.fatal("UTF-8 encoding not available");
+        }
+        return str;
+    }
 }

Modified: 
trunk/generic.sesam/view-control/src/main/java/no/sesat/search/view/navigation/BasicUrlGenerator.java
===================================================================
--- 
trunk/generic.sesam/view-control/src/main/java/no/sesat/search/view/navigation/BasicUrlGenerator.java
       2008-05-07 08:23:44 UTC (rev 6585)
+++ 
trunk/generic.sesam/view-control/src/main/java/no/sesat/search/view/navigation/BasicUrlGenerator.java
       2008-05-07 09:41:20 UTC (rev 6586)
@@ -20,8 +20,6 @@
 import no.sesat.search.datamodel.DataModel;
 import org.apache.log4j.Logger;
 
-import java.io.UnsupportedEncodingException;
-import java.net.URLEncoder;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
@@ -227,13 +225,4 @@
             urlBuilder.setLength(prefixLength);
         }
     }
-
-    private String enc(final String str) {
-        try {
-            return str != null ? URLEncoder.encode(str, "UTF-8") : null;
-        } catch (UnsupportedEncodingException e) {
-            LOG.fatal("UTF-8 encoding not available");
-        }
-        return str;
-    }
 }

Added: 
trunk/generic.sesam/view-control/src/main/java/no/sesat/search/view/navigation/TreeNavigationController.java
===================================================================
--- 
trunk/generic.sesam/view-control/src/main/java/no/sesat/search/view/navigation/TreeNavigationController.java
                                (rev 0)
+++ 
trunk/generic.sesam/view-control/src/main/java/no/sesat/search/view/navigation/TreeNavigationController.java
        2008-05-07 09:41:20 UTC (rev 6586)
@@ -0,0 +1,102 @@
+/**
+ * Copyright (2008) Schibsted Søk AS
+ * This file is part of SESAT.
+ *
+ *   SESAT is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU Affero General Public License as published 
by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   SESAT is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU Affero General Public License for more details.
+ *
+ *   You should have received a copy of the GNU Affero General Public License
+ *   along with SESAT.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+package no.sesat.search.view.navigation;
+
+import no.sesat.search.result.NavigationItem;
+import no.sesat.search.result.BasicNavigationItem;
+import no.sesat.search.datamodel.DataModel;
+import no.sesat.search.datamodel.generic.StringDataObject;
+
+import java.util.*;
+
+import org.apache.log4j.Logger;
+
+public class TreeNavigationController implements NavigationController {
+    private static final Logger LOG = 
Logger.getLogger(TreeNavigationController.class);
+    private final TreeNavigationConfig nav;
+
+    /**
+     *  Factor class for the TreeNavigationController
+     */
+    public static class Factory implements 
NavigationControllerFactory<TreeNavigationConfig> {
+        public NavigationController get(final TreeNavigationConfig nav) {
+            return new TreeNavigationController(nav);
+        }
+    }
+
+    private TreeNavigationController(final TreeNavigationConfig nav) {
+        this.nav = nav;
+    }
+
+    /**
+     * Go through configuration defined in views.xml (the tree navigation 
element),
+     * and build up a NavigationItem as result.
+     *
+     * @param context
+     * @return the navigationItem coresponding to the tree structure defined in
+     * views.xml.
+     */
+    public NavigationItem getNavigationItems(final Context context) {
+
+        final DataModel dataModel = context.getDataModel();
+
+        final NavigationItem item = new BasicNavigationItem();
+
+        final String url = context.getUrlGenerator().getURL(null, nav);
+
+        final BasicNavigationItem i = new BasicNavigationItem(nav.getName(), 
url, -1);
+        i.setSelected(true);
+        item.addResult(i);
+
+        getNavigationItemsHelper(context, i, nav.getChildNavs());
+
+        return item;
+    }
+
+    private void getNavigationItemsHelper(final Context context, 
NavigationItem item, List<NavigationConfig.Nav> children) {
+
+        if (children.isEmpty()) {
+            return;
+        }
+
+        for (NavigationConfig.Nav child : children) {
+
+            if(child instanceof TreeNavigationConfig) {
+                final String url = 
context.getUrlGenerator().getURL(((TreeNavigationConfig)child).getValue(), 
child);
+                final BasicNavigationItem i = new 
BasicNavigationItem(((TreeNavigationConfig)child).getName(), url, -1);
+
+                final StringDataObject selectedValue = 
context.getDataModel().getParameters().getValue(child.getField());
+
+                if (selectedValue != null && 
selectedValue.getString().equals(i.getTitle())) {
+                    i.setSelected(true);
+                }
+
+                item.addResult(i);
+
+                // If an item is selected all other items on the same 
navigation level are excluded.
+                if (i.isSelected()) {
+                    StringDataObject selectedSubItem = 
context.getDataModel().getParameters().getValue(child.getField());
+                    if (selectedSubItem != null && 
((TreeNavigationConfig)child).getValue().equals(selectedSubItem.getString())) {
+                        getNavigationItemsHelper(context, i, 
child.getChildNavs());
+                    }
+                }
+            }
+        }
+    }
+}
\ No newline at end of file

Added: 
trunk/generic.sesam/view-control/src/main/java/no/sesat/search/view/navigation/TreeUrlGenerator.java
===================================================================
--- 
trunk/generic.sesam/view-control/src/main/java/no/sesat/search/view/navigation/TreeUrlGenerator.java
                                (rev 0)
+++ 
trunk/generic.sesam/view-control/src/main/java/no/sesat/search/view/navigation/TreeUrlGenerator.java
        2008-05-07 09:41:20 UTC (rev 6586)
@@ -0,0 +1,86 @@
+/**
+ * Copyright (2008) Schibsted Søk AS
+ * This file is part of SESAT.
+ *
+ *   SESAT is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU Affero General Public License as published 
by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   SESAT is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU Affero General Public License for more details.
+ *
+ *   You should have received a copy of the GNU Affero General Public License
+ *   along with SESAT.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+package no.sesat.search.view.navigation;
+
+import no.sesat.search.datamodel.DataModel;
+
+import java.util.Map;
+import java.util.Set;
+import java.util.HashMap;
+
+/**
+ * This class is a url generator for the TreeNavigationController class.
+ */
+public class TreeUrlGenerator extends AbstractUrlGenerator {
+
+    public TreeUrlGenerator(DataModel dataModel, NavigationConfig.Navigation 
navigation, NavigationState state) {
+        super(dataModel, navigation, state);
+    }
+
+    public String getURL(String value, NavigationConfig.Nav nav){
+        return getURL(value, nav, new HashMap<String, String>());
+    }
+
+    /**
+     * Generate url for the given TreeNavigationConfiguration.
+     *
+     * @param value
+     * @param nav
+     * @param extraParameters
+     * @return The url
+     */
+    public String getURL(String value, NavigationConfig.Nav nav, Map<String, 
String> extraParameters) {
+        String url = getPrefix() + "?";
+
+        Set <String> parameters = getUrlComponentNames(nav, 
extraParameters.keySet(), value);
+
+        Set<String> remove = ((TreeNavigationConfig)nav).getResetParameter();
+
+        parameters.removeAll(remove);
+
+        for(String param: parameters) {
+            url += generateUrlParameter(param, getUrlComponentValue(nav,  
param,extraParameters));
+        }
+
+        TreeNavigationConfig n = (TreeNavigationConfig)nav;
+        while(n != null) {
+            if(!n.isHideParameter()) {
+                url += generateUrlParameter(n.getField(), n.getValue());
+            }
+            n = (TreeNavigationConfig)n.getParent();
+        }
+
+        return url;
+    }
+
+    /**
+     * Generate a string containing the name and the value as a correct 
encoded url part.
+     *
+     * @param name
+     * @param value
+     * @return
+     */
+    public static String generateUrlParameter(final String name, final String 
value) {
+        if (null != value && value.length() > 0) {
+            return enc(name) + "=" + enc(value) + "&amp;";
+        }
+        return "";
+    }
+}

Modified: 
trunk/result-spi/src/main/java/no/sesat/search/result/BasicNavigationItem.java
===================================================================
--- 
trunk/result-spi/src/main/java/no/sesat/search/result/BasicNavigationItem.java  
    2008-05-07 08:23:44 UTC (rev 6585)
+++ 
trunk/result-spi/src/main/java/no/sesat/search/result/BasicNavigationItem.java  
    2008-05-07 09:41:20 UTC (rev 6586)
@@ -22,6 +22,9 @@
 
 package no.sesat.search.result;
 
+import java.util.List;
+import java.util.Vector;
+
 /**
  *
  * @author <a href="mailto:[EMAIL PROTECTED]">Mick</a>
@@ -75,6 +78,23 @@
         return null;
     }
 
+    /**
+     * Return a list of NavigationItem's that makes up the selected
+     * path of this NavigationItem. This path will go from this element
+     * all the way to the last selected child.
+     *
+     * @return List of selected elements.
+     */
+    public List<NavigationItem> getSelectedBranch() {
+        Vector<NavigationItem> res = new Vector<NavigationItem>();
+        NavigationItem current = this;
+        while(current.isChildSelected()) {
+            current=current.getSelectedChild();
+            res.add(current);
+        }
+        return res;
+    }
+
     private NavigationItem getChildSelectedImpl() {
         
         // XXX Geir's original work had a dirty flag here to cache this result.

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

Reply via email to