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 Mar 11, 2005 12:01:47 PM
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.search;
044:
045: import java.util.ArrayList;
046:
047: import net.jforum.entities.Post;
048: import net.jforum.exceptions.SearchInstantiationException;
049: import net.jforum.util.preferences.ConfigKeys;
050: import net.jforum.util.preferences.SystemGlobals;
051:
052: import org.apache.log4j.Logger;
053:
054: /**
055: * @author Rafael Steil
056: * @version $Id: SearchFacade.java,v 1.8 2007/09/09 22:53:35 rafaelsteil Exp $
057: */
058: public class SearchFacade {
059: private static SearchManager searchManager;
060: private static Logger logger = Logger.getLogger(SearchFacade.class);
061:
062: public static void init() {
063: if (!isSearchEnabled()) {
064: logger
065: .info("Search indexing is disabled. Will try to create a SearchManager "
066: + "instance for runtime configuration changes");
067: }
068:
069: String clazz = SystemGlobals
070: .getValue(ConfigKeys.SEARCH_INDEXER_IMPLEMENTATION);
071:
072: if (clazz == null || "".equals(clazz)) {
073: logger.info(ConfigKeys.SEARCH_INDEXER_IMPLEMENTATION
074: + " is not defined. Skipping.");
075: } else {
076: try {
077: searchManager = (SearchManager) Class.forName(clazz)
078: .newInstance();
079: } catch (Exception e) {
080: logger.warn(e.toString(), e);
081: throw new SearchInstantiationException(
082: "Error while tring to start the search manager: "
083: + e);
084: }
085:
086: searchManager.init();
087: }
088: }
089:
090: public static void create(Post post) {
091: if (isSearchEnabled()) {
092: searchManager.create(post);
093: }
094: }
095:
096: public static void update(Post post) {
097: if (isSearchEnabled()) {
098: searchManager.update(post);
099: }
100: }
101:
102: public static SearchResult search(SearchArgs args) {
103: return isSearchEnabled() ? searchManager.search(args)
104: : new SearchResult(new ArrayList(), 0);
105: }
106:
107: private static boolean isSearchEnabled() {
108: return SystemGlobals
109: .getBoolValue(ConfigKeys.SEARCH_INDEXING_ENABLED);
110: }
111:
112: public static void delete(Post p) {
113: if (isSearchEnabled()) {
114: searchManager.delete(p);
115: }
116: }
117:
118: public static SearchManager manager() {
119: return searchManager;
120: }
121: }
|