/connect-web/src/main/java/org/osforce/connect/web/module/system/PermissionWidget.java

http://focus-sns.googlecode.com/ · Java · 103 lines · 81 code · 12 blank · 10 comment · 5 complexity · bd97eb84a59d9bc54bcdbf2aa57372f6 MD5 · raw file

  1. package org.osforce.connect.web.module.system;
  2. import java.util.List;
  3. import java.util.Map;
  4. import org.osforce.connect.entity.system.Permission;
  5. import org.osforce.connect.entity.system.ProjectCategory;
  6. import org.osforce.connect.entity.system.Resource;
  7. import org.osforce.connect.entity.system.Role;
  8. import org.osforce.connect.service.system.PermissionService;
  9. import org.osforce.connect.service.system.ProjectCategoryService;
  10. import org.osforce.connect.service.system.ResourceService;
  11. import org.osforce.connect.service.system.RoleService;
  12. import org.osforce.connect.web.AttributeKeys;
  13. import org.osforce.spring4me.commons.collection.CollectionUtil;
  14. import org.osforce.spring4me.web.stereotype.Widget;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.ui.Model;
  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 0.1.0
  24. * @create May 19, 2011 - 12:34:25 PM
  25. * <a href="http://www.opensourceforce.org">????</a>
  26. */
  27. @Widget
  28. @RequestMapping("/system/permission")
  29. public class PermissionWidget {
  30. private RoleService roleService;
  31. private ResourceService resourceService;
  32. private PermissionService permissionService;
  33. private ProjectCategoryService categoryService;
  34. public PermissionWidget() {
  35. }
  36. @Autowired
  37. public void setRoleService(RoleService roleService) {
  38. this.roleService = roleService;
  39. }
  40. @Autowired
  41. public void setResourceService(ResourceService resourceService) {
  42. this.resourceService = resourceService;
  43. }
  44. @Autowired
  45. public void setPermissionService(PermissionService permissionService) {
  46. this.permissionService = permissionService;
  47. }
  48. @Autowired
  49. public void setCategoryService(ProjectCategoryService categoryService) {
  50. this.categoryService = categoryService;
  51. }
  52. @RequestMapping("/list-view")
  53. public String doListView(@RequestParam Long siteId,
  54. @RequestParam(required=false) Long categoryId, Model model) {
  55. List<ProjectCategory> categories = categoryService.getProjectCategoryList(siteId, null);
  56. model.addAttribute(AttributeKeys.PROJECT_CATEGORY_LIST_KEY_READABLE, categories);
  57. if(categoryId==null && categories.size()>0) {
  58. categoryId = categories.get(0).getId();
  59. }
  60. model.addAttribute("categoryId", categoryId);
  61. //
  62. List<Permission> permissions = permissionService.getPermissionList(siteId, categoryId);
  63. Map<String, Permission> resourceMap = CollectionUtil.newHashMap();
  64. for(Permission p : permissions) {
  65. resourceMap.put(p.getResource().getCode(), p);
  66. }
  67. model.addAttribute("resourceMap", resourceMap);
  68. //
  69. List<Resource> resources = resourceService.getResourceList();
  70. model.addAttribute(AttributeKeys.RESOURCE_LIST_KEY_READABLE, resources);
  71. //
  72. List<Role> roles = roleService.getRoleList(categoryId);
  73. model.addAttribute(AttributeKeys.ROLE_LIST_KEY_READABLE, roles);
  74. return "system/permission-list";
  75. }
  76. @RequestMapping(value="/form-action", method=RequestMethod.POST)
  77. public String doFormAction(@RequestParam Long siteId, @RequestParam Long categoryId,
  78. @RequestParam Long[] resourceIds, @RequestParam Long[] roleIds) {
  79. for(int i=0; i<resourceIds.length; i++) {
  80. Long resourceId = resourceIds[i];
  81. Long roleId = roleIds[i];
  82. Permission permission = permissionService.getPermission(resourceId, categoryId);
  83. if(permission==null) {
  84. permission = new Permission(resourceId, categoryId);
  85. }
  86. permission.setRoleId(roleId);
  87. permissionService.updatePermission(permission);
  88. }
  89. return String.format("redirect:/system/permission/list?siteId=%s", siteId);
  90. }
  91. }