1
2
3
4 package ecar.dao;
5
6 import java.util.Calendar;
7 import java.util.Collections;
8 import java.util.Comparator;
9 import java.util.Date;
10 import java.util.List;
11
12 import javax.servlet.http.HttpServletRequest;
13
14 import org.hibernate.HibernateException;
15 import org.hibernate.Query;
16
17 import comum.database.Dao;
18
19 import ecar.exception.ECARException;
20 import ecar.pojo.RegControleAcessoRca;
21 import ecar.pojo.UsuarioUsu;
22
23
24
25
26 public class RegControleAcessoRcaDao extends Dao {
27
28
29
30
31 public RegControleAcessoRcaDao(HttpServletRequest request) {
32 super();
33 this.request = request;
34 }
35
36
37
38
39
40
41 public List listar(Long codUsuario, Date dataInicio, Date dataFim, String ordenacao) throws ECARException {
42 try {
43
44
45 Calendar calendarDataInicio = Calendar.getInstance();
46 Calendar calendarDataFim = Calendar.getInstance();
47
48 calendarDataInicio.setTime(dataInicio);
49 calendarDataInicio.set(Calendar.HOUR_OF_DAY, 0);
50 calendarDataInicio.set(Calendar.MINUTE, 0);
51 calendarDataInicio.set(Calendar.SECOND, 0);
52 calendarDataInicio.set(Calendar.MILLISECOND, 0);
53
54 calendarDataFim.setTime(dataFim);
55 calendarDataFim.set(Calendar.HOUR_OF_DAY, 23);
56 calendarDataFim.set(Calendar.MINUTE, 59);
57 calendarDataFim.set(Calendar.SECOND, 59);
58 calendarDataFim.set(Calendar.MILLISECOND, 999);
59
60 String query = " from RegControleAcessoRca rca";
61
62 query += " where";
63
64 if (codUsuario != null) {
65 query += " rca.usuarioUsu.codUsu = :codUsu and";
66 }
67 query += " rca.dataAcessoRca >= :dataInicio and rca.dataAcessoRca <= :dataFim";
68
69 query += " order by rca.dataAcessoRca desc";
70
71 Query q = session.createQuery(query);
72
73 if (codUsuario != null) {
74 q.setLong("codUsu", codUsuario.longValue());
75 }
76 q.setCalendar("dataInicio", calendarDataInicio);
77 q.setCalendar("dataFim", calendarDataFim);
78
79 List retorno = q.list();
80
81 if ("usuario".equals(ordenacao)) {
82
83 Collections.sort(retorno, new Comparator() {
84 public int compare(Object o1, Object o2) {
85 UsuarioUsu u1 = ((RegControleAcessoRca) o1).getUsuarioUsu();
86 UsuarioUsu u2 = ((RegControleAcessoRca) o2).getUsuarioUsu();
87
88 return u1.getNomeUsuSent().compareToIgnoreCase(u2.getNomeUsuSent());
89 }
90 });
91 }
92
93 return retorno;
94
95 } catch (HibernateException e) {
96 this.logger.error(e);
97 throw new ECARException(e);
98 }
99 }
100
101 }