package org.fedorahosted.flies.core.action;

import org.fedorahosted.flies.core.model.Account;
import org.fedorahosted.flies.core.model.FliesLocale;
import org.fedorahosted.flies.core.model.Person;
import org.fedorahosted.flies.core.model.Tribe;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.core.Conversation;
import org.jboss.seam.core.Events;
import org.jboss.seam.framework.EntityHome;
import org.jboss.seam.security.Identity;
import org.jboss.seam.security.management.JpaIdentityStore;

@Name("tribeHome")
@Scope(ScopeType.CONVERSATION)
public class TribeHome extends EntityHome<Tribe>{

	private static final long serialVersionUID = 5139154491040234980L;

	@Override
	protected Tribe loadInstance() {
		Session session = (Session) getEntityManager().getDelegate();
		return (Tribe) session.createCriteria(getEntityClass())
		.add( Restrictions.naturalId()
		        .set("locale", getEntityManager().find(FliesLocale.class, getId()))
		    ).setCacheable(true)
		    .uniqueResult();
	}
	
	@Begin(join = true)
	public void validateSuppliedId(){
		getInstance(); // this will raise an EntityNotFound exception
					   // when id is invalid and conversation will not
		               // start
		Conversation c = Conversation.instance();
		c.setDescription(getInstance().getLocale().getId());
	}
	
	@In(required=false, value=JpaIdentityStore.AUTHENTICATED_USER) 
	Account authenticatedAccount;

	@Transactional
	public void joinTribe(){

		if(authenticatedAccount == null){
			getLog().error("failed to load auth person");
			return;
		}
		Person currentPerson = getEntityManager().find(Person.class, authenticatedAccount.getPerson().getId());
		
		getLog().info("attempting to join tribe {0}", getId());
		getInstance().getMembers().add(currentPerson);
		getEntityManager().flush();
		Events.instance().raiseEvent("personJoinedTribe", currentPerson, getInstance());
		getLog().info("{0} joined tribe {1}", authenticatedAccount.getUsername(), getId());
	}
	
	@Transactional
	public void leaveTribe(){

		if(authenticatedAccount == null){
			getLog().error("failed to load auth person");
			return;
		}
		Person currentPerson = getEntityManager().find(Person.class, authenticatedAccount.getPerson().getId());
		
		getLog().info("attempting to leave tribe {0}", getId());
		getInstance().getMembers().remove(currentPerson);
		getEntityManager().flush();
		Events.instance().raiseEvent("personLeftTribe", currentPerson, getInstance());
		getLog().info("{0} left tribe {1}", authenticatedAccount.getUsername(), getId());
	}
	
	public void cancel(){}
}

@@ -1942,66 +1942,68 @@
 %09%0A%09%09
-getLog().info(%22attempting to join tribe %7B0%7D%22, getId());
+if(!getInstance().getMembers().contains(currentPerson))%7B
 %0A%09%09
+%09
 getI
@@ -2039,32 +2039,33 @@
 rrentPerson);%0A%09%09
+%09
 getEntityManager
@@ -2068,32 +2068,33 @@
 ager().flush();%0A
+%09
 %09%09Events.instanc
@@ -2152,32 +2152,33 @@
 getInstance());%0A
+%09
 %09%09getLog().info(
@@ -2243,24 +2243,28 @@
 , getId());%0A
+%09%09%7D%0A
 %09%7D%0A%09%0A%09@Trans
@@ -2510,70 +2510,68 @@
 ));%0A
-%09%09
 %0A%09%09
-getLog().info(%22attempting to leave tribe %7B0%7D%22, getId());%0A
+if(getInstance().getMembers().contains(currentPerson))%7B%0A%09
 %09%09ge
@@ -2620,16 +2620,17 @@
 son);%0A%09%09
+%09
 getEntit
@@ -2649,16 +2649,17 @@
 lush();%0A
+%09
 %09%09Events
@@ -2733,16 +2733,17 @@
 e());%0A%09%09
+%09
 getLog()
@@ -2812,24 +2812,28 @@
 , getId());%0A
+%09%09%7D%0A
 %09%7D%0A%09%0A%09public

clean up join/leave tribe methods