diff --git a/pom.xml b/pom.xml index cee55ff..b693c34 100644 --- a/pom.xml +++ b/pom.xml @@ -128,6 +128,11 @@ signature-widget 3.1.0 + + org.apache.poi + poi-ooxml + 5.2.0 + diff --git a/src/main/java/mx/gob/jumapacelaya/Application.java b/src/main/java/mx/gob/jumapacelaya/Application.java index 266c8f0..185a08f 100644 --- a/src/main/java/mx/gob/jumapacelaya/Application.java +++ b/src/main/java/mx/gob/jumapacelaya/Application.java @@ -2,6 +2,7 @@ package mx.gob.jumapacelaya; import com.vaadin.flow.component.page.AppShellConfigurator; import com.vaadin.flow.theme.Theme; +import mx.gob.jumapacelaya.services.DatabaseService; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -18,6 +19,11 @@ public class Application implements AppShellConfigurator { public static void main(String[] args) { SpringApplication.run(Application.class, args); + + DatabaseService databaseService = new DatabaseService(); + String archivo = "C:/Users/mramirezg/Documents/MRAMIREZG/Libro1 PRUEBA.xlsx"; + + databaseService.insertarDesdeExcel(archivo); } } diff --git a/src/main/java/mx/gob/jumapacelaya/services/DatabaseService.java b/src/main/java/mx/gob/jumapacelaya/services/DatabaseService.java index c9b8536..fcd47a2 100644 --- a/src/main/java/mx/gob/jumapacelaya/services/DatabaseService.java +++ b/src/main/java/mx/gob/jumapacelaya/services/DatabaseService.java @@ -1,8 +1,12 @@ package mx.gob.jumapacelaya.services; import com.fasterxml.jackson.databind.jsontype.impl.StdTypeResolverBuilder; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import javax.swing.plaf.PanelUI; +import java.io.FileInputStream; +import java.io.IOException; import java.sql.*; import java.time.LocalDate; import java.util.ArrayList; @@ -28,9 +32,9 @@ public class DatabaseService { Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query)) { - while (resultSet.next()) { - tiposDeMantenimientos.add(resultSet.getString("TIPOMANTID")); - } + while (resultSet.next()) { + tiposDeMantenimientos.add(resultSet.getString("TIPOMANTID")); + } } catch (SQLException e) { e.printStackTrace(); } @@ -38,12 +42,12 @@ public class DatabaseService { } // Método para obtener las nomenclaturas - public String getNomenclatura(String tipoMantenimiento ) { + public String getNomenclatura(String tipoMantenimiento) { String nomenclatura = ""; String query = "SELECT nomenclatura FROM TIPOMANT where TIPOMANTID = ?"; try (Connection connection = getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(query)) { + PreparedStatement preparedStatement = connection.prepareStatement(query)) { preparedStatement.setString(1, tipoMantenimiento); ResultSet resultSet = preparedStatement.executeQuery(); @@ -84,8 +88,8 @@ public class DatabaseService { String query = "select DEPARTAMENTOID from DEPARTAMENTOSFINAN"; try (Connection connection = getConnection(); - Statement statement = connection.createStatement(); - ResultSet resultSet = statement.executeQuery(query)) { + Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery(query)) { while (resultSet.next()) { departamentos.add(resultSet.getString("DEPARTAMENTOID")); @@ -97,15 +101,14 @@ public class DatabaseService { } - /* -------------- Metodo para obtener los tipos de hardware ---------------- */ public List getTiposHardware() { List tiposHardware = new ArrayList<>(); String query = "select TIPOHARDWAREID from TIPOSHARDWARE"; try (Connection connection = getConnection(); - Statement statement = connection.createStatement(); - ResultSet resultSet = statement.executeQuery(query)) { + Statement statement = connection.createStatement(); + ResultSet resultSet = statement.executeQuery(query)) { while (resultSet.next()) { tiposHardware.add(resultSet.getString("TIPOHARDWAREID")); @@ -117,14 +120,13 @@ public class DatabaseService { } - /* -------------- Obtener los nombres completos de los usuarios ------------- */ public String getNombreUsuario(String usuarioId) { String nombreUsuario = ""; String query = "SELECT nombre FROM USUARIOSFINAN WHERE usuarioid = ?"; - try(Connection connection = getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(query)) { + try (Connection connection = getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(query)) { preparedStatement.setString(1, usuarioId); ResultSet resultSet = preparedStatement.executeQuery(); @@ -139,7 +141,6 @@ public class DatabaseService { } - /* -------------- Metodos para insetar en la BD ---------------- */ // INSERTAR EN TABLA: MANTENIMINETOS public int insertarMantenimiento(LocalDate fecha, String tipoMantId, String departamentoId, String empleadoId, String formaMant) { @@ -147,7 +148,7 @@ public class DatabaseService { int nuevoId = -1; try (Connection connection = getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)) { + PreparedStatement preparedStatement = connection.prepareStatement(query, Statement.RETURN_GENERATED_KEYS)) { preparedStatement.setDate(1, Date.valueOf(fecha)); preparedStatement.setString(2, tipoMantId); @@ -175,7 +176,7 @@ public class DatabaseService { boolean isInserted = false; try (Connection connection = getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(query)) { + PreparedStatement preparedStatement = connection.prepareStatement(query)) { preparedStatement.setString(1, tipoHardwareId); preparedStatement.setInt(2, numSerie); @@ -214,7 +215,7 @@ public class DatabaseService { String query = "INSERT INTO ACTUALIZACIONESSEG (actualizacion, otrasactualizaciones, mantenimientoid) VALUES (?, ?, ?)"; try (Connection connection = getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(query)) { + PreparedStatement preparedStatement = connection.prepareStatement(query)) { preparedStatement.setString(1, descripcion); preparedStatement.setString(2, otras); @@ -227,4 +228,87 @@ public class DatabaseService { return false; } } + + + // 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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + + try (Connection connection = getConnection(); + FileInputStream file = new FileInputStream(rutaArchivoExcel); + Workbook workbook = new XSSFWorkbook(file)) { + + Sheet sheet = workbook.getSheetAt(0); // Primera hoja del archivo Excel + + 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(); + } + } + + System.out.println("Datos insertados desde Excel con éxito en MTTOPROGRAMADOS."); + } catch (IOException e) { + System.err.println("Error al leer el archivo Excel: " + e.getMessage()); + } catch (SQLException e) { + System.err.println("Error al insertar datos en la BD: " + e.getMessage()); + } + } + + // Método auxiliar para obtener un valor booleano 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; + } + } }