/* 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.Article; import no.dusken.aranea.model.MediaResource; import no.dusken.aranea.model.Page; import no.dusken.aranea.model.PageRelation; import no.dusken.aranea.service.MediaResourceService; import no.dusken.aranea.service.PageService; import no.dusken.common.exception.PageNotFoundException; import org.springframework.beans.factory.annotation.Required; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.ServletRequestUtils; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.*; /** * TODO: JAVADOC * * @author Erlend Hamnaberg<erlenha@underdusken.no> * @author Marvin B. Lillehaug<lillehau@underdusken.no> */ public class PageController extends MultiActionController { private PageService pageService; private MediaResourceService mediaResourceService; private static final String ARTICLEVIEW = "no/dusken/aranea/base/web/article/view"; private static final String MEDIARESOURCEVIEW = "no/dusken/aranea/base/web/mediaresource/view"; private static final String PDFVIEW = "pdf"; private int MAX_PARAGRAPHS; private boolean showUnpublished; public ModelAndView handleArticle(HttpServletRequest request, HttpServletResponse response) throws Exception { Map<String, Object> map = new HashMap<String, Object>(); if (showUnpublished) { //assuming that when this is true, we do not want caching. response.setHeader("Cache-Control", "no-cache"); } else { response.setHeader("Cache-Control", "max-age=3600"); } Long ID; Page p; if (request.getParameter("externalID") != null) { ID = ServletRequestUtils.getRequiredLongParameter(request, "externalID"); p = pageService.getEntityByExternalID(ID); } else { ID = ServletRequestUtils.getLongParameter(request, "ID", 0); p = pageService.getEntity(ID); } Calendar now = new GregorianCalendar(); if (p == null || (p != null && !((p.getPublished() || showUnpublished) && p.getVisibleFrom().before(now) && now.before(p.getVisibleTo() )))) { throw new PageNotFoundException("Page with ID: " + ID); } if (p instanceof Article) { Article article = (Article) p; List<String> paragraphs = getParagraphs(article); map.put("paragraphs", paragraphs); map.put("photographers", pageService.getPhotographers(article)); map.put("drawers", pageService.getDrawers(article)); int pages = 1; if (paragraphs.size() > MAX_PARAGRAPHS) { pages = paragraphs.size() / MAX_PARAGRAPHS; if (paragraphs.size() % MAX_PARAGRAPHS >= (MAX_PARAGRAPHS/2)) { pages++; } } int articlePage = ServletRequestUtils.getIntParameter(request, "articlepage", 1); if (articlePage < 1 || articlePage > pages) { articlePage = 1; } int startIndex, endIndex; if (pages == 1) { startIndex = 1; endIndex = paragraphs.size(); } else { int paragraphs_per_page = paragraphs.size() / pages; startIndex = paragraphs_per_page * (articlePage - 1) + 1; endIndex = startIndex + paragraphs_per_page + 1; if (endIndex >= paragraphs.size()) { endIndex = paragraphs.size(); } } map.put("articleStartIndex",startIndex); map.put("articleEndIndex", endIndex); map.put("articlePage", articlePage); map.put("articlePageCount", pages); } map.put("article", p); HashMap<String, List<Page>> relationMap = new HashMap<String, List<Page>>(); HashMap<Integer, List<PageRelation>> paragraphRelationMap = new HashMap<Integer, List<PageRelation>>(); for (PageRelation pr : p.getPageRelations()) { Integer paragraph = pr.getParagraph(p); Page relatedPage = pr.getPageRelatedTo(p); if (paragraph == 0) { String description = pr.getDescription(p); if (relationMap.containsKey(description)) { relationMap.get(description).add(relatedPage); } else { List<Page> list = new LinkedList<Page>(); list.add(relatedPage); relationMap.put(description, list); } } else { if(paragraphRelationMap.containsKey(paragraph)){ paragraphRelationMap.get(paragraph).add(pr); }else{ List<PageRelation> list = new LinkedList<PageRelation>(); list.add(pr); paragraphRelationMap.put(paragraph, list); } } } map.put("paragraphPagerelations", paragraphRelationMap); map.put("pageRelations", relationMap); map.put("captchaString", UUID.randomUUID().toString()); return new ModelAndView(ARTICLEVIEW, map); } public ModelAndView printArticle(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mav = handleArticle(request, response); mav.getModel().put("printing", true); return mav; } public ModelAndView handlePDF(HttpServletRequest request, HttpServletResponse response) throws Exception { Map<String, Object> map = new HashMap<String, Object>(); int ID = ServletRequestUtils.getRequiredIntParameter(request, "ID"); Page p = pageService.getEntity((long) ID); if (p == null) throw new PageNotFoundException("Pdf with ID: " + ID); map.put("page", p); return new ModelAndView(PDFVIEW, map); } private List<String> getParagraphs(Article article) { // TODO: This is very, very ugly. Please do it another way... String text = article.getText(); return Arrays.asList(text.split("</p>")); } /** * This is to handle a single mediaresource */ public ModelAndView handleMediaResource(HttpServletRequest request, HttpServletResponse response) throws Exception { Map<String, Object> map = new HashMap<String, Object>(); long id = ServletRequestUtils.getRequiredIntParameter(request, "ID"); Boolean forceQT = ServletRequestUtils.getBooleanParameter(request, "forceQT", false); map.put("forceQT", forceQT); MediaResource mr = mediaResourceService.getEntity(id); map.put("mediaResource", mr); return new ModelAndView(MEDIARESOURCEVIEW, map); } public boolean isShowUnpublished() { return showUnpublished; } public void setShowUnpublished(boolean showUnpublished) { this.showUnpublished = showUnpublished; } @Value("#{ ${maxParagraphsPerPage} }") @Required public void setMAX_PARAGRAPHS(int MAX_PARAGRAPHS) { this.MAX_PARAGRAPHS = MAX_PARAGRAPHS; } @Required public void setPageService(PageService pageService) { this.pageService = pageService; } @Required public void setMediaResourceService( MediaResourceService mediaResourceService) { this.mediaResourceService = mediaResourceService; } }