optika.sql.java Source code

Java tutorial

Introduction

Here is the source code for optika.sql.java

Source

/*
 * 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 optika;

import com.lowagie.text.Chunk;
import java.awt.print.*;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.FontFactory;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.FontSelector;
import com.lowagie.text.pdf.PdfBarcode;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import java.awt.Color;
import static java.awt.Color.blue;
import static java.awt.Color.green;
import static java.awt.Color.red;
import java.awt.Desktop;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import static java.lang.Integer.parseInt;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import static javax.swing.text.StyleConstants.FontFamily;
import static optika.login.kycu;

/**
 *
 * @author vilson
 */
public class sql {

    public BufferedReader br;
    private PrinterJob job;

    public Connection autorizimi() {
        Connection conn = null;
        if (new File("auth.txt").isFile()) {
            try {
                BufferedReader br = new BufferedReader(new FileReader("auth.txt"));
                String line = br.readLine();
                String[] arr = new String[2];
                int i = 0;
                while (line != null) {
                    arr[i] = line;
                    line = br.readLine();
                    i++;

                }

                Class.forName(arr[0]);
                conn = DriverManager.getConnection(arr[1]);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(sql.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(sql.class.getName()).log(Level.SEVERE, null, ex);
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(sql.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(sql.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return conn;
    }

    public String[] push(String[] array, String push) {
        String[] longer = new String[array.length + 1];
        for (int i = 0; i < array.length; i++) {
            longer[i] = array[i];
        }
        longer[array.length] = push;
        return longer;
    }

    public int getRecetatCount() {
        int row = 0;
        if (new File("auth.txt").isFile()) {

            try {

                if (autorizimi() != null) {
                    Statement sta = autorizimi().createStatement();

                    String Sql = "select * from recetat;";
                    ResultSet rs = sta.executeQuery(Sql);
                    int i = 0;
                    while (rs.next()) {

                        i++;
                    }
                    row = i;
                }

            } catch (SQLException ex) {
                Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return row;
    }

    public String[][] merrRecetat() {
        String recetat[][] = new String[getRecetatCount()][1];
        if (new File("auth.txt").isFile()) {

            try {

                if (autorizimi() != null) {
                    Statement sta = autorizimi().createStatement();

                    String Sql = "select * from recetat order by id desc;";
                    ResultSet rs = sta.executeQuery(Sql);
                    ResultSetMetaData rsmd = rs.getMetaData();

                    int i = 0;
                    while (rs.next()) {
                        for (int j = 1; j <= 7; j++) {
                            recetat[i] = push(recetat[i], rs.getString(j));

                        }
                        i++;
                    }

                }

            } catch (SQLException ex) {
                Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return recetat;
    }

    public String[][] merrReceten(String Sql) {
        String receta[][] = null;
        if (new File("auth.txt").isFile()) {

            try {

                if (autorizimi() != null) {
                    Statement sta = autorizimi().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                            ResultSet.CONCUR_READ_ONLY);

                    ResultSet rs = sta.executeQuery(Sql);
                    ResultSetMetaData rsmd = rs.getMetaData();

                    int columnsNumber = rsmd.getColumnCount();
                    String[][] recetat = new String[columnsNumber][1];
                    int i = 0;

                    while (rs.next()) {
                        for (int j = 1; j <= columnsNumber; j++) {
                            recetat[i] = push(recetat[i], rs.getString(j));

                        }
                        i++;
                    }
                    receta = recetat;
                }

            } catch (SQLException ex) {
                Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        return receta;
    }

    public void update(String Sql) {

        if (new File("auth.txt").isFile()) {

            try {

                if (autorizimi() != null) {
                    Statement sta = autorizimi().createStatement();
                    sta.executeUpdate(Sql);

                }

            } catch (SQLException ex) {
                Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    public boolean checkPassword(String Sql) {
        boolean po = false;
        if (new File("auth.txt").isFile()) {

            try {

                if (autorizimi() != null) {
                    Statement sta = autorizimi().createStatement();
                    ResultSet rs = sta.executeQuery(Sql);
                    while (rs.next()) {
                        po = true;
                    }
                }

            } catch (SQLException ex) {
                Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return po;
    }

    public void eksportoNePdf(String id) {
        Document document = new Document() {
        };
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("receta.pdf"));
            document.open();
            document.setPageSize(PageSize.A3);

            Image img = Image.getInstance("receta.jpg");
            img.setAbsolutePosition(450f, 10f);

            img.scaleToFit(600, 849);

            img.setAlignment(Image.LEFT | Image.ALIGN_BOTTOM | Image.ALIGN_BASELINE);
            img.setAbsolutePosition(0, 0);
            document.add(img);
            String[][] receta = merrReceten("select * from recetat where id=" + parseInt(id) + ";");
            String[][] tabela = merrReceten("select * from tabela where recetat_id=" + parseInt(id) + ";");
            int[] white = new int[tabela.length];
            for (int i = 0; i < tabela.length; i++) {
                white[i] = 0;
                int bosh = 0;
                for (int j = 3; j < tabela[i].length; j++) {
                    if (tabela[i][j] == tabela[i][6]) {
                        continue;
                    }

                    if (!tabela[i][j].isEmpty()) {
                        bosh = 1;
                    }

                }

                if (bosh == 0) {
                    white[i] = 1;
                }
            }
            Paragraph data = new Paragraph("Data: " + receta[0][7]);
            data.setSpacingBefore(38);
            data.setSpacingAfter(40);
            PdfPTable table = new PdfPTable(7);
            if (white[0] == 0) {
                table.addCell(getCellPadding("" + tabela[0][3], 1));
            } else {
                table.addCell(getCellWhite("_", 1, 30));
            }
            table.addCell(getCellPadding("" + tabela[0][4], 1));
            table.addCell(getCellPadding("" + tabela[0][5], 1));
            table.addCell(getCellPadding("", 1));
            table.addCell(getCellPadding("" + tabela[0][7], 1));
            table.addCell(getCellPadding("" + tabela[0][8], 1));
            table.addCell(getCellPadding("" + tabela[0][9], 1));
            table.setWidthPercentage(105);
            table.setHorizontalAlignment(-100);

            if (white[1] == 0) {
                table.addCell(getCellPadding("" + tabela[1][3], 1));
            } else {

                table.addCell(getCellWhite("_", 1, 36));
            }
            table.addCell(getCellPadding("" + tabela[1][4], 1));
            table.addCell(getCellPadding("" + tabela[1][5], 1));
            table.addCell(getCellPadding("", 1));
            table.addCell(getCellPadding("" + tabela[1][7], 1));
            table.addCell(getCellPadding("" + tabela[1][8], 1));
            table.addCell(getCellPadding("" + tabela[1][9], 1));
            table.setWidthPercentage(105);
            table.setHorizontalAlignment(-100);

            if (white[2] == 0) {
                if (white[1] == 0) {
                    table.addCell(getCell("" + tabela[2][3], 1, 30));

                } else {
                    table.addCell(getCellWhite("_", 1, 23));
                }
            } else {

                table.addCell(getCellWhite("_", 1, 28));

            }
            table.addCell(getCell("" + tabela[2][4], 1, 0));
            table.addCell(getCell("" + tabela[2][5], 1, 0));
            table.addCell(getCell("", 1, 0));
            table.addCell(getCell("" + tabela[2][7], 1, 0));
            table.addCell(getCell("" + tabela[2][8], 1, 0));
            table.addCell(getCell("" + tabela[2][9], 1, 0));
            table.setWidthPercentage(105);
            table.setSpacingBefore(27);
            table.setHorizontalAlignment(-100);

            String[][] distanca = merrReceten("select * from distanca where recetat_id=" + parseInt(id) + ";");

            PdfPTable largAfer = new PdfPTable(3);

            if (distanca[0][3].isEmpty()) {
                PdfPCell larg = getCellWhite("_", 2, 15);
                largAfer.addCell(larg);
            } else {
                PdfPCell larg = new PdfPCell(new Phrase("" + distanca[0][3]));
                larg.setPadding(0);
                larg.setHorizontalAlignment(2);
                larg.setBorder(PdfPCell.NO_BORDER);
                larg.setPaddingBottom(20);
                largAfer.addCell(larg);
            }
            largAfer.addCell(getCell("", PdfPCell.ALIGN_RIGHT, 25));

            if (distanca[0][8].isEmpty()) {
                largAfer.addCell(getCellWhite("_", PdfPCell.ALIGN_RIGHT, 15));
            } else {
                largAfer.addCell(getCell("" + distanca[0][8], PdfPCell.ALIGN_RIGHT, 25));
            }
            largAfer.setWidthPercentage(75);
            largAfer.setHorizontalAlignment(350);

            PdfPTable od_os = new PdfPTable(5);
            od_os.addCell(getCell("OD= " + distanca[0][4], 0, 195));
            od_os.addCell(getCell("OS= " + distanca[0][5], 2, 195));
            od_os.addCell(getCell("", 0, 0));
            od_os.addCell(getCell("OD= " + distanca[0][9], 0, 0));
            od_os.addCell(getCell("OS= " + distanca[0][10], 1, 0));
            od_os.setWidthPercentage(90);
            od_os.setHorizontalAlignment(150);

            PdfPTable visusi = new PdfPTable(2);
            if (distanca[0][6].isEmpty()) {
                PdfPCell od_pa = getCellWhite("_", 2, 17);
                od_pa.setPaddingRight(45);
                visusi.addCell(od_pa);
            } else {
                PdfPCell od_pa = getCell(distanca[0][6], 2, 17);
                od_pa.setPaddingRight(45);
                visusi.addCell(od_pa);
            }

            if (distanca[0][11].isEmpty()) {
                visusi.addCell(getCellWhite("_", 2, 17));
            } else {
                visusi.addCell(getCell(distanca[0][11], 2, 17));
            }

            if (distanca[0][7].isEmpty()) {
                PdfPCell os_pa = getCellWhite("_", 2, 17);
                os_pa.setPaddingRight(45);
                visusi.addCell(os_pa);
            } else {
                PdfPCell os_pa = getCell(distanca[0][7], 2, 17);
                os_pa.setPaddingRight(45);
                visusi.addCell(os_pa);
            }

            if (distanca[0][12].isEmpty()) {
                visusi.addCell(getCellWhite("_", 2, 17));
            } else {
                visusi.addCell(getCell(distanca[0][12], 2, 0));
            }
            visusi.setWidthPercentage(100);
            visusi.setHorizontalAlignment(50);

            String[][] admin = merrReceten("select * from admin where id=1;");
            PdfPTable klienti = new PdfPTable(3);
            klienti.addCell(getCell("", 0, 0));
            klienti.addCell(getCell("", 0, 0));
            klienti.addCell(getCell("Emri: " + receta[0][2], 0, 0));

            klienti.addCell(getCell("Celular: " + admin[0][4], 0, 0));
            klienti.addCell(getCell("", 0, 0));
            PdfPCell celReceta = getCell("Celular: " + receta[0][4], 0, 0);
            celReceta.setPaddingTop(5);
            klienti.addCell(celReceta);

            klienti.addCell(getCell("Email: " + admin[0][5], 0, 0));
            klienti.addCell(getCell("", 0, 0));
            PdfPCell emailReceta = getCell("Email: " + receta[0][5], 0, 0);
            emailReceta.setPaddingBottom(5);
            emailReceta.setPaddingTop(5);
            klienti.addCell(emailReceta);

            klienti.addCell(getCell("Adresa: " + admin[0][6], 0, 0));
            klienti.addCell(getCell("", 0, 0));
            klienti.addCell(getCell("Adresa: " + receta[0][6], 0, 0));
            klienti.setSpacingBefore(50);

            PdfPTable kreu = new PdfPTable(1);
            kreu.addCell(getCell(" ", 0, 0));

            document.add(kreu);
            document.add(klienti);
            document.add(data);
            document.add(table);
            document.add(largAfer);
            document.add(od_os);
            document.add(visusi);

        } catch (DocumentException de) {
            System.err.println(de.getMessage());
        } catch (IOException ioe) {
            System.err.println(ioe.getMessage());
        }
        document.close();

    }

    public void printPdf() {

        try {
            FileInputStream in = new FileInputStream("receta.pdf");
            Doc doc = new SimpleDoc(in, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
            PrintService service = PrintServiceLookup.lookupDefaultPrintService();
            service.createPrintJob().print(doc, null);
        } catch (PrintException e) {
            e.printStackTrace();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(sql.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public PdfPCell getCellPadding(String text, int alignment) {
        PdfPCell cell = new PdfPCell(new Phrase(text));
        cell.setPadding(0);
        cell.setHorizontalAlignment(alignment);
        cell.setBorder(PdfPCell.NO_BORDER);
        cell.setPaddingBottom(36);
        return cell;
    }

    public PdfPCell getCellWhite(String text, int alignment, int bottom) {
        PdfPCell cell = new PdfPCell();
        FontSelector selector = new FontSelector();
        com.lowagie.text.Font f1 = FontFactory.getFont(FontFactory.TIMES_ROMAN, 12);
        f1.setColor(Color.white);
        selector.addFont(f1);
        cell.addElement(selector.process(text));
        cell.setPadding(0);
        cell.setHorizontalAlignment(alignment);
        cell.setBorder(PdfPCell.NO_BORDER);
        cell.setPaddingBottom(bottom);
        return cell;
    }

    public PdfPCell getCell(String text, int alignment, int bottom) {
        PdfPCell cell = new PdfPCell(new Phrase(text));
        cell.setPadding(0);
        cell.setHorizontalAlignment(alignment);
        cell.setBorder(PdfPCell.NO_BORDER);
        cell.setPaddingBottom(bottom);
        return cell;
    }

    public int createTable(String sql) {
        int id = 0;
        try {

            if (autorizimi() != null) {
                Statement sta = autorizimi().createStatement();
                sta.executeUpdate(sql);

                ResultSet max = sta.executeQuery("select max(id) as id from recetat");
                while (max.next()) {
                    id = max.getInt("id");
                }
            }

        } catch (SQLException ex) {
            Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, ex);
        }
        return id;
    }

}