View Javadoc

1   package ecar.dao;
2   
3   import java.util.ArrayList;
4   import java.util.Collections;
5   import java.util.Comparator;
6   import java.util.Iterator;
7   import java.util.List;
8   
9   import javax.servlet.http.HttpServletRequest;
10  
11  import org.hibernate.HibernateException;
12  import org.hibernate.Transaction;
13  
14  import comum.database.Dao;
15  import comum.util.Data;
16  
17  import ecar.exception.ECARException;
18  import ecar.pojo.RegApontamentoRegda;
19  
20  /**
21   * Classe de manipulação de objetos da classe AtributoAtt.
22   * @author CodeGenerator - Esta classe foi gerada automaticamente
23   * @since 1.0
24   * @version 1.0, Fri Jan 27 07:54:28 BRST 2006
25   */
26  public class RegApontamentoDao extends Dao {
27    /**
28     * Construtor. Chama o Session factory do Hibernate
29     */
30    public RegApontamentoDao(HttpServletRequest request) {
31      super();
32      this.request = request;
33    }
34  
35    /**
36     * Classifica e ordena a lista conforme os parâmetros passados. aptCampo :
37     * infoRegda, dataRegda, usuarioUsu. aptOrden : asc ou desc.
38     * @param aptCampo - Campo pelo qual a lista será ordenada.
39     * @param aptOrdem - Ordem pela qual a lista será ordenada.
40     * @param lista - lista que será ordenada.
41     */
42    public void classificarOrdenacao(String aptCampo, String aptOrdem, List lista) {
43      // testar cada string para realizar a ordenação
44      if ("infoRegda".equals(aptCampo)) {
45        if ("asc".equals(aptOrdem)) {
46          Collections.sort(lista, new Comparator() {
47            public int compare(Object o1, Object o2) {
48              return ((RegApontamentoRegda) o1).getInfoRegda().compareToIgnoreCase(((RegApontamentoRegda) o2).getInfoRegda());
49            }
50          });
51        }
52        else {
53          Collections.sort(lista, new Comparator() {
54            public int compare(Object o1, Object o2) {
55              return -((RegApontamentoRegda) o1).getInfoRegda().compareToIgnoreCase(((RegApontamentoRegda) o2).getInfoRegda());
56            }
57          });
58        }
59      }
60      if ("dataRegda".equals(aptCampo)) {
61        if ("asc".equals(aptOrdem)) {
62          Collections.sort(lista, new Comparator() {
63            public int compare(Object o1, Object o2) {
64              RegApontamentoRegda regA1 = (RegApontamentoRegda) o1;
65              RegApontamentoRegda regA2 = (RegApontamentoRegda) o2;
66              // Quando a data está nula, foi utilizado um artifício para não
67              // ocorrer Exception
68              if (regA1.getDataRegda() != null) {
69                if (regA2.getDataRegda() != null) {
70                  return regA1.getDataRegda().compareTo(regA2.getDataRegda());
71                }
72                else {
73                  return "a".compareTo("");
74                }
75              }
76              else {
77                if (regA2.getDataRegda() != null) {
78                  return "".compareTo("a");
79                }
80                else {
81                  return "".compareTo("");
82                }
83              }
84            }
85          });
86        }
87        else {
88          Collections.sort(lista, new Comparator() {
89            public int compare(Object o1, Object o2) {
90              RegApontamentoRegda regA1 = (RegApontamentoRegda) o1;
91              RegApontamentoRegda regA2 = (RegApontamentoRegda) o2;
92              // Quando a data está nula, foi utilizado um artifício para não
93              // ocorrer Exception
94              if (regA1.getDataRegda() != null) {
95                if (regA2.getDataRegda() != null) {
96                  return -(regA1.getDataRegda().compareTo(regA2.getDataRegda()));
97                }
98                else {
99                  return -("a".compareTo(""));
100               }
101             }
102             else {
103               if (regA2.getDataRegda() != null) {
104                 return -("".compareTo("a"));
105               }
106               else {
107                 return -("".compareTo(""));
108               }
109             }
110           }
111         });
112       }
113     }
114     if ("usuarioUsu".equals(aptCampo)) {
115       if ("asc".equals(aptOrdem)) {
116         Collections.sort(lista, new Comparator() {
117           public int compare(Object o1, Object o2) {
118             return ((RegApontamentoRegda) o1).getUsuarioUsu().getNomeUsuSent().compareToIgnoreCase(((RegApontamentoRegda) o2).getUsuarioUsu().getNomeUsuSent());
119           }
120         });
121       }
122       else {
123         Collections.sort(lista, new Comparator() {
124           public int compare(Object o1, Object o2) {
125             return -((RegApontamentoRegda) o1).getUsuarioUsu().getNomeUsuSent().compareToIgnoreCase(((RegApontamentoRegda) o2).getUsuarioUsu().getNomeUsuSent());
126           }
127         });
128       }
129     }
130   }
131 
132   /**
133    * Verifica depois exclui
134    * @param codigosParaExcluir
135    * @throws ECARException
136    */
137   public void excluir(String[] codigosParaExcluir) throws ECARException {
138     Transaction tx = null;
139 
140     try {
141       ArrayList objetos = new ArrayList();
142 
143       super.inicializarLogBean();
144 
145       tx = session.beginTransaction();
146 
147       for (int i = 0; i < codigosParaExcluir.length; i++) {
148         RegApontamentoRegda regApontamento = (RegApontamentoRegda) buscar(RegApontamentoRegda.class, Long.valueOf(codigosParaExcluir[i]));
149         session.delete(regApontamento);
150         objetos.add(regApontamento);
151       }
152 
153       tx.commit();
154 
155       if (super.logBean != null) {
156         super.logBean.setCodigoTransacao(Data.getHoraAtual(false));
157         super.logBean.setOperacao("EXC");
158         Iterator itObj = objetos.iterator();
159 
160         while (itObj.hasNext()) {
161           super.logBean.setObj(itObj.next());
162           super.loggerAuditoria.info(logBean.toString());
163         }
164       }
165     } catch (HibernateException e) {
166       if (tx != null)
167         try {
168           tx.rollback();
169         } catch (HibernateException r) {
170           this.logger.error(r);
171           throw new ECARException("erro.hibernateException");
172         }
173       this.logger.error(e);
174       throw new ECARException("erro.hibernateException");
175     }
176   }
177 }