roller-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agillil...@apache.org
Subject svn commit: r416532 - in /incubator/roller/branches/roller_3.0: src/org/apache/roller/ui/rendering/model/ src/org/apache/roller/ui/rendering/search/ src/org/apache/roller/ui/rendering/servlets/ web/WEB-INF/classes/
Date Fri, 23 Jun 2006 00:31:48 GMT
Author: agilliland
Date: Thu Jun 22 17:31:48 2006
New Revision: 416532

URL: http://svn.apache.org/viewvc?rev=416532&view=rev
Log:
deleting old search display classes and adding in new search servlet with it's own search
results model.


Added:
    incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/SearchResultsRenderModel.java
    incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/SearchServlet.java
Removed:
    incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/search/
Modified:
    incubator/roller/branches/roller_3.0/web/WEB-INF/classes/roller.properties

Added: incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/SearchResultsRenderModel.java
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/SearchResultsRenderModel.java?rev=416532&view=auto
==============================================================================
--- incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/SearchResultsRenderModel.java
(added)
+++ incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/model/SearchResultsRenderModel.java
Thu Jun 22 17:31:48 2006
@@ -0,0 +1,238 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  The ASF licenses this file to You
+ * 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.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+
+package org.apache.roller.ui.rendering.model;
+
+import java.io.IOException;
+import java.util.Date;
+import java.util.Map;
+import java.util.ResourceBundle;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.TreeSet;
+import org.apache.commons.collections.comparators.ReverseComparator;
+import org.apache.commons.lang.StringUtils;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.search.Hits;
+import org.apache.roller.RollerException;
+import org.apache.roller.business.search.FieldConstants;
+import org.apache.roller.business.search.operations.SearchOperation;
+import org.apache.roller.model.IndexManager;
+import org.apache.roller.model.Roller;
+import org.apache.roller.model.RollerFactory;
+import org.apache.roller.model.WeblogManager;
+import org.apache.roller.pojos.WeblogEntryData;
+import org.apache.roller.pojos.WeblogEntryWrapperComparator;
+import org.apache.roller.pojos.wrapper.WeblogEntryDataWrapper;
+import org.apache.roller.ui.core.RollerContext;
+import org.apache.roller.ui.rendering.util.WeblogPageRequest;
+import org.apache.roller.ui.rendering.util.WeblogSearchRequest;
+import org.apache.roller.util.DateUtil;
+
+
+/**
+ * Extends normal page renderer model to represent search results.
+ *
+ * Also adds some new methods which are specific only to search results.
+ */
+public class SearchResultsRenderModel extends PageRenderModel {
+    
+    private static final ResourceBundle bundle = 
+            ResourceBundle.getBundle("ApplicationResources");
+    
+    public static final int RESULTS_PER_PAGE = 10;
+    
+    
+    // the original search request
+    WeblogSearchRequest searchRequest = null;
+    
+    // the actual search results mapped by Day -> Set of entries
+    private TreeMap results = new TreeMap(new ReverseComparator());
+    
+    private int hits = 0;
+    private int offset = 0;
+    private int limit = 0;
+    private Set categories = new TreeSet();
+    private boolean websiteSpecificSearch = true;
+    private String errorMessage = null;
+    
+    
+    public SearchResultsRenderModel() {}
+    
+    
+    public void init(Map initData) throws RollerException {
+        
+        // we expect the init data to contain a searchRequest object
+        searchRequest = (WeblogSearchRequest) initData.get("searchRequest");
+        if(searchRequest == null) {
+            throw new RollerException("expected searchRequest from init data");
+        }
+        
+        // we need to add a simple page request to make our parent happy
+        WeblogPageRequest pageRequest = new WeblogPageRequest();
+        pageRequest.setWeblogHandle(searchRequest.getWeblogHandle());
+        pageRequest.setWeblogCategory(searchRequest.getWeblogCategory());
+        initData.put("pageRequest", pageRequest);
+        
+        // let parent initialize
+        super.init(initData);
+        
+        // setup the search
+        IndexManager indexMgr = RollerFactory.getRoller().getIndexManager();
+        
+        SearchOperation search = new SearchOperation(indexMgr);
+        search.setTerm(searchRequest.getQuery());
+        
+        RollerContext rollerContext = RollerContext.getRollerContext();
+        if(rollerContext.isSiteWideWeblog(searchRequest.getWeblogHandle())) {
+            this.websiteSpecificSearch = false;
+        } else {
+            search.setWebsiteHandle(searchRequest.getWeblogHandle());
+        }
+        
+        if(StringUtils.isNotEmpty(searchRequest.getWeblogCategory())) {
+            search.setCategory(searchRequest.getWeblogCategory());
+        }
+        
+        // execute search
+        indexMgr.executeIndexOperationNow(search);
+        
+        if (search.getResultsCount() == -1) {
+            // this means there has been a parsing (or IO) error
+            this.errorMessage = bundle.getString("error.searchProblem");
+        } else {
+            Hits hits = search.getResults();
+            this.hits = search.getResultsCount();
+            
+            // Convert the Hits into WeblogEntryData instances.
+            convertHitsToEntries(hits);
+        }
+    }
+    
+    
+    private void convertHitsToEntries(Hits hits) throws RollerException {
+        
+        // determine offset
+        this.offset = searchRequest.getPageNum() * RESULTS_PER_PAGE;
+        if(this.offset >= hits.length()) {
+            this.offset = 0;
+        }
+        
+        // determine limit
+        this.limit = RESULTS_PER_PAGE;
+        if(this.offset + this.limit > hits.length()) {
+            this.limit = hits.length() - this.offset;
+        }
+        
+        try {
+            TreeSet categories = new TreeSet();
+            Roller roller = RollerFactory.getRoller();
+            WeblogManager weblogMgr = roller.getWeblogManager();
+            
+            WeblogEntryData entry = null;
+            Document doc = null;
+            String handle = null;
+            for(int i = offset; i < offset+limit; i++) {
+                
+                entry = null; // reset for each iteration
+                
+                doc = hits.doc(i);
+                handle = doc.getField(FieldConstants.WEBSITE_HANDLE).stringValue();
+                
+                if(websiteSpecificSearch &&
+                        handle.equals(searchRequest.getWeblogHandle())) {
+                    
+                    entry = weblogMgr.getWeblogEntry(
+                            doc.getField(FieldConstants.ID).stringValue());
+                } else {
+                    
+                    entry = weblogMgr.getWeblogEntry(
+                            doc.getField(FieldConstants.ID).stringValue());
+                    
+                    if (doc.getField(FieldConstants.CATEGORY) != null) {
+                        categories.add(
+                                doc.getField(FieldConstants.CATEGORY).stringValue());
+                    }
+                }
+                
+                // maybe null if search result returned inactive user
+                // or entry's user is not the requested user.
+                if (entry != null) {
+                    addEntryToResults(WeblogEntryDataWrapper.wrap(entry));
+                }
+            }
+            
+            if(categories.size() > 0) {
+                this.categories = categories;
+            }
+        } catch(IOException e) {
+            throw new RollerException(e);
+        }
+    }
+    
+    
+    private void addEntryToResults(WeblogEntryDataWrapper entry) {
+        
+        // convert entry's each date to midnight (00m 00h 00s)
+        Date midnight = DateUtil.getStartOfDay(entry.getPubTime());
+        
+        // ensure we do not get duplicates from Lucene by
+        // using a Set Collection.  Entries sorted by pubTime.
+        TreeSet set = (TreeSet) this.results.get(midnight);
+        if (set == null) {
+            // date is not mapped yet, so we need a new Set
+            set = new TreeSet( new WeblogEntryWrapperComparator());
+            this.results.put(midnight, set);
+        }
+        set.add(entry);
+    }
+    
+    
+    public String getTerm() {
+        return searchRequest.getQuery();
+    }
+
+    public int getHits() {
+        return hits;
+    }
+
+    public int getOffset() {
+        return offset;
+    }
+
+    public int getLimit() {
+        return limit;
+    }
+
+    public TreeMap getResults() {
+        return results;
+    }
+
+    public Set getCategories() {
+        return categories;
+    }
+
+    public boolean isWebsiteSpecificSearch() {
+        return websiteSpecificSearch;
+    }
+
+    public String getErrorMessage() {
+        return errorMessage;
+    }
+    
+}

