|
|
|
@@ -1,11 +1,22 @@ |
|
|
|
package vn.azteam.tpf.service.impl; |
|
|
|
|
|
|
|
import com.google.zxing.BarcodeFormat; |
|
|
|
import com.google.zxing.WriterException; |
|
|
|
import com.google.zxing.client.j2se.MatrixToImageWriter; |
|
|
|
import com.google.zxing.common.BitMatrix; |
|
|
|
import com.google.zxing.qrcode.QRCodeWriter; |
|
|
|
import io.github.jhipster.service.filter.LongFilter; |
|
|
|
import org.apache.commons.io.output.ByteArrayOutputStream; |
|
|
|
import org.dom4j.DocumentException; |
|
|
|
import org.jsoup.Jsoup; |
|
|
|
import org.jsoup.nodes.Document; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.data.domain.Pageable; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.transaction.annotation.Transactional; |
|
|
|
import org.xhtmlrenderer.layout.SharedContext; |
|
|
|
import org.xhtmlrenderer.pdf.ITextRenderer; |
|
|
|
import vn.azteam.tpf.domain.TBCode; |
|
|
|
import vn.azteam.tpf.domain.TBCodeDetails; |
|
|
|
import vn.azteam.tpf.domain.TBCodeStatusEnum; |
|
|
|
@@ -19,6 +30,11 @@ import vn.azteam.tpf.service.mapper.TBCropMapper; |
|
|
|
import vn.azteam.tpf.service.util.UserRoleUtil; |
|
|
|
import vn.azteam.tpf.web.rest.errors.BadRequestAlertException; |
|
|
|
|
|
|
|
import javax.imageio.ImageIO; |
|
|
|
import java.io.File; |
|
|
|
import java.io.FileInputStream; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.OutputStream; |
|
|
|
import java.time.Instant; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.Comparator; |
|
|
|
@@ -33,12 +49,16 @@ import java.util.stream.Collectors; |
|
|
|
@Service |
|
|
|
@Transactional |
|
|
|
public class TBCodeDetailsImpl implements TBCodeDetailsService { |
|
|
|
|
|
|
|
private static final String HTML_INPUT = "src/main/resources/html/template.html"; |
|
|
|
private final Logger log = LoggerFactory.getLogger(TBCodeDetailsImpl.class); |
|
|
|
|
|
|
|
private static final String ENTITY_NAME = "tBCrop"; |
|
|
|
|
|
|
|
private final TBCodeDetailsRepository tbCodeDetailsRepository; |
|
|
|
|
|
|
|
private final TBExampleStampService tbExampleStampService; |
|
|
|
|
|
|
|
private final TBCodeDetailsMapper tBCodeDetailsMapper; |
|
|
|
|
|
|
|
private final TBDetailUserRepository tBDetailUserRepository; |
|
|
|
@@ -55,18 +75,22 @@ public class TBCodeDetailsImpl implements TBCodeDetailsService { |
|
|
|
|
|
|
|
private final TBCropMapper tbCropMapper; |
|
|
|
|
|
|
|
private final FileStorageService fileStorageService; |
|
|
|
|
|
|
|
private final TBCodeDetailsSearchRepository tBCodeDetailsSearchRepository; |
|
|
|
|
|
|
|
public TBCodeDetailsImpl(TBCodeDetailsRepository tbCodeDetailsRepository, |
|
|
|
TBCodeDetailsMapper tBCodeDetailsMapper, |
|
|
|
TBExampleStampService tbExampleStampService, TBCodeDetailsMapper tBCodeDetailsMapper, |
|
|
|
TBDetailUserRepository tBDetailUserRepository, |
|
|
|
UserService userService, |
|
|
|
UserRoleUtil userRoleUtil, |
|
|
|
TBActivityQueryService tBActivityQueryService, TBCropService tbCropService, |
|
|
|
TBCropQueryService tBCropQueryService, |
|
|
|
TBCropMapper tbCropMapper, |
|
|
|
FileStorageService fileStorageService, |
|
|
|
TBCodeDetailsSearchRepository tBCodeDetailsSearchRepository) { |
|
|
|
this.tbCodeDetailsRepository = tbCodeDetailsRepository; |
|
|
|
this.tbExampleStampService = tbExampleStampService; |
|
|
|
this.tBCodeDetailsMapper = tBCodeDetailsMapper; |
|
|
|
this.tBDetailUserRepository = tBDetailUserRepository; |
|
|
|
this.userService = userService; |
|
|
|
@@ -75,6 +99,7 @@ public class TBCodeDetailsImpl implements TBCodeDetailsService { |
|
|
|
this.tbCropService = tbCropService; |
|
|
|
this.tBCropQueryService = tBCropQueryService; |
|
|
|
this.tbCropMapper = tbCropMapper; |
|
|
|
this.fileStorageService = fileStorageService; |
|
|
|
this.tBCodeDetailsSearchRepository = tBCodeDetailsSearchRepository; |
|
|
|
} |
|
|
|
|
|
|
|
@@ -276,4 +301,114 @@ public class TBCodeDetailsImpl implements TBCodeDetailsService { |
|
|
|
throw new RuntimeException(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public byte[] exportPdfInfoTBCodeDetail(Long tbCodeDetailId) { |
|
|
|
try { |
|
|
|
Optional<TBCodeDetails> tbCodeDetailsOptional = tbCodeDetailsRepository.findById(tbCodeDetailId); |
|
|
|
if (!tbCodeDetailsOptional.isPresent()) { |
|
|
|
throw new BadRequestAlertException("1047", "TB CodeDetail export could found", "1047"); |
|
|
|
} |
|
|
|
|
|
|
|
log.info("exportInfoTBCode: start query"); |
|
|
|
if (tbCodeDetailsOptional.isPresent()) { |
|
|
|
TBCodeDetails tbCodeDetails = tbCodeDetailsOptional.get(); |
|
|
|
if (tbCodeDetails.getTbCode().getTbExampleStampId() != null) { |
|
|
|
TBExampleStampDTO exampleStampDTO = tbExampleStampService.findById(tbCodeDetails.getTbCode().getTbExampleStampId()); |
|
|
|
|
|
|
|
return this.exportPdf(exampleStampDTO.getExampleStampImage(), |
|
|
|
tbCodeDetails.getCode(), exampleStampDTO.getWidth(), exampleStampDTO.getHeight(), |
|
|
|
exampleStampDTO.getX(), exampleStampDTO.getY()); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (Exception exception) { |
|
|
|
log.error("exportInfoCampaignCalling exception", exception); |
|
|
|
} |
|
|
|
return new byte[0]; |
|
|
|
} |
|
|
|
|
|
|
|
private static void xhtmlToPdf(Document xhtml, ByteArrayOutputStream byteOutputStream, |
|
|
|
byte[] background, byte[] barCode, Integer width, Integer height, |
|
|
|
Integer x, Integer y) throws Exception { |
|
|
|
try (OutputStream outputStream = byteOutputStream) { |
|
|
|
ITextRenderer renderer = new ITextRenderer(); |
|
|
|
SharedContext sharedContext = renderer.getSharedContext(); |
|
|
|
sharedContext.setPrint(true); |
|
|
|
sharedContext.setInteractive(false); |
|
|
|
sharedContext.setReplacedElementFactory(new CustomElementFactoryImpl(background, barCode)); |
|
|
|
String widthValue = width != null ? width.toString() + "px" : "302px"; |
|
|
|
String heightValue = height != null ? height.toString() + "px" : "302px"; |
|
|
|
String xValue = x != null ? x.toString() + "px" : "50px"; |
|
|
|
String yValue = y != null ? y.toString() + "px" : "51px"; |
|
|
|
renderer.setDocumentFromString(xhtml.html().replace("300px", widthValue) |
|
|
|
.replace("301px", heightValue).replace("50px", xValue) |
|
|
|
.replace("51px", yValue)); |
|
|
|
renderer.layout(); |
|
|
|
renderer.createPDF(outputStream); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static byte[] readFileToByteArray(File file) { |
|
|
|
byte[] byteArray = null; |
|
|
|
try (FileInputStream fis = new FileInputStream(file); |
|
|
|
ByteArrayOutputStream bos = new ByteArrayOutputStream()) { |
|
|
|
|
|
|
|
byte[] buffer = new byte[4096]; |
|
|
|
int bytesRead; |
|
|
|
while ((bytesRead = fis.read(buffer)) != -1) { |
|
|
|
bos.write(buffer, 0, bytesRead); |
|
|
|
} |
|
|
|
|
|
|
|
byteArray = bos.toByteArray(); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
return byteArray; |
|
|
|
} |
|
|
|
|
|
|
|
private void generateHtmlToPdf(ByteArrayOutputStream byteOutputStream, |
|
|
|
byte[] background, byte[] barCode, |
|
|
|
Integer width, Integer height, |
|
|
|
Integer x, Integer y) throws Exception { |
|
|
|
File inputHTML = new File(HTML_INPUT); |
|
|
|
Document inputHtml = createWellFormedHtml(inputHTML); |
|
|
|
log.info("generateHtmlToPdf"); |
|
|
|
xhtmlToPdf(inputHtml, byteOutputStream, background, barCode, width, height, x, y); |
|
|
|
} |
|
|
|
|
|
|
|
private static Document createWellFormedHtml(File inputHTML) throws IOException { |
|
|
|
Document document = Jsoup.parse(inputHTML, "UTF-8"); |
|
|
|
document.outputSettings() |
|
|
|
.syntax(Document.OutputSettings.Syntax.xml); |
|
|
|
return document; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public byte[] exportPdf(String image, String tbCodeDetailValue, Integer width, Integer height, |
|
|
|
Integer x, Integer y) throws IOException, |
|
|
|
DocumentException, WriterException { |
|
|
|
log.info("exportPdf"); |
|
|
|
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); |
|
|
|
try { |
|
|
|
|
|
|
|
ByteArrayOutputStream byteArrayOutputStreamBarCode = new ByteArrayOutputStream(); |
|
|
|
QRCodeWriter qrCodeWriter = new QRCodeWriter(); |
|
|
|
BitMatrix matrix = qrCodeWriter.encode(tbCodeDetailValue, BarcodeFormat.QR_CODE, |
|
|
|
200, 200); |
|
|
|
ImageIO.write(MatrixToImageWriter.toBufferedImage(matrix), "jpg", byteArrayOutputStreamBarCode); |
|
|
|
|
|
|
|
File backgroundImage = fileStorageService.loadFileAsResource("EXAMPLE_STAMP", |
|
|
|
image.replace("EXAMPLE_STAMP/", "")).getFile(); |
|
|
|
log.info("generateHtmlToPdf"); |
|
|
|
generateHtmlToPdf(byteOutputStream, |
|
|
|
readFileToByteArray(backgroundImage), byteArrayOutputStreamBarCode.toByteArray(), |
|
|
|
width, height, x, y); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
log.info("generateHtmlToPdf Exception"); |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
return byteOutputStream.toByteArray(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |