dao.DirectoryAbstractDao.java Source code

Java tutorial

Introduction

Here is the source code for dao.DirectoryAbstractDao.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.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.Fqn;
import util.*;

/**
 * It provides utility functions so that directory DAOs can use it.
 *
 * @author Smitha Gudur (smitha@redbasin.com)
 * @version $Revision: 1.1 $
 */
public abstract class DirectoryAbstractDao extends BaseDao {

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

    private volatile DirectoryChildrenExistQuery directorychildrenexistQuery;
    private volatile DirectoryWebsiteExistsQuery directoryWebsiteExistsQuery;
    private volatile DirAdminExistsQuery diradminexistsQuery;
    private volatile CollabrumAdminExistsQuery adminExistsQuery;

    protected volatile DiaryAdmin diaryAdmin;
    //protected volatile DirAdminQuery dirAdminQuery;
    protected volatile BasicQuery dirAdminQuery;

    private volatile DirectoryAddStreamBlobQuery addStreamBlobQuery;
    private volatile DirImageAddQuery addImageQuery;
    private volatile DirBlobTagsAddQuery addTagQuery;

    private volatile DirUserAddQuery addUserQuery;
    private volatile DirAdminAddQuery addAdminQuery;
    private volatile DirectoryBlockDeleteQuery deleteBlockQuery;

    /**
     * Checks if this directory has any children.
     * Checks in the dirtree, dircoll, dirwebsites
     * If children exist, return true otherwise false.
     * @param directoryId
     * @return boolean
     * @throws BaseDaoException
     */
    public boolean childrenExist(String directoryId) throws BaseDaoException {
        if (RegexStrUtil.isNull(directoryId)) {
            throw new BaseDaoException("directoryId params are null, childrenExist()");
        }

        /** Jboss methods
         * Fqn - full qualified name
         * Check if the directory already set in the cache
         * If directory exists, get leaf_node,
        * Return the value of childrenExist from directory that is cached
         */
        Fqn fqn = cacheUtil.fqn(DbConstants.DIRECTORY);
        Object obj = treeCache.get(fqn, directoryId);
        if (obj != null) {
            Directory dir = (Directory) obj;
            String leafNode = dir.getValue(DbConstants.IS_LEAFNODE);
            // children exist is true, if IS_LEAFNODE == 0
            if (!RegexStrUtil.isNull(leafNode) && leafNode.equals("0")) {
                return true;
            } else {
                return false;
            }
        }

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

        /**
         * Check if the children exist for this directoryId. 
              * dircoll, dirtree and dirwebsites.
         * check for blobs - or files. right now don't treat it as children 
        *  for dirblobs - until we support SAN delete, move etc.
         */
        List result = null;
        try {
            Object[] params = { (Object) directoryId, (Object) directoryId };
            result = directorychildrenexistQuery.execute(params);
        } catch (Exception e) {
            throw new BaseDaoException("error getting children for directory, "
                    + directorychildrenexistQuery.getSql() + " directoryId = " + directoryId, e);
        }

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

        List websiteResult = null;
        /**
         *  Get scalability datasource for dirwebsites - partitioned on directoryId
         */
        sourceName = scalabilityManager.getReadScalability(directoryId);
        ds = scalabilityManager.getSource(sourceName);
        if (ds == null) {
            throw new BaseDaoException("ds null, childrenExist() " + sourceName + " directoryId " + directoryId);
        }

        try {
            Object[] myparams = { (Object) directoryId };
            websiteResult = directoryWebsiteExistsQuery.execute(myparams);
        } catch (Exception e) {
            throw new BaseDaoException("error getting children for dirwebsites, "
                    + directoryWebsiteExistsQuery.getSql() + " directoryId = " + directoryId, e);
        }
        if ((websiteResult != null) && (websiteResult.size() > 0)) {
            return true;
        }

        return false;
    }

    /**
     * isAuthor() method
     * checks if this user is the author of this directory and returns the value of either true or false
     * @param myparams
     * @param adminResult
     * @return boolean
     * @throws BaseDaoException
     */
    public boolean isAuthor(String directoryId, String userId) {

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

        /**
         *  key = directoryid + userId
        *  remove dirauthor from the cache
         */
        StringBuffer sb = new StringBuffer(directoryId);
        sb.append("-");
        sb.append(userId);
        String key = sb.toString();
        Fqn fqn = cacheUtil.fqn(DbConstants.DIR_AUTHOR);
        Object obj = treeCache.get(fqn, key);
        if (obj != null) {
            return ((DirAuthor) obj).getValue(DbConstants.IS_AUTHOR).equals("1");
        }

        /**
         *  Get scalability datasource for diradmin, not partitioned
         */
        String sourceName = scalabilityManager.getReadZeroScalability();
        ds = scalabilityManager.getSource(sourceName);
        if (ds == null) {
            throw new BaseDaoException("ds null, isAuthor() " + sourceName + " userId = " + userId);
        }

        try {
            Object[] myparams = { (Object) directoryId, (Object) userId };
            List adminResult = diradminexistsQuery.execute(myparams);
            if ((adminResult != null) && (adminResult.size() > 0)) {
                ((DirAuthor) adminResult.get(0)).setValue(DbConstants.IS_AUTHOR, "1");
                treeCache.put(fqn, key, (DirAuthor) adminResult.get(0));
                return true;
            }
        } catch (BaseDaoException e) {
            throw new BaseDaoException("exception for isauthor, " + diradminexistsQuery.getSql(), e);
        }

        /**
         * put it in the jboss cache
         */
        DirAuthor dirAuthor = (DirAuthor) eop.newObject(DbConstants.DIR_AUTHOR);
        if (dirAuthor != null) {
            dirAuthor.setValue(DbConstants.IS_AUTHOR, "0");
            treeCache.put(fqn, key, dirAuthor);
        }
        return false;
    }

