1   
2   
3   
4   package ecar.dao;
5   
6   import java.util.Iterator;
7   import java.util.List;
8   
9   import javax.servlet.http.HttpServletRequest;
10  
11  import comum.database.Dao;
12  
13  import ecar.exception.ECARException;
14  import ecar.pojo.Uf;
15  
16  
17  
18  
19  public class UfDao extends Dao {
20  
21    
22  
23  
24    public UfDao(HttpServletRequest request) {
25      super();
26      this.request = request;
27    }
28  
29    
30  
31  
32  
33  
34    public void salvar(Uf uf) throws ECARException {
35      try {
36        if (pesquisarDuplos(uf, new String[] { "descricaoUf" }, "codUf").size() > 0)
37          throw new ECARException("uf.validacao.registroDuplicado");
38  
39        
40        if (buscar(Uf.class, uf.getCodUf()) != null)
41          throw new ECARException("uf.inclusao.jaExiste");
42  
43      } catch (ECARException e) {
44        this.logger.error(e);
45        if (e.getMessageKey().equalsIgnoreCase("erro.objectNotFound")) {
46          super.salvar(uf);
47        }
48        else
49          
50          throw e;
51      }
52    }
53  
54    
55  
56  
57  
58  
59    public void excluir(Uf uf) throws ECARException {
60      try {
61        
62        if (this.contar(uf.getUsuarioUsusByComercUfUsu()) > 0 || this.contar(uf.getUsuarioUsusByResidUfUsu()) > 0) {
63          throw new ECARException("uf.exclusao.erro.usuarioUsu");
64          
65        }
66        else if (this.contar(uf.getEmpresaEmps()) > 0) {
67          throw new ECARException("uf.exclusao.erro.empresaEmp");
68        }
69        else if (this.contar(uf.getEnderecoEnds()) > 0) {
70          throw new ECARException("uf.exclusao.erro.enderecoEnd");
71        }
72        else
73          super.excluir(uf);
74      } catch (ECARException e) {
75        this.logger.error(e);
76        throw e;
77      }
78    }
79  
80    
81  
82  
83  
84  
85    public void alterar(Uf uf) throws ECARException {
86      if (pesquisarDuplos(uf, new String[] { "descricaoUf" }, "codUf").size() > 0)
87        throw new ECARException("uf.validacao.registroDuplicado");
88      super.alterar(uf);
89    }
90  
91    
92  
93  
94  
95    public String getUfJS() throws ECARException {
96      List listaUf = super.listar(Uf.class, new String[] { "descricaoUf", "asc" });
97      Iterator itUf = listaUf.iterator();
98      StringBuilder javaScript = new StringBuilder("function getUf()\n{\n");
99      javaScript.append("\tconteudo = \"\";\n");
100     while (itUf.hasNext()) {
101       Uf uf = (Uf) itUf.next();
102       javaScript.append("\tconteudo += \"\\t\\t\\t\\t<option value = \\\"").append(uf.getCodUf().toString()).append("\\\">").append(uf.getDescricaoUf()).append("\";\n").append("\tconteudo += \"</option>\\n\"\n");
103     }
104     javaScript.append("\treturn(conteudo);\n}");
105     return javaScript.toString();
106   }
107 
108 }