no.dusken.aranea.web.control.IssueController.java Source code

Java tutorial

Introduction

Here is the source code for no.dusken.aranea.web.control.IssueController.java

Source

/*
 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.Issue;
import no.dusken.aranea.service.IssueService;
import no.dusken.common.exception.PageNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

public class IssueController extends AbstractController {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    private IssueService issueService;

    public IssueController() {
    }

    public ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
            throws PageNotFoundException {

        // this content does not change that often, allow two hours cache
        response.setHeader("Cache-Control", "max-age=72000");

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("baseImageDir", "/images");

        // LinkedHashMap maintains order...
        Map<Integer, Collection> issueMap = new LinkedHashMap<Integer, Collection>();
        int year = 0;
        List<Issue> issues;
        if (request.getParameter("year") != null) {
            try {
                year = ServletRequestUtils.getIntParameter(request, "year");
                issues = issueService.getIssuesPublished(year);
                if (issues == null || issues.size() == 0) {
                    //Throwing exception so this can be threated as a 404 error on a higher level
                    throw new PageNotFoundException("Issue in year " + year);
                }
                issueMap.put(year, issues);
            } catch (ServletRequestBindingException e) {
                log.info("Unable to get section name from request", e);
            }
        } else {
            // defaulting to all years
            year = (new GregorianCalendar()).get(GregorianCalendar.YEAR);
            issues = issueService.getIssuesPublished(year);
            issueMap.put(year, issues);
            // get all previous years
            do {
                year = year - 1;
                issues = issueService.getIssuesPublished(year);
                if (issues != null && issues.size() > 0) {
                    issueMap.put(year, issues);
                }
            } while (issues != null && issues.size() > 0);

        }
        map.put("issueMap", issueMap);
        map.put("year", year);
        return new ModelAndView("no/dusken/aranea/base/web/pdf/view", map);
    }

    @Required
    public void setIssueService(IssueService issueService) {
        this.issueService = issueService;
    }
}