    /**
     * checks if this user is an organizer/moderator
     * @param collabrumId the id of the collabrum
     * @param userLogin the login
     * @param userId the userId 
     * @return boolean
     * @throws BaseDaoException If we have a problem interpreting the data or the data is missing or incorrect
     */
    public boolean isOrganizer(String collabrumId, String userLogin, String userId) {

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

        /** Jboss methods
         * fqn - full qualified name
         * check if the organizer already set in the cache
         * If it exists, return the organizer from the cache.
         */
        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");
            }
        }

        /**
              *  if admin, return organizer as true
              */
        if (!RegexStrUtil.isNull(userLogin)) {
            if (diaryAdmin.isDiaryAdmin(userLogin)) {
                Member organizer = (Member) eop.newObject(DbConstants.MEMBER);
                if (organizer != null) {
                    organizer.setValue(DbConstants.IS_ORGANIZER, "1");
                    treeCache.put(fqn, key, organizer);
                    return true;
                } else {
                    throw new BaseDaoException("could not allocate eop member");
                }
            }
        }

        /**
         *  Get scalability datasource, colladmin is not partitioned
         */
        String sourceName = scalabilityManager.getReadZeroScalability();
        ds = scalabilityManager.getSource(sourceName);
        if (ds == null) {
            sb.delete(0, sb.length());
            sb.append("ds is null for isOrganizer() in collabrumDaoDb ");
            sb.append(sourceName);
            sb.append("collabrumId = ");
            sb.append(collabrumId);
            sb.append(" userId ");
            sb.append(userId);
            throw new BaseDaoException(sb.toString());
        }

        /**
          *  check if this admin exists for this collabrum
         **/
        List result = null;
        try {
            Object[] params = { (Object) collabrumId, (Object) userId };
            result = adminExistsQuery.execute(params);
        } catch (Exception e) {
            sb.delete(0, sb.length());
            sb.append("exception error in adminExistsQuery , ");
            sb.append(adminExistsQuery.getSql());
            sb.append(" collabrumId = ");
            sb.append(collabrumId);
            sb.append(" userId = ");
            sb.append(userId);
            throw new BaseDaoException(sb.toString(), e);
        }

        if ((result != null) && (result.size() > 0)) {
            Member organizer = (Member) eop.newObject(DbConstants.MEMBER);
            organizer.setValue(DbConstants.IS_ORGANIZER, "1");
            treeCache.put(fqn, key, organizer);
            return true;
        }
        return false;
    }

    /**
     *  This methods lists all authors for a directory
     *  @param directoryId  - directoryId
     *  @return List - list all authors for this directory
     *  @throws BaseDaoException
     */
    public List getAuthors(String directoryId, int accessFlag) throws BaseDaoException {
        if (RegexStrUtil.isNull(directoryId)) {
            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.DIR_ADMINS);
        Object obj = treeCache.get(fqn, directoryId);
        if (obj != null) {
            return (List) obj;
        }

        /**
         *  Get scalability datasource for directory, diradmin, not partitioned 
         */
        String queryName = null;
        if (accessFlag == 1) {
            queryName = scalabilityManager.getWriteZeroScalability("diradminquery");
        } else {
            queryName = scalabilityManager.getReadZeroScalability("diradminquery");
        }

        dirAdminQuery = getQueryMapper().getQuery(queryName);
        /**
         * Get the list of users for this directory
         */
        List result = null;
        try {
            Object params[] = { (Object) directoryId };
            result = dirAdminQuery.execute(params);
        } catch (Exception e) {
            throw new BaseDaoException(
                    "error in listing users " + dirAdminQuery.getSql() + " directoryId = " + directoryId);
        }

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

    public void removeUsersFromDirAuthorsCache(String directoryId) {
        List directories = getAuthors(directoryId, DbConstants.READ_FROM_SLAVE);
        if (directories != null && directories.size() > 0) {
            for (int i = 0; i < directories.size(); i++) {
                if (directories.get(i) != null) {
                    String ownerid = (String) ((Directory) directories.get(i)).getValue(DbConstants.OWNER_ID);
                    if (ownerid != null) {
                        Fqn fqn = cacheUtil.fqn(DbConstants.DIR_USERS_SCOPE);
                        if (treeCache.exists(fqn, ownerid)) {
                            treeCache.remove(fqn, ownerid);
                        }
                    }
                }
            }
        }
    }

    /**
      *  Adds the user for this directory access list
      *  @param directoryId the directory id
      *  @param member  the member who is added as user to this directory
      *  @param userId  the user Login is used to check if this user has the permission to add user
      *  @param userLogin  the user login is used to check if this user has the permission to add user
      *   @param checkPermission flag to check if the user should be a diary admin to add author
      *   @return none 
      */
    public void addUser(String directoryId, String member, String userId, String userLogin, boolean checkPermission)
            throws BaseDaoException {

        if (checkPermission) {
            if (RegexStrUtil.isNull(directoryId) || RegexStrUtil.isNull(userLogin) || RegexStrUtil.isNull(member)
                    || RegexStrUtil.isNull(userId)) {
                throw new BaseDaoException("params are null");
            }
            /**
                  *  check if this user has permission to add user
                  */
            if (!diaryAdmin.isDiaryAdmin(userLogin) && !isAuthor(directoryId, userId)) {
                throw new BaseDaoException("User does not have permission to add user to directory, " + directoryId
                        + " userId = " + userId);
            }
        } else {
            if (RegexStrUtil.isNull(directoryId) || RegexStrUtil.isNull(member)) {
                throw new BaseDaoException(
                        "params are null, don't check permissions as the scope allows everyone to be the authors for this directory.");
            }
        }

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

        /**
        *  Add user to this directory
        */
        String memberId = null;
        try {

            Hdlogin hdlogin = getLoginid(member);
            memberId = hdlogin.getValue(DbConstants.LOGIN_ID);
            addUserQuery.run(directoryId, memberId);
        } catch (Exception e) {
            throw new BaseDaoException("error executing add user to dirallow table " + addUserQuery.getSql(), e);
        }

        /**
        *  member's list needs to be updated with the new query, 
        *  remove the old entries for this member
        */
        Fqn fqn = cacheUtil.fqn(DbConstants.USER_PAGE);
        if (treeCache.exists(fqn, member)) {
            treeCache.remove(fqn, member);
        }

        fqn = cacheUtil.fqn(DbConstants.DIRECTORY);
        if (treeCache.exists(fqn, directoryId)) {
            treeCache.remove(fqn, directoryId);
        }

        fqn = cacheUtil.fqn(DbConstants.USER_DIRS);
        if (treeCache.exists(fqn, member)) {
            treeCache.remove(fqn, member);
        }

        fqn = cacheUtil.fqn(DbConstants.DIR_OF_USERS);
        if (treeCache.exists(fqn, directoryId)) {
            treeCache.remove(fqn, directoryId);
        }
        removeUsersFromDirAuthorsCache(directoryId);
    }

    /**
     *  Adds the author for this directory.
     *  @param directoryId the directory id
     *  @param member  the member who is added as author to this directory
     *  @param userId  the user Login is used to check if this user has the permission to add authors
     *  @param userLogin  the user Login is used to check if this user has the permission to add authors
     *  @param checkPermission flag to check if the user should be a diary admin to add author
     */
    public void addAuthor(String directoryId, String member, String userId, String userLogin,
            boolean checkPermission) throws BaseDaoException {

        if (checkPermission) {
            if (RegexStrUtil.isNull(directoryId) || RegexStrUtil.isNull(member)) {
                throw new BaseDaoException("params are null");
            }
            /**
               *  check if this user has permission to add authors
               */
            if (!diaryAdmin.isDiaryAdmin(userLogin) && !isAuthor(directoryId, userId)) {
                throw new BaseDaoException("User does not have permission to add authors to directory, "
                        + directoryId + " userId = " + userId);
            }
        } else {
            logger.info("need not check permission");
            if (RegexStrUtil.isNull(directoryId) || RegexStrUtil.isNull(member)) {
                throw new BaseDaoException(
                        "params are null, dont check permission for the addAuthor() as directory scope is add all as authors.");
            }
        }
        logger.info("came here 0");

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

        /**
        *  Add author to this directory
        */
        String memberId = null;
        Connection conn = null;
        try {
            Hdlogin hdlogin = getLoginid(member);
            logger.info("hdlogin getLoginId for member " + member);
            if (hdlogin == null) {
                throw new BaseDaoException("Hdlogin for member is null, addAuthor(), member= " + member);
            } else {
                memberId = hdlogin.getValue(DbConstants.LOGIN_ID);
                logger.info("memberId " + memberId);
                if (!RegexStrUtil.isNull(memberId)) {
                    conn = ds.getConnection();
                    if (conn == null) {
                        logger.info("conn is null");
                        throw new BaseDaoException("conn is null");
                    }
                    conn.setAutoCommit(false);
                    logger.info("addAdminQuery started memberId = " + memberId + " directoryId = " + directoryId);
                    addAdminQuery.run(conn, directoryId, memberId);
                    logger.info("addAdminQuery completed memberId = " + memberId + " directoryId = " + directoryId);
                    logger.info(
                            "deleteBlockQuery started memberId = " + memberId + " directoryId = " + directoryId);
                    deleteBlockQuery.run(conn, directoryId, memberId);
                    logger.info(
                            "deleteBlockQuery completed memberId = " + memberId + " directoryId = " + directoryId);
                } else {
                    throw new BaseDaoException("memberId is null for member = " + member);
                }
            }
        } 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() error, addAuthor(), memberId" + memberId
                            + " error Msg=" + e2.getMessage(), e2);
                }
                throw new BaseDaoException(" rollback() exception, for addAuthor(),  memberId = " + memberId
                        + " error Msg=" + e1.getMessage(), e1);
            }
            logger.info("came here into exception e " + e.getMessage());
            throw new BaseDaoException(" addAuthor exception,  member = " + member + " errorMsg=" + e.getMessage(),
                    e);
        }

        logger.info("came here ");
        // connection commit
        try {
            conn.commit();
        } catch (Exception e3) {
            throw new BaseDaoException(" commit() exception, for addAuthor() userId = " + userId, e3);
        }
        try {
            if (conn != null) {
                conn.setAutoCommit(true);
                conn.close();
            }
        } catch (Exception e4) {
            throw new BaseDaoException(" conn.close() exception, for commit(), addAuthor() userId = " + userId, e4);
        }

        logger.info("came here 2");

        /**
         *  member's list needs to be updated with the new query, 
         *  remove the old entries for this member
         */
        Fqn fqn = cacheUtil.fqn(DbConstants.AUTHORS_LIST);
        if (treeCache.exists(fqn, member)) {
            treeCache.remove(fqn, member);
        }

        fqn = cacheUtil.fqn(DbConstants.AUTHORS_LIST);
        if (treeCache.exists(fqn, userLogin)) {
            treeCache.remove(fqn, userLogin);
        }

        fqn = cacheUtil.fqn(DbConstants.USER_PAGE);
        if (treeCache.exists(fqn, member)) {
            treeCache.remove(fqn, member);
        }

        fqn = cacheUtil.fqn(DbConstants.DIRECTORY);
        if (treeCache.exists(fqn, directoryId)) {
            treeCache.remove(fqn, directoryId);
        }

        fqn = cacheUtil.fqn(DbConstants.DIR_AUTHORS);
        if (treeCache.exists(fqn, directoryId)) {
            treeCache.remove(fqn, directoryId);
        }

        fqn = cacheUtil.fqn(DbConstants.DIR_ADMINS);
        if (treeCache.exists(fqn, directoryId)) {
            treeCache.remove(fqn, directoryId);
        }

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

        fqn = cacheUtil.fqn(DbConstants.AUTHORS_DIRECTORIES);
        if (treeCache.exists(fqn, member)) {
            treeCache.remove(fqn, member);
        }

        StringBuffer sb = new StringBuffer(directoryId);
        sb.append("-");
        sb.append(memberId);
        fqn = cacheUtil.fqn(DbConstants.DIR_AUTHOR);
        if (treeCache.exists(fqn, sb.toString())) {
            treeCache.remove(fqn, sb.toString());
        }

        fqn = cacheUtil.fqn(DbConstants.AUTHOR_BLOCKED_DIRS);
        if (treeCache.exists(fqn, memberId)) {
            treeCache.remove(fqn, memberId);
        }
    }

    /**
     * Add a new blob to a directory of blobs
     * Adds an empty tag entry into tag directory
     * Adds an entry in the images directory that is not split
     * (used for searching)
     * User permissions are checked before the user is allowed to add it.
     * If the user is the author of this directory or diaryadmin, allow this user to add it.
     * @param List  - files list
     * @param directoryId - directoryId
     * @param userLogin - userLogin
     * @param userId - userId
     * @throws BaseDaoException If we have a problem interpreting the data or 
     *  the data is missing or incorrect
    */
    public void addStreamBlobs(List files, String directoryId, String userLogin, String userId)
            throws BaseDaoException {

        // String usertags, 
        if ((files == null) || (RegexStrUtil.isNull(directoryId)) || (userLogin == null) || (userId == null)) {
            throw new BaseDaoException("params are null");
        }

        /**
         *  Get scalability datasource for dirblob - partitioned on directoryId
         */
        String sourceName = scalabilityManager.getWriteBlobScalability(directoryId);
        ds = scalabilityManager.getSource(sourceName);
        if (ds == null) {
            throw new BaseDaoException("ds null, addStreamBlob " + sourceName);
        }

        if (!isAuthor(directoryId, userId) && !diaryAdmin.isDiaryAdmin(userLogin)) {
            throw new BaseDaoException("user does not have permission to add streamblobs in directory "
                    + directoryId + " userId " + userId);
        }

        Connection conn = null;
        String entryId = null;
        for (int i = 0; i < files.size(); i++) {
            if (files.get(i) == null) {
                logger.info("files.get(" + i + ") is null");
                continue;
            }

            logger.info("photo file is not null, i=" + i);

            long bsize = 0;
            int blobtype = 0;
            int zoom = 0;

            String bsizeStr = ((Photo) files.get(i)).getValue(DbConstants.BSIZE);
            if (!RegexStrUtil.isNull(bsizeStr)) {
                bsize = new Long(bsizeStr).longValue();
            }
            String blobtypeStr = ((Photo) files.get(i)).getValue(DbConstants.BLOBTYPE);
            if (!RegexStrUtil.isNull(blobtypeStr)) {
                blobtype = new Integer(blobtypeStr).intValue();
            }
            String mtype = ((Photo) files.get(i)).getValue(DbConstants.MIMETYPE);
            String btitle = ((Photo) files.get(i)).getValue(DbConstants.BTITLE);
            byte[] blob = ((Photo) files.get(i)).getBlob();

            String zoomStr = ((Photo) files.get(i)).getValue(DbConstants.ZOOM);
            if (!RegexStrUtil.isNull(zoomStr)) {
                zoom = new Integer(zoomStr).intValue();
            }
            String caption = ((Photo) files.get(i)).getValue(DbConstants.CAPTION);
            if ((bsize <= 0) || RegexStrUtil.isNull(mtype) || RegexStrUtil.isNull(btitle)) {
                throw new BaseDaoException("params are null");
            }

            logger.info("bsize=" + bsize + " mtype=" + mtype + " btitle=" + btitle);

            if (RegexStrUtil.isNull(caption)) {
                caption = btitle;
            }

            try {
                conn = ds.getConnection();
                if (conn == null) {
                    throw new BaseDaoException("conn is null");
                }
                conn.setAutoCommit(false);

                if (WebUtil.isSanEnabled()) {
                    byte[] noBlobData = { ' ' };
                    logger.info("noblobdata " + directoryId + " userId=" + userId);
                    addStreamBlobQuery.run(conn, noBlobData, blobtype, mtype, btitle, bsize, zoom, directoryId,
                            userId, caption);
                } else {
                    logger.info("blobdata");
                    addStreamBlobQuery.run(conn, blob, blobtype, mtype, btitle, bsize, zoom, directoryId, userId,
                            caption);
                }
                logger.info("came here addTagQuery");

                addTagQuery.run(conn, directoryId, "", "LAST_INSERT_ID()");
                logger.info("completed addTagQuery");
                boolean convertEntryId = false;
                addImageQuery.run(conn, "", blobtype, mtype, btitle, bsize, zoom, directoryId, userId, caption,
                        convertEntryId);
                logger.info("completed addImageQuery");

            } catch (Exception e) {
                logger.info("errorMsg=" + e.getMessage());
                try {
                    conn.rollback();
                } catch (Exception e1) {
                    logger.info("errorMsg e1=" + e1.getMessage());
                    try {
                        if (conn != null) {
                            conn.setAutoCommit(true);
                            conn.close();
                        }
                    } catch (Exception e2) {
                        throw new BaseDaoException(
                                "conn.close() error, addStreamBlob()/addTagQuery/addImageQuery, userId=" + userId
                                        + " ErrorMsg=" + e2.getMessage(),
                                e2);
                    }
                    throw new BaseDaoException(
                            " rollback() exception, for addStreamBlob()/addTagQuery()/addImageQuery(),  userId = "
                                    + userId + " ErrorMsg=" + e1.getMessage(),
                            e1);
                }
                throw new BaseDaoException("addStreamBlob()/addTagQuery()/addImageQuery exception,  userId = "
                        + userId + " ErrorMsg=" + e.getMessage(), e);
            }

            /* 
            * connection commit
            */
            try {
                conn.commit();
            } catch (Exception e3) {
                throw new BaseDaoException(
                        " commit() exception, for addStreamBlob()/addImageQuery/addTagQuery, userId = " + userId
                                + " ErrorMsg = " + e3.getMessage(),
                        e3);
            }
            try {
                if (conn != null) {
                    conn.setAutoCommit(true);
                    conn.close();
                }
            } catch (Exception e4) {
                throw new BaseDaoException(
                        " conn.close() exception, for commit(), addStreamBlob()/addTagQuery/addImageQuery userId = "
                                + userId + " ErrorMsg = " + e4.getMessage(),
                        e4);
            }

            /**
             * get stream blobs for a directory from cache, if it exists
             */
            Fqn fqn = cacheUtil.fqn(DbConstants.DIRECTORY_STREAM_BLOBS);
            if (treeCache.exists(fqn, directoryId)) {
                treeCache.remove(fqn, directoryId);
            }

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

            fqn = cacheUtil.fqn(DbConstants.DIR_CAT);
            StringBuffer sb = new StringBuffer(directoryId);
            sb.append("-");
            sb.append(DbConstants.PHOTO_CATEGORY);
            if (treeCache.exists(fqn, sb.toString())) {
                treeCache.remove(fqn, sb.toString());
            }

            sb.delete(0, sb.length());
            sb.append(directoryId);
            sb.append("-");
            sb.append(DbConstants.FILE_CATEGORY);
            fqn = cacheUtil.fqn(DbConstants.DIR_CAT);
            if (treeCache.exists(fqn, sb.toString())) {
                treeCache.remove(fqn, sb.toString());
            }

            fqn = cacheUtil.fqn(DbConstants.DIRECTORY);
            if (treeCache.exists(fqn, directoryId)) {
                treeCache.remove(fqn, directoryId);
            }
        }
    }

    /**
     *  These properties are set by spring automatically at startup.
     */
    public void setdiradminexistsQuery(DirAdminExistsQuery daq) {
        this.diradminexistsQuery = daq;
    }

    public void setJdbcSource(DataSource ds) {
        this.ds = ds;
    }

    public void setScalabilityManager(ScalabilityManager sm) {
        this.scalabilityManager = sm;
    }

    public void setTreeCache(DiarynetTreeCache treeCache) {
        this.treeCache = treeCache;
    }

    public void setCacheUtil(CacheApi cacheUtil) {
        this.cacheUtil = cacheUtil;
    }

    public void setdirectorychildrenexistQuery(DirectoryChildrenExistQuery daq) {
        this.directorychildrenexistQuery = daq;
    }

    public void setdirectorywebsiteexistsQuery(DirectoryWebsiteExistsQuery daq) {
        this.directoryWebsiteExistsQuery = daq;
    }

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

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

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

    public void setaddstreamblobQuery(DirectoryAddStreamBlobQuery daq) {
        this.addStreamBlobQuery = daq;
    }

    public void setdirblobtagsaddQuery(DirBlobTagsAddQuery daq) {
        this.addTagQuery = daq;
    }

    public void setdiraddimageQuery(DirImageAddQuery daq) {
        this.addImageQuery = daq;
    }

    public void setdirectoryadduserQuery(DirUserAddQuery daq) {
        this.addUserQuery = daq;
    }

    public void setdiraddadminQuery(DirAdminAddQuery daq) {
        this.addAdminQuery = daq;
    }

    public void setdirectoryblockdeleteQuery(DirectoryBlockDeleteQuery daq) {
        this.deleteBlockQuery = daq;
    }

}