Commit fc4d0496 authored by Aaron Gutierrez's avatar Aaron Gutierrez

Se agregó un utilitario para convertir el archivo de excel a csv

parent 7dd9e1b1
...@@ -4,18 +4,18 @@ ...@@ -4,18 +4,18 @@
<parent> <parent>
<groupId>com.bytesw.xdf</groupId> <groupId>com.bytesw.xdf</groupId>
<artifactId>byteXDF4Java-arq</artifactId> <artifactId>byteXDF4Java-arq</artifactId>
<version>3.1.0</version> <version>3.2.0-SNAPSHOT</version>
</parent> </parent>
<groupId>com.bytesw.bytebot</groupId> <groupId>com.bytesw.bytebot</groupId>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>bytebot-service</artifactId> <artifactId>bytebot-service</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>1.0.0</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<properties> <properties>
<xdf.version>3.1.0</xdf.version> <xdf.version>3.2.0-SNAPSHOT</xdf.version>
<camel.version>2.22.0</camel.version> <camel.version>2.22.0</camel.version>
<mainclass>com.bytesw.bytebot.BytebotApplication</mainclass> <mainclass>com.bytesw.bytebot.BytebotApplication</mainclass>
<jacoco.version>0.8.5</jacoco.version> <jacoco.version>0.8.5</jacoco.version>
...@@ -422,11 +422,6 @@ ...@@ -422,11 +422,6 @@
<artifactId>spring-boot-starter-undertow</artifactId> <artifactId>spring-boot-starter-undertow</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
......
...@@ -4,10 +4,10 @@ import com.bytesw.bytebot.bean.*; ...@@ -4,10 +4,10 @@ import com.bytesw.bytebot.bean.*;
import com.bytesw.bytebot.model.*; import com.bytesw.bytebot.model.*;
import com.bytesw.bytebot.model.enums.*; import com.bytesw.bytebot.model.enums.*;
import com.bytesw.bytebot.repository.*; import com.bytesw.bytebot.repository.*;
import com.bytesw.xdf.config.AppContextManager;
import com.bytesw.xdf.sql.beans.Pagination; import com.bytesw.xdf.sql.beans.Pagination;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.apache.camel.ProducerTemplate; import org.apache.camel.ProducerTemplate;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
...@@ -18,8 +18,6 @@ import org.springframework.stereotype.Service; ...@@ -18,8 +18,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.*; import java.util.*;
...@@ -452,6 +450,10 @@ public class AgentService extends CustomPaginationService<Agent> { ...@@ -452,6 +450,10 @@ public class AgentService extends CustomPaginationService<Agent> {
@Transactional(propagation = Propagation.REQUIRED) @Transactional(propagation = Propagation.REQUIRED)
public void synchronizeFiles(Long id, String user) { public void synchronizeFiles(Long id, String user) {
if (AppContextManager.getAppContext() == null) {
return;
}
FileConverter fileConverter = (FileConverter) AppContextManager.getAppContext().getBean("xlsToCsvFileConverter");
List<FrequentQuestion> frequentQuestions = frequentQuestionRepository.findAllByAgentIdAndStatus(id, FrequentQuestionStatusEnum.PENDING_SYNCHRONIZED); List<FrequentQuestion> frequentQuestions = frequentQuestionRepository.findAllByAgentIdAndStatus(id, FrequentQuestionStatusEnum.PENDING_SYNCHRONIZED);
...@@ -462,13 +464,13 @@ public class AgentService extends CustomPaginationService<Agent> { ...@@ -462,13 +464,13 @@ public class AgentService extends CustomPaginationService<Agent> {
Optional<QuestionFile> questionFileFound = questionFileRepository.findByUuid(frequentQuestion.getUuid()); Optional<QuestionFile> questionFileFound = questionFileRepository.findByUuid(frequentQuestion.getUuid());
if (questionFileFound.isPresent()) { if (questionFileFound.isPresent()) {
headers.put("CamelFileName", questionFileFound.get().getName()); headers.put("CamelFileName", fileConverter.getFileName(questionFileFound.get().getName()));
// try { // try {
// FileUtils.writeByteArrayToFile(new File("/home/schicoma/Escritorio/" + questionFileFound.get().getName()), questionFileFound.get().getData()); // FileUtils.writeByteArrayToFile(new File("/home/schicoma/Escritorio/" + questionFileFound.get().getName()), questionFileFound.get().getData());
// } catch (IOException e) { // } catch (IOException e) {
// e.printStackTrace(); // e.printStackTrace();
// } // }
producerTemplate.sendBodyAndHeaders("direct:transferFile", questionFileFound.get().getData(), headers); producerTemplate.sendBodyAndHeaders("direct:transferFile", fileConverter.convert(questionFileFound.get().getData(), questionFileFound.get().getName()), headers);
} }
frequentQuestion.setStatus(FrequentQuestionStatusEnum.UPLOADED); frequentQuestion.setStatus(FrequentQuestionStatusEnum.UPLOADED);
......
package com.bytesw.bytebot.service;
public interface FileConverter {
byte[] convert(byte[] inputFile, String fileName);
String getFileName(String fileName);
}
...@@ -14,7 +14,6 @@ import com.bytesw.bytebot.repository.QuestionFileRepository; ...@@ -14,7 +14,6 @@ import com.bytesw.bytebot.repository.QuestionFileRepository;
import com.bytesw.bytebot.utils.Utilities; import com.bytesw.bytebot.utils.Utilities;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
......
package com.bytesw.bytebot.service;
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Iterator;
@Component("xlsToCsvFileConverter")
public class XlsToCsvFileConverter implements FileConverter {
private static final String FILE_EXTENSION = ".csv";
@Value("${application.service.file-converter.xls-to-csv.separator:$$}")
private String separator;
@Override
public byte[] convert(byte[] inputFile, String fileName) {
StringBuffer data = new StringBuffer();
InputStream fis = null;
Workbook workbook = null;
try {
//FileOutputStream fos = new FileOutputStream();
// Get the workbook object for XLSX file
fis = new ByteArrayInputStream(inputFile);
String ext = FilenameUtils.getExtension(fileName);
if (ext.equalsIgnoreCase("xlsx")) {
workbook = new XSSFWorkbook(fis);
} else if (ext.equalsIgnoreCase("xls")) {
workbook = new HSSFWorkbook(fis);
}
// Get first sheet from the workbook
int numberOfSheets = workbook.getNumberOfSheets();
Row row;
Cell cell;
// Iterate through each rows from first sheet
for (int i = 0; i < numberOfSheets; i++) {
Sheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
row = rowIterator.next();
// For each row, iterate through each columns
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
cell = cellIterator.next();
switch (cell.getCellType()) {
case BOOLEAN:
data.append(cell.getBooleanCellValue() + separator);
break;
case NUMERIC:
data.append(cell.getNumericCellValue() + separator);
break;
case STRING:
data.append(cell.getStringCellValue() + separator);
break;
case BLANK:
data.append("" + separator);
break;
default:
data.append(cell + separator);
}
}
data.setLength(data.length() - separator.length());
data.append('\n'); // appending new line after each row
}
}
return data.toString().getBytes();
//fos.write(data.toString().getBytes());
//fos.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (workbook != null) {
try {
workbook.close();
} catch (Exception e) {}
}
if (fis != null) {
try {
fis.close();
} catch (Exception e) {}
}
}
return new byte[]{};
}
@Override
public String getFileName(String fileName) {
int position = fileName.lastIndexOf(".");
String fileNameWithoutExtension = fileName.substring(0, position);
return fileNameWithoutExtension + FILE_EXTENSION;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment