1
2 package com.pnpconsult.zeiterfassung.helper;
3
4 import java.util.Collection;
5 import java.util.List;
6
7 import jface.util.factory.FactoryException;
8 import net.sf.hibernate.Hibernate;
9 import net.sf.hibernate.HibernateException;
10 import net.sf.hibernate.type.Type;
11
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14
15 import com.pnpconsult.zeiterfassung.model.Bill;
16 import com.pnpconsult.zeiterfassung.model.UserEntry;
17
18 /***
19 * @author <a href="mailto:powerpete@users.sf.net">M. Petersen </a>
20 * @version $Id: BillManager.java,v 1.13 2004/08/02 20:36:08 powerpete Exp $
21 */
22 public class BillManager extends AbstractObjectManager2
23 {
24 private static final Log LOG = LogFactory.getLog(BillManager.class);
25
26 public Bill load(long id) throws HibernateException
27 {
28 return (Bill) load(Bill.class, new Long(id));
29 }
30
31 public List loadAll() throws HibernateException
32 {
33 return loadAll(Bill.class);
34 }
35
36 public List loadUserEntries(Bill bill) throws HibernateException
37 {
38 return findUserEntries(bill);
39 }
40
41 private List findUserEntries(Bill bill) throws HibernateException
42 {
43 String query = "as entry "
44 + "where entry.date >= ? "
45 + "and entry.date <= ? "
46 + "and entry.project.id = ? ";
47 Object[] objects = new Object[] {
48 bill.getStartDate(),
49 bill.getEndDate(),
50 new Long(bill.getProject().getId()) };
51 Type[] types = new Type[] {
52 Hibernate.DATE,
53 Hibernate.DATE,
54 Hibernate.LONG };
55 return find(UserEntry.class, query, objects, types);
56 }
57
58 public void delete(long id) throws HibernateException
59 {
60 super.delete(Bill.class, new Long(id));
61 }
62
63 public void save(Bill bill) throws HibernateException
64 {
65 Long key = (Long) super.save(bill);
66 bill.setId(key.longValue());
67 }
68
69 /***
70 * Returns a bill that is overlapped by the given bill object. Should be
71 * used to prevent creating duplicate bills with the same entries.
72 *
73 * @param bill
74 * The {@link Bill}object.
75 *
76 * @return One overlapping {@link Bill}object or <tt>null</tt> if no
77 * overlapping object exists.
78 *
79 * @throws HibernateException
80 * On Hibernate error.
81 * @throws FactoryException
82 * On configuration error.
83 */
84 public Bill loadSame(Bill bill) throws HibernateException
85 {
86 String query = "AS bill WHERE bill.startDate >= ? AND bill.endDate <= ? AND bill.project.id = ?";
87 Object[] objects = new Object[] {
88 bill.getStartDate(),
89 bill.getEndDate(),
90 new Long(bill.getProject().getId()) };
91 Type[] types = new Type[] {
92 Hibernate.DATE,
93 Hibernate.DATE,
94 Hibernate.LONG };
95 List bills = find(Bill.class, query, objects, types);
96 int size = bills.size();
97 if (size == 0) { return null; }
98 if (size > 1)
99 {
100 LOG.info("More than one overlapping bill found (" + size + ".");
101 }
102 return (Bill) bills.get(0);
103 }
104
105 public Collection loadNotPrinted() throws HibernateException
106 {
107 String query = "AS bill WHERE bill.printed = ?";
108 return sort(find(Bill.class, query, Boolean.FALSE, Hibernate.BOOLEAN));
109 }
110
111 public Collection loadPrinted() throws HibernateException
112 {
113 String query = "AS bill WHERE bill.printed = ?";
114 return sort(find(Bill.class, query, Boolean.TRUE, Hibernate.BOOLEAN));
115 }
116
117 public void update(Bill bill) throws HibernateException
118 {
119 super.update(bill);
120 }
121
122 public Collection loadPrintedAndNotArchived() throws HibernateException
123 {
124 String query = "AS bill WHERE bill.printed = ? AND bill.archived = ?";
125 Object[] objects = new Object[] {
126 Boolean.TRUE,
127 Boolean.FALSE };
128 Type[] types = new Type[] {
129 Hibernate.BOOLEAN,
130 Hibernate.BOOLEAN };
131 return sort(find(Bill.class, query, objects, types));
132 }
133
134 public void reset(long id) throws HibernateException
135 {
136
137 Bill bill = load(id);
138 bill.setPrinted(false);
139 bill.setArchived(false);
140 update(bill);
141 }
142 }