001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on 25/07/2007 19:27:48
040: *
041: * The JForum Project
042: * http://www.jforum.net
043: */
044: package net.jforum.search;
045:
046: import java.util.ArrayList;
047: import java.util.HashMap;
048: import java.util.Iterator;
049: import java.util.List;
050: import java.util.Map;
051:
052: import net.jforum.entities.Forum;
053: import net.jforum.repository.ForumRepository;
054:
055: /**
056: * @author Rafael Steil
057: * @version $Id: SearchOperation.java,v 1.5 2007/07/30 14:06:44 rafaelsteil Exp $
058: */
059: public abstract class SearchOperation {
060: public abstract SearchResult performSearch(SearchArgs args);
061:
062: public abstract int totalRecords();
063:
064: public abstract void prepareForDisplay();
065:
066: public abstract List results();
067:
068: public abstract String viewTemplate();
069:
070: protected abstract int extractForumId(Object value);
071:
072: public final List filterResults(List results) {
073: List l = new ArrayList();
074:
075: Map forums = new HashMap();
076:
077: for (Iterator iter = results.iterator(); iter.hasNext();) {
078: Object currentObject = iter.next();
079:
080: Integer forumId = new Integer(this
081: .extractForumId(currentObject));
082: ForumFilterResult status = (ForumFilterResult) forums
083: .get(forumId);
084:
085: if (status == null) {
086: Forum f = ForumRepository.getForum(forumId.intValue());
087: status = new ForumFilterResult(f);
088: forums.put(forumId, status);
089: }
090:
091: if (status.isValid()) {
092: // TODO: decouple
093: if (currentObject instanceof SearchPost) {
094: ((SearchPost) currentObject).setForum(status
095: .getForum());
096: }
097:
098: l.add(currentObject);
099: }
100: }
101:
102: return l;
103: }
104:
105: private static class ForumFilterResult {
106: private Forum forum;
107:
108: public ForumFilterResult(Forum forum) {
109: this .forum = forum;
110: }
111:
112: public Forum getForum() {
113: return this .forum;
114: }
115:
116: public boolean isValid() {
117: return this.forum != null;
118: }
119: }
120: }
|