/**
 * Copyright (c) 2004 Red Hat, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Component of: Red Hat Application Server
 * 
 * Initial Developers: Aizaz Ahmed
 *                     Vivek Lakshmanan
 *                     Andrew Overholt
 *                     Matthew Wringe
 *
 */

/*
 * UserBean.java
 * 
 * Created on Aug 16, 2004
 *
 */
package olstore.entity;

import javax.ejb.EntityBean;

import olstore.entity.AddressLocal;

import java.util.Collection;

/**
 * @ejb.bean name="User"
 *  description="This bean is used to store information about the user."
 *	type="CMP"
 *  schema="olstore_user"
 *  primkey-field="username"
 *  reentrant="false"
 *  cmp-version="2.x"
 *  view-type="local"
 *  local-jndi-name="UserLocal_L"
 * 
 * @ejb.relation
 *  name="Friend-User"
 *  role-name"User-can-be-a-friend"
 *  target-ejb="Friend"
 * 
 *  
 * @ejb.transaction
 *  type="Required"
 * 
 *--
 * This is needed for JOnAS.
 * If you are not using JOnAS you can safely remove the tags below.
 * @jonas.bean ejb-name="User"
 * jndi-name="UserLocal"
 * @jonas.jdbc-mapping  jndi-name="jdbc_1" jdbc-table-name="olstore_user" 
 * --
 * 
 *  @ejb.persistence 
 * 
 * @ejb.finder 
 *    query="SELECT OBJECT(a) FROM olstore_user as a"  
 *    signature="java.util.Collection findAll()"  
 * 
 * @ejb.finder 
 *    query="SELECT OBJECT(a) FROM olstore_user as a WHERE a.username = ?1"  
 *    signature="olstore.entity.UserLocal findByUsername(java.lang.String username)"
 * 
 * @ejb.finder 
 *    query="SELECT f.user FROM olstore_user u, IN (u.friends) f WHERE u.username = ?1 AND ?2 MEMBER OF f.user.purchased"  
 *    signature="java.util.Collection findByFriendsOfPurchased(java.lang.String username, olstore.entity.ItemLocal purchasedItem)"
 * 
 * 
 * 
 *--
 * This is needed for JOnAS.
 * If you are not using JOnAS you can safely remove the tags below.
 * @jonas.bean ejb-name="User"
 * @jonas.finder-method-jdbc-mapping  method-name="findAll"
 *	jdbc-where-clause=""
 * @jonas.jdbc-mapping  jndi-name="jdbc_1"
 *	jdbc-table-name="olstore_user"
 * 
 *--
 *  
 **/

public abstract class UserBean implements EntityBean {
    
    /**
     * The  ejbCreate method.
     * 
     * @ejb.create-method 
     */
    public java.lang.String ejbCreate(String username, String role,String lname, String fname, String emailAdd, String phoneNum, String passwd) throws javax.ejb.CreateException {
        setUsername (username);
        setLname (lname);
        setFname (fname);
        setEmailAdd (emailAdd);
        setPhoneNum (phoneNum);
        setPasswd (passwd);
        setRole (role); 
        return null;
    }
    
    /**
     * The container invokes this method immediately after it calls ejbCreate.
     * 
     */
    public void ejbPostCreate(String username, String role, String lname, String fname, String emailAdd, String phoneNum, String passwd) throws javax.ejb.CreateException {
    }
    
    /**
     * Returns the username
     * @return the username
     * 
     * @ejb.persistent-field 
     * @ejb.persistence
     *    column-name="username"
     *     sql-type="VARCHAR"
     * @ejb.pk-field 
     * @ejb.interface-method
     * 
     */
    public abstract java.lang.String getUsername();
    
    /**
     * Sets the username
     * 
     * @param java.lang.String the new username value
     * 
     * @ejb.interface-method
     */
    public abstract void setUsername(java.lang.String username);
    
    /**
     * Returns the fname
     * @return the fname
     * 
     * @ejb.persistent-field 
     * @ejb.persistence
     *    column-name="fname"
     *     sql-type="VARCHAR"
     *  
     * @ejb.interface-method
     * 
     */
    public abstract java.lang.String getFname();
    
    /**
     * Sets the fname
     * 
     * @param java.lang.String the new fname value
     * 
     * @ejb.interface-method
     */
    public abstract void setFname(java.lang.String fname);
    
    /**
     * Returns the lname
     * @return the lname
     * 
     * @ejb.persistent-field 
     * @ejb.persistence
     *    column-name="lname"
     *     sql-type="VARCHAR"
     *  
     * @ejb.interface-method
     * 
     */
    public abstract java.lang.String getLname();
    
