dao.PblogMessageDaoDb.java Source code

Java tutorial

Introduction

Here is the source code for dao.PblogMessageDaoDb.java

Source

/**
* Copyright (c) 2001-2012 "Redbasin Networks, INC" [http://redbasin.org]
*
* This file is part of Redbasin OpenDocShare community project.
*
* Redbasin OpenDocShare is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package dao;

import java.sql.Connection;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import javax.sql.DataSource;
import model.Blog;
import model.Hdlogin;
import model.Photo;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.Fqn;
import util.DbConstants;
import util.DiaryAdmin;
import util.RegexStrUtil;

/**
  * This object implements PblogMessageDao interface
  *
  * @author Smitha Gudur (smitha@redbasin.com)
  * @version $Revision: 1.1 $
  */
public class PblogMessageDaoDb extends BaseDao implements PblogMessageDao {

    protected final Log logger = LogFactory.getLog(getClass());
    private volatile PblogMsgScreenQuery screenMessageQuery;
    private volatile PblogMsgFreezeQuery freezeMessageQuery;

    private volatile PblogMessageQuery listQuery;
    private volatile PblogMessageAddQuery addQuery;
    private volatile PblogMessageDeleteQuery deleteQuery;
    private volatile PblogMsgAttrDeleteQuery deleteAttrQuery;
    private volatile PblogMessageDeleteMidQuery deleteMidQuery;

    // private volatile CarryonByCategoryQuery pQuery;

    //private volatile CarryonDefaultPhotoQuery defaultPhotoQuery;
    private volatile BasicQuery defaultPhotoQuery;

    private volatile PblogMsgAddAttributeQuery addAttrQuery;
    private volatile PblogMessageGetOneQuery getOneMessageQuery;
    private volatile PblogMsgExistsQuery msgExistsQuery;

    private volatile DiaryAdmin diaryAdmin;
    private int RECENT_VISIT_COUNT = 10;

    // replyid is auto incremented in the database, this is not passed.
    // mid is the reply to the thread. so mid value is the rid of some entry to which it was replied to.
    //
    //   -------------------------
    //   tid    mid       rid
    //   -------------------------
    //   1      null       1     
    //   1       1         2     
    //   1       2         3     
    //   1       1         4     
    //   1       1         4     
    //  -------------------------
    //   2      null       5
    //   2       5         6
    //  -------------------------
    //   1       4         7
    //

    /**
      * This method checks if posts/messages exist for a personal blog
      * @param pblogId - the personal blogger id whose blog is checked
      * @param tid - the thread id based on which the messages are retrieved
      * @return boolean - true (if messages exist for this thread), false - otherwise
      * @throws BaseDaoException
     **/
    public String msgExists(String pblogId, String tid) throws BaseDaoException {

        if (RegexStrUtil.isNull(pblogId) || RegexStrUtil.isNull(tid)) {
            throw new BaseDaoException("params are null");
        }

        /**
         *  Get scalability datasource for pblogmessages partitioned on pblogId
         */
        String sourceName = scalabilityManager.getReadScalability(pblogId);
        ds = scalabilityManager.getSource(sourceName);
        if (ds == null) {
            throw new BaseDaoException("ds null, msgExists() " + sourceName + " pblogId = " + pblogId);
        }

        try {
            Object[] params = { (Object) tid };
            List result = msgExistsQuery.execute(params);
            if ((result != null) && (result.size() > 0)) {
                return "1";
            }
        } catch (Exception e) {
            throw new BaseDaoException("error occured while counting pblog messages , " + msgExistsQuery.getSql()
                    + " params (1) tid = " + tid, e);
        }
        return "0";

    }

