📄 contactdao.java
字号:
/*******************************************************************************
* ***** BEGIN LICENSE BLOCK Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is the OpenCustomer CRM.
*
* The Initial Developer of the Original Code is Thomas Bader (Bader & Jene
* Software-Ingenieurb黵o). Portions created by the Initial Developer are
* Copyright (C) 2005 the Initial Developer. All Rights Reserved.
*
* Contributor(s): Thomas Bader <thomas.bader@bader-jene.de>
*
* ***** END LICENSE BLOCK *****
*/
package org.opencustomer.application.db.dao.crm;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.opencustomer.application.db.vo.crm.ContactVO;
import org.opencustomer.db.DatabaseUtility;
import org.opencustomer.db.dao.UndeletableDAO;
import org.opencustomer.web.util.Page;
import org.opencustomer.web.util.Sort;
/**
* @author chaenderl
*
*/
public final class ContactDAO extends UndeletableDAO<ContactVO>
{
private static final Logger log = Logger.getLogger(ContactDAO.class);
public final static int SORT_CONTACTTIMESTAMP = 1;
public final static int SORT_SUBJECT = 2;
public final static int SORT_CONTACTTYPE = 3;
public final static int SORT_BOUNDTYPE = 4;
/**
* @throws DatabaseException
*/
public ContactDAO() throws HibernateException
{
super();
}
protected Class getEntityClass()
{
return ContactVO.class;
}
protected final String getSortString(Sort sort)
{
String orderField = "";
switch (sort.getField())
{
case SORT_CONTACTTIMESTAMP:
default:
orderField = "e.contactTimestamp";
break;
case SORT_SUBJECT:
orderField = "e.subject";
break;
case SORT_CONTACTTYPE:
orderField = "e.contactType";
break;
case SORT_BOUNDTYPE:
orderField = "e.boundType";
break;
}
if (!sort.isAscending())
return orderField + " DESC";
else
return orderField + " ASC";
}
public List<ContactVO> getListForPerson(Integer personId, String subject, String contactType, String boundType, Date contactTimestampStart, Date contactTimestampEnd, String name, Sort sort, Page page)
{
String _subject = DatabaseUtility.toLower(DatabaseUtility.adjustWildcards(subject));
String _name = DatabaseUtility.toLower(DatabaseUtility.adjustWildcards(name));
List<ContactVO> list = new ArrayList<ContactVO>();
try
{
StringBuilder hql = new StringBuilder();
hql.append(" select distinct e ");
hql.append(" FROM ").append(getEntityClass().getName()).append(" e ");
hql.append(" left join e.personContacts pc ");
hql.append(" left join pc.person p ");
hql.append(" left join e.company c ");
hql.append(" WHERE e.deleted = 0 ");
hql.append(" AND e in ( ");
hql.append(" select e1 ");
hql.append(" from ").append(getEntityClass().getName()).append(" e1 ");
hql.append(" inner join e1.personContacts pc1 ");
hql.append(" inner join pc1.person p1 ");
hql.append(" where p1.id = :personId ");
hql.append(" ) ");
if (subject != null)
hql.append(" AND lower(e.subject) like :subject ");
if (contactType != null)
hql.append(" AND lower(e.contactType) like :contactType ");
if (boundType != null)
hql.append(" AND lower(e.boundType) like :boundType ");
if (contactTimestampStart != null)
hql.append(" AND lower(e.contactTimestamp) >= :contactTimestampStart ");
if (contactTimestampEnd != null)
hql.append(" AND lower(e.contactTimestamp) <= :contactTimestampEnd ");
if (name != null)
{
hql.append(" AND (lower(p.firstName) like :name ");
hql.append(" OR lower(p.lastName) like :name ");
hql.append(" OR lower(c.companyName) like :name ");
hql.append(" ) ");
}
if (sort != null)
hql.append(" order by " + getSortString(sort));
Query query = this.session.createQuery(hql.toString());
if (personId != null)
query.setInteger("personId", personId);
if (subject != null)
query.setString("subject", _subject);
if (contactType != null)
query.setString("contactType", contactType);
if (boundType != null)
query.setString("boundType", boundType);
if (contactTimestampStart != null)
query.setDate("contactTimestampStart", contactTimestampStart);
if (contactTimestampEnd != null)
query.setDate("contactTimestampEnd", contactTimestampEnd);
if (name != null)
query.setString("name", _name);
if (page != null)
{
query.setFirstResult(DatabaseUtility.getFirstResult(page));
query.setMaxResults(DatabaseUtility.getMaxResults(page));
}
list = toTypeSafeList(query.list());
if (log.isDebugEnabled())
log.debug("found " + list.size() + " contacts");
}
catch (HibernateException e)
{
log.error("Could not find contacts", e);
throw e;
}
return list;
}
public int countListForPerson(Integer personId, String subject, String contactType, String boundType, Date contactTimestampStart, Date contactTimestampEnd, String name)
{
String _subject = DatabaseUtility.toLower(DatabaseUtility.adjustWildcards(subject));
String _name = DatabaseUtility.toLower(DatabaseUtility.adjustWildcards(name));
Integer count = 0;
try
{
StringBuilder hql = new StringBuilder();
hql.append(" select count(distinct e.id) ");
hql.append(" FROM ").append(getEntityClass().getName()).append(" e ");
hql.append(" left join e.personContacts pc ");
hql.append(" left join pc.person p ");
hql.append(" left join e.company c ");
hql.append(" WHERE e.deleted = 0 ");
hql.append(" AND e in ( ");
hql.append(" select e1 ");
hql.append(" from ").append(getEntityClass().getName()).append(" e1 ");
hql.append(" inner join e1.personContacts pc1 ");
hql.append(" inner join pc1.person p1 ");
hql.append(" where p1.id = :personId ");
hql.append(" ) ");
if (subject != null)
hql.append(" AND lower(e.subject) like :subject ");
if (contactType != null)
hql.append(" AND lower(e.contactType) like :contactType ");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -