📄 userdao.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.system;
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.dao.calendar.CalendarDAO;
import org.opencustomer.application.db.vo.system.RoleVO;
import org.opencustomer.application.db.vo.system.UserVO;
import org.opencustomer.application.db.vo.system.UsergroupVO;
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 UserDAO extends UndeletableDAO<UserVO>
{
private static final Logger log = Logger.getLogger(UserDAO.class);
public final static int SORT_USERNAME = 1;
public final static int SORT_LAST_LOGIN = 2;
public final static int SORT_LOCKED = 3;
/**
* @throws DatabaseException
*/
public UserDAO() throws HibernateException
{
super();
}
protected Class getEntityClass()
{
return UserVO.class;
}
public void delete(UserVO entity, Integer userId) throws HibernateException
{
new CalendarDAO().delete(entity.getCalendar(), userId);
super.delete(entity, userId);
}
protected final String getSortString(Sort sort)
{
String orderField = "";
switch (sort.getField())
{
case SORT_USERNAME:
default:
orderField = "e.userName";
break;
case SORT_LAST_LOGIN:
orderField = "e.lastLogin";
break;
case SORT_LOCKED:
orderField = "e.locked";
break;
}
if (!sort.isAscending())
return orderField + " DESC";
else
return orderField + " ASC";
}
public UserVO getByUserName(String userName)
{
UserVO user = null;
try
{
StringBuilder hql = new StringBuilder();
hql.append(" FROM ").append(getEntityClass().getName()).append(" e ");
hql.append(" WHERE e.userName = :userName ");
hql.append(" AND e.deleted = 0 ");
user = (UserVO) this.session.createQuery(hql.toString()).setString("userName", userName).uniqueResult();
if (log.isDebugEnabled())
log.debug("found user: " + user);
}
catch (HibernateException e)
{
log.error("Could not find user by " + userName, e);
throw e;
}
return user;
}
public List<UserVO> getForRole(RoleVO role)
{
List<UserVO> list = new ArrayList<UserVO>();
try
{
StringBuilder hql = new StringBuilder();
hql.append(" FROM ").append(getEntityClass().getName()).append(" e ");
hql.append(" WHERE e.role = :role ");
hql.append(" AND e.deleted = 0 ");
Query query = this.session.createQuery(hql.toString());
query.setEntity("role", role);
list = toTypeSafeList(query.list());
if (log.isDebugEnabled())
log.debug("found role: " + role);
}
catch (HibernateException e)
{
log.error("Could not find role for " + role, e);
throw e;
}
return list;
}
public List<UserVO> getForUsergroup(UsergroupVO usergroup)
{
List<UserVO> list = new ArrayList<UserVO>();
try
{
StringBuilder hql = new StringBuilder();
hql.append(" SELECT e ");
hql.append(" FROM ").append(getEntityClass().getName()).append(" e ");
hql.append(" WHERE e.usergroups.id = :usergroupId ");
hql.append(" AND e.deleted = 0 ");
Query query = this.session.createQuery(hql.toString());
query.setInteger("usergroupId", usergroup.getId());
list = toTypeSafeList(query.list());
if (log.isDebugEnabled())
log.debug("found users: " + list.size());
}
catch (HibernateException e)
{
log.error("Could not find user for " + usergroup, e);
throw e;
}
return list;
}
public List<UserVO> getList(String userName, Date lastLoginStart, Date lastLoginEnd, Boolean locked, Sort sort, Page page)
{
String _userName = DatabaseUtility.toLower(DatabaseUtility.adjustWildcards(userName));
List<UserVO> list = new ArrayList<UserVO>();
try
{
StringBuilder hql = new StringBuilder();
hql.append(" FROM ").append(getEntityClass().getName()).append(" e ");
hql.append(" WHERE e.deleted = 0 ");
if (userName != null)
hql.append(" AND lower(e.userName) like :userName ");
if (lastLoginStart != null)
hql.append(" AND lower(e.lastLogin) >= :lastLoginStart ");
if (lastLoginEnd != null)
hql.append(" AND lower(e.lastLogin) <= :lastLoginEnd ");
if (locked != null)
if (locked)
hql.append(" AND locked = 1 ");
else
hql.append(" AND locked = 0 ");
if (sort != null)
hql.append(" order by " + getSortString(sort));
Query query = this.session.createQuery(hql.toString());
if (userName != null)
query.setString("userName", _userName);
if (lastLoginStart != null)
query.setDate("lastLoginStart", lastLoginStart);
if (lastLoginEnd != null)
query.setDate("lastLoginEnd", lastLoginEnd);
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() + " users");
}
catch (HibernateException e)
{
log.error("Could not find users", e);
throw e;
}
return list;
}
public int countList(String userName, Date lastLoginStart, Date lastLoginEnd, Boolean locked)
{
String _userName = DatabaseUtility.toLower(DatabaseUtility.adjustWildcards(userName));
Integer count = 0;
try
{
StringBuilder hql = new StringBuilder();
hql.append(" SELECT count(e.id) ");
hql.append(" FROM ").append(getEntityClass().getName()).append(" e ");
hql.append(" WHERE e.deleted = 0 ");
if (userName != null)
hql.append(" AND lower(e.userName) like :userName ");
if (lastLoginStart != null)
hql.append(" AND lower(e.lastLogin) >= :lastLoginStart ");
if (lastLoginEnd != null)
hql.append(" AND lower(e.lastLogin) <= :lastLoginEnd ");
if (locked != null)
if (locked)
hql.append(" AND locked = 1 ");
else
hql.append(" AND locked = 0 ");
Query query = this.session.createQuery(hql.toString());
if (userName != null)
query.setString("userName", _userName);
if (lastLoginStart != null)
query.setDate("lastLoginStart", lastLoginStart);
if (lastLoginEnd != null)
query.setDate("lastLoginEnd", lastLoginEnd);
count = (Integer) query.uniqueResult();
if (log.isDebugEnabled())
log.debug("count " + count + " users");
}
catch (HibernateException e)
{
log.error("Could not find users", e);
throw e;
}
return count;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -