package siprp.planoactuacao.print;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.jdom.Document;
import org.jdom.output.XMLOutputter;

import com.evolute.utils.fop.PDFCreator;
import com.evolute.utils.xml.XSLTransformer;

public class PlanoActuacaoPDFCreator
{
	public byte[] createPDF( Integer planoId )
		throws Exception
	{
		PlanoActuacaoToPrint plano = PlanoActuacaoPrintDataProvider.getProvider().getPlanoToPrint( planoId );
		Document foDoc = new Document( plano.toJdomElement() );
		XMLOutputter outputter = new XMLOutputter();
		ByteArrayOutputStream foBaos = new ByteArrayOutputStream();
		outputter.output( foDoc, foBaos );
		byte fo[] = 
			applyTemplate(
				getClass().getClassLoader().getResourceAsStream( "siprp/planoactuacao/print/plano_actuacao.xsl" ),
				new ByteArrayInputStream( foBaos.toByteArray() ) );
		byte pdf[] = PDFCreator.getPDFCreator().createPdfFromFo( fo );
		return pdf;
	}
	
	public byte[] applyTemplate( InputStream xsl, InputStream dataStream )
		throws Exception
	{
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		XSLTransformer.getXSLTransformer().transform( dataStream, xsl, baos );
		return baos.toByteArray();
	}
}
