/* Copyright 2006 - 2010 Under Dusken Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package no.dusken.aranea.web.control; import no.dusken.aranea.model.Page; import no.dusken.aranea.model.Section; import no.dusken.aranea.service.PageService; 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.Required; 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.RequestParam; import java.util.List; /** * @author anderlin * @author janoveo Date: 3/06/2006 Time: 11:10:24 * @author Marvin B. Lillehaug <lillehau@underdusken.no> */ @Controller public class SectionController { private SectionService sectionService; private static final String FEEDVIEW = "feed"; private static final String VIEW = "no/dusken/aranea/base/web/section/view"; private PageService pageService; private Integer articlesPerPage = 20; @RequestMapping("/subsection.do") public String getPagesBySectionAndSubSection(@RequestParam(defaultValue = "") String subsection, @RequestParam(defaultValue = "") String section, Model model) throws PageNotFoundException { Section parent = sectionService.getSectionByUrl(section, null); Section s = sectionService.getSectionByUrl(subsection, parent); if(parent == null || s == null || !parent.isActive() || !s.isActive()){ throw new PageNotFoundException("Subsection: " + subsection + " Section: " + section ); } model.addAttribute("section", s); model.addAttribute("pages", pageService.getSectionPagePublished(articlesPerPage, s)); return VIEW; } @RequestMapping("/section.do") public String getPagesBySection(@RequestParam(defaultValue = "") String section, Model model) throws PageNotFoundException { Section s = sectionService.getSectionByUrl(section, null); if( s == null || !s.isActive()){ throw new PageNotFoundException("Section: " + section ); } model.addAttribute("section", s); model.addAttribute("pages", pageService.getSectionPagePublished(articlesPerPage, s)); return VIEW; } @RequestMapping("/feed.do") public String handleSectionFeed(@RequestParam(defaultValue = "") String section, @RequestParam(defaultValue = "", required = false) String subsection, Model model) throws PageNotFoundException { model.addAttribute("feedType", "rss_2.0"); List<Page> pages; Section s; if(subsection != null && subsection.length() != 0){ Section parent = sectionService.getSectionByUrl(section, null); if(parent == null || !parent.isActive()){ throw new PageNotFoundException("Section: " + section); } s = sectionService.getSectionByUrl(subsection, parent); }else{ s = sectionService.getSectionByUrl(section, null); } if(s == null || !s.isActive()){ throw new PageNotFoundException("Section: " + section); } model.addAttribute("section", s); pages = pageService.getSectionPagePublished(articlesPerPage, s); if (pages != null && pages.size() > 0) { model.addAttribute("pages", pages); } return FEEDVIEW; } @Required @Autowired public void setSectionService(SectionService sectionService) { this.sectionService = sectionService; } @Required @Autowired public void setPageService(PageService pageService) { this.pageService = pageService; } @Value("#{ ${articlesPerPage} }") public void setArticlesPerPage(Integer articlesPerPage) { this.articlesPerPage = articlesPerPage; } }