Skip to content

Instantly share code, notes, and snippets.

@hprange
Created August 22, 2012 03:50
Show Gist options
  • Save hprange/3422144 to your computer and use it in GitHub Desktop.
Save hprange/3422144 to your computer and use it in GitHub Desktop.
Utilitário para executar operações em batch no easy-nfe
package br.com.easynfe.utils
import scala.io.Codec.charset2codec
import scala.io.Codec
import scala.io.Source
import org.apache.commons.codec.binary.Base64
import com.sun.jersey.api.client.config.DefaultClientConfig
import com.sun.jersey.api.client.Client
import br.com.doit.easynfe.client.NFeTransmitterProxy
import br.com.doit.easynfe.utils.NFeContextResolver
import br.com.easynfe.utils.BatchNFeCommunicator._
/**
* O BatchNFeCommunicator executa operações em notas do EasyNFe de acordo com um arquivo CSV
* contendo número da nota e série separados por ponto-e-vírgula.
*
* @author <a href="mailto:[email protected]">Henrique Prange</a>
*/
class BatchNFeCommunicator(justification: String, organization: String, certificatePath: String, password: String) {
val client = {
val config = new DefaultClientConfig
config.getClasses().add(classOf[NFeContextResolver])
Client.create(config)
}
val url = "http://easynfe.doit.com.br"
val transmitter = new NFeTransmitterProxy(client, url)
val certificate = base64encode(toByteArray(certificatePath))
def cancel(serie: Int, nfeNumber: Int) {
println("Cancelando nota Serie: " + serie + " Numero: " + nfeNumber)
transmitter.cancel(organization, serie, nfeNumber, justification, certificate, password)
}
}
object BatchNFeCommunicator {
val CSV_PATH = "path_to_csv_file"
val CERTIFICATE_PATH = "path_to_certificate_file"
def main(args: Array[String]) {
val communicator = new BatchNFeCommunicator("justification", "organization", CERTIFICATE_PATH, "password")
doForeachNfe(communicator.cancel)
}
def doForeachNfe(callback: (Int, Int) => Unit) {
Source.fromFile(CSV_PATH).getLines.foreach(line => {
val info = line.split(";")
val serie = info(1).toInt
val nfeNumber = info(0).toInt
runWithInterval(10, () => callback(serie, nfeNumber))
})
}
def runWithInterval(seconds: Int, callback: () => Unit) {
callback()
Thread sleep (seconds * 1000)
}
def base64encode(bytes: Array[Byte]): String = {
Base64.encodeBase64String(bytes)
}
def toByteArray(fileName: String): Array[Byte] = {
val source = Source.fromFile(fileName)(Codec.ISO8859)
try {
source.map(c => c.toByte).toArray
} finally {
source.close
}
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>br.com.easynfe</groupId>
<artifactId>easy-nfe-utils</artifactId>
<version>0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>br.com.doit</groupId>
<artifactId>easy-nfe-client</artifactId>
<version>1.4.5</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/src/main/scala</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment