/**
 * 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
 *
 */
/*
 * TypeHelperBean.java
 * 
 * Created on Aug 26, 2004
 *
 */
package olstore.session.helper;

import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;

import javax.ejb.CreateException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

import olstore.dto.PropertyValue;
import olstore.dto.TypeValue;
import olstore.framework.EJBHomeFactory;

import org.apache.commons.beanutils.BeanUtils;

import olstore.entity.PropertyLocal;
import olstore.entity.PropertyLocalHome;
import olstore.entity.TypeLocalHome;

/**
 * @ejb.bean name="TypeHelper"
 *	type="Stateless" 
 *  view-type="local"
 *  local-jndi-name="TypeHelperLocal_L"
 * 
 * @ejb.ejb-ref
 *  ejb-name="Type"
 *  view-type="local"
 * 
 * @ejb.ejb-ref
 *  ejb-name="Property"
 *  view-type="local"
 * 
 * @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="TypeHelper"
 * jndi-name="TypeHelperLocal"
 * 
 *--
 **/

public abstract class TypeHelperBean implements SessionBean {
    
    SessionContext ejbContext;
    
    // ------------------------------------------------------------------
    // SessionBean implementation
    // ------------------------------------------------------------------
    
    
    public void setSessionContext(SessionContext ctx) {
        ejbContext = ctx;
    }
    
    
    public void ejbRemove() {
    }
    
    
    public void ejbCreate() throws CreateException {
    }
    
    public void ejbPassivate() {
    }
    
    public void ejbActivate() {
    }
    
    // ------------------------------------------------------------------
    // TypeHelper implementation
    // ------------------------------------------------------------------
    
    /**
     * 
     * @ejb.interface-method
     * 
     */
    
    public TypeValue getTypeValueForName ( String name ) throws Exception {
        
        EJBHomeFactory factory = EJBHomeFactory.getInstance();
        TypeLocalHome typeHome = (TypeLocalHome) factory.getLocalHome ( EJBHomeFactory.TYPE );
        olstore.entity.TypeLocal type = typeHome.findByName ( name );
        TypeValue typeVal = TypeToDTO ( type ); 
        return typeVal;
    }
    
    /**
     * 
     * @ejb.interface-method
     * 
     */
    public void saveType ( TypeValue typeVal ) throws Exception {
        
        //determine if it already exists
        EJBHomeFactory factory = EJBHomeFactory.getInstance();
        TypeLocalHome typeHome = (TypeLocalHome) factory.getLocalHome ( EJBHomeFactory.TYPE );
        olstore.entity.TypeLocal type = null;
        
        String name = typeVal.getName();
        try {
            type = typeHome.findByName ( name );
        } catch ( Exception e ) {
            //could not find the Type, try and create a new
            //one by this name
            type = typeHome.create ( name );
        }
        
        updateType ( type, typeVal );
    }
    
    private void updateType ( olstore.entity.TypeLocal type , TypeValue typeVal ) throws Exception {
        
        //Get instance vars we will need
        EJBHomeFactory factory = EJBHomeFactory.getInstance();
        PropertyLocalHome propHome = (PropertyLocalHome) factory.getLocalHome ( EJBHomeFactory.PROP );
        TypeLocalHome typeHome = (TypeLocalHome) factory.getLocalHome ( EJBHomeFactory.TYPE );
        
        
        //Clear any existing Properties
        Collection props = type.getProperties();
        Iterator propIt = props.iterator();
        while ( propIt.hasNext() ) {
            PropertyLocal prop = (PropertyLocal) propIt.next();
            propIt.remove();
            prop.remove();
        }
        
        //Load new Properties
        Collection newProps = typeVal.getProperties();
        Iterator newPropIt = newProps.iterator();
        while ( newPropIt.hasNext() ) {
            PropertyValue newProp = (PropertyValue) newPropIt.next();
            PropertyLocal newPropLocal = propHome.create ( 
                    newProp.getName(), newProp.getValue() );
            props.add ( newPropLocal );
        }
        
    }
    
    
    private TypeValue TypeToDTO ( olstore.entity.TypeLocal type ) throws Exception {
        TypeValue typeVal = new TypeValue();
        type.setName ( typeVal.getName() );
        
        //properties
        Vector newProps = new Vector();
        Collection oldProps = type.getProperties();
        Iterator it = oldProps.iterator();
        while ( it.hasNext() ) {
            PropertyLocal prop = (PropertyLocal)it.next();
            PropertyValue propVal = new PropertyValue();
            BeanUtils.copyProperties ( propVal, prop );
            newProps.add ( propVal );
        }
        typeVal.setProperties ( newProps );
        
        return typeVal;
    }
    
}
