1 package ch.busyboxes.agoo.dao.impl;
2
3 import java.util.List;
4
5 import org.hibernate.Criteria;
6 import org.hibernate.Session;
7 import org.hibernate.criterion.Order;
8 import org.hibernate.criterion.Restrictions;
9 import org.springframework.orm.hibernate3.HibernateCallback;
10 import org.springframework.transaction.annotation.Transactional;
11
12 import ch.busyboxes.agoo.dao.WatchedFileDAO;
13 import ch.busyboxes.agoo.model.WatchedFile;
14 import ch.busyboxes.agoo.model.enumeration.WatchedFileState;
15
16
17
18
19
20
21 @Transactional
22 public class WatchedFileDAOImpl extends AbstractHibernateDAO implements WatchedFileDAO {
23
24
25
26
27 public void saveWatchedFile(WatchedFile file) {
28 if (file.getWatchedFileState() == null) {
29 file.setWatchedFileState(WatchedFileState.NEW);
30 }
31 hibernateTemplate.saveOrUpdate(file);
32 }
33
34
35
36
37 @SuppressWarnings("unchecked")
38 public long getTotalFilesWatched() {
39 List<Long> result = hibernateTemplate.find("select count(file) from WatchedFile file");
40 if (result.get(0) != null) {
41 return result.get(0).longValue();
42 } else {
43 return 0;
44 }
45 }
46
47
48
49
50 @Override
51 public WatchedFile getWatchedFileById(Long fileId) {
52 WatchedFile watchedFile = (WatchedFile) hibernateTemplate.get(WatchedFile.class, fileId);
53 return watchedFile;
54 }
55
56
57
58
59 @SuppressWarnings("unchecked")
60 @Override
61 public List<WatchedFile> getWatchedFilesByFolderPaged(final Long folderId,
62 final int filesPerPage, final int page) {
63
64 List<WatchedFile> result = (List<WatchedFile>)hibernateTemplate.execute(new HibernateCallback() {
65
66 public Object doInHibernate(Session session) {
67 Criteria criteria = session.createCriteria(WatchedFile.class);
68 criteria.add(Restrictions.eq("watchedFolder.id", folderId));
69 criteria.addOrder(Order.asc("filename"));
70 criteria.setFirstResult(filesPerPage * (page - 1));
71 criteria.setMaxResults(filesPerPage);
72 return (Object)criteria.list();
73 }
74 });
75
76 return result;
77 }
78 }