Package org.projectforge.plugins.crm

Source Code of org.projectforge.plugins.crm.PersonalContactDao

/////////////////////////////////////////////////////////////////////////////
//
// Project ProjectForge Community Edition
//         www.projectforge.org
//
// Copyright (C) 2001-2014 Kai Reinhard (k.reinhard@micromata.de)
//
// ProjectForge is dual-licensed.
//
// This community edition 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; version 3 of the License.
//
// This community edition 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 org.projectforge.plugins.crm;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.Validate;
import org.hibernate.LockMode;
import org.projectforge.access.AccessChecker;
import org.projectforge.access.AccessException;
import org.projectforge.core.BaseDao;
import org.projectforge.core.ModificationStatus;
import org.projectforge.user.PFUserContext;
import org.projectforge.user.PFUserDO;
import org.projectforge.user.UserDao;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
*
* @author Kai Reinhard (k.reinhard@micromata.de)
*
*/
public class PersonalContactDao extends HibernateDaoSupport
{
  private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(PersonalContactDao.class);

  private AccessChecker accessChecker;

  private UserDao userDao;

  public void setAccessChecker(final AccessChecker accessChecker)
  {
    this.accessChecker = accessChecker;
  }

  public void setUserDao(final UserDao userDao)
  {
    this.userDao = userDao;
  }

  /**
   * @param personalContact
   * @param ownerId If null, then task will be set to null;
   * @see BaseDao#getOrLoad(Integer)
   */
  public void setOwner(final PersonalContactDO personalContact, final Integer ownerId)
  {
    final PFUserDO user = userDao.getOrLoad(ownerId);
    personalContact.setOwner(user);
  }