Added: incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/SearchServlet.java
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/SearchServlet.java?rev=416532&view=auto
==============================================================================
--- incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/SearchServlet.java
(added)
+++ incubator/roller/branches/roller_3.0/src/org/apache/roller/ui/rendering/servlets/SearchServlet.java
Thu Jun 22 17:31:48 2006
@@ -0,0 +1,207 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  The ASF licenses this file to You
+ * 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.  For additional information regarding
+ * copyright in this work, please see the NOTICE file in the top level
+ * directory of this distribution.
+ */
+
+package org.apache.roller.ui.rendering.servlets;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.jsp.JspFactory;
+import javax.servlet.jsp.PageContext;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.roller.RollerException;
+import org.apache.roller.model.RollerFactory;
+import org.apache.roller.model.UserManager;
+import org.apache.roller.pojos.Template;
+import org.apache.roller.pojos.WebsiteData;
+import org.apache.roller.ui.core.RollerContext;
+import org.apache.roller.ui.rendering.Renderer;
+import org.apache.roller.ui.rendering.RendererManager;
+import org.apache.roller.ui.rendering.model.RenderModel;
+import org.apache.roller.ui.rendering.model.RenderModelLoader;
+import org.apache.roller.ui.rendering.model.SearchResultsRenderModel;
+import org.apache.roller.ui.rendering.util.InvalidRequestException;
+import org.apache.roller.ui.rendering.util.WeblogSearchRequest;
+import org.apache.roller.util.cache.CachedContent;
+
+
+/**
+ * Handles search queries for weblogs.
+ *
+ * @web.servlet name="SearchServlet" load-on-startup="5"
+ * @web.servlet-mapping url-pattern="/search/*"
+ */
+public class SearchServlet extends HttpServlet {
+    
+    private static Log log = LogFactory.getLog(SearchServlet.class);
+    
+    
+    /**
+     * Init method for this servlet
+     */
+    public void init(ServletConfig servletConfig) throws ServletException {
+        
+        super.init(servletConfig);
+        
+        log.info("Initializing SearchServlet");
+    }
+    
+    
+    /**
+     * Handle GET requests for weblog pages.
+     */
+    public void doGet(HttpServletRequest request, HttpServletResponse response) 
+            throws ServletException, IOException {
+        
+        log.debug("Entering");
+        
+        WebsiteData weblog = null;
+        WeblogSearchRequest searchRequest = null;
+        
+        // first off lets parse the incoming request and validate it
+        try {
+            searchRequest = new WeblogSearchRequest(request);
+            
+            // now make sure the specified weblog really exists
+            UserManager userMgr = RollerFactory.getRoller().getUserManager();
+            weblog = userMgr.getWebsiteByHandle(searchRequest.getWeblogHandle(), Boolean.TRUE);
+            
+        } catch(InvalidRequestException ire) {
+            // An error initializing the request is considered to be a 404
+            response.sendError(HttpServletResponse.SC_NOT_FOUND);
+            log.error("Bad Request: "+ire.getMessage());
+            return;
+            
+        } catch(RollerException re) {
+            // error looking up the weblog, we assume it doesn't exist
+            response.sendError(HttpServletResponse.SC_NOT_FOUND);
+            log.warn("Unable to lookup weblog ["+
+                    searchRequest.getWeblogHandle()+"] "+re.getMessage());
+            return;
+        }
+        
+        // get their default page template to use for rendering
+        Template page = null;
+        try {
+            page = weblog.getDefaultPage();
+            if(page == null) {
+                throw new RollerException("Could not lookup default page "+
+                        "for weblog "+weblog.getHandle());
+            }
+        } catch(Exception e) {
+            log.error("Error getting weblogs default page", e);
+        }
+        
+        // set the content type
+        response.setContentType("text/html; charset=utf-8");
+        
+        // looks like we need to render content
+        Map model = new HashMap();
+        try {
+            RollerContext rollerContext = RollerContext.getRollerContext();
+            PageContext pageContext = JspFactory.getDefaultFactory().getPageContext(
+                    this, request, response,"", true, 8192, true);
+            
+            // populate the rendering model
+            Map initData = new HashMap();
+            initData.put("request", request);
+            initData.put("searchRequest", searchRequest);
+            
+            // default weblog models
+            RenderModelLoader.loadSearchModels(model, initData);
+            
+            // special site wide models
+            if (rollerContext.isSiteWideWeblog(weblog.getHandle())) {
+                RenderModelLoader.loadSiteModels(model, initData);
+            }
+            
+            // add helpers
+            RenderModelLoader.loadUtilityHelpers(model);
+            RenderModelLoader.loadWeblogHelpers(pageContext, model);
+            RenderModelLoader.loadPluginHelpers(weblog, model);
+
+            // Feeds get weblog's custom models too
+            RenderModelLoader.loadCustomModels(weblog, model, initData);
+            
+            // ick, gotta load pre-3.0 model stuff as well :(
+            RenderModelLoader.loadOldModels(model, request, response, pageContext);
+            
+            // manually add search model again to support pre-3.0 weblogs
+            RenderModel searchModel = new SearchResultsRenderModel();
+            searchModel.init(initData);
+            model.put("searchResults", searchModel);
+            
+        } catch (RollerException ex) {
+            log.error("Error loading model objects for page", ex);
+            
+            if(!response.isCommitted()) response.reset();
+            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+            return;
+        }
+
+        
+        // lookup Renderer we are going to use
+        Renderer renderer = null;
+        try {
+            log.debug("Looking up renderer");
+            renderer = RendererManager.getRenderer("velocity", page.getId());
+        } catch(Exception e) {
+            // nobody wants to render my content :(
+            log.error("Couldn't find renderer for rsd template", e);
+            
+            if(!response.isCommitted()) response.reset();
+            response.sendError(HttpServletResponse.SC_NOT_FOUND);
+            return;
+        }
+        
+        // render content
+        CachedContent rendererOutput = new CachedContent(4096);
+        try {
+            log.debug("Doing rendering");
+            renderer.render(model, rendererOutput.getCachedWriter());
+            
+            // flush rendered output and close
+            rendererOutput.flush();
+            rendererOutput.close();
+        } catch(Exception e) {
+            // bummer, error during rendering
+            log.error("Error during rendering for rsd template", e);
+            
+            if(!response.isCommitted()) response.reset();
+            response.sendError(HttpServletResponse.SC_NOT_FOUND);
+            return;
+        }
+        
+        
+        // post rendering process
+        
+        // flush rendered content to response
+        log.debug("Flushing response output");
+        response.setContentLength(rendererOutput.getContent().length);
+        response.getOutputStream().write(rendererOutput.getContent());
+        
+        log.debug("Exiting");
+    }
+    
+}

Modified: incubator/roller/branches/roller_3.0/web/WEB-INF/classes/roller.properties
URL: http://svn.apache.org/viewvc/incubator/roller/branches/roller_3.0/web/WEB-INF/classes/roller.properties?rev=416532&r1=416531&r2=416532&view=diff
==============================================================================
--- incubator/roller/branches/roller_3.0/web/WEB-INF/classes/roller.properties (original)
+++ incubator/roller/branches/roller_3.0/web/WEB-INF/classes/roller.properties Thu Jun 22
17:31:48 2006
@@ -104,6 +104,10 @@
 org.apache.roller.ui.rendering.model.FeedRenderModel,\
 org.apache.roller.ui.rendering.model.ConfigRenderModel
 
+rendering.searchRenderModels=\
+org.apache.roller.ui.rendering.model.SearchResultsRenderModel,\
+org.apache.roller.ui.rendering.model.ConfigRenderModel
+
 # Set of page models to be created for site-wide rendering
 rendering.siteRenderModels=\
 org.apache.roller.ui.rendering.model.SiteRenderModel,\



Mime
View raw message