package no.dusken.aranea.admin.control;
import no.dusken.aranea.model.Page;
import no.dusken.aranea.model.Section;
import no.dusken.aranea.model.SectionPage;
import no.dusken.aranea.service.PageService;
import no.dusken.aranea.service.SectionPageService;
import no.dusken.aranea.service.SectionService;
import no.dusken.common.exception.PageNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
/**
* @author Marvin B. Lillehaug <lillehau@underdusken.no>
* Maintain the ordering of pages and their association to a section.
*/
@Controller
public class SectionPageController {
private SectionPageService sectionPageService;
private SectionService sectionService;
private String prioritizeView = "no/dusken/aranea/base/admin/sectionpage/prioritize";
private String addView = "no/dusken/aranea/base/admin/sectionpage/add";
private PageService pageService;
/**
* When cleaning a section, all sectionPages with ordering above cleanLimit should
* be removed.
*/
private Integer cleanLimit;
@RequestMapping(value="/page/prioritize.do", method= RequestMethod.GET)
public String handleSectionPages(@RequestParam(value = "sectionID", defaultValue = "0") Long sectionID, Model model) throws PageNotFoundException {
Section section = null;
if(sectionID == 0){
section = sectionService.getSectionByUrl("frontpage", null);
}else{
section = sectionService.getEntity(sectionID);
}
if(section == null) {
throw new PageNotFoundException("Section with id " + sectionID);
}
List<Section> topLevelSections = sectionService.getTopLevelSections(false);
if(!section.getHidden() && topLevelSections.size() > 0){
model.addAttribute("toplevelSections", topLevelSections);
}
model.addAttribute("section", section);
model.addAttribute("sectionpages", sectionPageService.getSectionPageByOwner(section));
return prioritizeView;
}
@RequestMapping(value="/page/prioritizeName.do", method= RequestMethod.GET)
public String handleSectionPages(@RequestParam String sectionName, Model model) throws PageNotFoundException {
Section section = sectionService.getSectionByUrl(sectionName, null);
if(section == null) {
throw new PageNotFoundException("Section with name " + sectionName);
}
return handleSectionPages(section.getID(), model);
}
@RequestMapping(value="/page/prioritizeUp.do", method= RequestMethod.POST)
public String prioritizeUp(@RequestParam Long sectionPageID) throws PageNotFoundException {
SectionPage sp1 = sectionPageService.getEntity(sectionPageID);
if(sp1 == null) {
throw new PageNotFoundException("SectionPage with id " + sectionPageID);
}else if(sp1.getOrdering() == 1){
return "redirect:/admin/page/prioritize.do?sectionID=" + sp1.getSection().getID();
}
SectionPage sp2 = sectionPageService.getSectionPageBySectionAndOrdering(sp1.getSection(), sp1.getOrdering() - 1);
if(sp2 != null){
sp2.setOrdering(sp1.getOrdering());
}
sp1.setOrdering(sp1.getOrdering() - 1);
List<SectionPage> sps = new LinkedList<SectionPage>();
sps.add(sp1);
sps.add(sp2);
sectionPageService.saveOrUpdateSectionPages(sps);
return "redirect:/admin/page/prioritize.do?sectionID=" + sp1.getSection().getID();
}
@RequestMapping(value="/page/prioritizeDown.do", method= RequestMethod.POST)
public String prioritizeDown(@RequestParam Long sectionPageID) throws PageNotFoundException {
SectionPage sp1 = sectionPageService.getEntity(sectionPageID);
if(sp1 == null) {
throw new PageNotFoundException("SectionPage with id " + sectionPageID);
}
SectionPage sp2 = sectionPageService.getSectionPageBySectionAndOrdering(sp1.getSection(), sp1.getOrdering() + 1);
if(sp2 != null){
sp2.setOrdering(sp1.getOrdering());
sp1.setOrdering(sp1.getOrdering() + 1);
List<SectionPage> sps = new LinkedList<SectionPage>();
sps.add(sp1);
sps.add(sp2);
sectionPageService.saveOrUpdateSectionPages(sps);
}
return "redirect:/admin/page/prioritize.do?sectionID=" + sp1.getSection().getID();
}
@RequestMapping(value="/page/prioritize.do", method= RequestMethod.POST)
public String setPriority(@RequestParam Long sectionPageID, @RequestParam Integer priority){
SectionPage sp = sectionPageService.getEntity(sectionPageID);
List<SectionPage> sps = sectionPageService.getSectionPageByOwner(sp.getSection());
if(priority >= 1 && priority <= sps.size()) {
sp.setOrdering(priority);
sps.remove(sp);
sps.add(priority - 1, sp);
sectionPageService.cascadeAndSaveOrUpdateSectionPages(sps);
}
return "redirect:/admin/page/prioritize.do?sectionID=" + sp.getSection().getID();
}
@RequestMapping(value="/sectionPage/add.do", method= RequestMethod.GET)
public String addSectionPage(@RequestParam Long pageID, Model model){
Page p = pageService.getEntity(pageID);
model.addAttribute("page", p);
model.addAttribute("sections", getLegalSections(p));
model.addAttribute("existingSectionPages", sectionPageService.getSectionPagesByPage(p));
return addView;
}
Set<Section> getLegalSections(Page page){
Set<Section> sections = new HashSet<Section>();
Section frontpage = sectionService.getSectionByUrl("frontpage", null);
if(sectionPageService.getSectionPageBySectionAndPage(frontpage, page) == null){
sections.add(frontpage);
}
Section s = page.getParent();
do{
if(sectionPageService.getSectionPageBySectionAndPage(s, page) == null){
sections.add(s);
}
s = s.getParent();
} while(s != null);
return sections;
}
@RequestMapping(value="/sectionPage/add.do", method= RequestMethod.POST)
public String handleAddSectionPage(@RequestParam Long pageID, @RequestParam Long sectionID){
Page p = pageService.getEntity(pageID);
Section s = sectionService.getEntity(sectionID);
if(getLegalSections(p).contains(s)){
SectionPage sp = new SectionPage(s, p, sectionPageService.getSectionPageByOwner(s).size() + 1, "");
sectionPageService.saveOrUpdate(sp);
}
return "redirect:/admin/page/prioritize.do?sectionID=" + s.getID();
}
@RequestMapping(value="/sectionPage/delete.do", method= RequestMethod.POST)
public String deleteSectionPage(@RequestParam Long sectionPageID){
SectionPage sp = sectionPageService.getEntity(sectionPageID);
if(sp != null){
sectionPageService.remove(sp);
List<SectionPage> sps = sectionPageService.getSectionPageByOwner(sp.getSection());
sectionPageService.cascadeAndSaveOrUpdateSectionPages(sps);
return "redirect:/admin/page/prioritize.do?sectionID=" + sp.getSection().getID();
}
return "redirect:/admin/page/prioritize.do";
}
@RequestMapping(value="/sectionPage/syncronize.do", method= RequestMethod.POST)
public String synchronizeSection(@RequestParam Long sectionPageID){
SectionPage sp = sectionPageService.getEntity(sectionPageID);
Section section = sp.getPage().getParent();
List<SectionPage> sps = sectionPageService.getSectionPageByOwner(section);
SectionPage sectionSp = sectionPageService.getSectionPageBySectionAndPage(section, sp.getPage());
if(sectionSp == null){
sectionSp = new SectionPage(section, sp.getPage(), sp.getOrdering(), "");
}else{
sectionSp.setOrdering(sp.getOrdering());
sps.remove(sectionSp);
}
sps.add(sp.getOrdering() - 1, sectionSp);
sectionPageService.cascadeAndSaveOrUpdateSectionPages(sps);
return "redirect:/admin/page/prioritize.do?sectionID=" + section.getID();
}
@RequestMapping(value="/sectionPage/clean.do", method= RequestMethod.POST)
public String cleanSection(@RequestParam Long sectionID){
Section s = sectionService.getEntity(sectionID);
List<SectionPage> pages = s.getPages();
int numberPages = pages.size();
int adjustedCleanLimit = getAdjustedCleanLimit(pages);
if(numberPages >= adjustedCleanLimit){
for(int i = adjustedCleanLimit; i < numberPages; i++){
sectionPageService.remove(pages.get(i));
}
}
return "redirect:/admin/page/prioritize.do?sectionID=" + s.getID();
}
private int getAdjustedCleanLimit(List<SectionPage> pages) {
int nonPublishedPages = 0;
for (int i = 0; i < cleanLimit; i++) {
SectionPage page = pages.get(i);
Boolean notPublished = !page.getPage().getPublished();
if(notPublished){
nonPublishedPages++;
}
}
return cleanLimit + nonPublishedPages;
}
@Autowired
public void setSectionPageService(SectionPageService sectionPageService) {
this.sectionPageService = sectionPageService;
}
@Autowired
public void setSectionService(SectionService sectionService) {
this.sectionService = sectionService;
}
@Autowired
public void setPageService(PageService pageService) {
this.pageService = pageService;
}
@Value("#{ ${articlesPerPage} } ")
public void setCleanLimit(Integer cleanLimit) {
this.cleanLimit = cleanLimit;
}
}