|
|
@@ -1,29 +1,21 @@
|
|
|
package es.uv.saic.web;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
-import java.io.PrintWriter;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
-import java.util.stream.Collectors;
|
|
|
-import java.util.stream.Stream;
|
|
|
-
|
|
|
-import jakarta.servlet.http.HttpServletResponse;
|
|
|
|
|
|
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.security.core.Authentication;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
-import org.springframework.ui.Model;
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
|
|
|
import es.uv.saic.domain.Instancia;
|
|
|
import es.uv.saic.domain.InstanciaTasca;
|
|
|
import es.uv.saic.domain.Proces;
|
|
|
-import es.uv.saic.domain.Usuari;
|
|
|
import es.uv.saic.service.ProcesService;
|
|
|
|
|
|
@Controller
|
|
|
@@ -41,17 +33,7 @@ public class ExportController {
|
|
|
* @param id_t Task ID
|
|
|
*/
|
|
|
@GetMapping("/export/{id_p}/{id_t}")
|
|
|
- public void exportData(Model model, Authentication auth, HttpServletResponse response,
|
|
|
- @PathVariable Integer id_p, @PathVariable Integer id_t) throws IOException {
|
|
|
- if(!((Usuari)auth.getPrincipal()).isAdmin() & !((Usuari)auth.getPrincipal()).isDataTest()) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- response.setContentType("text/csv");
|
|
|
- response.setCharacterEncoding("UTF-8");
|
|
|
- response.setHeader("Content-Disposition", "attachment; filename=data_export.csv");
|
|
|
-
|
|
|
- PrintWriter pw = response.getWriter();
|
|
|
+ public List<String[]> exportData(@PathVariable Integer id_p, @PathVariable Integer id_t) throws IOException {
|
|
|
Proces p = this.ps.findByID(id_p);
|
|
|
List<Instancia> ins = p.getInstancies();
|
|
|
List<String[]> lines = new ArrayList<>();
|
|
|
@@ -95,12 +77,8 @@ public class ExportController {
|
|
|
lines.add(line);
|
|
|
|
|
|
}
|
|
|
-
|
|
|
- lines.stream()
|
|
|
- .map(this::convertToCSV)
|
|
|
- .forEach(pw::println);
|
|
|
- pw.close();
|
|
|
- pw.flush();
|
|
|
+
|
|
|
+ return lines;
|
|
|
}
|
|
|
|
|
|
// @GetMapping("/export/{id_p}/{id_t}")
|
|
|
@@ -199,17 +177,7 @@ public class ExportController {
|
|
|
* @param id_t Task ID
|
|
|
*/
|
|
|
@GetMapping("/export2/{id_p}/{id_t}")
|
|
|
- public void exportData2(Model model, Authentication auth, HttpServletResponse response,
|
|
|
- @PathVariable Integer id_p, @PathVariable Integer id_t) throws IOException {
|
|
|
- if(!((Usuari)auth.getPrincipal()).isAdmin() & !((Usuari)auth.getPrincipal()).isDataTest()) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- response.setContentType("text/csv");
|
|
|
- response.setCharacterEncoding("UTF-8");
|
|
|
- response.setHeader("Content-Disposition", "attachment; filename=data_export.csv");
|
|
|
-
|
|
|
- PrintWriter pw = response.getWriter();
|
|
|
+ public List<String[]> exportData2(@PathVariable Integer id_p, @PathVariable Integer id_t) throws IOException {
|
|
|
Proces p = this.ps.findByID(id_p);
|
|
|
List<Instancia> ins = p.getInstancies();
|
|
|
List<String[]> lines = new ArrayList<>();
|
|
|
@@ -252,39 +220,6 @@ public class ExportController {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- lines.stream()
|
|
|
- .map(this::convertToCSV)
|
|
|
- .forEach(pw::println);
|
|
|
- pw.close();
|
|
|
- pw.flush();
|
|
|
- }
|
|
|
-
|
|
|
- /*
|
|
|
- * Convert an array of Strings to a single CSV line, escaping special characters
|
|
|
- * @param data Array of Strings to convert
|
|
|
- * @return A single CSV line as a String
|
|
|
- */
|
|
|
- public String convertToCSV(String[] data) {
|
|
|
- return Stream.of(data)
|
|
|
- .map(this::escapeSpecialCharacters)
|
|
|
- .collect(Collectors.joining(";"));
|
|
|
+ return lines;
|
|
|
}
|
|
|
-
|
|
|
- /*
|
|
|
- * Escape special characters in a String for CSV format
|
|
|
- * @param data The String to escape
|
|
|
- * @return The escaped String
|
|
|
- */
|
|
|
- public String escapeSpecialCharacters(String data) {
|
|
|
- if(data == null) return "";
|
|
|
- String escapedData = data.replaceAll("\\R", " ");
|
|
|
- data = data.replace("\"", "'");
|
|
|
- data = data.replace(";", ",");
|
|
|
- data = data.replace("\n", " ");
|
|
|
- escapedData = "\"" + data + "\"";
|
|
|
- return escapedData;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
}
|