/*
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.Banner;
import no.dusken.aranea.model.BannerLocation;
import no.dusken.aranea.service.BannerLocationService;
import no.dusken.aranea.service.BannerService;
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.multiaction.MultiActionController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
/**
* Handles viewing of banners, and redirecting when banners are clicked
*
* @author Jan Ove Øyen <janoveo@underdusken.no> Date: 31/10/2006 Time: 06:43:18
*/
public class BannerController extends MultiActionController {
private Logger log = LoggerFactory.getLogger(this.getClass());
private BannerService bannerService;
private BannerLocationService bannerLocationService;
/**
* Handles load requests for ads
*
* @param request The HttpServletRequest
* @param response The HttpServletResponse
* @return ModelAndView with a map containing the retrieved ads
*/
public ModelAndView handleBannerLoad(HttpServletRequest request,
HttpServletResponse response) {
Map<String, Object> map = new HashMap<String, Object>();
// banners does change often. Allow three hours caching
response.setHeader("Cache-Control", "max-age=10800");
// get the banner locations
List<BannerLocation> bls = bannerLocationService.getBannerLocations();
Set<Banner> ads = new HashSet<Banner>();
for (BannerLocation bl : bls) {
ads.add(bl.getBanner());
}
//the fishes are watching
if (!ads.isEmpty()) {
map.put("ads", ads);
} else {
log.info("No ads to put in map");
}
return new ModelAndView("no/dusken/aranea/base/web/ad/view", map);
}
/**
* Handles redirect requests for ads
*
* @param request The HttpServletRequest
* @param response The HttpServletResponse - used to redirect the browser to the
* Ad's url
* @return ModelAndView with a map containing the requested ad - useful if
* redirecting fails
* @throws org.springframework.web.bind.ServletRequestBindingException
* if missing banner id
* @throws no.dusken.common.exception.PageNotFoundException
* if no banner is found with the given id
*/
public ModelAndView handleBannerRedirect(HttpServletRequest request,
HttpServletResponse response)
throws ServletRequestBindingException, PageNotFoundException {
long ID = ServletRequestUtils.getRequiredLongParameter(request, "ID");
Banner banner = bannerService.getEntity(ID);
if (banner == null) {
// the banner was not found
throw new PageNotFoundException("Banner with id: " + ID);
}
// update number of clicks
banner.setClicks(banner.getClicks() + 1L);
banner = bannerService.saveOrUpdate(banner);
// redirect browser
return new ModelAndView("redirect:" + banner.getUrl());
}
@Required
public void setBannerLocationService(BannerLocationService bannerLocationService) {
this.bannerLocationService = bannerLocationService;
}
@Required
public void setBannerService(BannerService bannerService) {
this.bannerService = bannerService;
}
}