dao.SearchDaoDb.java Source code

Java tutorial

Introduction

Here is the source code for dao.SearchDaoDb.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.io.File;
import java.sql.Connection;
import java.util.*;
import javax.sql.DataSource;
import model.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.document.Document;
import org.apache.lucene.search.Hits;
import util.DbConstants;
import util.LuceneManager;
import util.RegexStrUtil;

/**
  * <B>SearchDaoDb</B> <BR>
  * This object implements SearchDao interface
  *
  * @author Smitha Gudur (smitha@redbasin.com)
  * @version $Revision: 1.1 $
  */
public class SearchDaoDb extends DirectoryAbstractDao implements SearchDao {

    protected final Log logger = LogFactory.getLog(getClass());

    protected volatile LuceneManager luceneManager;
    private volatile DirSearchQuery dirSearchQuery;
    private volatile DirBlobSearchQuery dirBlobSearchQuery;
    private volatile CollBlobSearchQuery collBlobSearchQuery;
    private volatile CollSearchQuery collSearchQuery;

    private volatile UserSearchQuery userSearchQuery;
    private volatile UserSearchBizAwareQuery bizUserSearchQuery;
    private volatile MykeywordsUserQuery myKeywordsQuery;

    private volatile PblogSearchQuery pblogSearchQuery;
    private volatile PblogSearchBizAwareQuery bizPblogSearchQuery;

    private volatile CarryonSearchQuery carryonSearchQuery;
    private volatile CarryonSearchBizAwareQuery bizCarryonSearchQuery;

    private volatile PblogTagWordsUpdateQuery pblogTagWordsUpdateQuery;
    private volatile TagAddQuery addTagQuery;
    private volatile SearchInTagsQuery searchInTagsQuery;
    private volatile TagIncrementHitsQuery incrementTagHitsQuery;
    private volatile TagsQuery getTagsQuery;
    private volatile DirectoryFileSearchQuery directoryFileSearchQuery;

    // private volatile TagSearchQuery tagInfoQuery;
    private volatile BasicQuery tagInfoQuery;
    private volatile VisitTrafficDailySearchTagCountQuery trafficInfoQuery;
    private int USER_PAGE = 1;
    private int BLOG = 2;
    private int PHOTO = 3;

