From 0ba774f867a225e1ebd7fd7c66aed830cfabeaad Mon Sep 17 00:00:00 2001 From: Nico <> Date: Fri, 27 Dec 2024 10:11:21 -0300 Subject: [PATCH] cambios nico comments --- .../lacnic/elections/utils/ExcelUtils.java | 318 +++++------------- 1 file changed, 82 insertions(+), 236 deletions(-) diff --git a/elections-ejb/src/main/java/net/lacnic/elections/utils/ExcelUtils.java b/elections-ejb/src/main/java/net/lacnic/elections/utils/ExcelUtils.java index dbbcbb3..a9396b4 100644 --- a/elections-ejb/src/main/java/net/lacnic/elections/utils/ExcelUtils.java +++ b/elections-ejb/src/main/java/net/lacnic/elections/utils/ExcelUtils.java @@ -2,7 +2,6 @@ import java.io.File; import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; @@ -15,283 +14,130 @@ import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import net.lacnic.elections.domain.UserVoter; import net.lacnic.elections.exception.CensusValidationException; -/** - * Util class for Excel operations - * - */ public class ExcelUtils { private static final Logger appLogger = LogManager.getLogger("ejbAppLogger"); - private static final String TEMP_DIR = "jboss.server.temp.dir"; private static final String CONTENT_TYPE_XLS = "application/vnd.ms-excel"; private static final String CONTENT_TYPE_XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; - /** - * Processes the census in the Excel file and returns it as a list of UserVoter - * - * @param contentType Excel type (xls or xlsx) - * @param census the file - * @return the UserVoter list - * @throws CensusValidationException - */ - private ExcelUtils() { } public static List processCensusExcel(String contentType, byte[] census) throws CensusValidationException { - List userVotersList = new ArrayList<>(); - String filePath = System.getProperty(TEMP_DIR).concat("/padron" + System.currentTimeMillis()); - File file = FilesUtils.convertBytesArrayToFile(census, filePath); - FileInputStream fis = null; - Workbook workbook = null; - - try { - fis = new FileInputStream(file); - if (contentType.equals(CONTENT_TYPE_XLS)) { - workbook = new HSSFWorkbook(fis); - } else if (contentType.equals(CONTENT_TYPE_XLSX)) { - workbook = new XSSFWorkbook(fis); - } else { - throw new CensusValidationException("censusManagementUploadUnknownFileType", null, null); - } + File file = FilesUtils.convertBytesArrayToFile(census, System.getProperty(TEMP_DIR).concat("/padron" + System.currentTimeMillis())); + try (FileInputStream fis = new FileInputStream(file); Workbook workbook = getWorkbook(contentType, fis)) { - int variablesAmount = 0; - int languageIndex = -1; - int nameIndex = -1; - int mailIndex = -1; - int voteAmountIndex = -1; - int countryIndex = -1; - int orgIdIndex = -1; Sheet sheet = workbook.getSheetAt(0); + Iterator rowIterator = sheet.iterator(); - Row firstRow = sheet.getRow(0); - // check that all required columns exist - if (validateRequiredColumns(firstRow)) { - // find column index for each field - while (variablesAmount < firstRow.getLastCellNum() && !firstRow.getCell(variablesAmount).getStringCellValue().equals("")) { - String value = firstRow.getCell(variablesAmount).getStringCellValue(); - if (value.equalsIgnoreCase(("idioma"))) { - languageIndex = variablesAmount; - } else if (value.equalsIgnoreCase(("nombre"))) { - nameIndex = variablesAmount; - } else if (value.equalsIgnoreCase(("mail"))) { - mailIndex = variablesAmount; - } else if (value.equalsIgnoreCase(("orgID"))) { - orgIdIndex = variablesAmount; - } else if (value.equalsIgnoreCase(("pais"))) { - countryIndex = variablesAmount; - } else if (value.equalsIgnoreCase(("cantVotos"))) { - voteAmountIndex = variablesAmount; - } - variablesAmount++; - } - - int i = 0; - CountryUtils countryUtils = new CountryUtils(); - // process each row - Iterator rowIterator = sheet.iterator(); - while (rowIterator.hasNext()) { - Row row = rowIterator.next(); - // first row is titles - if (i > 0) { - // check that required fields are not null - if (validateRequiredFields(row, languageIndex, nameIndex, mailIndex, voteAmountIndex)) { - UserVoter userVoter = new UserVoter(); - - // language - String language = row.getCell(languageIndex).getStringCellValue(); - if (!language.matches("SP|EN|PT")) { - throw new CensusValidationException("censusManagementUploadWrongLanguage", i, language); - } - userVoter.setLanguage(language); - - // name - userVoter.setName(row.getCell(nameIndex).getStringCellValue()); - - // mail - String mail = row.getCell(mailIndex).getStringCellValue(); - - if (!isValid(mail)) { - throw new CensusValidationException("censusManagementUploadWrongEmail", i, mail); - } - // Yes, it is possible that the same email - // participates many times in an election - - userVoter.setMail(mail); - - // votes - try { - int votes = (int) row.getCell(voteAmountIndex).getNumericCellValue(); - if (votes > 0) - userVoter.setVoteAmount(votes); - else - throw new CensusValidationException("censusManagementUploadWrongVoteAmount", i, null); - } catch (Exception e) { - throw new CensusValidationException("censusManagementUploadWrongVoteAmount", i, null); - } - - // org id - if (orgIdIndex != -1 && row.getCell(orgIdIndex) != null) - userVoter.setOrgID(row.getCell(orgIdIndex).getStringCellValue()); - - // country - if (countryIndex != -1 && row.getCell(countryIndex) != null) { - String country = row.getCell(countryIndex).getStringCellValue(); - if (!country.isEmpty() && !countryUtils.getIdsList().contains(country)) { - throw new CensusValidationException("censusManagementUploadWrongCountry", i, country); - } - userVoter.setCountry(country); - } + if (!rowIterator.hasNext()) { + throw new CensusValidationException("censusManagementUploadMissingColumns", null, null); + } - userVotersList.add(userVoter); - } else { - throw new CensusValidationException("censusManagementUploadNullRequiredFields", i, null); - } - } + Row headerRow = rowIterator.next(); + int[] columnIndices = findColumnIndices(headerRow); + List userVoters = new ArrayList<>(); + int rowIndex = 1; - i++; - } - } else { - throw new CensusValidationException("censusManagementUploadMissingColumns", null, null); + while (rowIterator.hasNext()) { + Row row = rowIterator.next(); + userVoters.add(parseRow(row, columnIndices, rowIndex++)); } - return userVotersList; - } catch (CensusValidationException cve) { - appLogger.error(cve); - throw cve; - } catch (IOException ioe) { - appLogger.error(ioe); + return userVoters; + } catch (IOException e) { + appLogger.error(e); throw new CensusValidationException("censusManagementUploadFileError", null, null); - } finally { - if (workbook != null) { - try { - workbook.close(); - } catch (IOException ioe) { - appLogger.error(ioe); - } - } - if (fis != null) { - try { - fis.close(); - } catch (IOException ioe) { - appLogger.error(ioe); - } - } } } - private static boolean isValid(String mail) { - return mail.contains("@") && mail.contains("."); + private static Workbook getWorkbook(String contentType, FileInputStream fis) throws IOException, CensusValidationException { + switch (contentType) { + case CONTENT_TYPE_XLS: + return new HSSFWorkbook(fis); + case CONTENT_TYPE_XLSX: + return new XSSFWorkbook(fis); + default: + throw new CensusValidationException("censusManagementUploadUnknownFileType", null, null); + } } - /** - * Validate that required columns exist - * - * @param firstRow titles row - * @return true if valid, false otherwise - */ - private static boolean validateRequiredColumns(Row firstRow) { - int variablesAmount = 0; - int languageIndex = -1; - int nameIndex = -1; - int mailIndex = -1; - int voteAmountIndex = -1; + private static int[] findColumnIndices(Row headerRow) throws CensusValidationException { + String[] requiredColumns = { "idioma", "nombre", "mail", "cantVotos", "pais", "orgID" }; + int[] indices = new int[requiredColumns.length]; - while (variablesAmount < firstRow.getLastCellNum() && !firstRow.getCell(variablesAmount).getStringCellValue().equals("")) { - String value = firstRow.getCell(variablesAmount).getStringCellValue(); - if (value.equalsIgnoreCase("idioma")) { - languageIndex = variablesAmount; - } else if (value.equalsIgnoreCase("nombre")) { - nameIndex = variablesAmount; - } else if (value.equalsIgnoreCase("mail")) { - mailIndex = variablesAmount; - } else if (value.equalsIgnoreCase("cantVotos")) { - voteAmountIndex = variablesAmount; - } - variablesAmount++; + for (int i = 0; i < requiredColumns.length; i++) { + indices[i] = findColumnIndex(headerRow, requiredColumns[i]); } - return !(languageIndex == -1 || nameIndex == -1 || mailIndex == -1 || voteAmountIndex == -1); + return indices; } - /** - * Validate that required fields are not null in given row - * - * @param row the row to check - * @param languageIndex the language column index - * @param nameIndex the name column index - * @param mailIndex the mail column index - * @param voteAmountIndex the voteAmount column index - * @return true if valid, false otherwise - */ - private static boolean validateRequiredFields(Row row, int languageIndex, int nameIndex, int mailIndex, int voteAmountIndex) { - return !(row.getCell(languageIndex) == null || row.getCell(nameIndex) == null || row.getCell(mailIndex) == null || row.getCell(voteAmountIndex) == null); + private static int findColumnIndex(Row row, String columnName) throws CensusValidationException { + for (int i = 0; i < row.getLastCellNum(); i++) { + Cell cell = row.getCell(i); + if (cell != null && cell.getStringCellValue().equalsIgnoreCase(columnName)) { + return i; + } + } + throw new CensusValidationException("censusManagementUploadMissingColumns", null, columnName); } - /** - * Export the list of voters to a xlsx Excel file - * - * @param userVoters the voters list - * @param fileName the file name - * @return the File object - */ - public static File exportToExcel(List userVoters, String fileName) { - File file = new File(System.getProperty(TEMP_DIR).concat(fileName)); + private static UserVoter parseRow(Row row, int[] columnIndices, int rowIndex) throws CensusValidationException { + UserVoter userVoter = new UserVoter(); - try (XSSFWorkbook workbook = new XSSFWorkbook()) { - XSSFSheet sheet = workbook.createSheet("Padron_Electoral"); + userVoter.setLanguage(validateLanguage(getCellValue(row, columnIndices[0]), rowIndex)); + userVoter.setName(getCellValue(row, columnIndices[1])); + userVoter.setMail(validateEmail(getCellValue(row, columnIndices[2]), rowIndex)); + userVoter.setVoteAmount(validateVoteAmount(row.getCell(columnIndices[3]), rowIndex)); + userVoter.setCountry(getOptionalCellValue(row, columnIndices[4])); + userVoter.setOrgID(getOptionalCellValue(row, columnIndices[5])); - int rowIndex = 0; - int columnIndex = 0; - Row row = sheet.createRow(rowIndex++); - Cell cell = row.createCell(columnIndex++); - cell.setCellValue("IDIOMA"); - cell = row.createCell(columnIndex++); - cell.setCellValue("NOMBRE"); - cell = row.createCell(columnIndex++); - cell.setCellValue("MAIL"); - cell = row.createCell(columnIndex++); - cell.setCellValue("CANTVOTOS"); - cell = row.createCell(columnIndex++); - cell.setCellValue("PAIS"); - cell = row.createCell(columnIndex++); - cell.setCellValue("ORGID"); + return userVoter; + } - for (UserVoter userVoter : userVoters) { - columnIndex = 0; - row = sheet.createRow(rowIndex++); - cell = row.createCell(columnIndex++); - cell.setCellValue(userVoter.getLanguage()); - cell = row.createCell(columnIndex++); - cell.setCellValue(userVoter.getName()); - cell = row.createCell(columnIndex++); - cell.setCellValue(userVoter.getMail()); - cell = row.createCell(columnIndex++); - cell.setCellValue(userVoter.getVoteAmount()); - cell = row.createCell(columnIndex++); - cell.setCellValue(userVoter.getCountry()); - cell = row.createCell(columnIndex++); - cell.setCellValue(userVoter.getOrgID()); - } + private static String getCellValue(Row row, int columnIndex) throws CensusValidationException { + Cell cell = row.getCell(columnIndex); + if (cell == null || cell.getStringCellValue().isEmpty()) { + throw new CensusValidationException("censusManagementUploadNullRequiredFields", null, null); + } + return cell.getStringCellValue(); + } - for (int i = 0; i < sheet.getRow(0).getLastCellNum(); i++) { - sheet.autoSizeColumn(i); - } + private static String getOptionalCellValue(Row row, int columnIndex) { + Cell cell = row.getCell(columnIndex); + return (cell != null) ? cell.getStringCellValue() : null; + } + + private static String validateLanguage(String language, int rowIndex) throws CensusValidationException { + if (!language.matches("SP|EN|PT")) { + throw new CensusValidationException("censusManagementUploadWrongLanguage", rowIndex, language); + } + return language; + } - workbook.write(new FileOutputStream(file)); - workbook.close(); - return file; - } catch (IOException ioe) { - appLogger.error(ioe); + private static String validateEmail(String email, int rowIndex) throws CensusValidationException { + if (!email.contains("@") || !email.contains(".")) { + throw new CensusValidationException("censusManagementUploadWrongEmail", rowIndex, email); } - return null; + return email; } + private static int validateVoteAmount(Cell cell, int rowIndex) throws CensusValidationException { + try { + int votes = (int) cell.getNumericCellValue(); + if (votes <= 0) { + throw new CensusValidationException("censusManagementUploadWrongVoteAmount", rowIndex, null); + } + return votes; + } catch (Exception e) { + throw new CensusValidationException("censusManagementUploadWrongVoteAmount", rowIndex, null); + } + } }