Author: gttersen
Date: 2007-03-21 20:15:39 +0100 (Wed, 21 Mar 2007)
New Revision: 4601

Modified:
   
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/SearchModeFactory.java
   
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/command/NewsAggregatorSearchCommand.java
   
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/config/NewsAggregatorSearchConfiguration.java
Log:
Category navigation implemented JIRA(AGGREG-83)

Modified: 
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/SearchModeFactory.java
===================================================================
--- 
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/SearchModeFactory.java
       2007-03-21 14:39:38 UTC (rev 4600)
+++ 
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/SearchModeFactory.java
       2007-03-21 19:15:39 UTC (rev 4601)
@@ -794,6 +794,9 @@
                     fillBeanProperty(nasc, inherit, "xmlMainFile", 
ParseType.String, commandE, "fp_main_main.xml");
                     fillBeanProperty(nasc, inherit, "resultsPerCluster", 
ParseType.Int, commandE, "");
                     fillBeanProperty(nasc, inherit, "clusterMaxFetch", 
ParseType.Int, commandE, "10");
+                    fillBeanProperty(nasc, inherit, "categoryFields", 
ParseType.String, commandE, "");
+                    fillBeanProperty(nasc, inherit, "geographicFields", 
ParseType.String, commandE, "");
+                    nasc.parseCategories(commandE);
                 }
 
                 // query transformers

Modified: 
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/command/NewsAggregatorSearchCommand.java
===================================================================
--- 
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/command/NewsAggregatorSearchCommand.java
     2007-03-21 14:39:38 UTC (rev 4600)
+++ 
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/command/NewsAggregatorSearchCommand.java
     2007-03-21 19:15:39 UTC (rev 4601)
@@ -8,6 +8,7 @@
 import com.fastsearch.esp.search.result.IDocumentSummaryField;
 import com.fastsearch.esp.search.result.IQueryResult;
 import com.fastsearch.esp.search.result.IllegalType;
+import no.schibstedsok.searchportal.datamodel.DataModel;
 import no.schibstedsok.searchportal.datamodel.generic.StringDataObject;
 import 
no.schibstedsok.searchportal.mode.config.NewsAggregatorSearchConfiguration;
 import no.schibstedsok.searchportal.result.BasicSearchResult;
@@ -17,6 +18,7 @@
 import no.schibstedsok.searchportal.result.Navigator;
 import no.schibstedsok.searchportal.result.SearchResult;
 import no.schibstedsok.searchportal.result.SearchResultItem;
+import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.Logger;
 import org.jdom.Document;
 import org.jdom.Element;
@@ -38,7 +40,7 @@
  */
 public class NewsAggregatorSearchCommand extends NavigatableESPFastCommand {
 
-    private final static Logger LOG = 
Logger.getLogger(NewsAggregatorSearchCommand.class);
+    private static final Logger LOG = 
Logger.getLogger(NewsAggregatorSearchCommand.class);
     private static final String PARAM_GEONAV = "geonav";
     private static final String PARAM_CLUSTER_ID = "clusterId";
 
@@ -51,34 +53,52 @@
     }
 
     public SearchResult execute() {
-        LOG.debug("News aggregator search executed with: " + getParameters());
-        LOG.debug("News aggregator search executed with: " + 
datamodel.getParameters());
-
         NewsAggregatorSearchConfiguration config = getSearchConfiguration();
-        LOG.debug("Loading xml file at: " + config.getXmlSource());
 
-        StringDataObject geoNav = 
datamodel.getParameters().getValue(PARAM_GEONAV);
+
         StringDataObject clusterId = 
datamodel.getParameters().getValue(PARAM_CLUSTER_ID);
-
-        String xmlFile = geoNav == null ? config.getXmlMainFile() : 
geoNav.getString();
-
+        String xmlFile = getXmlFileName(datamodel, config);
+        LOG.debug("Loading xml file at: " + config.getXmlSource() + xmlFile);
         if (clusterId == null) {
             return getPageResult(config, xmlFile);
         } else {
-            return getClusterResult(config, clusterId, xmlFile);
+            return getClusterResult(config, clusterId);
         }
     }
 
