/*
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.Person;
import no.dusken.aranea.model.Role;
import no.dusken.aranea.service.ArticleService;
import no.dusken.aranea.service.ImageService;
import no.dusken.aranea.service.PersonService;
import no.dusken.aranea.service.RoleService;
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 PersonController extends AbstractController{
private Logger log = LoggerFactory.getLogger(this.getClass());
private PersonService personService;
private RoleService roleService;
private ArticleService articleService;
private ImageService imageService;
private Integer IMAGES_PER_PAGE;
public ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws PageNotFoundException {
// this content does not change that often, allow one hour cache
response.setHeader("Cache-Control", "max-age=3600");
Map<String, Object> map = new HashMap<String, Object>();
if (request.getParameter("username") != null) {
try {
Person person;
String username = ServletRequestUtils.getStringParameter(
request, "username");
person = personService.getPersonByUsername(username);
if (person == null){
//response.sendError(HttpServletResponse.SC_NOT_FOUND,
// "No such person: " + username);
//Throwing exception so this can be threated as a 404 error on a higher level
throw new PageNotFoundException("Person with username: " + username);
}
map.put("person", person);
map.put("articles", articleService.getPublishedArticlesByPerson(person));
Integer numberOfImages = imageService
.getImageNumberByPerson(person);
// get offset
int offset = ServletRequestUtils.getIntParameter(request,
"offset", 0);
if (offset > 0) {
map.put("previous_offset", Math.max(0, offset
- IMAGES_PER_PAGE));
}
if (offset < numberOfImages - IMAGES_PER_PAGE) {
map.put("next_offset", Math.min(numberOfImages, offset
+ IMAGES_PER_PAGE));
}
map.put("images", imageService.getImagesByPerson(person,
IMAGES_PER_PAGE, offset));
} catch (ServletRequestBindingException e) {
log.info("Unable to get section name from request", e);
} //catch (IOException e) {
// log.warn("Unable to send error NOT_FOUND", e);
// }
return new ModelAndView("no/dusken/aranea/base/web/persons/singleview", map);
} else {
List<Role> rolesSet = roleService.getRoles();
List<Role> roles = new ArrayList<Role>();
roles.addAll(rolesSet);
Collections.sort(roles);
Map<Role, List<Person>> roleMap = new LinkedHashMap<Role, List<Person>>();
for (Role r : roles) {
// only add active persons
List<Person> persons = personService.getPersons(r, true);
if (persons != null && persons.size() > 0) {
roleMap.put(r, persons);
}
}
map.put("roleMap", roleMap);
return new ModelAndView("no/dusken/aranea/base/web/persons/multiview", map);
}
}
@Required
public void setPersonService(PersonService personService) {
this.personService = personService;
}
@Required
public void setRoleService(RoleService roleService) {
this.roleService = roleService;
}
@Required
public void setArticleService(ArticleService articleService) {
this.articleService = articleService;
}
@Required
public void setImageService(ImageService imageService) {
this.imageService = imageService;
}
@Required
public void setIMAGES_PER_PAGE(Integer IMAGES_PER_PAGE) {
this.IMAGES_PER_PAGE = IMAGES_PER_PAGE;
}
}