/*
 * Created on Dec 10, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package olstore.session.helper;

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

import olstore.entity.ItemLocal;
import olstore.entity.ItemLocalHome;
import olstore.entity.TypeLocalHome;
import olstore.entity.TypeLocal;
import olstore.framework.EJBHomeFactory;
import olstore.entity.PictureLocal;

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

import javax.xml.soap.AttachmentPart;

import org.apache.axis.MessageContext;

import org.apache.axis.attachments.AttachmentsImpl;
import org.apache.axis.Message;

/**
 * @author mwringe
 * 
 * @ejb.bean name="ItemSpecialHelper" type="Stateless"
 *           view-type="remote-service-endpoint" jndi-name="ItemSpecialRemote"
 * 
 * @ejb.transaction type="Required"
 * 
 *  -- This is need for JOnAS. If you are not using JOnAS you can safetly remove
 * the tags below.
 * 
 * @jonas.bean ejb-name="ItemSpecialHelper" jndi-name="ItemSpecialRemote"
 *  --
 *  
 */
public class ItemSpecialHelperBean implements SessionBean {

    private SessionContext ejbContext;

    public void setSessionContext(SessionContext ctx) {
        ejbContext = ctx;
    }

    public void ejbRemove() {
    }

    public void ejbCreate() throws CreateException {
    }

    public void ejbPassivate() {
    }

    public void ejbActivate() {
    }

    /**
     * 
     * @return All items on special (name, description and price)
     * @throws Exception
     * 
     * @ejb.interface-method view-type="remote-service-endpoint"
     *                       generate="service-endpoint"
     *                       service-endpoint-extends="java.rmi.Remote"
     *  
     */
    public String[][] allSpecials() throws Exception {
        EJBHomeFactory factory = EJBHomeFactory.getInstance();
        ItemLocalHome home = (ItemLocalHome) factory
                .getLocalHome(EJBHomeFactory.ITEM);
        Collection specials = home.findBySpecialOffer();
        Iterator specialIt = specials.iterator();

        String sArray[][] = new String[specials.size()][5];

        // get the response message for this request
        MessageContext msgContext = MessageContext.getCurrentContext();
        Message resMsg = msgContext.getResponseMessage();

        int counter = 0;
        while (specialIt.hasNext()) {
            ItemLocal item = (ItemLocal) specialIt.next();

            // Create the attachment as a MIME attachment
            AttachmentPart attachment = resMsg.createAttachmentPart();
            resMsg.getAttachmentsImpl().setSendType(
                    AttachmentsImpl.SEND_TYPE_MIME);
            attachment.setDataHandler(getPictureForItem(item));
            attachment.setContentType("images/jpeg");
            resMsg.addAttachmentPart(attachment);

            sArray[counter][0] = item.getTypeId().getName();
            sArray[counter][1] = item.getName();
            sArray[counter][2] = item.getLongDesc();
            sArray[counter][3] = (item.getPrice()).toString();
            sArray[counter][4] = item.getItemId().toString();
            counter++;
        }

        return sArray;

    }

    private DataHandler getPictureForItem(ItemLocal item) throws Exception {
        Collection pics = item.getPictures();
        Iterator it = pics.iterator();
        PictureLocal pic = null;

        while (it.hasNext()) {
            pic = (PictureLocal) it.next();
            if (pic.getPriority() == 0) {
                break;
            }
        }

        
        DataHandler dh = new DataHandler(this.getClass().getResource(pic.getLocation()));

        return dh;
    }

    public String[][] getSpecials(String type) throws Exception {
        EJBHomeFactory factory = EJBHomeFactory.getInstance();
        ItemLocalHome home = (ItemLocalHome) factory
                .getLocalHome(EJBHomeFactory.ITEM);
        Collection specials = home.findByTypeAndSpecialOffer(type);
        Iterator specialIt = specials.iterator();

        String sArray[][] = new String[specials.size()][5];

        // get the response message for this request
        MessageContext msgContext = MessageContext.getCurrentContext();
        Message resMsg = msgContext.getResponseMessage();

        int counter = 0;

        while (specialIt.hasNext()) {
            ItemLocal item = (ItemLocal) specialIt.next();

            // Create the attachment as a MIME attachment
            AttachmentPart attachment = resMsg.createAttachmentPart();
            resMsg.getAttachmentsImpl().setSendType(
                    AttachmentsImpl.SEND_TYPE_MIME);
            attachment.setDataHandler(getPictureForItem(item));
            attachment.setContentType("images/jpeg");
            resMsg.addAttachmentPart(attachment);

            sArray[counter][0] = item.getTypeId().getName();
            sArray[counter][1] = item.getName();
            sArray[counter][2] = item.getLongDesc();
            sArray[counter][3] = (item.getPrice()).toString();
            sArray[counter][4] = item.getItemId().toString();
            counter++;
        }

        return sArray;
    }

    public String[] getTypes() throws Exception {
        EJBHomeFactory factory = EJBHomeFactory.getInstance();
        TypeLocalHome home = (TypeLocalHome) factory
                .getLocalHome(EJBHomeFactory.TYPE);
        Collection types = home.findAll();
        Iterator typesIt = types.iterator();

        String tArray[] = new String[types.size()];
        int counter = 0;
        while (typesIt.hasNext()) {
            TypeLocal type = (TypeLocal) typesIt.next();
            tArray[counter] = type.getName();
            counter++;
        }

        return tArray;
    }

}
