package br.com.ec.domain.service.etiqueta.impl;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.Document;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lowagie.text.pdf.Barcode;
import com.lowagie.text.pdf.BarcodeEAN;
import br.com.ec.core.generic.AbstractService;
import br.com.ec.core.generic.GenericRepository;
import br.com.ec.core.util.VerificadorUtil;
import br.com.ec.core.validador.Validador;
import br.com.ec.domain.dto.EtiquetaDTO;
import br.com.ec.domain.service.etiqueta.EtiquetaService;
import br.com.ec.domain.shared.ConstantesSEC;
import br.com.ec.domain.util.CodigoBarraEAN;
@Service
public class EtiquetaServiceImpl
extends AbstractService
<EtiquetaDTO
> implements EtiquetaService
{
@Autowired
public EtiquetaServiceImpl
(Validador validador
) {
super(validador
);
}
@
Override
protected GenericRepository
<EtiquetaDTO
> getRepository
() {
return null;
}
@
Override
public StreamedContent gerarEtiquetas
(List<EtiquetaDTO
> etiquetas, ServletContext contexto,
Boolean comBarras,
Boolean comAtacado
) {
ByteArrayInputStream input =
new ByteArrayInputStream(gerarAquivos
(etiquetas, contexto, comBarras, comAtacado
));
return new DefaultStreamedContent
(input,
"application/zip",
"paginas.zip");
}
private byte[] gerarAquivos
(List<EtiquetaDTO
> etiquetas, ServletContext contexto,
Boolean comBarras,
Boolean comAtacado
) {
ByteArrayOutputStream bytesOut =
new ByteArrayOutputStream();
ZipOutputStream out =
new ZipOutputStream(bytesOut
);
try {
List<EtiquetaDTO
> paginaEtiquetas =
new ArrayList<EtiquetaDTO
>();
int quantidadePagina =
0;
for (EtiquetaDTO etiqueta : etiquetas
) {
paginaEtiquetas.
add(etiqueta
);
if (paginaEtiquetas.
size() == ConstantesSEC.
Etiqueta.
QUANTIDADE_MAXIMA_POR_FOLHA) {
quantidadePagina++
;
byte[] input = gerarPagina
(paginaEtiquetas, contexto, comBarras, comAtacado
);
adicionarPaginaNoZip
(input, quantidadePagina, out
);
paginaEtiquetas.
clear();
}
}
if (!paginaEtiquetas.
isEmpty()) {
quantidadePagina++
;
byte[] input = gerarPagina
(paginaEtiquetas, contexto, comBarras, comAtacado
);
adicionarPaginaNoZip
(input, quantidadePagina, out
);
}
} finally {
try {
out.
close();
} catch (IOException e
) {
e.
printStackTrace();
}
}
return bytesOut.
toByteArray();
}
private void adicionarPaginaNoZip
(byte[] input,
int quantidadePagina,
ZipOutputStream out
) {
try {
ZipEntry newEntry =
new ZipEntry("pagina_" + quantidadePagina +
".docx");
out.
putNextEntry(newEntry
);
out.
write(input,
0, input.
length);
out.
closeEntry();
} catch (Exception e
) {
e.
printStackTrace();
}
}
public static void main
(String[] args
) {
/*
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("barcodes.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
Barcode39 code39 = new Barcode39();
code39.setCode("CODE39-1234567890");
code39.setStartStopText(false);
Image image39 = code39.createImageWithBarcode(cb, null, null);
Barcode39 code39ext = new Barcode39();
code39ext.setCode("The willows.");
code39ext.setStartStopText(false);
code39ext.setExtended(true);
Image image39ext = code39ext.createImageWithBarcode(cb, null, null);
Barcode128 code128 = new Barcode128();
code128.setCode("1Z234786 hello");
Image image128 = code128.createImageWithBarcode(cb, null, null);
BarcodeEAN codeEAN = new BarcodeEAN();
codeEAN.setCodeType(Barcode.EAN13);
codeEAN.setCode("9000000015883");
Image imageEAN = codeEAN.createImageWithBarcode(cb, null, null);
BarcodeInter25 code25 = new BarcodeInter25();
code25.setGenerateChecksum(true);
code25.setCode("41-1200076041-001");
Image image25 = code25.createImageWithBarcode(cb, null, null);
BarcodePostnet codePost = new BarcodePostnet();
codePost.setCode("12345");
Image imagePost = codePost.createImageWithBarcode(cb, null, null);
BarcodePostnet codePlanet = new BarcodePostnet();
codePlanet.setCode("50201402356");
codePlanet.setCodeType(Barcode.PLANET);
Image imagePlanet = codePlanet.createImageWithBarcode(cb, null, null);
BarcodeEAN codeSUPP = new BarcodeEAN();
codeSUPP.setCodeType(Barcode.SUPP5);
codeSUPP.setCode("54995");
codeSUPP.setBaseline(-2);
BarcodeEANSUPP eanSupp = new BarcodeEANSUPP(codeEAN, codeSUPP);
Image imageEANSUPP = eanSupp.createImageWithBarcode(cb, null, Color.blue);
PdfPTable table = new PdfPTable(2);
table.setWidthPercentage(100);
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
table.getDefaultCell().setFixedHeight(70);
table.addCell("CODE 39");
table.addCell(new Phrase(new Chunk(image39, 0, 0)));
table.addCell("CODE 39 EXTENDED");
table.addCell(new Phrase(new Chunk(image39ext, 0, 0)));
table.addCell("CODE 128");
table.addCell(new Phrase(new Chunk(image128, 0, 0)));
table.addCell("CODE EAN");
table.addCell(new Phrase(new Chunk(imageEAN, 0, 0)));
table.addCell("CODE EAN\nWITH\nSUPPLEMENTAL 5");
table.addCell(new Phrase(new Chunk(imageEANSUPP, 0, 0)));
table.addCell("CODE INTERLEAVED");
table.addCell(new Phrase(new Chunk(image25, 0, 0)));
table.addCell("CODE POSTNET");
table.addCell(new Phrase(new Chunk(imagePost, 0, 0)));
table.addCell("CODE PLANET");
table.addCell(new Phrase(new Chunk(imagePlanet, 0, 0)));
document.add(table);
}
catch (Exception de) {
de.printStackTrace();
}
// step 5: we close the document
document.close();
*/
}
private byte[] gerarPagina2
(List<EtiquetaDTO
> etiquetas, ServletContext contexto
) {
ByteArrayOutputStream baos =
new ByteArrayOutputStream();
XWPFDocument doc
;
try {
doc =
new XWPFDocument
(OPCPackage.
open(contexto.
getResourceAsStream("/arquivos/codigos.docx")));
XWPFParagraph par = doc.
createParagraph();
XWPFRun run = par.
createRun();
InputStream pic = contexto.
getResourceAsStream("/imagens/bandeira.jpg");
BarcodeEAN codeEAN =
new BarcodeEAN
();
codeEAN.
setCodeType(Barcode.
EAN13);
codeEAN.
setCode("5901234123457");
/*
Image imageEAN = codeEAN.createAwtImage(Color.WHITE, Color.BLACK);
BufferedImage bi = new BufferedImage(imageEAN.getWidth(null), imageEAN.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics bg = bi.getGraphics();
bg.drawImage(imageEAN, 0, 0, null);
bg.dispose();
ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", baos2 );
baos2.flush();
byte[] imageInByte = baos2.toByteArray();
baos2.close();
InputStream is = new ByteArrayInputStream(imageInByte);
int format = XWPFDocument.PICTURE_TYPE_JPEG;
run.addBreak();
run.addPicture(is, format, "bandeira", Units.toEMU(226), Units.toEMU(135));
run.addBreak(BreakType.PAGE);
*/
doc.
write(baos
);
byte[] input = baos.
toByteArray();
return input
;
} catch (Exception e
) {
e.
printStackTrace();
}
return null;
}
private byte[] gerarPagina
(List<EtiquetaDTO
> etiquetas, ServletContext contexto,
Boolean comBarras,
Boolean comAtacado
) {
ByteArrayOutputStream baos =
new ByteArrayOutputStream();
XWPFDocument doc
;
try {
if (comBarras
) {
if (comAtacado
) {
doc =
new XWPFDocument
(OPCPackage.
open(contexto.
getResourceAsStream("/arquivos/codigos_barras_atacado.docx")));
} else {
doc =
new XWPFDocument
(OPCPackage.
open(contexto.
getResourceAsStream("/arquivos/codigos_barras.docx")));
}
} else {
doc =
new XWPFDocument
(OPCPackage.
open(contexto.
getResourceAsStream("/arquivos/codigos.docx")));
}
Integer contador =
0;
for (XWPFTable tbl : doc.
getTables()) {
for (XWPFTableRow row : tbl.
getRows()) {
for (XWPFTableCell cell : row.
getTableCells()) {
if (etiquetas.
size() > contador
) {
EtiquetaDTO etiqueta = etiquetas.
get(contador
);
contador++
;
for (XWPFParagraph p : cell.
getParagraphs()) {
for (XWPFRun r : p.
getRuns()) {
String text = r.
getText(0);
if (text
!=
null && text.
contains("#")) {
String valor =
"";
if (comAtacado
) {
if (VerificadorUtil.
naoEstaNuloOuVazio(etiqueta.
getProduto().
getValorVarejoPopular())) {
valor = etiqueta.
getProduto().
getValorVarejoPopular().
toString();
} else {
valor =
"--";
}
} else {
if (VerificadorUtil.
naoEstaNuloOuVazio(etiqueta.
getProduto().
getValorVarejo())) {
valor = etiqueta.
getProduto().
getValorVarejo().
toString();
} else {
valor =
"--";
}
}
valor = valor.
replace(".0",
",00");
text = text.
replace("#", valor
);
r.
setText(text,
0);
if (!comBarras
) {
break;
}
}
if (text
!=
null && text.
contains("&")) {
String valor =
"";
if (comAtacado
&& VerificadorUtil.
naoEstaNuloOuVazio(etiqueta.
getProduto().
getValorAtacado())) {
valor = etiqueta.
getProduto().
getValorAtacado().
toString();
} else {
valor =
"--";
}
valor = valor.
replace(".0",
"00");
text = text.
replace("&", valor
);
r.
setText(text,
0);
if (!comBarras
) {
break;
}
}
if (text
!=
null && text.
contains("@")) {
if (VerificadorUtil.
naoEstaNuloOuVazio(etiqueta.
getProduto().
getDescricaoEtiqueta())) {
text = text.
replace("@", etiqueta.
getProduto().
getDescricaoEtiqueta());
} else {
text = text.
replace("@", etiqueta.
getProduto().
getModelo().
getDescricaoEtiqueta());
}
r.
setText(text,
0);
}
if (text
!=
null && text.
contains("!")) {
text = text.
replace("!", etiqueta.
getProduto().
getCodigoProdutoPadrao());
if (VerificadorUtil.
naoEstaNulo(etiqueta.
getLote())) {
text = text +
" [" + etiqueta.
getLote() +
"]";
}
r.
setText(text,
0);
}
if (text
!=
null && text.
contains("%")) {
BarcodeEAN codeEAN =
new BarcodeEAN
();
codeEAN.
setCodeType(Barcode.
EAN13);
codeEAN.
setCode(CodigoBarraEAN.
gerarCodigoBarras(etiqueta.
getProduto().
getCodigo()));
Image imageEAN = codeEAN.
createAwtImage(Color.
BLACK,
Color.
WHITE);
BufferedImage bi =
new BufferedImage(imageEAN.
getWidth(null), imageEAN.
getHeight(null),
BufferedImage.
TYPE_INT_RGB);
Graphics bg = bi.
getGraphics();
bg.
drawImage(imageEAN,
0,
0,
null);
bg.
dispose();
ByteArrayOutputStream baos2 =
new ByteArrayOutputStream();
ImageIO.
write(bi,
"jpg", baos2
);
baos2.
flush();
byte[] imageInByte = baos2.
toByteArray();
baos2.
close();
InputStream is =
new ByteArrayInputStream(imageInByte
);
// width - height
r.
addPicture(is,
Document.
PICTURE_TYPE_JPEG,
"barras.jpg", Units.
toEMU(65), Units.
toEMU(12));
r.
setText("",
0);
is.
close();
if (comBarras
) {
break;
}
}
}
}
} else {
for (XWPFParagraph p : cell.
getParagraphs()) {
for (XWPFRun r : p.
getRuns()) {
String text = r.
getText(0);
if (text
!=
null && text.
contains("!")) {
text = text.
replace("!",
"............");
r.
setText(text,
0);
}
if (text
!=
null && text.
contains("@")) {
text = text.
replace("@",
"...........................");
r.
setText(text,
0);
}
if (text
!=
null && text.
contains("#")) {
text = text.
replace("#",
"................");
r.
setText(text,
0);
break;
}
}
}
}
}
}
}
doc.
write(baos
);
byte[] input = baos.
toByteArray();
return input
;
} catch (Exception e
) {
e.
printStackTrace();
}
return null;
}
}