Skip to content

Commit

Permalink
* Metodo para bajar el archivo y procesarlo, falla por dependencia ci…
Browse files Browse the repository at this point in the history
…rcular
  • Loading branch information
ripper2hl committed Apr 11, 2024
1 parent 6988965 commit 0ee01e8
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.perales</groupId>
<artifactId>sepomex-api</artifactId>
<version>1.0-SNAPSHOT</version>
<version>1.1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
Expand Down Expand Up @@ -38,6 +38,16 @@
</parent>

<dependencies>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/com/perales/sepomex/service/ArchivoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,40 @@
import com.perales.sepomex.model.Archivo;
import com.perales.sepomex.model.Archivo;
import com.perales.sepomex.repository.ArchivoRepository;
import io.undertow.server.handlers.form.FormData;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.io.IOUtils;
import org.apache.lucene.search.Query;
import org.hibernate.search.jpa.FullTextEntityManager;
import org.hibernate.search.jpa.Search;
import org.hibernate.search.query.dsl.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.commons.fileupload.FileItem;
@Service
public class ArchivoService implements ServiceGeneric<Archivo, Integer> {

@Autowired
private ArchivoRepository ArchivoRepository;

@Autowired
private ColoniaService coloniaService;

@PersistenceContext
private EntityManager em;
Expand Down Expand Up @@ -55,4 +69,44 @@ public Archivo borrar(Integer id) {
ArchivoRepository.deleteById(id);
return Archivo;
}

private void descargarArchivo(String fileUrl, String destinationPath) throws Exception {
URL url = new URL(fileUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");

try (InputStream inputStream = connection.getInputStream();
FileOutputStream outputStream = new FileOutputStream(destinationPath)) {

byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
}
}

public Boolean procesarArchivo() {
try {
String fileUrl = "https://www.correosdemexico.gob.mx/datosabiertos/cp/cpdescarga.txt";
String tempFilePath = "/tmp/tempFile" + System.currentTimeMillis() + ".txt";
descargarArchivo(fileUrl, tempFilePath);
File file = new File(tempFilePath);
FileItem fileItem = new DiskFileItem("file", Files.probeContentType(file.toPath()),
false, file.getName(), (int) file.length(), file.getParentFile());

// Leer el contenido del archivo y copiarlo al FileItem
try (InputStream input = new FileInputStream(file);
OutputStream outputStream = fileItem.getOutputStream()) {
IOUtils.copy(input, outputStream);
}

MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
return coloniaService.actualizacionMasiva(multipartFile);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

}
6 changes: 3 additions & 3 deletions src/main/java/com/perales/sepomex/service/ColoniaService.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public class ColoniaService implements ServiceGeneric<Colonia, Long> {
@Autowired
private ColoniaRepository coloniaRepository;

@Autowired
private ArchivoService archivoService;

@Autowired
private CiudadRepository ciudadRepository;

Expand Down Expand Up @@ -132,9 +135,6 @@ public Colonia borrar(Long id) {
return colonia;
}

@Autowired
private ArchivoService archivoService;

public Boolean cargaMasiva(MultipartFile file) throws IOException {
EntityManager em = emf.createEntityManager();
try (BufferedReader br = new BufferedReader( new InputStreamReader( file.getInputStream() , "UTF-8") )) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertTrue;

@SpringBootTest(classes = AppTestConfig.class)
@WebAppConfiguration
Expand Down Expand Up @@ -109,4 +110,11 @@ void borrar() {
NoSuchElementException exception = Assertions.assertThrows( NoSuchElementException.class, () -> archivoService.buscarPorId( archivoId ) );
assertThat("Debe lanzar la un NoSuchElementException ", exception, is( notNullValue() ) );
}

@Test
void procesarArchivo() {
boolean result = archivoService.procesarArchivo();
assertTrue(result);
}

}

0 comments on commit 0ee01e8

Please sign in to comment.