/connect-web/src/main/java/org/osforce/connect/web/module/admin/AdminWidget.java

http://focus-sns.googlecode.com/ · Java · 155 lines · 129 code · 17 blank · 9 comment · 9 complexity · d1657ec57fbbf4e860c6de21b494e98b MD5 · raw file

  1. package org.osforce.connect.web.module.admin;
  2. import java.util.List;
  3. import javax.validation.Valid;
  4. import org.osforce.connect.entity.commons.Template;
  5. import org.osforce.connect.entity.system.Project;
  6. import org.osforce.connect.entity.system.ProjectCategory;
  7. import org.osforce.connect.entity.system.ProjectFeature;
  8. import org.osforce.connect.entity.system.Role;
  9. import org.osforce.connect.entity.system.Site;
  10. import org.osforce.connect.service.commons.TemplateService;
  11. import org.osforce.connect.service.system.ProjectCategoryService;
  12. import org.osforce.connect.service.system.ProjectFeatureService;
  13. import org.osforce.connect.service.system.ProjectService;
  14. import org.osforce.connect.service.system.RoleService;
  15. import org.osforce.connect.web.AttributeKeys;
  16. import org.osforce.connect.web.module.util.ModuleUtil;
  17. import org.osforce.spring4me.web.bind.annotation.PrefParam;
  18. import org.osforce.spring4me.web.bind.annotation.RequestAttr;
  19. import org.osforce.spring4me.web.stereotype.Widget;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.ui.Model;
  22. import org.springframework.validation.BindingResult;
  23. import org.springframework.web.bind.annotation.ModelAttribute;
  24. import org.springframework.web.bind.annotation.RequestMapping;
  25. import org.springframework.web.bind.annotation.RequestParam;
  26. import org.springframework.web.context.request.WebRequest;
  27. /**
  28. *
  29. * @author <a href="mailto:haozhonghu@hotmail.com">gavin</a>
  30. * @since 1.1.0
  31. * @create May 22, 2011 - 12:51:22 PM
  32. * <a href="http://www.opensourceforce.org">????</a>
  33. */
  34. @Widget
  35. @RequestMapping("/admin")
  36. public class AdminWidget {
  37. private RoleService roleService;
  38. private ProjectService projectService;
  39. private TemplateService templateService;
  40. private ProjectFeatureService featureService;
  41. private ProjectCategoryService categoryService;
  42. public AdminWidget() {
  43. }
  44. @Autowired
  45. public void setRoleService(RoleService roleService) {
  46. this.roleService = roleService;
  47. }
  48. @Autowired
  49. public void setProjectService(ProjectService projectService) {
  50. this.projectService = projectService;
  51. }
  52. @Autowired
  53. public void setTemplateService(TemplateService templateService) {
  54. this.templateService = templateService;
  55. }
  56. @Autowired
  57. public void setFeatureService(ProjectFeatureService featureService) {
  58. this.featureService = featureService;
  59. }
  60. @Autowired
  61. public void setCategoryService(ProjectCategoryService categoryService) {
  62. this.categoryService = categoryService;
  63. }
  64. @RequestMapping("/admin/info-view")
  65. public String doInfoView() {
  66. return "admin/admin-info";
  67. }
  68. @RequestMapping("/project/form-view")
  69. public String doProjectForm(@RequestAttr Site site,
  70. @ModelAttribute @Valid Project project, BindingResult result,
  71. Model model, Boolean showErrors, WebRequest request) {
  72. if(!showErrors) {
  73. if(project==null || project.getId()==null) {
  74. project = (Project) request.getAttribute(AttributeKeys.PROJECT_KEY, WebRequest.SCOPE_REQUEST);
  75. }
  76. List<ProjectCategory> categories = categoryService.getProjectCategoryList(site.getId());
  77. model.addAttribute(AttributeKeys.PROJECT_CATEGORY_LIST_KEY_READABLE, categories);
  78. model.addAttribute(AttributeKeys.PROJECT_KEY_READABLE, project);
  79. }
  80. return "admin/project-form";
  81. }
  82. @RequestMapping("/project/form-action")
  83. public String doProjectAction(@ModelAttribute @Valid Project project,
  84. BindingResult result, Model model) {
  85. if(result.hasErrors()) {
  86. model.addAttribute(AttributeKeys.SHOW_ERRORS_KEY_READABLE, true);
  87. model.addAttribute(AttributeKeys.FEATURE_CODE_KEY_READABLE,ProjectFeature.FEATURE_ADMIN);
  88. return "page:/admin/project-form";
  89. }
  90. //
  91. projectService.updateProject(project);
  92. return String.format("redirect:/%s/admin/project/form", project.getUniqueId());
  93. }
  94. @RequestMapping("/features/form-view")
  95. public String doFeaturesForm(@PrefParam String templateCode,
  96. @RequestAttr Project project, Model model) {
  97. Template template = templateService.getTemplate(project.getCategoryId(), templateCode);
  98. List<ProjectFeature> features = ModuleUtil.parseToModules(template.getContent());
  99. for(ProjectFeature feature : features) {
  100. ProjectFeature tmp = featureService.getProjectFeature(feature.getCode(), project.getId());
  101. if(tmp!=null) {
  102. feature.setId(tmp.getId());
  103. feature.setLabel(tmp.getLabel());
  104. feature.setShow(tmp.getShow());
  105. if(tmp.getLevel()!=null) {
  106. feature.setLevel(tmp.getLevel());
  107. }
  108. feature.setRoleId(tmp.getRoleId());
  109. feature.setProjectId(project.getId());
  110. } else {
  111. Role role = roleService.getRole(feature.getRoleCode(), project.getCategoryId());
  112. feature.setRole(role);
  113. feature.setProject(project);
  114. featureService.createProjectFeature(feature);
  115. }
  116. }
  117. model.addAttribute(AttributeKeys.PROJECT_FEATURE_LIST_KEY_READABLE, features);
  118. //
  119. List<Role> roles = roleService.getRoleList(project.getCategoryId());
  120. model.addAttribute(AttributeKeys.ROLE_LIST_KEY_READABLE, roles);
  121. return "admin/features-form";
  122. }
  123. @RequestMapping("/features/form-action")
  124. public String doFeaturesAction(@RequestParam Long[] ids,
  125. @RequestParam Long[] roleIds, @RequestParam Integer[] levels,
  126. @RequestParam Boolean[] shows, @RequestParam String[] labels, @RequestAttr Project project) {
  127. for(int i=0; i<ids.length; i++) {
  128. ProjectFeature feature = featureService.getProjectFeature(ids[i]);
  129. feature.setLabel(labels[i]);
  130. feature.setLevel(levels[i]);
  131. feature.setShow(shows[i]);
  132. feature.setRoleId(roleIds[i]);
  133. feature.setProjectId(project.getId());
  134. featureService.updateProjectFeature(feature);
  135. }
  136. return String.format("redirect:/%s/admin/features/form", project.getUniqueId());
  137. }
  138. }