/src/main/java/com/google/ie/web/controller/AdminController.java

http://thoughtsite.googlecode.com/ · Java · 649 lines · 354 code · 34 blank · 261 comment · 51 complexity · 27e7184272680c7f49b637f5eb20857e MD5 · raw file

  1. /* Copyright 2010 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS.
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License
  14. */
  15. package com.google.ie.web.controller;
  16. import static com.google.ie.web.controller.WebConstants.ERROR;
  17. import static com.google.ie.web.controller.WebConstants.SUCCESS;
  18. import static com.google.ie.web.controller.WebConstants.VIEW_STATUS;
  19. import com.google.ie.business.domain.AdminRequest;
  20. import com.google.ie.business.domain.Comment;
  21. import com.google.ie.business.domain.Idea;
  22. import com.google.ie.business.domain.Project;
  23. import com.google.ie.business.domain.User;
  24. import com.google.ie.business.service.AdminService;
  25. import com.google.ie.business.service.CommentService;
  26. import com.google.ie.business.service.ProjectService;
  27. import com.google.ie.business.service.UserService;
  28. import com.google.ie.common.builder.IdeaBuilder;
  29. import com.google.ie.common.builder.ProjectBuilder;
  30. import com.google.ie.common.exception.IdeasExchangeException;
  31. import com.google.ie.dto.IdeaDetail;
  32. import com.google.ie.dto.ProjectDetail;
  33. import com.google.ie.dto.RetrievalInfo;
  34. import com.google.ie.dto.ViewStatus;
  35. import org.apache.log4j.Logger;
  36. import org.springframework.beans.factory.annotation.Autowired;
  37. import org.springframework.beans.factory.annotation.Qualifier;
  38. import org.springframework.stereotype.Controller;
  39. import org.springframework.web.bind.annotation.ModelAttribute;
  40. import org.springframework.web.bind.annotation.PathVariable;
  41. import org.springframework.web.bind.annotation.RequestMapping;
  42. import org.springframework.web.bind.annotation.RequestParam;
  43. import org.springframework.web.bind.annotation.SessionAttributes;
  44. import java.util.HashMap;
  45. import java.util.List;
  46. import java.util.Map;
  47. import javax.servlet.http.HttpServletRequest;
  48. import javax.servlet.http.HttpSession;
  49. /**
  50. * A controller that handles requests for admin user for delete idea,approve and
  51. * deny request for duplicate and objectionable for ideas and comments.
  52. *
  53. * @author Surabhi
  54. */
  55. @Controller
  56. @RequestMapping("/admin")
  57. @SessionAttributes("user")
  58. public class AdminController {
  59. /* Logger for logging information. */
  60. private static Logger log = Logger.getLogger(AdminController.class);
  61. /* Services autowired for invoking service methods */
  62. @Autowired
  63. @Qualifier("ideaCommentServiceImpl")
  64. private CommentService ideaCommentService;
  65. @Autowired
  66. @Qualifier("projectCommentServiceImpl")
  67. private CommentService projectCommentService;
  68. @Autowired
  69. private AdminService adminService;
  70. @Autowired
  71. private ProjectService projectService;
  72. @Autowired
  73. private UserService userService;
  74. /* Builder for building object of idea and project */
  75. @Autowired
  76. private ProjectBuilder projectBuilder;
  77. @Autowired
  78. private IdeaBuilder ideaBuilder;
  79. /**
  80. * Get View Status For Invalid User.
  81. *
  82. * @return ViewStatus
  83. */
  84. private ViewStatus getViewStatusForInvalidUser() {
  85. ViewStatus viewStatus = new ViewStatus();
  86. viewStatus.setStatus(ERROR);
  87. viewStatus.addMessage(ERROR, WebConstants.INVALID_USER);
  88. return viewStatus;
  89. }
  90. /**
  91. *
  92. * Handles request for view Action inbox items request
  93. *
  94. * @param retrievalInfo RetrievalInfo provide detail for fetching data
  95. * @param requestType String
  96. * @param model Map request map
  97. * @param req HttpServletRequest
  98. * @return String resource to which request get forwarded.
  99. */
  100. @RequestMapping("/action")
  101. public String actionItems(@ModelAttribute RetrievalInfo retrievalInfo,
  102. @RequestParam(required = false) String requestType, Map<String, Object> model,
  103. HttpServletRequest req) {
  104. if (!isUserAdmin(req)) {
  105. model.put(VIEW_STATUS, getViewStatusForInvalidUser());
  106. return "admin/inbox-items";
  107. }
  108. /* Fetch the range parameters as sent in the request */
  109. long startIndex = retrievalInfo.getStartIndex();
  110. retrievalInfo.setNoOfRecords(WebConstants.PAGE_LIMIT);
  111. long noOfRecordsRequested = retrievalInfo.getNoOfRecords();
  112. List<AdminRequest> inboxItems = adminService.getAdminRequests(retrievalInfo, requestType);
  113. HashMap<String, Object> parameters = new HashMap<String, Object>();
  114. /* Map containing the previous and next index values */
  115. HashMap<String, Long> pagingMap = new HashMap<String, Long>();
  116. /*
  117. * If the size of the list is greater than the no. of records requested
  118. * ,set the parameter 'next' to be used as start index for the next
  119. * page retrieval.
  120. */
  121. if (inboxItems != null && inboxItems.size() > noOfRecordsRequested) {
  122. pagingMap.put(WebConstants.NEXT, startIndex + noOfRecordsRequested);
  123. } else {
  124. /*
  125. * If the list size is not greater than the number requested set
  126. * the 'next' parameter to minus one
  127. */
  128. pagingMap.put(WebConstants.NEXT, (long) WebConstants.MINUS_ONE);
  129. }
  130. /*
  131. * Set the parameter 'previous' to be used as the start index for the
  132. * previous page retrieval
  133. */
  134. pagingMap.put(WebConstants.PREVIOUS, startIndex - noOfRecordsRequested);
  135. /* Add the map containing the paging values to the map of parameters */
  136. parameters.put(WebConstants.PAGING, pagingMap);
  137. ViewStatus viewStatus = ViewStatus.createTheViewStatus(inboxItems,
  138. WebConstants.ADMIN_REQUESTS, parameters);
  139. model.put(VIEW_STATUS, viewStatus);
  140. return "admin/inbox-items";
  141. }
  142. /**
  143. * Handles request to Approve list of entities on user request
  144. *
  145. * @param key Primary key of entity(idea or comment) whose details are to be
  146. * shown
  147. * @return String resource to which request get forwarded.
  148. */
  149. @RequestMapping("/approveList/{key}")
  150. public String approveList(@PathVariable String key) {
  151. log.info("Into Admin Controller");
  152. // adminService.approveIdea(key);
  153. return "admin/show";
  154. }
  155. /**
  156. * Handles request to Approve user request
  157. *
  158. * @param key Primary key of entity(AdminRequest) which need to be approve.
  159. * @param user User Logged in user detail
  160. * @param retrievalInfo RetrievalInfo provide detail for fetching data
  161. * @param requestType String
  162. * @param adminReason String reason for request approval
  163. * @param model Map request
  164. * @param req HttpServletRequest
  165. * @return String resource to which request get forwarded.
  166. */
  167. @RequestMapping("/approve/{key}")
  168. public String approve(@PathVariable String key, HttpSession session,
  169. @ModelAttribute RetrievalInfo retrievalInfo,
  170. @RequestParam(required = false) String requestType,
  171. @RequestParam(required = true) String adminReason, Map<String, Object> model,
  172. HttpServletRequest req) {
  173. if (!isUserAdmin(req)) {
  174. model.put(VIEW_STATUS, getViewStatusForInvalidUser());
  175. return "admin/inbox-items";
  176. }
  177. log.info("Into Admin Controller to approve the request");
  178. User user = (User) session.getAttribute(WebConstants.USER);
  179. AdminRequest adminReq = new AdminRequest();
  180. adminReq.setKey(key);
  181. adminReq.setAdminReason(adminReason);
  182. adminService.approveAdminRequest(adminReq, user);
  183. List<AdminRequest> inboxItems = adminService.getAdminRequests(retrievalInfo, requestType);
  184. ViewStatus viewStatus = ViewStatus.createTheViewStatus(inboxItems,
  185. WebConstants.ADMIN_REQUESTS, null);
  186. model.put(VIEW_STATUS, viewStatus);
  187. return "admin/inbox-items";
  188. }
  189. /**
  190. * Handles request to deny user request
  191. *
  192. * @param key Primary key of entity(AdminRequest) which need to be deny.
  193. * @param user User detail of logged-in user.
  194. * @param retrievalInfo RetrievalInfo provide detail for fetching data
  195. * @param requestType String
  196. * @param adminReason String reason for denying the request
  197. * @param model request map
  198. * @param req HttpServletRequest
  199. * @return String resource to which request get forwarded.
  200. */
  201. @RequestMapping("/deny/{key}")
  202. public String deny(@PathVariable String key, HttpSession session,
  203. @ModelAttribute RetrievalInfo retrievalInfo,
  204. @RequestParam(required = false) String requestType,
  205. @RequestParam(required = true) String adminReason,
  206. Map<String, Object> model, HttpServletRequest req) {
  207. if (!isUserAdmin(req)) {
  208. model.put(VIEW_STATUS, getViewStatusForInvalidUser());
  209. return "admin/inbox-items";
  210. }
  211. log.info("Into Admin Controller to deny the request");
  212. User user = (User) session.getAttribute(WebConstants.USER);
  213. AdminRequest adminReq = new AdminRequest();
  214. adminReq.setKey(key);
  215. adminReq.setAdminReason(adminReason);
  216. adminService.denyAdminRequest(adminReq, user);
  217. List<AdminRequest> inboxItems = adminService.getAdminRequests(retrievalInfo, requestType);
  218. ViewStatus viewStatus = ViewStatus.createTheViewStatus(inboxItems,
  219. WebConstants.ADMIN_REQUESTS, null);
  220. model.put(VIEW_STATUS, viewStatus);
  221. return "admin/inbox-items";
  222. }
  223. /**
  224. * Handles request to delete idea/comment
  225. *
  226. * @param key Primary key of entity(idea or comment) which need to be
  227. * deleted.
  228. * @param user User
  229. * @param retrievalInfo RetrievalInfo provide detail for fetching data
  230. * @param requestType String
  231. * @param adminReason String reason for deleting idea or comment
  232. * @param model request map
  233. * @param req HttpServletRequest
  234. * @return String resource to which request get forwarded.
  235. */
  236. @RequestMapping("/delete/{key}")
  237. public String deleteIdea(@PathVariable String key, @ModelAttribute RetrievalInfo retrievalInfo,
  238. @RequestParam String adminReason,
  239. Map<String, Object> model, HttpSession session, HttpServletRequest req) {
  240. if (!isUserAdmin(req)) {
  241. model.put(VIEW_STATUS, getViewStatusForInvalidUser());
  242. return "admin/ideas";
  243. }
  244. log.info("Into Admin Controller");
  245. User user = (User) session.getAttribute(WebConstants.USER);
  246. adminService.deleteIdea(key, user, adminReason);
  247. listIdea(retrievalInfo, model);
  248. return "admin/ideas";
  249. }
  250. /**
  251. * Handles request to delete project
  252. *
  253. * @param key Primary key of entity(idea or comment) which need to be
  254. * deleted.
  255. * @param user User detail of logged in user
  256. * @param retrievalInfo RetrievalInfo provide detail for fetching data
  257. * @param requestType String
  258. * @param adminReason String reason for deleting project
  259. * @param model request map
  260. * @param req HttpServletRequest
  261. * @return String resource to which request get forwarded.
  262. */
  263. @RequestMapping("/deleteProject/{key}")
  264. public String deleteProject(@PathVariable String key,
  265. @ModelAttribute RetrievalInfo retrievalInfo,
  266. @RequestParam String adminReason,
  267. Map<String, Object> model, HttpSession session, HttpServletRequest req) {
  268. if (!isUserAdmin(req)) {
  269. model.put(VIEW_STATUS, getViewStatusForInvalidUser());
  270. return "admin/projects";
  271. }
  272. log.info("Into Admin Controller to delete the project.");
  273. User user = (User) session.getAttribute(WebConstants.USER);
  274. adminService.deleteProject(key, user, adminReason);
  275. listProjects(retrievalInfo, model, req);
  276. return "admin/projects";
  277. }
  278. /**
  279. *
  280. * @param key primary key of Entity(Idea)
  281. * @param model request map
  282. * @return String resource to which request get forwarded.
  283. */
  284. @RequestMapping("/viewIdea/{key}")
  285. public String getIdeaByIdeaCommentKey(@PathVariable String key, Map<String, Object> model) {
  286. Comment comment = ideaCommentService.getCommentById(key);
  287. Idea idea = adminService.getIdeaByCommentKey(key);
  288. ViewStatus viewStatus = new ViewStatus();
  289. IdeaDetail ideaDetail = null;
  290. try {
  291. ideaDetail = ideaBuilder.getIdeaDetail(idea.getKey(), true);
  292. if (null != comment && null != ideaDetail && null != ideaDetail.getIdea()) {
  293. viewStatus.addData(WebConstants.IDEA_DETAIL, ideaDetail);
  294. viewStatus.addData(WebConstants.IDEA_COMMENT, comment);
  295. viewStatus.setStatus(SUCCESS);
  296. } else {
  297. viewStatus.setStatus(ERROR);
  298. viewStatus.addMessage(ERROR, WebConstants.RECORD_NOT_FOUND);
  299. }
  300. } catch (IdeasExchangeException e) {
  301. viewStatus.setStatus(ERROR);
  302. viewStatus.addMessage(ERROR, e.getMessage());
  303. }
  304. model.put(VIEW_STATUS, viewStatus);
  305. return "admin/ideaComment";
  306. }
  307. /**
  308. * Handles request to delete idea request
  309. *
  310. * @param key Primary key of entity(idea or comment) whose details are to be
  311. * shown
  312. * @return String resource to which request get forwarded.
  313. */
  314. @RequestMapping("/viewProject/{key}")
  315. public String getProjectByProjectCommentKey(@PathVariable String key, Map<String, Object> model) {
  316. Comment comment = projectCommentService.getCommentById(key);
  317. Project proj = adminService.getProjectByCommentKey(key);
  318. ViewStatus viewStatus = new ViewStatus();
  319. ProjectDetail projectDetail = null;
  320. projectDetail = projectBuilder.getProjectDetail(proj.getKey());
  321. if (null != comment && null != projectDetail && null != projectDetail.getProject()) {
  322. viewStatus.addData(WebConstants.PROJECT_DETAIL, projectDetail);
  323. viewStatus.addData(WebConstants.PROJECT_COMMENT, comment);
  324. viewStatus.setStatus(SUCCESS);
  325. } else {
  326. viewStatus.setStatus(ERROR);
  327. viewStatus.addMessage(ERROR, WebConstants.RECORD_NOT_FOUND);
  328. }
  329. model.put(VIEW_STATUS, viewStatus);
  330. return "admin/projectComment";
  331. }
  332. /**
  333. * Handle request for listing idea.
  334. *
  335. * @param retrievalInfo RetrievalInfo provide detail for fetching data
  336. * @param model request map
  337. */
  338. public void listIdea(@ModelAttribute RetrievalInfo retrievalInfo, Map<String, Object> model) {
  339. /* Fetch the range parameters as sent in the request */
  340. long startIndex = retrievalInfo.getStartIndex();
  341. long noOfRecordsRequested = retrievalInfo.getNoOfRecords();
  342. /* Get the idea list */
  343. List<IdeaDetail> ideas = ideaBuilder.getIdeasForListing(retrievalInfo);
  344. /* Map of data to be inserted into the view status object */
  345. HashMap<String, Object> parameters = new HashMap<String, Object>();
  346. /* Map containing the previous and next index values */
  347. HashMap<String, Long> pagingMap = new HashMap<String, Long>();
  348. /*
  349. * If the size of the list is greater than the no. of records requested
  350. * ,set the parameter 'next' to be used as start index for the next
  351. * page retrieval.
  352. */
  353. if (ideas != null && ideas.size() > noOfRecordsRequested) {
  354. pagingMap.put(WebConstants.NEXT, startIndex + noOfRecordsRequested);
  355. } else {
  356. /*
  357. * If the list size is not greater than the number requested set
  358. * the 'next' parameter to minus one
  359. */
  360. pagingMap.put(WebConstants.NEXT, (long) WebConstants.MINUS_ONE);
  361. }
  362. /*
  363. * Set the parameter 'previous' to be used as the start index for the
  364. * previous page retrieval
  365. */
  366. pagingMap.put(WebConstants.PREVIOUS, startIndex - noOfRecordsRequested);
  367. /* Add the map containing the paging values to the map of parameters */
  368. parameters.put(WebConstants.PAGING, pagingMap);
  369. ViewStatus viewStatus = ViewStatus
  370. .createTheViewStatus(ideas, WebConstants.IDEAS, parameters);
  371. model.put(VIEW_STATUS, viewStatus);
  372. }
  373. /**
  374. * Handle request for getting list of ideas.
  375. *
  376. * @param RetrievalInfo provide detail for fetching data
  377. * @param model request map
  378. * @param req HttpServletRequest
  379. * @return String resource on which response get forwarded.
  380. */
  381. @RequestMapping(value = "/ideas")
  382. public String listIdeas(@ModelAttribute RetrievalInfo retrievalInfo, Map<String, Object> model,
  383. HttpServletRequest req) {
  384. if (!isUserAdmin(req)) {
  385. model.put(VIEW_STATUS, getViewStatusForInvalidUser());
  386. return "admin/ideas";
  387. }
  388. /* Fetch the range parameters as sent in the request */
  389. long startIndex = retrievalInfo.getStartIndex();
  390. retrievalInfo.setNoOfRecords(WebConstants.PAGE_LIMIT);// set the
  391. // number of records to 50 for
  392. // Admin view
  393. long noOfRecordsRequested = retrievalInfo.getNoOfRecords();
  394. /* Get the idea list */
  395. List<IdeaDetail> ideas = ideaBuilder.getIdeasForListing(retrievalInfo);
  396. /* Map of data to be inserted into the view status object */
  397. HashMap<String, Object> parameters = new HashMap<String, Object>();
  398. /* Map containing the previous and next index values */
  399. HashMap<String, Long> pagingMap = new HashMap<String, Long>();
  400. /*
  401. * If the size of the list is greater than the no. of records requested
  402. * ,set the parameter 'next' to be used as start index for the next
  403. * page retrieval.
  404. */
  405. if (ideas != null && ideas.size() > noOfRecordsRequested) {
  406. pagingMap.put(WebConstants.NEXT, startIndex + noOfRecordsRequested);
  407. } else {
  408. /*
  409. * If the list size is not greater than the number requested set
  410. * the 'next' parameter to minus one
  411. */
  412. pagingMap.put(WebConstants.NEXT, (long) WebConstants.MINUS_ONE);
  413. }
  414. /*
  415. * Set the parameter 'previous' to be used as the start index for the
  416. * previous page retrieval
  417. */
  418. pagingMap.put(WebConstants.PREVIOUS, startIndex - noOfRecordsRequested);
  419. /* Add the map containing the paging values to the map of parameters */
  420. parameters.put(WebConstants.PAGING, pagingMap);
  421. ViewStatus viewStatus = ViewStatus
  422. .createTheViewStatus(ideas, WebConstants.IDEAS, parameters);
  423. model.put(VIEW_STATUS, viewStatus);
  424. return "admin/ideas";
  425. }
  426. /**
  427. * Handle request for getting list of projects.
  428. *
  429. * @param RetrievalInfo provide detail for fetching data
  430. * @param model request map
  431. * @param req HttpServletRequest
  432. * @return String resource on which response get forwarded.
  433. */
  434. @RequestMapping(value = "/projects")
  435. public String listProjects(@ModelAttribute RetrievalInfo retrievalInfo,
  436. Map<String, Object> model, HttpServletRequest req) {
  437. if (!isUserAdmin(req)) {
  438. model.put(VIEW_STATUS, getViewStatusForInvalidUser());
  439. return "admin/projects";
  440. }
  441. /* Fetch the range parameters as sent in the request */
  442. long startIndex = retrievalInfo.getStartIndex();
  443. retrievalInfo.setNoOfRecords(WebConstants.PAGE_LIMIT);
  444. long noOfRecordsRequested = retrievalInfo.getNoOfRecords();
  445. List<Project> projects = projectService.listProjects(retrievalInfo);
  446. /* Map of data to be inserted into the view status object */
  447. HashMap<String, Object> parameters = new HashMap<String, Object>();
  448. /* Map containing the previous and next index values */
  449. HashMap<String, Long> pagingMap = new HashMap<String, Long>();
  450. /*
  451. * If the size of the list is greater than the no. of records requested
  452. * ,set the parameter 'next' to be used as start index for the next
  453. * page retrieval.
  454. */
  455. if (projects != null && projects.size() > noOfRecordsRequested) {
  456. pagingMap.put(WebConstants.NEXT, startIndex + noOfRecordsRequested);
  457. } else {
  458. /*
  459. * If the list size is not greater than the number requested set
  460. * the 'next' parameter to minus one
  461. */
  462. pagingMap.put(WebConstants.NEXT, (long) WebConstants.MINUS_ONE);
  463. }
  464. /*
  465. * Set the parameter 'previous' to be used as the start index for the
  466. * previous page retrieval
  467. */
  468. pagingMap.put(WebConstants.PREVIOUS, startIndex - noOfRecordsRequested);
  469. /* Add the map containing the paging values to the map of parameters */
  470. parameters.put(WebConstants.PAGING, pagingMap);
  471. // Create viewStatus
  472. ViewStatus viewStatus = ViewStatus
  473. .createTheViewStatus(projects, WebConstants.PROJECTS, parameters);
  474. model.put(VIEW_STATUS, viewStatus);
  475. return "admin/projects";
  476. }
  477. /**
  478. * Handle request for getting list of users.
  479. *
  480. * @param RetrievalInfo provide detail for fetching data
  481. * @param model request map
  482. * @param req HttpServletRequest
  483. * @return String resource on which response get forwarded.
  484. */
  485. @RequestMapping(value = "/users")
  486. public String listUsers(@ModelAttribute RetrievalInfo retrievalInfo,
  487. Map<String, Object> model, HttpServletRequest req) {
  488. if (!isUserAdmin(req)) {
  489. model.put(VIEW_STATUS, getViewStatusForInvalidUser());
  490. return "admin/projects";
  491. }
  492. /* Fetch the range parameters as sent in the request */
  493. long startIndex = retrievalInfo.getStartIndex();
  494. long noOfRecordsRequested = retrievalInfo.getNoOfRecords();
  495. List<User> users = userService.getUsers(retrievalInfo, null);
  496. /* Map of data to be inserted into the view status object */
  497. HashMap<String, Object> parameters = new HashMap<String, Object>();
  498. /* Map containing the previous and next index values */
  499. HashMap<String, Long> pagingMap = new HashMap<String, Long>();
  500. /*
  501. * If the size of the list is greater than the no. of records requested
  502. * ,set the parameter 'next' to be used as start index for the next
  503. * page retrieval.
  504. */
  505. if (users != null && users.size() > noOfRecordsRequested) {
  506. pagingMap.put(WebConstants.NEXT, startIndex + noOfRecordsRequested);
  507. } else {
  508. /*
  509. * If the list size is not greater than the number requested set
  510. * the 'next' parameter to minus one
  511. */
  512. pagingMap.put(WebConstants.NEXT, (long) WebConstants.MINUS_ONE);
  513. }
  514. /*
  515. * Set the parameter 'previous' to be used as the start index for the
  516. * previous page retrieval
  517. */
  518. pagingMap.put(WebConstants.PREVIOUS, startIndex - noOfRecordsRequested);
  519. /* Add the map containing the paging values to the map of parameters */
  520. parameters.put(WebConstants.PAGING, pagingMap);
  521. // Create viewStatus
  522. ViewStatus viewStatus = ViewStatus
  523. .createTheViewStatus(users, WebConstants.USERS, parameters);
  524. model.put(VIEW_STATUS, viewStatus);
  525. return "admin/users";
  526. }
  527. /**
  528. * Handles request to ban user request
  529. *
  530. * @param key Primary key of entity(User) which need to be banned.
  531. * @param user User detail of logged-in user.
  532. * @param adminReason String reason for banning user
  533. * @param model request map
  534. * @param req HttpServletRequest
  535. * @return String resource to which request get forwarded.
  536. */
  537. @RequestMapping(value = "/banUser/{key}")
  538. public String banUser(@PathVariable String key, HttpSession session,
  539. @RequestParam String adminReason,
  540. Map<String, Object> model, HttpServletRequest req) {
  541. if (!isUserAdmin(req)) {
  542. model.put(VIEW_STATUS, getViewStatusForInvalidUser());
  543. return "admin/users";
  544. }
  545. ViewStatus viewStatus = new ViewStatus();
  546. User userToBan = new User();
  547. userToBan.setUserKey(key);
  548. User user = (User) session.getAttribute(WebConstants.USER);
  549. userToBan = adminService.blacklistUser(userToBan, user.getUserKey(), adminReason);
  550. if (userToBan.getStatus() == User.STATUS_BANNED) {
  551. viewStatus.addData(WebConstants.USER, userToBan);
  552. viewStatus.setStatus(SUCCESS);
  553. }
  554. model.put(VIEW_STATUS, viewStatus);
  555. return "admin/users";
  556. }
  557. /**
  558. * Handles request to activate user request
  559. *
  560. * @param key Primary key of entity(User) which need to be activated.
  561. * @param user User detail of logged-in user.
  562. * @param adminReason String reason for banning user
  563. * @param model request map
  564. * @param req HttpServletRequest
  565. * @return String resource to which request get forwarded.
  566. */
  567. @RequestMapping(value = "/activateUser/{key}")
  568. public String activateUser(@PathVariable String key, HttpSession session,
  569. @RequestParam String adminReason,
  570. Map<String, Object> model, HttpServletRequest req) {
  571. if (!isUserAdmin(req)) {
  572. model.put(VIEW_STATUS, getViewStatusForInvalidUser());
  573. return "admin/users";
  574. }
  575. ViewStatus viewStatus = new ViewStatus();
  576. User user = (User) session.getAttribute(WebConstants.USER);
  577. User userToBan = new User();
  578. userToBan.setUserKey(key);
  579. userToBan = adminService.activateUser(userToBan, user.getUserKey(), adminReason);
  580. if (userToBan.getStatus() == User.STATUS_ACTIVE) {
  581. viewStatus.addData(WebConstants.USER, userToBan);
  582. viewStatus.setStatus(SUCCESS);
  583. }
  584. model.put(VIEW_STATUS, viewStatus);
  585. return "admin/users";
  586. }
  587. /**
  588. * @return the ideaBuilder
  589. */
  590. public IdeaBuilder getIdeaBuilder() {
  591. return ideaBuilder;
  592. }
  593. /**
  594. * @param ideaBuilder the ideaBuilder to set
  595. */
  596. public void setIdeaBuilder(IdeaBuilder ideaBuilder) {
  597. this.ideaBuilder = ideaBuilder;
  598. }
  599. /**
  600. * Check for user logging or not and also check for admin role
  601. *
  602. * @param req HttpServletRequest
  603. * @return boolean reture true if user is admin
  604. */
  605. private boolean isUserAdmin(HttpServletRequest req) {
  606. boolean isAdmin = false;
  607. if (req.getSession(true).getAttribute(WebConstants.USER) != null) {
  608. User user = (User) req.getSession(true).getAttribute(WebConstants.USER);
  609. if (User.ROLE_ADMIN.equalsIgnoreCase(user.getRoleName())) {
  610. isAdmin = true;
  611. }
  612. }
  613. return isAdmin;
  614. }
  615. }