|
|
|
|
@ -1,17 +1,28 @@
|
|
|
|
|
package com.service.ingresantes.service;
|
|
|
|
|
|
|
|
|
|
import com.service.ingresantes.entity.*;
|
|
|
|
|
import com.service.ingresantes.repository.*;
|
|
|
|
|
import com.service.ingresantes.entity.Inscripcion;
|
|
|
|
|
import com.service.ingresantes.entity.Modalidad;
|
|
|
|
|
import com.service.ingresantes.entity.Postulante;
|
|
|
|
|
import com.service.ingresantes.entity.Proceso;
|
|
|
|
|
import com.service.ingresantes.entity.ProgramaEstudio;
|
|
|
|
|
import com.service.ingresantes.repository.InscripcionRepository;
|
|
|
|
|
import com.service.ingresantes.repository.ModalidadRepository;
|
|
|
|
|
import com.service.ingresantes.repository.PostulanteRepository;
|
|
|
|
|
import com.service.ingresantes.repository.ProcesoRepository;
|
|
|
|
|
import com.service.ingresantes.repository.ProgramaRepository;
|
|
|
|
|
import jakarta.transaction.Transactional;
|
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
import jakarta.transaction.Transactional;
|
|
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.time.LocalDate;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
import java.time.ZoneId;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
import java.util.TreeMap;
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
|
public class ExcelService {
|
|
|
|
|
@ -34,157 +45,388 @@ public class ExcelService {
|
|
|
|
|
this.inscripcionRepo = inscripcionRepo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- Método auxiliar para leer cualquier celda como String ---
|
|
|
|
|
private String getCellStringValue(Cell cell) {
|
|
|
|
|
if (cell == null) return "";
|
|
|
|
|
switch (cell.getCellType()) {
|
|
|
|
|
case STRING:
|
|
|
|
|
return cell.getStringCellValue().trim();
|
|
|
|
|
case NUMERIC:
|
|
|
|
|
if (DateUtil.isCellDateFormatted(cell)) {
|
|
|
|
|
return cell.getLocalDateTimeCellValue().toLocalDate().toString();
|
|
|
|
|
} else {
|
|
|
|
|
return String.valueOf((long) cell.getNumericCellValue());
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
return switch (cell.getCellType()) {
|
|
|
|
|
case STRING -> cell.getStringCellValue().trim();
|
|
|
|
|
|
|
|
|
|
case NUMERIC -> {
|
|
|
|
|
if (DateUtil.isCellDateFormatted(cell)) {
|
|
|
|
|
yield cell.getLocalDateTimeCellValue().toLocalDate().toString();
|
|
|
|
|
} else {
|
|
|
|
|
double value = cell.getNumericCellValue();
|
|
|
|
|
long longValue = (long) value;
|
|
|
|
|
if (value == longValue) {
|
|
|
|
|
yield String.valueOf(longValue);
|
|
|
|
|
} else {
|
|
|
|
|
yield String.valueOf(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case BOOLEAN -> String.valueOf(cell.getBooleanCellValue());
|
|
|
|
|
|
|
|
|
|
case FORMULA -> {
|
|
|
|
|
try {
|
|
|
|
|
FormulaEvaluator evaluator = cell.getSheet()
|
|
|
|
|
.getWorkbook()
|
|
|
|
|
.getCreationHelper()
|
|
|
|
|
.createFormulaEvaluator();
|
|
|
|
|
|
|
|
|
|
CellValue cellValue = evaluator.evaluate(cell);
|
|
|
|
|
|
|
|
|
|
yield switch (cellValue.getCellType()) {
|
|
|
|
|
case STRING -> cellValue.getStringValue().trim();
|
|
|
|
|
case NUMERIC -> {
|
|
|
|
|
double value = cellValue.getNumberValue();
|
|
|
|
|
long longValue = (long) value;
|
|
|
|
|
if (value == longValue) {
|
|
|
|
|
yield String.valueOf(longValue);
|
|
|
|
|
} else {
|
|
|
|
|
yield String.valueOf(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case BOOLEAN -> String.valueOf(cellValue.getBooleanValue());
|
|
|
|
|
default -> "";
|
|
|
|
|
};
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
yield "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
case BOOLEAN:
|
|
|
|
|
return String.valueOf(cell.getBooleanCellValue());
|
|
|
|
|
case FORMULA:
|
|
|
|
|
return cell.getCellFormula();
|
|
|
|
|
default:
|
|
|
|
|
return "";
|
|
|
|
|
|
|
|
|
|
case BLANK -> "";
|
|
|
|
|
default -> "";
|
|
|
|
|
};
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
public void importarExcel(MultipartFile file) throws Exception {
|
|
|
|
|
InputStream is = file.getInputStream();
|
|
|
|
|
Workbook workbook = WorkbookFactory.create(is);
|
|
|
|
|
Sheet sheet = workbook.getSheetAt(0);
|
|
|
|
|
private LocalDate getCellLocalDate(Cell cell) {
|
|
|
|
|
if (cell == null) return null;
|
|
|
|
|
|
|
|
|
|
DateTimeFormatter fechaHoraFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
try {
|
|
|
|
|
if (cell.getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(cell)) {
|
|
|
|
|
return cell.getDateCellValue()
|
|
|
|
|
.toInstant()
|
|
|
|
|
.atZone(ZoneId.systemDefault())
|
|
|
|
|
.toLocalDate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (Row row : sheet) {
|
|
|
|
|
if (row.getRowNum() == 0) continue; // Saltar encabezado
|
|
|
|
|
String value = getCellStringValue(cell);
|
|
|
|
|
if (value == null || value.isBlank()) return null;
|
|
|
|
|
|
|
|
|
|
String dni = getCellStringValue(row.getCell(0));
|
|
|
|
|
if (dni.isEmpty()) {
|
|
|
|
|
System.out.println("Fila " + row.getRowNum() + ": DNI vacío, se omite");
|
|
|
|
|
continue;
|
|
|
|
|
return LocalDate.parse(value.trim());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private LocalDateTime getCellLocalDateTime(Cell cell) {
|
|
|
|
|
if (cell == null) return null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (cell.getCellType() == CellType.NUMERIC && DateUtil.isCellDateFormatted(cell)) {
|
|
|
|
|
return cell.getDateCellValue()
|
|
|
|
|
.toInstant()
|
|
|
|
|
.atZone(ZoneId.systemDefault())
|
|
|
|
|
.toLocalDateTime();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Postulante postulante = postulanteRepo.findById(dni).orElseGet(() -> {
|
|
|
|
|
String value = getCellStringValue(cell);
|
|
|
|
|
if (value == null || value.isBlank()) return null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
return LocalDateTime.parse(value.trim().replace(" ", "T"));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
try {
|
|
|
|
|
return LocalDate.parse(value.trim()).atStartOfDay();
|
|
|
|
|
} catch (Exception ex) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Integer parseInteger(String value) {
|
|
|
|
|
try {
|
|
|
|
|
if (value == null || value.isBlank()) return null;
|
|
|
|
|
return Integer.parseInt(value.trim());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Long parseLong(String value) {
|
|
|
|
|
try {
|
|
|
|
|
if (value == null || value.isBlank()) return null;
|
|
|
|
|
return Long.parseLong(value.trim());
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void incrementar(Map<String, Integer> mapa, String clave) {
|
|
|
|
|
mapa.put(clave, mapa.getOrDefault(clave, 0) + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
|
public String importarExcel(MultipartFile file) throws Exception {
|
|
|
|
|
int totalFilas = 0;
|
|
|
|
|
int postulantesCreados = 0;
|
|
|
|
|
int inscripcionesGuardadas = 0;
|
|
|
|
|
int filasOmitidas = 0;
|
|
|
|
|
int errores = 0;
|
|
|
|
|
|
|
|
|
|
int dniVacio = 0;
|
|
|
|
|
int procesoVacio = 0;
|
|
|
|
|
int procesoInvalido = 0;
|
|
|
|
|
int procesoNoEncontrado = 0;
|
|
|
|
|
int programaInvalido = 0;
|
|
|
|
|
int programaNoEncontrado = 0;
|
|
|
|
|
int modalidadInvalida = 0;
|
|
|
|
|
int modalidadNoEncontrada = 0;
|
|
|
|
|
int inscripcionDuplicada = 0;
|
|
|
|
|
|
|
|
|
|
Map<String, Integer> omitidasPorProceso = new TreeMap<>();
|
|
|
|
|
Map<String, Integer> omitidasPorPrograma = new TreeMap<>();
|
|
|
|
|
Map<String, Integer> omitidasPorModalidad = new TreeMap<>();
|
|
|
|
|
Map<String, Integer> omitidasPorProcesoModalidad = new TreeMap<>();
|
|
|
|
|
Map<String, Integer> omitidasPorCausa = new TreeMap<>();
|
|
|
|
|
|
|
|
|
|
try (InputStream is = file.getInputStream();
|
|
|
|
|
Workbook workbook = WorkbookFactory.create(is)) {
|
|
|
|
|
|
|
|
|
|
Sheet sheet = workbook.getSheetAt(0);
|
|
|
|
|
|
|
|
|
|
for (Row row : sheet) {
|
|
|
|
|
if (row.getRowNum() == 0) continue;
|
|
|
|
|
totalFilas++;
|
|
|
|
|
|
|
|
|
|
String dni = getCellStringValue(row.getCell(0));
|
|
|
|
|
String procesoStr = getCellStringValue(row.getCell(18));
|
|
|
|
|
String programaStr = getCellStringValue(row.getCell(19));
|
|
|
|
|
String modalidadStr = getCellStringValue(row.getCell(20));
|
|
|
|
|
|
|
|
|
|
String procesoKey = procesoStr.isBlank() ? "SIN_PROCESO" : "Proceso " + procesoStr;
|
|
|
|
|
String programaKey = programaStr.isBlank() ? "SIN_PROGRAMA" : "Programa " + programaStr;
|
|
|
|
|
String modalidadKey = modalidadStr.isBlank() ? "SIN_MODALIDAD" : "Modalidad " + modalidadStr;
|
|
|
|
|
String procesoModalidadKey = procesoKey + " | " + modalidadKey;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Postulante p = new Postulante();
|
|
|
|
|
p.setDni(dni);
|
|
|
|
|
p.setPaterno(getCellStringValue(row.getCell(1)));
|
|
|
|
|
p.setMaterno(getCellStringValue(row.getCell(2)));
|
|
|
|
|
p.setNombres(getCellStringValue(row.getCell(3)));
|
|
|
|
|
p.setSexo(getCellStringValue(row.getCell(4)));
|
|
|
|
|
|
|
|
|
|
String fechaNacStr = getCellStringValue(row.getCell(5));
|
|
|
|
|
if (!fechaNacStr.isEmpty()) {
|
|
|
|
|
try { p.setFechaNacimiento(LocalDate.parse(fechaNacStr)); } catch (Exception e) {}
|
|
|
|
|
if (dni.isBlank()) {
|
|
|
|
|
filasOmitidas++;
|
|
|
|
|
dniVacio++;
|
|
|
|
|
incrementar(omitidasPorProceso, procesoKey);
|
|
|
|
|
incrementar(omitidasPorPrograma, programaKey);
|
|
|
|
|
incrementar(omitidasPorModalidad, modalidadKey);
|
|
|
|
|
incrementar(omitidasPorProcesoModalidad, procesoModalidadKey);
|
|
|
|
|
incrementar(omitidasPorCausa, "DNI vacío");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Postulante postulante = postulanteRepo.findById(dni).orElse(null);
|
|
|
|
|
|
|
|
|
|
if (postulante == null) {
|
|
|
|
|
postulante = new Postulante();
|
|
|
|
|
postulante.setDni(dni);
|
|
|
|
|
postulante.setPaterno(getCellStringValue(row.getCell(1)));
|
|
|
|
|
postulante.setMaterno(getCellStringValue(row.getCell(2)));
|
|
|
|
|
postulante.setNombres(getCellStringValue(row.getCell(3)));
|
|
|
|
|
postulante.setSexo(getCellStringValue(row.getCell(4)));
|
|
|
|
|
|
|
|
|
|
LocalDate fechaNacimiento = getCellLocalDate(row.getCell(5));
|
|
|
|
|
if (fechaNacimiento != null) postulante.setFechaNacimiento(fechaNacimiento);
|
|
|
|
|
|
|
|
|
|
Integer edad = parseInteger(getCellStringValue(row.getCell(6)));
|
|
|
|
|
if (edad != null) postulante.setEdad(edad);
|
|
|
|
|
|
|
|
|
|
postulante.setUbigeoResidencia(getCellStringValue(row.getCell(7)));
|
|
|
|
|
postulante.setDepartamentoResidencia(getCellStringValue(row.getCell(8)));
|
|
|
|
|
postulante.setProvinciaResidencia(getCellStringValue(row.getCell(9)));
|
|
|
|
|
postulante.setDistritoResidencia(getCellStringValue(row.getCell(10)));
|
|
|
|
|
|
|
|
|
|
Integer egreso = parseInteger(getCellStringValue(row.getCell(11)));
|
|
|
|
|
if (egreso != null) postulante.setEgreso(egreso);
|
|
|
|
|
|
|
|
|
|
postulante.setCodModular(getCellStringValue(row.getCell(12)));
|
|
|
|
|
postulante.setUbigeoColegio(getCellStringValue(row.getCell(13)));
|
|
|
|
|
postulante.setDepartamentoColegio(getCellStringValue(row.getCell(14)));
|
|
|
|
|
postulante.setProvinciaColegio(getCellStringValue(row.getCell(15)));
|
|
|
|
|
postulante.setDistritoColegio(getCellStringValue(row.getCell(16)));
|
|
|
|
|
|
|
|
|
|
postulante = postulanteRepo.save(postulante);
|
|
|
|
|
postulantesCreados++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String edadStr = getCellStringValue(row.getCell(6));
|
|
|
|
|
if (!edadStr.isEmpty()) {
|
|
|
|
|
try { p.setEdad(Integer.parseInt(edadStr)); } catch (NumberFormatException ignored) {}
|
|
|
|
|
LocalDateTime fechaInscripcion = getCellLocalDateTime(row.getCell(17));
|
|
|
|
|
if (fechaInscripcion == null) {
|
|
|
|
|
fechaInscripcion = LocalDateTime.now();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p.setUbigeoResidencia(getCellStringValue(row.getCell(7)));
|
|
|
|
|
p.setDepartamentoResidencia(getCellStringValue(row.getCell(8)));
|
|
|
|
|
p.setProvinciaResidencia(getCellStringValue(row.getCell(9)));
|
|
|
|
|
p.setDistritoResidencia(getCellStringValue(row.getCell(10)));
|
|
|
|
|
if (procesoStr.isBlank()) {
|
|
|
|
|
filasOmitidas++;
|
|
|
|
|
procesoVacio++;
|
|
|
|
|
incrementar(omitidasPorProceso, procesoKey);
|
|
|
|
|
incrementar(omitidasPorPrograma, programaKey);
|
|
|
|
|
incrementar(omitidasPorModalidad, modalidadKey);
|
|
|
|
|
incrementar(omitidasPorProcesoModalidad, procesoModalidadKey);
|
|
|
|
|
incrementar(omitidasPorCausa, "Proceso vacío");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String egresoStr = getCellStringValue(row.getCell(11));
|
|
|
|
|
if (!egresoStr.isEmpty()) {
|
|
|
|
|
try { p.setEgreso(Integer.parseInt(egresoStr)); } catch (NumberFormatException ignored) {}
|
|
|
|
|
Long procesoId = parseLong(procesoStr);
|
|
|
|
|
if (procesoId == null) {
|
|
|
|
|
filasOmitidas++;
|
|
|
|
|
procesoInvalido++;
|
|
|
|
|
incrementar(omitidasPorProceso, procesoKey);
|
|
|
|
|
incrementar(omitidasPorPrograma, programaKey);
|
|
|
|
|
incrementar(omitidasPorModalidad, modalidadKey);
|
|
|
|
|
incrementar(omitidasPorProcesoModalidad, procesoModalidadKey);
|
|
|
|
|
incrementar(omitidasPorCausa, "Proceso inválido");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p.setCodModular(getCellStringValue(row.getCell(12)));
|
|
|
|
|
p.setUbigeoColegio(getCellStringValue(row.getCell(13)));
|
|
|
|
|
p.setDepartamentoColegio(getCellStringValue(row.getCell(14)));
|
|
|
|
|
p.setProvinciaColegio(getCellStringValue(row.getCell(15)));
|
|
|
|
|
p.setDistritoColegio(getCellStringValue(row.getCell(16)));
|
|
|
|
|
Optional<Proceso> procesoOpt = procesoRepo.findById(procesoId);
|
|
|
|
|
if (procesoOpt.isEmpty()) {
|
|
|
|
|
filasOmitidas++;
|
|
|
|
|
procesoNoEncontrado++;
|
|
|
|
|
incrementar(omitidasPorProceso, procesoKey);
|
|
|
|
|
incrementar(omitidasPorPrograma, programaKey);
|
|
|
|
|
incrementar(omitidasPorModalidad, modalidadKey);
|
|
|
|
|
incrementar(omitidasPorProcesoModalidad, procesoModalidadKey);
|
|
|
|
|
incrementar(omitidasPorCausa, "Proceso no encontrado");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Proceso proceso = procesoOpt.get();
|
|
|
|
|
|
|
|
|
|
System.out.println("Fila " + row.getRowNum() + ": Postulante creado -> " + dni);
|
|
|
|
|
return postulanteRepo.save(p);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
System.out.println("Fila " + row.getRowNum() + ": Error guardando postulante " + dni + " -> " + e.getMessage());
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
Long programaId = parseLong(programaStr);
|
|
|
|
|
if (programaId == null) {
|
|
|
|
|
filasOmitidas++;
|
|
|
|
|
programaInvalido++;
|
|
|
|
|
incrementar(omitidasPorProceso, procesoKey);
|
|
|
|
|
incrementar(omitidasPorPrograma, programaKey);
|
|
|
|
|
incrementar(omitidasPorModalidad, modalidadKey);
|
|
|
|
|
incrementar(omitidasPorProcesoModalidad, procesoModalidadKey);
|
|
|
|
|
incrementar(omitidasPorCausa, "Programa inválido");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (postulante == null) continue;
|
|
|
|
|
Optional<ProgramaEstudio> programaOpt = programaRepo.findById(programaId);
|
|
|
|
|
if (programaOpt.isEmpty()) {
|
|
|
|
|
filasOmitidas++;
|
|
|
|
|
programaNoEncontrado++;
|
|
|
|
|
incrementar(omitidasPorProceso, procesoKey);
|
|
|
|
|
incrementar(omitidasPorPrograma, programaKey);
|
|
|
|
|
incrementar(omitidasPorModalidad, modalidadKey);
|
|
|
|
|
incrementar(omitidasPorProcesoModalidad, procesoModalidadKey);
|
|
|
|
|
incrementar(omitidasPorCausa, "Programa no encontrado");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
ProgramaEstudio programa = programaOpt.get();
|
|
|
|
|
|
|
|
|
|
Long modalidadId = parseLong(modalidadStr);
|
|
|
|
|
if (modalidadId == null) {
|
|
|
|
|
filasOmitidas++;
|
|
|
|
|
modalidadInvalida++;
|
|
|
|
|
incrementar(omitidasPorProceso, procesoKey);
|
|
|
|
|
incrementar(omitidasPorPrograma, programaKey);
|
|
|
|
|
incrementar(omitidasPorModalidad, modalidadKey);
|
|
|
|
|
incrementar(omitidasPorProcesoModalidad, procesoModalidadKey);
|
|
|
|
|
incrementar(omitidasPorCausa, "Modalidad inválida");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Optional<Modalidad> modalidadOpt = modalidadRepo.findById(modalidadId);
|
|
|
|
|
if (modalidadOpt.isEmpty()) {
|
|
|
|
|
filasOmitidas++;
|
|
|
|
|
modalidadNoEncontrada++;
|
|
|
|
|
incrementar(omitidasPorProceso, procesoKey);
|
|
|
|
|
incrementar(omitidasPorPrograma, programaKey);
|
|
|
|
|
incrementar(omitidasPorModalidad, modalidadKey);
|
|
|
|
|
incrementar(omitidasPorProcesoModalidad, procesoModalidadKey);
|
|
|
|
|
incrementar(omitidasPorCausa, "Modalidad no encontrada");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Modalidad modalidad = modalidadOpt.get();
|
|
|
|
|
|
|
|
|
|
boolean existe = inscripcionRepo.existsByPostulanteAndProceso(postulante, proceso);
|
|
|
|
|
if (existe) {
|
|
|
|
|
filasOmitidas++;
|
|
|
|
|
inscripcionDuplicada++;
|
|
|
|
|
incrementar(omitidasPorProceso, procesoKey);
|
|
|
|
|
incrementar(omitidasPorPrograma, programaKey);
|
|
|
|
|
incrementar(omitidasPorModalidad, modalidadKey);
|
|
|
|
|
incrementar(omitidasPorProcesoModalidad, procesoModalidadKey);
|
|
|
|
|
incrementar(omitidasPorCausa, "Inscripción duplicada");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Inscripcion inscripcion = new Inscripcion();
|
|
|
|
|
inscripcion.setFechaInscripcion(fechaInscripcion);
|
|
|
|
|
inscripcion.setPostulante(postulante);
|
|
|
|
|
inscripcion.setProceso(proceso);
|
|
|
|
|
inscripcion.setPrograma(programa);
|
|
|
|
|
inscripcion.setModalidad(modalidad);
|
|
|
|
|
inscripcionRepo.save(inscripcion);
|
|
|
|
|
inscripcionesGuardadas++;
|
|
|
|
|
|
|
|
|
|
String fechaInsStr = getCellStringValue(row.getCell(17));
|
|
|
|
|
LocalDateTime fechaInscripcion;
|
|
|
|
|
if (!fechaInsStr.isEmpty()) {
|
|
|
|
|
try {
|
|
|
|
|
fechaInscripcion = LocalDateTime.parse(fechaInsStr, fechaHoraFormatter);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
try { fechaInscripcion = LocalDate.parse(fechaInsStr).atStartOfDay(); }
|
|
|
|
|
catch (Exception ex) { fechaInscripcion = LocalDateTime.now(); }
|
|
|
|
|
errores++;
|
|
|
|
|
incrementar(omitidasPorCausa, "Error interno");
|
|
|
|
|
System.out.println("Fila " + (row.getRowNum() + 1) + ": ERROR -> " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
fechaInscripcion = LocalDateTime.now();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String procesoStr = getCellStringValue(row.getCell(18));
|
|
|
|
|
if (procesoStr.isEmpty()) {
|
|
|
|
|
System.out.println("Fila " + row.getRowNum() + ": Proceso vacío, se omite");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Long procesoId;
|
|
|
|
|
try { procesoId = Long.parseLong(procesoStr); }
|
|
|
|
|
catch (NumberFormatException e) {
|
|
|
|
|
System.out.println("Fila " + row.getRowNum() + ": Proceso inválido -> " + procesoStr);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Optional<Proceso> procesoOpt = procesoRepo.findById(procesoId);
|
|
|
|
|
if (procesoOpt.isEmpty()) {
|
|
|
|
|
System.out.println("Fila " + row.getRowNum() + ": Proceso no encontrado -> " + procesoId);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Proceso proceso = procesoOpt.get();
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
sb.append("Importación finalizada.\n");
|
|
|
|
|
sb.append("Total filas: ").append(totalFilas).append("\n");
|
|
|
|
|
sb.append("Postulantes creados: ").append(postulantesCreados).append("\n");
|
|
|
|
|
sb.append("Inscripciones guardadas: ").append(inscripcionesGuardadas).append("\n");
|
|
|
|
|
sb.append("Filas omitidas: ").append(filasOmitidas).append("\n");
|
|
|
|
|
sb.append("Errores: ").append(errores).append("\n\n");
|
|
|
|
|
|
|
|
|
|
String programaStr = getCellStringValue(row.getCell(19));
|
|
|
|
|
Optional<ProgramaEstudio> programaOpt = programaRepo.findById(Long.parseLong(programaStr));
|
|
|
|
|
if (programaOpt.isEmpty()) {
|
|
|
|
|
System.out.println("Fila " + row.getRowNum() + ": Programa no encontrado -> " + programaStr);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
ProgramaEstudio programa = programaOpt.get();
|
|
|
|
|
sb.append("DETALLE POR CAUSA:\n");
|
|
|
|
|
sb.append("- DNI vacío: ").append(dniVacio).append("\n");
|
|
|
|
|
sb.append("- Proceso vacío: ").append(procesoVacio).append("\n");
|
|
|
|
|
sb.append("- Proceso inválido: ").append(procesoInvalido).append("\n");
|
|
|
|
|
sb.append("- Proceso no encontrado: ").append(procesoNoEncontrado).append("\n");
|
|
|
|
|
sb.append("- Programa inválido: ").append(programaInvalido).append("\n");
|
|
|
|
|
sb.append("- Programa no encontrado: ").append(programaNoEncontrado).append("\n");
|
|
|
|
|
sb.append("- Modalidad inválida: ").append(modalidadInvalida).append("\n");
|
|
|
|
|
sb.append("- Modalidad no encontrada: ").append(modalidadNoEncontrada).append("\n");
|
|
|
|
|
sb.append("- Inscripción duplicada: ").append(inscripcionDuplicada).append("\n\n");
|
|
|
|
|
|
|
|
|
|
String modalidadStr = getCellStringValue(row.getCell(20));
|
|
|
|
|
Optional<Modalidad> modalidadOpt = modalidadRepo.findById(Long.parseLong(modalidadStr));
|
|
|
|
|
if (modalidadOpt.isEmpty()) {
|
|
|
|
|
System.out.println("Fila " + row.getRowNum() + ": Modalidad no encontrada -> " + modalidadStr);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
Modalidad modalidad = modalidadOpt.get();
|
|
|
|
|
sb.append("OMITIDAS POR PROCESO:\n");
|
|
|
|
|
for (Map.Entry<String, Integer> entry : omitidasPorProceso.entrySet()) {
|
|
|
|
|
sb.append("- ").append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boolean existe = inscripcionRepo.existsByPostulanteAndProceso(postulante, proceso);
|
|
|
|
|
if (existe) {
|
|
|
|
|
System.out.println("Fila " + row.getRowNum() + ": La inscripción ya existe para el postulante " + dni);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
sb.append("\nOMITIDAS POR PROGRAMA:\n");
|
|
|
|
|
for (Map.Entry<String, Integer> entry : omitidasPorPrograma.entrySet()) {
|
|
|
|
|
sb.append("- ").append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Inscripcion inscripcion = new Inscripcion();
|
|
|
|
|
inscripcion.setFechaInscripcion(fechaInscripcion);
|
|
|
|
|
inscripcion.setPostulante(postulante);
|
|
|
|
|
inscripcion.setProceso(proceso);
|
|
|
|
|
inscripcion.setPrograma(programa);
|
|
|
|
|
inscripcion.setModalidad(modalidad);
|
|
|
|
|
inscripcionRepo.save(inscripcion);
|
|
|
|
|
System.out.println("Fila " + row.getRowNum() + ": Inscripción guardada -> " + dni);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
System.out.println("Fila " + row.getRowNum() + ": Error guardando inscripción -> " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
sb.append("\nOMITIDAS POR MODALIDAD:\n");
|
|
|
|
|
for (Map.Entry<String, Integer> entry : omitidasPorModalidad.entrySet()) {
|
|
|
|
|
sb.append("- ").append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sb.append("\nOMITIDAS POR PROCESO Y MODALIDAD:\n");
|
|
|
|
|
for (Map.Entry<String, Integer> entry : omitidasPorProcesoModalidad.entrySet()) {
|
|
|
|
|
sb.append("- ").append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sb.append("\nRESUMEN DE CAUSAS AGRUPADAS:\n");
|
|
|
|
|
for (Map.Entry<String, Integer> entry : omitidasPorCausa.entrySet()) {
|
|
|
|
|
sb.append("- ").append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
workbook.close();
|
|
|
|
|
is.close();
|
|
|
|
|
return sb.toString();
|
|
|
|
|
}
|
|
|
|
|
}
|