de.inetsource.jsfforum.beans.PostBean.java Source code

Java tutorial

Introduction

Here is the source code for de.inetsource.jsfforum.beans.PostBean.java

Source

/*
 * Copyright (C) 2014 Jrg Wiesmann
 *
 * 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.lazy.LazyPostDataModel;
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.db.UserFacade;
import de.inetsource.jsfforum.entity.Post;
import de.inetsource.jsfforum.entity.Thread;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.model.LazyDataModel;
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 Jrg Wiesmann
 */
@Component
@Scope("view")
public class PostBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private Post newPost;
    private int threadId = -1;
    private de.inetsource.jsfforum.entity.Thread thread;
    @Autowired
    private UserBean userBean;
    @Autowired
    private PostFacade postFacade;
    @Autowired
    private UserFacade userFacade;
    @Autowired
    private ThreadFacade threadFacade;
    @Autowired
    private ForumFacade forumFacade;
    private LazyDataModel<Post> lazyModel;

    @PostConstruct
    public void init() {
        Map<String, String> parameter = FacesContext.getCurrentInstance().getExternalContext()
                .getRequestParameterMap();
        if (StringUtils.isNotEmpty(parameter.get("threadId"))) {
            threadId = Integer.valueOf(parameter.get("threadId"));
            thread = threadFacade.find(threadId);
            lazyModel = new LazyPostDataModel(thread, postFacade,
                    StringUtils.isNotEmpty(parameter.get("lastPost")));
        }
        newPost = new Post();
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void save() {
        if (newPost.getText() != null) {
            newPost.setCreated(new Date());
            if (userBean.isLoggedIn()) {
                newPost.setUsersUsername(userFacade.find(userBean.getUser().getUsername()));
            }
            newPost.setThreadId(thread);
            postFacade.create(newPost);
            thread.getForumId().setPostcount(thread.getForumId().getPostcount() + 1);
            forumFacade.edit(thread.getForumId());
            newPost = new Post();
        }
    }

    public Post getNewPost() {
        return newPost;
    }

    public void setNewPost(Post newPost) {
        this.newPost = newPost;
    }

    public UserBean getUserBean() {
        return userBean;
    }

    public void setUserBean(UserBean userBean) {
        this.userBean = userBean;
    }

    public void setThreadId(int threadId) {
        this.threadId = threadId;
    }

    public Thread getThread() {
        return thread;
    }

    public void setThread(Thread thread) {
        this.thread = thread;
    }

    public LazyDataModel<Post> getLazyModel() {
        return lazyModel;
    }

    public void setLazyModel(LazyDataModel<Post> lazyModel) {
        this.lazyModel = lazyModel;
    }

}