1
2
3
4 package ecar.dao;
5
6 import java.util.ArrayList;
7 import java.util.Calendar;
8 import java.util.Collection;
9 import java.util.Iterator;
10 import java.util.List;
11
12 import javax.servlet.http.HttpServletRequest;
13
14 import org.hibernate.HibernateException;
15 import org.hibernate.Query;
16 import org.hibernate.Transaction;
17
18 import comum.database.Dao;
19 import comum.util.Data;
20 import comum.util.Pagina;
21
22 import ecar.exception.ECARException;
23 import ecar.pojo.AgendaAge;
24 import ecar.pojo.AgendaOcorrenciaAgeo;
25 import ecar.pojo.DestaqueItemRelDtqir;
26 import ecar.pojo.DestaqueSubAreaDtqsa;
27
28
29
30
31 public class AgendaOcorrenciaDao extends Dao {
32
33
34
35
36 public AgendaOcorrenciaDao(HttpServletRequest request) {
37 super();
38 this.request = request;
39 }
40
41 public List pesquisar(HttpServletRequest request) throws ECARException {
42 try {
43 String dataDe = Pagina.getParamStr(request, "dataDe");
44
45 String dataLimite = Pagina.getParamStr(request, "dataLimite");
46 String argumento = Pagina.getParamStr(request, "argumento");
47 String ativo = Pagina.getParamStr(request, "ativo");
48
49 boolean orderHoraMinuto = Pagina.getParamBool(request, "orderHoraMinuto");
50 long codAgeo = Pagina.getParamLong(request, "codAgeo");
51
52 String select = "select agendaOC from AgendaOcorrenciaAgeo agendaOC";
53 String where = "";
54 String order = "";
55
56 if (codAgeo > 0)
57 where = " where agendaOC.codAgeo=:codAgeo";
58
59 if (!"".equals(argumento)) {
60 where = " where ( upper( agendaOC.agendaAge.eventoAge ) like :argumento" + " or upper( agendaOC.descricaoAgeo ) like :argumento" + " or upper( agendaOC.localAgeo ) like :argumento )";
61 }
62
63 if (dataDe != null && !"".equals(dataDe)) {
64 if ("".equals(where))
65 where = " where ";
66 else
67 where = where + " and ";
68
69
70
71
72
73 if (dataLimite != null && !"".equals(dataLimite)) {
74 where = where + "(agendaOC.dataEventoAgeo >= :dataDe and agendaOC.dataEventoAgeo <= :dataLimite)";
75 }
76 else {
77 where = where + "(agendaOC.dataEventoAgeo >= :dataDe)";
78 }
79 }
80
81 if ((ativo != null) && !("".equals(ativo))) {
82 where += " and agendaOC.agendaAge.indAtivoAge = 'S'";
83 }
84
85 if (orderHoraMinuto)
86 order = " order by agendaOC.dataEventoAgeo, agendaOC.horaEventoAgeo, agendaOC.minutoEventoAgeo, agendaOC.agendaAge.eventoAge";
87 else
88 order = " order by agendaOC.dataEventoAgeo, agendaOC.agendaAge.eventoAge";
89
90 Query query = this.getSession().createQuery(select + where + order);
91
92
93 if (!"".equals(argumento)) {
94 query.setString("argumento", "%" + argumento.toUpperCase() + "%");
95 }
96 if (dataDe != null && !"".equals(dataDe)) {
97 query.setDate("dataDe", Data.parseDate(dataDe));
98 }
99
100
101
102
103 if (dataLimite != null && !"".equals(dataLimite)) {
104 query.setDate("dataLimite", Data.parseDate(dataLimite));
105 }
106
107 if (codAgeo > 0) {
108 query.setLong("codAgeo", codAgeo);
109 }
110
111 return query.list();
112 } catch (Exception e) {
113 this.logger.error(e);
114 throw new ECARException(e);
115 }
116 }
117
118
119
120
121
122
123
124 public Collection getAgendaOcorrenciaNaoVinculadaASubAreaOrderByData(AgendaAge agenda, DestaqueSubAreaDtqsa subArea) throws ECARException {
125 try {
126 Query query = this.getSession().createQuery("select agendaOC from AgendaOcorrenciaAgeo agendaOC " + "where agendaOC.agendaAge.codAge = :codAge " + "order by agendaOC.dataEventoAgeo asc");
127 query.setLong("codAge", agenda.getCodAge().longValue());
128
129 Collection cAgenda = query.list();
130 List cSubArea = new ArrayList();
131
132 for (Iterator it = subArea.getDestaqueItemRelDtqirs().iterator(); it.hasNext();) {
133 DestaqueItemRelDtqir dtqItem = (DestaqueItemRelDtqir) it.next();
134 if (dtqItem.getAgendaOcorrenciaAgeo() != null)
135 cSubArea.add(dtqItem.getAgendaOcorrenciaAgeo());
136 }
137 cAgenda.removeAll(cSubArea);
138 return cAgenda;
139 } catch (HibernateException e) {
140 this.logger.error(e);
141 throw new ECARException(e);
142 }
143 }
144
145
146
147
148
149
150
151 public String getLabelExibicao(AgendaOcorrenciaAgeo ocorrencia) {
152 String strData = Data.parseDate(ocorrencia.getDataEventoAgeo());
153 String strHora = ocorrencia.getHoraEventoAgeo().toString();
154 String strMinutos = ocorrencia.getMinutoEventoAgeo().toString();
155 if (strMinutos.length() == 1)
156 strMinutos = "0" + strMinutos;
157 String strEvento = ocorrencia.getAgendaAge().getEventoAge();
158 return strData + " " + strHora + ":" + strMinutos + " " + strEvento;
159 }
160
161
162
163
164
165
166 public void setAgendaOcorrencia(AgendaOcorrenciaAgeo agendaOC, AgendaAge agenda) {
167 agendaOC.setAgendaAge(agenda);
168 agendaOC.setDataEventoAgeo(agenda.getDataAge());
169 agendaOC.setDescricaoAgeo(agenda.getDescricaoAge());
170 agendaOC.setHoraEventoAgeo(agenda.getHoraEventoAge());
171 agendaOC.setMinutoEventoAgeo(agenda.getMinutoEventoAge());
172 agendaOC.setLocalAgeo(agenda.getLocalAge());
173 }
174
175
176
177
178
179
180 public void setAgendaOcorrencia(HttpServletRequest campos, AgendaOcorrenciaAgeo agendaOC) {
181 if (Pagina.getParamDataBanco(campos, "dataEventoAgeo") != null)
182 agendaOC.setDataEventoAgeo(Pagina.getParamDataBanco(campos, "dataEventoAgeo"));
183 if (!Pagina.getParamStr(campos, "descricaoAgeo").equals(""))
184 agendaOC.setDescricaoAgeo(Pagina.getParamStr(campos, "descricaoAgeo"));
185 if (!Pagina.getParamStr(campos, "horaEventoAgeo").equals(""))
186 agendaOC.setHoraEventoAgeo(Integer.valueOf(Pagina.getParamStr(campos, "horaEventoAgeo")));
187 if (!Pagina.getParamStr(campos, "minutoEventoAgeo").equals(""))
188 agendaOC.setMinutoEventoAgeo(Integer.valueOf(Pagina.getParamStr(campos, "minutoEventoAgeo")));
189 if (!Pagina.getParamStr(campos, "localAgeo").equals(""))
190 agendaOC.setLocalAgeo(Pagina.getParamStr(campos, "localAgeo"));
191 }
192
193
194
195
196
197
198 public void alterarOcorrencias(HttpServletRequest request, AgendaAge agenda) throws ECARException {
199 Transaction tx = null;
200
201 try {
202 ArrayList objetos = new ArrayList();
203
204 super.inicializarLogBean();
205
206 tx = session.beginTransaction();
207
208 for (Iterator it = agenda.getAgendaOcorrenciaAgeos().iterator(); it.hasNext();) {
209 AgendaOcorrenciaAgeo agendaOC = (AgendaOcorrenciaAgeo) it.next();
210 agendaOC.setDescricaoAgeo(Pagina.getParamStr(request, "descricaoAgeo"));
211 agendaOC.setHoraEventoAgeo(Integer.valueOf(Pagina.getParamStr(request, "horaEventoAgeo")));
212 agendaOC.setMinutoEventoAgeo(Integer.valueOf(Pagina.getParamStr(request, "minutoEventoAgeo")));
213 agendaOC.setLocalAgeo(Pagina.getParamStr(request, "localAgeo"));
214 session.update(agendaOC);
215 objetos.add(agendaOC);
216 }
217
218 tx.commit();
219
220 if (super.logBean != null) {
221 super.logBean.setCodigoTransacao(Data.getHoraAtual(false));
222 super.logBean.setOperacao("ALT");
223
224 for (Iterator itObj = objetos.iterator(); itObj.hasNext();) {
225 super.logBean.setObj(itObj.next());
226 super.loggerAuditoria.info(logBean.toString());
227 }
228 }
229 } catch (HibernateException e) {
230 if (tx != null)
231 try {
232 tx.rollback();
233 } catch (HibernateException r) {
234 this.logger.error(r);
235 throw new ECARException("erro.hibernateException");
236 }
237 this.logger.error(e);
238 throw new ECARException("erro.hibernateException");
239 }
240 }
241
242 public List getAnosComAgenda() throws ECARException {
243 try {
244 List anos = new ArrayList();
245 Query query = this.getSession().createQuery("select agendaOC from AgendaOcorrenciaAgeo agendaOC order by agendaOC.dataEventoAgeo");
246
247 List list = query.list();
248
249 for (Iterator it = list.iterator(); it.hasNext();) {
250 AgendaOcorrenciaAgeo ageo = (AgendaOcorrenciaAgeo) it.next();
251
252 Calendar calendar = Calendar.getInstance();
253 calendar.setTime(ageo.getDataEventoAgeo());
254
255 String ano = String.valueOf(calendar.get(Calendar.YEAR));
256
257 if (!anos.contains(ano)) {
258 anos.add(ano);
259 }
260 }
261
262 return anos;
263 } catch (Exception e) {
264 this.logger.error(e);
265 throw new ECARException(e);
266 }
267 }
268
269 }