    public void addPblogMessage(String tid, String mid, String message, String topic, String userId,
            String userLogin, String pblogId, String fontSize, String fontFace, String fontColor, String moodId,
            String bgColor) throws BaseDaoException {

        if (RegexStrUtil.isNull(message) && RegexStrUtil.isNull(topic)) {
            throw new BaseDaoException("message & topic are null");
        }

        if (RegexStrUtil.isNull(tid) || RegexStrUtil.isNull(mid) || RegexStrUtil.isNull(userId)
                || RegexStrUtil.isNull(pblogId)) {
            throw new BaseDaoException("params are null");
        }

        /**
         *  Get scalability datasource for pblogmessages partitioned on pblogId
         */
        String sourceName = scalabilityManager.getWriteScalability(pblogId);
        ds = scalabilityManager.getSource(sourceName);
        if (ds == null) {
            throw new BaseDaoException("ds null, addPblogMessage() " + sourceName + " pblogId = " + pblogId);
        }
        Connection conn = null;
        try {
            conn = ds.getConnection();
            conn.setAutoCommit(false);
            addQuery.run(conn, tid, mid, message, topic, userId, pblogId);
            addAttrQuery.run(conn, tid, "LAST_INSERT_ID()", fontSize, fontFace, fontColor, moodId, bgColor, userId);
        } catch (Exception e) {
            try {
                conn.rollback();
            } catch (Exception e1) {
                try {
                    if (conn != null) {
                        conn.setAutoCommit(true);
                        conn.close();
                    }
                } catch (Exception e2) {
                    throw new BaseDaoException("conn.close() exception", e2);
                }
                throw new BaseDaoException("error rollingback entries from pblogmessageDao", e1);
            }
            throw new BaseDaoException("error adding entries, pblogmessagesDao", e);
        }

        try {
            conn.commit();
        } catch (Exception e3) {
            throw new BaseDaoException("conn.commit exception", e3);
        }

        try {
            if (conn != null) {
                conn.setAutoCommit(true);
                conn.close();
            }
        } catch (Exception e4) {
            throw new BaseDaoException("conn.close exception, setAutoCommit(true) pblogMessageDao ", e4);
        }

        Fqn fqn = cacheUtil.fqn(DbConstants.PBLOG_TOPIC_LIST);
        if (treeCache.exists(fqn, pblogId)) {
            treeCache.remove(fqn, pblogId);
        }

        fqn = cacheUtil.fqn(DbConstants.PBLOG_DAILY_LIST);
        if (treeCache.exists(fqn, pblogId)) {
            treeCache.remove(fqn, pblogId);
        }

        fqn = cacheUtil.fqn(DbConstants.RECENT_PBLOG);
        if (treeCache.exists(fqn, pblogId)) {
            treeCache.remove(fqn, pblogId);
        }

        StringBuffer sb = new StringBuffer(pblogId);
        sb.append("-");
        sb.append(tid);
        fqn = cacheUtil.fqn(DbConstants.PBLOG_TOPIC);
        if (treeCache.exists(fqn, sb.toString())) {
            treeCache.remove(fqn, sb.toString());
        }

        fqn = cacheUtil.fqn(DbConstants.PBLOG_MESSAGES);
        if (treeCache.exists(fqn, sb.toString())) {
            treeCache.remove(fqn, sb.toString());
        }

        fqn = cacheUtil.fqn(DbConstants.PERSONAL_BLOG);
        if (treeCache.exists(fqn, pblogId)) {
            treeCache.remove(fqn, pblogId);
        }

        fqn = cacheUtil.fqn(DbConstants.PERSONAL_BLOG);
        if (treeCache.exists(fqn, pblogId)) {
            treeCache.remove(fqn, pblogId);
        }

        fqn = cacheUtil.fqn(DbConstants.POPULAR_BLOGS);
        if (treeCache.exists(fqn, DbConstants.POPULAR_BLOG_KEY)) {
            treeCache.remove(fqn, DbConstants.POPULAR_BLOG_KEY);
        }

        fqn = cacheUtil.fqn(DbConstants.RECENT_BLOGS);
        if (treeCache.exists(fqn, DbConstants.RECENT_BLOG_KEY)) {
            treeCache.remove(fqn, DbConstants.RECENT_BLOG_KEY);
        }

    }

