import java.util.List;


import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import org.hibernate.Transaction;

import org.hibernate.Query;
import org.hibernate.Session;
import com.HibernateSessionFactory;
import com.Userinfo;
import com.opensymphony.xwork2.ActionSupport;


@Entity
public class RegisterAction extends ActionSupport {
	@ManyToOne
	private Userinfo user;

	public Userinfo getUser() {
		return user;
	}

	public void setUser(Userinfo user) {
		this.user = user;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return SUCCESS;
	}
	public String register() throws Exception{
		try{
			Session session=HibernateSessionFactory.getSession();
			Transaction tx=session.beginTransaction();
			session.save(user);
			tx.commit();
			return SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			HibernateSessionFactory.getSession().close();
			return ERROR;
		}
	}
	public String login() throws Exception{
		try{
			Session session=HibernateSessionFactory.getSession();
			Transaction tx=session.beginTransaction();
		//	Query query=session.createQuery("from Userinfo where username=:uname");
			Query query = session.createQuery("from Userinfo where username=:uname and password=:upassword");
			query.setParameter("uname", user.getUsername());
			query.setParameter("upassword", user.getPassword());
			List list=query.list();
			
			tx.commit();
			
			if(list.size()>0){
				return SUCCESS;
			}else
				return ERROR;
		}catch(Exception e){
			e.printStackTrace();
			HibernateSessionFactory.getSession().close();
			return ERROR;
		}
	}


}
