diff --git a/trunk/SIPRPSoft/src/siprp/fop/PDFCreator.java b/trunk/SIPRPSoft/src/siprp/fop/PDFCreator.java new file mode 100644 index 00000000..2cf0e0cb --- /dev/null +++ b/trunk/SIPRPSoft/src/siprp/fop/PDFCreator.java @@ -0,0 +1,190 @@ +/* + * PDFCreator.java + * + * Created on 19 de Abril de 2006, 18:20 + * + * To change this template, choose Tools | Template Manager + * and open the template in the editor. + */ + +package siprp.fop; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import javax.swing.JFileChooser; +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.sax.SAXResult; +import javax.xml.transform.stream.StreamSource; + +import org.apache.avalon.framework.logger.ConsoleLogger; +import org.apache.fop.apps.Driver; +import org.apache.fop.apps.Options; +import org.apache.fop.viewer.SecureResourceBundle; +import org.apache.fop.viewer.Translator; + +import com.evolute.utils.fop.FOPCreator; + +/** + * PDFCreator creates PDF's using FO. + * + * @author Frederico Palma + * @version %I%, %G% + */ +public class PDFCreator +{ + private final Translator translator = new SecureResourceBundle( null ); + + private static final Object LOCK = new Object(); + + private static PDFCreator pdfCreator; + + private static String userConfig; + + public static PDFCreator getPDFCreator() + { + synchronized( LOCK ) + { + if( pdfCreator == null ) + { + pdfCreator = new PDFCreator(); + } + } + return pdfCreator; + } + + public static void setUserConfig(String string) + { + PDFCreator.userConfig = string; + } + + public static void main( String arg[] ) + throws Exception + { + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + final FOPCreator creator = FOPCreator.getFOPCreator(); + System.out.println( "Creating FO" ); + + JFileChooser chooser = new JFileChooser(); + chooser.setDialogTitle( "Select XML data file:" ); + int returnVal = chooser.showOpenDialog( null ); + String dataFile = null; + if( returnVal == JFileChooser.APPROVE_OPTION ) + { + dataFile = chooser.getSelectedFile().getAbsolutePath(); + } + chooser.setDialogTitle( "Select XSL-FO template file:" ); + returnVal = chooser.showOpenDialog( null ); + String templateFile = null; + if( returnVal == JFileChooser.APPROVE_OPTION ) + { + templateFile = chooser.getSelectedFile().getAbsolutePath(); +System.out.println( chooser.getSelectedFile().getAbsolutePath() ); + } + + final String DATA = dataFile; + final String TEMPLATE = templateFile; + creator.createFOfromXML( new FileInputStream( DATA ), + new FileInputStream( TEMPLATE ), baos ); +// Thread t = new Thread() { +// public void run() +// { +// try +// { +// creator.createFOfromXML( new FileInputStream( DATA ), +// new FileInputStream( TEMPLATE ), baos ); +// System.out.println( "FO created" ); +// } +// catch( Exception ex ) +// { +// ex.printStackTrace(); +// } +// } }; +// t.start(); +// Thread.currentThread().sleep( 2 ); + System.out.println( "Starting to print" ); +// options.put( FOP_DUPLEX_TYPE, javax.print.attribute.standard.Sides.TUMBLE ); + byte pdf[] = PDFCreator.getPDFCreator().createPdfFromFo( baos.toByteArray() ); +// new FOPPrinter().printFO( new FileInputStream( "c:\\simplecol.fo" ), true, false ); + File pdfFile = new File( "/home/fpalma/acss/teste.pdf" ); + File foFile = new File( "/home/fpalma/Desktop/teste.fo" ); + pdfFile.createNewFile(); + foFile.createNewFile(); + FileOutputStream fos = new FileOutputStream( pdfFile ); + FileOutputStream fofos = new FileOutputStream( foFile ); + fos.write( pdf ); + fofos.write( baos.toByteArray() ); + fos.close(); + fofos.close(); + System.out.println( "DONE" ); + } + + /** Creates a new instance of PDFCreator */ + private PDFCreator() + { + } + + /** + * Creates PDF content as an array of bytes, given FO content as an array of bytes. + * + * @param fo the byte[] with the FO content to convert + * @return the PDF content + * @throws Exception if the conversion fails + * + */ + public byte[]createPdfFromFo( byte []fo ) + throws Exception + { + + InputStream inputStreamConf = ClassLoader.getSystemClassLoader().getResourceAsStream("siprp/fop/fop.xml"); + InputStream inputStreamDTD = ClassLoader.getSystemClassLoader().getResourceAsStream("siprp/fop/config.dtd"); + + /*************************/ + + File f = File.createTempFile("fop", "conf"); + f.deleteOnExit(); + writeFile(inputStreamConf, f); + + System.out.println(f.getParentFile()); + + /*************************/ + File dtd = new File(f.getParentFile(), "config.dtd"); + writeFile(inputStreamDTD, dtd); + + new Options(f); + + ByteArrayOutputStream pdf = new ByteArrayOutputStream(); + TransformerFactory factory = TransformerFactory.newInstance(); + Transformer transformer = factory.newTransformer(); + Source src = new StreamSource( new ByteArrayInputStream( fo ) ); + Driver driver = new Driver(); + driver.setLogger( new ConsoleLogger( ConsoleLogger.LEVEL_INFO ) ); + driver.setRenderer( Driver.RENDER_PDF ); + driver.setOutputStream( pdf ); + Result res = new SAXResult( driver.getContentHandler() ); + transformer.transform( src, res ); + return pdf.toByteArray(); + + } + + private void writeFile(InputStream inputStreamConf, File f) + throws FileNotFoundException, IOException { + OutputStream out = new FileOutputStream(f); + byte buf[] = new byte[1024]; + int len; + while ((len = inputStreamConf.read(buf)) > 0) + out.write(buf, 0, len); + out.close(); + inputStreamConf.close(); + } +} diff --git a/trunk/SIPRPSoft/src/siprp/fop/config.dtd b/trunk/SIPRPSoft/src/siprp/fop/config.dtd new file mode 100644 index 00000000..6b05bebe --- /dev/null +++ b/trunk/SIPRPSoft/src/siprp/fop/config.dtd @@ -0,0 +1,22 @@ + + + + + + + + + + + + diff --git a/trunk/SIPRPSoft/src/siprp/fop/fop.xml b/trunk/SIPRPSoft/src/siprp/fop/fop.xml new file mode 100644 index 00000000..ca87f437 --- /dev/null +++ b/trunk/SIPRPSoft/src/siprp/fop/fop.xml @@ -0,0 +1,89 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/trunk/SIPRPSoft/src/siprp/higiene/relatorio/print/RelatorioPDFCreator.java b/trunk/SIPRPSoft/src/siprp/higiene/relatorio/print/RelatorioPDFCreator.java index 701c6c6a..0f4dc301 100644 --- a/trunk/SIPRPSoft/src/siprp/higiene/relatorio/print/RelatorioPDFCreator.java +++ b/trunk/SIPRPSoft/src/siprp/higiene/relatorio/print/RelatorioPDFCreator.java @@ -2,12 +2,17 @@ package siprp.higiene.relatorio.print; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileOutputStream; import java.io.InputStream; +import java.io.OutputStream; import org.jdom.Document; +import org.jdom.output.Format; import org.jdom.output.XMLOutputter; -import com.evolute.utils.fop.PDFCreator; +import siprp.fop.PDFCreator; + import com.evolute.utils.xml.XSLTransformer; public class RelatorioPDFCreator @@ -17,9 +22,12 @@ public class RelatorioPDFCreator { RelatorioToPrint relatorio = RelatorioPrintDataProvider.getProvider().getRelatorioToPrint( relatorioId ); Document foDoc = new Document( relatorio.toJdomElement() ); - XMLOutputter outputter = new XMLOutputter(); + XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); ByteArrayOutputStream foBaos = new ByteArrayOutputStream(); +// FileOutputStream fofos = new FileOutputStream("/home/jneto/Desktop/a.xml"); outputter.output( foDoc, foBaos ); +// outputter.output( foDoc, fofos ); + // System.out.println( new String( foBaos.toByteArray() ) ); byte fo[] = applyTemplate( @@ -27,7 +35,11 @@ public class RelatorioPDFCreator "siprp/higiene/relatorio/print/relatorio.xsl" ), new ByteArrayInputStream( foBaos.toByteArray() ) ); // System.out.println( new String( fo ) ); - byte pdf[] = PDFCreator.getPDFCreator().createPdfFromFo( fo ); + + //FIXME: VERSAO PDFCREATOR HACKADA. FAZER UPGRADE PARA 0.20 + PDFCreator pdfCreator = PDFCreator.getPDFCreator(); + + byte pdf[] = pdfCreator.createPdfFromFo( fo ); return pdf; } diff --git a/trunk/SIPRPSoft/src/siprp/higiene/relatorio/print/RelatorioPrintDataProvider.java b/trunk/SIPRPSoft/src/siprp/higiene/relatorio/print/RelatorioPrintDataProvider.java index 8b66f6bd..d58e0bb4 100644 --- a/trunk/SIPRPSoft/src/siprp/higiene/relatorio/print/RelatorioPrintDataProvider.java +++ b/trunk/SIPRPSoft/src/siprp/higiene/relatorio/print/RelatorioPrintDataProvider.java @@ -1,5 +1,7 @@ package siprp.higiene.relatorio.print; +import java.util.Collections; +import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.Vector; @@ -22,6 +24,8 @@ import com.evolute.utils.sql.Select2; public class RelatorioPrintDataProvider { private static final Object LOCK = new Object(); + protected static final Object CONTROLLED = "Controlado"; + protected static final Object INDETERMINATE = "Indeterminado"; private static RelatorioPrintDataProvider instance = null; protected final Executer EXECUTER; @@ -394,7 +398,7 @@ public class RelatorioPrintDataProvider Select select = new Select2( new String[]{ "hs_relatorio_risco", "hs_relatorio_posto_risco", "hs_relatorio_risco_valor_qualitativo" }, - new Integer[]{ Select2.JOIN_INNER, Select2.JOIN_INNER }, + new Integer[]{ Select2.JOIN_INNER, Select2.JOIN_LEFT_OUTER }, new Expression[]{ new Field( "hs_relatorio_risco.id" ).isEqual( new Field( "hs_relatorio_posto_risco.risco_id" ) ), new Field( "hs_relatorio_posto_risco.valor_qualitativo_id" ).isEqual( new Field( "hs_relatorio_risco_valor_qualitativo.id" ) ) @@ -405,8 +409,11 @@ public class RelatorioPrintDataProvider "hs_relatorio_posto_risco.severidade", "hs_relatorio_risco_valor_qualitativo.description" }, new Field( "hs_relatorio_posto_risco.posto_id" ).isEqual( postoId ).and( - new Field( "hs_relatorio_risco.deleted_date" ).isEqual( null ) ), - new String[]{ "hs_relatorio_risco.id" }, + new Field( "hs_relatorio_risco.deleted_date" ).isEqual( null ).and( + new Field("hs_relatorio_posto_risco.probabilidade").isDifferent(null).or( + new Field("hs_relatorio_posto_risco.severidade").isDifferent(null).or( + new Field("hs_relatorio_posto_risco.valor_qualitativo_id").isDifferent(null)))) ), + new String[]{ "hs_relatorio_posto_risco.probabilidade*hs_relatorio_posto_risco.severidade DESC" }, null, null, null ); @@ -429,6 +436,44 @@ public class RelatorioPrintDataProvider getMedidasToPrintByRiscoId( id ) ) ); } + Collections.sort(riscos, new Comparator(){ + + @Override + public int compare(RiscoToPrint o1, RiscoToPrint o2) { + int comparevalue = 0; + if (o1.probabilidade != null && o1.severidade != null + && o2.probabilidade != null && o2.severidade != null){ + Integer o1Value = o1.probabilidade*o1.severidade; + Integer o2Value = o2.probabilidade*o2.severidade; + comparevalue = 0-o1Value.compareTo(o2Value); + } else if (o1.valorQualitativo != null + && o1.valorQualitativo.length() > 0 + && o2.valorQualitativo != null + && o2.valorQualitativo.length() > 0) { + if(o1.valorQualitativo.equals(CONTROLLED)){ + comparevalue = -1; + }else if(o1.valorQualitativo.equals(INDETERMINATE)){ + comparevalue = 1; + }else{ + if(o2.valorQualitativo.equals(CONTROLLED)){ + comparevalue = 1; + }else{ + comparevalue = -1; + } + } + }else{ + if(o1.valorQualitativo!=null){ + comparevalue = 1; + }else{ + comparevalue = -1; + } + } + return comparevalue; + + } + + }); + return riscos; } diff --git a/trunk/SIPRPSoft/src/siprp/higiene/relatorio/print/RiscoToPrint.java b/trunk/SIPRPSoft/src/siprp/higiene/relatorio/print/RiscoToPrint.java index 25a2e15b..b409aea8 100644 --- a/trunk/SIPRPSoft/src/siprp/higiene/relatorio/print/RiscoToPrint.java +++ b/trunk/SIPRPSoft/src/siprp/higiene/relatorio/print/RiscoToPrint.java @@ -37,7 +37,7 @@ public class RiscoToPrint Element probabilidadeElement = new Element( "probabilidade" ); probabilidadeElement.setText( probabilidade != null ? probabilidade.toString() : " " ); riscoElement.addContent( probabilidadeElement ); - Element valorElement = new Element( "valor" ); + Element valorElement = new Element( "valor-numerico" ); valorElement.setText( ( severidade != null && probabilidade != null ) ? "" + ( severidade * probabilidade ) : " " ); riscoElement.addContent( valorElement ); Element valorQualitativoElement = new Element( "valor-qualitativo" ); diff --git a/trunk/SIPRPSoft/src/siprp/higiene/relatorio/print/relatorio.xsl b/trunk/SIPRPSoft/src/siprp/higiene/relatorio/print/relatorio.xsl index 8416f0d1..fce6baab 100644 --- a/trunk/SIPRPSoft/src/siprp/higiene/relatorio/print/relatorio.xsl +++ b/trunk/SIPRPSoft/src/siprp/higiene/relatorio/print/relatorio.xsl @@ -3809,10 +3809,9 @@ + text-align="left" > + src="url('http://www.evolute.pt/~siprp/planoactuacao/siprp_logo.jpg')" height="2cm"/> @@ -3823,7 +3822,7 @@ + src="url('{$logotipo}')" height="2cm"/> @@ -4110,14 +4109,18 @@ writing-mode="lr-tb" padding="2pt"> + text-align="left" margin-left="0.1in"> + + + - - + + +