    /**
     * delete message from personal blogs matching this user and the replyid.
     * @param rid  - the reply id
     * @param userId  - the user id
     * @param userLogin  - the user login
     * @param pblogId  - the blog id
     * @param tid  - the tid
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect
    */
    public void deletePblogMessage(String rid, String userId, String userLogin, String pblogId, String tid)
            throws BaseDaoException {

        if (RegexStrUtil.isNull(rid) || RegexStrUtil.isNull(userId) || RegexStrUtil.isNull(pblogId)) {
            throw new BaseDaoException("params are null");
        }

        /**
         *  check the permission
         */
        boolean isAdmin = true;
        if (!diaryAdmin.isDiaryAdmin(userLogin) && (!pblogId.equals(userId))) {
            isAdmin = false;
        }

        /**
         *  Get scalability datasource for pblogmessages partitioned on pblogid
         */
        String sourceName = scalabilityManager.getWriteScalability(pblogId);
        ds = scalabilityManager.getSource(sourceName);
        if (ds == null) {
            throw new BaseDaoException("ds null, deletePblogMessage() " + sourceName + " pblogid = " + pblogId);
        }

        Connection conn = null;
        try {
            conn = ds.getConnection();
            deleteQuery.run(conn, rid, userId, isAdmin);
            deleteAttrQuery.run(conn, userId, rid);
            //deleteMidQuery.run(conn, rid);
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e2) {
                throw new BaseDaoException("conn.close() exception for pblogmessage  " + " params (2) " + " rid = "
                        + rid + " userId = " + userId, e2);
            }
            throw new BaseDaoException(
                    "error deleting pblogmessage " + " params (2) " + " rid = " + rid + " userId = " + userId, e);
        }
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e2) {
            throw new BaseDaoException("connection close exception for deletePblogMessage() ", e2);
        }

        StringBuffer sb = new StringBuffer(pblogId);
        sb.append("-");
        sb.append(tid);
        String key = sb.toString();
        Fqn fqn = cacheUtil.fqn(DbConstants.PBLOG_MESSAGES);
        if (treeCache.exists(fqn, key)) {
            treeCache.remove(fqn, key);
        }

        fqn = cacheUtil.fqn(DbConstants.PBLOG_TOPIC);
        if (treeCache.exists(fqn, key)) {
            treeCache.remove(fqn, key);
        }

        fqn = cacheUtil.fqn(DbConstants.PBLOG_TOPIC_LIST);
        if (treeCache.exists(fqn, pblogId)) {
            treeCache.remove(fqn, pblogId);
        }

        fqn = cacheUtil.fqn(DbConstants.PBLOG_DAILY_LIST);
        if (treeCache.exists(fqn, pblogId)) {
            treeCache.remove(fqn, pblogId);
        }

        fqn = cacheUtil.fqn(DbConstants.RECENT_PBLOG);
        if (treeCache.exists(fqn, pblogId)) {
            treeCache.remove(fqn, pblogId);
        }

        fqn = cacheUtil.fqn(DbConstants.PERSONAL_BLOG);
        if (treeCache.exists(fqn, pblogId)) {
            treeCache.remove(fqn, pblogId);
        }

        fqn = cacheUtil.fqn(DbConstants.POPULAR_BLOGS);
        if (treeCache.exists(fqn, DbConstants.POPULAR_BLOG_KEY)) {
            treeCache.remove(fqn, DbConstants.POPULAR_BLOG_KEY);
        }

        fqn = cacheUtil.fqn(DbConstants.RECENT_BLOGS);
        if (treeCache.exists(fqn, DbConstants.RECENT_BLOG_KEY)) {
            treeCache.remove(fqn, DbConstants.RECENT_BLOG_KEY);
        }

    }

    /**
      *  Gets the personal blog post that matches this rid.
      *  @param pblogId - the blogger's blogid (loginid)
      *  @param rid - the rid
      *  @param blogger - the blogger's login whose blog is being accessed
      *  @param tid - the tid
      *  @param userLogin - user login 
      *  @return Blog - returns blog bean
      *  @throws BaseDaoException - when error occurs
      */

    public Blog getPblogOneMessage(String pblogId, String rid, String blogger, String tid, String userLogin)
            throws BaseDaoException {

        if (RegexStrUtil.isNull(pblogId) || RegexStrUtil.isNull(rid) || RegexStrUtil.isNull(blogger)) {
            throw new BaseDaoException("params are null");
        }

        /**
         *  Get scalability datasource for pblogmessages partitioned on (pblogId)
         */
        String sourceName = scalabilityManager.getReadScalability(pblogId);
        ds = scalabilityManager.getSource(sourceName);
        if (ds == null) {
            throw new BaseDaoException("ds null, getPblogOneMessage() " + sourceName + " pblogId = " + pblogId);
        }

        try {
            Object[] params = { (Object) rid };
            List result = getOneMessageQuery.execute(params);
            if ((result != null) && (result.size() > 0)) {
                Blog blog = (Blog) result.get(0);
                Hdlogin hdlogin = getLoginid(blogger);
                blog.setObject(DbConstants.BLOGGER_INFO, hdlogin);
                if (!RegexStrUtil.isNull(userLogin) && (diaryAdmin.isDiaryAdmin(userLogin))) {
                    blog.setValue(DbConstants.IS_ADMIN, "1");
                } else {
                    blog.setValue(DbConstants.IS_ADMIN, "0");
                }
                return blog;
            }
        } catch (Exception e) {
            throw new BaseDaoException("error executing " + getOneMessageQuery.getSql() + " rid =" + rid, e);
        }
        return null;
    }

    /**
     * get all messages from personal blogs matching this thread id, reads from slave
     * @param blogId - the blog id of this message
     * @param tid - the thread id
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect
     */
    public Vector getPblogMessages(String blogId, String tid) throws BaseDaoException {
        return getPblogMessages(blogId, tid, DbConstants.READ_FROM_SLAVE, "");
    }

    /**
     * get all messages from personal blogs matching this thread id
     * @param blogId - the personal blog id who owns this topic
     * @param tid - the thread id - the thread id of these messages
     * @param accessFlag  - access information from master(1) o READ_FROM_MASTER or slave (0) READ_FROM_SLAVE
     * @param userLogin  - the userLogin who is accessing this information 
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect
     */
    public Vector getPblogMessages(String blogId, String tid, int accessFlag, String userLogin)
            throws BaseDaoException {

        if (RegexStrUtil.isNull(tid)) {
            throw new BaseDaoException("params are null");
        }

        /** Jboss methods
         * fqn - full qualified name
         * check if the pblog already set in the cache
         * If it exists, return the pblog from the cache.
         */
        String key = null;
        Fqn fqn = cacheUtil.fqn(DbConstants.PBLOG_MESSAGES);
        if (blogId != null) {
            StringBuffer sb = new StringBuffer(blogId);
            sb.append("-");
            sb.append(tid);
            key = sb.toString();
            Object obj = treeCache.get(fqn, key);
            if (obj != null) {
                return (Vector) obj;
            }
        }

        /**
         *  Get scalability datasource for personal blogs, pblogmsgattr partitioned on pblogid 
         */
        String sourceName = null;
        if (accessFlag == 1) {
            sourceName = scalabilityManager.getWriteScalability(blogId);
        } else {
            sourceName = scalabilityManager.getReadScalability(blogId);
        }
        ds = scalabilityManager.getSource(sourceName);
        if (ds == null) {
            throw new BaseDaoException("ds null, listing pblog messages () " + sourceName + " userid = " + blogId);
        }

        List result = null;
        try {
            Object[] params = { (Object) tid };
            result = listQuery.execute(params);
        } catch (Exception e) {
            throw new BaseDaoException(
                    "error occured, listing pblog messages , " + listQuery.getSql() + " params (1) tid = " + tid,
                    e);
        }

        Blog pblogMsg = null;
        Blog root = new Blog();
        root.setValue(DbConstants.RID, "0");
        root.setValue(DbConstants.MID, "0");
        root.setValue(DbConstants.LEVEL, "0");
        root.setValue(DbConstants.TID, tid);
        /**
         * cannot delete the root, set it to 1
         */
        root.setValue(DbConstants.NUM_POSTS, "1");

        String rid, mid;
        rid = mid = null;

        if (result != null) {
            for (int i = 0; i < result.size(); i++) {
                pblogMsg = (Blog) result.get(i);
                rid = pblogMsg.getValue(DbConstants.REPLY_ID);
                mid = pblogMsg.getValue(DbConstants.MESSAGE_ID);
                pblogMsg.setValue(DbConstants.NUM_POSTS, "0");
                // set the diaryadmin
                if (!RegexStrUtil.isNull(userLogin) && (diaryAdmin.isDiaryAdmin(userLogin))) {
                    pblogMsg.setValue(DbConstants.IS_ADMIN, "1");
                } else {
                    pblogMsg.setValue(DbConstants.IS_ADMIN, "0");
                }

                if (mid.equals("0")) {
                    setLoginAndPhoto(pblogMsg.getValue(DbConstants.OWNER_ID), pblogMsg, accessFlag);
                    pblogMsg.setValue(DbConstants.LEVEL, "1");
                    root.addReply(pblogMsg);
                    //logger.info("Adding child reply to root: mid = " + pblogMsg.getValue(DbConstants.MESSAGE_ID) + ", rid = " + pblogMsg.getValue(DbConstants.REPLY_ID));
                } else {
                    Blog tempMsg = getNode(mid, root);
                    if (tempMsg != null) {
                        //logger.info("Finding node with mid = " + mid + ", node = " + tempMsg.toString());
                        String l1 = tempMsg.getValue(DbConstants.LEVEL);
                        //logger.info("l1 = " + l1);
                        int i1 = new Integer(l1).intValue() + 1;
                        //logger.info("i1 = " + i1);
                        String level = new Integer(i1).toString();
                        //logger.info("level = " + level);
                        //String level = new Integer(new Integer(tempMsg.getValue("level")).intValue() + 1).toString();
                        //logger.info("Setting level to node: level = " + level + ", mid = " + pblogMsg.getValue(DbConstants.MESSAGE_ID) + ", rid = " + pblogMsg.getValue(DbConstants.REPLY_ID));
                        pblogMsg.setValue(DbConstants.LEVEL, level);
                        setLoginAndPhoto(pblogMsg.getValue(DbConstants.OWNER_ID), pblogMsg, accessFlag);
                        /**
                         *  num_posts indicates, this message has reply message or children messages
                         */
                        tempMsg.setValue(DbConstants.NUM_POSTS, "1");
                        tempMsg.addReply(pblogMsg);
                        //logger.info("Added child reply to node: parent mid = " + tempMsg.getValue(DbConstants.MESSAGE_ID) + ", parent rid = " + tempMsg.getValue(DbConstants.REPLY_ID) + ", child mid = " + pblogMsg.getValue(DbConstants.MESSAGE_ID) + ", child rid = " + pblogMsg.getValue(DbConstants.REPLY_ID));
                    }
                }
            }

        }

        Vector v = getList(root, new Vector());
        /* if (v.size() > 0) {
           logger.info("v.size() > 0 " + v.size());
        } */
        if (!RegexStrUtil.isNull(blogId)) {
            treeCache.put(fqn, key, v);
        }
        return v;
    }

    /**
     *  Sets the photo and login for each user
     */
    private void setLoginAndPhoto(String ownerId, Blog pBlogMsg, int accessFlag) {

        if (RegexStrUtil.isNull(ownerId) || (pBlogMsg == null)) {
            throw new BaseDaoException("params are null");
        }

        /** Jboss methods
          * fqn - full qualified name
          * check if the photo already set in the cache, get the first photo
          * If it exists, return the photo from the cache.
          */
        Fqn fqn = cacheUtil.fqn(DbConstants.DEFAULT_PHOTO);
        Object obj = treeCache.get(fqn, ownerId);
        if (obj != null) {
            pBlogMsg.setPhoto((Photo) obj);
        }

        Fqn memFqn = cacheUtil.fqn(DbConstants.LOGIN_INFO);
        Object memObj = treeCache.get(memFqn, ownerId);
        if (memObj != null) {
            pBlogMsg.setObject(DbConstants.OWNER_INFO, memObj);
        }

        if ((obj != null) && (memObj != null)) {
            return;
        }
        /**
         *  Get the hdlogin information for each user
         */
        if (memObj == null) {
            Hdlogin hdlogin = getLogin(ownerId);
            if (hdlogin != null) {
                pBlogMsg.setObject(DbConstants.OWNER_INFO, hdlogin);
                treeCache.put(memFqn, ownerId, (Hdlogin) hdlogin);
            }
        }

        /**
         *  Get scalability datasource for pblogmessages partitioned on tid
         */
        String queryName = null;
        if (accessFlag == DbConstants.READ_FROM_MASTER) {
            queryName = scalabilityManager.getWriteZeroScalability("userdefaultphotoQuery");
        } else {
            queryName = scalabilityManager.getReadZeroScalability("userdefaultphotoQuery");
        }
        defaultPhotoQuery = getQueryMapper().getQuery(queryName);

        /**
        *  get the photo query for each user, retrieves the entryid of Ist photo for each user
        * save the entire list of photos in the photos directory
        */
        try {
            //Object[] params = {(Object)ownerId, (Object)DbConstants.PHOTO_CATEGORY};
            //List uResult = pQuery.execute(params);
            Object[] params = { (Object) ownerId };
            List uResult = defaultPhotoQuery.execute(params);
            if ((uResult != null) && (uResult.size() > 0)) {
                pBlogMsg.setPhoto((Photo) uResult.get(0));
                treeCache.put(fqn, ownerId, (Photo) uResult.get(0));
            }
        } catch (Exception e) {
            throw new BaseDaoException("error executing Query " + defaultPhotoQuery.getSql(), e);
        }

    }

    private int getNodeLevel(String rid, Vector msgs, Blog pBlogMsg) {
        if (RegexStrUtil.isNull(rid) || (msgs == null) || (pBlogMsg == null)) {
            throw new BaseDaoException("params are null");
        }

        int j = 0;
        Iterator iterator = msgs.iterator();
        if (rid != null) {
            while (iterator.hasNext()) {
                Vector replies = (Vector) iterator.next();
                if (replies == null) {
                    //logger.info("replies is null ");
                    continue;
                }
                for (int i = 0; i < replies.size(); i++) {
                    if (rid.equalsIgnoreCase(((Blog) replies.elementAt(i)).getValue(DbConstants.REPLY_ID))) {
                        String levelStr = ((Blog) replies.elementAt(i)).getValue(DbConstants.LEVEL);

                        //logger.info("level = " + levelStr + "msgs rid " + ((Blog)replies.elementAt(i)).getValue(DbConstants.MESSAGE_ID) );
                        if (levelStr != null) {
                            Integer level = new Integer(levelStr);
                            //logger.info("level Integer = " + level);
                            level++;
                            //logger.info("level Integer = " + level.toString() + " j = " + j);
                            pBlogMsg.setValue("level", level.toString());
                        }
                        return (j + 1);
                    }
                }
                j++;
            }
        }
        return -1;
    }

    private Blog getNode(String mid, Blog root) {

        if (RegexStrUtil.isNull(mid) || (root == null)) {
            throw new BaseDaoException("params are null");
        }
        if (mid.equals(root.getValue(DbConstants.REPLY_ID))) {
            return root;
        } else {
            List pBlogMsgs = (List) root.getReplies();
            for (int i = 0; i < pBlogMsgs.size(); i++) {
                Blog pBlogMsg = getNode(mid, (Blog) pBlogMsgs.get(i));
                if (pBlogMsg != null) {
                    return pBlogMsg;
                }
            }
        }
        return null;
    }

    private Vector getList(Blog root, Vector v) {

        if ((root == null) || (v == null)) {
            throw new BaseDaoException("params are null");
        }

        List children = (List) root.getReplies();
        if (children != null) {
            for (int i = 0; i < children.size(); i++) {
                Blog pBlogMsg = (Blog) children.get(i);
                v.add(children.get(i));
                v = getList((Blog) children.get(i), v);
            }
        }
        return v;
    }

    /**
      * freeze message from personal blogs that matches this reply id
      * @param pblogId - personal blog id
      * @param rid - reply id
      * @param userId - user id
      * @param userLogin - user login
      * @param freezeVal - freeze value
      * @param tid - the thread id
      * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect
      *
      *   Freezes messages, so no replies can be posted on this message. 
      *   In the personal blogging, the owner is entered as the administrator of the personal blog.
      *   so the owner is organizer of the personalblog and is allowed to screen/delete/freeze personal blog messages.
      *   In the personalblog, the owner can screen/delete/freeze other people's posting to the personalblog.
      */
    public void freezeMessage(String pblogId, String rid, String userId, String userLogin, String freezeVal,
            String tid) throws BaseDaoException {

        if (RegexStrUtil.isNull(pblogId) || RegexStrUtil.isNull(rid) || RegexStrUtil.isNull(userLogin)
                || RegexStrUtil.isNull(userId) || RegexStrUtil.isNull(tid)) {
            throw new BaseDaoException("params are null");
        }

        boolean isAdmin = true;

        /**
         * Check for permissions in the pblogmessages, is this pblogger or the admin? 
         */
        if (!diaryAdmin.isDiaryAdmin(userLogin) && (!pblogId.equals(userId))) {
            isAdmin = false;
        }

        /**
         *  Get scalability datasource for pblogmessages partitioned on loginid
         */
        String sourceName = scalabilityManager.getWriteScalability(pblogId);
        ds = scalabilityManager.getSource(sourceName);
        if (ds == null) {
            throw new BaseDaoException("ds null, freezeMessage() " + sourceName + " pblogId = " + pblogId);
        }

        Connection conn = null;
        try {
            conn = ds.getConnection();
            freezeMessageQuery.run(conn, rid, userId, isAdmin, freezeVal);
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e2) {
                throw new BaseDaoException("connection close exception for freezeMessage() userId = " + userId
                        + " rid =" + rid + " isAdmin=" + isAdmin, e2);
            }
            throw new BaseDaoException("error occured while freezeMessage(), userId =" + userId + " rid=" + rid
                    + " isAdmin = " + isAdmin, e);
        }

        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e2) {
            throw new BaseDaoException("connection close exception for freezeMessage() pblogId = " + pblogId
                    + " rid =" + rid + " userLogin=" + userLogin, e2);
        }

        StringBuffer sb = new StringBuffer(pblogId);
        sb.append("-");
        sb.append(tid);
        String key = sb.toString();
        Fqn fqn = cacheUtil.fqn(DbConstants.PBLOG_MESSAGES);
        if (treeCache.exists(fqn, key)) {
            treeCache.remove(fqn, key);
        }

        fqn = cacheUtil.fqn(DbConstants.PBLOG_TOPIC);
        if (treeCache.exists(fqn, key)) {
            treeCache.remove(fqn, key);
        }

        fqn = cacheUtil.fqn(DbConstants.PBLOG_TOPIC_LIST);
        if (treeCache.exists(fqn, pblogId)) {
            treeCache.remove(fqn, pblogId);
        }

        fqn = cacheUtil.fqn(DbConstants.PBLOG_DAILY_LIST);
        if (treeCache.exists(fqn, pblogId)) {
            treeCache.remove(fqn, pblogId);
        }

        fqn = cacheUtil.fqn(DbConstants.RECENT_PBLOG);
        if (treeCache.exists(fqn, pblogId)) {
            treeCache.remove(fqn, pblogId);
        }

        fqn = cacheUtil.fqn(DbConstants.PERSONAL_BLOG);
        if (treeCache.exists(fqn, pblogId)) {
            treeCache.remove(fqn, pblogId);
        }

        fqn = cacheUtil.fqn(DbConstants.POPULAR_BLOGS);
        if (treeCache.exists(fqn, DbConstants.POPULAR_BLOG_KEY)) {
            treeCache.remove(fqn, DbConstants.POPULAR_BLOG_KEY);
        }

        fqn = cacheUtil.fqn(DbConstants.RECENT_BLOGS);
        if (treeCache.exists(fqn, DbConstants.RECENT_BLOG_KEY)) {
            treeCache.remove(fqn, DbConstants.RECENT_BLOG_KEY);
        }
    }

    /**
     * screen message from personal blogs that matches this reply id
     * @param pblogId - the personal blog id
     * @param rid - reply id
     * @param userId - the user id
     * @param tid - thread id
     * @param userLogin - the user login
     * @param screenVal - the screen value (to display or not to display)
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect
     *
     *   screen Message - screens the Message from others 
     *   In the personal blogging, the owner is entered as the administrator of the personal blog
     *   so the owner is organizer of the personalblog and is allowed to screen/delete/freeze personal blog messages
     *   In the personalblog, the owner can screen/delete/freeze other people's posting to the personalblog
     */
    public void screenMessage(String pblogId, String rid, String userId, String tid, String userLogin,
            String screenVal) throws BaseDaoException {

        if (RegexStrUtil.isNull(pblogId) || RegexStrUtil.isNull(rid) || RegexStrUtil.isNull(userLogin)
                || RegexStrUtil.isNull(userId) || RegexStrUtil.isNull(tid)) {
            throw new BaseDaoException("params are null");
        }

        /**
         *  Get scalability datasource for pblogmessages partitioned on pblogId
         */
        String sourceName = scalabilityManager.getWriteScalability(pblogId);
        ds = scalabilityManager.getSource(sourceName);
        if (ds == null) {
            throw new BaseDaoException("ds null, screenMessage() " + sourceName + " pblogId = " + pblogId);
        }

        /**
         *    check for pblog messages, is this user organizer or the admin?
         */
        boolean isAdmin = true;
        if (!diaryAdmin.isDiaryAdmin(userLogin) && !pblogId.equals(userId)) {
            isAdmin = false;
        }

        /**
         *  Screen this message, update it in the DB 
         */
        Connection conn = null;
        try {
            conn = ds.getConnection();
            screenMessageQuery.run(conn, rid, userId, isAdmin, screenVal);
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e2) {
                throw new BaseDaoException("connection close exception for screenMessage() userId = " + userId
                        + " rid =" + rid + " isAdmin=" + isAdmin, e2);
            }
            throw new BaseDaoException("error occured while screenMessage, userId =" + userId + " rid=" + rid
                    + " isAdmin = " + isAdmin, e);
        }
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e2) {
            throw new BaseDaoException("connection close exception for screenTopic() userId = " + userId + " rid ="
                    + rid + " isAdmin=" + isAdmin, e2);
        }

        StringBuffer sb = new StringBuffer(pblogId);
        sb.append("-");
        sb.append(tid);
        String key = sb.toString();
        Fqn fqn = cacheUtil.fqn(DbConstants.PBLOG_MESSAGES);
        if (treeCache.exists(fqn, key)) {
            treeCache.remove(fqn, key);
        }

        fqn = cacheUtil.fqn(DbConstants.PBLOG_TOPIC);
        if (treeCache.exists(fqn, key)) {
            treeCache.remove(fqn, key);
        }

        fqn = cacheUtil.fqn(DbConstants.PBLOG_TOPIC_LIST);
        if (treeCache.exists(fqn, pblogId)) {
            treeCache.remove(fqn, pblogId);
        }

        fqn = cacheUtil.fqn(DbConstants.PBLOG_DAILY_LIST);
        if (treeCache.exists(fqn, pblogId)) {
            treeCache.remove(fqn, pblogId);
        }

        fqn = cacheUtil.fqn(DbConstants.RECENT_PBLOG);
        if (treeCache.exists(fqn, pblogId)) {
            treeCache.remove(fqn, pblogId);
        }

        fqn = cacheUtil.fqn(DbConstants.PERSONAL_BLOG);
        if (treeCache.exists(fqn, pblogId)) {
            treeCache.remove(fqn, pblogId);
        }

        fqn = cacheUtil.fqn(DbConstants.POPULAR_BLOGS);
        if (treeCache.exists(fqn, DbConstants.POPULAR_BLOG_KEY)) {
            treeCache.remove(fqn, DbConstants.POPULAR_BLOG_KEY);
        }

        fqn = cacheUtil.fqn(DbConstants.RECENT_BLOGS);
        if (treeCache.exists(fqn, DbConstants.RECENT_BLOG_KEY)) {
            treeCache.remove(fqn, DbConstants.RECENT_BLOG_KEY);
        }
    }

    /**
      *  This property is setby spring automatically at web.xml startup
      *  @param ds - this is JDBC datasource bean that is connection from the pool
      */
    public void setJdbcSource(DataSource ds) {
        this.ds = ds;
    }

    public void setpblogmessagescreenQuery(PblogMsgScreenQuery daq) {
        this.screenMessageQuery = daq;
    }

    public void setpblogmessagefreezeQuery(PblogMsgFreezeQuery daq) {
        this.freezeMessageQuery = daq;
    }

    public void setpblogmessageQuery(PblogMessageQuery daq) {
        this.listQuery = daq;
    }

    public void setpblogmessageaddQuery(PblogMessageAddQuery daq) {
        this.addQuery = daq;
    }

    public void setpblogmessagedeleteQuery(PblogMessageDeleteQuery daq) {
        this.deleteQuery = daq;
    }

    public void setpblogmsgattrdeleteQuery(PblogMsgAttrDeleteQuery daq) {
        this.deleteAttrQuery = daq;
    }

    public void setpblogmessagedeletemidQuery(PblogMessageDeleteMidQuery daq) {
        this.deleteMidQuery = daq;
    }

    /*
       public void setcarryonbycategoryQuery(CarryonByCategoryQuery daq) {
    this.pQuery = daq;
       }
    */

    public void setDiaryadmin(DiaryAdmin diaryAdmin) {
        this.diaryAdmin = diaryAdmin;
    }

    public void setaddattrQuery(PblogMsgAddAttributeQuery daq) {
        this.addAttrQuery = daq;
    }

    public void setpblogmessagegetoneQuery(PblogMessageGetOneQuery daq) {
        this.getOneMessageQuery = daq;
    }

    public void setpblogmsgexistsQuery(PblogMsgExistsQuery daq) {
        this.msgExistsQuery = daq;
    }

}