daily pastebin goal
9%
SHARE
TWEET

Untitled

masx1996 Nov 24th, 2017 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import java.util.List;
  3.  
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.ui.Model;
  7. import org.springframework.web.bind.annotation.PathVariable;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10. import com.server.models.email.Email;
  11. import com.server.models.user.User;
  12. import com.server.repos.EmailDao;
  13. import com.server.repos.TeamDao;
  14. import com.server.repos.UserDao;
  15.  
  16. import com.server.models.team.Team;
  17.  
  18. /**
  19.  * AdminController class gives us possibility to manage  users and emails by web page.
  20.  * We can also print user database and email database.
  21.  * @author msekta
  22.  *
  23.  */
  24. @Controller  
  25. @RequestMapping(value="/admin")
  26. public class AdminController {
  27.     private UserDao userDao;
  28.     private EmailDao emailDao;
  29.     private TeamDao teamDao;
  30.    
  31.     @Autowired
  32.     public AdminController(UserDao userDao, EmailDao emailDao, TeamDao teamDao) {
  33.         this.userDao = userDao;
  34.         this.emailDao = emailDao;
  35.         this.teamDao = teamDao;  
  36.     }
  37. /**
  38.  * Method is responsible for creating new emails.
  39.  * @param model
  40.  * @return
  41.  */
  42.     @RequestMapping(value="/addemail")
  43.     public String addNewEmail (Model model) {
  44.         model.addAttribute("email",new Email());
  45.         return "admin/addEmail";
  46.     }
  47.    
  48.     /**
  49.      * Method is responsible for creating new users.
  50.      * @param model
  51.      * @return
  52.      */
  53.     @RequestMapping("/adduser")
  54.     public String addNewUser(Model model) {
  55.         model.addAttribute("user",new User());
  56.         return "admin/addUser";
  57.     }
  58.    
  59.     /**
  60.      * Method is responsible for updating information about user. If searched user does not exist user
  61.      * user is redirected to /userNotFound page.
  62.      * @param email
  63.      * @param firstName
  64.      * @param lastName
  65.      * @param password
  66.      * @return
  67.      */
  68.     //TODO when first and last name are not changed, we can not save.
  69.     @RequestMapping(value="/updateuser/{id}")
  70.     public String updateUser(@PathVariable("id")String id,Model model){
  71.         if(userDao.findById(id)==null) {
  72.             return "error404/userNotFound";
  73.         }
  74.         User user=userDao.findById(id);
  75.         model.addAttribute("user",user);
  76.         return"admin/addUser";
  77.     }
  78.     /**
  79.      * Method is responsible for looking for user by @param email and delete user from table "user". If searched user does not exist user
  80.      * user is redirected to /userNotFound page.
  81.      * @param email
  82.      * @return
  83.      */
  84.     @RequestMapping(value="/deleteuser/{id}")
  85.     public String deleteUser(@PathVariable String id) {
  86.         if(userDao.findById(id)==null) {
  87.             return "error404/userNotFound";
  88.         }
  89.         User user=userDao.findById(id);
  90.         teamDao.findByTeamName(user.getTeamName()).deleteMember();
  91.         userDao.delete(user);
  92.         return "redirect:/admin/users";  
  93.     }
  94.     /**
  95.      * Method is responsible for adding new team to database.
  96.      * @param teamName
  97.      * @return
  98.      */
  99.     @RequestMapping(value="/addteam")
  100.     public String addTeam(Model model) {
  101.         model.addAttribute("team",new Team());
  102.         return "admin/addTeam";
  103.     }
  104.     /**
  105.      * Method is responsible for deleting team.If searched team does not exist user
  106.      * user is redirected to /teamNotFound page. If any user has deleted team as a team his/her
  107.      * teamName field is changed in "NO TEAM"
  108.      * @param teamName
  109.      * @return
  110.      */
  111.     @RequestMapping(value="/deleteteam/{teamName}")
  112.     public String deleteTeam(@PathVariable("teamName")String teamName) {
  113.         if(teamDao.findByTeamName(teamName)==null) {
  114.             return "error404/teamNotFound";
  115.         }
  116.         List<User> users=userDao.findByTeamName(teamName);
  117.         for(User user:users) {
  118.             user.setTeamName("NO TEAM");
  119.         }
  120.         Team team=teamDao.findByTeamName(teamName);
  121.         teamDao.delete(team);
  122.         return "redirect:/admin/teams";
  123.        
  124.     }
  125.     @RequestMapping(value="/email/{id}")
  126.     public String showDetails(@PathVariable("id") String id,Model model) {
  127.         if(emailDao.findById(id)==null) {
  128.             return "error404/emailNotFound";
  129.         }
  130.         model.addAttribute("email",emailDao.findById(id));
  131.         return "admin/emailDetails";
  132.     }
  133.    
  134.     /**
  135.      *Method is responsible for saving emails in database.
  136.      * @param email
  137.      * @return
  138.      */
  139.     @RequestMapping( value = "/saveemail", method = RequestMethod.POST )
  140.     public String saveEmail(Email email) {
  141.         email.setSentDate();
  142.         emailDao.save(email);
  143.         return "redirect:/admin/emails";
  144.     }
  145.     /**
  146.      * Method is responsible for saving user in database. If team does not exist, new team is created.
  147.      * If new user has the same username as other user then we have to refill form.
  148.      * @param user
  149.      * @return
  150.      */
  151.     @RequestMapping( value = "/saveuser", method = RequestMethod.POST )
  152.     public String saveUser(User user) {
  153.         user.createUsername(user.getFirstName(), user.getLastName());
  154.         if(userDao.findByUsername(user.getUsername()) != null) {
  155.             return "redirect:/admin/adduser";  
  156.         }
  157.         if(teamDao.findByTeamName(user.getTeamName())==null) {
  158.             teamDao.save(new Team(user.getTeamName()));
  159.         }
  160.         teamDao.findByTeamName(user.getTeamName()).addNewMember();
  161.         userDao.save(user);
  162.         return "redirect:/admin/users";
  163.     }
  164.     /**
  165.      * Method is responsible for saving new team.
  166.      * If new team has the same name as other team we have to refill form.
  167.      * @param team
  168.      * @return
  169.      */
  170.     @RequestMapping(value="/saveteam",method=RequestMethod.POST)
  171.     public String saveTeam(Team team) {
  172.         if(teamDao.findByTeamName(team.getTeamName()) != null) {
  173.             return "redirect:/admin/addteam";
  174.         }
  175.         team.createEmailAddress(team.getTeamName());
  176.         team.setId(0);
  177.         teamDao.save(team);
  178.         return "redirect:/admin/teams";
  179.     }
  180.     /**
  181.      * Method is responsible for looking for user in database by @param email and print all information about this user.
  182.      * @param email
  183.      * @return
  184.      */
  185.     @RequestMapping(value="/showuseremails/{id}",method=RequestMethod.GET)
  186.     public String showUser(@PathVariable String id,Model model) {
  187.         if(userDao.findById(id)==null) {
  188.             return "error404/userNotFound";
  189.         }
  190.         User user=userDao.findById(id);
  191.         model.addAttribute("emails", emailDao.findByRecipientOrderByRecipientDesc(user.getUsername()));
  192.         return "admin/showUserEmails";
  193.     }
  194.     /**
  195.      * Method is responsible for returning list of all users.
  196.      * @return
  197.      */
  198.     @RequestMapping(value="/users",method=RequestMethod.GET)
  199.     public String allUsers(Model model) {
  200.         model.addAttribute("users",userDao.findAll());
  201.         return "admin/users";
  202.     }
  203.     /**
  204.      * Method is responsible for returning list of all emails.
  205.      * @return
  206.      */
  207.     @RequestMapping(value = "/emails", method= RequestMethod.GET)
  208.     public String allEmails(Model model) {
  209.         model.addAttribute("emails",emailDao.findAllByOrderByIdDesc());
  210.         return "admin/emails";
  211.     }
  212.  
  213.     /**
  214.      * Method is responsible for showing all users of the team.
  215.      * @param teamName
  216.      * @return
  217.      */
  218.     @RequestMapping(value="/team/{teamName}",method=RequestMethod.GET)
  219.     public String team(@PathVariable("teamName") String teamName,Model model) {
  220.         if(teamDao.findByTeamName(teamName)==null) {
  221.             return "error404/teamNotFound";
  222.         }
  223.         model.addAttribute("teamMembers",userDao.findByTeamName(teamName));
  224.         return "admin/teamMembers";  
  225.     }
  226.     /**
  227.      * Method is responsible for showing all teams.
  228.      * @return
  229.      */
  230.     @RequestMapping(value="/teams",method=RequestMethod.GET)
  231.     public String showTeams(Model model) {
  232.         model.addAttribute("teams",teamDao.findAll());
  233.         return "admin/teams";
  234.     }
  235.    
  236.    
  237. }
RAW Paste Data
Top