forked from Coded/SIPRP
git-svn-id: https://svn.coded.pt/svn/SIPRP@1424 bb69d46d-e84e-40c8-a05a-06db0d633741
parent
0c3300fc93
commit
075f4240c0
Binary file not shown.
@ -1,218 +0,0 @@
|
|||||||
package siprp.higiene.gestao.importacao;
|
|
||||||
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Vector;
|
|
||||||
|
|
||||||
import org.jdom.Document;
|
|
||||||
import org.jdom.Element;
|
|
||||||
import org.jdom.input.SAXBuilder;
|
|
||||||
|
|
||||||
import com.evolute.utils.arrays.Virtual2DArray;
|
|
||||||
import com.evolute.utils.db.DBManager;
|
|
||||||
import com.evolute.utils.db.Executer;
|
|
||||||
import com.evolute.utils.db.JDBCManager;
|
|
||||||
import com.evolute.utils.db.keyretrievers.JDBCAutoKeyRetriever;
|
|
||||||
import com.evolute.utils.sql.Assignment;
|
|
||||||
import com.evolute.utils.sql.Expression;
|
|
||||||
import com.evolute.utils.sql.Field;
|
|
||||||
import com.evolute.utils.sql.Insert;
|
|
||||||
import com.evolute.utils.sql.Select;
|
|
||||||
import com.evolute.utils.sql.Select2;
|
|
||||||
import com.evolute.utils.strings.UnicodeChecker;
|
|
||||||
|
|
||||||
public class Importador
|
|
||||||
{
|
|
||||||
private Executer EXECUTER;
|
|
||||||
|
|
||||||
public static void main( String args[] )
|
|
||||||
throws Exception
|
|
||||||
{
|
|
||||||
Insert.setDefaultKeyRetriever( JDBCAutoKeyRetriever.DEFAULT );
|
|
||||||
UnicodeChecker.setUseDoubleSlash( true );
|
|
||||||
String url = "jdbc:postgresql://localhost:5436/siprp_local";
|
|
||||||
String user = "postgres";
|
|
||||||
String pwd = "Typein";
|
|
||||||
DBManager dbm = new JDBCManager( url, user, pwd, 10, 8, 8, null );
|
|
||||||
|
|
||||||
Importador importador = new Importador( dbm.getSharedExecuter(Importador.class) );
|
|
||||||
// importador.converter();
|
|
||||||
importador.importar();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Importador( Executer executer )
|
|
||||||
throws Exception
|
|
||||||
{
|
|
||||||
// DBManager dbm = ( DBManager ) Singleton.getInstance( Singleton.DEFAULT_DBMANAGER );
|
|
||||||
// EXECUTER = dbm.getSharedExecuter();
|
|
||||||
EXECUTER = executer;
|
|
||||||
}
|
|
||||||
|
|
||||||
// NAO APAGAR
|
|
||||||
// public void converter()
|
|
||||||
// throws Exception
|
|
||||||
// {
|
|
||||||
// InputStream xsl = getClass().getClassLoader().getResourceAsStream( "siprp/higiene/gestao/importacao/importacao_ods_to_xml.xsl" );
|
|
||||||
// InputStream xmlContent = getClass().getClassLoader().getResourceAsStream( "siprp/higiene/gestao/importacao/content.xml" );
|
|
||||||
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
||||||
// XSLTransformer.getXSLTransformer().transform( xmlContent, xsl, baos );
|
|
||||||
// FileOutputStream fos = new FileOutputStream( "/home/fpalma/projectos2/SIPRP/SIPRPSoft/src/siprp/higiene/gestao/importacao/data.xml" );
|
|
||||||
// fos.write( baos.toByteArray() );
|
|
||||||
// fos.close();
|
|
||||||
// }
|
|
||||||
|
|
||||||
public void importar()
|
|
||||||
throws Exception
|
|
||||||
{
|
|
||||||
SAXBuilder builder = new SAXBuilder();
|
|
||||||
InputStream fileFoIs = getClass().getClassLoader().getResourceAsStream( "siprp/higiene/gestao/importacao/data.xml" );
|
|
||||||
Document fileDocument = builder.build( fileFoIs );
|
|
||||||
Element root = fileDocument.getRootElement();
|
|
||||||
List<Element> entradas = root.getChildren( "entrada" );
|
|
||||||
for( Element entrada : entradas )
|
|
||||||
{
|
|
||||||
Element temaElement = entrada.getChild( "tema" );
|
|
||||||
String tema = getTexto( temaElement );
|
|
||||||
Element riscoElement = entrada.getChild( "risco" );
|
|
||||||
String risco = getTexto( riscoElement );
|
|
||||||
Element requisitoElement = entrada.getChild( "requisito" );
|
|
||||||
String requisito = getTexto( requisitoElement );
|
|
||||||
Element medidaElement = entrada.getChild( "medida" );
|
|
||||||
String medida = getTexto( medidaElement );
|
|
||||||
if( tema.length() == 0 || risco.length() == 0 )
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Integer idTema = inserirTema( tema );
|
|
||||||
Integer idRisco = inserirRisco( idTema, risco );
|
|
||||||
inserirMedida( idRisco, medida, requisito );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected String getTexto( Element elemento )
|
|
||||||
{
|
|
||||||
List<Element> linhas = elemento != null ? elemento.getChildren( "linha" ) : new Vector<Element>();
|
|
||||||
String texto = limparEspacos( linhas.size() > 0 ? linhas.get( 0 ).getText() : "" );
|
|
||||||
for( int l = 1; l < linhas.size(); l++ )
|
|
||||||
{
|
|
||||||
String linha = limparEspacos( linhas.get( 0 ).getText() );
|
|
||||||
texto += "\n" + linha;
|
|
||||||
}
|
|
||||||
return texto.trim();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected String limparEspacos( String texto )
|
|
||||||
{
|
|
||||||
texto = texto.replaceAll( "\n", " " );
|
|
||||||
texto = texto.replaceAll( "\t", "" );
|
|
||||||
while( texto.indexOf( " " ) != -1 )
|
|
||||||
{
|
|
||||||
texto = texto.replaceAll( " ", " " );
|
|
||||||
}
|
|
||||||
return texto;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Integer inserirTema( String tema )
|
|
||||||
throws Exception
|
|
||||||
{
|
|
||||||
Select select =
|
|
||||||
new Select2(
|
|
||||||
new String[]{ "hs_risco_tema" },
|
|
||||||
new Integer[]{},
|
|
||||||
new Expression[]{},
|
|
||||||
new String[]{ "id" },
|
|
||||||
new Field( "description" ).isEqual( tema ),
|
|
||||||
null,
|
|
||||||
null,
|
|
||||||
null,
|
|
||||||
null );
|
|
||||||
Virtual2DArray array = EXECUTER.executeQuery( select );
|
|
||||||
Integer id = ( array.columnLength() > 0 && array.get( 0, 0 ) != null ) ? ( Integer ) array.get( 0, 0 ) : null;
|
|
||||||
if( id == null )
|
|
||||||
{
|
|
||||||
Insert insert =
|
|
||||||
new Insert( "hs_risco_tema",
|
|
||||||
new Assignment[]{
|
|
||||||
new Assignment( "description", tema )
|
|
||||||
} );
|
|
||||||
EXECUTER.executeQuery( insert );
|
|
||||||
id = inserirTema( tema );
|
|
||||||
}
|
|
||||||
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Integer inserirRisco( Integer temaId, String risco )
|
|
||||||
throws Exception
|
|
||||||
{
|
|
||||||
Select select =
|
|
||||||
new Select2(
|
|
||||||
new String[]{ "hs_risco" },
|
|
||||||
new Integer[]{},
|
|
||||||
new Expression[]{},
|
|
||||||
new String[]{ "id" },
|
|
||||||
new Field( "description" ).isEqual( risco ).and(
|
|
||||||
new Field( "tema_id" ).isEqual( temaId ) ),
|
|
||||||
null,
|
|
||||||
null,
|
|
||||||
null,
|
|
||||||
null );
|
|
||||||
Virtual2DArray array = EXECUTER.executeQuery( select );
|
|
||||||
|
|
||||||
Integer id = ( array.columnLength() > 0 && array.get( 0, 0 ) != null ) ? ( Integer ) array.get( 0, 0 ) : null;
|
|
||||||
if( id == null )
|
|
||||||
{
|
|
||||||
Insert insert =
|
|
||||||
new Insert( "hs_risco",
|
|
||||||
new Assignment[]{
|
|
||||||
new Assignment( "description", risco ),
|
|
||||||
new Assignment( "tema_id", temaId )
|
|
||||||
} );
|
|
||||||
EXECUTER.executeQuery( insert );
|
|
||||||
id = inserirRisco( temaId, risco );
|
|
||||||
}
|
|
||||||
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected Integer inserirMedida( Integer riscoId, String medida, String requisito )
|
|
||||||
throws Exception
|
|
||||||
{
|
|
||||||
Select select =
|
|
||||||
new Select2(
|
|
||||||
new String[]{ "hs_medida", "hs_risco_medida" },
|
|
||||||
new Integer[]{ Select2.JOIN_LEFT_OUTER },
|
|
||||||
new Expression[]{
|
|
||||||
new Field( "hs_medida.id" ).isEqual( new Field( "hs_risco_medida.medida_id" ) ).and(
|
|
||||||
new Field( "hs_risco_medida.risco_id" ).isEqual( riscoId ) )
|
|
||||||
},
|
|
||||||
new String[]{ "hs_medida.id" },
|
|
||||||
new Field( "hs_medida.description" ).isEqual( medida ).and(
|
|
||||||
new Field( "hs_medida.requesitos_legais" ).isEqual( requisito ) ),
|
|
||||||
null,
|
|
||||||
null,
|
|
||||||
null,
|
|
||||||
null );
|
|
||||||
Virtual2DArray array = EXECUTER.executeQuery( select );
|
|
||||||
Integer id = array.columnLength() > 0 ? ( Integer ) array.get( 0, 0 ) : null;
|
|
||||||
if( id == null )
|
|
||||||
{
|
|
||||||
Insert insert =
|
|
||||||
new Insert( "hs_medida",
|
|
||||||
new Assignment[]{
|
|
||||||
new Assignment( new Field( "description" ), medida ),
|
|
||||||
new Assignment( new Field( "requesitos_legais" ), requisito ),
|
|
||||||
} );
|
|
||||||
EXECUTER.executeQuery( insert );
|
|
||||||
id = inserirMedida( null, medida, requisito );
|
|
||||||
insert =
|
|
||||||
new Insert( "hs_risco_medida",
|
|
||||||
new Assignment[]{
|
|
||||||
new Assignment( new Field( "medida_id" ), id ),
|
|
||||||
new Assignment( new Field( "risco_id" ), riscoId ),
|
|
||||||
} );
|
|
||||||
EXECUTER.executeQuery( insert );
|
|
||||||
}
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load Diff
@ -1,55 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<!--
|
|
||||||
Create the page layout
|
|
||||||
-->
|
|
||||||
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" version="1.0">
|
|
||||||
<xsl:template match="office:document-content">
|
|
||||||
<importacao>
|
|
||||||
<xsl:apply-templates select="office:body" />
|
|
||||||
</importacao>
|
|
||||||
</xsl:template>
|
|
||||||
<xsl:template match="office:body">
|
|
||||||
<xsl:apply-templates select="office:spreadsheet" />
|
|
||||||
</xsl:template>
|
|
||||||
<xsl:template match="office:spreadsheet">
|
|
||||||
<xsl:apply-templates select="table:table" />
|
|
||||||
</xsl:template>
|
|
||||||
<xsl:template match="table:table">
|
|
||||||
<xsl:for-each select="table:table-row">
|
|
||||||
<xsl:if test="position() > 1">
|
|
||||||
<entrada>
|
|
||||||
<xsl:for-each select="table:table-cell">
|
|
||||||
<xsl:choose>
|
|
||||||
<xsl:when test="position() = 1">
|
|
||||||
<tema>
|
|
||||||
<xsl:apply-templates />
|
|
||||||
</tema>
|
|
||||||
</xsl:when>
|
|
||||||
<xsl:when test="position() = 2">
|
|
||||||
<risco>
|
|
||||||
<xsl:apply-templates />
|
|
||||||
</risco>
|
|
||||||
</xsl:when>
|
|
||||||
<xsl:when test="position() = 3">
|
|
||||||
<requisito>
|
|
||||||
<xsl:apply-templates />
|
|
||||||
</requisito>
|
|
||||||
</xsl:when>
|
|
||||||
<xsl:when test="position() = 4">
|
|
||||||
<medida>
|
|
||||||
<xsl:apply-templates />
|
|
||||||
</medida>
|
|
||||||
</xsl:when>
|
|
||||||
</xsl:choose>
|
|
||||||
</xsl:for-each>
|
|
||||||
</entrada>
|
|
||||||
</xsl:if>
|
|
||||||
</xsl:for-each>
|
|
||||||
</xsl:template>
|
|
||||||
<xsl:template match="text:p">
|
|
||||||
<linha>
|
|
||||||
<xsl:value-of select="." />
|
|
||||||
</linha>
|
|
||||||
</xsl:template>
|
|
||||||
</xsl:stylesheet>
|
|
||||||
|
|
||||||
Loading…
Reference in new issue