View Javadoc

1   /*
2    * Criado em 28/10/2004
3    */
4   package ecar.dao;
5   
6   import java.util.ArrayList;
7   import java.util.Iterator;
8   import java.util.List;
9   
10  import javax.servlet.http.HttpServletRequest;
11  
12  import org.hibernate.HibernateException;
13  import org.hibernate.Query;
14  
15  import comum.database.Dao;
16  
17  import ecar.bean.AtributoEstruturaListagemItens;
18  import ecar.exception.ECARException;
19  import ecar.pojo.ItemEstruturaIett;
20  import ecar.pojo.OrgaoOrg;
21  import ecar.pojo.OrgaoPeriodoExercicioOrgPerExe;
22  
23  /**
24   * @author felipev
25   */
26  public class OrgaoDao extends Dao {
27  
28    /**
29     * Construtor. Chama o Session factory do Hibernate
30     */
31    public OrgaoDao(HttpServletRequest request) {
32      super();
33      this.request = request;
34    }
35  
36    /**
37     * Utiliza o método excluir da classe Dao realizando antes validações de
38     * relacionamento do órgão com registros em outras tabelas.
39     * @param situacao
40     * @throws ECARException
41     */
42    public void excluir(OrgaoOrg orgao) throws ECARException {
43      try {
44        boolean excluir = true;
45        if (contar(orgao.getAcompReferenciaArefs()) > 0) {
46          excluir = false;
47          throw new ECARException("orgao.exclusao.erro.acompReferenciaArefs");
48        }
49        if (contar(orgao.getItemEstruturaIettsByCodOrgaoResponsavel1Iett()) > 0) {
50          excluir = false;
51          throw new ECARException("orgao.exclusao.erro.itemEstruturaIettsByCodOrgaoResponsavel1Iett");
52        }
53        if (contar(orgao.getItemEstruturaIettsByCodOrgaoResponsavel2Iett()) > 0) {
54          excluir = false;
55          throw new ECARException("orgao.exclusao.erro.itemEstruturaIettsByCodOrgaoResponsavel2Iett");
56        }
57        if (contar(orgao.getUsuarioUsus()) > 0) {
58          excluir = false;
59          throw new ECARException("orgao.exclusao.erro.usuarioUsus");
60        }
61        if (contar(orgao.getRegDemandaRegds()) > 0) {
62          excluir = false;
63          throw new ECARException("orgao.exclusao.erro.regDemandaRegds");
64        }
65        if (excluir)
66          super.excluir(orgao);
67      } catch (ECARException e) {
68        this.logger.error(e);
69        throw e;
70      }
71    }
72  
73    /**
74     * verifica duplicação depois salva
75     * @param orgao
76     * @throws ECARException
77     */
78    public void salvar(OrgaoOrg orgao) throws ECARException {
79      if (pesquisarDuplos(orgao, new String[] { "descricaoOrg", "siglaOrg" }, "codOrg").size() > 0)
80        throw new ECARException("orgao.validacao.registroDuplicado");
81      if (orgao.getCodigoIdentOrg() != null)
82        if (pesquisarDuplos(orgao, new String[] { "codigoIdentOrg" }, "codOrg").size() > 0)
83          throw new ECARException("orgao.validacao.registroDuplicado.codigoIdentOrg");
84      super.salvar(orgao);
85    }
86  
87    /**
88     * verifica duplicação depois altera
89     * @param orgao
90     * @throws ECARException
91     */
92    public void alterar(OrgaoOrg orgao) throws ECARException {
93      if (pesquisarDuplos(orgao, new String[] { "descricaoOrg", "siglaOrg" }, "codOrg").size() > 0)
94        throw new ECARException("orgao.validacao.registroDuplicado");
95      if (orgao.getCodigoIdentOrg() != null)
96        if (pesquisarDuplos(orgao, new String[] { "codigoIdentOrg" }, "codOrg").size() > 0)
97          throw new ECARException("orgao.validacao.registroDuplicado.codigoIdentOrg");
98      super.alterar(orgao);
99    }
100 
101   /**
102    * Retorna lista de orgão responsáveis de itemEstrutura
103    * @param listItemEstruturaIett
104    * @return List
105    * @throws ECARException
106    */
107   public List getOrgaosRespItemEstrutura(List listItemEstruturaIett) throws ECARException {
108     try {
109       List codigosOrgaos = new ArrayList();
110       List orgaos = new ArrayList();
111       Iterator it = listItemEstruturaIett.iterator();
112 
113       while (it.hasNext()) {
114         ItemEstruturaIett item = (ItemEstruturaIett) it.next();
115 
116         OrgaoOrg org = null;
117 
118         if (item.getOrgaoOrgByCodOrgaoResponsavel1Iett() != null) {
119           org = item.getOrgaoOrgByCodOrgaoResponsavel1Iett();
120         }
121         else {
122           /* Se não possuir orgao procura orgao do pai */
123           ItemEstruturaIett itemAux = item;
124           while (itemAux != null && itemAux.getOrgaoOrgByCodOrgaoResponsavel1Iett() == null && itemAux.getItemEstruturaIett() != null) {
125             itemAux = itemAux.getItemEstruturaIett();
126           }
127           if (itemAux != null && itemAux.getOrgaoOrgByCodOrgaoResponsavel1Iett() != null) {
128             org = itemAux.getOrgaoOrgByCodOrgaoResponsavel1Iett();
129           }
130         }
131 
132         if (org != null && !codigosOrgaos.contains(org.getCodOrg().toString())) {
133           codigosOrgaos.add(org.getCodOrg().toString());
134           orgaos.add(org);
135         }
136       }
137 
138       return orgaos;
139     } catch (Exception e) {
140       this.logger.error(e);
141       throw new ECARException(e);
142     }
143   }
144 
145   /*
146    * Método anterior - com problema public List getOrgaosRespItemEstrutura(Long
147    * codAref) throws ECARException{ try{ String query =
148    * "select distinct orgao from OrgaoOrg orgao " +
149    * "join orgao.itemEstruturaIettsByCodOrgaoResponsavel1Iett item " +
150    * "join item.acompReferenciaItemAris ari " +
151    * "join ari.acompReferenciaAref aref "; String where =
152    * "where orgao.indAtivoOrg = 'S' "; String order = "order by orgao.siglaOrg";
153    * if(codAref != null) where += "and aref.codAref = :codAref  "; Query q =
154    * this.getSession().createQuery(query + where + order); if(codAref != null)
155    * q.setLong("codAref", codAref.longValue()); return q.list(); }
156    * catch(HibernateException e){ this.logger.error(e); throw new
157    * ECARException(e); } }
158    */
159 
160   /**
161    * Retorna lista de orgão responsáveis de itemEstrutura para a relação de
162    * itens da tela do Relatório
163    * @return
164    * @throws ECARException
165    */
166   public List getOrgaosRespItemEstruturaRelatorio(List listItemEstruturaIett) throws ECARException {
167     try {
168       List codigosOrgaos = new ArrayList();
169       List orgaos = new ArrayList();
170       Iterator it = listItemEstruturaIett.iterator();
171 
172       while (it.hasNext()) {
173 
174         AtributoEstruturaListagemItens aeIett = (AtributoEstruturaListagemItens) it.next();
175         // ItemEstruturaIett item = (ItemEstruturaIett) it.next();
176         ItemEstruturaIett item = aeIett.getItem();
177 
178         OrgaoOrg org = null;
179 
180         if (item.getOrgaoOrgByCodOrgaoResponsavel1Iett() != null) {
181           org = item.getOrgaoOrgByCodOrgaoResponsavel1Iett();
182         }
183         else {
184           /* Se não possuir orgao procura orgao do pai */
185           ItemEstruturaIett itemAux = item;
186           while (itemAux != null && itemAux.getOrgaoOrgByCodOrgaoResponsavel1Iett() == null && itemAux.getItemEstruturaIett() != null) {
187             itemAux = itemAux.getItemEstruturaIett();
188           }
189           if (itemAux != null && itemAux.getOrgaoOrgByCodOrgaoResponsavel1Iett() != null) {
190             org = itemAux.getOrgaoOrgByCodOrgaoResponsavel1Iett();
191           }
192         }
193 
194         if (org != null && !codigosOrgaos.contains(org.getCodOrg().toString())) {
195           codigosOrgaos.add(org.getCodOrg().toString());
196           orgaos.add(org);
197         }
198       }
199 
200       return orgaos;
201     } catch (Exception e) {
202       this.logger.error(e);
203       throw new ECARException(e);
204     }
205   }
206 
207   /**
208    * Retorna lista de orgão responsável 2 de itemEstrutura
209    * @return
210    * @throws ECARException
211    */
212   public List getOrgaosResp2ItemEstrutura(List listItemEstruturaIett) throws ECARException {
213     try {
214       List codigosOrgaos = new ArrayList();
215       List orgaos = new ArrayList();
216       Iterator it = listItemEstruturaIett.iterator();
217 
218       while (it.hasNext()) {
219         ItemEstruturaIett item = (ItemEstruturaIett) it.next();
220 
221         OrgaoOrg org = null;
222 
223         if (item.getOrgaoOrgByCodOrgaoResponsavel2Iett() != null) {
224           org = item.getOrgaoOrgByCodOrgaoResponsavel2Iett();
225         }
226         else {
227           /* Se não possuir orgao procura orgao do pai */
228           ItemEstruturaIett itemAux = item;
229           while (itemAux != null && itemAux.getOrgaoOrgByCodOrgaoResponsavel2Iett() == null && itemAux.getItemEstruturaIett() != null) {
230             itemAux = itemAux.getItemEstruturaIett();
231           }
232           if (itemAux != null && itemAux.getOrgaoOrgByCodOrgaoResponsavel2Iett() != null) {
233             org = itemAux.getOrgaoOrgByCodOrgaoResponsavel2Iett();
234           }
235         }
236 
237         if (org != null && !codigosOrgaos.contains(org.getCodOrg().toString())) {
238           codigosOrgaos.add(org.getCodOrg().toString());
239           orgaos.add(org);
240         }
241       }
242 
243       return orgaos;
244     } catch (Exception e) {
245       this.logger.error(e);
246       throw new ECARException(e);
247     }
248   }
249 
250   /**
251    * Carrega listagem de Orgaos de acordo com o periodo, status e Poder
252    * @param codPerExe Identificador <PeriodoExercicioPerExe>
253    * @param poderId Identificador do Poder
254    * @param indAtivoPodPerExe Identificador de status Ativo / Inativo
255    * @return ArrayList<OrgaoOrg>
256    */
257   public ArrayList<OrgaoOrg> getOrgaoByPeriodicidade(Long codPerExe, Long poderId, Character indAtivoPodPerExe) throws ECARException {
258 
259     try {
260       StringBuilder qry = new StringBuilder("from OrgaoPeriodoExercicioOrgPerExe as orgao").append(" where orgao.indAtivoOrgPerExe  = :status").append(" and orgao.periodoExercicioPerExe.codPerExe  = :periodo").append(" and orgao.orgaoOrg.poderPod.codPod  = :poder").append(" order by orgao.orgaoOrg.descricaoOrg");
261 
262       Query q = this.session.createQuery(qry.toString());
263       q.setLong("periodo", codPerExe.longValue());
264       q.setLong("poder", poderId.longValue());
265       q.setCharacter("status", indAtivoPodPerExe);
266 
267       List orgaoPerexe = q.list();
268       ArrayList<OrgaoOrg> listOrgao = new ArrayList<OrgaoOrg>();
269 
270       for (Iterator iter = orgaoPerexe.iterator(); iter.hasNext();) {
271         OrgaoPeriodoExercicioOrgPerExe orgTmp = (OrgaoPeriodoExercicioOrgPerExe) iter.next();
272         listOrgao.add(orgTmp.getOrgaoOrg());
273       }
274 
275       return listOrgao;
276 
277     } catch (HibernateException e) {
278       this.logger.error(e);
279       throw new ECARException("erro.hibernateException");
280     }
281 
282   }
283 
284   /**
285    * Carrega listagem de Orgaos de acordo com o periodo e status
286    * @param codPerExe Identificador <PeriodoExercicioPerExe>
287    * @param indAtivoPodPerExe Identificador de status Ativo / Inativo
288    * @return ArrayList<OrgaoOrg>
289    */
290   public ArrayList<OrgaoOrg> getOrgaoByPeriodicidade(Long codPerExe, Character indAtivoPodPerExe) throws ECARException {
291 
292     try {
293       StringBuilder qry = new StringBuilder("from OrgaoPeriodoExercicioOrgPerExe as orgao").append(" where orgao.indAtivoOrgPerExe  = :status").append(" and orgao.periodoExercicioPerExe.codPerExe  = :periodo").append(" order by orgao.orgaoOrg.descricaoOrg");
294 
295       Query q = this.session.createQuery(qry.toString());
296       q.setLong("periodo", codPerExe.longValue());
297       q.setCharacter("status", indAtivoPodPerExe);
298 
299       List orgaoPerexe = q.list();
300       ArrayList<OrgaoOrg> listOrgao = new ArrayList<OrgaoOrg>();
301 
302       for (Iterator iter = orgaoPerexe.iterator(); iter.hasNext();) {
303         OrgaoPeriodoExercicioOrgPerExe orgTmp = (OrgaoPeriodoExercicioOrgPerExe) iter.next();
304         listOrgao.add(orgTmp.getOrgaoOrg());
305       }
306 
307       return listOrgao;
308 
309     } catch (HibernateException e) {
310       this.logger.error(e);
311       throw new ECARException("erro.hibernateException");
312     }
313 
314   }
315 
316 }