PageRenderTime 76ms CodeModel.GetById 43ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 0ms

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