package sino.controller.post;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import sino.entities.Post;
import sino.entities.PostType;
import sino.service.PostServiceException;
import sino.utility.ParamUtility;
@Controller
@RequestMapping(ViewPostController.BASE_PATH)
public class ViewPostController extends BasePostController {
public static final String BASE_PATH = "/";
private String viewAchievementsView = "post/view_achievements";
private String viewNewsView = "post/view_news";
private String postDetailView = "post/post_detail";
private int indexNewsSize = 5;
private int indexAchievementsSize = 5;
private int newsSize = 20;
private int achievementsSize = 9;
@ModelAttribute("previewNews")
public List < Post > getPreviewNews() throws PostServiceException {
return getPostService().getAllPostsSortByDate(PostType.NEWS, 0, this.indexNewsSize).getObjects();
}
@RequestMapping("/locale.do")
public String changeLocale(@RequestParam("language") String language, HttpSession session) {
Locale locale = new Locale(language);
session.setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, locale);
return "redirect:index.html";
}
@RequestMapping("/view.do")
public String view(@RequestParam("name") String viewName) {
ParamUtility.checkStringNotNullOrEmpty(null, viewName, "viewName");
return viewName.replaceAll(" ", "/");
}
@RequestMapping("/index.html")
public ModelAndView showIndex(HttpSession httpSession, HttpServletRequest request) throws PostServiceException {
ParamUtility.checkNotNull(null, httpSession, "httpSession");
if (request.getHeader("Accept-Language").toLowerCase().startsWith("ru")) {
return new ModelAndView("redirect:/ru");
}
ModelAndView mov = new ModelAndView("index");
mov.addObject("achievements", getPostService().getAllPostsSortByDate(PostType.ACHIEVEMENTS, 0,
this.indexAchievementsSize).getObjects());
return mov;
}
@RequestMapping(value = "/{type}/list.do")
public ModelAndView viewPosts(@PathVariable("type") String type,
@RequestParam(value = "page", required = false, defaultValue = "0") int pageNumber)
throws PostServiceException {
ParamUtility.checkNotNegative(null, pageNumber, "pageNumber");
PostType postType = PostType.fromName(type);
ModelAndView mov = new ModelAndView(postType == PostType.ACHIEVEMENTS ? this.viewAchievementsView
: this.viewNewsView);
mov.addObject("posts", getPostService().getAllPostsSortByDate(postType, pageNumber,
postType == PostType.ACHIEVEMENTS ? this.achievementsSize : this.newsSize));
mov.addObject("type", type);
return mov;
}
@RequestMapping(value = "/{type}/post.do")
public ModelAndView viewPost(@PathVariable("type") String type, @RequestParam(value = "id") long id)
throws PostServiceException {
ModelAndView mov = new ModelAndView(this.postDetailView);
mov.addObject("type", type);
mov.addObject("post", getPostService().getPost(id));
return mov;
}
@RequestMapping("/file.do")
public void getFile(@RequestParam("id") String id, OutputStream os) throws PostControllerException {
InputStream is = ModifyPostController.class.getResourceAsStream(getUploadDir() + "/" + id);
if (is == null) {
throw new PostControllerException("Does not find picture with the id '" + id + "'");
}
try {
byte[] buffer = new byte[4096];
int len = is.read(buffer);
while (len != -1) {
os.write(buffer, 0, len);
len = is.read(buffer);
}
} catch (IOException e) {
throw new PostControllerException("Cannot output picture with the id '" + id + "'. IO exception: "
+ e.getMessage());
} finally {
close(new Closeable[] {is, os});
}
}
public String getViewAchievementsView() {
return viewAchievementsView;
}
public void setViewAchievementsView(String viewAchievementsView) {
this.viewAchievementsView = viewAchievementsView;
}
public String getViewNewsView() {
return viewNewsView;
}
public void setViewNewsView(String viewNewsView) {
this.viewNewsView = viewNewsView;
}
public void setPostDetailView(String postDetailView) {
this.postDetailView = postDetailView;
}
public String getPostDetailView() {
return postDetailView;
}
}