01: package auction.dao.hibernate;
02:
03: import auction.dao.BookDAO;
04: import auction.model.Bid;
05: import auction.model.Book;
06: import auction.model.Category;
07: import auction.model.Language;
08: import auction.model.User;
09: import java.util.List;
10: import org.hibernate.Criteria;
11: import org.hibernate.Query;
12: import org.hibernate.criterion.CriteriaSpecification;
13: import org.hibernate.criterion.MatchMode;
14: import org.hibernate.criterion.Restrictions;
15:
16: public class BookDAOHibernate extends GenericHibernateDAO<Book, Long>
17: implements BookDAO {
18: public Bid getMaxBid(Long bookId) {
19: Query query = getSession().getNamedQuery("getBookMaxBid");
20: query.setParameter("bookid", bookId);
21: List resultList = query.list();
22: return (resultList.size() > 0 ? (Bid) resultList.get(0) : null);
23: }
24:
25: public List<Book> search(String title, String author,
26: String[] languageNames, Long[] categoryIds, User user,
27: boolean onlyActive) {
28:
29: Criteria crit = getSession().createCriteria(Book.class);
30: crit = crit
31: .setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
32:
33: if (onlyActive)
34: crit = crit.add(Restrictions.eq("active", true));
35:
36: if ((title != null) && (title.length() > 0))
37: crit = crit.add(Restrictions.like("title", title,
38: MatchMode.ANYWHERE));
39:
40: if ((author != null) && (author.length() > 0))
41: crit = crit.add(Restrictions.like("author", author,
42: MatchMode.ANYWHERE));
43:
44: if ((languageNames != null) && (languageNames.length > 0))
45: crit = crit.createAlias("language", "l").add(
46: Restrictions.in("l.name", languageNames));
47:
48: if ((categoryIds != null) && (categoryIds.length > 0))
49: crit = crit.createAlias("categories", "c").add(
50: Restrictions.in("c.id", categoryIds));
51:
52: if (user != null)
53: crit = crit.createCriteria("bids").createCriteria("bidder")
54: .add(Restrictions.eq("id", user.getId()));
55:
56: return crit.list();
57: }
58:
59: }
|