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

http://focus-sns.googlecode.com/ · Java · 79 lines · 55 code · 12 blank · 12 comment · 2 complexity · 83a3e8755be50cc04e291e7c2a995a1f MD5 · raw file

  1. package org.osforce.connect.web.module.system;
  2. import java.util.List;
  3. import javax.validation.Valid;
  4. import org.osforce.connect.entity.system.Resource;
  5. import org.osforce.connect.service.system.ResourceService;
  6. import org.osforce.connect.web.AttributeKeys;
  7. import org.osforce.spring4me.web.stereotype.Widget;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.ui.Model;
  10. import org.springframework.util.Assert;
  11. import org.springframework.validation.BindingResult;
  12. import org.springframework.web.bind.annotation.ModelAttribute;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RequestMethod;
  15. import org.springframework.web.bind.annotation.RequestParam;
  16. /**
  17. *
  18. * @author <a href="mailto:haozhonghu@hotmail.com">gavin</a>
  19. * @since 0.1.0
  20. * @create May 19, 2011 - 10:38:16 AM
  21. * <a href="http://www.opensourceforce.org">????</a>
  22. */
  23. @Widget
  24. @RequestMapping("/system/resource")
  25. public class ResourceWidget {
  26. private ResourceService resourceService;
  27. public ResourceWidget() {
  28. }
  29. @Autowired
  30. public void setResourceService(ResourceService resourceService) {
  31. this.resourceService = resourceService;
  32. }
  33. @RequestMapping(value="/list-view")
  34. public String doListView(Model model) {
  35. List<Resource> resources = resourceService.getResourceList();
  36. model.addAttribute(AttributeKeys.RESOURCE_LIST_KEY_READABLE, resources);
  37. return "system/resource-list";
  38. }
  39. @RequestMapping("/form-view")
  40. public String doFormView(@RequestParam(required=false) Long resourceId,
  41. @ModelAttribute @Valid Resource resource, BindingResult result, Model model) {
  42. if(resourceId!=null) {
  43. resource = resourceService.getResource(resourceId);
  44. }
  45. model.addAttribute(AttributeKeys.RESOURCE_KEY_READABLE, resource);
  46. return "system/resource-form";
  47. }
  48. @RequestMapping(value="form-action", method=RequestMethod.POST)
  49. public String doFormAction(@Valid Resource resource, BindingResult result) {
  50. if(result.hasErrors()) {
  51. return "page:/system/resource-form";
  52. }
  53. //
  54. Assert.notNull(resource.getId(), "Resource is not allowed to add, please use synchronize instead!");
  55. resourceService.updateResource(resource);
  56. return "redirect:/system/resource/list";
  57. }
  58. /**
  59. * Synchronize resources from resources.xml which is under /WEB-INF/
  60. * @return
  61. */
  62. @RequestMapping(value="/sync-action", method=RequestMethod.GET)
  63. public String doSyncAction() {
  64. resourceService.syncResources();
  65. return "redirect:/system/resource/list";
  66. }
  67. }