├── .gitignore ├── pom.xml └── src └── main ├── java ├── CriaLancamentos.java └── com │ └── algaworks │ └── financeiro │ ├── controller │ ├── CadastroLancamentoBean.java │ ├── ConsultaLancamentosBean.java │ ├── LoginBean.java │ └── Usuario.java │ ├── converter │ ├── LancamentosConverter.java │ └── PessoaConverter.java │ ├── filter │ └── AutorizacaoFilter.java │ ├── model │ ├── Lancamento.java │ ├── Pessoa.java │ └── TipoLancamento.java │ ├── repository │ ├── Lancamentos.java │ └── Pessoas.java │ ├── service │ ├── CadastroLancamentos.java │ └── NegocioException.java │ ├── util │ ├── EntityManagerProducer.java │ ├── JpaUtil.java │ ├── TransactionInterceptor.java │ └── Transactional.java │ └── validation │ └── DecimalPositivo.java ├── resources ├── META-INF │ ├── beans.xml │ └── persistence.xml ├── ValidationMessages.properties └── com │ └── algaworks │ └── financeiro │ └── resources │ └── Messages.properties └── webapp ├── CadastroLancamento.xhtml ├── ConsultaLancamentos.xhtml ├── Login.xhtml ├── META-INF └── context.xml ├── WEB-INF ├── faces-config.xml ├── template │ └── Layout.xhtml └── web.xml └── resources └── algaworks ├── estilo.css ├── loading.gif └── logo.png /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | .DS_Store 3 | Servers/* 4 | .metadata 5 | .settings 6 | .classpath 7 | .project 8 | target/ 9 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | com.algaworks 4 | Financeiro 5 | 0.0.1-SNAPSHOT 6 | war 7 | 8 | 9 | UTF-8 10 | 11 | 12 | 13 | 14 | 15 | org.omnifaces 16 | omnifaces 17 | 2.0 18 | compile 19 | 20 | 21 | 22 | 23 | org.primefaces 24 | primefaces 25 | 5.1 26 | compile 27 | 28 | 29 | 30 | 31 | org.jboss.weld.servlet 32 | weld-servlet 33 | 2.2.9.Final 34 | compile 35 | 36 | 37 | 38 | 39 | org.jboss 40 | jandex 41 | 1.2.3.Final 42 | compile 43 | 44 | 45 | 46 | 47 | org.hibernate 48 | hibernate-validator 49 | 5.1.3.Final 50 | compile 51 | 52 | 53 | 54 | 55 | org.hibernate 56 | hibernate-core 57 | 4.3.8.Final 58 | compile 59 | 60 | 61 | 62 | 63 | org.hibernate 64 | hibernate-entitymanager 65 | 4.3.8.Final 66 | compile 67 | 68 | 69 | 70 | 71 | mysql 72 | mysql-connector-java 73 | 5.1.34 74 | compile 75 | 76 | 77 | 78 | 79 | org.glassfish 80 | javax.faces 81 | 2.2.10 82 | compile 83 | 84 | 85 | 86 | javax.servlet 87 | javax.servlet-api 88 | 3.1.0 89 | provided 90 | 91 | 92 | 93 | 94 | 95 | 96 | maven-compiler-plugin 97 | 3.0 98 | 99 | 1.8 100 | 1.8 101 | 102 | 103 | 104 | 105 | 106 | -------------------------------------------------------------------------------- /src/main/java/CriaLancamentos.java: -------------------------------------------------------------------------------- 1 | import java.math.BigDecimal; 2 | import java.util.Calendar; 3 | 4 | import javax.persistence.EntityManager; 5 | import javax.persistence.EntityTransaction; 6 | 7 | import com.algaworks.financeiro.model.Lancamento; 8 | import com.algaworks.financeiro.model.Pessoa; 9 | import com.algaworks.financeiro.model.TipoLancamento; 10 | import com.algaworks.financeiro.util.JpaUtil; 11 | 12 | public class CriaLancamentos { 13 | 14 | public static void main(String[] args) { 15 | EntityManager manager = JpaUtil.getEntityManager(); 16 | EntityTransaction trx = manager.getTransaction(); 17 | trx.begin(); 18 | 19 | Calendar dataVencimento1 = Calendar.getInstance(); 20 | dataVencimento1.set(2013, 10, 1, 0, 0, 0); 21 | 22 | Calendar dataVencimento2 = Calendar.getInstance(); 23 | dataVencimento2.set(2013, 12, 10, 0, 0, 0); 24 | 25 | Pessoa cliente = new Pessoa(); 26 | cliente.setNome("WWW Indústria de Alimentos"); 27 | 28 | Pessoa fornecedor = new Pessoa(); 29 | fornecedor.setNome("SoftBRAX Treinamentos"); 30 | 31 | Lancamento lancamento1 = new Lancamento(); 32 | lancamento1.setDescricao("Venda de licença de software"); 33 | lancamento1.setPessoa(cliente); 34 | lancamento1.setDataVencimento(dataVencimento1.getTime()); 35 | lancamento1.setDataPagamento(dataVencimento1.getTime()); 36 | lancamento1.setValor(new BigDecimal(103_000)); 37 | lancamento1.setTipo(TipoLancamento.RECEITA); 38 | 39 | Lancamento lancamento2 = new Lancamento(); 40 | lancamento2.setDescricao("Venda de suporte anual"); 41 | lancamento2.setPessoa(cliente); 42 | lancamento2.setDataVencimento(dataVencimento1.getTime()); 43 | lancamento2.setDataPagamento(dataVencimento1.getTime()); 44 | lancamento2.setValor(new BigDecimal(15_000)); 45 | lancamento2.setTipo(TipoLancamento.RECEITA); 46 | 47 | Lancamento lancamento3 = new Lancamento(); 48 | lancamento3.setDescricao("Treinamento da equipe"); 49 | lancamento3.setPessoa(fornecedor); 50 | lancamento3.setDataVencimento(dataVencimento2.getTime()); 51 | lancamento3.setValor(new BigDecimal(68_000)); 52 | lancamento3.setTipo(TipoLancamento.DESPESA); 53 | 54 | manager.persist(cliente); 55 | manager.persist(fornecedor); 56 | manager.persist(lancamento1); 57 | manager.persist(lancamento2); 58 | manager.persist(lancamento3); 59 | 60 | trx.commit(); 61 | manager.close(); 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/com/algaworks/financeiro/controller/CadastroLancamentoBean.java: -------------------------------------------------------------------------------- 1 | package com.algaworks.financeiro.controller; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | 6 | import javax.faces.application.FacesMessage; 7 | import javax.faces.context.FacesContext; 8 | import javax.faces.event.AjaxBehaviorEvent; 9 | import javax.inject.Inject; 10 | import javax.inject.Named; 11 | 12 | import com.algaworks.financeiro.model.Lancamento; 13 | import com.algaworks.financeiro.model.Pessoa; 14 | import com.algaworks.financeiro.model.TipoLancamento; 15 | import com.algaworks.financeiro.repository.Lancamentos; 16 | import com.algaworks.financeiro.repository.Pessoas; 17 | import com.algaworks.financeiro.service.CadastroLancamentos; 18 | import com.algaworks.financeiro.service.NegocioException; 19 | 20 | @Named 21 | @javax.faces.view.ViewScoped 22 | public class CadastroLancamentoBean implements Serializable { 23 | 24 | private static final long serialVersionUID = 1L; 25 | 26 | @Inject 27 | private CadastroLancamentos cadastro; 28 | 29 | @Inject 30 | private Pessoas pessoas; 31 | 32 | @Inject 33 | private Lancamentos lancamentos; 34 | 35 | private Lancamento lancamento; 36 | private List todasPessoas; 37 | 38 | public void prepararCadastro() { 39 | this.todasPessoas = this.pessoas.todas(); 40 | 41 | if (this.lancamento == null) { 42 | this.lancamento = new Lancamento(); 43 | } 44 | } 45 | 46 | public List pesquisarDescricoes(String descricao) { 47 | return this.lancamentos.descricoesQueContem(descricao); 48 | } 49 | 50 | public void dataVencimentoAlterada(AjaxBehaviorEvent event) { 51 | if (this.lancamento.getDataPagamento() == null) { 52 | this.lancamento.setDataPagamento(this.lancamento.getDataVencimento()); 53 | } 54 | } 55 | 56 | public void salvar() { 57 | FacesContext context = FacesContext.getCurrentInstance(); 58 | 59 | try { 60 | this.cadastro.salvar(this.lancamento); 61 | 62 | this.lancamento = new Lancamento(); 63 | context.addMessage(null, new FacesMessage("Lançamento salvo com sucesso!")); 64 | } catch (NegocioException e) { 65 | 66 | FacesMessage mensagem = new FacesMessage(e.getMessage()); 67 | mensagem.setSeverity(FacesMessage.SEVERITY_ERROR); 68 | context.addMessage(null, mensagem); 69 | } 70 | } 71 | 72 | public List getTodasPessoas() { 73 | return this.todasPessoas; 74 | } 75 | 76 | public TipoLancamento[] getTiposLancamentos() { 77 | return TipoLancamento.values(); 78 | } 79 | 80 | public Lancamento getLancamento() { 81 | return lancamento; 82 | } 83 | 84 | public void setLancamento(Lancamento lancamento) { 85 | this.lancamento = lancamento; 86 | } 87 | 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/com/algaworks/financeiro/controller/ConsultaLancamentosBean.java: -------------------------------------------------------------------------------- 1 | package com.algaworks.financeiro.controller; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | 6 | import javax.faces.application.FacesMessage; 7 | import javax.faces.context.FacesContext; 8 | import javax.faces.view.ViewScoped; 9 | import javax.inject.Inject; 10 | import javax.inject.Named; 11 | 12 | import com.algaworks.financeiro.model.Lancamento; 13 | import com.algaworks.financeiro.repository.Lancamentos; 14 | import com.algaworks.financeiro.service.CadastroLancamentos; 15 | import com.algaworks.financeiro.service.NegocioException; 16 | 17 | @Named 18 | @ViewScoped 19 | public class ConsultaLancamentosBean implements Serializable { 20 | 21 | private static final long serialVersionUID = 1L; 22 | 23 | @Inject 24 | private Lancamentos lancamentosRepository; 25 | 26 | @Inject 27 | private CadastroLancamentos cadastro; 28 | 29 | private List lancamentos; 30 | 31 | private Lancamento lancamentoSelecionado; 32 | 33 | public void excluir() { 34 | FacesContext context = FacesContext.getCurrentInstance(); 35 | 36 | try { 37 | this.cadastro.excluir(this.lancamentoSelecionado); 38 | this.consultar(); 39 | 40 | context.addMessage(null, new FacesMessage("Lançamento excluído com sucesso!")); 41 | } catch (NegocioException e) { 42 | 43 | FacesMessage mensagem = new FacesMessage(e.getMessage()); 44 | mensagem.setSeverity(FacesMessage.SEVERITY_ERROR); 45 | context.addMessage(null, mensagem); 46 | } 47 | } 48 | 49 | public void consultar() { 50 | this.lancamentos = lancamentosRepository.todos(); 51 | } 52 | 53 | public List getLancamentos() { 54 | return lancamentos; 55 | } 56 | 57 | public Lancamento getLancamentoSelecionado() { 58 | return lancamentoSelecionado; 59 | } 60 | 61 | public void setLancamentoSelecionado(Lancamento lancamentoSelecionado) { 62 | this.lancamentoSelecionado = lancamentoSelecionado; 63 | } 64 | 65 | } -------------------------------------------------------------------------------- /src/main/java/com/algaworks/financeiro/controller/LoginBean.java: -------------------------------------------------------------------------------- 1 | package com.algaworks.financeiro.controller; 2 | 3 | import java.util.Date; 4 | 5 | import javax.enterprise.context.RequestScoped; 6 | import javax.faces.application.FacesMessage; 7 | import javax.faces.context.FacesContext; 8 | import javax.inject.Inject; 9 | import javax.inject.Named; 10 | 11 | @Named 12 | @RequestScoped 13 | public class LoginBean { 14 | 15 | @Inject 16 | private Usuario usuario; 17 | 18 | private String nomeUsuario; 19 | private String senha; 20 | 21 | public String login() { 22 | FacesContext context = FacesContext.getCurrentInstance(); 23 | 24 | if ("admin".equals(this.nomeUsuario) && "123".equals(this.senha)) { 25 | this.usuario.setNome(this.nomeUsuario); 26 | this.usuario.setDataLogin(new Date()); 27 | 28 | return "/ConsultaLancamentos?faces-redirect=true"; 29 | } else { 30 | FacesMessage mensagem = new FacesMessage("Usuário/senha inválidos!"); 31 | mensagem.setSeverity(FacesMessage.SEVERITY_ERROR); 32 | context.addMessage(null, mensagem); 33 | } 34 | 35 | return null; 36 | } 37 | 38 | public String logout() { 39 | FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); 40 | return "/Login?faces-redirect=true"; 41 | } 42 | 43 | public String getNomeUsuario() { 44 | return nomeUsuario; 45 | } 46 | 47 | public void setNomeUsuario(String nomeUsuario) { 48 | this.nomeUsuario = nomeUsuario; 49 | } 50 | 51 | public String getSenha() { 52 | return senha; 53 | } 54 | 55 | public void setSenha(String senha) { 56 | this.senha = senha; 57 | } 58 | 59 | } -------------------------------------------------------------------------------- /src/main/java/com/algaworks/financeiro/controller/Usuario.java: -------------------------------------------------------------------------------- 1 | package com.algaworks.financeiro.controller; 2 | 3 | import java.io.Serializable; 4 | import java.util.Date; 5 | 6 | import javax.enterprise.context.SessionScoped; 7 | import javax.inject.Named; 8 | 9 | @Named 10 | @SessionScoped 11 | public class Usuario implements Serializable { 12 | 13 | private static final long serialVersionUID = 1L; 14 | 15 | private String nome; 16 | private Date dataLogin; 17 | 18 | public boolean isLogado() { 19 | return nome != null; 20 | } 21 | 22 | public String getNome() { 23 | return nome; 24 | } 25 | 26 | public void setNome(String nome) { 27 | this.nome = nome; 28 | } 29 | 30 | public Date getDataLogin() { 31 | return dataLogin; 32 | } 33 | 34 | public void setDataLogin(Date dataLogin) { 35 | this.dataLogin = dataLogin; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/algaworks/financeiro/converter/LancamentosConverter.java: -------------------------------------------------------------------------------- 1 | package com.algaworks.financeiro.converter; 2 | 3 | import javax.faces.component.UIComponent; 4 | import javax.faces.context.FacesContext; 5 | import javax.faces.convert.Converter; 6 | import javax.faces.convert.FacesConverter; 7 | import javax.inject.Inject; 8 | 9 | import com.algaworks.financeiro.model.Lancamento; 10 | import com.algaworks.financeiro.repository.Lancamentos; 11 | 12 | @FacesConverter(forClass = Lancamento.class) 13 | public class LancamentosConverter implements Converter { 14 | 15 | @Inject 16 | private Lancamentos lancamentos; 17 | 18 | @Override 19 | public Object getAsObject(FacesContext context, UIComponent component, String value) { 20 | Lancamento retorno = null; 21 | 22 | if (value != null && !"".equals(value)) { 23 | retorno = this.lancamentos.porId(new Long(value)); 24 | } 25 | 26 | return retorno; 27 | } 28 | 29 | @Override 30 | public String getAsString(FacesContext context, UIComponent component, Object value) { 31 | if (value != null) { 32 | Lancamento lancamento = ((Lancamento) value); 33 | return lancamento.getId() == null ? null : lancamento.getId().toString(); 34 | } 35 | return null; 36 | } 37 | 38 | } -------------------------------------------------------------------------------- /src/main/java/com/algaworks/financeiro/converter/PessoaConverter.java: -------------------------------------------------------------------------------- 1 | package com.algaworks.financeiro.converter; 2 | 3 | import javax.faces.component.UIComponent; 4 | import javax.faces.context.FacesContext; 5 | import javax.faces.convert.Converter; 6 | import javax.faces.convert.FacesConverter; 7 | import javax.inject.Inject; 8 | 9 | import com.algaworks.financeiro.model.Pessoa; 10 | import com.algaworks.financeiro.repository.Pessoas; 11 | 12 | @FacesConverter(forClass = Pessoa.class) 13 | public class PessoaConverter implements Converter { 14 | 15 | @Inject // funciona graças ao OmniFaces 16 | private Pessoas pessoas; 17 | 18 | @Override 19 | public Object getAsObject(FacesContext context, UIComponent component, String value) { 20 | Pessoa retorno = null; 21 | 22 | if (value != null && !"".equals(value)) { 23 | retorno = this.pessoas.porId(new Long(value)); 24 | } 25 | 26 | return retorno; 27 | } 28 | 29 | @Override 30 | public String getAsString(FacesContext context, UIComponent component, Object value) { 31 | if (value != null) { 32 | return ((Pessoa) value).getId().toString(); 33 | } 34 | return null; 35 | } 36 | 37 | } -------------------------------------------------------------------------------- /src/main/java/com/algaworks/financeiro/filter/AutorizacaoFilter.java: -------------------------------------------------------------------------------- 1 | package com.algaworks.financeiro.filter; 2 | 3 | import java.io.IOException; 4 | 5 | import javax.inject.Inject; 6 | import javax.servlet.Filter; 7 | import javax.servlet.FilterChain; 8 | import javax.servlet.FilterConfig; 9 | import javax.servlet.ServletException; 10 | import javax.servlet.ServletRequest; 11 | import javax.servlet.ServletResponse; 12 | import javax.servlet.annotation.WebFilter; 13 | import javax.servlet.http.HttpServletRequest; 14 | import javax.servlet.http.HttpServletResponse; 15 | 16 | import com.algaworks.financeiro.controller.Usuario; 17 | 18 | @WebFilter("*.xhtml") 19 | public class AutorizacaoFilter implements Filter { 20 | 21 | @Inject 22 | private Usuario autenticacao; 23 | 24 | @Override 25 | public void doFilter(ServletRequest req, ServletResponse res, 26 | FilterChain chain) throws IOException, ServletException { 27 | HttpServletResponse response = (HttpServletResponse) res; 28 | HttpServletRequest request = (HttpServletRequest) req; 29 | 30 | if (!autenticacao.isLogado() && !request.getRequestURI().endsWith("/Login.xhtml") 31 | && !request.getRequestURI().contains("/javax.faces.resource/")) { 32 | response.sendRedirect(request.getContextPath() + "/Login.xhtml"); 33 | } else { 34 | chain.doFilter(req, res); 35 | } 36 | } 37 | 38 | @Override 39 | public void init(FilterConfig config) throws ServletException { 40 | } 41 | 42 | @Override 43 | public void destroy() { 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/com/algaworks/financeiro/model/Lancamento.java: -------------------------------------------------------------------------------- 1 | package com.algaworks.financeiro.model; 2 | 3 | import java.io.Serializable; 4 | import java.math.BigDecimal; 5 | import java.util.Date; 6 | 7 | import javax.persistence.Column; 8 | import javax.persistence.Entity; 9 | import javax.persistence.EnumType; 10 | import javax.persistence.Enumerated; 11 | import javax.persistence.GeneratedValue; 12 | import javax.persistence.Id; 13 | import javax.persistence.JoinColumn; 14 | import javax.persistence.ManyToOne; 15 | import javax.persistence.Table; 16 | import javax.persistence.Temporal; 17 | import javax.persistence.TemporalType; 18 | import javax.validation.constraints.NotNull; 19 | import javax.validation.constraints.Size; 20 | 21 | import org.hibernate.validator.constraints.NotEmpty; 22 | 23 | import com.algaworks.financeiro.validation.DecimalPositivo; 24 | 25 | @Entity 26 | @Table(name = "lancamento") 27 | public class Lancamento implements Serializable { 28 | 29 | private static final long serialVersionUID = 1L; 30 | 31 | private Long id; 32 | private Pessoa pessoa; 33 | private String descricao; 34 | private BigDecimal valor; 35 | private TipoLancamento tipo; 36 | private Date dataVencimento; 37 | private Date dataPagamento; 38 | 39 | @Id 40 | @GeneratedValue 41 | public Long getId() { 42 | return id; 43 | } 44 | 45 | public void setId(Long id) { 46 | this.id = id; 47 | } 48 | 49 | @NotNull 50 | @ManyToOne(optional = false) 51 | @JoinColumn(name = "pessoa_id") 52 | public Pessoa getPessoa() { 53 | return pessoa; 54 | } 55 | 56 | public void setPessoa(Pessoa pessoa) { 57 | this.pessoa = pessoa; 58 | } 59 | 60 | @NotEmpty 61 | @Size(max = 80) 62 | @Column(length = 80, nullable = false) 63 | public String getDescricao() { 64 | return descricao; 65 | } 66 | 67 | public void setDescricao(String descricao) { 68 | this.descricao = descricao; 69 | } 70 | 71 | @DecimalPositivo 72 | @Column(precision = 10, scale = 2, nullable = false) 73 | public BigDecimal getValor() { 74 | return valor; 75 | } 76 | 77 | public void setValor(BigDecimal valor) { 78 | this.valor = valor; 79 | } 80 | 81 | @NotNull 82 | @Enumerated(EnumType.STRING) 83 | @Column(nullable = false) 84 | public TipoLancamento getTipo() { 85 | return tipo; 86 | } 87 | 88 | public void setTipo(TipoLancamento tipo) { 89 | this.tipo = tipo; 90 | } 91 | 92 | @NotNull 93 | @Temporal(TemporalType.DATE) 94 | @Column(name = "data_vencimento", nullable = false) 95 | public Date getDataVencimento() { 96 | return dataVencimento; 97 | } 98 | 99 | public void setDataVencimento(Date dataVencimento) { 100 | this.dataVencimento = dataVencimento; 101 | } 102 | 103 | @Temporal(TemporalType.DATE) 104 | @Column(name = "data_pagamento", nullable = true) 105 | public Date getDataPagamento() { 106 | return dataPagamento; 107 | } 108 | 109 | public void setDataPagamento(Date dataPagamento) { 110 | this.dataPagamento = dataPagamento; 111 | } 112 | 113 | @Override 114 | public int hashCode() { 115 | final int prime = 31; 116 | int result = 1; 117 | result = prime * result + ((id == null) ? 0 : id.hashCode()); 118 | return result; 119 | } 120 | 121 | @Override 122 | public boolean equals(Object obj) { 123 | if (this == obj) 124 | return true; 125 | if (obj == null) 126 | return false; 127 | if (getClass() != obj.getClass()) 128 | return false; 129 | Lancamento other = (Lancamento) obj; 130 | if (id == null) { 131 | if (other.id != null) 132 | return false; 133 | } else if (!id.equals(other.id)) 134 | return false; 135 | return true; 136 | } 137 | 138 | } -------------------------------------------------------------------------------- /src/main/java/com/algaworks/financeiro/model/Pessoa.java: -------------------------------------------------------------------------------- 1 | package com.algaworks.financeiro.model; 2 | 3 | import java.io.Serializable; 4 | 5 | import javax.persistence.Column; 6 | import javax.persistence.Entity; 7 | import javax.persistence.GeneratedValue; 8 | import javax.persistence.Id; 9 | import javax.persistence.Table; 10 | import javax.validation.constraints.Size; 11 | 12 | import org.hibernate.validator.constraints.NotEmpty; 13 | 14 | @Entity 15 | @Table(name = "pessoa") 16 | public class Pessoa implements Serializable { 17 | 18 | private static final long serialVersionUID = 1L; 19 | 20 | private Long id; 21 | private String nome; 22 | 23 | @Id 24 | @GeneratedValue 25 | public Long getId() { 26 | return id; 27 | } 28 | 29 | public void setId(Long id) { 30 | this.id = id; 31 | } 32 | 33 | @NotEmpty 34 | @Size(max = 60) 35 | @Column(length = 60, nullable = false) 36 | public String getNome() { 37 | return nome; 38 | } 39 | 40 | public void setNome(String nome) { 41 | this.nome = nome; 42 | } 43 | 44 | @Override 45 | public int hashCode() { 46 | final int prime = 31; 47 | int result = 1; 48 | result = prime * result + ((id == null) ? 0 : id.hashCode()); 49 | return result; 50 | } 51 | 52 | @Override 53 | public boolean equals(Object obj) { 54 | if (this == obj) 55 | return true; 56 | if (obj == null) 57 | return false; 58 | if (getClass() != obj.getClass()) 59 | return false; 60 | Pessoa other = (Pessoa) obj; 61 | if (id == null) { 62 | if (other.id != null) 63 | return false; 64 | } else if (!id.equals(other.id)) 65 | return false; 66 | return true; 67 | } 68 | 69 | } -------------------------------------------------------------------------------- /src/main/java/com/algaworks/financeiro/model/TipoLancamento.java: -------------------------------------------------------------------------------- 1 | package com.algaworks.financeiro.model; 2 | 3 | public enum TipoLancamento { 4 | 5 | RECEITA("Receita"), 6 | DESPESA("Despesa"); 7 | 8 | private String descricao; 9 | 10 | TipoLancamento(String descricao) { 11 | this.descricao = descricao; 12 | } 13 | 14 | public String getDescricao() { 15 | return descricao; 16 | } 17 | 18 | } -------------------------------------------------------------------------------- /src/main/java/com/algaworks/financeiro/repository/Lancamentos.java: -------------------------------------------------------------------------------- 1 | package com.algaworks.financeiro.repository; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | 6 | import javax.inject.Inject; 7 | import javax.persistence.EntityManager; 8 | import javax.persistence.TypedQuery; 9 | 10 | import com.algaworks.financeiro.model.Lancamento; 11 | 12 | public class Lancamentos implements Serializable { 13 | 14 | private static final long serialVersionUID = 1L; 15 | 16 | private EntityManager manager; 17 | 18 | @Inject 19 | public Lancamentos(EntityManager manager) { 20 | this.manager = manager; 21 | } 22 | 23 | public Lancamento porId(Long id) { 24 | return manager.find(Lancamento.class, id); 25 | } 26 | 27 | public List descricoesQueContem(String descricao) { 28 | TypedQuery query = manager.createQuery( 29 | "select distinct descricao from Lancamento " 30 | + "where upper(descricao) like upper(:descricao)", 31 | String.class); 32 | query.setParameter("descricao", "%" + descricao + "%"); 33 | return query.getResultList(); 34 | } 35 | 36 | public List todos() { 37 | TypedQuery query = manager.createQuery( 38 | "from Lancamento", Lancamento.class); 39 | return query.getResultList(); 40 | } 41 | 42 | public void adicionar(Lancamento lancamento) { 43 | this.manager.persist(lancamento); 44 | } 45 | 46 | public Lancamento guardar(Lancamento lancamento) { 47 | return this.manager.merge(lancamento); 48 | } 49 | 50 | public void remover(Lancamento lancamento) { 51 | this.manager.remove(lancamento); 52 | } 53 | 54 | } -------------------------------------------------------------------------------- /src/main/java/com/algaworks/financeiro/repository/Pessoas.java: -------------------------------------------------------------------------------- 1 | package com.algaworks.financeiro.repository; 2 | 3 | import java.io.Serializable; 4 | import java.util.List; 5 | 6 | import javax.inject.Inject; 7 | import javax.persistence.EntityManager; 8 | import javax.persistence.TypedQuery; 9 | 10 | import com.algaworks.financeiro.model.Pessoa; 11 | 12 | public class Pessoas implements Serializable { 13 | 14 | private static final long serialVersionUID = 1L; 15 | 16 | private EntityManager manager; 17 | 18 | @Inject 19 | public Pessoas(EntityManager manager) { 20 | this.manager = manager; 21 | } 22 | 23 | public Pessoa porId(Long id) { 24 | return manager.find(Pessoa.class, id); 25 | } 26 | 27 | public List todas() { 28 | TypedQuery query = manager.createQuery( 29 | "from Pessoa", Pessoa.class); 30 | return query.getResultList(); 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /src/main/java/com/algaworks/financeiro/service/CadastroLancamentos.java: -------------------------------------------------------------------------------- 1 | package com.algaworks.financeiro.service; 2 | 3 | import java.io.Serializable; 4 | import java.util.Date; 5 | 6 | import javax.inject.Inject; 7 | 8 | import com.algaworks.financeiro.model.Lancamento; 9 | import com.algaworks.financeiro.repository.Lancamentos; 10 | import com.algaworks.financeiro.util.Transactional; 11 | 12 | public class CadastroLancamentos implements Serializable { 13 | 14 | private static final long serialVersionUID = 1L; 15 | 16 | @Inject 17 | private Lancamentos lancamentos; 18 | 19 | @Transactional 20 | public void salvar(Lancamento lancamento) throws NegocioException { 21 | if (lancamento.getDataPagamento() != null && 22 | lancamento.getDataPagamento().after(new Date())) { 23 | throw new NegocioException( 24 | "Data de pagamento não pode ser uma data futura."); 25 | } 26 | 27 | this.lancamentos.guardar(lancamento); 28 | } 29 | 30 | @Transactional 31 | public void excluir(Lancamento lancamento) throws NegocioException { 32 | lancamento = this.lancamentos.porId(lancamento.getId()); 33 | 34 | if (lancamento.getDataPagamento() != null) { 35 | throw new NegocioException("Não é possível excluir um lançamento pago!"); 36 | } 37 | 38 | this.lancamentos.remover(lancamento); 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /src/main/java/com/algaworks/financeiro/service/NegocioException.java: -------------------------------------------------------------------------------- 1 | package com.algaworks.financeiro.service; 2 | 3 | public class NegocioException extends Exception { 4 | 5 | private static final long serialVersionUID = 1L; 6 | 7 | public NegocioException(String msg) { 8 | super(msg); 9 | } 10 | 11 | } -------------------------------------------------------------------------------- /src/main/java/com/algaworks/financeiro/util/EntityManagerProducer.java: -------------------------------------------------------------------------------- 1 | package com.algaworks.financeiro.util; 2 | 3 | import javax.enterprise.context.ApplicationScoped; 4 | import javax.enterprise.context.RequestScoped; 5 | import javax.enterprise.inject.Disposes; 6 | import javax.enterprise.inject.Produces; 7 | import javax.persistence.EntityManager; 8 | import javax.persistence.EntityManagerFactory; 9 | import javax.persistence.Persistence; 10 | 11 | @ApplicationScoped 12 | public class EntityManagerProducer { 13 | 14 | private EntityManagerFactory factory; 15 | 16 | public EntityManagerProducer() { 17 | this.factory = Persistence.createEntityManagerFactory("FinanceiroPU"); 18 | } 19 | 20 | @Produces 21 | @RequestScoped 22 | public EntityManager createEntityManager() { 23 | return factory.createEntityManager(); 24 | } 25 | 26 | public void closeEntityManager(@Disposes EntityManager manager) { 27 | manager.close(); 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /src/main/java/com/algaworks/financeiro/util/JpaUtil.java: -------------------------------------------------------------------------------- 1 | package com.algaworks.financeiro.util; 2 | 3 | import javax.persistence.EntityManager; 4 | import javax.persistence.EntityManagerFactory; 5 | import javax.persistence.Persistence; 6 | 7 | public class JpaUtil { 8 | 9 | private static EntityManagerFactory factory; 10 | 11 | static { 12 | factory = Persistence.createEntityManagerFactory("FinanceiroPU"); 13 | } 14 | 15 | public static EntityManager getEntityManager() { 16 | return factory.createEntityManager(); 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /src/main/java/com/algaworks/financeiro/util/TransactionInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.algaworks.financeiro.util; 2 | 3 | import java.io.Serializable; 4 | 5 | import javax.inject.Inject; 6 | import javax.interceptor.AroundInvoke; 7 | import javax.interceptor.Interceptor; 8 | import javax.interceptor.InvocationContext; 9 | import javax.persistence.EntityManager; 10 | import javax.persistence.EntityTransaction; 11 | 12 | @Interceptor 13 | @Transactional 14 | public class TransactionInterceptor implements Serializable { 15 | 16 | private static final long serialVersionUID = 1L; 17 | 18 | private @Inject EntityManager manager; 19 | 20 | @AroundInvoke 21 | public Object invoke(InvocationContext context) throws Exception { 22 | EntityTransaction trx = manager.getTransaction(); 23 | boolean criador = false; 24 | 25 | try { 26 | if (!trx.isActive()) { 27 | // truque para fazer rollback no que já passou 28 | // (senão, um futuro commit, confirmaria até mesmo operações sem transação) 29 | trx.begin(); 30 | trx.rollback(); 31 | 32 | // agora sim inicia a transação 33 | trx.begin(); 34 | 35 | criador = true; 36 | } 37 | 38 | return context.proceed(); 39 | } catch (Exception e) { 40 | if (trx != null && criador) { 41 | trx.rollback(); 42 | } 43 | 44 | throw e; 45 | } finally { 46 | if (trx != null && trx.isActive() && criador) { 47 | trx.commit(); 48 | } 49 | } 50 | } 51 | 52 | } -------------------------------------------------------------------------------- /src/main/java/com/algaworks/financeiro/util/Transactional.java: -------------------------------------------------------------------------------- 1 | package com.algaworks.financeiro.util; 2 | 3 | import java.lang.annotation.ElementType; 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | import java.lang.annotation.Target; 7 | 8 | import javax.interceptor.InterceptorBinding; 9 | 10 | @InterceptorBinding 11 | @Retention(RetentionPolicy.RUNTIME) 12 | @Target({ ElementType.TYPE, ElementType.METHOD }) 13 | public @interface Transactional { 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/main/java/com/algaworks/financeiro/validation/DecimalPositivo.java: -------------------------------------------------------------------------------- 1 | package com.algaworks.financeiro.validation; 2 | 3 | import static java.lang.annotation.ElementType.ANNOTATION_TYPE; 4 | import static java.lang.annotation.ElementType.CONSTRUCTOR; 5 | import static java.lang.annotation.ElementType.FIELD; 6 | import static java.lang.annotation.ElementType.METHOD; 7 | import static java.lang.annotation.ElementType.PARAMETER; 8 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 9 | 10 | import java.lang.annotation.Retention; 11 | import java.lang.annotation.Target; 12 | 13 | import javax.validation.Constraint; 14 | import javax.validation.OverridesAttribute; 15 | import javax.validation.Payload; 16 | import javax.validation.constraints.DecimalMin; 17 | import javax.validation.constraints.NotNull; 18 | 19 | @Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) 20 | @Retention(RUNTIME) 21 | @Constraint(validatedBy = {}) 22 | @NotNull 23 | @DecimalMin("0") 24 | public @interface DecimalPositivo { 25 | 26 | @OverridesAttribute(constraint = DecimalMin.class, name = "message") 27 | String message() default "{com.algaworks.NumeroDecimal.message}"; 28 | 29 | Class[] groups() default {}; 30 | 31 | Class[] payload() default {}; 32 | 33 | } -------------------------------------------------------------------------------- /src/main/resources/META-INF/beans.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | com.algaworks.financeiro.util.TransactionInterceptor 10 | 11 | 12 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/persistence.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | org.hibernate.ejb.HibernatePersistence 9 | 10 | 11 | 13 | 14 | 15 | 17 | 18 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/main/resources/ValidationMessages.properties: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algaworks/ebook-javaee/60ce90fc59b50cd207a22941a8c1c532710130e2/src/main/resources/ValidationMessages.properties -------------------------------------------------------------------------------- /src/main/resources/com/algaworks/financeiro/resources/Messages.properties: -------------------------------------------------------------------------------- 1 | javax.faces.validator.BeanValidator.MESSAGE={1} {0} -------------------------------------------------------------------------------- /src/main/webapp/CadastroLancamento.xhtml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | Cadastro de lançamento 16 | 17 | 18 |

Cadastro de lançamento

19 | 20 | 21 | 22 | 23 | 24 | 25 | 27 | 30 | 31 | 32 | 33 | 35 | 36 | 38 | 39 | 40 | 41 | 44 | 45 | 46 | 48 | 49 | 50 | 51 | 52 | 54 | 56 | 58 | 59 | 60 | 61 | 63 | 64 | 65 | 67 | 68 | 69 |
70 | 71 |
-------------------------------------------------------------------------------- /src/main/webapp/ConsultaLancamentos.xhtml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | 11 | 12 | 13 | Consulta de lançamentos 14 | 15 | 16 | 17 |

Consulta de lançamentos

18 | 19 | 20 | 21 | 22 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 36 | 37 | 38 | 39 | 40 | 42 | 43 | 44 | 45 | 46 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 58 | 60 | 61 | 62 | 63 | 64 |
65 | 66 |
-------------------------------------------------------------------------------- /src/main/webapp/Login.xhtml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | Login 8 | 9 | 10 | 11 | 12 | 13 |
14 |

Login

15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 | 32 |
33 | 34 | -------------------------------------------------------------------------------- /src/main/webapp/META-INF/context.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/faces-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 10 | com.algaworks.financeiro.resources.Messages 11 | 12 | 13 | 14 | 15 | * 16 | 17 | oi 18 | /Ola.xhtml 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/template/Layout.xhtml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | <ui:insert name="titulo">Sistema Financeiro</ui:insert> 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
31 | Olá #{usuario.nome}! 32 |
33 |
34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 |
52 | 53 |
54 | 55 |
56 | Sistema Financeiro - AlgaWorks 57 |
58 |
59 | 60 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | javax.faces.PROJECT_STAGE 8 | Development 9 | 10 | 11 | 12 | javax.faces.FACELETS_REFRESH_PERIOD 13 | 0 14 | 15 | 16 | 17 | org.jboss.weld.environment.servlet.Listener 18 | 19 | 20 | 21 | BeanManager 22 | javax.enterprise.inject.spi.BeanManager 23 | 24 | 25 | 26 | Faces Servlet 27 | javax.faces.webapp.FacesServlet 28 | 29 | 30 | 31 | Faces Servlet 32 | *.xhtml 33 | 34 | 35 | -------------------------------------------------------------------------------- /src/main/webapp/resources/algaworks/estilo.css: -------------------------------------------------------------------------------- 1 | @charset "utf-8"; 2 | 3 | body { 4 | font-size: 12px; 5 | font-family: Arial, Helvetica, sans-serif; 6 | margin: 0px; 7 | font-weight: normal 8 | } 9 | 10 | header { 11 | padding: 5px; 12 | margin-bottom: 20px; 13 | height: 30px; 14 | background-color: #545454; 15 | color: #fff; 16 | box-shadow: 0px 2px 2px #ccc 17 | } 18 | 19 | #conteudo { 20 | padding: 0px 8px 21 | } 22 | 23 | footer { 24 | border-top: 1px solid #ccc; 25 | padding: 5px 8px; 26 | margin-top: 20px; 27 | margin-bottom: 10px 28 | } 29 | 30 | h1 { 31 | font-size: 24px; 32 | font-weight: 500; 33 | padding: 0px; 34 | margin: 0px; 35 | margin-bottom: 10px 36 | } 37 | 38 | .ajax-status { 39 | position: fixed; 40 | top: 85px; 41 | right: 10px; 42 | width: 35px; 43 | height: 35px 44 | } 45 | 46 | #login-dialog { 47 | width: 260px; 48 | margin: auto; 49 | margin-top: 150px; 50 | } 51 | 52 | .grid-login { 53 | background-color: #f2f2f2; 54 | border-radius: 8px; 55 | border: 1px solid #ccc; 56 | margin-top: 8px; 57 | padding: 10px; 58 | width: 100% 59 | } -------------------------------------------------------------------------------- /src/main/webapp/resources/algaworks/loading.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algaworks/ebook-javaee/60ce90fc59b50cd207a22941a8c1c532710130e2/src/main/webapp/resources/algaworks/loading.gif -------------------------------------------------------------------------------- /src/main/webapp/resources/algaworks/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/algaworks/ebook-javaee/60ce90fc59b50cd207a22941a8c1c532710130e2/src/main/webapp/resources/algaworks/logo.png --------------------------------------------------------------------------------