Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package br.com.recursive.biblioteca.servicos; import br.com.recursive.biblioteca.entidades.Emprestimo; import br.com.recursive.biblioteca.entidades.Pessoa; import br.com.recursive.biblioteca.entidades.TipoEmprestimo; import br.com.recursive.biblioteca.entidades.VezesEmprestimo; import br.com.recursive.biblioteca.entidades.vos.AutorVO; import br.com.recursive.biblioteca.entidades.vos.LivroVO; import br.com.recursive.biblioteca.persistencia.HibernateDAO; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.joda.time.DateTime; import org.joda.time.Days; /** * * @author Claudivan Moreira */ public class AcervoService { private static final Map<String, String> mapConsultasNoAcervo; static { mapConsultasNoAcervo = new HashMap<String, String>(); mapConsultasNoAcervo.put("titulo", LivroVO.CONSULTA_LIVRO_POR_TITULO); mapConsultasNoAcervo.put("subtitulo", LivroVO.CONSULTA_LIVRO_POR_SUBTITULO); mapConsultasNoAcervo.put("autor", LivroVO.CONSULTA_LIVRO_POR_AUTOR); mapConsultasNoAcervo.put("numchamada", LivroVO.CONSULTA_LIVRO_POR_NUMERO_CHAMADA); mapConsultasNoAcervo.put("editora", LivroVO.CONSULTA_LIVRO_POR_EDITORA); mapConsultasNoAcervo.put("anopublicacao", LivroVO.CONSULTA_LIVRO_POR_ANOPUBLICACAO); } private HibernateDAO hibernateDAO; public AcervoService(HibernateDAO hibernateDAO) { this.hibernateDAO = hibernateDAO; } public List<LivroVO> buscaLivrosComFiltro(String tipoDoFiltro, String palavraChave) { List<LivroVO> listLivros = new ArrayList<LivroVO>(); if (tipoDoFiltro != null && palavraChave != null) { Map<String, Object> parametros = new HashMap<String, Object>(); parametros.put("pattern", "%" + palavraChave.toUpperCase() + "%"); listLivros = hibernateDAO.findWithJpqlQuery(mapConsultasNoAcervo.get(tipoDoFiltro), parametros); for (LivroVO livroVO : listLivros) { livroVO.setAutores(buscaAutoresPorLivro(livroVO.getId())); } } return listLivros; } private String buscaAutoresPorLivro(Long idLivro) { Map<String, Object> parametros = new HashMap<String, Object>(); parametros.put("idLivro", idLivro); List<AutorVO> autores = hibernateDAO.findWithJpqlQuery(LivroVO.CONSULTA_AUTORES_POR_LIVRO, parametros); StringBuilder builderStrAutores = new StringBuilder(); for (AutorVO autorVO : autores) { builderStrAutores.append(autorVO).append("\n"); } return builderStrAutores.toString(); } public List<Emprestimo> buscaEmprestimoDoSolicitane(String matriculaDoAluno) { Map<String, Object> parametros = new HashMap<String, Object>(); parametros.put("matricula", matriculaDoAluno); return this.hibernateDAO.findWithNamedQuery(Pessoa.BUSCA_EMPRESTIMOS, parametros); } public void renovaEmprestimo(Emprestimo emprestimo) throws Exception { DateTime dataProgramadaDeDevolucao = new DateTime(emprestimo.getUltimaRenovacao().getDataDevolucao()); DateTime hoje = new DateTime(new Date()); int qtdeDiasMulta = Days.daysBetween(hoje, dataProgramadaDeDevolucao).getDays(); if (qtdeDiasMulta < 0) { throw new Exception( "<b>Emprstimos com multas no podem ser renovados!</b><br/>Esse emprstimo possui multa no valor de <b>R$ " + -1 * qtdeDiasMulta * 0.50 + "</b> referente a <b>" + (-1) * qtdeDiasMulta + " dias</b> de atraso"); } else if (emprestimo.getVezesEmprestimo().size() == 3) { throw new Exception( "<b>Quantidade mxima de Renovaes atingida!</b><br/>Cada emprstimo pode ser renovado somente por 2 vezes consecutivas."); } else if (!emprestimo.getTipoEmprestimo().equals(TipoEmprestimo.NORMAL)) { throw new Exception( "<b>Somente emprstimos normais podem ser renovados online</b>.<br>Dirija-se biblioteca para conversar com um dos atendentes."); } else { DateTime novaDataDeDevolucao = hoje.plusDays(10); Calendar calendar = Calendar.getInstance(); calendar.setTime(novaDataDeDevolucao.toDate()); int diaDaSemana = calendar.get(Calendar.DAY_OF_WEEK); if (diaDaSemana == 1) { novaDataDeDevolucao = novaDataDeDevolucao.plusDays(1); } else if (diaDaSemana == 7) { novaDataDeDevolucao = novaDataDeDevolucao.plusDays(2); } VezesEmprestimo ve = new VezesEmprestimo(); ve.setDataEmprestimo(new Date()); ve.setDataDevolucao(novaDataDeDevolucao.toDate()); ve.setFuncionario(emprestimo.getUltimaRenovacao().getFuncionario()); emprestimo.getVezesEmprestimo().add(ve); hibernateDAO.update(emprestimo); } } }