//
// FlowStepBodySet.java - Copy variable to message body
//

package nyangau.jmsadapter;

//...simports:0:
import java.io.Serializable;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.Text;
import javax.jms.JMSException;
//...e

public class FlowStepBodySet extends FlowStep
  {
  protected String var;
  protected boolean clear;
  protected String item;  // used if MapMessage
//...sFlowStepBodySet:2:
public FlowStepBodySet(Node eStep)
  throws FlowProcessorException
  {
  super(eStep);
  NamedNodeMap nnmStep = eStep.getAttributes();
  Node aVar = nnmStep.getNamedItem("var");
  var = ( aVar != null ) ? aVar.getNodeValue() : "v0";
  Node aClear = nnmStep.getNamedItem("clear");
  clear = ( aClear != null ) ? aClear.getNodeValue().equals("true") : true;
  Node aItem = nnmStep.getNamedItem("item");
  item = ( aItem != null ) ? aItem.getNodeValue() : null;
  }
//...e
//...sprocess:2:
public void process(FlowContext c)
  throws FlowProcessorException, JMSException
  {
  JmsaMessage m = c.getMessage();
  Object o = c.getVar(var);
  if ( o == null )
    throw new FlowProcessorException(var+" undefined in step "+name);
  if ( o instanceof FlowNullValue )
    throw new FlowProcessorException(var+" is null in step "+name);
  if ( clear )
    m.clearBody();
  if ( m instanceof JmsaBytesMessage )
//...shandle BytesMessage using writeObject\40\\41\:6:
{
((JmsaBytesMessage)m).writeObject(o);
  // works if o is one of
  //   byte[]
  //   Boolean
  //   Byte
  //   Character
  //   Short
  //   Integer
  //   Long
  //   Float
  //   Double
  //   String
  // else throws MesageFormatException
}
//...e
  else if ( m instanceof JmsaTextMessage )
//...shandle TextMessage using setText\40\\41\ and maybe toString\40\\41\:6:
{
if ( o instanceof String )
  ((JmsaTextMessage)m).setText((String) o);
else
  ((JmsaTextMessage)m).setText(o.toString());
}
//...e
  else if ( m instanceof JmsaMapMessage )
//...shandle MapMessage using setObject\40\\41\:6:
{
if ( item == null )
  throw new FlowProcessorException("item is null and we have a MapMessage");
((JmsaMapMessage)m).setObject(item, o);
  // works if o is one of
  //   Boolean
  //   Character
  //   Byte
  //   Short
  //   Integer
  //   Long
  //   Float
  //   Double
  //   String
  //   byte[]
  // else throws MessageFormatException
}
//...e
  else if ( m instanceof JmsaStreamMessage )
//...shandle StreamMessage using writeXxx\40\\41\:6:
{
((JmsaStreamMessage)m).writeObject(o);
  // works if o is one of
  //   byte[]
  //   Boolean
  //   Byte
  //   Character
  //   Short
  //   Integer
  //   Long
  //   Float
  //   Double
  //   String
  // else throws MessageFormatException
}
//...e
  else if ( m instanceof JmsaObjectMessage )
//...shandle ObjectMessage using setObject\40\\41\:6:
{
if ( o instanceof Serializable )
  ((JmsaObjectMessage)m).setObject((Serializable) o);
else
  throw new FlowProcessorException("object is not Serializable and we have an ObjectMessage in step "+name);
}
//...e
  else
    throw new FlowProcessorException("can't bodyset to plain old Message in step "+name);
  }
//...e
  }