  /**
   * @param obj
   * @return the generated identifier.
   */
  @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, isolation = Isolation.REPEATABLE_READ)
  public Serializable saveOrUpdate(final PersonalContactDO obj)
  {
    if (internalUpdate(obj) == true) {
      return obj.getId();
    }
    return internalSave(obj);
  }

  private void checkAccess(final PersonalContactDO obj)
  {
    Validate.notNull(obj);
    Validate.notNull(obj.getOwnerId());
    Validate.notNull(obj.getContactId());
    final PFUserDO owner = PFUserContext.getUser();
    if (owner == null || owner.getId().equals(obj.getOwnerId()) == false) {
      throw new AccessException("address.accessException.userIsNotOwnerOfPersonalAddress");
    }
    accessChecker.checkRestrictedOrDemoUser();
  }

  private Serializable internalSave(final PersonalContactDO obj)
  {
    if (isEmpty(obj) == true) {
      // No entry, so we do not need to save this entry.
      return null;
    }
    checkAccess(obj);
    obj.setCreated();
    obj.setLastUpdate();
    final Serializable id = getHibernateTemplate().save(obj);
    log.info("New object added (" + id + "): " + obj.toString());
    return id;
  }

  private boolean isEmpty(final PersonalContactDO obj)
  {
    return (obj.isFavoriteCard() == false)
        && (obj.isFavoriteBusinessPhone() == false)
        && (obj.isFavoriteMobilePhone() == false)
        && (obj.isFavoriteFax() == false)
        && (obj.isFavoritePrivatePhone() == false)
        && (obj.isFavoritePrivateMobilePhone() == false);
  }

  /**
   * @param obj
   * @return true, if already existing entry was updated, otherwise false (e. g. if no entry exists for update).
   */
  private boolean internalUpdate(final PersonalContactDO obj)
  {
    PersonalContactDO dbObj = null;
    if (obj.getId() != null) {
      dbObj = getHibernateTemplate().load(PersonalContactDO.class, obj.getId(), LockMode.PESSIMISTIC_WRITE);
    }
    if (dbObj == null) {
      dbObj = getByContactId(obj.getContactId());
    }
    if (dbObj == null) {
      return false;
    }
    checkAccess(dbObj);
    Validate.isTrue(ObjectUtils.equals(dbObj.getContactId(), obj.getContactId()));
    obj.setId(dbObj.getId());
    // Copy all values of modified user to database object.
    final ModificationStatus modified = dbObj.copyValuesFrom(obj, "owner", "address", "id");
    if (modified == ModificationStatus.MAJOR) {
      dbObj.setLastUpdate();
      log.info("Object updated: " + dbObj.toString());
    }
    return true;
  }

  /**
   * @return the PersonalContactDO entry assigned to the given address for the context user or null, if not exist.
   */
  @SuppressWarnings("unchecked")
  @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
  public PersonalContactDO getByContactId(final Integer contactId)
  {
    final PFUserDO owner = PFUserContext.getUser();
    Validate.notNull(owner);
    Validate.notNull(owner.getId());
    final List<PersonalContactDO> list = getHibernateTemplate().find(
        "from " + PersonalContactDO.class.getSimpleName() + " t where t.owner.id = ? and t.contact.id = ?",
        new Object[] { owner.getId(), contactId});
    if (list != null) {
      if (list.size() == 0) {
        return null;
      }
      if (list.size() > 1) {
        log.error("Multiple personal address book entries for same user ("
            + owner.getId()
            + " and same address ("
            + contactId
            + "). Should not occur?!");
      }
      return list.get(0);
    }
    return null;
  }

  /**
   * @return the list of all PersonalContactDO entries for the context user.
   */
  @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
  public List<PersonalContactDO> getList()
  {
    final PFUserDO owner = PFUserContext.getUser();
    Validate.notNull(owner);
    Validate.notNull(owner.getId());
    @SuppressWarnings("unchecked")
    final List<PersonalContactDO> list = getHibernateTemplate().find(
        "from "
            + PersonalContactDO.class.getSimpleName()
            + " t join fetch t.contact where t.owner.id=? and t.contact.deleted=false order by t.contact.name, t.contact.firstName",
            owner.getId());
    return list;
  }

  /**
   * @return the list of all address ids of personal address book for the context user (isFavorite() must be true).
   * @see PersonalContactDO#isFavorite()
   */
  @SuppressWarnings("unchecked")
  @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
  public Map<Integer, PersonalContactDO> getPersonalAddressByAddressId()
  {
    final PFUserDO owner = PFUserContext.getUser();
    Validate.notNull(owner);
    Validate.notNull(owner.getId());
    final List<PersonalContactDO> list = getHibernateTemplate().find(
        "from " + PersonalContactDO.class.getSimpleName() + " t where t.owner.id = ?", owner.getId());
    final Map<Integer, PersonalContactDO> result = new HashMap<Integer, PersonalContactDO>();
    for (final PersonalContactDO entry : list) {
      if (entry.isFavorite() == true) {
        result.put(entry.getContactId(), entry);
      }
    }
    return result;
  }

  /**
   * @return the list of all address entries for the context user (isFavorite() must be true).
   * @see PersonalContactDO#isFavorite()
   */
  @SuppressWarnings("unchecked")
  @Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
  public List<ContactDO> getMyAddresses()
  {
    final PFUserDO owner = PFUserContext.getUser();
    Validate.notNull(owner);
    Validate.notNull(owner.getId());
    final List<PersonalContactDO> list = getHibernateTemplate().find(
        "from "
            + PersonalContactDO.class.getSimpleName()
            + " t join fetch t.contact where t.owner.id = ? and t.contact.deleted = false order by t.contact.name, t.contact.firstName",
            owner.getId());
    final List<ContactDO> result = new ArrayList<ContactDO>();
    for (final PersonalContactDO entry : list) {
      if (entry.isFavorite() == true) {
        result.add(entry.getContact());
      }
    }
    return result;
  }
}
TOP

Related Classes of org.projectforge.plugins.crm.PersonalContactDao

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.