/connect-web/src/main/java/org/osforce/connect/web/module/document/FolderWidget.java

http://focus-sns.googlecode.com/ · Java · 124 lines · 104 code · 12 blank · 8 comment · 11 complexity · 4ac5a43da7e06b3987c3dfcac1516323 MD5 · raw file

  1. package org.osforce.connect.web.module.document;
  2. import java.util.Collections;
  3. import java.util.List;
  4. import java.util.Map;
  5. import javax.validation.Valid;
  6. import org.osforce.connect.entity.document.Folder;
  7. import org.osforce.connect.entity.system.Project;
  8. import org.osforce.connect.entity.system.ProjectFeature;
  9. import org.osforce.connect.entity.system.User;
  10. import org.osforce.connect.service.document.FolderService;
  11. import org.osforce.connect.web.AttributeKeys;
  12. import org.osforce.connect.web.security.annotation.Permission;
  13. import org.osforce.spring4me.commons.collection.CollectionUtil;
  14. import org.osforce.spring4me.web.bind.annotation.RequestAttr;
  15. import org.osforce.spring4me.web.stereotype.Widget;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.ui.Model;
  18. import org.springframework.validation.BindingResult;
  19. import org.springframework.web.bind.annotation.ModelAttribute;
  20. import org.springframework.web.bind.annotation.RequestMapping;
  21. import org.springframework.web.bind.annotation.RequestMethod;
  22. import org.springframework.web.bind.annotation.RequestParam;
  23. import org.springframework.web.bind.annotation.ResponseBody;
  24. /**
  25. *
  26. * @author <a href="mailto:haozhonghu@hotmail.com">gavin</a>
  27. * @since 1.1.0
  28. * @create May 21, 2011 - 12:57:04 PM
  29. * <a href="http://www.opensourceforce.org">????</a>
  30. */
  31. @Widget
  32. @RequestMapping("/document/folder")
  33. public class FolderWidget {
  34. private FolderService folderService;
  35. public FolderWidget() {
  36. }
  37. @Autowired
  38. public void setFolderService(FolderService folderService) {
  39. this.folderService = folderService;
  40. }
  41. @RequestMapping("/tree-view")
  42. @Permission({"folder-view"})
  43. public String doTreeView() {
  44. return "document/folder-tree";
  45. }
  46. @RequestMapping(value="/tree-action", method=RequestMethod.POST)
  47. public @ResponseBody Object doTreeAction(
  48. @RequestParam(required=false) Long id, @RequestAttr Project project) {
  49. List<Folder> folders = Collections.emptyList();
  50. List<Map<String, Object>> nodeList = CollectionUtil.newArrayList();
  51. if(id==-1) {
  52. Map<String, Object> node = CollectionUtil.newHashMap();
  53. node.put("id", "0");
  54. node.put("data", "ROOT");
  55. node.put("haschild", true);
  56. nodeList.add(node);
  57. } else if(id==0) {
  58. folders = folderService.getFolderList(project.getId());
  59. } else {
  60. folders = folderService.getFolderList(project.getId(), id);
  61. }
  62. for(Folder folder : folders) {
  63. Map<String, Object> node = CollectionUtil.newHashMap();
  64. node.put("id", String.valueOf(folder.getId()));
  65. node.put("data", folder.getName());
  66. node.put("haschild", true);
  67. nodeList.add(node);
  68. }
  69. return nodeList;
  70. }
  71. @RequestMapping("/form-view")
  72. @Permission(value={"folder-add", "folder-edit"}, userRequired=true, projectRequired=true)
  73. public String doFormView(
  74. @RequestParam(required=false) Long folderId,
  75. @RequestParam(required=false) Long parentFolderId,
  76. @ModelAttribute @Valid Folder folder, BindingResult result,
  77. @RequestAttr Project project, @RequestAttr User user, Model model, Boolean showErrors) {
  78. if(!showErrors) {
  79. folder.setModifiedBy(user);
  80. folder.setEnteredBy(user);
  81. folder.setProject(project);
  82. if(folderId!=null) {
  83. folder = folderService.getFolder(folderId);
  84. }
  85. Folder parentFolder = null;
  86. if(parentFolderId!=null) {
  87. parentFolder = folderService.getFolder(parentFolderId);
  88. folder.setParent(parentFolder);
  89. }
  90. model.addAttribute(AttributeKeys.FOLDER_KEY_READABLE, folder);
  91. }
  92. return "document/folder-form";
  93. }
  94. @RequestMapping(value="/form-action", method=RequestMethod.POST)
  95. @Permission(value={"folder-add", "folder-edit"}, userRequired=true, projectRequired=true)
  96. public String doFormAction(@ModelAttribute @Valid Folder folder,
  97. BindingResult result, Model model, @RequestAttr Project project) {
  98. if(result.hasErrors()) {
  99. model.addAttribute(AttributeKeys.SHOW_ERRORS_KEY_READABLE, true);
  100. model.addAttribute(AttributeKeys.FEATURE_CODE_KEY_READABLE, ProjectFeature.FEATURE_DOCUMENT);
  101. return "page:/document/folder-form";
  102. }
  103. //
  104. if(folder.getId()==null) {
  105. folderService.createFolder(folder);
  106. } else {
  107. folderService.updateFolder(folder);
  108. }
  109. return String.format("redirect:/%s/document/file/list?folderId=%s",
  110. project.getUniqueId(), folder.getId());
  111. }
  112. }