package haipham.webnntp.ejb.user;

import javax.rmi.*;
import javax.ejb.*;
import java.util.*;
import haipham.webnntp.ejb.*;
import haipham.webnntp.ejb.group.*;
import javax.naming.*;
import javax.rmi.*;

public class UserEJB extends UserModel implements EntityBean{

    /** Create an user who doesn't belong to any group */
    public String ejbCreate(String user, String pass)
        throws CreateException{

        if (user == null || pass == null || user.equals("")
                || pass.equals("") ){
            throw new CreateException("Invalid input");
        }

        groups = new Vector();

        try{
            UserGroup group = EJBUtils.getGroupHome().create(user, "descp");
            groups.add(group);
        }catch(Exception e){
            throw new CreateException(e.getMessage());
        }

        userName = user;
        password = pass;
        userType = User.USER;
        settings = new Setting();

        return null;
    }

    public void setEntityContext(EntityContext context){
        m_context = context;
    }

    public void ejbActivate(){
        userName = (String) m_context.getPrimaryKey();
    }

    public void ejbPassivate(){
        userName = null;
        password = null;
    }

    public void ejbRemove(){
        // remove the associated group
        try{
            UserGroupHome   home = EJBUtils.getGroupHome();
            UserGroup group = home.findByPrimaryKey(userName);
            group.remove();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public void ejbPostCreate(String user, String pass){}
    public void ejbLoad(){}
    public void ejbStore(){}
    public void unsetEntityContext(){};
}
