| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660 |
- 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.HashMap;
- 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.jsoup.select.Elements;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.core.io.ClassPathResource;
- 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.domain.Indicador;
- import es.uv.saic.domain.Plantilla;
- import es.uv.saic.domain.PlantillaComentario;
- import es.uv.saic.domain.PlantillaConversation;
- import es.uv.saic.domain.PlantillaRepository;
- import es.uv.saic.dto.IndicadorEnquestaDTO;
- import es.uv.saic.dto.IndicadorEnquestaValorDTOImp;
- import es.uv.saic.dto.InstanciaTascaDTO;
- import es.uv.saic.dto.NomProcesOrganDTO;
- import es.uv.saic.dto.OrganTransferDTO;
- import es.uv.saic.feign.CoreClient;
- @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 CoreClient core;
-
- 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 String addTemplateData(InstanciaTascaDTO it, String template){
- HashMap<String, String> context = new HashMap<String, String>();
- HashMap<String, String> header = new HashMap<String, String>();
- HashMap<String, String> img = new HashMap<String, String>();
-
- Integer idTitulacio = it.getInstancia().getTitulacio();
- Integer cursAvaluat = it.getInstancia().getCursAvaluat();
- Integer idCentre = it.getInstancia().getCentre();
- String ambit = "G";
- String opcionsStr = it.getTasca().getOpcions().replaceAll("^\\.$", "");
- String [] opcions = new String[0];
- if(!opcionsStr.isEmpty()) {
- opcions = opcionsStr.split(";");
- }
- if(it.getInstancia().getTlugar().equals("C")) {
- if(it.getInstancia().getTitulacio() == 2) {
- ambit = "M";
- }
- }
-
- /** Add header information **/
- addHeaderData(it.getInstancia().getNomval(), it.getInstancia().getLugar(), it.getInstancia().getNomValTitulacio(), it.getInstancia().getCursAvaluat(), header, img);
-
- /** Iterate elements inside {[loop]} ... {[endloop]} **/
- template = this.iterateLoopTag(template, idCentre, ambit, cursAvaluat, context);
-
- /** Iterate elements with data-loop attribute **/
- Document doc = Jsoup.parse(template);
- doc = iterateLoopAttr(idCentre, cursAvaluat, doc, context);
-
- /** Add template data (non iterated data)**/
- getTemplateData(idTitulacio, idCentre, cursAvaluat, context);
- this.replaceValues(doc, context, header, img);
-
- /** Replace sections if specified in options **/
- this.replaceSection(opcions, it, doc);
-
- return doc.html();
- }
-
- public String addTemplateData(Integer idTitulacio, Integer idCentre, Integer curs, String template){
-
- HashMap<String, String> context = new HashMap<String, String>();
- HashMap<String, String> header = new HashMap<String, String>();
- HashMap<String, String> img = new HashMap<String, String>();
-
- OrganTransferDTO centre = core.findOrganById("C", idCentre);
- OrganTransferDTO titulacio = core.findOrganById("T", idTitulacio);
-
- /** Add header information **/
- addHeaderData(centre.getNomVal(), centre.getLugarCentre(), titulacio.getNomVal(), curs, header, img);
-
- /** Iterate elements inside {[loop]} ... {[endloop]} **/
- template = this.iterateLoopTag(template, idCentre, titulacio.getTambit(), curs, context);
-
- /** Iterate elements with data-loop attribute **/
- Document doc = Jsoup.parse(template);
- doc = iterateLoopAttr(idCentre, curs, doc, context);
-
- /** Add template data (non iterated data)**/
- getTemplateData(idTitulacio, idCentre, curs, context);
- this.replaceValues(doc, context, header, img);
-
- return doc.html();
- }
-
- private void addHeaderData(String nomVal, Integer lugar, String nomValTitulacion, Integer curs, HashMap<String, String> header, HashMap<String, String> img) {
- header.put("centre", nomVal);
- header.put("titulacio", nomValTitulacion);
- header.put("curs", "CURSO "+Integer.toString(curs-1)+" - "+Integer.toString(curs));
- header.put("curs_anterior", Integer.toString(curs-2)+" - "+Integer.toString(curs-1));
- header.put("conv-ant1", "CONVOCATORIA "+Integer.toString(curs-1)+" - "+Integer.toString(curs));
- header.put("conv-ant2", "CONVOCATORIA "+Integer.toString(curs-2)+" - "+Integer.toString(curs-1));
- header.put("periode-ant1", Integer.toString(curs-6)+" - "+Integer.toString(curs-1));
- header.put("periode-ant2", Integer.toString(curs-7)+" - "+Integer.toString(curs-2));
- ClassPathResource fuv = new ClassPathResource("/static/logos/UV.png");
- ClassPathResource fc = new ClassPathResource("/static/logos/C"+Integer.toString(lugar)+".png");
- String logouv_b64;
- String logoc_b64;
- try {
- logouv_b64 = "data:image/png;base64, "+Base64.getEncoder().encodeToString(fuv.getInputStream().readAllBytes());
- } catch (IOException e) {
- logouv_b64 = "https://saic.uv.es/public/logos/UV.png";
- }
- try {
- logoc_b64 = "data:image/png;base64, "+Base64.getEncoder().encodeToString(fc.getInputStream().readAllBytes());
- } catch (IOException e) {
- logoc_b64 = "https://saic.uv.es/public/logos/C"+Integer.toString(lugar)+".png";
- }
- img.put("logo_centre", logoc_b64);
- img.put("logo_uv", logouv_b64);
- }
-
- private Document iterateLoopAttr(Integer idCentre, Integer curs, Document doc, HashMap<String, String> context) {
- Elements loop_elements = doc.select("*[data-loop]");
- for(Element e : loop_elements) {
- String Tambit = e.attr("data-loop");
- List<OrganTransferDTO> tits = core.getTitulacionsByCentreTambit(idCentre, Tambit);
- for(OrganTransferDTO org : tits) {
- context.clear();
- Element e_copy = e.clone();
- getTemplateData(org.getLugarTitulacion(), org.getLugarCentre(), curs, context);
- context.put("titulacio_loop", org.getNomVal());
- Elements ielements = e_copy.select("*:matchesWholeOwnText(\\{\\{([^\\}]*)\\}\\})");
- for(Element ielem : ielements) {
- String t = ielem.html().replace("{", "").replace("}", "");
- String i = this.formatValue(context.get(t));
- if(i != null) {
- if(ielem.tagName().equals("td")) {
- ielem.attr("class", "mceEditable");
- ielem.attr("id", "ind_"+t);
- ielem.html(i);
- }
- else {
- ielem.html(i);
- }
- }
- else {
- if(ielem.tagName().equals("td")) {
- ielem.attr("class", "mceEditable");
- ielem.attr("id", "ind_"+t);
- ielem.html("");
- }
- else {
- ielem.html("");
- }
- }
- }
- e.after(e_copy);
- }
- e.remove();
- }
- context.clear();
- return doc;
- }
-
- private String iterateLoopTag(String template, Integer idCentre, String Tambit, Integer curs, HashMap<String, String> context) {
- Pattern pattern = Pattern.compile(".*<p>\\{\\[loop\\]\\}</p>([\\S\\s]*)<p>\\{\\[endloop\\]\\}</p>.*");
- Matcher matcher = pattern.matcher(template);
- String replaced = "";
-
- while(matcher.find()) {
- String group = matcher.group(1);
- Document tab = Jsoup.parse(group);
- List<OrganTransferDTO> tits = core.getTitulacionsByCentreTambit(idCentre, Tambit);
- for(OrganTransferDTO t : tits) {
- Document tabcopy = Jsoup.parse(tab.html());
- context.clear();
- getTemplateData(t.getLugarTitulacion(), t.getLugarCentre(), curs, context);
- context.put("titulacio_loop", t.getNomVal());
- tabcopy = this.replaceValuesLoop(tabcopy, context);
- replaced += tabcopy.html()+"<p></p>";
- }
- template = template.replace("<p>{[loop]}</p>"+group+"<p>{[endloop]}</p>", replaced);
- }
- context.clear();
- return template;
- }
-
- private Document replaceValues(Document doc, HashMap<String, String> context, HashMap<String, String> header, HashMap<String, String> img) {
- Elements elems = doc.select("*:matchesWholeOwnText(\\{\\{([^\\}]*)\\}\\})");
- for(Element e : elems) {
- String t = e.html().replace("{", "").replace("}", "");
- if(header.containsKey(t)) {
- e.html(header.get(t));
- }
- else if(img.containsKey(t)) {
- Element x = new Element("img");
- x.attr("src", img.get(t));
- x.attr("style", "display:block; margin-left:auto; margin-right:auto; width:100px; max-width:100px;");
- x.attr("class", "logo");
- e.html("");
- e.appendChild(x);
- }
- else {
- String i = this.formatValue(context.get(t));
- if(i != null) {
- if(e.parent().tagName().equals("td")) {
- e.parent().attr("class", "mceEditable");
- e.parent().attr("id", "ind_"+t);
- e.parent().attr("style", e.attr("style"));
- e.parent().html(i);
- }
- else {
- e.attr("class", "mceEditable");
- e.attr("ind", "ind_"+t);
- e.html(i);
- }
- }
- else {
- if(e.parent().tagName().equals("td")) {
- e.parent().attr("class", "mceEditable");
- e.parent().attr("id", "ind_"+t);
- e.parent().attr("style", e.attr("style"));
- e.parent().html("");
- }
- else {
- e.attr("class", "mceEditable");
- e.attr("ind", "ind_"+t);
- e.html("");
- }
- }
- }
- }
- return doc;
- }
-
- private void getTemplateData(Integer idTitulacio, Integer idCentre, Integer curs, HashMap<String, String> context) {
-
- /* Indicadores del data warehouse */
- try {
- List<Indicador> indicadores;
- indicadores = core.getFromTitulacion(idTitulacio, curs);
- for(Indicador i : indicadores) {
- context.put(i.getIndicador(), i.getValor());
- }
- }
- catch(Exception e) { }
-
- /* Indicadores de encuestas y otros almacenados en BD */
- IndicadorEnquestaDTO indicadorEnquestaDTO = new IndicadorEnquestaDTO(idTitulacio, idCentre, curs);
- List<IndicadorEnquestaValorDTOImp> enquestesT = core.getAllInds2(indicadorEnquestaDTO);
-
- for(IndicadorEnquestaValorDTOImp i : enquestesT) {
- String indicador = i.getAmbit().toLowerCase().equals("t") ? (i.getEnquesta().toLowerCase()+"_"+i.getIndicador().toLowerCase()) : (i.getEnquesta().toLowerCase()+"_"+i.getIndicador().toLowerCase()+"_"+i.getAmbit().toLowerCase());
- if(i.getNum() == null) {
- indicador = i.getTipus().toLowerCase().equals("avg") ? indicador : (indicador += "_"+i.getTipus().toLowerCase());
- indicador = i.getCursd() == null ? indicador : (indicador += "_"+i.getCursd().toLowerCase());
- context.put(indicador, formatValue(i.getValor()));
- }
- else {
- context.put(indicador, i.getNum());
- }
-
- }
- }
-
- private Document replaceValuesLoop(Document doc, HashMap<String, String> context) {
- Elements elems = doc.select("*:matchesWholeOwnText(\\{\\{([^\\}]*)\\}\\})");
- for(Element e : elems) {
- String t = e.html().replace("{", "").replace("}", "");
- String i = this.formatValue(context.get(t));
- if(i != null) {
- if(e.parent().tagName().equals("td")) {
- e.parent().attr("class", "mceEditable");
- e.parent().attr("id", "ind_"+t);
- e.parent().attr("style", e.attr("style"));
- e.parent().html(i);
- }
- else {
- e.attr("class", "mceEditable");
- e.attr("ind", "ind_"+t);
- e.html(i);
- }
- }
- else {
- if(e.parent().tagName().equals("td")) {
- e.parent().attr("class", "mceEditable");
- e.parent().attr("id", "ind_"+t);
- e.parent().attr("style", e.attr("style"));
- e.parent().html("");
- }
- else {
- e.attr("class", "mceEditable");
- e.attr("ind", "ind_"+t);
- e.html("");
- }
- }
- }
- return doc;
- }
-
- private String formatValue(String v) {
- if(v == null) return "";
- if(v.isEmpty() | v.isBlank()) return "";
- if(v.equals("NP")) return "NP";
-
- try {
- double d = Double.parseDouble(v);
- return (Integer.toString((int)d).equals(v) ? v : df.format(d).replace(",", "."));
- }
- catch (NumberFormatException e) { }
-
- if(v.endsWith("%") && v.startsWith(".")) {
- return "0"+v;
- }
-
- return v;
-
- }
-
- 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 = core.findInstanciaTascaById2(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 = core.findInstanciaTascaById2(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 Document replaceSection(String[] opcions, InstanciaTascaDTO it, Document doc) {
- if(opcions.length > 0) {
- NomProcesOrganDTO nomProcesOrganDTO = new NomProcesOrganDTO(opcions[0],
- it.getInstancia().getTlugar(),
- it.getInstancia().getLugar(),
- it.getInstancia().getCentre(),
- it.getInstancia().getTitulacio());
- InstanciaTascaDTO itOld = core.getReportFromNomProcesOrgan(nomProcesOrganDTO);
- try {
- Document doc2 = Jsoup.parse(itOld.getText());
- Elements target = null;
- Elements source = null;
- if(opcions.length == 2) {
- target = this.extractTemplateAnchor(doc2, true, opcions[1]);
- source = this.extractTemplateAnchor(doc, false, opcions[1]);
- }
- else {
- target = this.extractTemplateAnchor(doc2, true, opcions[1], opcions[2]);
- source = this.extractTemplateAnchor(doc, false, opcions[1], opcions[2]);
- }
-
- if(target == null || source == null) {
- return doc;
- }
- Element e = source.get(0);
- e.before(this.clear(target.outerHtml()));
- source.remove();
- }
- catch(NullPointerException e){
- System.out.println("No previous version found for "+it.getIdInstanciaTasca().toString());
- }
-
- }
- return doc;
- }
-
- public Elements extractTemplateAnchor(Document doc, Boolean target, String... args) {
- // <a id="pam"></a>
-
- /* Hardcoded to fix anchor positions in existing templates */
- try{
- String anchor_ini = args[0];
- Element eFirst = doc.select("p:has(a#"+anchor_ini+")").first();
- Elements elems = eFirst.nextElementSiblings();
- Elements table = elems.select("table:not(.pdfignore)");
- return table;
- }
- catch(Exception e){
- return null;
- }
-
- /* Use this code in future */
- /*
- if(args.length == 2){
- String anchor_end = args[1];
- Element eLast = doc.select("p:has(a#"+anchor_end+")").first();
- boolean remove = false;
- for(Iterator<Element> iter = elems.iterator(); iter.hasNext();){
- Element x = iter.next();
- if(remove){
- iter.remove();
- }
- if(x.id().equals(anchor_end) || x.html().contains(anchor_end)){
- remove = true;
- }
- }
- }
- */
-
- }
- public String clear(String html){
- return html.replaceAll("data-mce-style=\"[ ,;:.\\d\\w\\(\\)\\#\\%-]{1,}\"", "")
- .replaceAll("font-family:[ \\d\\w-,]{1,};", "")
- .replaceAll("font-size:[ \\d\\w,\\.]{1,};", "")
- .replaceAll("\\<\\!\\-\\-tinycomments\\|2\\.1\\|data\\:application\\/json\\;base64\\,[A-Za-z0-9\\/\\+\\\\]*\\=*\\-\\-\\>", "")
- .replaceAll("data\\-mce\\-annotation\\-uid\\=\"mce\\-conversation\\_[a-zA-Z0-9]+\"", "")
- .replaceAll("data\\-mce\\-annotation\\=\"tinycomments\"", "")
- .replaceAll("class=\"mce-annotation\"", "");
- }
-
- 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);
- }
-
- }
|