1   package ecar.dao;
2   
3   import java.util.ArrayList;
4   import java.util.Date;
5   import java.util.Iterator;
6   import java.util.List;
7   
8   import javax.servlet.http.HttpServletRequest;
9   
10  import org.hibernate.HibernateException;
11  import org.hibernate.Transaction;
12  
13  import comum.database.Dao;
14  import comum.util.Data;
15  import comum.util.Pagina;
16  
17  import ecar.exception.ECARException;
18  import ecar.pojo.PeriodoRevisaoPrev;
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  public class PeriodoRevisaoPrevDao extends Dao {
29    
30  
31  
32  
33    public boolean setPeriodoRevisaoPrev(HttpServletRequest request, PeriodoRevisaoPrev prev) throws ECARException {
34      if (!existeIntersecaoPeriodos(Pagina.getParamDataBanco(request, "dtInicioPrev"), Pagina.getParamDataBanco(request, "dtFimPrev"), prev)) {
35        if (!"".equals(Pagina.getParamStr(request, "dtInicioPrev")))
36          prev.setDtInicioPrev(Pagina.getParamDataBanco(request, "dtInicioPrev"));
37        else
38          prev.setDtInicioPrev(null);
39        if (!"".equals(Pagina.getParamStr(request, "dtInicioPrev")))
40          prev.setDtFimPrev(Pagina.getParamDataBanco(request, "dtFimPrev"));
41        else
42          prev.setDtFimPrev(null);
43        prev.setDescricaoPrev(Pagina.getParamStr(request, "descricaoPrev"));
44        prev.setConfiguracaoCfg(new ConfiguracaoDao(request).getConfiguracao());
45        return true;
46      }
47      return false;
48    }
49  
50    
51  
52  
53  
54    public PeriodoRevisaoPrevDao(HttpServletRequest request) {
55      super();
56      this.request = request;
57    }
58  
59    
60  
61  
62  
63  
64  
65  
66    public boolean existeIntersecaoPeriodos(Date dtInicio, Date dtFim, PeriodoRevisaoPrev prevComp) throws ECARException {
67      List periodos = this.listar(PeriodoRevisaoPrev.class, new String[] { "dtInicioPrev", "ASC" });
68      if (!periodos.equals(null) && periodos.size() > 0) {
69        Iterator itPeriodos = periodos.iterator();
70        while (itPeriodos.hasNext()) {
71          PeriodoRevisaoPrev prev = (PeriodoRevisaoPrev) itPeriodos.next();
72          if (!prevComp.equals(prev)) {
73            if (((!dtInicio.before(prev.getDtInicioPrev())) && (!dtInicio.after(prev.getDtFimPrev()))) || ((!dtFim.before(prev.getDtInicioPrev())) && (!dtFim.after(prev.getDtFimPrev()))))
74              return true;
75          }
76        }
77      }
78      return false;
79    }
80  
81    
82  
83  
84  
85  
86    public boolean existePeriodo(Date data) throws ECARException {
87      List periodos = this.listar(PeriodoRevisaoPrev.class, new String[] { "dtInicioPrev", "ASC" });
88      if (!periodos.equals(null) && periodos.size() > 0) {
89        Iterator itPeriodos = periodos.iterator();
90        while (itPeriodos.hasNext()) {
91          PeriodoRevisaoPrev prev = (PeriodoRevisaoPrev) itPeriodos.next();
92          if (!(data.before(prev.getDtInicioPrev())) && (!data.after(prev.getDtFimPrev()))) {
93            return true;
94          }
95        }
96      }
97      return false;
98    }
99  
100   
101 
102 
103 
104   public PeriodoRevisaoPrev getPeriodoAtual() throws ECARException {
105     List periodos = this.listar(PeriodoRevisaoPrev.class, new String[] { "dtInicioPrev", "ASC" });
106     PeriodoRevisaoPrev prev = new PeriodoRevisaoPrev();
107     if (!periodos.equals(null) && periodos.size() > 0) {
108       Iterator itPeriodos = periodos.iterator();
109       while (itPeriodos.hasNext()) {
110         prev = (PeriodoRevisaoPrev) itPeriodos.next();
111         Date hoje = Data.getDataAtual();
112         if ((!hoje.before(prev.getDtInicioPrev())) && (!hoje.after(prev.getDtFimPrev()))) {
113           return prev;
114         }
115       }
116     }
117     return prev;
118   }
119 
120   public boolean estaNoPeriodoAtual(Date data) throws ECARException {
121     PeriodoRevisaoPrev prev = this.getPeriodoAtual();
122     if ((!data.before(prev.getDtInicioPrev())) && (!data.after(prev.getDtFimPrev()))) {
123       return true;
124     }
125     return false;
126   }
127 
128   
129 
130 
131 
132 
133 
134 
135   public void excluir(String[] codigosParaExcluir) throws ECARException {
136     Transaction tx = null;
137 
138     try {
139       ArrayList objetos = new ArrayList();
140 
141       super.inicializarLogBean();
142 
143       tx = session.beginTransaction();
144 
145       for (int i = 0; i < codigosParaExcluir.length; i++) {
146         PeriodoRevisaoPrev prev = (PeriodoRevisaoPrev) buscar(PeriodoRevisaoPrev.class, Long.valueOf(codigosParaExcluir[i]));
147         session.delete(prev);
148         objetos.add(prev);
149       }
150 
151       tx.commit();
152 
153       if (super.logBean != null) {
154         super.logBean.setCodigoTransacao(Data.getHoraAtual(false));
155         super.logBean.setOperacao("EXC");
156         Iterator itObj = objetos.iterator();
157 
158         while (itObj.hasNext()) {
159           super.logBean.setObj(itObj.next());
160           super.loggerAuditoria.info(logBean.toString());
161         }
162       }
163     } catch (HibernateException e) {
164       if (tx != null)
165         try {
166           tx.rollback();
167         } catch (HibernateException r) {
168           this.logger.error(r);
169           throw new ECARException("erro.hibernateException");
170         }
171       this.logger.error(e);
172       throw new ECARException("erro.hibernateException");
173     }
174   }
175 
176 }