| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- package es.uv.saic.service;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.io.Reader;
- import java.io.UncheckedIOException;
- import java.math.BigInteger;
- import java.nio.file.Files;
- import java.text.DecimalFormat;
- import java.util.Base64;
- import java.util.List;
- import java.util.Optional;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- import org.jsoup.nodes.Element;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.core.io.DefaultResourceLoader;
- import org.springframework.core.io.Resource;
- import org.springframework.core.io.ResourceLoader;
- import org.springframework.stereotype.Service;
- import org.springframework.util.FileCopyUtils;
- import com.fasterxml.jackson.core.JsonParseException;
- import com.fasterxml.jackson.core.type.TypeReference;
- import com.fasterxml.jackson.databind.JsonMappingException;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import es.uv.saic.shared.domain.Plantilla;
- import es.uv.saic.shared.domain.PlantillaComentario;
- import es.uv.saic.shared.domain.PlantillaConversation;
- import es.uv.saic.shared.domain.PlantillaRepository;
- import es.uv.saic.shared.dto.InstanciaTascaDTO;
- import es.uv.saic.shared.feign.IndicadorClient;
- import es.uv.saic.shared.feign.OrganClient;
- import es.uv.saic.shared.feign.ProceduresClient;
- import es.uv.saic.shared.feign.TascaClient;
- @Service
- public class PlantillaService {
- @Autowired
- private PlantillaRepository r;
-
- @Value("${saic.data.filePath}")
- private String filePath;
- @Value("${saic.data.tmpPath}")
- private String tmpPath;
- @Value("${saic.data.templates.fileNotFound}")
- private String fileNotFound;
- @Value("${saic.data.templates.filePath}")
- private String templatePath;
- @Value("${saic.data.templates.logoPath}")
- private String logoPath;
-
- @Autowired
- private TascaClient tc;
-
- @Autowired
- private OrganClient oc;
- @Autowired
- private ProceduresClient pc;
- @Autowired
- private IndicadorClient ic;
-
- private static final DecimalFormat df = new DecimalFormat("0.00");
-
- public List<Plantilla> findAll(){
- return this.r.findAll();
- }
-
- public Plantilla findByID(Integer id) {
- return this.r.findByIdPlantilla(id);
- }
-
- public Plantilla findByVersioCodiAmbit(Integer versio, String codi, String ambit) {
- return this.r.findByVersioCodiAmbit(versio, codi, ambit);
- }
-
- public void save(Plantilla p) {
- this.r.save(p);
- this.r.flush();
- }
-
- public void delete(Plantilla p) {
- this.r.delete(p);
- }
-
- public boolean isUsed(Integer idPlantilla) {
- List<Plantilla> l = this.r.findUsedByIdPlantilla(idPlantilla);
- if(l.size() > 0) {
- return true;
- }
- return false;
- }
-
-
- public Plantilla findEvidencia(Integer codiEvidencia, Integer titulacio, String tambit) {
- Plantilla p = this.findByID(codiEvidencia);
- Plantilla p2 = null;
-
- if(titulacio > 0) {
- if(!p.getAmbit().equals(tambit)) {
- p2 = this.findByVersioCodiAmbit(p.getVersio(), p.getCodi(), tambit);
- }
- }
-
- if(p2 != null) {
- p = p2;
- }
- return p;
- }
-
-
- public String savePDF(String content, BigInteger idtascai) throws IOException, InterruptedException {
- content = content.replace("<p><!-- pagebreak --></p>", "<p class=\"pagebreak\"><!-- pagebreak --></p>");
- Document d = Jsoup.parse(content, "UTF8");
- d.head().append("<style>"
- + "@page{size: 297mm 210mm;} "
- + "html{ max-width:297mm; max-height:210mm; margin:0; padding:0; transform:scale(0.925); transform-origin:left top; } "
- + "table { padding:2px !important; }"
- + "th, td { padding:2px !important; }"
- + ".pagebreak { page-break-before:always; }"
- + "</style>");
- List<Element> trs = d.getElementsByTag("tr");
- for(Element t : trs) {
- if(t.hasAttr("height")) {
- t.removeAttr("height");
- }
- if(t.hasAttr("style")) {
- String style = t.attr("style");
- style = style.replaceAll("height:[ \\d.pxcmin]{1,};", "");
- t.attr("style", style);
- }
- }
- List<Element> tds = d.getElementsByTag("td");
- for(Element t : tds) {
- if(t.hasAttr("height")) {
- t.removeAttr("height");
- }
- if(t.hasAttr("style")) {
- String style = t.attr("style");
- style = style.replaceAll("height:[ \\d.pxcmin]{1,};", "");
- t.attr("style", style);
- }
- }
- List<Element> ignore = d.getElementsByClass("pdfignore");
- for(Element e: ignore) {
- e.remove();
- }
-
- InstanciaTascaDTO ita = tc.findInstanciaTascaById(idtascai);
- if(ita.getTasca().getNomRol().equals("u_uq")) {
- d.body().append(this.parseComments(d.html()));
- }
-
- String basecommand = "google-chrome --headless --disable-gpu --no-pdf-header-footer --run-all-compositor-stages-before-draw --no-sandbox";
- String dst = idtascai.toString()+".pdf";
- File src = File.createTempFile("saic-pdfexport-", ".tmp.html", new File(tmpPath));
- src.deleteOnExit();
- PrintWriter out = new PrintWriter(src.getAbsolutePath());
- out.println(d.html());
- out.flush();
- out.close();
- System.out.println(basecommand+" --print-to-pdf='"+filePath+dst+"' "+src.getAbsolutePath());
-
- ProcessBuilder pb = new ProcessBuilder("bash", "-c", basecommand+" --print-to-pdf='"+filePath+dst+"' "+src.getAbsolutePath());
- Process pr = pb.start();
- pr.waitFor();
- src.delete();
-
- return dst;
- }
-
- public byte[] toPDF(String content, Optional<BigInteger> idtascai) throws IOException, InterruptedException {
- content = content.replace("<p><!-- pagebreak --></p>", "<p class=\"pagebreak\"><!-- pagebreak --></p>");
- Document d = Jsoup.parse(content, "UTF8");
- d.head().append("<style>"
- + "@page{size: 297mm 210mm;} "
- + "html{ max-width:297mm; max-height:210mm; margin:0; padding:0; transform:scale(0.925); transform-origin:left top; } "
- + "table { padding:2px !important; }"
- + "th, td { padding:2px !important; }"
- + ".pagebreak { page-break-before:always; }"
- + "</style>");
- List<Element> trs = d.getElementsByTag("tr");
- for(Element t : trs) {
- if(t.hasAttr("height")) {
- t.removeAttr("height");
- }
- if(t.hasAttr("style")) {
- String style = t.attr("style");
- style = style.replaceAll("height:[ \\d.pxcmin]{1,};", "");
- t.attr("style", style);
- }
- }
- List<Element> tds = d.getElementsByTag("td");
- for(Element t : tds) {
- if(t.hasAttr("height")) {
- t.removeAttr("height");
- }
- if(t.hasAttr("style")) {
- String style = t.attr("style");
- style = style.replaceAll("height:[ \\d.pxcmin]{1,};", "");
- t.attr("style", style);
- }
- }
- List<Element> ignore = d.getElementsByClass("pdfignore");
- for(Element e: ignore) {
- e.remove();
- }
-
- if(idtascai.isPresent()) {
- InstanciaTascaDTO ita = tc.findInstanciaTascaById(idtascai.get());
- if(ita.getTasca().getNomRol().equals("u_uq")) {
- d.body().append(this.parseComments(d.html()));
- }
- }
-
- d.body().append(this.parseComments(d.html()));
-
- String basecommand = "google-chrome --headless --disable-gpu --no-pdf-header-footer --run-all-compositor-stages-before-draw --no-sandbox";
- File dst = File.createTempFile("saic-pdfpreview-", ".tmp.pdf", new File(tmpPath));
- File src = File.createTempFile("saic-pdfpreview-", ".tmp.html", new File(tmpPath));
- src.deleteOnExit();
- dst.deleteOnExit();
-
- PrintWriter out = new PrintWriter(src.getAbsolutePath());
- out.println(d.html());
- out.flush();
- out.close();
- ProcessBuilder pb = new ProcessBuilder("bash", "-c", basecommand+" --print-to-pdf='"+dst+"' "+src.getAbsolutePath());
- Process pr = pb.start();
- pr.waitFor();
-
- byte[] bytes = Files.readAllBytes(dst.toPath());
- src.delete();
- dst.delete();
-
- return bytes;
- }
-
- private String parseComments(String content) throws JsonParseException, JsonMappingException, IOException {
- // <!--tinycomments\|2\.1\|data:application\/json;base64,([A-Za-z0-9]*)=-->
- Pattern pattern = Pattern.compile("\\<\\!\\-\\-tinycomments\\|2\\.1\\|data\\:application\\/json\\;base64\\,([A-Za-z0-9\\/\\+\\\\]*)\\=*\\-\\-\\>");
- Matcher matcher = pattern.matcher(content);
- String rawComments = "";
- if(matcher.find()) {
- byte[] decoded = Base64.getDecoder().decode(matcher.group(1));
- rawComments = new String(decoded, "UTF-8");
-
- if(rawComments.length() < 10) {
- return "";
- }
-
- rawComments = rawComments.replaceAll("^.", "[");
- rawComments = rawComments.replaceAll(".$", "]");
- rawComments = rawComments.replaceAll("\"mce-conversation\\_\\d*\":", "");
-
- ObjectMapper mapper = new ObjectMapper();
- List<PlantillaConversation> comments = mapper.readValue(rawComments, new TypeReference<List<PlantillaConversation>>(){});
-
- String tabComments = "<p class=\"pagebreak\"><!-- pagebreak --></p>"
- + "<h3>COMENTARIOS GENERADOS DURANTE LA REVISIÓN DEL DOCUMENTO</h3>"
- + "<h3><small>Nota: Esta página no será visible en la versión final publicada del documento.</small></h3>"
- + "<table style=\"border-collapse: collapse; width: 297mm; border-width: 1px; border-spacing: 0px; border-color: rgb(149, 165, 166); margin-left: 0px; margin-right: auto;\" border=\"1\" width=\"297mm\" cellspacing=\"0\" cellpadding=\"8\">"
- + "<thead>"
- + " <tr style=\"padding: 0px; border-width: 1px; border-color: rgb(149, 165, 166);\" bgcolor=\"#153d63\">"
- + " <th style=\"padding: 0px; border-width: 1px; border-color: rgb(149, 165, 166); color:white;\" scope=\"col\">Usuario</th>"
- + " <th style=\"padding: 0px; border-width: 1px; border-color: rgb(149, 165, 166); color:white;\" scope=\"col\">Nombre</th>"
- + " <th style=\"padding: 0px; border-width: 1px; border-color: rgb(149, 165, 166); color:white;\" scope=\"col\">Comentario</th>"
- + " </tr>"
- + " </thead>"
- + "<tbody>";
- for(PlantillaConversation conv: comments) {
- for(PlantillaComentario c: conv.getComments()) {
- tabComments += ("<tr style=\"padding: 0px; border-width: 1px; border-color: rgb(149, 165, 166);\"> "
- + " <td style=\"padding: 0px; border-width: 1px; border-color: rgb(149, 165, 166);\">"+c.getAuthor()+"</td>"
- + " <td style=\"padding: 0px; border-width: 1px; border-color: rgb(149, 165, 166);\">"+c.getAuthorName()+"</td>"
- + " <td style=\"padding: 0px; border-width: 1px; border-color: rgb(149, 165, 166);\">"+c.getContent()+"</td>"
- + "</tr> ");
- }
- }
- tabComments += "</tbody></table>";
- return tabComments;
- }
-
- return "";
- }
-
-
- public static String asString(Resource resource) {
- try (Reader reader = new InputStreamReader(resource.getInputStream(), "UTF-8")) {
- return FileCopyUtils.copyToString(reader);
- } catch (IOException e) {
- throw new UncheckedIOException(e);
- }
- }
-
- public static String readFileToString(String path) {
- ResourceLoader resourceLoader = new DefaultResourceLoader();
- Resource resource = resourceLoader.getResource(path);
- return asString(resource);
- }
-
- }
|