Java tutorial
/* * Copyright (C) 2014 Joerg Wiesmann joerg.wiesmann@gmail.com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package de.inetsource.jsfforum.beans; import de.inetsource.jsfforum.beans.user.UserBean; import de.inetsource.jsfforum.db.ForumFacade; import de.inetsource.jsfforum.db.PostFacade; import de.inetsource.jsfforum.db.ThreadFacade; import de.inetsource.jsfforum.entity.Forum; import de.inetsource.jsfforum.entity.Thread; import java.io.Serializable; import java.util.List; import java.util.Map; import javax.annotation.PostConstruct; import javax.faces.application.FacesMessage; import javax.faces.context.FacesContext; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; /** * @author Joerg Wiesmann joerg.wiesmann@gmail.com */ @Component @Scope("view") public class ThreadBean implements Serializable { private static final long serialVersionUID = 1L; @Autowired protected ThreadFacade threadFacade; @Autowired protected PostFacade postFacade; @Autowired protected ForumFacade forumFacade; @Autowired UserBean userBean; @Autowired PostBean postBean; private int forumId = -1; private Forum forum; private Thread thread; private List<Thread> threads; @PostConstruct public void init() { Map<String, String> parameter = FacesContext.getCurrentInstance().getExternalContext() .getRequestParameterMap(); if (StringUtils.isNotEmpty(parameter.get("forumId"))) { forumId = Integer.valueOf(parameter.get("forumId")); threads = threadFacade.findArticlesFromTopic(forumId); forum = forumFacade.find(forumId); for (Thread t : threads) { t.setLastPost(threadFacade.getLastPost(t)); t.setFirstPost(threadFacade.getFirstPost(t)); } } thread = new Thread(); } public String createNewThread() { return "createthread.xhtml?faces-redirect=true&forumId=" + forumId; } @Transactional(propagation = Propagation.REQUIRED) public String save() { try { thread.setForumId(forum); if (userBean.isLoggedIn()) { thread.setUsersUsername(userBean.getUser()); } threadFacade.create(thread); postBean.setThread(thread); postBean.save(); return "/post/posts.xhtml?faces-redirect=true&threadId=" + thread.getId(); } catch (Exception e) { FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_WARN, "Creating a thread failed", e.toString()); FacesContext.getCurrentInstance().addMessage(null, message); e.printStackTrace(); } return null; } public Thread getThread() { return thread; } public void setThread(Thread thread) { this.thread = thread; } public List<Thread> getThreads() { return threads; } public void setThreads(List<Thread> threads) { this.threads = threads; } public UserBean getUserBean() { return userBean; } public void setUserBean(UserBean userBean) { this.userBean = userBean; } public PostBean getPostBean() { return postBean; } public void setPostBean(PostBean postBean) { this.postBean = postBean; } public Forum getForum() { return forum; } public void setForum(Forum forum) { this.forum = forum; } }