dao.ColTopicDaoDb.java Source code

Java tutorial

Introduction

Here is the source code for dao.ColTopicDaoDb.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.List;
import javax.sql.DataSource;
import model.ColTopic;
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.RegexStrUtil;
import util.GlobalConst;

/**
  * <B>ColTopicDaoDb</B>
  * <BR>
  * This object implements ColTopicDao interface
  * This object has methods for collabrum topic management
  * @author Smitha Gudur (smitha@redbasin.com)
  * @version $Revision: 1.1 $
  */
public class ColTopicDaoDb extends DirectoryAbstractDao implements ColTopicDao {

    //private volatile DiaryAdmin diaryAdmin;

    protected final Log logger = LogFactory.getLog(getClass());
    //private volatile ExpiringObjectPool eop;

    // private volatile CarryonByCategoryQuery pQuery; 
    private volatile BasicQuery defaultPhotoQuery;

    private volatile ColTopicAddQuery addQuery;
    private volatile ColTopicDeleteQuery deleteQuery;
    private volatile ColTopicFreezeQuery freezeQuery;
    private volatile ColTopicQuery topicQuery;
    private volatile ColTopicGetQuery getTopicsQuery;
    // private volatile CollabrumAdminExistsQuery adminExistsQuery;
    private volatile ColMessageCountQuery msgCountQuery;
    private volatile ColMessageExistsQuery msgExistsQuery;
    private volatile ColTopicScreenQuery screenQuery;
    private volatile ColTopicIncrementHitsQuery incrementHitsQuery;
    private volatile ColTopicAddAttributeQuery addAttributeQuery;

    // 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
    //

    /**
     * add a new topic in collabrum or network blog
     * @param collabrumId
     * @param topic
     * @param message
     * @param userId
     * @param userLogin
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect
     */
    public void addColTopic(String collabrumId, String topic, String message, String userId, String userLogin,
            String title, String fontSize, String fontFace, String fontColor, String moodId, String bgColor)
            throws BaseDaoException {

        //if ( RegexStrUtil.isNull(collabrumId) || RegexStrUtil.isNull(message) || 
        //   RegexStrUtil.isNull(topic) || RegexStrUtil.isNull(userId) || RegexStrUtil.isNull(userLogin) ) {

        if (RegexStrUtil.isNull(collabrumId) || RegexStrUtil.isNull(topic) || RegexStrUtil.isNull(userId)
                || RegexStrUtil.isNull(userLogin)) {
            throw new BaseDaoException("params are null");
        }

        /**
              *  check if this user is the diaryadmin or moderator for this collabrum topic - if yes, delete
              *  otherwise barf!
              */
        /*
           if (!isOrganizer(collabrumId, userLogin, userId) ) {
              throw new BaseDaoException("Cannot add collabrum topic, neither DiaryAdmin nor Moderator" + userLogin + collabrumId);
                }
        */

        /**
         *  Get scalability datasource for colltopic partitioned on collabrumid
         */
        String sourceName = scalabilityManager.getWriteScalability(collabrumId);
        ds = scalabilityManager.getSource(sourceName);
        if (ds == null) {
            throw new BaseDaoException("ds null, addColTopic() " + sourceName + " userId = " + userId);
        }

        Connection conn = null;
        try {
            conn = ds.getConnection();
            conn.setAutoCommit(false);
            addQuery.run(conn, collabrumId, message, topic, userId, title);
            addAttributeQuery.run(conn, "LAST_INSERT_ID()", fontSize, fontFace, fontColor, moodId, bgColor);
        } catch (Exception e) {
            try {
                conn.rollback();
            } catch (Exception e1) {
                try {
                    if (conn != null) {
                        conn.setAutoCommit(true);
                        conn.close();
                    }
                } catch (Exception e2) {
                    throw new BaseDaoException("connection close exception for adding coltopic & attr", e2);
                }
                throw new BaseDaoException("error occured while rollingback entries from adding coltopic", e1);
            }
            throw new BaseDaoException("AutoCommit(false) and add exception while adding coltopic", e);
        }

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

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

        Fqn fqn = cacheUtil.fqn(DbConstants.COLTOPICS);
        if (treeCache.exists(fqn, collabrumId)) {
            treeCache.remove(fqn, collabrumId);
        }
        fqn = cacheUtil.fqn(DbConstants.COLLABRUM);
        if (treeCache.exists(fqn, collabrumId)) {
            treeCache.remove(fqn, collabrumId);
        }
    }

