1 package ch.busyboxes.agoo.dao.impl;
2
3 import java.util.Calendar;
4
5 import org.apache.log4j.Logger;
6 import org.hibernate.Criteria;
7 import org.hibernate.Session;
8 import org.hibernate.criterion.Order;
9 import org.hibernate.criterion.Restrictions;
10 import org.springframework.orm.hibernate3.HibernateCallback;
11 import org.springframework.transaction.annotation.Transactional;
12
13 import ch.busyboxes.agoo.dao.WatcherTaskDAO;
14 import ch.busyboxes.agoo.model.WatcherTask;
15 import ch.busyboxes.agoo.model.enumeration.TaskState;
16 import ch.busyboxes.agoo.model.enumeration.TaskType;
17
18
19
20
21
22
23 @Transactional
24 public class WatcherTaskDAOImpl extends AbstractHibernateDAO implements WatcherTaskDAO {
25
26
27 private Logger logger = Logger.getLogger(WatcherTaskDAOImpl.class);
28
29
30
31
32 @Override
33 public WatcherTask fetchNextPendingTask() {
34
35 return (WatcherTask)hibernateTemplate.execute(new HibernateCallback() {
36
37 public Object doInHibernate(Session session) {
38 Criteria criteria = session.createCriteria(WatcherTask.class);
39 criteria.add(Restrictions.eq("taskState", TaskState.PENDING));
40 criteria.addOrder(Order.asc("id"));
41 criteria.setMaxResults(1);
42 return criteria.uniqueResult();
43 }
44 });
45 }
46
47
48
49
50 @Override
51 public void createNewTask(TaskType taskType, Long targetId) {
52
53 if (logger.isDebugEnabled()) {
54 logger.debug("Creating a new task: " + taskType + ", with target id: " + targetId);
55 }
56
57 WatcherTask watcherTask = new WatcherTask();
58 watcherTask.setTaskState(TaskState.PENDING);
59 watcherTask.setCreated(Calendar.getInstance().getTime());
60 watcherTask.setTaskType(taskType);
61 watcherTask.setTargetId(targetId);
62
63 hibernateTemplate.save(watcherTask);
64 }
65
66
67
68
69 @Override
70 public void updateTask(WatcherTask watcherTask) {
71 hibernateTemplate.update(watcherTask);
72 }
73
74
75
76
77 @Override
78 public void removeTask(WatcherTask watcherTask) {
79 hibernateTemplate.delete(watcherTask);
80 }
81 }