    /**
     * Sets the lname
     * 
     * @param java.lang.String the new lname value
     * 
     * @ejb.interface-method
     */
    public abstract void setLname(java.lang.String lname);
    
    /**
     * Returns the emailAdd
     * @return the emailAdd
     * 
     * @ejb.persistent-field 
     * @ejb.persistence
     *    column-name="emailAdd"
     *     sql-type="VARCHAR"
     *  
     * @ejb.interface-method
     * 
     */
    public abstract java.lang.String getEmailAdd();
    
    /**
     * Sets the emailAdd
     * 
     * @param java.lang.String the new emailAdd value
     * 
     * @ejb.interface-method
     */
    public abstract void setEmailAdd(java.lang.String emailAdd);
    
    /**
     * Returns the phoneNum
     * @return the phoneNum
     * 
     * @ejb.persistent-field 
     * @ejb.persistence
     *    column-name="phoneNum"
     *     sql-type="VARCHAR"
     *  
     * @ejb.interface-method
     * 
     */
    public abstract java.lang.String getPhoneNum();
    
    /**
     * Sets the phoneNum
     * 
     * @param java.lang.String the new phoneNum value
     * 
     * @ejb.interface-method
     */
    public abstract void setPhoneNum(java.lang.String phoneNum);
    
    /**
     * Returns the passwd
     * @return the passwd
     * 
     * @ejb.persistent-field 
     * @ejb.persistence
     *    column-name="passwd"
     *     sql-type="VARCHAR"
     *  
     * @ejb.interface-method
     * 
     */
    public abstract java.lang.String getPasswd();
    
    /**
     * Sets the passwd
     * 
     * @param java.lang.String the new passwd value
     * 
     * @ejb.interface-method
     */
    public abstract void setPasswd(java.lang.String passwd);
    
    /**
     * Returns the role
     * @return the role
     * 
     * @ejb.persistent-field 
     * @ejb.persistence
     *    column-name="role"
     *     sql-type="VARCHAR"
     *  
     * @ejb.interface-method
     * 
     */
    public abstract java.lang.String getRole();
    
    /**
     * Sets the role
     * 
     * @param java.lang.String the new role value
     * 
     * @ejb.interface-method
     */
    public abstract void setRole(java.lang.String role);
    
    /**
     * Returns the User's Address
     * 
     * @return the User's Address 
     *  
     * @ejb.interface-method
     * 
     * @ejb.relation
     *  name="User-Address"
     *  role-name="User-has-an-Address"
     *  target-ejb="Address"
     * 
     */
    
    public abstract AddressLocal getAddress ();
    
    /**
     * 
     * @param userAddress
     * 
     * @ejb.interface-method
     * 
     */
    
    public abstract void setAddress (AddressLocal userAddress);
    
    /**
     * Returns the User's Orders
     * 
     * @return the User's Orders 
     *  
     * @ejb.interface-method
     * 
     * @ejb.relation
     *  name="User-Order"
     *  role-name="User-has-many-Orders"
     *  target-ejb="Order"
     * 
     */
    
    public abstract Collection getOrders ();
    public abstract void setOrders (Collection orders);
    
    /**
     * Returns the User's Friends
     * 
     * @return the User's Friends 
     *  
     * @ejb.interface-method
     * 
     * @ejb.relation
     *  name="User-Friend"
     *  role-name="User-has-many-Friends"
     *  target-ejb="Friend"
     *  target-multiple="yes"
     *
     */
    
    public abstract Collection getFriends ();
    
    /**
     * Sets the User's friends
     * 
     * @param java.util.Collection friends
     * 
     * @ejb.interface-method
     *
     *  @ejb.transaction
     *  type="Required"
     *
     */
    
    public abstract void setFriends (Collection friends);
    
    /**
     * Returns the User's purchased Items
     * 
     * @return the User's purchased Items
     *  
     * @ejb.interface-method
     * 
     * @ejb.relation
     *  name="User-Item"
     *  role-name="User-has-many-Items"
     *  target-ejb="Item"
     *  target-multiple="yes"
     *
     */
    
    public abstract Collection getPurchased ();
    
    /**
     * Sets the purchased items
     * 
     * @param java.util.Collection the User's purchased items
     * 
     * @ejb.interface-method
     */
    
    public abstract void setPurchased (Collection purchased);	
    
    /**
     * 
     * @return
     * 
     * @ejb.interface-method
     * 
     */
    
    public boolean isAdmin () {
        String role = getRole();
        if ( role != null && ! role.equals("") && role.equals( "admin" ) ) {
            return true;
        } else {
            return false;
        }
    }
    
}
