package org.fedorahosted.flies.action;

import org.fedorahosted.flies.dao.AccountDAO;
import org.fedorahosted.flies.model.HAccount;
import org.fedorahosted.flies.model.HPerson;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.framework.EntityHome;
import org.jboss.seam.international.StatusMessages;
import org.jboss.seam.log.Log;
import org.jboss.seam.security.management.IdentityManager;
import org.jboss.seam.security.management.JpaIdentityStore;

@Name("personHome")
@Scope(ScopeType.CONVERSATION)
public class PersonHome extends EntityHome<HPerson>{
	
	@In(required=false, value=JpaIdentityStore.AUTHENTICATED_USER) 
	HAccount authenticatedAccount;
	
	@In
	AccountDAO accountDAO;
	
	@Logger
	Log log;
	
	private String name;
	private String email;
	
	@Override
	public Object getId() {
		Object id = super.getId();
		if(id == null && authenticatedAccount != null && authenticatedAccount.getPerson() != null){
			return authenticatedAccount.getPerson().getId();
		}
		return id;
	}
	
	public void regenerateApiKey(){
		accountDAO.createApiKey(getInstance().getAccount());
		getEntityManager().merge(getInstance().getAccount());
		log.info("Reset API key for {0}", getInstance().getAccount().getUsername());
	}
	
	@Override
	public String update(){
		if (getName() == null || "".equals(getName())
				|| getEmail() == null || "".equals(getEmail())) {
			StatusMessages.instance().addToControl("emptyField", "Please fill out all required fields.");
            return "failure";
		} else {
			getInstance().setName(getName());
			getInstance().setEmail(getEmail());
			FacesMessages.instance().add("Profile is updated.");
		}
		return "success";
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getEmail() {
		return email;
	}
}

@@ -391,212 +391,58 @@
 am.f
-aces.FacesMessages;%0Aimport org.jboss.seam.framework.EntityHome;%0Aimport org.jboss.seam.international.StatusMessages;%0Aimport org.jboss.seam.log.Log;%0Aimport org.jboss.seam.security.management.IdentityManager
+ramework.EntityHome;%0Aimport org.jboss.seam.log.Log
 ;%0Aim
@@ -761,55 +761,8 @@
 ;%0A%09%0A
-%09private String name;%0A%09private String email;%0A%09%0A
 %09@Ov
@@ -1220,661 +1220,6 @@
 %0A%09%7D%0A
-%09%0A%09@Override%0A%09public String update()%7B%0A%09%09if (getName() == null %7C%7C %22%22.equals(getName())%0A%09%09%09%09%7C%7C getEmail() == null %7C%7C %22%22.equals(getEmail())) %7B%0A%09%09%09StatusMessages.instance().addToControl(%22emptyField%22, %22Please fill out all required fields.%22);%0A            return %22failure%22;%0A%09%09%7D else %7B%0A%09%09%09getInstance().setName(getName());%0A%09%09%09getInstance().setEmail(getEmail());%0A%09%09%09FacesMessages.instance().add(%22Profile is updated.%22);%0A%09%09%7D%0A%09%09return %22success%22;%0A%09%7D%0A%0A%09public void setName(String name) %7B%0A%09%09this.name = name;%0A%09%7D%0A%0A%09public String getName() %7B%0A%09%09return name;%0A%09%7D%0A%0A%09public void setEmail(String email) %7B%0A%09%09this.email = email;%0A%09%7D%0A%0A%09public String getEmail() %7B%0A%09%09return email;%0A%09%7D%0A
 %7D%0A

Revert PersonHome when editprofile is now using #{personHome.instance.*}