ProceduresController.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. package es.uv.saic.web;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.math.BigInteger;
  5. import java.sql.Timestamp;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.beans.factory.annotation.Value;
  12. import org.springframework.security.core.Authentication;
  13. import org.springframework.stereotype.Controller;
  14. import org.springframework.ui.Model;
  15. import org.springframework.web.bind.annotation.DeleteMapping;
  16. import org.springframework.web.bind.annotation.GetMapping;
  17. import org.springframework.web.bind.annotation.PathVariable;
  18. import org.springframework.web.bind.annotation.PostMapping;
  19. import org.springframework.web.bind.annotation.RequestParam;
  20. import org.springframework.web.bind.annotation.ResponseBody;
  21. import org.springframework.web.multipart.MultipartFile;
  22. import es.uv.saic.shared.domain.EvidenciaIndicadorEnquesta;
  23. import es.uv.saic.shared.domain.InstanciaTascaVer;
  24. import es.uv.saic.shared.domain.Plantilla;
  25. import es.uv.saic.shared.domain.Proces;
  26. import es.uv.saic.shared.domain.Tipus;
  27. import es.uv.saic.shared.domain.Usuari;
  28. import es.uv.saic.shared.dto.RolDTO;
  29. import es.uv.saic.shared.feign.PlantillaClient;
  30. import es.uv.saic.shared.feign.ProceduresClient;
  31. import es.uv.saic.shared.feign.UsuariClient;
  32. import jakarta.servlet.http.HttpServletResponse;
  33. import jakarta.servlet.http.HttpSession;
  34. @Controller
  35. public class ProceduresController {
  36. @Autowired
  37. private ProceduresClient pc;
  38. @Autowired
  39. private PlantillaClient plc;
  40. @Autowired
  41. private UsuariClient uc;
  42. @Value("${saic.data.filePath}")
  43. private String filePath;
  44. /* Redirect root to /procedures */
  45. @GetMapping("/")
  46. public void getRoot(Model model, Authentication auth, HttpSession session, HttpServletResponse response) throws IOException {
  47. response.sendRedirect("/procedures");
  48. }
  49. /*
  50. * Load the list of active procedure instances for the logged-in user
  51. *
  52. * @param model
  53. * @param auth Authentication
  54. * @param session HttpSession
  55. * @param _new Optional parameter to indicate a new access
  56. * @return The name of the view to render
  57. */
  58. @GetMapping("/procedures")
  59. public String getActiveInstances(Model model, Authentication auth, HttpSession session, @RequestParam(required = false) String _new) {
  60. HashMap<String, Object> response =
  61. pc.getActiveInstances( _new, ((Usuari) auth.getPrincipal()).getUsuari());
  62. if (response == null) {
  63. return "401";
  64. }
  65. model.addAllAttributes(response);
  66. return "procedures";
  67. }
  68. /*
  69. * Loads a procedure instance and its tasks
  70. *
  71. * @param model
  72. * @param auth Authentication
  73. * @param session HttpSession
  74. * @param id Instancia ID Instance to load
  75. * @return The name of the view to render
  76. */
  77. @GetMapping("/procedure/{id}")
  78. public String getInstance(Model model, Authentication auth, HttpSession session, @PathVariable BigInteger id) {
  79. HashMap<String, Object> response =
  80. pc.getInstance(id, ((Usuari) auth.getPrincipal()).getUsuari());
  81. if(session.getAttribute("location") != null) {
  82. model.addAttribute("location", session.getAttribute("location"));
  83. }
  84. else {
  85. session.setAttribute("location", "procedures");
  86. model.addAttribute("location", "procedures");
  87. }
  88. if(response == null){
  89. return "401";
  90. }
  91. model.addAllAttributes(response);
  92. return "procedure";
  93. }
  94. /*
  95. * Updates a task instance with evidence files (for specific task types)
  96. *
  97. * @param model
  98. * @param auth Authentication
  99. * @param session HttpSession
  100. * @param id Instancia ID Instance to load
  101. * @param params Form parameters
  102. * @param evidencias List of evidence files (if any)
  103. * @return The number of files uploaded (as a String)
  104. * @throws IllegalStateException
  105. * @throws IOException
  106. * @throws InterruptedException
  107. */
  108. @PostMapping("/procedure/files/{id}")
  109. @ResponseBody
  110. public String updateInstanciaTascaEvidencia(Model model, Authentication auth, HttpSession session, @PathVariable BigInteger id, @RequestParam Map<String,String> params,
  111. @RequestParam(required = true) List<MultipartFile> evidencias) throws IllegalStateException, IOException {
  112. List<File> files;
  113. // Convert MultipartFiles (evidencias) to Files before sending to the ProceduresClient
  114. files = new ArrayList<>();
  115. for (MultipartFile multipartFile : evidencias) {
  116. File convFile = new File(filePath + multipartFile.getOriginalFilename());
  117. multipartFile.transferTo(convFile);
  118. files.add(convFile);
  119. }
  120. HashMap<String, Object> response =
  121. pc.updateInstanciaTascaEvidencia(id, params, files, ((Usuari) auth.getPrincipal()).getUsuari());
  122. if (response != null && response.get("ammount") != null) {
  123. model.addAllAttributes(response);
  124. return response.get("ammount").toString();
  125. }
  126. return "0";
  127. }
  128. /*
  129. * Updates a task instance
  130. *
  131. * @param model
  132. * @param auth Authentication
  133. * @param session HttpSession
  134. * @param id Instancia ID Instance to load
  135. * @param params Form parameters
  136. * @param evidencias List of evidence files (if any)
  137. * @return The name of the view to render
  138. */
  139. @PostMapping("/procedure/{id}")
  140. public String updateInstanciaTasca(Model model, Authentication auth, HttpSession session, @PathVariable BigInteger id, @RequestParam Map<String,String> params,
  141. @RequestParam(required = false) List<MultipartFile> evidencias) throws IllegalStateException, IOException, InterruptedException {
  142. model.addAttribute("location", "procedures");
  143. HashMap<String, Object> response =
  144. pc.updateInstanciaTasca(id, params, evidencias, ((Usuari) auth.getPrincipal()).getUsuari());
  145. if (response == null) {
  146. return "401";
  147. }
  148. model.addAllAttributes(response);
  149. return "procedure";
  150. }
  151. /*
  152. * Saves a draft of a task instance (for specific task types)
  153. *
  154. * @param model
  155. * @param auth Authentication
  156. * @param session HttpSession
  157. * @param id Instancia ID Instance to load
  158. * @param text Text content of the draft
  159. * @param manual Whether the save was manually triggered by the user
  160. * @return The timestamp of the save operation formatted as a String
  161. */
  162. @PostMapping("/procedure/save/{id}")
  163. @ResponseBody
  164. public String saveDraft(Model model, Authentication auth, HttpSession session, @PathVariable BigInteger id, @RequestParam String text,
  165. @RequestParam boolean manual) {
  166. HashMap<String, Object> response =
  167. pc.saveDraft(id, text, manual, ((Usuari) auth.getPrincipal()).getUsuari());
  168. if(session.getAttribute("location") != null) {
  169. model.addAttribute("location", session.getAttribute("location"));
  170. }
  171. else {
  172. session.setAttribute("location", "procedures");
  173. model.addAttribute("location", "procedures");
  174. }
  175. return response.get("date") == null ? "No se pudo obtener la fecha" : response.get("date").toString();
  176. }
  177. /*
  178. * Get all drafts for a given task instance
  179. * @param model
  180. * @param auth Authentication
  181. * @param session HttpSession
  182. * @param id InstanciaTasca ID to load drafts for
  183. * @return The name of the view to render
  184. */
  185. @GetMapping("/procedure/drafts/{id}")
  186. public String getDrafts(Model model, Authentication auth, HttpSession session, @PathVariable BigInteger id) {
  187. HashMap<String, Object> response =
  188. pc.getDrafts(id);
  189. if (response == null) {
  190. return "401";
  191. }
  192. model.addAllAttributes(response);
  193. return "procedure_versions";
  194. }
  195. /*
  196. * Get a specific draft for a given task instance
  197. * @param model
  198. * @param auth Authentication
  199. * @param session HttpSession
  200. * @param id InstanciaTasca ID to load drafts for
  201. * @param dataMod Timestamp of the draft to load
  202. * @return The InstanciaTascaVer object representing the draft
  203. */
  204. @GetMapping("/procedure/draft/{id}")
  205. @ResponseBody
  206. public InstanciaTascaVer getDraft(Model model, Authentication auth, HttpSession session, @PathVariable BigInteger id, @RequestParam Timestamp dataMod) {
  207. return pc.getDraft(id, dataMod);
  208. }
  209. /*
  210. * Restore a specific draft for a given task instance
  211. * @param model
  212. * @param auth Authentication
  213. * @param session HttpSession
  214. * @param id InstanciaTasca ID to restore draft for
  215. * @param dataMod Timestamp of the draft to restore
  216. * @return "1" if successful, "0" otherwise
  217. */
  218. @PostMapping("/procedure/draft/{id}")
  219. @ResponseBody
  220. public String restoreDraft(Model model, Authentication auth, HttpSession session, @PathVariable BigInteger id, @RequestParam Timestamp dataMod) {
  221. return pc.restoreDraft(id, dataMod);
  222. }
  223. // POST que se utiliza para conseguir los cursos a partir de la titulación
  224. @PostMapping("/procedure/search/years")
  225. public String getYearsByCenterTitulation(Model model, Authentication auth,
  226. @RequestParam(name="centers[]", required=false) List<Integer> centres,
  227. @RequestParam("titulations[]") List<Integer> titulacions) throws IOException {
  228. HashMap<String, Object> response =
  229. pc.getYearsByCenterTitulation(centres, titulacions, ((Usuari) auth.getPrincipal()).getUsuari());
  230. if (response == null) {
  231. return "401";
  232. }
  233. model.addAllAttributes(response);
  234. return "components/selector_cursos";
  235. }
  236. // POST que se utiliza para conseguir los procedimiento que se han llevado a cabo por cursos y por titulación
  237. @PostMapping("/procedure/search/procedures")
  238. public String getProceduresByCenterTitulationYear(Model model, Authentication auth,
  239. @RequestParam(name="centers[]", required=false) List<Integer> centres,
  240. @RequestParam("years[]") List<Integer> cursos,
  241. @RequestParam("titulations[]") List<Integer> titulacions) throws IOException {
  242. HashMap<String, Object> response =
  243. pc.getProceduresByCenterTitulationYear(centres, cursos, titulacions, ((Usuari) auth.getPrincipal()).getUsuari());
  244. if (response == null) {
  245. return "401";
  246. }
  247. model.addAllAttributes(response);
  248. return "components/selector_processos";
  249. }
  250. @PostMapping("/find/procedure")
  251. public String findProcedure(Model model, Authentication auth, @RequestParam("procedure") Integer idProces,
  252. @RequestParam("action") String action) throws IOException {
  253. HashMap<String, Object> response =
  254. pc.findProcedure(idProces.toString(), action);
  255. if (response != null) {
  256. model.addAllAttributes(response);
  257. if (action.equals("remove")) {
  258. return "components/form_procedure_remove_confirm";
  259. } else if (action.equals("duplicate") || action.equals("edit") || idProces == 0) {
  260. return "components/form_procedure";
  261. }
  262. }
  263. return "401";
  264. }
  265. // POST para crear el form de creación de plantilla
  266. @PostMapping("/template/form")
  267. public String formTemplate(Model model, Authentication auth, @RequestParam("id") Integer idPlantilla,
  268. @RequestParam("action") String action) throws IOException {
  269. HashMap<String, Object> response =
  270. pc.formTemplate(idPlantilla, action);
  271. if (response != null && response.get("redirect") != null) {
  272. model.addAllAttributes(response);
  273. return (boolean) response.get("redirect") ? "components/form_template" : "";
  274. }
  275. return "401";
  276. }
  277. // GET para renderizar el formulario de cración de nueva tarea
  278. @GetMapping("/newTask/{i}")
  279. public String newTaskForm(Model model, Authentication auth, @PathVariable Integer i) throws IOException {
  280. List<RolDTO> roles = uc.findAssignables();
  281. List<Tipus> tipus = pc.findAll();
  282. List<Plantilla> templates = plc.findAll();
  283. model.addAttribute("tipus", tipus);
  284. model.addAttribute("roles", roles);
  285. model.addAttribute("i", i);
  286. model.addAttribute("templates", templates);
  287. return "components/form_procedure_task";
  288. }
  289. @ResponseBody
  290. @PostMapping("/find/template")
  291. public String findTemplate(Model model, Authentication auth, @RequestParam("id") Integer idPlantilla) throws IOException {
  292. Plantilla p = plc.findByID(idPlantilla);
  293. return p.getText();
  294. }
  295. // GET para comprobar si una plantilla esta siendo usada
  296. @ResponseBody
  297. @GetMapping("/template/used/{idPlantilla}")
  298. public int isTemplateUsed(Model model, Authentication auth, @PathVariable("idPlantilla") Integer idPlantilla) throws IOException {
  299. Boolean u = plc.isUsed(idPlantilla);
  300. return u ? 1 : 0;
  301. }
  302. @ResponseBody
  303. @PostMapping("/template/edit")
  304. public String templateEdit(Model model, Authentication auth, @RequestParam("id") Integer idPlantilla,@RequestParam("text") String text, @RequestParam("versio") Integer versio,
  305. @RequestParam("nomCas") String nomCas, @RequestParam("nomVal") String nomVal, @RequestParam("ambit") String ambit) throws IOException {
  306. Plantilla p = plc.findByID(idPlantilla);
  307. p.setNomCas(nomCas);
  308. p.setNomVal(nomVal);
  309. p.setText(text);
  310. p.setAmbit(ambit);
  311. plc.save(p);
  312. return "1";
  313. }
  314. @ResponseBody
  315. @PostMapping("/template/save")
  316. public String templateSave(Model model, Authentication auth, @RequestParam("text") String text, @RequestParam("codi") String codi, @RequestParam("versio") Integer versio,
  317. @RequestParam("nomCas") String nomCas, @RequestParam("nomVal") String nomVal, @RequestParam("ambit") String ambit) throws IOException {
  318. Plantilla p = new Plantilla();
  319. Plantilla p2 = plc.findByVersioCodiAmbit(versio, codi, ambit);
  320. if(p2 != null) {
  321. return "0";
  322. }
  323. p.setCodi(codi);
  324. p.setVersio(versio);
  325. p.setNomCas(nomCas);
  326. p.setNomVal(nomVal);
  327. p.setText(text);
  328. p.setAmbit(ambit);
  329. plc.save(p);
  330. return "1";
  331. }
  332. @ResponseBody
  333. @PostMapping("/draft/save/{id}")
  334. public String saveTemplate(Model model, Authentication auth, @PathVariable("id") Integer idPlantilla,
  335. @RequestParam("text") String text) throws IOException {
  336. Plantilla p = plc.findByID(idPlantilla);
  337. p.setText(text);
  338. plc.save(p);
  339. return "1";
  340. }
  341. @DeleteMapping("/template/form")
  342. @ResponseBody
  343. public String formTemplateRemove(Model model, Authentication auth, @RequestParam("id") Integer idPlantilla) throws IOException {
  344. if(plc.isUsed(idPlantilla)){
  345. return "0";
  346. }
  347. else {
  348. Plantilla p = plc.findByID(idPlantilla);
  349. plc.delete(p);
  350. return "1";
  351. }
  352. }
  353. @PostMapping("/find/template/inds")
  354. public String findTemplatesInds(Model model, Authentication auth, @RequestParam("procedure") Integer idProces,
  355. @RequestParam("center") String idCentre, @RequestParam("titulation") String idTitulacio,
  356. @RequestParam("ev") String evidencia) throws IOException {
  357. Proces p = this.pc.findProcesByID(idProces);
  358. List<EvidenciaIndicadorEnquesta> inds = this.pc.getByProcesEvidencia(p.getNomProces(), evidencia);
  359. model.addAttribute("inds", inds);
  360. model.addAttribute("proces", p.getNomProces());
  361. model.addAttribute("idCentre", idCentre);
  362. model.addAttribute("idTitulacio", idTitulacio);
  363. model.addAttribute("evidencia", evidencia);
  364. return "components/form_templates_indicators";
  365. }
  366. }