package eu.osci.helper;

import java.io.OutputStream;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;

import eu.osci._2008._05.common.urn.messagetypes.letterstyle.LetterStyleMessageType;
import eu.osci._2008._05.common.urn.messagetypes.letterstyle.LetterStyleMsgBodyType;
import eu.osci._2008._05.common.urn.messagetypes.letterstyle.LetterStyleMessageType.LetterStyleMsg;
import eu.osci.messageparts.JaxbContextHolder;


/**
 * Helper class for LetterSytle creation.<br>
 * Copyright © 2010 Freie Hansestadt Bremen.<br>
 * Created by bremen online services Entwicklungs- und Betriebsgesellschaft mbH & Co. KG. <br>
 * This library (Starter Kit) can be used by anybody according to the Bremer Lizenz für freie
 * Softwarebibliotheken. The license terms can be viewed at www.osci.de or can be ordered at the
 * OSCI-Leitstelle (Die Senatorin für Finanzen, Freie Hansestadt Bremen, Postfach 10 15 40, 28015 Bremen).
 * 
 * @author A. Bastian, R. Lindemann
 */
public class LetterStyleHelper
{

  /**
   * Creates a DOM document including the given LetterStyleMessageType object.
   * 
   * @param _content LetterSytle object.
   * @return The DOM document.
   * @throws ParserConfigurationException
   * @throws JAXBException
   * @throws ParserConfigurationException
   */
  public static Document getDocumentFromLetterStyle(LetterStyleMessageType _content) throws JAXBException,
    ParserConfigurationException
  {
    JAXBElement<LetterStyleMessageType> messageBody = new JAXBElement<LetterStyleMessageType>(
                                                                                              QNAME_LETTER_STYLE,
                                                                                              LetterStyleMessageType.class,
                                                                                              null, _content);

    Document doc = XMLGeneralHelper.createDOMDocument();
    JaxbContextHolder.getLetterStyleJaxBContext().createMarshaller().marshal(messageBody, doc);
    return doc;
  }

  /**
   * Writes the LetterStyleMessageType JAXB object to the given OutputStream.
   * 
   * @param _content The LetterStyle object.
   * @param returnStream The OutputStream to write the response.
   * @throws JAXBException
   */
  public static void getStreamFromLetterStyle(LetterStyleMessageType _content, OutputStream returnStream)
    throws JAXBException
  {
    JAXBElement<LetterStyleMessageType> messageBody = new JAXBElement<LetterStyleMessageType>(
                                                                                              QNAME_LETTER_STYLE,
                                                                                              LetterStyleMessageType.class,
                                                                                              null, _content);
    JaxbContextHolder.getLetterStyleJaxBContext().createMarshaller().marshal(messageBody, returnStream);
  }



  /** Now we have to define the URI for the LetterStyle. */
  public static final QName QNAME_LETTER_STYLE = new QName(
                                                           "http://www.osci.eu/2008/05/common/urn/messageTypes/LetterStyle",
                                                           "LetterStyleMessage");





  /**
   * Creates a simple LetterStyle structure with the given parameter.
   * 
   * @param _subject The subject of the LetterStyle structure
   * @param _data The XML data of the LetterStyle structure
   * @return The created LetterStyle JaxB object.
   */
  public static LetterStyleMessageType getLetterStyle(String _subject, String _data)
  {
    LetterStyleMessageType letterStyleJaxbObject = new LetterStyleMessageType();

    LetterStyleMsg msgType = new LetterStyleMsg();
    letterStyleJaxbObject.setLetterStyleMsg(msgType);
    LetterStyleMsgBodyType msgBody = new LetterStyleMsgBodyType();
    msgBody.setId("LetterStyleBody");
    letterStyleJaxbObject.getLetterStyleMsg().setLetterStyleMsgBody(msgBody);
    letterStyleJaxbObject.getLetterStyleMsg().getLetterStyleMsgBody().setSubject(_subject);
    msgBody.getText().add(_data);
    return letterStyleJaxbObject;
  }

}
