Guest User

Untitled

a guest
Mar 8th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.81 KB | None | 0 0
  1. package it.teragate.gateenergy.web;
  2.  
  3. import it.teragate.gateenergy.contants.Constants;
  4. import it.teragate.gateenergy.entity.Impianto;
  5. import it.teragate.gateenergy.entity.Utente;
  6. import it.teragate.gateenergy.entity.assembler.AnagraficaAssembler;
  7. import it.teragate.gateenergy.model.AnagraficaExt;
  8. import it.teragate.gateenergy.notification.entity.Contatto;
  9. import it.teragate.gateenergy.notification.entity.RubricaPersonale;
  10. import it.teragate.gateenergy.notification.entity.TemplateNotifica;
  11. import it.teragate.gateenergy.services.AnagraficaService;
  12. import it.teragate.gateenergy.services.ContattoService;
  13. import it.teragate.gateenergy.services.GroupService;
  14. import it.teragate.gateenergy.services.ImpiantoService;
  15. import it.teragate.gateenergy.services.RoleService;
  16. import it.teragate.gateenergy.services.RubricaPersonaleService;
  17. import it.teragate.gateenergy.services.TemplateNotificaService;
  18. import it.teragate.gateenergy.services.UserService;
  19. import it.teragate.gateenergy.services.UtenteService;
  20. import it.teragate.gateenergy.utils.GeMail;
  21. import it.teragate.gateenergy.web.validator.AnagraficaValidator;
  22. import it.teragate.registry.Anagrafica;
  23. import it.teragate.registry.Group;
  24. import it.teragate.registry.User;
  25. import it.teragate.registry.exceptions.AuthenticationException;
  26. import it.teragate.registry.util.UserHelper;
  27.  
  28. import java.security.NoSuchAlgorithmException;
  29. import java.util.ArrayList;
  30. import java.util.Iterator;
  31. import java.util.List;
  32.  
  33. import javax.servlet.http.HttpServletRequest;
  34.  
  35. import org.springframework.beans.factory.annotation.Autowired;
  36. import org.springframework.security.core.context.SecurityContextHolder;
  37. import org.springframework.stereotype.Controller;
  38. import org.springframework.ui.ModelMap;
  39. import org.springframework.validation.BindingResult;
  40. import org.springframework.web.bind.annotation.ModelAttribute;
  41. import org.springframework.web.bind.annotation.PathVariable;
  42. import org.springframework.web.bind.annotation.RequestMapping;
  43. import org.springframework.web.bind.annotation.RequestMethod;
  44.  
  45. @Controller
  46. public class AnagraficaController {
  47. public static Long ROLE_USER = 2L;
  48. final static String subject = "Invio credenziali"; // TODO: properties
  49. private String text = "Grazie per essersi registrato, \n\n le sue credenziali d'accesso sono:\n ";
  50. private UtenteService utenteService;
  51. private AnagraficaService anagraficaService;
  52. private RoleService roleService;
  53. private UserService subjectService;
  54. private AnagraficaValidator anagraficaValidator;
  55. private GroupService groupService;
  56. private ImpiantoService impiantoService;
  57. private UserService userService;
  58. private RubricaPersonaleService rubricaPersonaleService;
  59. private ContattoService contattoService;
  60. private TemplateNotificaService templateNotificaService;
  61.  
  62.  
  63.  
  64. @Autowired
  65. public void setTemplateNotificaService(TemplateNotificaService templateNotificaService) {
  66. this.templateNotificaService = templateNotificaService;
  67. }
  68.  
  69. @RequestMapping(value = "/profilo/{id}/edit", method = RequestMethod.GET)
  70. public String editprofilo(@PathVariable("id") Long id, ModelMap modelMap) {
  71. if (id != null) {
  72. List<Anagrafica> anagraficaList = anagraficaService.findByProperty("user", id);
  73. Anagrafica anagrafica = null;
  74. AnagraficaAssembler anagraficaAssembler = new AnagraficaAssembler();
  75. AnagraficaExt model = new AnagraficaExt();
  76. if (!anagraficaList.isEmpty()) {
  77. anagrafica = anagraficaList.get(0);
  78. model = (AnagraficaExt) anagraficaAssembler.fromEntity(anagrafica, model);
  79. }
  80. modelMap.addAttribute("anagrafica", model);
  81. return "profiloUtente";
  82. } else {
  83. return "redirect:/login.htm";
  84. }
  85.  
  86. }
  87.  
  88. @RequestMapping(value = "/profilo/{id}/edit", method = RequestMethod.POST)
  89. public String onSubmitModificaProfilo(@PathVariable("id") Long id, @ModelAttribute("anagrafica") AnagraficaExt model, BindingResult result, ModelMap modelMap, HttpServletRequest request) {
  90. User user = null;
  91. Anagrafica anagrafica = new Anagrafica();
  92.  
  93. try {
  94. user = utenteService.getUser(SecurityContextHolder.getContext().getAuthentication().getName());
  95. model.setUser(user);
  96. } catch (AuthenticationException e) {
  97. return "redirect:/login.htm";
  98. }
  99. if (model.getId() != null) {
  100. anagrafica = anagraficaService.findById(model.getId());
  101. }
  102. anagraficaValidator.validateProfilo(model, result);
  103. if (!result.hasErrors()) {
  104. AnagraficaAssembler anagraficaAssembler = new AnagraficaAssembler();
  105. anagrafica = (Anagrafica) anagraficaAssembler.fromModel(model, anagrafica);
  106. Utente utente = anagrafica.getUser().getUtente();
  107. utente.setNominativo(anagrafica.getNome() + " " +anagrafica.getCognome());
  108. try {
  109. anagrafica.getUser().setPassword(UserHelper.getInstance().encode(model.getNuovaPassword()));
  110. } catch (NoSuchAlgorithmException e) {
  111. e.printStackTrace();
  112. }
  113. utente.setUser(anagrafica.getUser());
  114. utenteService.saveOrUpdate(utente);
  115. anagraficaService.saveOrUpdate(anagrafica);
  116.  
  117. TemplateNotifica templateNotifica = templateNotificaService.findById(-3L);
  118.  
  119. if(!model.getVecchiaPassword().equals(model.getNuovaPassword())){
  120. String testo = "";
  121. testo += templateNotifica.getTesto();
  122. testo = testo +"username: " + anagrafica.getUser().getUsername() + ", password: " + model.getNuovaPassword();
  123. String oggetto = templateNotifica.getSubject();
  124. GeMail.getInstance().sendMail(anagrafica.getEmail(), oggetto, testo, null);
  125. }
  126. } else {
  127. // sono error in modifica
  128. popolaModelMap(modelMap, model);
  129. return "profiloUtente";
  130. }
  131.  
  132. return "redirect:/impiantoHome.htm";
  133.  
  134. }
  135.  
  136. /**
  137. * metodo get per la creazione della form di ricerca
  138. *
  139. * @param modelMap
  140. * @return
  141. */
  142. @RequestMapping(value = "/anagraficaSearchForm", method = RequestMethod.GET)
  143. public String anagraficaSearch(ModelMap modelMap) {
  144. AnagraficaExt anagrafica = new AnagraficaExt();
  145. List anagraficaList = new ArrayList();
  146. Utente utente = null;
  147. User user = null;
  148. try {
  149. user = utenteService.getUser(SecurityContextHolder.getContext().getAuthentication().getName());
  150. } catch (AuthenticationException e) {
  151. }
  152. if (user.isAdmin()) {
  153. anagraficaList = anagraficaService.findAnagraficaFromModelForAdmin(anagrafica);
  154. } else {
  155. utente = utenteService.findByUser(user.getId());
  156. anagrafica.setUtenteCorrente(utente);
  157. anagraficaList = anagraficaService.findAnagraficaFromModel(anagrafica);
  158. }
  159. modelMap.addAttribute("anagraficaList", anagraficaList);
  160. modelMap.addAttribute("anagrafica", anagrafica);
  161. return "anagraficaSearchForm";
  162. }
  163.  
  164. /**
  165. * metodo post per la ricerca delle anagrafiche
  166. *
  167. * @param modelMap
  168. * @param model
  169. * @param result
  170. * @return
  171. */
  172.  
  173. @RequestMapping(value = "/anagraficaSearchForm", method = RequestMethod.POST)
  174. public String anagraficaSearch(ModelMap modelMap, @ModelAttribute("anagrafica") AnagraficaExt model, BindingResult result) {
  175. List anagraficaList = new ArrayList();
  176. Utente utente = null;
  177. User user = null;
  178. try {
  179. user = utenteService.getUser(SecurityContextHolder.getContext().getAuthentication().getName());
  180. } catch (AuthenticationException e) {
  181. }
  182. if (user.isAdmin()) {
  183. anagraficaList = anagraficaService.findAnagraficaFromModelForAdmin(model);
  184. } else {
  185. utente = utenteService.findByUser(user.getId());
  186. model.setUtenteCorrente(utente);
  187. anagraficaList = anagraficaService.findAnagraficaFromModel(model);
  188. }
  189. modelMap.addAttribute("anagrafica", model);
  190. modelMap.addAttribute("anagraficaList", anagraficaList);
  191. return "anagraficaSearchForm";
  192. }
  193.  
  194. /**
  195. * metodo get per la modifica di una anagrafica
  196. *
  197. * @param id
  198. * @param modelMap
  199. * @return
  200. */
  201. @RequestMapping(value = "/anagraficaEditForm/{id}/edit", method = RequestMethod.GET)
  202. public String editForm(@PathVariable("id") Long id, ModelMap modelMap) {
  203. if (id != null) {
  204. List<Impianto> impiantoList = new ArrayList();
  205. List<Group> groupList = new ArrayList<Group>();
  206. AnagraficaAssembler anagraficaAssembler = new AnagraficaAssembler();
  207. Anagrafica anagrafica = anagraficaService.findById(id);
  208. AnagraficaExt model = new AnagraficaExt();
  209. model.setReadOnly(false);
  210. model = (AnagraficaExt) anagraficaAssembler.fromEntity(anagrafica, model);
  211.  
  212. User user = null;
  213. Utente utente = null;
  214. try {
  215. user = utenteService.getUser(SecurityContextHolder.getContext().getAuthentication().getName());
  216. } catch (AuthenticationException e) {
  217. }
  218. if (user.isAdmin()) {
  219. impiantoList = impiantoService.find(Constants.Q_SELECT_IMPIANTOS_ADMIN, null, null);
  220. } else {
  221. utente = utenteService.findByUser(user.getId());
  222. Object[] values = new Object[] { utente.getId() };
  223. impiantoList = impiantoService.find(Constants.Q_SELECT_IMPIANTOS_BY_ID_UTENTE, values, null);
  224. }
  225. groupList = groupService.findGroupById(user.getGroups().iterator().next().getId());
  226.  
  227. modelMap.addAttribute("impiantoList", impiantoList);
  228. modelMap.addAttribute("anagrafica", model);
  229. modelMap.addAttribute("groupList", groupList);
  230. return "anagraficaEditForm";
  231. } else {
  232. return "redirect:/anagraficaSearchForm.htm";
  233. }
  234.  
  235. }
  236.  
  237. /**
  238. * metodo get per la delete di una anagrafica
  239. *
  240. * @param id
  241. * @param modelMap
  242. * @return
  243. */
  244. @RequestMapping(value = "/anagraficaEditForm/{id}/delete", method = RequestMethod.GET)
  245. public String Delete(@PathVariable("id") Long id, ModelMap modelMap) {
  246. if (id != null) {
  247. Anagrafica anagrafica = anagraficaService.findById(id);
  248. anagrafica.setStatus(Anagrafica.Status.DISABLED);
  249. anagrafica.getUser().setStatus(User.Status.DISABLED);
  250. anagrafica.getUser().setEnabled(false);
  251. anagrafica.getUser().setPassword("");
  252. anagrafica.getUser().setUsername("_"+anagrafica.getUser().getUsername());
  253. // subjectService.update(anagrafica.getUser());
  254. anagraficaService.update(anagrafica);
  255. subjectService.update(anagrafica.getUser());
  256. }
  257. return "redirect:/anagraficaSearchForm.htm";
  258. }
  259.  
  260. /**
  261. * metodo get per la visualizzazione dettagliata di una anagrafica
  262. *
  263. * @param id
  264. * @param modelMap
  265. * @return
  266. */
  267. @RequestMapping(value = "/anagraficaEditForm/{id}/view", method = RequestMethod.GET)
  268. public String view(@PathVariable("id") Long id, ModelMap modelMap) {
  269. if (id != null) {
  270. List<Group> groupList = new ArrayList<Group>();
  271. List<Impianto> impiantoList = new ArrayList();
  272. AnagraficaAssembler anagraficaAssembler = new AnagraficaAssembler();
  273. Anagrafica anagrafica = anagraficaService.findById(id);
  274. AnagraficaExt model = new AnagraficaExt();
  275. model.setReadOnly(true);
  276. model = (AnagraficaExt) anagraficaAssembler.fromEntity(anagrafica, model);
  277. modelMap.addAttribute("anagrafica", model);
  278.  
  279. User user = null;
  280. Utente utente = null;
  281. try {
  282. user = utenteService.getUser(SecurityContextHolder.getContext().getAuthentication().getName());
  283. } catch (AuthenticationException e) {
  284. }
  285. if (user.isAdmin()) {
  286. impiantoList = impiantoService.find(Constants.Q_SELECT_IMPIANTOS_ADMIN, null, null);
  287. } else {
  288. utente = utenteService.findByUser(user.getId());
  289. Object[] values = new Object[] { utente.getId() };
  290. impiantoList = impiantoService.find(Constants.Q_SELECT_IMPIANTOS_BY_ID_UTENTE, values, null);
  291. }
  292. groupList = groupService.findGroupById(user.getGroups().iterator().next().getId());
  293.  
  294. modelMap.addAttribute("impiantoList", impiantoList);
  295. modelMap.addAttribute("groupList", groupList);
  296. return "anagraficaEditForm";
  297. } else {
  298. return "redirect:/anagraficaSearchForm.htm";
  299. }
  300. }
  301.  
  302. /**
  303. * Metodo get per la creazione della form
  304. *
  305. * @param modelMap
  306. * @param request
  307. * @return
  308. */
  309.  
  310. @RequestMapping(value = "anagraficaEditForm", method = RequestMethod.GET)
  311. public String createForm(ModelMap modelMap, HttpServletRequest request) {
  312. AnagraficaExt anagrafica = new AnagraficaExt();
  313. popolaModelMap(modelMap, anagrafica);
  314. return "anagraficaEditForm";
  315. }
  316.  
  317. /**
  318. * metodo post per l'inserimento e modifica di un anagrafica
  319. *
  320. * @param model
  321. * @param result
  322. * @param modelMap
  323. * @param request
  324. * @return
  325. */
  326. @RequestMapping(method = RequestMethod.POST)
  327. public String onSubmit(@ModelAttribute("anagrafica") AnagraficaExt model, BindingResult result, ModelMap modelMap, HttpServletRequest request) {
  328. User user = null;
  329. try {
  330. user = utenteService.getUser(SecurityContextHolder.getContext().getAuthentication().getName());
  331. } catch (AuthenticationException e) {
  332. return "redirect:/anagraficaSearchForm.htm";
  333. }
  334. Anagrafica anagrafica = new Anagrafica();
  335. boolean update = false;
  336.  
  337. if (model.getId() != null) {
  338. anagrafica = anagraficaService.findById(model.getId());
  339. update = true;
  340. }
  341. anagraficaValidator.validate(model, result);
  342. if (!result.hasErrors()) {
  343. AnagraficaAssembler anagraficaAssembler = new AnagraficaAssembler();
  344.  
  345. Group group = null;
  346. if (model.getId() == null) {
  347. anagrafica = (Anagrafica) anagraficaAssembler.fromModel(model, anagrafica);
  348. Utente subOf = utenteService.findById(user.getId());
  349. anagrafica.getUser().setUsername(utenteService.creaUsername(anagrafica));
  350. group = groupService.findById(model.getIdGroup());
  351. anagrafica.getUser().getGroups().add(group);
  352.  
  353. Utente utente = new Utente(anagrafica.getNome() + " " + anagrafica.getCognome(), subOf);
  354. utente.setUser(anagrafica.getUser());
  355. if (model.getImpianti() != null) {
  356. if (model.getImpianti().length > 0) {
  357. for (Long s : model.getImpianti()) {
  358. Impianto i = impiantoService.findById(s);
  359. utente.getImpiantos().add(i);
  360. }
  361. }
  362. }
  363. utenteService.saveOrUpdate(utente);
  364.  
  365. if (!user.isAdmin()) {
  366. List<RubricaPersonale> rubricaPersonaleList= new ArrayList<RubricaPersonale>();
  367. rubricaPersonaleList = rubricaPersonaleService.findByProperty("utente", user.getUtente());
  368. Contatto contatto =new Contatto();
  369. contatto.setNominativo(utente.getNominativo());
  370. contatto.setRiferimento(anagrafica.getEmail());
  371. contatto.setTpContatto(1L);
  372. contattoService.saveOrUpdate(contatto);
  373. if(rubricaPersonaleList.isEmpty()){
  374. RubricaPersonale rubrica= new RubricaPersonale(user.getUtente(),"Rubrica Contatti");
  375. rubrica.getContatti().add(contatto);
  376. rubrica.getUtenti().add(utente);
  377. rubricaPersonaleService.saveOrUpdate(rubrica);
  378. }else{
  379. for (RubricaPersonale rubricaPersonale : rubricaPersonaleList) {
  380. rubricaPersonale.getContatti().add(contatto);
  381. rubricaPersonale.getUtenti().add(utente);
  382. rubricaPersonaleService.saveOrUpdate(rubricaPersonale);
  383. }
  384. }
  385. }
  386. // creo la rubrica per l'utente che stò creando
  387. List<RubricaPersonale> rubricaUtenteCreatoList= new ArrayList<RubricaPersonale>();
  388. rubricaUtenteCreatoList = rubricaPersonaleService.findByProperty("utente",utente);
  389. if(rubricaUtenteCreatoList.isEmpty()){
  390. RubricaPersonale rubricaUteneCreato= new RubricaPersonale(utente,"Rubrica Contatti");
  391. rubricaPersonaleService.saveOrUpdate(rubricaUteneCreato);
  392. }
  393.  
  394.  
  395. } else {
  396. anagrafica = anagraficaService.findById(model.getId());
  397. anagrafica = (Anagrafica) anagraficaAssembler.fromModel(model, anagrafica);
  398. Utente utente = anagrafica.getUser().getUtente();
  399. Iterator it = anagrafica.getUser().getGroups().iterator();
  400. it.next();
  401. it.remove();
  402. group = groupService.findById(model.getIdGroup());
  403. anagrafica.getUser().getGroups().add(group);
  404.  
  405. if (!anagrafica.getUser().getUtente().getImpiantos().isEmpty()) {
  406. Iterator itImp = utente.getImpiantos().iterator();
  407. while (itImp.hasNext()) {
  408. itImp.next();
  409. itImp.remove();
  410. }
  411. }
  412. if (model.getImpianti() != null) {
  413. if (model.getImpianti().length > 0) {
  414. for (Long s : model.getImpianti()) {
  415. Impianto i = impiantoService.findById(s);
  416. utente.getImpiantos().add(i);
  417. }
  418. }
  419. }
  420.  
  421. utenteService.saveOrUpdate(utente);
  422.  
  423.  
  424. }
  425. TemplateNotifica templateNotifica = templateNotificaService.findById(-3L);
  426. String saveOrUpdate = anagraficaService.saveOrUpdate(anagrafica);
  427. if ((!update || !"update".equals(saveOrUpdate))) {
  428. String testo = "";
  429. testo += templateNotifica.getTesto();//text;
  430. testo = testo +" username: " + anagrafica.getUser().getUsername() + ", password: " + anagrafica.getCognome().replaceAll(" ", ".").trim().toLowerCase(); //anagrafica.getUser().getPassword();
  431. String body = testo;
  432. GeMail.getInstance().sendMail(anagrafica.getEmail(), templateNotifica.getSubject(), body, null);
  433. }
  434. } else {
  435. // sono error in modifica
  436. popolaModelMap(modelMap, model);
  437. return "anagraficaEditForm";
  438. }
  439.  
  440. return "redirect:/anagraficaSearchForm.htm";
  441.  
  442. }
  443.  
  444. /**
  445. * metodo di utlità per il precaricamento del model map
  446. *
  447. * @param modelMap
  448. * @param model
  449. * @return
  450. */
  451. private ModelMap popolaModelMap(ModelMap modelMap, AnagraficaExt anagrafica) {
  452. List<Group> groupList = new ArrayList<Group>();
  453. List<Impianto> impiantoList = new ArrayList();
  454. User user = null;
  455. Utente utente = null;
  456. try {
  457. user = utenteService.getUser(SecurityContextHolder.getContext().getAuthentication().getName());
  458. } catch (AuthenticationException e) {
  459. }
  460. if (user.isAdmin()) {
  461. impiantoList = impiantoService.find(Constants.Q_SELECT_IMPIANTOS_ADMIN, null, null);
  462. } else {
  463. utente = user.getUtente();
  464. Object[] values = new Object[] { utente.getId() };
  465. impiantoList = impiantoService.find(Constants.Q_SELECT_IMPIANTOS_BY_ID_UTENTE, values, null);
  466. }
  467. groupList = groupService.findGroupById(user.getGroups().iterator().next().getId());
  468. modelMap.addAttribute("impiantoList", impiantoList);
  469. modelMap.addAttribute("groupList", groupList);
  470. modelMap.addAttribute("anagrafica", anagrafica);
  471. modelMap.addAttribute("date_format", "dd/MM/yyyy");
  472. return modelMap;
  473. }
  474.  
  475.  
  476. public RubricaPersonaleService getRubricaPersonaleService() {
  477. return rubricaPersonaleService;
  478. }
  479. @Autowired
  480. public void setRubricaPersonaleService(RubricaPersonaleService rubricaPersonaleService) {
  481. this.rubricaPersonaleService = rubricaPersonaleService;
  482. }
  483.  
  484.  
  485. @Autowired
  486. public void setUtenteService(UtenteService utenteService) {
  487. this.utenteService = utenteService;
  488. }
  489.  
  490. public UtenteService getUtenteService() {
  491. return utenteService;
  492. }
  493.  
  494. @Autowired
  495. public void setPersonService(AnagraficaService anagraficaService) {
  496. this.anagraficaService = anagraficaService;
  497. }
  498.  
  499. public AnagraficaService getAnagraficaService() {
  500. return anagraficaService;
  501. }
  502.  
  503. @Autowired
  504. public void setRoleService(RoleService roleService) {
  505. this.roleService = roleService;
  506. }
  507.  
  508. public RoleService getRoleService() {
  509. return roleService;
  510. }
  511.  
  512. @Autowired
  513. public void setSubjectService(UserService subjectService) {
  514. this.subjectService = subjectService;
  515. }
  516.  
  517. public UserService getSubjectService() {
  518. return subjectService;
  519. }
  520.  
  521. public AnagraficaValidator getAnagraficaValidator() {
  522. return anagraficaValidator;
  523. }
  524.  
  525. @Autowired
  526. public void setPersonValidator(AnagraficaValidator anagraficaValidator) {
  527. this.anagraficaValidator = anagraficaValidator;
  528. }
  529.  
  530. @Autowired
  531. public void setGroupService(GroupService groupService) {
  532. this.groupService = groupService;
  533. }
  534.  
  535. public GroupService getGroupService() {
  536. return groupService;
  537. }
  538.  
  539. @Autowired
  540. public void setImpiantoService(ImpiantoService impiantoService) {
  541. this.impiantoService = impiantoService;
  542. }
  543.  
  544. public ImpiantoService getImpiantoService() {
  545. return impiantoService;
  546. }
  547.  
  548. @Autowired
  549. public void setUserService(UserService userService) {
  550. this.userService = userService;
  551. }
  552.  
  553. public UserService getUserService() {
  554. return userService;
  555. }
  556. public ContattoService getContattoService() {
  557. return contattoService;
  558. }
  559. @Autowired
  560. public void setContattoService(ContattoService contattoService) {
  561. this.contattoService = contattoService;
  562. }
  563.  
  564. }
Add Comment
Please, Sign In to add comment