Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 648 | blopes | 1 | package br.com.ec.controller; |
| 2 | |||
| 3 | import java.io.File; |
||
| 4 | import java.io.FileOutputStream; |
||
| 5 | import java.io.IOException; |
||
| 6 | import java.io.Serializable; |
||
| 7 | import java.util.List; |
||
| 8 | |||
| 9 | import javax.annotation.PostConstruct; |
||
| 10 | import javax.inject.Inject; |
||
| 11 | import javax.inject.Named; |
||
| 12 | |||
| 13 | import org.primefaces.event.FileUploadEvent; |
||
| 14 | import org.primefaces.model.file.UploadedFile; |
||
| 15 | import org.springframework.context.annotation.Scope; |
||
| 16 | |||
| 17 | import br.com.ec.core.exception.NegocioException; |
||
| 18 | import br.com.ec.core.generic.GenericService; |
||
| 19 | import br.com.ec.core.util.ArquivoUtil; |
||
| 20 | import br.com.ec.core.util.VerificadorUtil; |
||
| 21 | import br.com.ec.domain.dto.ImagemDTO; |
||
| 22 | import br.com.ec.domain.dto.VigenciaDTO; |
||
| 23 | import br.com.ec.domain.model.Avaliacao; |
||
| 24 | import br.com.ec.domain.model.AvaliacaoProduto; |
||
| 25 | import br.com.ec.domain.model.Vigencia; |
||
| 26 | import br.com.ec.domain.service.AvaliacaoService; |
||
| 27 | import br.com.ec.domain.service.ImagemService; |
||
| 28 | import br.com.ec.domain.service.VigenciaService; |
||
| 29 | import br.com.ec.web.exception.VerificadorLancamentoException; |
||
| 30 | import br.com.ec.web.exception.VerificadorLancamentoException.CommandBean; |
||
| 31 | import br.com.ec.web.generic.AbstractBean; |
||
| 32 | import br.com.ec.web.message.LancadorMensagem; |
||
| 33 | import br.com.ec.web.util.TipoOperacao; |
||
| 34 | |||
| 35 | @Named |
||
| 36 | @Scope("view") |
||
| 37 | public class GestaoMensalArquivosBean extends AbstractBean<Vigencia> implements Serializable { |
||
| 38 | |||
| 39 | private static final long serialVersionUID = 1L; |
||
| 40 | |||
| 41 | private VigenciaService vigenciaService; |
||
| 42 | private AvaliacaoService avaliacaoService; |
||
| 43 | private ImagemService imagemService; |
||
| 44 | |||
| 45 | private VigenciaDTO vigenciaSelecionada; |
||
| 46 | |||
| 47 | private List<AvaliacaoProduto> produtosPromocaoInterna; |
||
| 48 | private String codigoProdutoParaVincular; |
||
| 49 | |||
| 50 | private ImagemDTO imagemDTO; |
||
| 51 | private UploadedFile imagemParaUpload; |
||
| 52 | |||
| 53 | @Inject |
||
| 54 | public GestaoMensalArquivosBean(VigenciaService vigenciaService, AvaliacaoService avaliacaoService, ImagemService imagemService) { |
||
| 55 | this.vigenciaService = vigenciaService; |
||
| 56 | this.avaliacaoService = avaliacaoService; |
||
| 57 | this.imagemService = imagemService; |
||
| 58 | } |
||
| 59 | |||
| 60 | @Override |
||
| 61 | public void preCarregamento() { |
||
| 62 | limparEntidade(); |
||
| 63 | } |
||
| 64 | |||
| 65 | @PostConstruct |
||
| 66 | private void posCarregamento() { |
||
| 67 | if (VerificadorUtil.estaNulo(vigenciaSelecionada)) { |
||
| 68 | if (VerificadorUtil.naoEstaNulo(getParametro("vigencia"))) { |
||
| 69 | setVigenciaSelecionada(vigenciaService.consultarVigencia(new Long(getParametro("vigencia").toString()))); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | } |
||
| 73 | |||
| 74 | @Override |
||
| 75 | public void limparEntidade() { |
||
| 76 | setEntidade(new Vigencia()); |
||
| 77 | } |
||
| 78 | |||
| 79 | @Override |
||
| 80 | public GenericService<Vigencia> getService() { |
||
| 81 | return vigenciaService; |
||
| 82 | } |
||
| 83 | |||
| 84 | @Override |
||
| 85 | public Vigencia getEntidade() { |
||
| 86 | return entidade; |
||
| 87 | } |
||
| 88 | |||
| 89 | @Override |
||
| 90 | public void preConsultar() { |
||
| 91 | setTipoOperacao(TipoOperacao.CONSULTAR); |
||
| 92 | } |
||
| 93 | |||
| 94 | @Override |
||
| 95 | public Vigencia getId() { |
||
| 96 | return getEntidade(); |
||
| 97 | } |
||
| 98 | |||
| 99 | public VigenciaDTO getVigenciaSelecionada() { |
||
| 100 | return vigenciaSelecionada; |
||
| 101 | } |
||
| 102 | public void setVigenciaSelecionada(VigenciaDTO vigenciaSelecionada) { |
||
| 103 | this.vigenciaSelecionada = vigenciaSelecionada; |
||
| 104 | } |
||
| 105 | |||
| 106 | public List<AvaliacaoProduto> getProdutosPromocaoInterna() { |
||
| 107 | return produtosPromocaoInterna; |
||
| 108 | } |
||
| 109 | public void setProdutosPromocaoInterna(List<AvaliacaoProduto> produtosPromocaoInterna) { |
||
| 110 | this.produtosPromocaoInterna = produtosPromocaoInterna; |
||
| 111 | } |
||
| 112 | |||
| 113 | public String getCodigoProdutoParaVincular() { |
||
| 114 | return codigoProdutoParaVincular; |
||
| 115 | } |
||
| 116 | public void setCodigoProdutoParaVincular(String codigoProdutoParaVincular) { |
||
| 117 | this.codigoProdutoParaVincular = codigoProdutoParaVincular; |
||
| 118 | } |
||
| 119 | |||
| 120 | public ImagemDTO getImagemDTO() { |
||
| 121 | return imagemDTO; |
||
| 122 | } |
||
| 123 | public void setImagemDTO(ImagemDTO imagemDTO) { |
||
| 124 | this.imagemDTO = imagemDTO; |
||
| 125 | } |
||
| 126 | |||
| 127 | public UploadedFile getImagemParaUpload() { |
||
| 128 | return imagemParaUpload; |
||
| 129 | } |
||
| 130 | public void setImagemParaUpload(UploadedFile imagemParaUpload) { |
||
| 131 | this.imagemParaUpload = imagemParaUpload; |
||
| 132 | } |
||
| 133 | |||
| 134 | /***************************************************************/ |
||
| 135 | |||
| 136 | public void uploadImagemPromocoes(final FileUploadEvent e) { |
||
| 137 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 138 | public void execute() { |
||
| 139 | uploadImagem(e, "promocoes_"); |
||
| 140 | } |
||
| 141 | }); |
||
| 142 | } |
||
| 143 | |||
| 144 | public void uploadImagemEscala(final FileUploadEvent e) { |
||
| 145 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 146 | public void execute() { |
||
| 147 | uploadImagem(e, "escala_"); |
||
| 148 | } |
||
| 149 | }); |
||
| 150 | } |
||
| 151 | |||
| 152 | public void uploadImagemDestaques(final FileUploadEvent e) { |
||
| 153 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 154 | public void execute() { |
||
| 155 | uploadImagem(e, "destaques_"); |
||
| 156 | } |
||
| 157 | }); |
||
| 158 | } |
||
| 159 | |||
| 160 | private void uploadImagem(final FileUploadEvent e, String caminho) { |
||
| 161 | ImagemDTO imagemAdicionada = gerarArquivo(e.getFile()); |
||
| 162 | imagemAdicionada.setDescricao(caminho + getVigenciaSelecionada().getSequencial()); |
||
| 163 | // imagemAdicionada.compactarImagem(); |
||
| 164 | // imagemAdicionada.converterPngParaJpg(); |
||
| 165 | lancarExcecaoCasoArquivoExcedeuLimite(imagemAdicionada); |
||
| 166 | setImagemParaUpload(e.getFile()); |
||
| 167 | uploadFisico(imagemAdicionada); |
||
| 168 | } |
||
| 169 | |||
| 170 | public ImagemDTO gerarArquivo(UploadedFile file) { |
||
| 171 | ImagemDTO arquivo = new ImagemDTO(); |
||
| 172 | arquivo.setArquivo(file.getContent()); |
||
| 173 | arquivo.setDescricao(file.getFileName()); |
||
| 174 | arquivo.setExtensao(ArquivoUtil.retornarExtensaoDoArquivo(file.getFileName())); |
||
| 175 | return arquivo; |
||
| 176 | } |
||
| 177 | |||
| 178 | private void lancarExcecaoCasoArquivoExcedeuLimite(ImagemDTO arquivoGerado) { |
||
| 179 | if (arquivoGerado.isExcedeuLimite()) { |
||
| 180 | throw new NegocioException("Arquivo: " + arquivoGerado.getDescricao() + " excedeu o limite de 3000 kbytes. "); |
||
| 181 | } |
||
| 182 | } |
||
| 183 | |||
| 184 | public void uploadFisico(ImagemDTO arquivoGerado) { |
||
| 185 | String caminho = imagemService.caminhoPastaImagens() + arquivoGerado.getDescricao() + "." + arquivoGerado.getExtensao(); |
||
| 186 | FileOutputStream fos; |
||
| 187 | try { |
||
| 188 | fos = new FileOutputStream(caminho); |
||
| 189 | fos.write(arquivoGerado.getArquivo()); |
||
| 190 | fos.close(); |
||
| 191 | } catch (Exception e1) { |
||
| 192 | e1.printStackTrace(); |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | public void removerImagemPromocoes() { |
||
| 197 | String caminhoPromocao = getVigenciaSelecionada().getImagemPromocoes(); |
||
| 198 | if (!caminhoPromocao.contains("SEM")) { |
||
| 199 | File imagem = new File(caminhoPromocao); |
||
| 200 | imagem.delete(); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | public void removerImagemEscala() { |
||
| 205 | String caminhoPromocao = getVigenciaSelecionada().getImagemEscala(); |
||
| 206 | if (!caminhoPromocao.contains("SEM")) { |
||
| 207 | File imagem = new File(caminhoPromocao); |
||
| 208 | imagem.delete(); |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | public void removerImagemDestaques() { |
||
| 213 | String caminhoPromocao = getVigenciaSelecionada().getImagemDestaques(); |
||
| 214 | if (!caminhoPromocao.contains("SEM")) { |
||
| 215 | File imagem = new File(caminhoPromocao); |
||
| 216 | imagem.delete(); |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | /**************************************************/ |
||
| 221 | |||
| 222 | public void redirecionarGestaoMensal() { |
||
| 223 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 224 | public void execute() { |
||
| 225 | try { |
||
| 226 | LancadorMensagem.redirecionarTela("/sistema/gestao_mensal/gestao_mensal.xhtml"); |
||
| 227 | } catch (IOException e) { |
||
| 228 | e.printStackTrace(); |
||
| 229 | } |
||
| 230 | } |
||
| 231 | }); |
||
| 232 | } |
||
| 233 | |||
| 234 | public void consultarProdutosPromocaoInterna() { |
||
| 235 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 236 | public void execute() { |
||
| 237 | setProdutosPromocaoInterna(avaliacaoService.consultarProdutosPromocaoInterna(getEntidade().getSequencial())); |
||
| 238 | } |
||
| 239 | }); |
||
| 240 | } |
||
| 241 | |||
| 242 | public void vincularProdutoPromocaoInterna() { |
||
| 243 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 244 | public void execute() { |
||
| 245 | // avaliacaoService.cadastrarProdutoPromocaoInterna(getEntidade(), getCodigoProdutoParaVincular()); |
||
| 246 | // setProdutosPromocaoInterna(avaliacaoService.consultarProdutosPromocaoInterna(getEntidade().getSequencial())); |
||
| 247 | // LancadorMensagem.lancarSucesso("PRODUTO " + getCodigoProdutoParaVincular() + " VINCULADO COM SUCESSO"); |
||
| 248 | // setCodigoProdutoParaVincular(null); |
||
| 249 | } |
||
| 250 | }); |
||
| 251 | } |
||
| 252 | |||
| 253 | public void desvincularProdutoPromocaoInterna(final AvaliacaoProduto avaliacaoProduto) { |
||
| 254 | new VerificadorLancamentoException().tratarIhRelancarExcecaoSemLimparEntidade(new CommandBean() { |
||
| 255 | public void execute() { |
||
| 256 | avaliacaoService.excluirProdutoPromocaoInterna(avaliacaoProduto); |
||
| 257 | setProdutosPromocaoInterna(avaliacaoService.consultarProdutosPromocaoInterna(getEntidade().getSequencial())); |
||
| 258 | LancadorMensagem.lancarSucesso("PRODUTO DESVINCULADO COM SUCESSO"); |
||
| 259 | } |
||
| 260 | }); |
||
| 261 | } |
||
| 262 | |||
| 263 | } |