package at.fhj.swd14.pse.message;

import java.io.Serializable;

import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import at.fhj.swd14.pse.person.PersonService;
import at.fhj.swd14.pse.user.UserDto;
import at.fhj.swd14.pse.user.UserService;

@Named
@RequestScoped
public class MessageBean implements Serializable {

	private static final long serialVersionUID = -163448660457877436L;

	private static final Logger LOGGER = LogManager.getLogger(MessageBean.class);

    @EJB(name = "ejb/MessageService")
    private transient MessageService messageService;
    
    @EJB(name = "ejb/PersonService")
    private transient PersonService personService;

    @EJB(name = "ejb/UserService")
    private transient UserService userService;
    
    /**
     * The message to be transmitted
     */
    private MessageDto message;
    
    /**
     * The user that posts the message
     */
    private UserDto currentUser;
    
    /**
     * The recipient of the message
     */
    private UserDto recipient;
	
    public MessageBean() {
    	LOGGER.debug("Create: " + MessageBean.class.getSimpleName());
    	this.message = new MessageDto();
    	this.recipient = new UserDto();
    }
    
    public MessageDto getMessage() {
    	return this.message;
    }
    
    /**
     * Saves a message via MessageService
     */
    public void createMessage() {
    	LOGGER.info("Creating message");
    	if(currentUser == null) {
            long userID =  ((at.fhj.swd14.pse.security.DatabasePrincipal) FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal()).getUserId();
            currentUser = userService.find(userID); 
    	}
    	
    	if(currentUser != null) {
    		if(message.getContent() != null && message.getTitle() != null) {
    			message.setAuthor(currentUser);
    			
    	    	final long generatedId = messageService.save(message);
    	    	LOGGER.info("Created new message with ID {}", generatedId);
    	    	this.message = new MessageDto();
    	    	this.recipient = new UserDto();
    		}
    	
    	} else {
    		LOGGER.warn("Not able to access user Id for message creation!");
    	}
    	
    }
}

@@ -1090,93 +1090,8 @@
    %0A
-    /**%0A     * The recipient of the message%0A     */%0A    private UserDto recipient;%0A%09%0A
     
@@ -1222,45 +1222,8 @@
 ();%0A
-    %09this.recipient = new UserDto();%0A
     
@@ -2023,50 +2023,8 @@
 ();%0A
-    %09    %09this.recipient = new UserDto();%0A
     

remove unnecessary field