diff --git a/src/main/java/mx/gob/jumapacelaya/services/DatabaseService.java b/src/main/java/mx/gob/jumapacelaya/services/DatabaseService.java index fcd47a2..c2a8fc1 100644 --- a/src/main/java/mx/gob/jumapacelaya/services/DatabaseService.java +++ b/src/main/java/mx/gob/jumapacelaya/services/DatabaseService.java @@ -230,11 +230,10 @@ public class DatabaseService { } - // INSERTAR EN LA TABLA: MTTOPROGRAMADOS public void insertarDesdeExcel(String rutaArchivoExcel) { - String query = "INSERT INTO MTTOPROGRAMADOS (MTTOPROGRAMADOID, NOMEQUIPO, DEPARTAMENTO, MONITOR, TECLADO, MOUSE, " + - "REGULADOR, CPU, IMPRESORA, MINIPRINT, LAPTOP, ESCANER, FECHAPROG, TECNICOSMT, ESTADO, FECHAREALIZADO) " + - "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + String query = "INSERT INTO MTTOPROGRAMADOS (NOMEQUIPO, DEPARTAMENTO, MONITOR, TECLADO, MOUSE, " + + "REGULADOR, CPU, IMPRESORA, MINIPRINT, LAPTOP, ESCANER, FECHAPROG, TECNICOSMT, ESTADO) " + + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; try (Connection connection = getConnection(); FileInputStream file = new FileInputStream(rutaArchivoExcel); @@ -245,46 +244,54 @@ public class DatabaseService { for (Row row : sheet) { if (row.getRowNum() == 0) continue; // Saltar la primera fila si contiene encabezados - // Leer cada celda de la fila - int mttoprogramadoId = (int) row.getCell(0).getNumericCellValue(); - String nomequipo = row.getCell(1).getStringCellValue(); - String departamento = row.getCell(2).getStringCellValue(); - boolean monitor = getBooleanCellValue(row.getCell(3)); - boolean teclado = getBooleanCellValue(row.getCell(4)); - boolean mouse = getBooleanCellValue(row.getCell(5)); - boolean regulador = getBooleanCellValue(row.getCell(6)); - boolean cpu = getBooleanCellValue(row.getCell(7)); - boolean impresora = getBooleanCellValue(row.getCell(8)); - boolean miniprint = getBooleanCellValue(row.getCell(9)); - boolean laptop = getBooleanCellValue(row.getCell(10)); - boolean escaner = getBooleanCellValue(row.getCell(11)); - Date fechaprog = Date.valueOf(row.getCell(12).getLocalDateTimeCellValue().toLocalDate()); - String tecnicosmt = row.getCell(13).getStringCellValue(); - String estado = row.getCell(14).getStringCellValue(); - Date fecharealizado = null; - if (row.getCell(15) != null && row.getCell(15).getCellType() != CellType.BLANK) { - fecharealizado = Date.valueOf(row.getCell(15).getLocalDateTimeCellValue().toLocalDate()); - } - - try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { - preparedStatement.setInt(1, mttoprogramadoId); - preparedStatement.setString(2, nomequipo); - preparedStatement.setString(3, departamento); - preparedStatement.setBoolean(4, monitor); - preparedStatement.setBoolean(5, teclado); - preparedStatement.setBoolean(6, mouse); - preparedStatement.setBoolean(7, regulador); - preparedStatement.setBoolean(8, cpu); - preparedStatement.setBoolean(9, impresora); - preparedStatement.setBoolean(10, miniprint); - preparedStatement.setBoolean(11, laptop); - preparedStatement.setBoolean(12, escaner); - preparedStatement.setDate(13, fechaprog); - preparedStatement.setString(14, tecnicosmt); - preparedStatement.setString(15, estado); - preparedStatement.setDate(16, fecharealizado); - - preparedStatement.executeUpdate(); + try { + // Leer cada celda de la fila según el índice correcto + String nomequipo = getStringCellValue(row.getCell(0)); // Columna 1 + String departamento = getStringCellValue(row.getCell(1)); // Columna 2 + boolean monitor = getBooleanCellValue(row.getCell(2)); // Columna 3 + boolean teclado = getBooleanCellValue(row.getCell(3)); // Columna 4 + boolean mouse = getBooleanCellValue(row.getCell(4)); // Columna 5 + boolean regulador = getBooleanCellValue(row.getCell(5)); // Columna 6 + boolean cpu = getBooleanCellValue(row.getCell(6)); // Columna 7 + boolean impresora = getBooleanCellValue(row.getCell(7)); // Columna 8 + boolean miniprint = getBooleanCellValue(row.getCell(8)); // Columna 9 + boolean laptop = getBooleanCellValue(row.getCell(9)); // Columna 10 + boolean escaner = getBooleanCellValue(row.getCell(10)); // Columna 11 + + // Leer la fecha de la columna correcta + Date fechaprog = getDateCellValue(row.getCell(11)); // Columna 12 + String tecnicosmt = getStringCellValue(row.getCell(12)); // Columna 13 + String estado = getStringCellValue(row.getCell(13)); // Columna 14 + Date fecharealizado = getDateCellValue(row.getCell(14)); // Columna 15 + + // Insertar datos en la base de datos + try (PreparedStatement preparedStatement = connection.prepareStatement(query)) { + preparedStatement.setString(1, nomequipo); + preparedStatement.setString(2, departamento); + preparedStatement.setBoolean(3, monitor); + preparedStatement.setBoolean(4, teclado); + preparedStatement.setBoolean(5, mouse); + preparedStatement.setBoolean(6, regulador); + preparedStatement.setBoolean(7, cpu); + preparedStatement.setBoolean(8, impresora); + preparedStatement.setBoolean(9, miniprint); + preparedStatement.setBoolean(10, laptop); + preparedStatement.setBoolean(11, escaner); + + // Manejo de fechas + if (fechaprog != null) { + preparedStatement.setDate(12, new java.sql.Date(fechaprog.getTime())); + } else { + preparedStatement.setNull(12, java.sql.Types.DATE); // Manejo de valor nulo + } + + preparedStatement.setString(13, tecnicosmt); + preparedStatement.setString(14, estado); + + preparedStatement.executeUpdate(); + } + } catch (Exception e) { + System.err.println("Error procesando la fila " + row.getRowNum() + ": " + e.getMessage()); } } @@ -296,19 +303,58 @@ public class DatabaseService { } } - // Método auxiliar para obtener un valor booleano de una celda + // Método auxiliar para obtener un valor de tipo Date de una celda + private Date getDateCellValue(Cell cell) { + if (cell != null) { + switch (cell.getCellType()) { + case NUMERIC: + return new Date(cell.getDateCellValue().getTime()); // Retorna la fecha directamente + case STRING: + try { + return Date.valueOf(LocalDate.parse(cell.getStringCellValue().trim())); // Asume formato ISO + } catch (Exception e) { + System.err.println("No se puede convertir la cadena a fecha: " + cell.getStringCellValue()); + } + break; + case BLANK: + return null; // Devuelve null si la celda está vacía + default: + System.err.println("Tipo de celda inesperado en la fila " + cell.getRowIndex()); + break; + } + } + return null; // Retorna null si no se puede obtener una fecha + } + + // Método auxiliar para obtener un valor de tipo String de una celda + private String getStringCellValue(Cell cell) { + if (cell != null) { + switch (cell.getCellType()) { + case STRING: + return cell.getStringCellValue().trim(); + case NUMERIC: + return String.valueOf((int) cell.getNumericCellValue()); // Convierte el número a cadena + default: + return ""; // Devuelve cadena vacía si la celda no es de tipo STRING o NUMERIC + } + } + return ""; // Devuelve cadena vacía si la celda es nula + } + + // Método auxiliar para obtener un valor de tipo boolean de una celda private boolean getBooleanCellValue(Cell cell) { - if (cell == null) return false; - switch (cell.getCellType()) { - case BOOLEAN: - return cell.getBooleanCellValue(); - case NUMERIC: - return cell.getNumericCellValue() != 0; - case STRING: - String value = cell.getStringCellValue().trim().toLowerCase(); - return value.equals("true") || value.equals("1") || value.equals("sí") || value.equals("yes"); - default: - return false; + if (cell != null) { + switch (cell.getCellType()) { + case NUMERIC: + return cell.getNumericCellValue() != 0; // Considera 0 como false, cualquier otro número como true + case BOOLEAN: + return cell.getBooleanCellValue(); + case STRING: + return Boolean.parseBoolean(cell.getStringCellValue().trim()); // Conversión de string a boolean + default: + return false; // Valor por defecto + } } + return false; // Valor por defecto si la celda es nula } }