
package org.mule.modules.salesforce.processors;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import org.mule.api.MuleContext;
import org.mule.api.MuleEvent;
import org.mule.api.config.ConfigurationException;
import org.mule.api.construct.FlowConstruct;
import org.mule.api.construct.FlowConstructAware;
import org.mule.api.context.MuleContextAware;
import org.mule.api.lifecycle.Initialisable;
import org.mule.api.registry.RegistrationException;
import org.mule.api.transformer.Transformer;
import org.mule.config.i18n.MessageFactory;
import org.mule.transformer.TransformerTemplate;
import org.mule.transport.NullPayload;

@Generated(value = "Mule DevKit Version 3.4-SNAPSHOT", date = "2012-07-18T11:29:04-05:00", comments = "Build UNKNOWN_BUILDNUMBER")
public abstract class AbstractMessageProcessor<O >
    extends AbstractConnectedProcessor
    implements FlowConstructAware, MuleContextAware, Initialisable
{

    /**
     * Module object
     * 
     */
    protected O moduleObject;
    /**
     * Mule Context
     * 
     */
    protected MuleContext muleContext;
    /**
     * Flow construct
     * 
     */
    protected FlowConstruct flowConstruct;

    /**
     * Sets moduleObject
     * 
     * @param value Value to set
     */
    public void setModuleObject(O value) {
        this.moduleObject = value;
    }

    /**
     * Sets muleContext
     * 
     * @param value Value to set
     */
    public void setMuleContext(MuleContext value) {
        this.muleContext = value;
    }

    /**
     * Sets flowConstruct
     * 
     * @param value Value to set
     */
    public void setFlowConstruct(FlowConstruct value) {
        this.flowConstruct = value;
    }

    /**
     * Retrieves moduleObject
     * 
     */
    public O getModuleObject() {
        return this.moduleObject;
    }

    /**
     * Retrieves muleContext
     * 
     */
    public MuleContext getMuleContext() {
        return this.muleContext;
    }

    /**
     * Retrieves flowConstruct
     * 
     */
    public FlowConstruct getFlowConstruct() {
        return this.flowConstruct;
    }

    /**
     * Obtains the expression manager from the Mule context and initialises the connector. If a target object  has not been set already it will search the Mule registry for a default one.
     * 
     * @throws InstantiationException
     * @throws ConfigurationException
     * @throws IllegalAccessException
     * @throws RegistrationException
     */
    protected void findOrCreate(Class moduleClass, boolean shouldAutoCreate)
        throws IllegalAccessException, InstantiationException, ConfigurationException, RegistrationException
    {
        if (moduleObject == null) {
            moduleObject = ((O) muleContext.getRegistry().lookupObject(moduleClass));
            if (moduleObject == null) {
                if (shouldAutoCreate) {
                    moduleObject = ((O) moduleClass.newInstance());
                    muleContext.getRegistry().registerObject(moduleClass.getName(), moduleObject);
                } else {
                    throw new ConfigurationException(MessageFactory.createStaticMessage("Cannot find object"));
                }
            }
        }
        if (moduleObject instanceof String) {
            moduleObject = muleContext.getRegistry().lookupObject(((String) moduleObject));
            if (moduleObject == null) {
                throw new ConfigurationException(MessageFactory.createStaticMessage("Cannot find object by config name"));
            }
        }
    }

    /**
     * Overwrites the event payload with the specified one
     * 
     */
    public void overwritePayload(MuleEvent event, Object resultPayload)
        throws Exception
    {
        TransformerTemplate.OverwitePayloadCallback overwritePayloadCallback = null;
        if (resultPayload == null) {
            overwritePayloadCallback = new TransformerTemplate.OverwitePayloadCallback(NullPayload.getInstance());
        } else {
            overwritePayloadCallback = new TransformerTemplate.OverwitePayloadCallback(resultPayload);
        }
        List<Transformer> transformerList;
        transformerList = new ArrayList<Transformer>();
        transformerList.add(new TransformerTemplate(overwritePayloadCallback));
        event.getMessage().applyTransformers(event, transformerList);
    }

}
