Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package control; import com.itextpdf.text.BadElementException; import com.itextpdf.text.Chunk; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Image; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.text.pdf.draw.DottedLineSeparator; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Calendar; import java.sql.Connection; import java.sql.Date; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Time; import java.sql.Timestamp; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; /** * * @author dns */ public class ModificaPrestamo extends javax.swing.JFrame { private boolean usuario; private Integer id; /** * Creates new form ModificaPrestamo */ public ModificaPrestamo(Integer id, boolean usuario) { initComponents(); this.id = id; String sql = "SELECT * FROM inventario.presta where idprestamo = " + id; System.out.println(sql); conectar conect = new conectar(); Connection con = conect.conexion(); ResultSet rs = conectar.consultar(sql, con); try { while (rs.next()) { String idequipo = rs.getString("idEquipo"); sql = "SELECT * FROM inventario.equipo where idEquipo = '" + idequipo + "'"; System.out.println(sql); ResultSet rs2 = conectar.consultar(sql, con); while (rs2.next()) { Object[] fila = new Object[4]; fila[0] = rs2.getString("idEquipo"); fila[1] = rs2.getString("nomEquipo"); fila[2] = rs2.getString("accesorios"); fila[3] = rs2.getString("descEquipo"); DefaultTableModel modelo = (DefaultTableModel) tabReporte.getModel(); modelo.addRow(fila); } } sql = "SELECT * FROM inventario.prestamo where idprestamo = " + id; ResultSet rs3 = conectar.consultar(sql, con); while (rs3.next()) { Solicitante.setText(rs3.getString("solicitante")); Proyecto.setText(rs3.getString("proyecto")); //Date prestado = rs3.getDate("fprestamo"); Timestamp prestado = rs3.getTimestamp("fprestamo"); Date devuelto = rs3.getDate("fdevolucion"); Timestamp deverdad = rs3.getTimestamp("freal"); System.out.println(prestado); System.out.println(devuelto); System.out.println(deverdad); java.util.Calendar cal = Calendar.getInstance(); cal.setTime(prestado); hora.setText(cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE)); fecha.setText(cal.get(Calendar.DAY_OF_MONTH) + "/" + (cal.get(Calendar.MONTH + 1) - 5) + "/" + cal.get(Calendar.YEAR)); cal.setTime(devuelto); fecha1.setText(cal.get(Calendar.DAY_OF_MONTH) + "/" + (cal.get(Calendar.MONTH + 1) - 5) + "/" + cal.get(Calendar.YEAR)); if (deverdad != null) { cal.setTime(deverdad); fecha2.setText(cal.get(Calendar.DAY_OF_MONTH) + "/" + (cal.get(Calendar.MONTH + 1) - 5) + "/" + cal.get(Calendar.YEAR)); } } con.close(); } catch (SQLException ex) { Logger.getLogger(ModificaPrestamo.class.getName()).log(Level.SEVERE, null, ex); } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jButton3 = new javax.swing.JButton(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); fecha = new javax.swing.JLabel(); fecha1 = new javax.swing.JLabel(); fecha2 = new javax.swing.JLabel(); label1 = new javax.swing.JLabel(); label2 = new javax.swing.JLabel(); Solicitante = new javax.swing.JTextField(); jLabel13 = new javax.swing.JLabel(); Proyecto = new javax.swing.JTextField(); jLabel14 = new javax.swing.JLabel(); jScrollPane2 = new javax.swing.JScrollPane(); tabReporte = new javax.swing.JTable(); hora = new javax.swing.JLabel(); jButton4 = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); clausula1 = new javax.swing.JTextArea(); jScrollPane3 = new javax.swing.JScrollPane(); clausula2 = new javax.swing.JTextArea(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setText("Imprimir"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jButton2.setText("Cancelar"); jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton2ActionPerformed(evt); } }); jButton3.setText("Eliminar"); jButton3.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton3ActionPerformed(evt); } }); jLabel3.setText("Fecha y Hora de Prestamo:"); jLabel4.setText("Fecha Devolucin"); jLabel5.setText("Fecha Real de Entrega:"); fecha.setText("00/00/00"); fecha1.setText("00/00/00"); fecha2.setText("00/00/00"); label1.setText("Clausula 1"); label2.setText("Clausula 2"); Solicitante.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { SolicitanteActionPerformed(evt); } }); jLabel13.setText("Nombre Del Solicitante:"); Proyecto.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { ProyectoActionPerformed(evt); } }); jLabel14.setText("Proyecto en el que trabaja:"); tabReporte.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "id Equipo", "Equipo", "Accesorios", "Descripcion" }) { boolean[] canEdit = new boolean[] { false, false, false, false }; public boolean isCellEditable(int rowIndex, int columnIndex) { return canEdit[columnIndex]; } }); tabReporte.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { tabReporteMouseClicked(evt); } }); jScrollPane2.setViewportView(tabReporte); hora.setText("00:00"); jButton4.setText("Entregar"); jButton4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton4ActionPerformed(evt); } }); clausula1.setColumns(20); clausula1.setRows(5); clausula1.setText( "En caso de que el equipo presente algn desperfecto en su funcionamiento o apariencia el solicitante se har responsable de pagar los costos generados por su reparacin o reposicin."); jScrollPane1.setViewportView(clausula1); clausula2.setColumns(20); clausula2.setRows(5); clausula2.setText( "En caso de que el equipo presente algn desperfecto en su funcionamiento o apariencia el solicitante se har responsable de pagar los costos generados por su reparacin o reposicin."); jScrollPane3.setViewportView(clausula2); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(80, 80, 80) .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(20, 20, 20).addComponent(Solicitante, javax.swing.GroupLayout.PREFERRED_SIZE, 340, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGap(60, 60, 60) .addComponent(jLabel14, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(20, 20, 20).addComponent(Proyecto, javax.swing.GroupLayout.PREFERRED_SIZE, 340, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGap(110, 110, 110) .addComponent(jButton2).addGap(125, 125, 125).addComponent(jButton3) .addGap(119, 119, 119).addComponent(jButton1).addGap(108, 108, 108) .addComponent(jButton4))) .addGap(0, 471, Short.MAX_VALUE)) .addGroup(layout.createSequentialGroup().addContainerGap(68, Short.MAX_VALUE) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 920, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel3).addComponent(jLabel4) .addComponent(jLabel5).addComponent(label1) .addComponent(label2)) .addGap(47, 47, 47) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(fecha).addComponent(fecha1) .addComponent(fecha2).addComponent(hora)) .addGap(0, 0, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout .createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)))))) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(30, 30, 30).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jLabel13) .addComponent(Solicitante, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(10, 10, 10) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel14).addComponent(Proyecto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(fecha) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 440, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3).addComponent(hora)).addGap(36, 36, 36) .addComponent(jLabel4).addGap(36, 36, 36) .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(40, 40, 40).addComponent(label1)) .addGroup(layout.createSequentialGroup().addGap(50, 50, 50) .addComponent(fecha1).addGap(36, 36, 36).addComponent(fecha2))) .addGap(1, 1, 1) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(label2) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButton2).addComponent(jButton3).addComponent(jButton1) .addComponent(jButton4)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed JFileChooser jf = new JFileChooser(); int res = jf.showSaveDialog(this); if (res == JFileChooser.APPROVE_OPTION) { String path = jf.getSelectedFile() + ".pdf"; System.out.println("Archivo " + path); try { if (this.generapdf(path)) { JOptionPane.showMessageDialog(this, "Se ha almacenado el archivo "); } else { JOptionPane.showMessageDialog(this, "No se ha almacenado el archivo "); } } catch (DocumentException ex) { Logger.getLogger(nuevoReporte.class.getName()).log(Level.SEVERE, null, ex); } catch (FileNotFoundException ex) { Logger.getLogger(nuevoReporte.class.getName()).log(Level.SEVERE, null, ex); } } }//GEN-LAST:event_jButton1ActionPerformed private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed listaReporte IE = new listaReporte(usuario); IE.setVisible(true); dispose(); }//GEN-LAST:event_jButton2ActionPerformed private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed int res = JOptionPane.showConfirmDialog(this, "Deseas eliminarlo?", "Eliminar", JOptionPane.YES_NO_OPTION); if (res == JOptionPane.YES_OPTION) { String sql = "DELETE FROM `inventario`.`presta` WHERE idprestamo = " + id; conectar conect = new conectar(); Connection con = conect.conexion(); boolean r = conectar.ejecutarTransaccion(sql, con); sql = "DELETE FROM `inventario`.`prestamo` WHERE idprestamo = " + id; r = conectar.ejecutarTransaccion(sql, con); if (r) { JOptionPane.showMessageDialog(this, "Se ha eliminado el prestamo"); listaReporte lr = new listaReporte(usuario); lr.setVisible(true); this.dispose(); } else { JOptionPane.showMessageDialog(this, ""); } try { con.close(); } catch (SQLException ex) { Logger.getLogger(ModificaPrestamo.class.getName()).log(Level.SEVERE, null, ex); } } }//GEN-LAST:event_jButton3ActionPerformed private void SolicitanteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SolicitanteActionPerformed // TODO add your handling code here: }//GEN-LAST:event_SolicitanteActionPerformed private void ProyectoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ProyectoActionPerformed // TODO add your handling code here: }//GEN-LAST:event_ProyectoActionPerformed private void tabReporteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabReporteMouseClicked // TODO add your handling code here: }//GEN-LAST:event_tabReporteMouseClicked private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed int res = JOptionPane.showConfirmDialog(this, "Deseas marcar el reporte como devuelto?", "Devolucion", JOptionPane.YES_NO_OPTION); if (res == JOptionPane.YES_OPTION) { java.util.Date dt = new java.util.Date(); java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String fechahoy = sdf.format(dt); String sql = "UPDATE `inventario`.`prestamo` SET `freal` = '" + fechahoy + "' WHERE `idprestamo` = " + id; conectar conect = new conectar(); Connection con = conect.conexion(); boolean r = conectar.ejecutarTransaccion(sql, con); if (!r) { JOptionPane.showMessageDialog(this, "NO ha registrado la devolucion"); return; } Object[][] contenido = this.getTableData(tabReporte); for (Object[] fila : contenido) { sql = "UPDATE `inventario`.`equipo` SET `equipoActivo` = 'activo' WHERE `idEquipo` = '" + fila[0].toString() + "'"; System.out.println(sql); r = conectar.ejecutarTransaccion(sql, con); } JOptionPane.showMessageDialog(this, "Se ha registrado la devolucion"); listaReporte lr = new listaReporte(usuario); lr.setVisible(true); this.dispose(); try { con.close(); } catch (SQLException ex) { Logger.getLogger(ModificaPrestamo.class.getName()).log(Level.SEVERE, null, ex); } } }//GEN-LAST:event_jButton4ActionPerformed /** * @param args the command line arguments */ // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JTextField Proyecto; private javax.swing.JTextField Solicitante; private javax.swing.JTextArea clausula1; private javax.swing.JTextArea clausula2; private javax.swing.JLabel fecha; private javax.swing.JLabel fecha1; private javax.swing.JLabel fecha2; private javax.swing.JLabel hora; private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JButton jButton3; private javax.swing.JButton jButton4; private javax.swing.JLabel jLabel13; private javax.swing.JLabel jLabel14; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JLabel label1; private javax.swing.JLabel label2; private javax.swing.JTable tabReporte; // End of variables declaration//GEN-END:variables public Object[][] getTableData(JTable table) { DefaultTableModel dtm = (DefaultTableModel) table.getModel(); int nRow = dtm.getRowCount(), nCol = dtm.getColumnCount(); Object[][] tableData = new Object[nRow][nCol]; for (int i = 0; i < nRow; i++) for (int j = 0; j < nCol; j++) tableData[i][j] = dtm.getValueAt(i, j); return tableData; } public boolean generapdf(String path) throws DocumentException, FileNotFoundException { boolean estado = false; Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(path)); document.open(); Image img = null; try { img = Image.getInstance("src/img/imgen.jpg"); img.scaleAbsolute(80f, 50f); img.setAbsolutePosition(470f, 750f); document.add(img); } catch (BadElementException ex) { Logger.getLogger(nuevoReporte.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(nuevoReporte.class.getName()).log(Level.SEVERE, null, ex); } Paragraph paragraph1 = new Paragraph("Solicitante: " + Solicitante.getText()); Paragraph paragraph2 = new Paragraph("Proyecto: " + Proyecto.getText()); Paragraph paragraph3 = new Paragraph( "Fecha y hora del prestamo: " + fecha.getText() + " " + hora.getText()); Paragraph paragraph4 = new Paragraph("Fecha de entrega: " + fecha1.getText()); document.add(paragraph1); document.add(paragraph2); document.add(paragraph3); document.add(paragraph4); Paragraph tmp = new Paragraph(""); tmp.setSpacingBefore(10); document.add(tmp); PdfPTable table = new PdfPTable(4); table.addCell("Id Equipo"); table.addCell("Equipo"); table.addCell("Accesorios"); table.addCell("Descripcion"); Object[][] tabla = this.getTableData(tabReporte); for (Object[] o : tabla) for (Object e : o) { table.addCell((String) e); } document.add(table); System.out.println(clausula1.getText() + " " + clausula2.getText()); Paragraph paragraph5 = new Paragraph("Clausula 1: " + clausula1.getText()); Paragraph paragraph6 = new Paragraph("Clausula 2: " + clausula2.getText()); document.add(paragraph5); document.add(paragraph6); tmp = new Paragraph(""); tmp.setSpacingBefore(10); document.add(tmp); Paragraph p = new Paragraph("Firma del solicitante: "); p.add(new Chunk(new DottedLineSeparator())); p.add(" Firma del prestador: "); p.add(new Chunk(new DottedLineSeparator())); document.add(p); //PdfPTable firma = new PdfPTable(1); //firma.addCell("Firma"); //document.add(new Paragraph("")); //document.add(firma); document.close(); return true; } }