package com.aperture_software.glados_wiki.webmvc.controllers; import com.aperture_software.glados_wiki.entities.Group; import com.aperture_software.glados_wiki.services.GroupService; import com.aperture_software.glados_wiki.support.Pagination; import com.aperture_software.glados_wiki.webmvc.controllers.group_admin.GroupListPaginationToLinkFunction; import com.aperture_software.glados_wiki.webmvc.controllers.support.ListParam; import com.google.common.base.Optional; import com.google.common.base.Strings; import org.apache.shiro.authz.annotation.RequiresRoles; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; import java.util.List; /** * Created by jhyun on 14. 1. 4. */ @RequestMapping(value = "/admin/group") @Controller public class GroupAdminController { private static Logger LOG = LoggerFactory.getLogger(GroupAdminController.class); @Autowired private GroupService groupService; @ResponseBody @RequiresRoles(GroupService.ADMIN_ROLE) @RequestMapping(value = "list2", produces = {MediaType.APPLICATION_JSON_VALUE}) public List<Group> list2(@ModelAttribute("listParam") ListParam listParam) { List<Group> l = list(listParam); return l; } @RequiresRoles(GroupService.ADMIN_ROLE) @RequestMapping(value = "list") public ModelAndView list(ModelMap m, @ModelAttribute("listParam") ListParam listParam) { // m.put("groups", list(listParam)); Pagination p = new Pagination(groupService.count(listParam.getSearchTermAsOptional()), listParam.getOffset(), listParam.getLimit()); m.put("groupsPagination", p); // GroupListPaginationToLinkFunction pf = new GroupListPaginationToLinkFunction(); pf.setPagination(p); pf.setSearchTerm(listParam.getSearchTermAsOptional()); m.put("groupsPaginationToLinkFunction", pf); // return new ModelAndView("admin/group/list", m); } private List<Group> list(final ListParam listParam) { List<Group> l = groupService.list(listParam.getSearchTermAsOptional(), listParam.getOffset(), listParam.getLimit()); return l; } @RequiresRoles(GroupService.ADMIN_ROLE) @ResponseBody @RequestMapping(value = "delete/{groupName}", produces = {MediaType.TEXT_PLAIN_VALUE}) public String delete(@PathVariable("groupName") final String groupName) { Optional<Group> g = groupService.getByName(groupName); if (g.isPresent()) { groupService.delete(g.get()); } return "OK. DELETED."; } @RequiresRoles(GroupService.ADMIN_ROLE) @RequestMapping(value = "form") public ModelAndView form(ModelMap m, @RequestParam(value = "groupName", required = false, defaultValue = "") final String groupName) { // if (false == Strings.isNullOrEmpty(groupName)) { Optional<Group> g = groupService.getByName(groupName); if (g.isPresent()) { m.put("group", g.get()); } } // return new ModelAndView("admin/group/form", m); } @RequiresRoles(GroupService.ADMIN_ROLE) @ResponseBody @RequestMapping(value = "toggleEnabled/{groupName}", produces = {MediaType.TEXT_PLAIN_VALUE}) public String toggleEnabled(@PathVariable(value = "groupName") final String groupName, @RequestParam(value = "enabled", required = true) final boolean enabled) throws Exception { Optional<Group> g = groupService.getByName(groupName); if (g.isPresent()) { g.get().setEnabled(enabled); groupService.update(g.get()); return "OK. TOGGLED."; } else { throw new Exception(String.format("GROUP NOT FOUND [%s]", groupName)); } } @RequiresRoles(GroupService.ADMIN_ROLE) @ResponseBody @RequestMapping(value = "insert", produces = {MediaType.TEXT_PLAIN_VALUE}, method = {RequestMethod.POST}) public String insert(@ModelAttribute("group") Group group) { Group g = groupService.create(group.getName(), group.getDescription()); return "OK. INSERTED."; } @RequiresRoles(GroupService.ADMIN_ROLE) @ResponseBody @RequestMapping(value = "update", produces = {MediaType.TEXT_PLAIN_VALUE}, method = {RequestMethod.POST}) public String update(@ModelAttribute("group") Group group) throws Exception { Optional<Group> g = groupService.getByName(group.getName()); if (g.isPresent()) { g.get().setDescription(group.getDescription()); groupService.update(g.get()); return "OK. UPDATED."; } else { throw new Exception(String.format("GROUP NOT FOUND [%s]", group.getName())); } } }