/connect-web/src/main/java/org/osforce/connect/web/module/gallery/AlbumWidget.java

http://focus-sns.googlecode.com/ · Java · 88 lines · 70 code · 10 blank · 8 comment · 5 complexity · 501fe982fa81163f176a13e7d7a2690b MD5 · raw file

  1. package org.osforce.connect.web.module.gallery;
  2. import javax.validation.Valid;
  3. import org.osforce.connect.entity.gallery.Album;
  4. import org.osforce.connect.entity.system.Project;
  5. import org.osforce.connect.entity.system.ProjectFeature;
  6. import org.osforce.connect.entity.system.User;
  7. import org.osforce.connect.service.gallery.AlbumService;
  8. import org.osforce.connect.web.AttributeKeys;
  9. import org.osforce.connect.web.security.annotation.Permission;
  10. import org.osforce.spring4me.dao.Page;
  11. import org.osforce.spring4me.web.bind.annotation.RequestAttr;
  12. import org.osforce.spring4me.web.stereotype.Widget;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.ui.Model;
  15. import org.springframework.validation.BindingResult;
  16. import org.springframework.web.bind.annotation.ModelAttribute;
  17. import org.springframework.web.bind.annotation.RequestMapping;
  18. import org.springframework.web.bind.annotation.RequestMethod;
  19. import org.springframework.web.bind.annotation.RequestParam;
  20. /**
  21. *
  22. * @author <a href="mailto:haozhonghu@hotmail.com">gavin</a>
  23. * @since 1.1.0
  24. * @create May 21, 2011 - 11:16:16 AM
  25. * <a href="http://www.opensourceforce.org">????</a>
  26. */
  27. @Widget
  28. @RequestMapping("/gallery/album")
  29. public class AlbumWidget {
  30. private AlbumService albumService;
  31. public AlbumWidget() {
  32. }
  33. @Autowired
  34. public void setAlbumService(AlbumService albumService) {
  35. this.albumService = albumService;
  36. }
  37. @RequestMapping("/list-view")
  38. @Permission({"album-view"})
  39. public String doListView(Page<Album> page,
  40. @RequestAttr Project project, Model model) {
  41. page = albumService.getAlbumPage(page, project.getId());
  42. model.addAttribute(AttributeKeys.PAGE_KEY_READABLE, page);
  43. return "gallery/album-list";
  44. }
  45. @RequestMapping("/form-view")
  46. @Permission(value={"album-add", "album-edit"}, userRequired=true, projectRequired=true)
  47. public String doFormView(@RequestParam(required=false) Long albumId,
  48. @ModelAttribute @Valid Album album, BindingResult result,
  49. @RequestAttr Project project, @RequestAttr User user, Model model, Boolean showErrors) {
  50. if(!showErrors) {
  51. album.setEnteredBy(user);
  52. album.setModifiedBy(user);
  53. album.setProject(project);
  54. if(albumId!=null) {
  55. album = albumService.getAlbum(albumId);
  56. }
  57. }
  58. model.addAttribute(AttributeKeys.ALBUM_KEY_READABLE, album);
  59. return "gallery/album-form";
  60. }
  61. @RequestMapping(value="/form-action", method=RequestMethod.POST)
  62. @Permission(value={"album-add", "album-edit"}, userRequired=true, projectRequired=true)
  63. public String doFormAction(@ModelAttribute @Valid Album album,
  64. BindingResult result, Model model, @RequestAttr Project project) {
  65. if(result.hasErrors()) {
  66. model.addAttribute(AttributeKeys.SHOW_ERRORS_KEY_READABLE, true);
  67. model.addAttribute(AttributeKeys.FEATURE_CODE_KEY_READABLE, ProjectFeature.FEATURE_GALLERY);
  68. return "page:/gallery/album-form";
  69. }
  70. //
  71. if(album.getId()==null) {
  72. albumService.createAlbum(album);
  73. } else {
  74. albumService.updateAlbum(album);
  75. }
  76. return String.format("redirect:/%s/gallery/album/list", project.getUniqueId());
  77. }
  78. }