ProceduresController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. package es.uv.saic.web;
  2. import java.io.IOException;
  3. import java.math.BigInteger;
  4. import java.sql.Timestamp;
  5. import java.time.Year;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.beans.factory.annotation.Value;
  11. import org.springframework.security.core.Authentication;
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.ui.Model;
  14. import org.springframework.web.bind.annotation.DeleteMapping;
  15. import org.springframework.web.bind.annotation.GetMapping;
  16. import org.springframework.web.bind.annotation.PathVariable;
  17. import org.springframework.web.bind.annotation.PostMapping;
  18. import org.springframework.web.bind.annotation.RequestParam;
  19. import org.springframework.web.bind.annotation.ResponseBody;
  20. import org.springframework.web.multipart.MultipartFile;
  21. import es.uv.saic.domain.EvidenciaIndicadorEnquesta;
  22. import es.uv.saic.domain.InstanciaTascaVer;
  23. import es.uv.saic.domain.Plantilla;
  24. import es.uv.saic.domain.Proces;
  25. import es.uv.saic.domain.Rol;
  26. import es.uv.saic.domain.Tipus;
  27. import es.uv.saic.domain.Usuari;
  28. import es.uv.saic.dto.RolDTO;
  29. import es.uv.saic.feign.PlantillaClient;
  30. import es.uv.saic.feign.ProceduresClient;
  31. import es.uv.saic.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. HashMap<String, Object> response =
  113. pc.updateInstanciaTascaEvidencia(id, params, evidencias, ((Usuari) auth.getPrincipal()).getUsuari());
  114. if (response != null && response.get("ammount") != null) {
  115. model.addAllAttributes(response);
  116. return response.get("ammount").toString();
  117. }
  118. return "0";
  119. }
  120. /*
  121. * Updates a task instance
  122. *
  123. * @param model
  124. * @param auth Authentication
  125. * @param session HttpSession
  126. * @param id Instancia ID Instance to load
  127. * @param params Form parameters
  128. * @param evidencias List of evidence files (if any)
  129. * @return The name of the view to render
  130. */
  131. @PostMapping("/procedure/{id}")
  132. public String updateInstanciaTasca(Model model, Authentication auth, HttpSession session, @PathVariable BigInteger id, @RequestParam Map<String,String> params,
  133. @RequestParam(required = false) List<MultipartFile> evidencias) throws IllegalStateException, IOException, InterruptedException {
  134. HashMap<String, Object> response =
  135. pc.updateInstanciaTasca(id, params, evidencias, ((Usuari) auth.getPrincipal()).getUsuari());
  136. if (response == null) {
  137. return "401";
  138. }
  139. model.addAllAttributes(response);
  140. return "procedures";
  141. }
  142. /*
  143. * Saves a draft of a task instance (for specific task types)
  144. *
  145. * @param model
  146. * @param auth Authentication
  147. * @param session HttpSession
  148. * @param id Instancia ID Instance to load
  149. * @param text Text content of the draft
  150. * @param manual Whether the save was manually triggered by the user
  151. * @return The timestamp of the save operation formatted as a String
  152. */
  153. @PostMapping("/procedure/save/{id}")
  154. @ResponseBody
  155. public String saveDraft(Model model, Authentication auth, HttpSession session, @PathVariable BigInteger id, @RequestParam String text,
  156. @RequestParam boolean manual) {
  157. HashMap<String, Object> response =
  158. pc.saveDraft(id, text, manual, ((Usuari) auth.getPrincipal()).getUsuari());
  159. if(session.getAttribute("location") != null) {
  160. model.addAttribute("location", session.getAttribute("location"));
  161. }
  162. else {
  163. session.setAttribute("location", "procedures");
  164. model.addAttribute("location", "procedures");
  165. }
  166. if (response == null) {
  167. return "401";
  168. }
  169. return "procedures";
  170. }
  171. /*
  172. * Get all drafts for a given task instance
  173. * @param model
  174. * @param auth Authentication
  175. * @param session HttpSession
  176. * @param id InstanciaTasca ID to load drafts for
  177. * @return The name of the view to render
  178. */
  179. @GetMapping("/procedure/drafts/{id}")
  180. public String getDrafts(Model model, Authentication auth, HttpSession session, @PathVariable BigInteger id) {
  181. HashMap<String, Object> response =
  182. pc.getDrafts(id);
  183. if (response == null) {
  184. return "401";
  185. }
  186. model.addAllAttributes(response);
  187. return "procedure_versions";
  188. }
  189. /*
  190. * Get a specific draft for a given task instance
  191. * @param model
  192. * @param auth Authentication
  193. * @param session HttpSession
  194. * @param id InstanciaTasca ID to load drafts for
  195. * @param dataMod Timestamp of the draft to load
  196. * @return The InstanciaTascaVer object representing the draft
  197. */
  198. @GetMapping("/procedure/draft/{id}")
  199. @ResponseBody
  200. public InstanciaTascaVer getDraft(Model model, Authentication auth, HttpSession session, @PathVariable BigInteger id, @RequestParam Timestamp dataMod) {
  201. return pc.getDraft(id, dataMod);
  202. }
  203. /*
  204. * Restore a specific draft for a given task instance
  205. * @param model
  206. * @param auth Authentication
  207. * @param session HttpSession
  208. * @param id InstanciaTasca ID to restore draft for
  209. * @param dataMod Timestamp of the draft to restore
  210. * @return "1" if successful, "0" otherwise
  211. */
  212. @PostMapping("/procedure/draft/{id}")
  213. @ResponseBody
  214. public String restoreDraft(Model model, Authentication auth, HttpSession session, @PathVariable BigInteger id, @RequestParam Timestamp dataMod) {
  215. return pc.restoreDraft(id, dataMod);
  216. }
  217. // POST que se utiliza para conseguir los cursos a partir de la titulación
  218. @PostMapping("/ajax/search/years")
  219. public String getYearsByCenterTitulation(Model model, Authentication auth,
  220. @RequestParam(name="centers[]", required=false) List<Integer> centres,
  221. @RequestParam("titulations[]") List<Integer> titulacions) throws IOException {
  222. HashMap<String, Object> response =
  223. pc.getYearsByCenterTitulation(centres, titulacions, ((Usuari) auth.getPrincipal()).getUsuari());
  224. if (response == null) {
  225. return "401";
  226. }
  227. model.addAllAttributes(response);
  228. return "components/selector_cursos";
  229. }
  230. // POST que se utiliza para conseguir los procedimiento que se han llevado a cabo por cursos y por titulación
  231. @PostMapping("/ajax/search/procedures")
  232. public String getProceduresByCenterTitulationYear(Model model, Authentication auth,
  233. @RequestParam(name="centers[]", required=false) List<Integer> centres,
  234. @RequestParam("years[]") List<Integer> cursos,
  235. @RequestParam("titulations[]") List<Integer> titulacions) throws IOException {
  236. HashMap<String, Object> response =
  237. pc.getProceduresByCenterTitulationYear(centres, cursos, titulacions, ((Usuari) auth.getPrincipal()).getUsuari());
  238. if (response == null) {
  239. return "401";
  240. }
  241. model.addAllAttributes(response);
  242. return "components/selector_processos";
  243. }
  244. @PostMapping("/ajax/find/procedure")
  245. public String findProcedure(Model model, Authentication auth, @RequestParam("procedure") Integer idProces,
  246. @RequestParam("action") String action) throws IOException {
  247. HashMap<String, Object> response =
  248. pc.findProcedure(idProces.toString(), action);
  249. if (response != null) {
  250. model.addAllAttributes(response);
  251. if (action.equals("remove")) {
  252. return "components/form_procedure_remove_confirm";
  253. } else if (action.equals("duplicate") || action.equals("edit") || idProces == 0) {
  254. return "components/form_procedure";
  255. }
  256. }
  257. return "401";
  258. }
  259. // POST para crear el form de creación de plantilla
  260. @PostMapping("/ajax/template/form")
  261. public String formTemplate(Model model, Authentication auth, @RequestParam("id") Integer idPlantilla,
  262. @RequestParam("action") String action) throws IOException {
  263. HashMap<String, Object> response =
  264. pc.formTemplate(idPlantilla, action);
  265. if (response != null && response.get("redirect") != null) {
  266. model.addAllAttributes(response);
  267. return (boolean) response.get("redirect") ? "components/form_template" : "";
  268. }
  269. return "401";
  270. }
  271. // GET para renderizar el formulario de cración de nueva tarea
  272. @GetMapping("/ajax/newTask/{i}")
  273. public String newTaskForm(Model model, Authentication auth, @PathVariable Integer i) throws IOException {
  274. List<RolDTO> roles = uc.findAssignables();
  275. List<Tipus> tipus = pc.findAll();
  276. List<Plantilla> templates = plc.findAll();
  277. model.addAttribute("tipus", tipus);
  278. model.addAttribute("roles", roles);
  279. model.addAttribute("i", i);
  280. model.addAttribute("templates", templates);
  281. return "components/form_procedure_task";
  282. }
  283. @ResponseBody
  284. @PostMapping("/ajax/find/template")
  285. public String findTemplate(Model model, Authentication auth, @RequestParam("id") Integer idPlantilla) throws IOException {
  286. Plantilla p = plc.findByID(idPlantilla);
  287. return p.getText();
  288. }
  289. // GET para comprobar si una plantilla esta siendo usada
  290. @ResponseBody
  291. @GetMapping("/ajax/template/used/{idPlantilla}")
  292. public int isTemplateUsed(Model model, Authentication auth, @PathVariable("idPlantilla") Integer idPlantilla) throws IOException {
  293. Boolean u = plc.isUsed(idPlantilla);
  294. return u ? 1 : 0;
  295. }
  296. @ResponseBody
  297. @PostMapping("/ajax/template/edit")
  298. public String templateEdit(Model model, Authentication auth, @RequestParam("id") Integer idPlantilla,@RequestParam("text") String text, @RequestParam("versio") Integer versio,
  299. @RequestParam("nomCas") String nomCas, @RequestParam("nomVal") String nomVal, @RequestParam("ambit") String ambit) throws IOException {
  300. Plantilla p = plc.findByID(idPlantilla);
  301. p.setNomCas(nomCas);
  302. p.setNomVal(nomVal);
  303. p.setText(text);
  304. p.setAmbit(ambit);
  305. plc.save(p);
  306. return "1";
  307. }
  308. @ResponseBody
  309. @PostMapping("/ajax/template/save")
  310. public String templateSave(Model model, Authentication auth, @RequestParam("text") String text, @RequestParam("codi") String codi, @RequestParam("versio") Integer versio,
  311. @RequestParam("nomCas") String nomCas, @RequestParam("nomVal") String nomVal, @RequestParam("ambit") String ambit) throws IOException {
  312. Plantilla p = new Plantilla();
  313. Plantilla p2 = plc.findByVersioCodiAmbit(versio, codi, ambit);
  314. if(p2 != null) {
  315. return "0";
  316. }
  317. p.setCodi(codi);
  318. p.setVersio(versio);
  319. p.setNomCas(nomCas);
  320. p.setNomVal(nomVal);
  321. p.setText(text);
  322. p.setAmbit(ambit);
  323. plc.save(p);
  324. return "1";
  325. }
  326. @ResponseBody
  327. @PostMapping("/ajax/draft/save/{id}")
  328. public String saveTemplate(Model model, Authentication auth, @PathVariable("id") Integer idPlantilla,
  329. @RequestParam("text") String text) throws IOException {
  330. Plantilla p = plc.findByID(idPlantilla);
  331. p.setText(text);
  332. plc.save(p);
  333. return "1";
  334. }
  335. @DeleteMapping("/ajax/template/form")
  336. @ResponseBody
  337. public String formTemplateRemove(Model model, Authentication auth, @RequestParam("id") Integer idPlantilla) throws IOException {
  338. if(plc.isUsed(idPlantilla)){
  339. return "0";
  340. }
  341. else {
  342. Plantilla p = plc.findByID(idPlantilla);
  343. plc.delete(p);
  344. return "1";
  345. }
  346. }
  347. @PostMapping("/ajax/find/template/inds")
  348. public String findTemplatesInds(Model model, Authentication auth, @RequestParam("procedure") Integer idProces,
  349. @RequestParam("center") String idCentre, @RequestParam("titulation") String idTitulacio,
  350. @RequestParam("ev") String evidencia) throws IOException {
  351. Proces p = this.pc.findProcesByID(idProces);
  352. List<EvidenciaIndicadorEnquesta> inds = this.pc.getByProcesEvidencia(p.getNomProces(), evidencia);
  353. model.addAttribute("inds", inds);
  354. model.addAttribute("proces", p.getNomProces());
  355. model.addAttribute("idCentre", idCentre);
  356. model.addAttribute("idTitulacio", idTitulacio);
  357. model.addAttribute("evidencia", evidencia);
  358. return "components/form_templates_indicators";
  359. }
  360. }