package com.aperture_software.glados_wiki.webmvc.controllers;
import com.aperture_software.glados_wiki.entities.Page;
import com.aperture_software.glados_wiki.services.GroupService;
import com.aperture_software.glados_wiki.services.PageAclService;
import com.aperture_software.glados_wiki.services.PageService;
import com.aperture_software.glados_wiki.webmvc.controllers.helpers.RedirectHelper;
import com.google.common.base.Optional;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.View;
import javax.inject.Inject;
/**
* Created by jhyun on 14. 3. 12.
*/
@RequestMapping(value = "/versions")
@Controller
public class PageVersionController {
@Inject
private PageService pageService;
@Inject
private PageAclService pageAclService;
@Inject
private RedirectHelper redirectHelper;
@RequiresRoles(GroupService.ADMIN_ROLE)
@RequestMapping(value = "/truncate/{title:.+}", method = {RequestMethod.GET})
public View truncateVersions(@PathVariable("title") final String title) throws Exception {
Optional<Page> p = pageService.getPageByTitle(title);
if (p.isPresent()) {
pageAclService.checkWritable(p.get());
pageService.truncatePageVersions(p.get());
return redirectHelper.redirectToPageView(title);
} else {
throw new Exception(String.format(String.format("PAGE NOT FOUND [%s]", title)));
}
}
}