dao.FriendDaoDb.java Source code

Java tutorial

Introduction

Here is the source code for dao.FriendDaoDb.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.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import javax.sql.DataSource;
import model.Hdlogin;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import util.DbConstants;
import util.RegexStrUtil;

/**
  *  This object implements FriendDaoDb
  */
public class FriendDaoDb extends BaseDao implements FriendDao {

    protected final Log logger = LogFactory.getLog(getClass());
    //private volatile FriendCheckQuery friendQuery; 

    private volatile BasicQuery friendQuery;

    /**
     * Get the friends back as a List of Friend beans if they
     * happen to be friends of each other.
     *
     * @param friend1 
     * @param friend2
     * @exception BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect
     * @return HashSet
    */
    public HashSet getSecondFriends(String friend1, String friend2) throws BaseDaoException {

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

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

        // friend1
        Hdlogin hdlogin = getLoginid(friend1);
        if (hdlogin == null) {
            throw new BaseDaoException("hdlogin is null for friend1 " + friend1);
        }

        String friendid1 = hdlogin.getValue(DbConstants.LOGIN_ID);
        if (RegexStrUtil.isNull(friendid1)) {
            throw new BaseDaoException("friendid1 is null for friend1 " + friend1);
        }

        // friend2
        hdlogin = getLoginid(friend2);
        if (hdlogin == null) {
            throw new BaseDaoException("hdlogin is null for friend2 " + friend2);
        }
        String friendid2 = hdlogin.getValue(DbConstants.LOGIN_ID);
        if (RegexStrUtil.isNull(friendid2)) {
            throw new BaseDaoException("friendid2 is null for friend2 " + friend2);
        }

        /**
         *  Get scalability datasource for hdfriends - not partitioned
         **/
        String sourceName = scalabilityManager.getWriteZeroScalability();
        ds = scalabilityManager.getSource(sourceName);
        if (ds == null) {
            throw new BaseDaoException("ds null, getSecondFriends()" + sourceName);
        }

        // first query
        Object[] params = { (Object) friendid1, (Object) friendid1 };
        List result = null;
        try {
            result = friendQuery.execute(params);
        } catch (Exception e) {
            throw new BaseDaoException("error occured while executing dbquery " + friendQuery.getSql(), e);
        }

        if (result == null) {
            result = new ArrayList();
        }
        HashSet hs1 = new HashSet(result);

        // second query
        params[0] = friendid2;
        params[1] = friendid2;
        List result2 = friendQuery.execute(params);
        if (result2 == null) {
            result2 = new ArrayList();
        }
        HashSet hs2 = new HashSet(result2);

        // return the intersection of the two sets
        boolean f = hs1.retainAll(hs2);
        //treeCache.put(fqn, key, hs1);
        return hs1;
    }

    /**
      *  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 setfriendcheckQuery(BasicQuery ds) {
        this.friendQuery = ds;
    }
}