Java tutorial
/** * Copyright (c) 2010 Red Hat, Inc. * * This software is licensed to you under the GNU General Public License, * version 2 (GPLv2). There is NO WARRANTY for this software, express or * implied, including the implied warranties of MERCHANTABILITY or FITNESS FOR A * PARTICULAR PURPOSE. You should have received a copy of GPLv2 along with this * software; if not, see http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. * * Red Hat trademarks are not licensed under GPLv2. No permission is granted to * use or replicate Red Hat trademarks that are incorporated in this software or * its documentation. */ package org.zanata.dao; import java.security.MessageDigest; import java.security.SecureRandom; import java.util.List; import javax.enterprise.inject.Specializes; import javax.inject.Inject; import javax.inject.Named; import org.apache.commons.lang.StringUtils; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.criterion.Restrictions; import org.zanata.model.HAccount; @Named("accountDAO") @javax.enterprise.context.Dependent public class AccountDAO extends AbstractDAOImpl<HAccount, Long> { public AccountDAO() { super(HAccount.class); } @Inject public AccountDAO(Session session) { super(HAccount.class, session); } public HAccount getByUsername(String username) { Criteria cr = getSession().createCriteria(HAccount.class); cr.add(Restrictions.eq("username", username)); cr.setCacheable(true).setComment("AccountDAO.getByUsername"); return (HAccount) cr.uniqueResult(); } public HAccount getByEmail(String email) { return (HAccount) getSession().createQuery("from HAccount acc where acc.person.email = :email") .setString("email", email).setComment("AccountDAO.getByEmail").uniqueResult(); } public HAccount getByUsernameAndEmail(String username, String email) { return (HAccount) getSession() .createQuery("from HAccount acc where acc.username = :username and acc.person.email = :email") .setString("username", username).setString("email", email) .setComment("AccountDAO.getByUsernameAndEmail").uniqueResult(); } public HAccount getByApiKey(String apikey) { return (HAccount) getSession().createCriteria(HAccount.class).add(Restrictions.eq("apiKey", apikey)) .uniqueResult(); } // @SuppressWarnings("unchecked") // public List<HAccount> searchQuery(String searchQuery) throws // ParseException // { // log.info("start searching {}", searchQuery); // TermQuery tq = new TermQuery(new Term("username", searchQuery)); // EdgeNGramTokenFilter // FullTextQuery fullTextQuery = ((FullTextEntityManager) // entityManager).createFullTextQuery(tq, HAccount.class); // return fullTextQuery.getResultList(); // } @SuppressWarnings("unchecked") // TODO: use hibernate search public List<HAccount> searchQuery(String searchQuery, int maxResults, int firstResult) { String userName = "%" + searchQuery + "%"; Query query = getSession().createQuery("from HAccount as a where lower(a.username) like lower(:username)"); query.setParameter("username", userName); query.setMaxResults(maxResults); query.setFirstResult(firstResult); query.setComment("AccountDAO.searchQuery/username"); return query.list(); } public List<String> getUserNames(String filter, int offset, int maxResults) { StringBuilder queryBuilder = new StringBuilder("select username from HAccount "); if (!StringUtils.isEmpty(filter)) { queryBuilder.append("where lower(username) like :filter"); } Query query = getSession().createQuery(queryBuilder.toString()); if (!StringUtils.isEmpty(filter)) { query.setParameter("filter", "%" + filter + "%"); } query.setMaxResults(maxResults); query.setFirstResult(offset); query.setCacheable(true); query.setComment("accountDAO.getUserNames"); return (List<String>) query.list(); } public int getUserCount(String filter) { StringBuilder queryBuilder = new StringBuilder("select count(*) from HAccount "); if (!StringUtils.isEmpty(filter)) { queryBuilder.append("where lower(username) like :filter"); } Query query = getSession().createQuery(queryBuilder.toString()); if (!StringUtils.isEmpty(filter)) { query.setParameter("filter", "%" + filter + "%"); } query.setCacheable(true); query.setComment("accountDAO.getUserCount"); Long totalCount = (Long) query.uniqueResult(); if (totalCount == null) { return 0; } return totalCount.intValue(); } public HAccount getByCredentialsId(String credentialsId) { Query query = getSession().createQuery("select c.account from HCredentials c where c.user = :id"); query.setParameter("id", credentialsId); query.setComment("AccountDAO.getByCredentialsId"); return (HAccount) query.uniqueResult(); } /** * Returns all accounts merged into the another one. * * @param mergedInto * The account into which all returned accounts were merged. * @return A list of accounts that in the past were merged into the given * account. */ public List<HAccount> getAllMergedAccounts(HAccount mergedInto) { Query query = getSession().createQuery("from HAccount as a where a.mergedInto = :mergedInto"); query.setParameter("mergedInto", mergedInto); query.setComment("AccountDAO.getAllMergedAccounts"); return (List<HAccount>) query.list(); } }