[EDIT] SE MODIFICO OPTIMIZARON LAS FUNCIONES DE GENERACION DE EXCEL, AHORA USAN HILOS Y ASYNCRONIA

parent a251c77e
......@@ -185,6 +185,11 @@
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.20.0</version>
</dependency>
</dependencies>
<build>
<finalName>trismegisto-services</finalName>
......
<?xml version="1.0"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
<deployment>
<exclusions>
<module name="org.apache.commons.logging" />
<module name="org.apache.log4j" />
<module name="org.jboss.logging" />
<module name="org.jboss.logging.jul-to-slf4j-stub" />
<module name="org.jboss.logmanager" />
<module name="org.jboss.logmanager.log4j" />
<module name="org.slf4j" />
<module name="org.slf4j.impl" />
</exclusions>
</deployment>
</jboss-deployment-structure>
\ No newline at end of file
......@@ -2,8 +2,10 @@ package web.multitask.trismegistoservices;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableAsync
public class AppApplication {
public static void main(String[] args) {
......
package web.multitask.trismegistoservices.api;
import kotlin.ParameterName;
import lombok.AllArgsConstructor;
import org.json.JSONObject;
import org.springframework.http.MediaType;
......@@ -9,10 +9,7 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import web.multitask.trismegistoservices.model.DriveRequest;
import web.multitask.trismegistoservices.services.DriveService;
import javax.annotation.Nullable;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@RestController
@CrossOrigin(origins = "*")
......
package web.multitask.trismegistoservices.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@Configuration
public class ExecutorConfig {
@Bean
public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(100);
executor.setMaxPoolSize(100);
executor.setQueueCapacity(1000);
executor.setThreadNamePrefix("ts-executor-");
executor.initialize();
return executor;
}
}
\ No newline at end of file
......@@ -30,11 +30,10 @@ public class GoogleConfig {
String APPLICATION_NAME = "FullService Application";
JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY,
return new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY,
new HttpCredentialsAdapter(getCredentials(HTTP_TRANSPORT)))
.setApplicationName(APPLICATION_NAME)
.build();
return service;
} catch (GeneralSecurityException | IOException e) {
System.out.println("Error: " + e);
return null;
......@@ -45,7 +44,6 @@ public class GoogleConfig {
throws IOException {
InputStream in = GoogleConfig.class.getResourceAsStream(CREDENTIALS_FOLDER_PATH+CREDENTIALS_FILE_PATH);
if (in == null) throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FOLDER_PATH+CREDENTIALS_FILE_PATH);
GoogleCredentials clientSecrets = GoogleCredentials.fromStream(in).createScoped(SCOPES);
return clientSecrets;
return GoogleCredentials.fromStream(in).createScoped(SCOPES);
}
}
}
\ No newline at end of file
......@@ -2,11 +2,11 @@ package web.multitask.trismegistoservices.services;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
......@@ -18,10 +18,14 @@ import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class ExcelService {
public class ExcelService {
List<String> stylesSaved = new ArrayList<>();
List<XSSFCellStyle> styleObjectsSaved = new ArrayList<>();
public byte[] generateExcel(JSONObject json) {
ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
......@@ -37,62 +41,69 @@ public class ExcelService {
}
}
private void createSheet(XSSFWorkbook workbook, JSONObject json) {
@Async
public void createSheet(XSSFWorkbook workbook, JSONObject json) {
styleObjectsSaved.clear();
stylesSaved.clear();
List<CompletableFuture<Boolean>> results = new ArrayList<>();
if (json.optJSONArray("sheetArray", new JSONArray()).isEmpty()) {
XSSFSheet sheet = workbook.createSheet(json.optString("sheet_name", "no_name"));
buildSheet(workbook, sheet, json);
}else{
JSONArray sheetArray = json.optJSONArray("sheetArray", new JSONArray());
for(int i = 0; i < sheetArray.length(); i++){
JSONObject sheetObject = sheetArray.optJSONObject(i);
XSSFSheet sheet = workbook.createSheet(sheetObject.optString("sheet_name", "no_name"));
buildSheet(workbook, sheet, sheetObject);
}
sheetArray.toList().stream().parallel().forEach(sheetObject -> {
HashMap<String, Object> sheetMap = (HashMap<String, Object>) sheetObject;
JSONObject jsonObject = new JSONObject(sheetMap);
XSSFSheet sheet = workbook.createSheet(jsonObject.optString("sheet_name", "no_name"));
JSONArray identifiers = jsonObject.getJSONArray("identifiers");
for (int j = 0; j < identifiers.length(); j++) {
sheet.setColumnWidth(j, 16* 512);
}
CompletableFuture<Boolean> result = buildSheet(workbook, sheet, jsonObject);
results.add(result);
});
}
}
private void buildSheet(XSSFWorkbook workbook, XSSFSheet sheet, JSONObject json) {
@Async
public CompletableFuture<Boolean> buildSheet(XSSFWorkbook workbook, XSSFSheet sheet, JSONObject json) {
final XSSFCellStyle defaultStyle = createCellStyle(workbook, new JSONObject());
XSSFRow row;
XSSFCell cell;
XSSFCellStyle cellStyle;
JSONArray identifiers = json.optJSONArray("identifiers", new JSONArray());
JSONArray data = json.optJSONArray("data", new JSONArray());
JSONArray styles = json.optJSONArray("styles", new JSONArray());
fillStylesArray(styles,workbook,stylesSaved,styleObjectsSaved);
for (int i = 0; i < data.length(); i++) {
XSSFRow row = sheet.createRow(i);
row = sheet.createRow(i);
JSONObject row_data = data.optJSONObject(i);
for (int j = 0; j < identifiers.length(); j++) {
XSSFCell cell = row.createCell(j);
cell = row.createCell(j);
JSONArray styleArray = styles.optJSONArray(i, new JSONArray());
JSONObject style = styleArray.optJSONObject(j, new JSONObject());
if(style.isEmpty()){
cell.setCellStyle(defaultStyle);
}else{
XSSFCellStyle cellStyle = createCellStyle(workbook,style);
cell.setCellStyle(cellStyle);
}
if(row_data.get(identifiers.optString(j, "")) instanceof String){
cell.setCellValue(row_data.optString(identifiers.optString(j, ""), ""));
cell.setCellType(org.apache.poi.ss.usermodel.CellType.STRING);
}else if(row_data.get(identifiers.optString(j, "")) instanceof Integer){
cell.setCellValue(row_data.optInt(identifiers.optString(j, ""), 0));
cell.setCellType(org.apache.poi.ss.usermodel.CellType.NUMERIC);
}else if(row_data.get(identifiers.optString(j, "")) instanceof Double){
cell.setCellValue(row_data.optDouble(identifiers.optString(j, ""), 0.0));
cell.setCellType(org.apache.poi.ss.usermodel.CellType.NUMERIC);
}else if(row_data.get(identifiers.optString(j, "")) instanceof Boolean){
cell.setCellValue(row_data.optBoolean(identifiers.optString(j, ""), false));
cell.setCellType(org.apache.poi.ss.usermodel.CellType.BOOLEAN);
}else{
cell.setCellValue(row_data.optString(identifiers.optString(j, ""), ""));
cell.setCellType(org.apache.poi.ss.usermodel.CellType.STRING);
if(!stylesSaved.contains(style.toString())){
stylesSaved.add(style.toString());
cellStyle = createCellStyle(workbook,style);
cell.setCellStyle(cellStyle);
styleObjectsSaved.add(cellStyle);
}else{
cell.setCellStyle(styleObjectsSaved.get(stylesSaved.indexOf(style.toString())));
}
}
cell.setCellValue(row_data.optString(identifiers.optString(j, "")));
}
}
for (int i = 0; i < identifiers.length(); i++) {
sheet.autoSizeColumn(i, true);
}
return CompletableFuture.completedFuture(true);
}
private XSSFCellStyle createCellStyle(XSSFWorkbook workbook, JSONObject styleJson) {
......@@ -105,24 +116,20 @@ public class ExcelService {
XSSFCellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
style.setAlignment( align.equals("center") ? HorizontalAlignment.CENTER : align.equals("left") ? HorizontalAlignment.LEFT : HorizontalAlignment.RIGHT);
style.setWrapText(true);
style.setWrapText(false);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setBorderBottom(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
if (border) {
style.setBorderBottom(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderColor(BorderSide.BOTTOM, getXSSFColor("#333333"));
style.setBorderColor(BorderSide.TOP, getXSSFColor("#333333"));
style.setBorderColor(BorderSide.LEFT, getXSSFColor("#333333"));
style.setBorderColor(BorderSide.RIGHT, getXSSFColor("#333333"));
}else{
style.setBorderBottom(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderColor(BorderSide.BOTTOM, getXSSFColor("#aaaaaa"));
style.setBorderColor(BorderSide.TOP, getXSSFColor("#aaaaaa"));
style.setBorderColor(BorderSide.LEFT, getXSSFColor("#aaaaaa"));
......@@ -162,4 +169,18 @@ public class ExcelService {
}
};
}
public void fillStylesArray(JSONArray styles, XSSFWorkbook workbook, List<String> stylesSaved, List<XSSFCellStyle> styleObjectsSaved) {
for (int i = 0; i < styles.length(); i++) {
JSONArray styleArray = styles.optJSONArray(i, new JSONArray());
for (int j = 0; j < styleArray.length(); j++) {
JSONObject style = styleArray.optJSONObject(j, new JSONObject());
if(!stylesSaved.contains(style.toString())){
stylesSaved.add(style.toString());
XSSFCellStyle cellStyle = createCellStyle(workbook,style);
styleObjectsSaved.add(cellStyle);
}
}
}
}
}
\ No newline at end of file
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