Преглед изворни кода

Merge branch 'fixed-templates' of UV-APPS/UV_SAIC_DOCS into master

Mario Martínez Hernández пре 1 недеља
родитељ
комит
f3d1bbced5

+ 12 - 338
src/main/java/es/uv/saic/service/PlantillaService.java

@@ -10,7 +10,6 @@ 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;
@@ -19,10 +18,8 @@ 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;
@@ -39,8 +36,6 @@ 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.dto.NomProcesOrganDTO;
-import es.uv.saic.shared.dto.OrganDTO;
 import es.uv.saic.shared.feign.IndicadorClient;
 import es.uv.saic.shared.feign.OrganClient;
 import es.uv.saic.shared.feign.ProceduresClient;
@@ -106,266 +101,24 @@ public class PlantillaService {
 		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)**/
-		ic.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>();
-		 
-		OrganDTO centre = oc.findByID("C", idCentre);
-		OrganDTO titulacio = oc.findByID("T", idTitulacio);
-		
-		/** Add header information **/
-		addHeaderData(centre.getNomVal(), centre.getLugar2(), 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)**/
-		ic.getTemplateData(idTitulacio, idCentre, curs, context);	
-		this.replaceValues(doc, context, header, img);
+	public Plantilla findEvidencia(Integer codiEvidencia, Integer titulacio, String tambit) {
+		Plantilla p = this.findByID(codiEvidencia);
+		Plantilla p2 = null;
 		
-		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<OrganDTO> tits = oc.getTitulacionsByCentreTambit(idCentre, tambit);
-			for(OrganDTO org : tits) {
-				context.clear();
-				Element e_copy = e.clone();
-				ic.getTemplateData(org.getLugar(), org.getLugar2(), 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);
+		if(titulacio > 0) {
+			if(!p.getAmbit().equals(tambit)) {
+				p2 = this.findByVersioCodiAmbit(p.getVersio(), p.getCodi(), tambit);
 			}
-			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<OrganDTO> tits = oc.getTitulacionsByCentreTambit(idCentre, Tambit);
-			for(OrganDTO t : tits) {
-				Document tabcopy = Jsoup.parse(tab.html());
-				context.clear();
-				ic.getTemplateData(t.getLugar(), t.getLugar2(), 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 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("");
-				}
-			}
+		if(p2 != null) {
+			p = p2;
 		}
-		return doc;
+
+		return p;
 	}
 	
-	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>");
@@ -541,87 +294,8 @@ public class PlantillaService {
 		
 		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 = tc.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")) {

+ 6 - 13
src/main/java/es/uv/saic/web/PlantillaController.java

@@ -10,13 +10,12 @@ import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
 import es.uv.saic.service.PlantillaService;
 import es.uv.saic.shared.domain.Plantilla;
 import es.uv.saic.shared.dto.PdfDTO;
-import es.uv.saic.shared.dto.TemplateDataDTO;
-
 
 @RestController
 @RequestMapping("/plantilla")
@@ -48,6 +47,11 @@ public class PlantillaController {
         }
     }
 
+    @GetMapping("/evidencia")
+    public Plantilla findEvidencia(@RequestParam Integer codiEvidencia, @RequestParam Integer titulacio, @RequestParam String tambit) {
+        return ps.findEvidencia(codiEvidencia, titulacio, tambit);
+    }
+
     @PostMapping
     public String save(@RequestBody Plantilla plantilla) {
         try {
@@ -70,20 +74,9 @@ public class PlantillaController {
        return ps.isUsed(id);
     }
 
-    @PostMapping("/addTemplateData")
-    public String addTemplateData(@RequestBody TemplateDataDTO td) {
-        return ps.addTemplateData(td.getInstanciaTasca(), td.getTemplate());
-    }
-    
-    @PostMapping("/addTemplateData2")
-    public String addTemplateData2(@RequestBody TemplateDataDTO td) {
-        return ps.addTemplateData(td.getIdTitulacio(), td.getIdCentre(), td.getCurs(), td.getTemplate());        
-}
-
     @PostMapping("/savePDF")
     public String savePDF(@RequestBody PdfDTO pdf) throws IOException, InterruptedException {
         return ps.savePDF(pdf.getContent(), pdf.getIdtasca());        
 
     }
-    
 }