    /**
     * This method searches in directories for all the strings in the searchText
     * @param searchText - search strings
     * @return HashSet - of results
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing
     * or incorrect
     */
    public HashSet searchDirectory(int accessFlag, String searchText) {
        /**
         *  Get scalability datasource for directory - not partitioned
         */
        String sourceName = null;
        if (accessFlag == DbConstants.READ_FROM_MASTER) {
            sourceName = scalabilityManager.getWriteZeroScalability();
        } else {
            sourceName = scalabilityManager.getReadZeroScalability();
        }

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

        Connection conn = null;
        HashSet result = null;
        try {
            conn = ds.getConnection();
            result = dirSearchQuery.run(conn, searchText);
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e1) {
                throw new BaseDaoException("error in conn.close(), searchDirectory() ", e1);
            }
            throw new BaseDaoException("error in result, searchDirectory() ", e);
        }

        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            throw new BaseDaoException("complete in conn.close(), searchDirectory() ", e);
        }
        return result;
    }

    /**
     * This method searches in directory blobs for all the strings in the searchText
     * @param searchText - search strings
     * @return HashSet - of results
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing
     * or incorrect
     */
    public HashSet searchDirectoryBlobs(int accessFlag, String searchText) {

        /**
         *  Get scalability datasource for directory - not partitioned
         */
        String sourceName = null;
        if (accessFlag == DbConstants.READ_FROM_MASTER) {
            sourceName = scalabilityManager.getWriteZeroScalability();
        } else {
            sourceName = scalabilityManager.getReadZeroScalability();
        }

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

        Connection conn = null;
        HashSet result = null;
        try {
            conn = ds.getConnection();
            result = dirBlobSearchQuery.run(conn, searchText);
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e1) {
                throw new BaseDaoException("error in conn.close(), searchDirectoryBlobs()", e1);
            }
            throw new BaseDaoException("error in result, searchDirectoryBlobs()", e);
        }

        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            throw new BaseDaoException("complete in conn.close(), searchDirectoryBlobs()", e);
        }
        return result;
    }

    /**
    * LuceneSearchBlobs - lucene search blobs
    * @param dirPath - directory path
    * @param searchText - search text
    * @return List - list of matches based on the text
    */
    public List luceneSearchBlobs(String dirPath, String searchText) {

        logger.info("searchText indexDir() = " + dirPath);
        dirPath = dirPath + "/";
        logger.info("searchText searchText() = " + searchText);
        // index the directory
        try {
            luceneManager.indexDir(new File(dirPath));
        } catch (Exception e) {
            throw new BaseDaoException(
                    "Exception in LuceneManager.indexDir(), dirPath = " + dirPath + " " + e.getMessage(), e);
        }

        // get the hits
        ArrayList arrayHits = null;
        if (!RegexStrUtil.isNull(searchText)) {
            // String modexpr = searchText.replaceAll("[[\\W]&&[\\S]]", "");
            String modexpr = RegexStrUtil.goodStr(searchText);
            StringTokenizer st = new StringTokenizer(modexpr, " ");
            while (st.hasMoreTokens()) {
                String token = st.nextToken();
                Hits hits = null;
                try {
                    logger.info("token search docs:" + token);
                    hits = luceneManager.searchDocs(token);
                } catch (Exception e) {
                    throw new BaseDaoException(
                            "Exception in LuceneManager searchDocs(token), token=" + token + e.getMessage(), e);
                }

                if (hits == null) {
                    logger.info("LuceneManager, hits is null");
                    return null;
                } else {
                    logger.info("hits.length() = " + hits.length());
                    arrayHits = new ArrayList();
                    for (int j = 0; j < hits.length(); j++) {
                        logger.info("hits.length() = " + j);
                        Document doc = null;
                        try {
                            doc = hits.doc(j);
                        } catch (Exception e) {
                            throw new BaseDaoException(
                                    "Exception in LuceneManager, hits.doc(j),j=" + j + " errMsg=" + e.getMessage(),
                                    e);
                        }

                        if (doc == null) {
                            logger.info("doc is null for j = " + j);
                            continue;
                        } else {
                            logger.info("doc = " + doc.toString());
                            if (!RegexStrUtil.isNull(doc.get("path"))) {
                                String fileName = null;
                                String docDir = null;
                                String dirName = null;
                                String str = doc.get("path");
                                // find the sanpath in the str and strip it off
                                int ind = -1;
                                if (str.startsWith(dirPath)) {
                                    ind = dirPath.length();
                                }
                                if (ind != -1) {
                                    docDir = str.substring(ind, str.length());
                                    logger.info("docDir = " + docDir.toString());
                                    if (!RegexStrUtil.isNull(docDir)) {
                                        // get filename, docDir (directories) separated with spaces
                                        int endInd = docDir.lastIndexOf(File.separator);
                                        if (endInd != -1) {
                                            if ((endInd + 1) <= docDir.length()) {
                                                fileName = docDir.substring(endInd + 1, docDir.length()).trim();
                                                logger.info("fileName = " + fileName.toString());
                                                docDir = docDir.substring(0, endInd).trim();
                                                endInd = docDir.lastIndexOf(File.separator);
                                                if (endInd != -1) {
                                                    if (endInd + 1 <= docDir.length()) {
                                                        dirName = docDir.substring(endInd + 1, docDir.length())
                                                                .trim();
                                                        docDir = docDir.substring(0, endInd).trim();
                                                    }
                                                }
                                                logger.info("fileName = " + fileName);
                                                logger.info("dirName = " + dirName);
                                                logger.info("docDir = " + docDir);
                                            }
                                            if (!RegexStrUtil.isNull(docDir)) {
                                                // write the query
                                                docDir = docDir.replaceAll(File.separator, " ");
                                                logger.info("docDir with spaces = " + docDir.toString());
                                            }
                                            // docDir can be null i.e dirPath can be null 
                                            HashSet hs = getDirectoryFile(docDir, fileName, dirName);
                                            // there should be only one match of a file for each sanpath
                                            if (hs != null && hs.size() == 1) {
                                                Iterator it1 = hs.iterator();
                                                while (it1.hasNext()) {
                                                    Directory dir = (Directory) it1.next();
                                                    if (dir == null)
                                                        continue;
                                                    logger.info(" found filematch = " + dir.toString());
                                                    arrayHits.add(dir);
                                                } // while
                                            } // if hs
                                        } // endInd != -1
                                    } // docDir
                                } // ind != -1
                            } // if
                        } // else
                    } // for         
                } // else
            } // while
        } // if
        return arrayHits;
    }

    /**
     * This method searches in collabrum blobs for all the strings in the searchText
     * @param searchText - search strings
     * @return HashSet - of results
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing
     * or incorrect
     */
    public HashSet searchCollabrumBlobs(int accessFlag, String searchText) {

        /**
         *  Get scalability datasource for collabrum - not partitioned
         */
        String sourceName = null;
        if (accessFlag == DbConstants.READ_FROM_MASTER) {
            sourceName = scalabilityManager.getWriteZeroScalability();
        } else {
            sourceName = scalabilityManager.getReadZeroScalability();
        }

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

        Connection conn = null;
        HashSet result = null;
        try {
            conn = ds.getConnection();
            result = collBlobSearchQuery.run(conn, searchText);
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e1) {
                throw new BaseDaoException("error in conn.close(), searchCollabrumBlobs()", e1);
            }
            throw new BaseDaoException("error in result, searchCollabrumBlobs()", e);
        }

        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            throw new BaseDaoException("complete in conn.close(), searchCollabrumBlobs()", e);
        }
        return result;
    }

    /**
     * This method searches in collabrum for all the strings in the searchText
     * @param searchText - search strings
     * @return HashSet - of results
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing
     * or incorrect
     */
    public HashSet searchCollabrum(int accessFlag, String searchText) {

        /**
         *  Get scalability datasource for collabrum - not partitioned
         */
        String sourceName = null;
        if (accessFlag == DbConstants.READ_FROM_MASTER) {
            sourceName = scalabilityManager.getWriteZeroScalability();
        } else {
            sourceName = scalabilityManager.getReadZeroScalability();
        }

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

        Connection conn = null;
        HashSet result = null;
        try {
            conn = ds.getConnection();
            result = collSearchQuery.run(conn, searchText);
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e1) {
                throw new BaseDaoException("error in conn.close(), searchCollabrum()", e1);
            }
            throw new BaseDaoException("error in result, searchCollabrum()", e);
        }

        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            throw new BaseDaoException("complete, in conn.close(), searchCollabrum()", e);
        }
        return result;
    }

    /**
     * This method searches in personal blogs for all the strings in the searchText
     * @param accessFlag - access flag for DS
     * @param searchText - search strings
     * @return HashSet - of results
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing
     * or incorrect
     */
    public HashSet searchPblogs(int accessFlag, String searchText) throws BaseDaoException {
        /**
         *  Get scalability datasource for pblog tags - not partitioned
         */
        String sourceName = null;
        if (accessFlag == DbConstants.READ_FROM_MASTER) {
            sourceName = scalabilityManager.getWriteZeroScalability();
        } else {
            sourceName = scalabilityManager.getReadZeroScalability();
        }

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

        Connection conn = null;
        HashSet result = null;
        try {
            conn = ds.getConnection();
            result = pblogSearchQuery.run(conn, searchText);
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e1) {
            }
            throw new BaseDaoException("error in result, searchPblogs() ", e);
        }

        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            throw new BaseDaoException("error in conn.close(), searchPblogs() ", e);
        }
        return result;
    }

    /**
     * This method is business intelligent and searches in personal blogs for all the strings in the searchText
     * @param accessFlag - access flag for DS
     * @param searchText - search strings
     * @param bid - bid
     * @param login - login who is accessing the search
     * @return HashSet - of results
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing
     * or incorrect
     */
    public HashSet bizSearchPblogs(int accessFlag, String searchText, String bid, String login)
            throws BaseDaoException {
        /**
         *  Get scalability datasource for pblogstags - not partitioned
         */
        String sourceName = null;
        if (accessFlag == DbConstants.READ_FROM_MASTER) {
            sourceName = scalabilityManager.getWriteZeroScalability();
        } else {
            sourceName = scalabilityManager.getReadZeroScalability();
        }

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

        Connection conn = null;
        HashSet resultSet = null;
        try {
            conn = ds.getConnection();
            resultSet = bizPblogSearchQuery.run(conn, searchText, bid);
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e1) {
            }
            throw new BaseDaoException("error in result, bizSearchPblogs() ", e);
        }

        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            throw new BaseDaoException("error in conn.close(), bizSearchPblogs() ", e);
        }
        return (cleanForBizAccess(resultSet, bid, login, BLOG));
    }

    /**
     * This method searches in users for all the strings in the searchText
     * @param accessFlag - access flag for DS
     * @param searchText - search strings
     * @return HashSet - of results
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing
     * or incorrect
     */
    public HashSet searchUsers(int accessFlag, String searchText) {
        /**
         *  Get scalability datasource for usertab - not partitioned
         */
        String sourceName = null;
        if (accessFlag == DbConstants.READ_FROM_MASTER) {
            sourceName = scalabilityManager.getWriteZeroScalability();
        } else {
            sourceName = scalabilityManager.getReadZeroScalability();
        }

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

        Connection conn = null;
        HashSet result = null;
        try {
            conn = ds.getConnection();
            result = userSearchQuery.run(conn, searchText);
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e1) {
                throw new BaseDaoException("error in conn.close(), searchUsers() ", e1);
            }
            throw new BaseDaoException("error in result, searchUsers() ", e);
        }

        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            throw new BaseDaoException("error in conn.close(), searchUsers() ", e);
        }
        return result;
    }

    /**
     * This method is business intelligent, and searches in users for all the strings in the searchText
     * @param accessFlag - accessFlag for DS
     * @param searchText - search strings
     * @param bid - bid
     * @param login - login who is accessing the search
     * @return HashSet - of results
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing
     * or incorrect
     */
    public HashSet bizSearchUsers(int accessFlag, String searchText, String bid, String login) {
        /**
         *  Get scalability datasource for usertab - not partitioned
         */
        String sourceName = null;
        if (accessFlag == DbConstants.READ_FROM_MASTER) {
            sourceName = scalabilityManager.getWriteZeroScalability();
        } else {
            sourceName = scalabilityManager.getReadZeroScalability();
        }

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

        Connection conn = null;
        HashSet resultSet = null;
        try {
            conn = ds.getConnection();
            resultSet = bizUserSearchQuery.run(conn, searchText, bid);
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e1) {
                throw new BaseDaoException("error in conn.close(), bizSearchUsers() ", e1);
            }
            throw new BaseDaoException("error in result, bizSearchUsers() ", e);
        }

        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            throw new BaseDaoException("error in conn.close(), bizSearchUsers() ", e);
        }
        return (cleanForBizAccess(resultSet, bid, login, USER_PAGE));
    }

    /**
     * This method is business intelligent search in carryontag for all the strings in the searchText.
     * @param accessFlag - access flag
     * @param searchText - search strings
     * @param bid -  bid
     * @param login - userlogin who is accessing the search 
     * @return HashSet - of results
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing
     * or incorrect
     */
    public HashSet bizSearchCarryon(int accessFlag, String searchText, String bid, String login)
            throws BaseDaoException {

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

        /**
         *  Get scalability datasource for carryontag - not partitioned
         */
        String sourceName = null;
        if (accessFlag == DbConstants.READ_FROM_MASTER) {
            sourceName = scalabilityManager.getWriteZeroScalability();
        } else {
            sourceName = scalabilityManager.getReadZeroScalability();
        }

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

        Connection conn = null;
        HashSet resultSet = null;
        try {
            conn = ds.getConnection();
            resultSet = bizCarryonSearchQuery.run(conn, searchText, bid);
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e1) {
                throw new BaseDaoException("error in conn.close(), bizSearchCarryon() ", e1);
            }
            throw new BaseDaoException("error in resultSet, bizSearchCarryon()", e);
        }

        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            throw new BaseDaoException("error in conn.close(), bizSearchCarryon() ", e);
        }
        return (cleanForBizAccess(resultSet, bid, login, PHOTO));
    }

    /**
     * This method searches in carryontag for all the strings in the searchText
     * @param accessFlag - access flag for DS
     * @param searchText - search strings
     * @return HashSet - of results
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing
     * or incorrect
     */
    public HashSet searchCarryon(int accessFlag, String searchText) {
        /**
         *  Get scalability datasource for carryontag - not partitioned
         */
        String sourceName = null;
        if (accessFlag == DbConstants.READ_FROM_MASTER) {
            sourceName = scalabilityManager.getWriteZeroScalability();
        } else {
            sourceName = scalabilityManager.getReadZeroScalability();
        }

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

        Connection conn = null;
        HashSet result = null;
        try {
            conn = ds.getConnection();
            result = carryonSearchQuery.run(conn, searchText);
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e1) {
                throw new BaseDaoException("error in conn.close(), searchCarryon() ", e1);
            }
            throw new BaseDaoException("error in result, searchCarryon()", e);
        }

        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            throw new BaseDaoException("error in conn.close(), searchCarryon() ", e);
        }
        return result;
    }

    /**
     * addTags - adds the tags entered by users in search text
     * @param searchText - searchText 
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing
     * or incorrect
     */
    public void addTags(String searchText) {

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

        List tagList = RegexStrUtil.getWords(searchText);
        if (tagList == null) {
            throw new BaseDaoException("tags are null");
        }

        Connection conn = null;
        List searchResult = null;
        try {
            conn = ds.getConnection();
            searchResult = searchInTagsQuery.run(conn, searchText);
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e1) {
                throw new BaseDaoException("error in conn.close(), searchInTagsQuery() ", e1);
            }
            throw new BaseDaoException("error in result, searchInTagsQuery()", e);
        }

        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            throw new BaseDaoException("error in conn.close(), searchInTagsQuery() ", e);
        }

        /** 
              * none found, add all tags in the DB
         */
        if (searchResult == null) {
            for (int i = 0; i < tagList.size(); i++) {
                try {
                    //Object params[] = {(Object)(String)tagList.get(i)};
                    addTagQuery.run((String) tagList.get(i));
                } catch (Exception e) {
                    throw new BaseDaoException("error in" + addTagQuery.getSql(), e);
                }
            }
        }

        if (searchResult != null) {
            /**
             * get the tags that are found in the DB and increment the hits
             */
            for (int i = 0; i < searchResult.size(); i++) {
                try {
                    incrementTagHitsQuery
                            .run((String) ((Yourkeywords) searchResult.get(i)).getValue(DbConstants.ENTRYID));
                } catch (Exception e) {
                    throw new BaseDaoException("error in incrementTagHitsQuery tag= "
                            + ((Yourkeywords) searchResult.get(i)).getValue(DbConstants.ENTRYID), e);
                }
            }

            /* get the tags that don't exist in the database  */
            HashSet foundList = new HashSet();
            for (int i = 0; i < searchResult.size(); i++) {
                if (searchResult.get(i) != null) {
                    foundList.add(((Yourkeywords) searchResult.get(i)).getValue(DbConstants.TAG));
                }
            }

            /**
             * get the tags and add the tags that are not found in the searchResult
             */
            HashSet tagSet = new HashSet(tagList);
            boolean f = tagSet.removeAll(foundList);
            if (tagSet != null) {
                Iterator it1 = tagSet.iterator();
                while (it1.hasNext()) {
                    try {
                        addTagQuery.run((String) it1.next());
                    } catch (Exception e) {
                        throw new BaseDaoException("error in" + addTagQuery.getSql(), e);
                    }
                }
            }
        }
    }

    /**
     * getSearchTags() returns the list of top tags in terms of hits
     * @param numTags - number of tags to return (top 10, top 100 etc)
     * @return List - tag results
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing
     * or incorrect
     */
    public List getSearchTags(int numTags) {

        List tags = null;
        Connection conn = null;
        try {
            conn = ds.getConnection();
            tags = getTagsQuery.run(conn, numTags);
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e1) {
                throw new BaseDaoException("getSearchTags(), conn.close() error", e1);
            }
            throw new BaseDaoException("getSearchTags() error, getTagsQuery", e);
        }

        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            throw new BaseDaoException("getSearchTags() error,  conn.close()", e);
        }
        return tags;
    }

    /**
     * cleanForBizAccess  - looks for business access for the accessibility
     * @param resultSet - resultSet of objects
     * @param bid - bid to which the login belongs
     * @param login - login who is searching for info
     * @param modelType - type of the model (Userpage/Blog/Photo)
     * @return HashSet - cleanedup for biz access resultSet
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing
     * or incorrect
     */
    private HashSet cleanForBizAccess(HashSet resultSet, String bid, String login, int modelType) {

        if (RegexStrUtil.isNull(bid) || RegexStrUtil.isNull(login) || resultSet == null) {
            throw new BaseDaoException("params are null");
        }

        logger.info("bid = " + bid + " login=" + login);
        logger.info("resultSet = " + resultSet.toString());

        /**
         * don't create a new set - in future, fix this
         */
        HashSet bizHashSet = new HashSet();
        Iterator it1 = resultSet.iterator();
        while (it1.hasNext()) {
            Object obj = it1.next();
            String mbid, bsearch;
            bsearch = mbid = null;
            if (modelType == USER_PAGE) {
                mbid = ((Userpage) obj).getValue(DbConstants.BID);
                bsearch = ((Userpage) obj).getValue(DbConstants.BSEARCH);
            }
            if (modelType == BLOG) {
                mbid = ((Blog) obj).getValue(DbConstants.BID);
                bsearch = ((Blog) obj).getValue(DbConstants.BSEARCH);
            }

            if (modelType == PHOTO) {
                mbid = ((Photo) obj).getValue(DbConstants.BID);
                bsearch = ((Photo) obj).getValue(DbConstants.BSEARCH);
            }

            if ((bsearch != null && bsearch.equals(DbConstants.BSEARCH_ALLOW))
                    || (mbid != null && mbid.equals(bid))) {
                /** cannot access this members information 
                 * remove it from the list
                 */
                bizHashSet.add(obj);
            }
        }
        logger.info("bizHashSet = " + bizHashSet.toString());
        return bizHashSet;
    }

    /**
     * getMyKeywords() returns the list of tags that match with the loginids 
     * @param searchUsersList - list of userids 
     * @return HashSet - result
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing
     * or incorrect
     */
    public HashSet getMyKeywords(List searchUsersList) {

        if (searchUsersList == null) {
            throw new BaseDaoException("params are null");
        }

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < searchUsersList.size(); i++) {
            sb.append(((Userpage) searchUsersList.get(i)).getValue(DbConstants.LOGIN_ID));
            sb.append(" ");
        }

        HashSet tags = null;
        Connection conn = null;
        try {
            conn = ds.getConnection();
            tags = myKeywordsQuery.run(conn, (String) sb.toString());
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e1) {
                throw new BaseDaoException("getMykeywords(), conn.close() error", e1);
            }
            throw new BaseDaoException("getMykeywords() error, getTagsQuery", e);
        }

        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            throw new BaseDaoException("getMykeywords() error,  conn.close()", e);
        }
        return tags;
    }

    /**
     * This method provides information related with the tag
     * @param accessFlag - access flag for DS
     * @param searchText -  search the text
     * @return Yourkeywords 
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing
     */
    public Yourkeywords getTagInfo(int accessFlag, String searchText) {
        if (searchText == null) {
            throw new BaseDaoException("params are null");
        }

        String queryName = null;
        if (accessFlag == 1) {
            queryName = scalabilityManager.getWriteZeroScalability("taginfoquery");
        } else {
            queryName = scalabilityManager.getReadZeroScalability("taginfoquery");
        }
        tagInfoQuery = getQueryMapper().getQuery(queryName);
        Object[] params = { searchText };
        List uResult = null;
        try {
            uResult = tagInfoQuery.execute(params);
        } catch (Exception e) {
            throw new BaseDaoException("error executing Query", e);
        }
        if (uResult != null && uResult.size() > 0) {
            return (Yourkeywords) uResult.get(0);
        } else {
            return null;
        }
    }

    /**
     * This method searches in visitraffic for the strings in the searchText
     * @param accessFlag - access flag for DS
     * @param searchText - search strings
     * @return Yourkeywords - of results
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing
     * or incorrect
     */
    public Yourkeywords getVisitTrafficInfo(int accessFlag, String searchText) {
        /**
         *  Get scalability datasource for visittraffic - not partitioned
         */
        String sourceName = null;
        if (accessFlag == DbConstants.READ_FROM_MASTER) {
            sourceName = scalabilityManager.getWriteZeroScalability();
        } else {
            sourceName = scalabilityManager.getReadZeroScalability();
        }

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

        Connection conn = null;
        List result = null;
        try {
            conn = ds.getConnection();
            result = trafficInfoQuery.run(conn, searchText);
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e1) {
                throw new BaseDaoException("error in conn.close(), getTrafficInfo() ", e1);
            }
            throw new BaseDaoException("error in result, getTrafficInfo()", e);
        }

        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            throw new BaseDaoException("error in conn.close(), getTrafficInfo() ", e);
        }

        if (result != null && result.size() > 0) {
            return ((Yourkeywords) result.get(0));
        } else {
            return null;
        }
    }

    public HashSet getDirectoryFile(String searchText, String fileName, String dirName) {
        /**
         *  Get scalability datasource for directory - not partitioned
         */
        String sourceName = scalabilityManager.getReadZeroScalability();

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

        Connection conn = null;
        HashSet result = null;
        try {
            conn = ds.getConnection();
            result = directoryFileSearchQuery.run(conn, searchText, fileName, dirName);
        } catch (Exception e) {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e1) {
                throw new BaseDaoException("error in conn.close(), DirectoryFileSearchQuery() ", e1);
            }
            throw new BaseDaoException("error in result, DirectoryFileSearchQuery() ", e);
        }

        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            throw new BaseDaoException("complete in conn.close(), DirectoryFileSearchQuery() ", e);
        }
        return result;
    }

    /**
     *  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 setdirsearchQuery(DirSearchQuery daq) {
        this.dirSearchQuery = daq;
    }

    public void setcollsearchQuery(CollSearchQuery daq) {
        this.collSearchQuery = daq;
    }

    public void setcollblobsearchQuery(CollBlobSearchQuery daq) {
        this.collBlobSearchQuery = daq;
    }

    public void setdirblobsearchQuery(DirBlobSearchQuery daq) {
        this.dirBlobSearchQuery = daq;
    }

    public void setusersearchQuery(UserSearchQuery daq) {
        this.userSearchQuery = daq;
    }

    public void setmykeywordsuserQuery(MykeywordsUserQuery daq) {
        this.myKeywordsQuery = daq;
    }

    public void setbizusersearchQuery(UserSearchBizAwareQuery daq) {
        this.bizUserSearchQuery = daq;
    }

    public void setpblogsearchQuery(PblogSearchQuery daq) {
        this.pblogSearchQuery = daq;
    }

    public void setbizpblogsearchQuery(PblogSearchBizAwareQuery daq) {
        this.bizPblogSearchQuery = daq;
    }

    public void setcarryonsearchQuery(CarryonSearchQuery daq) {
        this.carryonSearchQuery = daq;
    }

    public void setbizcarryonsearchQuery(CarryonSearchBizAwareQuery daq) {
        this.bizCarryonSearchQuery = daq;
    }

    /** Tags **/
    public void settagsQuery(TagsQuery daq) {
        this.getTagsQuery = daq;
    }

    public void setsearchintagsQuery(SearchInTagsQuery daq) {
        this.searchInTagsQuery = daq;
    }

    public void settagaddQuery(TagAddQuery daq) {
        this.addTagQuery = daq;
    }

    public void settagincrementhitsQuery(TagIncrementHitsQuery daq) {
        this.incrementTagHitsQuery = daq;
    }

    public void setvisittrafficdailysearchtagcountQuery(VisitTrafficDailySearchTagCountQuery daq) {
        this.trafficInfoQuery = daq;
    }

    public void setluceneManager(LuceneManager lm) {
        this.luceneManager = lm;
    }

    public void setdirectoryfilesearchQuery(DirectoryFileSearchQuery daq) {
        this.directoryFileSearchQuery = daq;
    }

    /*
        public void setpblogtagwordsupdateQuery(PblogTagWordsUpdateQuery daq) {
       this.pblogTagWordsUpdateQuery = daq;
        }
        
        public void setcolltagupdateQuery(CollTagUpdateQuery daq) {
       this.collTagUpdateQuery = daq;
        }
        
        public void setusertagupdateQuery(UserTagUpdateQuery daq) {
       this.userTagUpdateQuery = daq;
        }
    */

    /*
        public void setcarryontagupdateQuery(CarryonTagUpdateQuery daq) {
       this.carryonTagUpdateQuery = daq;
        }
        
        public void setdirtagupdateQuery(DirTagUpdateQuery daq) {
       this.dirTagUpdateQuery = daq;
        }
        
        public void setdirblobtagupdateQuery(DirBlobTagUpdateQuery daq) {
       this.dirBlobTagUpdateQuery = daq;
        }
        
        public void setcollblobtagupdateQuery(CollBlobTagUpdateQuery daq) {
       this.collBlobTagUpdateQuery = daq;
        }
    */

}