-    private SearchResult getClusterResult(NewsAggregatorSearchConfiguration 
config, StringDataObject clusterId, String xmlFile) {
-        try {
-            final NewsAggregatorXmlParser newsAggregatorXmlParser = new 
NewsAggregatorXmlParser();
-            final InputStream inputStream = getInputStream(config, xmlFile);
-            return newsAggregatorXmlParser.parseCluster(config, inputStream, 
clusterId.getString(), this);
-        } catch (IOException e) {
-            LOG.debug("Falling back to search instead of xml parse", e);
-        } catch (JDOMException e) {
-            LOG.debug("Falling back to search instead of xml parse", e);
+    private String getXmlFileName(DataModel dataModel, 
NewsAggregatorSearchConfiguration config) {
+        String geographic = "main";
+        String category = "main";
+        String[] geographicFields = config.getGeographicFieldArray();
+        for (String geographicField : geographicFields) {
+            StringDataObject geo = 
dataModel.getParameters().getValue(geographicField);
+            if (geo != null) {
+                geographic = formatToConvention(geo.getString());
+                break;
+            }
         }
+
+        for (String categoryField : config.getCategoryFieldArray()) {
+            StringDataObject cat = 
dataModel.getParameters().getValue(categoryField);
+            if (cat != null) {
+                category = formatToConvention(cat.getString());
+                break;
+            }
+        }
+        StringBuilder sb = new StringBuilder("fp_");
+        sb.append(category).append('_').append(geographic).append(".xml");
+        return sb.toString();
+    }
+
+    private String formatToConvention(String replaceString) {
+        String newString = 
StringUtils.replaceChars(replaceString.toLowerCase(), "æ", "ae");
+        newString = StringUtils.replaceChars(newString, 'ø', 'o');
+        newString = StringUtils.replaceChars(newString, 'å', 'a');
+        newString = StringUtils.replaceChars(newString, ' ', '_');
+        return newString;
+    }
+
+    private SearchResult getClusterResult(NewsAggregatorSearchConfiguration 
config, StringDataObject clusterId) {
         return search(config, clusterId.getString());
     }
 
@@ -110,7 +130,6 @@
         LOG.debug("query-server=" + config.getQueryServer());
         LOG.debug("-----------------------------------------------");
         SearchResult searchResult = super.execute();
-
         return searchResult;
     }
 
@@ -221,7 +240,9 @@
     private SearchResult getPageResult(NewsAggregatorSearchConfiguration 
config, String xmlFile) {
         final NewsAggregatorXmlParser newsAggregatorXmlParser = new 
NewsAggregatorXmlParser();
         try {
-            return newsAggregatorXmlParser.parseFullPage(config, 
getInputStream(config, xmlFile), this);
+            FastSearchResult searchResult = 
newsAggregatorXmlParser.parseFullPage(config, getInputStream(config, xmlFile), 
this);
+            addCategoryNavigators(config, searchResult);
+            return searchResult;
         } catch (JDOMException e) {
             LOG.debug("Falling back to search instead of xml parse", e);
         } catch (IOException e) {
@@ -230,6 +251,34 @@
         return search(config, null);
     }
 
+    private void addCategoryNavigators(NewsAggregatorSearchConfiguration 
config, FastSearchResult searchResult) {
+        final String[] categoryFields = config.getCategoryFieldArray();
+        LOG.debug("Adding category navigators: categoryFields=" + 
categoryFields);
+        if (categoryFields.length > 0) {
+            List<NewsAggregatorSearchConfiguration.Category> categoryList = 
config.getCategories();
+            LOG.debug("categoryList=" + categoryList);
+            for (int i = categoryFields.length - 1; i >= 0; i--) {
+                String categoryField = categoryFields[i];
+                NewsAggregatorSearchConfiguration.Category selectedCategory = 
null;
+                StringDataObject selectedFieldData = 
datamodel.getParameters().getValue(categoryField);
+                LOG.debug("selectedFieldData=" + selectedFieldData);
+                for (NewsAggregatorSearchConfiguration.Category category : 
categoryList) {
+                    searchResult.addModifier(categoryField, new 
Modifier(category.getDisplayName(), -1, null));
+                    LOG.debug("Adding modifier name=" + categoryField + ", " + 
category.getDisplayName());
+                    if (selectedFieldData != null && 
selectedFieldData.getString().equals(category.getDisplayName())) {
+                        selectedCategory = category;
+                        LOG.debug("selectedCategory=" + selectedCategory);
+                    }
+                }
+                if (selectedCategory != null && 
selectedCategory.getSubCategories() != null) {
+                    categoryList = selectedCategory.getSubCategories();
+                } else {
+                    break;
+                }
+            }
+        }
+    }
+
     private InputStream getInputStream(NewsAggregatorSearchConfiguration 
config, String xmlFile) throws IOException {
         final URL url = new URL(config.getXmlSource() + xmlFile);
 // ---------
@@ -256,7 +305,6 @@
         return searchResultItem;
     }
 
-
     @SuppressWarnings({"unchecked"})
     public static class NewsAggregatorXmlParser {
         private static final String ELEMENT_CLUSTER = "cluster";
@@ -279,7 +327,7 @@
             return saxBuilder.build(inputStream);
         }
 
-        public SearchResult parseCluster(NewsAggregatorSearchConfiguration 
config, InputStream inputStream, String clusterId, NewsAggregatorSearchCommand 
searchCommand) throws JDOMException, IOException {
+        public FastSearchResult parseCluster(NewsAggregatorSearchConfiguration 
config, InputStream inputStream, String clusterId, NewsAggregatorSearchCommand 
searchCommand) throws JDOMException, IOException {
             try {
                 final FastSearchResult searchResult = new 
FastSearchResult(searchCommand);
                 final Document doc = getDocument(inputStream);
@@ -305,7 +353,7 @@
             }
         }
 
-        public SearchResult parseFullPage(NewsAggregatorSearchConfiguration 
config, InputStream inputStream, SearchCommand searchCommand) throws 
JDOMException, IOException {
+        public FastSearchResult 
parseFullPage(NewsAggregatorSearchConfiguration config, InputStream 
inputStream, SearchCommand searchCommand) throws JDOMException, IOException {
             try {
 
                 final FastSearchResult searchResult = new 
FastSearchResult(searchCommand);
@@ -446,8 +494,6 @@
                 collapsedResults.addResult(nestedResultItem);
             }
         }
-
-
     }
 
 }

Modified: 
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/config/NewsAggregatorSearchConfiguration.java
===================================================================
--- 
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/config/NewsAggregatorSearchConfiguration.java
        2007-03-21 14:39:38 UTC (rev 4600)
+++ 
trunk/core-api/src/main/java/no/schibstedsok/searchportal/mode/config/NewsAggregatorSearchConfiguration.java
        2007-03-21 19:15:39 UTC (rev 4601)
@@ -1,10 +1,21 @@
 // Copyright (2007) Schibsted Søk AS
 package no.schibstedsok.searchportal.mode.config;
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.log4j.Logger;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 
+import java.util.ArrayList;
+import java.util.List;
+
 public class NewsAggregatorSearchConfiguration extends 
NavigatableESPFastConfiguration {
-    private final static Logger log = 
Logger.getLogger(NewsAggregatorSearchConfiguration.class);
+    private final static Logger LOG = 
Logger.getLogger(NewsAggregatorSearchConfiguration.class);
+    private static final String CMD_ELEMENT_CATEGORIES = "categories";
+    private static final String CMD_ELEMENT_CATEGORY = "category";
+    private static final String CMD_ATTR_DISPLAY_NAME = "display-name";
+    private static final String CMD_ATTR_ID = "id";
 
     private String xmlSource;
     private String xmlMainFile;
@@ -13,6 +24,9 @@
     private int relatedMaxCount = 30;
     private int resultsPerCluster;
     private int clusterMaxFetch;
+    private List<Category> categories;
+    private String geographicFields;
+    private String categoryFields;
 
     public NewsAggregatorSearchConfiguration() {
         super(null);
@@ -22,16 +36,54 @@
         super(sc);
         if (sc instanceof NewsAggregatorSearchConfiguration) {
             final NewsAggregatorSearchConfiguration nasc = 
(NewsAggregatorSearchConfiguration) sc;
-            nasc.setXmlSource(nasc.getXmlSource());
-            nasc.setXmlMainFile(nasc.getXmlMainFile());
-            nasc.setClusterField(nasc.getClusterField());
-            nasc.setNestedResultsField(nasc.getNestedResultsField());
-            nasc.setRelatedMaxCount(nasc.getRelatedMaxCount());
-            nasc.setResultsPerCluster(nasc.getResultsPerCluster());
-            nasc.setClusterMaxFetch(nasc.getClusterMaxFetch());
+            xmlSource = nasc.getXmlSource();
+            xmlMainFile = nasc.getXmlMainFile();
+            clusterField = nasc.getClusterField();
+            nestedResultsField = nasc.getNestedResultsField();
+            relatedMaxCount = nasc.getRelatedMaxCount();
+            resultsPerCluster = nasc.getResultsPerCluster();
+            clusterMaxFetch = nasc.getClusterMaxFetch();
+            categories = nasc.getCategories();
+            geographicFields = nasc.getGeographicFields();
+            categoryFields = nasc.getCategoryFields();
         }
     }
 
+    public void parseCategories(Element commandElement) {
+        NodeList catsNodeList = 
commandElement.getElementsByTagName(CMD_ELEMENT_CATEGORIES);
+        if (catsNodeList != null && catsNodeList.getLength() > 0) {
+            NodeList categoryNodeList = catsNodeList.item(0).getChildNodes();
+            categories = parseCategories(categoryNodeList);
+        }
+    }
+
+    private List<Category> parseCategories(NodeList categoryElements) {
+        if (categoryElements != null && categoryElements.getLength() > 0) {
+            LOG.debug("Parsing categoryList size = " + 
categoryElements.getLength());
+            List<Category> categoryList = null;
+            for (int i = 0; i < categoryElements.getLength(); i++) {
+                Node node = categoryElements.item(i);
+                if (node instanceof Element && 
CMD_ELEMENT_CATEGORY.equals(node.getNodeName())) {
+                    if (categoryList == null) {
+                        categoryList = new ArrayList<Category>();
+                    }
+                    categoryList.add(parseCategory((Element) node));
+                }
+            }
+            return categoryList;
+        } else {
+            return null;
+        }
+    }
+
+    private Category parseCategory(Element categoryElement) {
+        LOG.debug("Parsing category:" + categoryElement);
+        String id = categoryElement.getAttribute(CMD_ATTR_ID);
+        String displayName = 
categoryElement.getAttribute(CMD_ATTR_DISPLAY_NAME);
+        List<Category> subCategories = 
parseCategories(categoryElement.getElementsByTagName(CMD_ELEMENT_CATEGORY));
+        return new Category(id, displayName, subCategories);
+    }
+
     public int getRelatedMaxCount() {
         return relatedMaxCount;
     }
@@ -52,6 +104,26 @@
         this.xmlMainFile = xmlMainFile;
     }
 
+    public String getGeographicFields() {
+        return geographicFields;
+    }
+
+    public void setGeographicFields(String geographicFields) {
+        this.geographicFields = geographicFields;
+    }
+
+    public String[] getCategoryFieldArray() {
+        return StringUtils.split(categoryFields, ',');
+    }
+
+    public String getCategoryFields() {
+        return categoryFields;
+    }
+
+    public void setCategoryFields(String categoryFields) {
+        this.categoryFields = categoryFields;
+    }
+
     public String getXmlMainFile() {
         return xmlMainFile;
     }
@@ -88,4 +160,53 @@
     public void setClusterMaxFetch(int clusterMaxFetch) {
         this.clusterMaxFetch = clusterMaxFetch;
     }
+
+    public List<Category> getCategories() {
+        return categories;
+    }
+
+    public void setCategories(List<Category> categories) {
+        this.categories = categories;
+    }
+
+    public String[] getGeographicFieldArray() {
+        return StringUtils.split(geographicFields, ',');
+    }
+
+    public static final class Category {
+        private final String id;
+        private final String displayName;
+        private final List<Category> subCategories;
+
+        public Category(String id, String displayName) {
+            this(id, displayName, null);
+        }
+
+        public Category(String id, String displayName, List<Category> 
subCategories) {
+            this.id = id;
+            this.displayName = displayName;
+            this.subCategories = subCategories;
+        }
+
+        public String getId() {
+            return id;
+        }
+
+        public String getDisplayName() {
+            return displayName;
+        }
+
+        public List<Category> getSubCategories() {
+            return subCategories;
+        }
+
+        public String toString() {
+            return "Category{" +
+                    "id='" + id + '\'' +
+                    ", displayName='" + displayName + '\'' +
+                    ", hasSubCategories=" + (subCategories != null && 
subCategories.size() > 0) +
+                    '}';
+        }
+    }
+
 }

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

Reply via email to