    /**
     * delete message from coltopic, check if this is the owner
     * @param collabrumId - the collabrum id
     * @param tid  - the thread id
     * @param userId  - the user id
     * @param userLogin  - the user login
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect
     */
    public void deleteColTopic(String collabrumId, String tid, String userLogin, String userId)
            throws BaseDaoException {

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

        /**
              * check if collabrum messages exist for this topic, if they exist, donot delete it
         */
        Fqn fqn = cacheUtil.fqn(DbConstants.COLTOPIC);
        StringBuffer buf = new StringBuffer(collabrumId);
        buf.append("-");
        buf.append(tid);
        String key = buf.toString();
        Object obj = treeCache.get(fqn, key);
        if (obj != null) {
            int numPosts = new Integer(((ColTopic) obj).getValue(DbConstants.NUM_POSTS)).intValue();
            if (numPosts > 0) {
                throw new BaseDaoException("This collabrum topic cannot be removed as it has messages posted");
            }
        } else {
            int numPosts = new Integer(((String) getColMessagesCount(tid, DbConstants.READ_FROM_MASTER)))
                    .intValue();
            if (numPosts > 0) {
                throw new BaseDaoException("This collabrum topic cannot be removed as it has messages posted");
            }
        }

        /**
         *  Get scalability datasource for colltopic partitioned on collabrumid
         */
        String sourceName = scalabilityManager.getWriteScalability(collabrumId);
        ds = scalabilityManager.getSource(sourceName);
        if (ds == null) {
            throw new BaseDaoException("ds null, addColTopic() " + sourceName + " userId = " + userId);
        }

        Connection conn = null;
        try {
            conn = ds.getConnection();
            deleteQuery.run(conn, tid, userId);
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e2) {
                throw new BaseDaoException("connection close exception for deleteColTopic() ", e2);
            }
            throw new BaseDaoException("error occured while deleting topic", e);
        }
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e2) {
            throw new BaseDaoException("connection close exception for deleteColTopic() ", e2);
        }

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

        fqn = cacheUtil.fqn(DbConstants.COLTOPICS);
        if (treeCache.exists(fqn, collabrumId)) {
            treeCache.remove(fqn, collabrumId);
        }
        fqn = cacheUtil.fqn(DbConstants.COLLABRUM);
        if (treeCache.exists(fqn, collabrumId)) {
            treeCache.remove(fqn, collabrumId);
        }
        /*
           fqn = cacheUtil.fqn(DbConstants.IS_TOPIC);
                if (treeCache.exists(fqn, collabrumId)) {
                   treeCache.remove(fqn, collabrumId);
           }
        */
    }

    /**
     * This method retrieves the topics based on the thread id, from slave(0)
     * @param tid - the thread id
     * @return <code>ColTopic</code> - collabrum topic bean
     **/
    public ColTopic getColTopic(String collabrumId, String tid) throws BaseDaoException {
        return getColTopic(collabrumId, tid, DbConstants.READ_FROM_SLAVE);
    }

    /**
     * This method retrieves the topic based on the thread id
     * @param tid - the thread id
     * @param accessFlag - the access flag which specifies the datasource (Master(1)/slave(0))
     * @return <code>ColTopic</code> - the collabrum topic bean
     */
    public ColTopic getColTopic(String collabrumId, String tid, int accessFlag) throws BaseDaoException {

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

        /** 
        * Jboss methods
         * fqn - full qualified name
         * set the login and photo - get the photo, in case it is deleted by the user
         * 
         */
        Fqn fqn = cacheUtil.fqn(DbConstants.COLTOPIC);
        StringBuffer buf = new StringBuffer(collabrumId);
        buf.append("-");
        buf.append(tid);
        String key = buf.toString();
        Object obj = treeCache.get(fqn, key);
        if (obj != null) {
            setLoginAndPhoto(((ColTopic) obj).getValue(DbConstants.OWNER_ID), (ColTopic) obj, accessFlag);
            return (ColTopic) obj;
        }

        /**
         *  Get scalability datasource for colltopic partitioned on collabrumid
         */
        String sourceName = null;
        if (accessFlag == 1) {
            sourceName = scalabilityManager.getWriteScalability(collabrumId);
        } else {
            sourceName = scalabilityManager.getReadScalability(collabrumId);
        }
        ds = scalabilityManager.getSource(sourceName);
        if (ds == null) {
            throw new BaseDaoException("ds null, getColTopic() " + sourceName + " collabrumId = " + collabrumId);
        }

        /**
         *    get the collabrum topics
         */
        List result = null;
        Object[] params = { (Object) tid };
        try {
            result = topicQuery.execute(params);
        } catch (Exception e) {
            throw new BaseDaoException("error occured while listing coltopic messages." + topicQuery.getSql(), e);
        }

        /**
         *  return null;
         */
        if ((result == null) || (result.size() <= 0)) {
            return null;
        }

        /**
         *  retrieve the ownername (first + last) for each ownerid of the collabrum topic
         *  retrieve hits
         */
        if (result.size() > 0) {
            ColTopic coltopic = (ColTopic) result.get(0);
            if (coltopic != null) {
                Hdlogin hdlogin = getLogin(coltopic.getValue(DbConstants.OWNER_ID));
                if (hdlogin == null) {
                    throw new BaseDaoException(
                            "hdlogin is null for coltopic tid = " + tid + " collabrumId = " + collabrumId);
                }
                coltopic.setObject(DbConstants.OWNER_INFO, hdlogin);
                setLoginAndPhoto(coltopic.getValue(DbConstants.OWNER_ID), coltopic, accessFlag);
                coltopic.setValue(DbConstants.NUM_POSTS, getColMessagesCount(tid, accessFlag));
            }
            treeCache.put(fqn, key, (ColTopic) result.get(0));
            return (ColTopic) result.get(0);
        }
        return null;
    }

    /**
     *  Allow users to view it, with session or without session
     *  topics are retrieved from datasource READ_FROM_SLAVE (0)
     *  Retrieves topics based on collabrumid.
     *  @param collabrumId - the collabrum id
     *  @param userId - the user id
     *  @param userLogin - the user login
     */
    public List getTopics(String collabrumId, String userId, String userLogin) throws BaseDaoException {
        return (getTopics(collabrumId, userId, userLogin, DbConstants.READ_FROM_SLAVE));
    }

    /**
     *  Allow users to view it, with session or without session
     *  topics are retrieved from datasource that is based on accessFlag
     *  Retrieves topics based on collabrumid.
     *  @param collabrumId - the collabrum id
     *  @param userId - the user id
     *  @param userLogin - the user login
     *  @param accessFlag - the access Flag, which indicates the datasource from which topics are retrieved.
     *    READ_FROM_MASTER (1), READ_FROM_SLAVE(0)
     *
     */
    public List getTopics(String collabrumId, String userId, String userLogin, int accessFlag)
            throws BaseDaoException {

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

        /** 
        * Jboss methods
         * fqn - full qualified name
         * check if the userpage already set in the cache
         * If it exists, return the userpage from the cache.
         */
        Fqn fqn = cacheUtil.fqn(DbConstants.COLTOPICS);
        Object obj = treeCache.get(fqn, collabrumId);
        if (obj != null) {
            return (List) obj;
        }

        /**
         *  Get scalability ds for topics, topics partitioned on collabrumid
         */
        String sourceName = scalabilityManager.getReadScalability(collabrumId);
        ds = scalabilityManager.getSource(sourceName);
        if (ds == null) {
            throw new BaseDaoException("ds null, getTopics() " + sourceName + " collabrumId = " + collabrumId
                    + " userLogin = " + userLogin);
        }

        List result = null;
        try {
            Object[] params = { (Object) GlobalConst.displayMsgSize, (Object) collabrumId };
            result = getTopicsQuery.execute(params);
        } catch (Exception e) {
            throw new BaseDaoException("error occured while listing coltopic messages.", e);
        }

        /**
         *  throw exception if coltopics is null
         */
        if (result == null) {
            throw new BaseDaoException("coltopics are null, for collabrumId " + collabrumId);
        }

        /**
         *  set ownername, login for each ownerid, set numreplies for each topic
         */
        Hdlogin hdlogin = null;
        ColTopic colTopic = null;
        for (int i = 0; i < result.size(); i++) {
            colTopic = (ColTopic) result.get(i);
            if (colTopic == null) {
                throw new BaseDaoException("coltopic result is null for collabrumId = " + collabrumId);
            }
            /**
             *  set ownername and login for each ownerid of the collabrum
             */
            hdlogin = getLogin(colTopic.getValue(DbConstants.OWNER_ID));
            if (hdlogin != null) {
                colTopic.setValue(DbConstants.LOGIN, hdlogin.getValue(DbConstants.LOGIN));
                colTopic.setValue(DbConstants.OWNER_NAME, hdlogin.getValue(DbConstants.OWNER_NAME));
            } else {
                throw new BaseDaoException("hdlogin is null for coltopic  collabrumId = " + collabrumId);
            }

            /**
             *  set num replies for topic or tid, should be done later
             */
            String tid = ((ColTopic) result.get(i)).getValue(DbConstants.TID);
            colTopic.setValue(DbConstants.NUM_POSTS, getColMessagesCount(tid, accessFlag));
        }

        if ((result != null) && (result.size() > 0)) {
            treeCache.put(fqn, collabrumId, result);
        }
        return result;
    }

    private String getColMessagesCount(String tid, int accessFlag) {

        if (RegexStrUtil.isNull(tid)) {
            throw new BaseDaoException("param is null");
        }

        /**
         *  Get scalability datasource for collmessages partitioned on tid
         */
        String sourceName = null;
        if (accessFlag == 1) {
            sourceName = scalabilityManager.getWriteScalability(tid);
        } else {
            sourceName = scalabilityManager.getReadScalability(tid);
        }

        ds = scalabilityManager.getSource(sourceName);
        if (ds == null) {
            throw new BaseDaoException("ds null, getColTopic() " + sourceName + " tid = " + tid);
        }

        try {
            Object[] params = { (Object) tid };
            List countList = msgCountQuery.execute(params);
            if ((countList != null) && (countList.size() > 0)) {
                if (((ColTopic) countList.get(0)) != null) {
                    return ((ColTopic) countList.get(0)).getValue("count(*)");
                }
            }
        } catch (Exception e) {
            throw new BaseDaoException("error in " + msgCountQuery.getSql() + " tid = " + tid, e);
        }
        return "0";
    }

    /**
     * @param collabrumId the id of the collabrum
     * @param userId the userId who does the query to get the userIds, need to be organizer.
     * @return boolean
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect
     */

    /*
       private boolean isOrganizer(String collabrumId,  String userId) {
           
    Fqn fqn = cacheUtil.fqn(DbConstants.ORGANIZER);
       StringBuffer sb = new StringBuffer(collabrumId);
       sb.append("-");
       sb.append(userId);
       String key = sb.toString();
    Object obj = treeCache.get(fqn, key);
    if (obj != null) {
          if ( ((Member)obj).getValue(DbConstants.IS_ORGANIZER) != null) {
     return ((Member)obj).getValue(DbConstants.IS_ORGANIZER).equals("1");
          }
    }
    String sourceName = scalabilityManager.getReadZeroScalability();
    ds = scalabilityManager.getSource(sourceName);
    if (ds == null) {
       throw new BaseDaoException("ds null, isOrganizer() " + sourceName + " collabrumId = " + collabrumId +  " userId = " + userId);
    }
        
    List result = null;
    try {
       Object[] params = {(Object)collabrumId, (Object)userId};
       result = adminExistsQuery.execute(params);
    } catch (Exception e) {
       throw new BaseDaoException("exception error in adminExistsQuery , " + adminExistsQuery.getSql() + " collabrumId = " + collabrumId + " userId = " + userId, e);
    }
           
       boolean isOrganizer = false;   
    if ((result != null) && (result.size() > 0)) {
          isOrganizer = true;
    } 
        
       Member organizer = (Member)eop.newObject(DbConstants.MEMBER);   
       if (isOrganizer) {
          organizer.setValue(DbConstants.IS_ORGANIZER, "1");
       } else {
          organizer.setValue(DbConstants.IS_ORGANIZER, "0");
       }
    treeCache.put(fqn, key, organizer);
       return isOrganizer;
       }
    */

    /**
     * freeze topic from coltopic matching this thread id
     * @param collabrumId - the collabrum id
     * @param tid - the tid
     * @param freeze - the freeze
     * @param userId - the user id
     * @param userLogin - the user login
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect
    */
    public void freezeTopic(String collabrumId, String tid, String freeze, String userId, String userLogin)
            throws BaseDaoException {

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

        /**
         *  both are checked: is the organizer or the admin
         */
        boolean isAdmin = true;
        if (!isOrganizer(collabrumId, userLogin, userId)) {
            isAdmin = false;
        }

        /**
         *  Get scalability ds for topics, topics partitioned on collabrumid
         **/
        String sourceName = scalabilityManager.getWriteScalability(collabrumId);
        ds = scalabilityManager.getSource(sourceName);
        if (ds == null) {
            throw new BaseDaoException("ds null, freezeTopic() " + sourceName);
        }

        Connection conn = null;
        try {
            conn = ds.getConnection();
            if (conn != null) {
                freezeQuery.run(conn, tid, freeze, userId, isAdmin);
            }
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e2) {
                throw new BaseDaoException("connection close exception for freezeTopic() collabrumId = "
                        + collabrumId + " tid =" + tid + " userLogin=" + userLogin, e2);
            }
            //throw new BaseDaoException("error occured while freezeTopic, collabrumId =" + collabrumId + " tid=" + tid  + " userLogin = " + userLogin, e);
        }
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e2) {
            throw new BaseDaoException("connection close exception for freezeTopic() collabrumId = " + collabrumId
                    + " tid =" + tid + " userLogin=" + userLogin, e2);
        }

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

        fqn = cacheUtil.fqn(DbConstants.COLTOPIC);
        StringBuffer buf = new StringBuffer(collabrumId);
        buf.append("-");
        buf.append(tid);
        String key = buf.toString();
        if (treeCache.exists(fqn, key)) {
            treeCache.remove(fqn, key);
        }
    }

    /**
     * screen topic from coltopic matching this thread id
     * @param collabrumId - the collabrumid 
     * @param tid - the thread id
     * @param screen - the screen
     * @param userId - the user id
     * @param userLogin - the user login
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect
    */
    public void screenTopic(String collabrumId, String tid, String screen, String userId, String userLogin)
            throws BaseDaoException {
        if (RegexStrUtil.isNull(collabrumId) || RegexStrUtil.isNull(tid) || RegexStrUtil.isNull(userLogin)
                || RegexStrUtil.isNull(screen) || RegexStrUtil.isNull(userId)) {
            throw new BaseDaoException("params are null");
        }

        /** 
         * both are checked: is the organizer or the admin
         */
        boolean isAdmin = true;
        if (!isOrganizer(collabrumId, userLogin, userId)) {
            isAdmin = false;
        }

        /**
         *  Get scalability ds for topics, topics partitioned on collabrumid
         **/
        String sourceName = scalabilityManager.getWriteScalability(collabrumId);
        ds = scalabilityManager.getSource(sourceName);
        if (ds == null) {
            throw new BaseDaoException("ds null, screenTopic() " + sourceName);
        }

        Connection conn = null;
        try {
            conn = ds.getConnection();
            screenQuery.run(conn, tid, screen, userId, isAdmin);
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e2) {
                throw new BaseDaoException("connection close exception for screenTopic() collabrumId = "
                        + collabrumId + " tid =" + tid + " userLogin=" + userLogin, e2);
            }
            throw new BaseDaoException("error occured while screenTopic, collabrumId =" + collabrumId + " tid="
                    + tid + " userLogin = " + userLogin, e);
        }
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e2) {
            throw new BaseDaoException("connection close exception for screenTopic() collabrumId = " + collabrumId
                    + " tid =" + tid + " userLogin=" + userLogin, e2);
        }

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

        fqn = cacheUtil.fqn(DbConstants.COLTOPIC);
        StringBuffer buf = new StringBuffer(collabrumId);
        buf.append("-");
        buf.append(tid);
        String key = buf.toString();
        if (treeCache.exists(fqn, key)) {
            treeCache.remove(fqn, key);
        }
    }

    /**
     * increments hits for the social networking blog based on the topic
     * @param tid tid
     * @param collabrumId collabrum id
     * @throws BaseDaoException when error occurs
     */
    public void incrementHits(String tid, String collabrumId) {
        /**
         *  Get scalability ds for topics, topics partitioned on collabrumid
         **/
        String sourceName = scalabilityManager.getWriteScalability(collabrumId);
        ds = scalabilityManager.getSource(sourceName);
        if (ds == null) {
            throw new BaseDaoException("ds null, incrementHits() " + sourceName);
        }
        Connection conn = null;
        try {
            conn = ds.getConnection();
            incrementHitsQuery.run(conn, tid);
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e2) {
                throw new BaseDaoException("connection close exception for incrementHits() collabrumId = "
                        + collabrumId + " tid =" + tid, e2);
            }
            throw new BaseDaoException(
                    "error occured while incrementHits, collabrumId =" + collabrumId + " tid=" + tid, e);
        }
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e2) {
            throw new BaseDaoException(
                    "connection close exception for incrementHits() collabrumId = " + collabrumId + " tid =" + tid,
                    e2);
        }
    }

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

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

        /**
         * get default photo stream blob 
         */
        Fqn fqn = cacheUtil.fqn(DbConstants.DEFAULT_PHOTO);
        Object obj = treeCache.get(fqn, ownerId);
        if (obj != null) {
            colTopic.setObject(DbConstants.PHOTO, (Photo) obj);
            Hdlogin hdlogin = getLogin(ownerId);
            if (hdlogin != null) {
                colTopic.setObject(DbConstants.OWNER_INFO, hdlogin);
            }
            return;
        }

        /**
         *  Get scalability datasource for carryon partitioned on loginid
         */
        String queryName = null;
        if (accessFlag == 1) {
            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
        */
        try {
            Object[] params = { (Object) ownerId };
            //List uResult = pQuery.execute(params);
            List uResult = defaultPhotoQuery.execute(params);
            if ((uResult != null) && (uResult.size() > 0)) {
                colTopic.setObject(DbConstants.PHOTO, (Photo) uResult.get(0));
                treeCache.put(fqn, ownerId, (Photo) uResult.get(0));
            }
        } catch (Exception e) {
            throw new BaseDaoException("error executing Query " + defaultPhotoQuery.getSql(), e);
        }

        Hdlogin hdlogin = getLogin(ownerId);
        if (hdlogin != null) {
            colTopic.setObject(DbConstants.OWNER_INFO, hdlogin);
        }
    }

    /**
     *  Setting all the properties required for coltopic dao
     */
    public void setJdbcSource(DataSource ds) {
        this.ds = ds;
    }

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

    public void setcoltopicaddQuery(ColTopicAddQuery daq) {
        this.addQuery = daq;
    }

    public void setcoltopicdeleteQuery(ColTopicDeleteQuery daq) {
        this.deleteQuery = daq;
    }

    public void setcoltopicfreezeQuery(ColTopicFreezeQuery daq) {
        this.freezeQuery = daq;
    }

    public void setcoltopicQuery(ColTopicQuery daq) {
        this.topicQuery = daq;
    }

    public void setcoltopicgetQuery(ColTopicGetQuery daq) {
        this.getTopicsQuery = daq;
    }

    /*
       public void setcollabrumadminexistsQuery(CollabrumAdminExistsQuery daq) {
          this.adminExistsQuery = daq;
       }
    */

    public void setmsgcountQuery(ColMessageCountQuery daq) {
        this.msgCountQuery = daq;
    }

    public void setmsgexistsQuery(ColMessageExistsQuery daq) {
        this.msgExistsQuery = daq;
    }

    public void setcoltopicscreenQuery(ColTopicScreenQuery daq) {
        this.screenQuery = daq;
    }

    public void setcoltopicincrementhitsQuery(ColTopicIncrementHitsQuery daq) {
        this.incrementHitsQuery = daq;
    }

    /*
       public void setEop(ExpiringObjectPool eop) {
    this.eop = eop;
       }
    */

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

    public void setcoltopicaddattributeQuery(ColTopicAddAttributeQuery daq) {
        this.addAttributeQuery = daq;
    }

}