Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.98 KB | None | 0 0
  1. @Component
  2. public class GraficiUserFormValidator implements Validator {
  3.  
  4. @Override
  5. public boolean supports(Class<?> arg0) {
  6. return arg0.equals(GraficiUserForm.class);
  7. }
  8.  
  9. @Override
  10. public void validate(Object arg0, Errors arg1) {
  11. GraficiUserForm form = (GraficiUserForm) arg0;
  12. validateCheckBoxes(arg1, form);
  13.  
  14. }
  15.  
  16. private void validateCheckBoxes(Errors arg1, GraficiUserForm form) {
  17. if ((form.getG1().equals("on") && form.getG2().equals("on") && form.getG3().equals("on") && form.getG4().equals("on") &&
  18. form.getG5().equals("on") && form.getG6().equals("on")) ||(form.getG1().equals("on") && form.getG2().equals("on") && form.getG3().equals("on") && form.getG4().equals("on") &&
  19. form.getG5().equals("on"))||(form.getG1().equals("on") && form.getG2().equals("on") && form.getG3().equals("on") && form.getG4().equals("on") &&
  20. form.getG6().equals("on"))||(form.getG1().equals("on") && form.getG2().equals("on") && form.getG3().equals("on") && form.getG5().equals("on") &&
  21. form.getG6().equals("on"))||(form.getG1().equals("on") && form.getG2().equals("on") && form.getG4().equals("on") && form.getG5().equals("on") &&
  22. form.getG6().equals("on"))||(form.getG1().equals("on") && form.getG3().equals("on") && form.getG4().equals("on") && form.getG5().equals("on") &&
  23. form.getG6().equals("on"))||(form.getG2().equals("on") && form.getG3().equals("on") && form.getG4().equals("on") && form.getG5().equals("on") &&
  24. form.getG6().equals("on"))
  25. ){
  26. arg1.reject("grafici.tooMany", "Troppi grafici selezionati, si possono selezionare solo 4 grafici");
  27. }
  28.  
  29. }
  30.  
  31. }
  32.  
  33. @Controller
  34. public class UsersController {
  35.  
  36. private static final Logger LOGGER = LoggerFactory.getLogger(UsersController.class);
  37. private final UserService userService;
  38. private final SitoService sitoService;
  39. private final ClusterService clusterService;
  40. private final GraficiUserFormValidator graficiUserFormValidator;
  41.  
  42. @Autowired
  43. public UsersController(UserService userService, ClusterService clusterService, SitoService sitoService, GraficiUserFormValidator graficiUserFormValidator) {
  44. this.userService = userService;
  45. this.sitoService = sitoService;
  46. this.clusterService = clusterService;
  47. this.graficiUserFormValidator = graficiUserFormValidator;
  48. }
  49.  
  50. @InitBinder("form")
  51. public void initBinder(WebDataBinder binder) {
  52. binder.addValidators(graficiUserFormValidator);
  53. }
  54.  
  55. @PreAuthorize("hasAuthority('ADMIN') or hasAuthority('CLUSTERADMIN') or hasAuthority('SITEADMIN')")
  56. @RequestMapping(value = "/update/settings", method = RequestMethod.POST)
  57. public String updateSettingsGraphs(@Valid GraficiUserForm form, HttpSession session, HttpServletRequest request, BindingResult result,
  58. Model model, Long id){
  59. SecurityContextHolderAwareRequestWrapper wrapper = new SecurityContextHolderAwareRequestWrapper(request, null);
  60.  
  61. if (result.hasErrors()) {
  62. if (wrapper.isUserInRole("ADMIN")) {
  63. result.reject("graphs.tooMany", "Puoi selezionare solo quattro grafici alla volta");
  64. Collection<User> users = userService.getAllUsers();
  65. model.addAttribute("users", users);
  66.  
  67.  
  68. for (User user : users) {
  69. model.addAttribute("form"+user.getId().toString(), new GraficiUserForm());
  70. }
  71.  
  72. return "users";
  73. }
  74.  
  75. else if (wrapper.isUserInRole("CLUSTERADMIN")) {
  76. result.reject("graphs.tooMany", "Puoi selezionare solo quattro grafici alla volta");
  77. Collection<User> users = clusterService.getAllByCluster(id);
  78. model.addAttribute("users", users);
  79.  
  80.  
  81. for (User user : users) {
  82. model.addAttribute("form"+user.getId().toString(), new GraficiUserForm());
  83. }
  84. return "users-cluster";
  85. }
  86.  
  87. else {
  88. result.reject("graphs.tooMany", "Puoi selezionare solo quattro grafici alla volta");
  89. Collection<User> users = sitoService.getAllBySite(id);
  90. model.addAttribute("users", users);
  91.  
  92.  
  93. for (User user : users) {
  94. model.addAttribute("form"+user.getId().toString(), new GraficiUserForm());
  95. }
  96. return "users-site";
  97. }
  98. }
  99. User user = userService.getUserById(form.getUserid());
  100. if("on".equals(form.getG1())){
  101. user.setG1("S");
  102. } else {
  103. user.setG1("N");
  104. }
  105. if("on".equals(form.getG2())){
  106. user.setG2("S");
  107. } else {
  108. user.setG2("N");
  109. }
  110. if("on".equals(form.getG3())){
  111. user.setG3("S");
  112. } else {
  113. user.setG3("N");
  114. }
  115. if("on".equals(form.getG4())){
  116. user.setG4("S");
  117. } else {
  118. user.setG4("N");
  119. }
  120. if("on".equals(form.getG5())){
  121. user.setG5("S");
  122. } else {
  123. user.setG5("N");
  124. }
  125. if("on".equals(form.getG6())){
  126. user.setG6("S");
  127. } else {
  128. user.setG6("N");
  129. }
  130. user = userService.save(user);
  131. if (wrapper.isUserInRole("ADMIN")) {
  132. return "redirect:/users";
  133. }
  134.  
  135. else if (wrapper.isUserInRole("CLUSTERADMIN")) {
  136. return "redirect:/users-cluster";
  137. }
  138.  
  139. else {
  140. return "redirect:/users-site";
  141. }
  142.  
  143. }
  144.  
  145. }
  146.  
  147. <#-- @ftlvariable name="users" type="java.util.List<it.energyway.application.domain.User>" -->
  148. <#-- @ftlvariable name="_csrf" type="org.springframework.security.web.csrf.CsrfToken" -->
  149. <#list users as user>
  150. <#-- @ftlvariable name="form${user.id}" type="it.energyway.application.domain.GraficiUserForm" -->
  151. </#list>
  152. <#import "/spring.ftl" as spring>
  153. <!DOCTYPE html>
  154. <html lang="it">
  155. <head>
  156. <meta charset="utf-8">
  157. <title>List of Users</title>
  158. </head>
  159. <body>
  160.  
  161. <nav role="navigation">
  162. <ul>
  163. <li><a href="/">Dashboard</a></li>
  164. <li><a href="/user/create">Create a new user</a></li>
  165. <li>Amministrazione
  166. <ul>
  167. <li><a href="/users">Gestione Utenti e Permessi</a></li>
  168. </ul>
  169. </li>
  170. </ul>
  171. </nav>
  172.  
  173. <h1>Utenti</h1>
  174.  
  175. <table>
  176. <thead>
  177. <tr>
  178. <th>E-mail</th>
  179. <th>Role</th>
  180. <th>Grafico 1</th>
  181. <th>Grafico 2</th>
  182. <th>Grafico 3</th>
  183. <th>Grafico 4</th>
  184. <th>Grafico 5</th>
  185. <th>Grafico 6</th>
  186. <th>Cambio</th>
  187. </tr>
  188. </thead>
  189. <tbody>
  190. <#list users as user>
  191. <form role="form${user.id}" name="form${user.id}" action="/update/settings" method="post">
  192. <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
  193. <input type="hidden" name="userid" id="userid" value="${user.id}"/>
  194. <tr>
  195. <td><a href="/user/${user.id}">${user.email}</a></td>
  196. <td>${user.role}</td>
  197. <td><input class="graphs" type="checkbox" name="g1" <#if (user.g1 == "S")>checked=true<#else></#if></td>
  198. <td><input class="graphs" type="checkbox" name="g2" <#if (user.g2 == "S")>checked=true<#else></#if></td>
  199. <td><input class="graphs" type="checkbox" name="g3" <#if (user.g3 == "S")>checked=true<#else></#if></td>
  200. <td><input class="graphs" type="checkbox" name="g4" <#if (user.g4 == "S")>checked=true<#else></#if></td>
  201. <td><input class="graphs" type="checkbox" name="g5" <#if (user.g5 == "S")>checked=true<#else></#if></td>
  202. <td><input class="graphs" type="checkbox" name="g6" <#if (user.g6 == "S")>checked=true<#else></#if></td>
  203. <td><button type="submit">Save</button></td>
  204. </tr>
  205. </form>
  206. </#list>
  207. </tbody>
  208. </table>
  209. <#list users as user>
  210. <@spring.bind "form${user.id}" />
  211. </#list>
  212. <#if (spring.status.error)??>
  213. <ul>
  214. <#list spring.status.errorMessages as error>
  215. <li>${error}</li>
  216. </#list>
  217. </ul><#else>
  218. </#if>
  219. </body>
  220. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement