From 4b1acef3252db1805adf7a3e6f652a96bd9e2b10 Mon Sep 17 00:00:00 2001 From: "Eric J. Bowersox" Date: Fri, 7 Dec 2001 23:11:15 +0000 Subject: [PATCH] implemented the post search facility at the engine level --- .../venice/core/CommunityContext.java | 5 + .../venice/core/ConferenceContext.java | 5 + .../silverwrist/venice/core/TopicContext.java | 6 +- .../venice/core/TopicMessageFound.java | 34 +++++ .../silverwrist/venice/core/UserContext.java | 6 + .../core/impl/CommunityUserContextImpl.java | 110 ++++++++++++++++ .../core/impl/ConferenceUserContextImpl.java | 105 ++++++++++++++++ .../core/impl/TopicMessageFoundHelper.java | 115 +++++++++++++++++ .../core/impl/TopicMessageFoundImpl.java | 118 ++++++++++++++++++ .../core/impl/TopicUserContextImpl.java | 104 ++++++++++++++- .../venice/core/impl/UserContextImpl.java | 108 ++++++++++++++++ 11 files changed, 713 insertions(+), 3 deletions(-) create mode 100644 src/com/silverwrist/venice/core/TopicMessageFound.java create mode 100644 src/com/silverwrist/venice/core/impl/TopicMessageFoundHelper.java create mode 100644 src/com/silverwrist/venice/core/impl/TopicMessageFoundImpl.java diff --git a/src/com/silverwrist/venice/core/CommunityContext.java b/src/com/silverwrist/venice/core/CommunityContext.java index d60aec9..765179f 100644 --- a/src/com/silverwrist/venice/core/CommunityContext.java +++ b/src/com/silverwrist/venice/core/CommunityContext.java @@ -183,4 +183,9 @@ public interface CommunityContext extends SearchMode public abstract void massMail(String subject, String text) throws AccessError, DataException; + public abstract List searchPosts(String search_terms, int offset, int count) + throws AccessError, DataException; + + public abstract int getSearchPostCount(String search_terms) throws AccessError, DataException; + } // end interface CommunityContext diff --git a/src/com/silverwrist/venice/core/ConferenceContext.java b/src/com/silverwrist/venice/core/ConferenceContext.java index e0d19a7..82369cd 100644 --- a/src/com/silverwrist/venice/core/ConferenceContext.java +++ b/src/com/silverwrist/venice/core/ConferenceContext.java @@ -183,4 +183,9 @@ public interface ConferenceContext public abstract void removeCustomBlocks() throws AccessError, DataException; + public abstract List searchPosts(String search_terms, int offset, int count) + throws AccessError, DataException; + + public abstract int getSearchPostCount(String search_terms) throws AccessError, DataException; + } // end interface ConferenceContext diff --git a/src/com/silverwrist/venice/core/TopicContext.java b/src/com/silverwrist/venice/core/TopicContext.java index b094df4..c34be8e 100644 --- a/src/com/silverwrist/venice/core/TopicContext.java +++ b/src/com/silverwrist/venice/core/TopicContext.java @@ -113,5 +113,9 @@ public interface TopicContext public abstract void sendMailToParticipants(boolean posters, int day_limit, String subject, String text) throws AccessError, DataException; -} // end interface TopicContext + public abstract List searchPosts(String search_terms, int offset, int count) + throws AccessError, DataException; + public abstract int getSearchPostCount(String search_terms) throws AccessError, DataException; + +} // end interface TopicContext diff --git a/src/com/silverwrist/venice/core/TopicMessageFound.java b/src/com/silverwrist/venice/core/TopicMessageFound.java new file mode 100644 index 0000000..90d1ccd --- /dev/null +++ b/src/com/silverwrist/venice/core/TopicMessageFound.java @@ -0,0 +1,34 @@ +/* + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at . + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT + * WARRANTY OF ANY KIND, either express or implied. See the License for the specific + * language governing rights and limitations under the License. + * + * The Original Code is the Venice Web Communities System. + * + * The Initial Developer of the Original Code is Eric J. Bowersox , + * for Silverwrist Design Studios. Portions created by Eric J. Bowersox are + * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. + * + * Contributor(s): + */ +package com.silverwrist.venice.core; + +import java.util.Date; + +public interface TopicMessageFound +{ + public abstract String getIdentifier(); + + public abstract String getAuthor(); + + public abstract Date getPostDate(); + + public abstract int getLineCount(); + + public abstract String getText(); + +} // end interface TopicMessageFound diff --git a/src/com/silverwrist/venice/core/UserContext.java b/src/com/silverwrist/venice/core/UserContext.java index 3edbcc3..d2bf68c 100644 --- a/src/com/silverwrist/venice/core/UserContext.java +++ b/src/com/silverwrist/venice/core/UserContext.java @@ -123,4 +123,10 @@ public interface UserContext extends SearchMode public abstract boolean canSetUserPhoto() throws DataException; + public abstract List searchPosts(String search_terms, int offset, int count) + throws AccessError, DataException; + + public abstract int getSearchPostCount(String search_terms) throws AccessError, DataException; + } // end interface UserContext + diff --git a/src/com/silverwrist/venice/core/impl/CommunityUserContextImpl.java b/src/com/silverwrist/venice/core/impl/CommunityUserContextImpl.java index ed7f29a..9182262 100644 --- a/src/com/silverwrist/venice/core/impl/CommunityUserContextImpl.java +++ b/src/com/silverwrist/venice/core/impl/CommunityUserContextImpl.java @@ -1394,6 +1394,116 @@ class CommunityUserContextImpl implements CommunityContext, CommunityBackend } // end massMail + public List searchPosts(String search_terms, int offset, int count) throws AccessError, DataException + { + if (logger.isDebugEnabled()) + logger.debug("searchPosts('" + search_terms + "', " + offset + ", " + count + ") entry"); + if (search_terms==null) + throw new NullPointerException("invalid search terms"); + if (offset<0) + throw new IllegalArgumentException("invalid offset parameter"); + if (count<=0) + throw new IllegalArgumentException("invalid count parameter"); + + Connection conn = null; + ArrayList rc = new ArrayList(); + + try + { // get a database connection + conn = env.getConnection(); + TopicMessageFoundHelper helper = new TopicMessageFoundHelper(conn); + Statement stmt = conn.createStatement(); + + // create the SQL statement + StringBuffer sql = + new StringBuffer("SELECT c.confid, t.topicid, t.topicnum, p.postid, p.num, p.creator_uid, " + + "p.posted, p.linecount, d.data FROM confs c, sigtoconf s, topics t, posts p, " + + "postdata d, users u LEFT JOIN sigmember m ON (s.sigid = m.sigid " + + "AND u.uid = m.uid) LEFT JOIN confmember x ON (c.confid = x.confid AND " + + "u.uid = x.uid) WHERE u.uid = "); + sql.append(env.getUserID()).append(" AND c.confid = s.confid AND s.sigid = "); + sql.append(cid); + sql.append(" AND GREATEST(u.base_lvl,IFNULL(m.granted_lvl,0),s.granted_lvl,IFNULL(x.granted_lvl,0)) " + + ">= c.read_lvl AND t.confid = c.confid AND t.topicid = p.topicid AND " + + "p.scribble_uid IS NULL AND p.postid = d.postid AND MATCH(d.data) AGAINST ("); + sql.append(SQLUtil.encodeStringArg(search_terms)).append(") LIMIT ").append(offset).append(", "); + sql.append(count).append(';'); + + // execute the query and load the results into the return arraylist + ResultSet rs = stmt.executeQuery(sql.toString()); + while (rs.next()) + rc.add(new TopicMessageFoundImpl(helper,cid,rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getLong(4), + rs.getInt(5),rs.getInt(6),SQLUtil.getFullDateTime(rs,7),rs.getInt(8), + rs.getString(9))); + + } // end try + catch (SQLException e) + { // this becomes a DataException + logger.error("DB error searching for posts: " + e.getMessage(),e); + throw new DataException("unable to search: " + e.getMessage(),e); + + } // end catch + finally + { // make sure we release the connection before we go + env.releaseConnection(conn); + + } // end finally + + if (rc.isEmpty()) + return Collections.EMPTY_LIST; + else + return Collections.unmodifiableList(rc); + + } // end searchPosts + + public int getSearchPostCount(String search_terms) throws AccessError, DataException + { + if (logger.isDebugEnabled()) + logger.debug("getSearchPostCount('" + search_terms + ") entry"); + if (search_terms==null) + throw new NullPointerException("invalid search terms"); + + Connection conn = null; + + try + { // get a database connection + conn = env.getConnection(); + Statement stmt = conn.createStatement(); + + // create the SQL statement + StringBuffer sql = + new StringBuffer("SELECT COUNT(*) FROM confs c, sigtoconf s, topics t, posts p, postdata d, " + + "users u LEFT JOIN sigmember m ON (s.sigid = m.sigid AND u.uid = m.uid) " + + "LEFT JOIN confmember x ON (c.confid = x.confid AND u.uid = x.uid) " + + "WHERE u.uid = "); + sql.append(env.getUserID()).append(" AND c.confid = s.confid AND s.sigid = "); + sql.append(cid); + sql.append(" AND GREATEST(u.base_lvl,IFNULL(m.granted_lvl,0),s.granted_lvl,IFNULL(x.granted_lvl,0)) " + + ">= c.read_lvl AND t.confid = c.confid AND t.topicid = p.topicid AND " + + "p.scribble_uid IS NULL AND p.postid = d.postid AND MATCH(d.data) AGAINST ("); + sql.append(SQLUtil.encodeStringArg(search_terms)).append(");"); + + // execute the query and load the results + ResultSet rs = stmt.executeQuery(sql.toString()); + if (!(rs.next())) + throw new InternalStateError("query failure in CommunityUserContextImpl.getSearchPostCount!"); + return rs.getInt(1); + + } // end try + catch (SQLException e) + { // this becomes a DataException + logger.error("DB error searching for posts: " + e.getMessage(),e); + throw new DataException("unable to search: " + e.getMessage(),e); + + } // end catch + finally + { // make sure we release the connection before we go + env.releaseConnection(conn); + + } // end finally + + } // end getSearchPostCount + /*-------------------------------------------------------------------------------- * Implementations from interface CommunityBackend *-------------------------------------------------------------------------------- diff --git a/src/com/silverwrist/venice/core/impl/ConferenceUserContextImpl.java b/src/com/silverwrist/venice/core/impl/ConferenceUserContextImpl.java index ae28e97..9fab3c4 100644 --- a/src/com/silverwrist/venice/core/impl/ConferenceUserContextImpl.java +++ b/src/com/silverwrist/venice/core/impl/ConferenceUserContextImpl.java @@ -1625,6 +1625,111 @@ class ConferenceUserContextImpl implements ConferenceContext, ConferenceBackend } // end removeCustomBlocks + public List searchPosts(String search_terms, int offset, int count) throws AccessError, DataException + { + if (logger.isDebugEnabled()) + logger.debug("searchPosts('" + search_terms + "', " + offset + ", " + count + ") entry"); + if (search_terms==null) + throw new NullPointerException("invalid search terms"); + if (offset<0) + throw new IllegalArgumentException("invalid offset parameter"); + if (count<=0) + throw new IllegalArgumentException("invalid count parameter"); + if (getConferenceData().canReadConference(level)) + throw new AccessError("You are not permitted to search for posts within this conference."); + + Connection conn = null; + ArrayList rc = new ArrayList(); + + try + { // get a database connection + conn = env.getConnection(); + TopicMessageFoundHelper helper = new TopicMessageFoundHelper(conn); + Statement stmt = conn.createStatement(); + + // create the SQL statement + StringBuffer sql = + new StringBuffer("SELECT t.topicid, t.topicnum, p.postid, p.num, p.creator_uid, p.posted, " + + "p.linecount, d.data FROM topics t, posts p, postdata d WHERE t.confid = "); + sql.append(confid); + sql.append(" AND t.topicid = p.topicid AND p.scribble_uid IS NULL AND p.postid = d.postid " + + "AND MATCH(d.data) AGAINST("); + sql.append(SQLUtil.encodeStringArg(search_terms)).append(") LIMIT ").append(offset).append(", "); + sql.append(count+1).append(';'); + + // execute the query and load the results into the return arraylist + ResultSet rs = stmt.executeQuery(sql.toString()); + while (rs.next()) + rc.add(new TopicMessageFoundImpl(helper,env.getCommunityID(),confid,rs.getInt(1),rs.getInt(2), + rs.getLong(3),rs.getInt(4),rs.getInt(5), + SQLUtil.getFullDateTime(rs,6),rs.getInt(7),rs.getString(8))); + + } // end try + catch (SQLException e) + { // this becomes a DataException + logger.error("DB error searching for posts: " + e.getMessage(),e); + throw new DataException("unable to search: " + e.getMessage(),e); + + } // end catch + finally + { // make sure we release the connection before we go + env.releaseConnection(conn); + + } // end finally + + if (rc.isEmpty()) + return Collections.EMPTY_LIST; + else + return Collections.unmodifiableList(rc); + + } // end searchPosts + + public int getSearchPostCount(String search_terms) throws AccessError, DataException + { + if (logger.isDebugEnabled()) + logger.debug("getSearchPostCount('" + search_terms + ") entry"); + if (search_terms==null) + throw new NullPointerException("invalid search terms"); + + if (getConferenceData().canReadConference(level)) + throw new AccessError("You are not permitted to search for posts within this conference."); + + Connection conn = null; + + try + { // get a database connection + conn = env.getConnection(); + Statement stmt = conn.createStatement(); + + // create the SQL statement + StringBuffer sql = + new StringBuffer("SELECT COUNT(*) FROM topics t, posts p, postdata d WHERE t.confid = "); + sql.append(confid); + sql.append(" AND t.topicid = p.topicid AND p.scribble_uid IS NULL AND p.postid = d.postid " + + "AND MATCH(d.data) AGAINST("); + sql.append(SQLUtil.encodeStringArg(search_terms)).append(");"); + + // execute the query and load the results + ResultSet rs = stmt.executeQuery(sql.toString()); + if (!(rs.next())) + throw new InternalStateError("query failure in ConferenceUserContextImpl.getSearchPostCount!"); + return rs.getInt(1); + + } // end try + catch (SQLException e) + { // this becomes a DataException + logger.error("DB error searching for posts: " + e.getMessage(),e); + throw new DataException("unable to search: " + e.getMessage(),e); + + } // end catch + finally + { // make sure we release the connection before we go + env.releaseConnection(conn); + + } // end finally + + } // end getSearchPostCount + /*-------------------------------------------------------------------------------- * Implementations from interface ConferenceBackend *-------------------------------------------------------------------------------- diff --git a/src/com/silverwrist/venice/core/impl/TopicMessageFoundHelper.java b/src/com/silverwrist/venice/core/impl/TopicMessageFoundHelper.java new file mode 100644 index 0000000..99f5a01 --- /dev/null +++ b/src/com/silverwrist/venice/core/impl/TopicMessageFoundHelper.java @@ -0,0 +1,115 @@ +/* + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at . + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT + * WARRANTY OF ANY KIND, either express or implied. See the License for the specific + * language governing rights and limitations under the License. + * + * The Original Code is the Venice Web Communities System. + * + * The Initial Developer of the Original Code is Eric J. Bowersox , + * for Silverwrist Design Studios. Portions created by Eric J. Bowersox are + * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. + * + * Contributor(s): + */ +package com.silverwrist.venice.core.impl; + +import java.sql.*; +import java.util.*; + +class TopicMessageFoundHelper +{ + /*-------------------------------------------------------------------------------- + * Attributes + *-------------------------------------------------------------------------------- + */ + + private Connection conn; + private HashMap comm_aliases = new HashMap(); + private HashMap conf_aliases = new HashMap(); + private HashMap user_names = new HashMap(); + + /*-------------------------------------------------------------------------------- + * Constructor + *-------------------------------------------------------------------------------- + */ + + TopicMessageFoundHelper(Connection conn) + { + this.conn = conn; + + } // end constructor + + /*-------------------------------------------------------------------------------- + * External operations + *-------------------------------------------------------------------------------- + */ + + final String getCommunityAlias(int cid) throws SQLException + { + Integer key = new Integer(cid); + String rc = (String)(comm_aliases.get(key)); + if (rc==null) + { // create an SQL statement to find the alias + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT alias FROM sigs WHERE sigid = " + cid + ";"); + if (rs.next()) + rc = rs.getString(1); + else + rc = "(unknown)"; + + comm_aliases.put(key,rc); + + } // end if + + return rc; + + } // end getCommunityAlias + + final String getConferenceAlias(int confid) throws SQLException + { + Integer key = new Integer(confid); + String rc = (String)(conf_aliases.get(key)); + if (rc==null) + { // create an SQL statement to find the alias + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT alias FROM confalias WHERE confid = " + confid + " LIMIT 1;"); + if (rs.next()) + rc = rs.getString(1); + else + rc = "(unknown)"; + + conf_aliases.put(key,rc); + + } // end if + + return rc; + + } // end getConferenceAlias + + final String getUserName(int uid) throws SQLException + { + Integer key = new Integer(uid); + String rc = (String)(user_names.get(key)); + if (rc==null) + { // create an SQL statement to find the username + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT username FROM users WHERE uid = " + uid + ";"); + if (rs.next()) + rc = rs.getString(1); + else + rc = "(unknown)"; + + user_names.put(key,rc); + + } // end if + + return rc; + + } // end getUserName + +} // end TopicMessageFoundHelper + diff --git a/src/com/silverwrist/venice/core/impl/TopicMessageFoundImpl.java b/src/com/silverwrist/venice/core/impl/TopicMessageFoundImpl.java new file mode 100644 index 0000000..19c11ff --- /dev/null +++ b/src/com/silverwrist/venice/core/impl/TopicMessageFoundImpl.java @@ -0,0 +1,118 @@ +/* + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at . + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT + * WARRANTY OF ANY KIND, either express or implied. See the License for the specific + * language governing rights and limitations under the License. + * + * The Original Code is the Venice Web Communities System. + * + * The Initial Developer of the Original Code is Eric J. Bowersox , + * for Silverwrist Design Studios. Portions created by Eric J. Bowersox are + * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. + * + * Contributor(s): + */ +package com.silverwrist.venice.core.impl; + +import java.sql.SQLException; +import java.util.*; +import com.silverwrist.venice.core.*; + +class TopicMessageFoundImpl implements TopicMessageFound +{ + /*-------------------------------------------------------------------------------- + * Attributes + *-------------------------------------------------------------------------------- + */ + + private int cid; + private int confid; + private int topicid; + private int topicnum; + private long postid; + private int postnum; + private String ident; + private int creator_uid; + private String creator_name; + private Date postdate; + private int linecount; + private String text; + + /*-------------------------------------------------------------------------------- + * Constructor + *-------------------------------------------------------------------------------- + */ + + TopicMessageFoundImpl(TopicMessageFoundHelper helper, int cid, int confid, int topicid, int topicnum, + long postid, int postnum, int creator_uid, Date postdate, int linecount, String text) + throws SQLException + { + this.cid = cid; + this.confid = confid; + this.topicid = topicid; + this.topicnum = topicnum; + this.postid = postid; + this.postnum = postnum; + this.ident = helper.getCommunityAlias(cid) + "!" + helper.getConferenceAlias(confid) + "." + topicnum + + "." + postnum; + this.creator_uid = creator_uid; + this.creator_name = helper.getUserName(creator_uid); + this.postdate = postdate; + this.linecount = linecount; + this.text = transformText(text); + + } // end constructor + + /*-------------------------------------------------------------------------------- + * Internal operations + *-------------------------------------------------------------------------------- + */ + + private static final String transformText(String text) + { + return text; // TEMP + + } // end transformText + + /*-------------------------------------------------------------------------------- + * Implementations from interface TopicMessageFound + *-------------------------------------------------------------------------------- + */ + + public String getIdentifier() + { + return ident; + + } // end getIdentifier + + public String getAuthor() + { + return creator_name; + + } // end getAuthor + + public Date getPostDate() + { + return postdate; + + } // end getPostDate + + public int getLineCount() + { + return linecount; + + } // end getLineCount + + public String getText() + { + return text; + + } // end getText + +} // end class TopicMessageFoundImpl + + + diff --git a/src/com/silverwrist/venice/core/impl/TopicUserContextImpl.java b/src/com/silverwrist/venice/core/impl/TopicUserContextImpl.java index 03cd169..d1b931d 100644 --- a/src/com/silverwrist/venice/core/impl/TopicUserContextImpl.java +++ b/src/com/silverwrist/venice/core/impl/TopicUserContextImpl.java @@ -1408,6 +1408,108 @@ class TopicUserContextImpl implements TopicContext } // end sendMailToParticipants + public List searchPosts(String search_terms, int offset, int count) throws AccessError, DataException + { + if (logger.isDebugEnabled()) + logger.debug("searchPosts('" + search_terms + "', " + offset + ", " + count + ") entry"); + if (search_terms==null) + throw new NullPointerException("invalid search terms"); + if (offset<0) + throw new IllegalArgumentException("invalid offset parameter"); + if (count<=0) + throw new IllegalArgumentException("invalid count parameter"); + if (!(env.getConference().userCanRead())) + throw new AccessError("You are not permitted to search for posts within this topic."); + + Connection conn = null; + ArrayList rc = new ArrayList(); + + try + { // get a database connection + conn = env.getConnection(); + TopicMessageFoundHelper helper = new TopicMessageFoundHelper(conn); + Statement stmt = conn.createStatement(); + + // create the SQL statement + StringBuffer sql = + new StringBuffer("SELECT p.postid, p.num, p.creator_uid, p.posted, p.linecount, d.data " + + "FROM posts p, postdata d WHERE p.topicid = "); + sql.append(topicid); + sql.append(" AND p.scribble_uid IS NULL AND d.postid = p.postid AND MATCH (d.data) AGAINST ("); + sql.append(SQLUtil.encodeStringArg(search_terms)).append(") LIMIT ").append(offset).append(", "); + sql.append(count+1).append(';'); + + // execute the query and load the results into the return arraylist + ResultSet rs = stmt.executeQuery(sql.toString()); + while (rs.next()) + rc.add(new TopicMessageFoundImpl(helper,env.getCommunityID(),env.getConfID(),topicid,topicnum, + rs.getLong(1),rs.getInt(2),rs.getInt(3), + SQLUtil.getFullDateTime(rs,4),rs.getInt(5),rs.getString(6))); + + } // end try + catch (SQLException e) + { // this becomes a DataException + logger.error("DB error searching for posts: " + e.getMessage(),e); + throw new DataException("unable to search: " + e.getMessage(),e); + + } // end catch + finally + { // make sure we release the connection before we go + env.releaseConnection(conn); + + } // end finally + + if (rc.isEmpty()) + return Collections.EMPTY_LIST; + else + return Collections.unmodifiableList(rc); + + } // end searchPosts + + public int getSearchPostCount(String search_terms) throws AccessError, DataException + { + if (logger.isDebugEnabled()) + logger.debug("getSearchPostCount('" + search_terms + ") entry"); + if (search_terms==null) + throw new NullPointerException("invalid search terms"); + if (!(env.getConference().userCanRead())) + throw new AccessError("You are not permitted to search for posts within this topic."); + + Connection conn = null; + + try + { // get a database connection + conn = env.getConnection(); + Statement stmt = conn.createStatement(); + + // create the SQL statement + StringBuffer sql = + new StringBuffer("SELECT COUNT(*) FROM posts p, postdata d WHERE p.topicid = "); + sql.append(topicid); + sql.append(" AND p.scribble_uid IS NULL AND d.postid = p.postid AND MATCH (d.data) AGAINST ("); + sql.append(SQLUtil.encodeStringArg(search_terms)).append(");"); + + // execute the query and load the results into the return arraylist + ResultSet rs = stmt.executeQuery(sql.toString()); + if (!(rs.next())) + throw new InternalStateError("query failure in TopicUserContextImpl.getSearchPostCount!"); + return rs.getInt(1); + + } // end try + catch (SQLException e) + { // this becomes a DataException + logger.error("DB error searching for posts: " + e.getMessage(),e); + throw new DataException("unable to search: " + e.getMessage(),e); + + } // end catch + finally + { // make sure we release the connection before we go + env.releaseConnection(conn); + + } // end finally + + } // end getSearchPostCount + /*-------------------------------------------------------------------------------- * External operations usable only from within the package *-------------------------------------------------------------------------------- @@ -1663,5 +1765,3 @@ class TopicUserContextImpl implements TopicContext } // end getTopicByNumber } // end class TopicUserContextImpl - - diff --git a/src/com/silverwrist/venice/core/impl/UserContextImpl.java b/src/com/silverwrist/venice/core/impl/UserContextImpl.java index 0ae9b4d..f4da030 100644 --- a/src/com/silverwrist/venice/core/impl/UserContextImpl.java +++ b/src/com/silverwrist/venice/core/impl/UserContextImpl.java @@ -1431,6 +1431,114 @@ class UserContextImpl implements UserContext, UserBackend } // end canSetUserPhoto + public List searchPosts(String search_terms, int offset, int count) throws AccessError, DataException + { + if (logger.isDebugEnabled()) + logger.debug("searchPosts('" + search_terms + "', " + offset + ", " + count + ") entry"); + if (search_terms==null) + throw new NullPointerException("invalid search terms"); + if (offset<0) + throw new IllegalArgumentException("invalid offset parameter"); + if (count<=0) + throw new IllegalArgumentException("invalid count parameter"); + + Connection conn = null; + ArrayList rc = new ArrayList(); + + try + { // get a database connection + conn = env.getConnection(); + TopicMessageFoundHelper helper = new TopicMessageFoundHelper(conn); + Statement stmt = conn.createStatement(); + + // create the SQL statement + StringBuffer sql = + new StringBuffer("SELECT s.sigid, c.confid, t.topicid, t.topicnum, p.postid, p.num, p.creator_uid, " + + "p.posted, p.linecount, d.data FROM confs c, sigtoconf s, topics t, posts p, " + + "postdata d, users u, sigmember m LEFT JOIN confmember x " + + "ON (c.confid = x.confid AND u.uid = x.uid) WHERE u.uid = "); + sql.append(uid); + sql.append(" AND c.confid = s.confid AND s.sigid = m.sigid AND m.uid = u.uid AND " + + "GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,IFNULL(x.granted_lvl,0)) >= c.read_lvl " + + "AND t.confid = c.confid AND t.topicid = p.topicid AND p.scribble_uid IS NULL AND " + + "p.postid = d.postid AND MATCH(d.data) AGAINST ("); + sql.append(SQLUtil.encodeStringArg(search_terms)).append(") LIMIT ").append(offset).append(", "); + sql.append(count).append(';'); + + // execute the query and load the results into the return arraylist + ResultSet rs = stmt.executeQuery(sql.toString()); + while (rs.next()) + rc.add(new TopicMessageFoundImpl(helper,rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getInt(4), + rs.getLong(5),rs.getInt(6),rs.getInt(7), + SQLUtil.getFullDateTime(rs,8),rs.getInt(9),rs.getString(10))); + + } // end try + catch (SQLException e) + { // this becomes a DataException + logger.error("DB error searching for posts: " + e.getMessage(),e); + throw new DataException("unable to search: " + e.getMessage(),e); + + } // end catch + finally + { // make sure we release the connection before we go + env.releaseConnection(conn); + + } // end finally + + if (rc.isEmpty()) + return Collections.EMPTY_LIST; + else + return Collections.unmodifiableList(rc); + + } // end searchPosts + + public int getSearchPostCount(String search_terms) throws AccessError, DataException + { + if (logger.isDebugEnabled()) + logger.debug("getSearchPostCount('" + search_terms + ") entry"); + if (search_terms==null) + throw new NullPointerException("invalid search terms"); + + Connection conn = null; + + try + { // get a database connection + conn = env.getConnection(); + Statement stmt = conn.createStatement(); + + // create the SQL statement + StringBuffer sql = + new StringBuffer("SELECT COUNT(*) FROM confs c, sigtoconf s, topics t, posts p, postdata d, " + + "users u, sigmember m LEFT JOIN confmember x ON (c.confid = x.confid AND " + + "u.uid = x.uid) WHERE u.uid = "); + sql.append(uid); + sql.append(" AND c.confid = s.confid AND s.sigid = m.sigid AND m.uid = u.uid AND " + + "GREATEST(u.base_lvl,m.granted_lvl,s.granted_lvl,IFNULL(x.granted_lvl,0)) >= c.read_lvl " + + "AND t.confid = c.confid AND t.topicid = p.topicid AND p.scribble_uid IS NULL AND " + + "p.postid = d.postid AND MATCH(d.data) AGAINST ("); + sql.append(SQLUtil.encodeStringArg(search_terms)).append(");"); + + // execute the query and load the results + ResultSet rs = stmt.executeQuery(sql.toString()); + if (!(rs.next())) + throw new InternalStateError("query failure in UserContextImpl.getSearchPostCount!"); + return rs.getInt(1); + + } // end try + catch (SQLException e) + { // this becomes a DataException + logger.error("DB error searching for posts: " + e.getMessage(),e); + throw new DataException("unable to search: " + e.getMessage(),e); + + } // end catch + finally + { // make sure we release the connection before we go + env.releaseConnection(conn); + + } // end finally + + } // end getSearchPostCount + /*-------------------------------------------------------------------------------- * Implementations from interface UserBackend *--------------------------------------------------------------------------------