segunda-feira, 13 de maio de 2013

Copiar arquivos no Java

Dessa forma o arquivo de destino não é alterado durante a cópia:


  1.  public static void copyFile(File source, File destination) throws IOException {  
  2.      if (destination.exists())  
  3.          destination.delete();  
  4.   
  5.      FileChannel sourceChannel = null;  
  6.      FileChannel destinationChannel = null;  
  7.   
  8.      try {  
  9.          sourceChannel = new FileInputStream(source).getChannel();  
  10.          destinationChannel = new FileOutputStream(destination).getChannel();  
  11.          sourceChannel.transferTo(0, sourceChannel.size(),  
  12.                  destinationChannel);  
  13.      } finally {  
  14.          if (sourceChannel != null && sourceChannel.isOpen())  
  15.              sourceChannel.close();  
  16.          if (destinationChannel != null && destinationChannel.isOpen())  
  17.              destinationChannel.close();  
  18.     }  
  19. }  

Dessa forma o arquivo de destino pode sofrer alterações:

  1. public class Teste {  
  2.     public static void main(String[] args) throws IOException {  
  3.         File arquivoOrigem = new File("c:/temp/original.txt");  
  4.         FileReader fis = new FileReader(arquivoOrigem);  
  5.         BufferedReader bufferedReader = new BufferedReader(fis);  
  6.         StringBuilder buffer = new StringBuilder();  
  7.         String line = "";  
  8.         while ((line = bufferedReader.readLine()) != null) {  
  9.             buffer.append(line).append("\n");             
  10.         }  
  11.           
  12.         fis.close();  
  13.         bufferedReader.close();  
  14.           
  15.         File arquivoDestino = new File("C:/temp/copia.txt");  
  16.         FileWriter writer = new FileWriter(arquivoDestino);  
  17.         writer.write(buffer.toString());  
  18.         writer.flush();  
  19.         writer.close();  
  20.           
  21.           
  22.           
  23.           
  24.     }  
  25. } 

quarta-feira, 17 de abril de 2013

JSF: Setar foco em um elemento que foi renderizado



<p:outputPanel id="optPanel">
   <h:inputText value="#{myBean.nome}" id="nome"/>

   <h:inputText>
      <a4j:ajax event="complete"  render="optPanel" oncomplete="nome.focus();"/>
   </h:inputText>
</p:outputPanel>

sexta-feira, 15 de março de 2013

limitar o número de registros retornados em uma consulta SQL


DB2 -- select * from table fetch first 10 rows only 
Informix, InterBase/Firebird, SKIP  -- select first 10 * from table 
Microsoft SQL Server and Access -- select top 10 * from table 
MySQL, PostgreSQL, SQLite, HSQL/H2 -- select * from table limit 10 
Oracle -- select * from table where rownum <= 10
Fonte: http://pontodebug.blogspot.com.br/2011/01/como-limitar-o-numero-de-registros.html

sexta-feira, 22 de fevereiro de 2013

JSF: selectBooleanCheckbox não funciona no primeiro click

Problema:
selectBooleanCheckbox não funciona no primeiro click, apenas do segundo em diante.


<a4j:commandButton
image="/resources/images/16x16/add.png"
execute="@form"
render="baixaPane"
oncomplete="#{rich:component('baixaPane')}.show();">
</a4j:commandButton>


<rich:column>

<f:facet name="header">BAIXAR</f:facet>
<h:selectBooleanCheckbox  value="#{lancamento.baixar}"
valueChangeListener="#{movimentoContaMB.receptor}" >
<a4j:ajax execute="@form" render="@form" />
</h:selectBooleanCheckbox>
</rich:column>

Solução:
Remover o render do a4j:commandButton.
<a4j:commandButton
image="/resources/images/16x16/add.png"
execute="@form"
oncomplete="#{rich:component('baixaPane')}.show();">
</a4j:commandButton>

terça-feira, 5 de fevereiro de 2013

Hibernate: Multipos @DiscriminatorValue / Multiple discriminator values



// Classe Base 
@DiscriminatorFormula("case when str_origem in ('CP','FE') then 'CP' when str_origem in ('CR','FE') then 'CR'  when str_origem in ('CP','CR','FE') then 'AL' end")
@DiscriminatorValue(value="AL")
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class LancamentoCc implements Serializable{
}

// SubClasse
@Entity
@DiscriminatorValue(value="CP")
public class ContaPagar extends LancamentoCc implements Serializable,Cloneable{
}

JSF: Exibir valor de h:inputText em managed bean

FacesContext ctx = FacesContext.getCurrentInstance();

Map<String,String> request = ctx.getExternalContext().getRequestParameterMap();
String data = request.get("NRO_PROCESSO");

sexta-feira, 1 de fevereiro de 2013

Firebird: Exibir total de registros de todas as tabelas


CREATE PROCEDURE SOMAREGISTROS
RETURNS (
    TABELA VARCHAR (31),
    QTD_REG INTEGER)
AS  declare variable comando varchar(100);
  declare variable qtdtot integer;
  declare variable qtdtab integer;

begin
     qtdtot = 0;
     qtdtab = 0;
   
     for select rdb$relation_name
         from rdb$relations
         where rdb$flags = 1 and rdb$view_blr is null
         order by 1
         into :tabela do
     begin
          qtdtab = qtdtab + 1;
          comando = 'select count(*) from ' || :tabela;
          execute statement :comando into :qtd_reg;
          qtdtot = :qtdtot + :qtd_reg;
          suspend;
     end
     tabela = :qtdtab || ' Tabelas - Total:';
     qtd_reg = :qtdtot;
     suspend;
end