package app.PREPROCESSING;

import app.DataUtil;
import app.HibernateUtil;
import org.hibernate.Session;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Petr Flégl on 17.11.2016.
 */
public class Parser implements ParserInterface {
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ";";
    ArrayList<String []> dataTable = new ArrayList<String[]>();

    @Override
    public void loadDataSource(String source, int sourceType) {
        if (sourceType == TYPE_FILE) {
            try {

                br = new BufferedReader(new FileReader(source));
                while ((line = br.readLine()) != null) {
                    dataTable.add(line.split(cvsSplitBy));
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        if (sourceType == TYPE_DB){
            String params[] = source.split(";");
            final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
            final String DB_URL = "jdbc:mysql://"+params[0];
            final String USER = params[2];
            final String PASS = params[3];

            Connection conn = null;
            Statement stmt = null;
            try{
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection(DB_URL, USER, PASS);
                stmt = conn.createStatement();

                String sql = "SELECT * FROM (SELECT * FROM "+params[1]+" ORDER BY "+params[5]+" DESC LIMIT "+params[4]+") sub ORDER BY "+params[5]+" ASC";
                ResultSet rs = stmt.executeQuery(sql);
                ResultSetMetaData rsmd = rs.getMetaData();
                int colCount = rsmd.getColumnCount();

                while(rs.next()){
                    String vals[] = new String[colCount];
                    for(int i= 0; i<colCount;i++){
                        vals[i] = rs.getString(i+1);
                    }
                    dataTable.add(vals);

                }
                rs.close();
            }catch(SQLException se){
                se.printStackTrace();
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                try{
                    if(stmt!=null)
                        conn.close();
                }catch(SQLException se){
                }
                try{
                    if(conn!=null)
                        conn.close();
                }catch(SQLException se){
                    se.printStackTrace();
                }
            }

        }
    }

    @Override
    public int getDatasetSize() {
        return dataTable.size();
    }

    @Override
    public List<?> processData(String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException{
        Class classTemp = Class.forName(className);
        ArrayList <Object> dataSave = new ArrayList();
        try {
        if (className.equals("app.DAO.Table1")){

            Session session = HibernateUtil.getSessionFactory().openSession();
            for (int i = 0; i < dataTable.size(); i++) {
                if (dataTable.get(i).length > 1) {
                    Object obj = classTemp.newInstance();
                    int timestamp = 0;
                    timestamp = DataUtil.dateToTimestamp(dataTable.get(i)[5], "\\.");

                    double kukurice = Double.valueOf(dataTable.get(i)[0].replace(",", "."));
                    double senaz = Double.valueOf(dataTable.get(i)[1].replace(",", "."));
                    double rizky = Double.valueOf(dataTable.get(i)[2].replace(",", "."));
                    double trava = Double.valueOf(dataTable.get(i)[3].replace(",", "."));
                    double kejda = Double.valueOf(dataTable.get(i)[4].replace(",", "."));
                    double digestat = (kukurice + senaz + rizky + trava + kejda) * 0.85;
                    double davka = (kukurice + senaz + rizky + trava);

                    obj.getClass().getMethod("setDatumDavky", Integer.TYPE).invoke(obj, timestamp);
                    obj.getClass().getMethod("setKukurice", Double.TYPE).invoke(obj, kukurice);
                    obj.getClass().getMethod("setSenaz", Double.TYPE).invoke(obj, senaz);
                    obj.getClass().getMethod("setRizky", Double.TYPE).invoke(obj, rizky);
                    obj.getClass().getMethod("setTrava", Double.TYPE).invoke(obj, trava);
                    obj.getClass().getMethod("setKejda", Double.TYPE).invoke(obj, kejda);
                    obj.getClass().getMethod("setDigestat", Double.TYPE).invoke(obj, digestat);
                    obj.getClass().getMethod("setDavka", Double.TYPE).invoke(obj, davka);

                    session.beginTransaction();
                    session.save(obj);
                    session.getTransaction().commit();

                    dataSave.add(obj);
                }

            }

            session.close();

        }

            if (className.equals("app.DAO.Table2")){

                Session session = HibernateUtil.getSessionFactory().openSession();
                for (int i = 0; i < dataTable.size(); i++) {
                    if (dataTable.get(i).length > 1) {
                        Object obj = classTemp.newInstance();

                    double kukuriceV = Double.valueOf(dataTable.get(i)[5].replace(",", "."))*240;
                    double senazV = Double.valueOf(dataTable.get(i)[7].replace(",", "."))*185;
                    double rizkyV = Double.valueOf(dataTable.get(i)[6].replace(",", "."))*90;
                    double travaV = Double.valueOf(dataTable.get(i)[8].replace(",", "."))*30;
                    double kejdaV = Double.valueOf(dataTable.get(i)[4].replace(",", "."))*25;
                    double celkVytez = (kukuriceV + senazV + rizkyV + travaV + kejdaV);
                    int datumVytez = Integer.valueOf(dataTable.get(i)[1].replace(",", "."));

                    obj.getClass().getMethod("setKukuriceV", Double.TYPE).invoke(obj, kukuriceV);
                    obj.getClass().getMethod("setSenazV", Double.TYPE).invoke(obj, senazV);
                    obj.getClass().getMethod("setRizkyV", Double.TYPE).invoke(obj, rizkyV);
                    obj.getClass().getMethod("setTravaV", Double.TYPE).invoke(obj, travaV);
                    obj.getClass().getMethod("setKejdaV", Double. TYPE).invoke(obj, kejdaV);
                    obj.getClass().getMethod("setCelkVytez", Double.TYPE).invoke(obj, celkVytez);
                        obj.getClass().getMethod("setDatumVytez", Integer.TYPE).invoke(obj, datumVytez);

                        session.beginTransaction();
                        session.save(obj);
                        session.getTransaction().commit();

                        dataSave.add(obj);
                    }

                }

                session.close();

            }

            if (className.equals("app.DAO.Table3")) {

                Session session = HibernateUtil.getSessionFactory().openSession();

                for (int i = 0; i < dataTable.size(); i++) {
                    if (dataTable.get(i).length > 1) {
                        Object obj = classTemp.newInstance();
                        int timestamp = 0;
                        timestamp = DataUtil.dateToTimestamp(dataTable.get(i)[0], "\\.");

                        obj.getClass().getMethod("setDatumOdberu", Integer.TYPE).invoke(obj, timestamp);
                        obj.getClass().getMethod("setNH4", Short.TYPE).invoke(obj, Short.valueOf(dataTable.get(i)[1]));
                        obj.getClass().getMethod("setFos", Short.TYPE).invoke(obj, Short.valueOf(dataTable.get(i)[2]));
                        obj.getClass().getMethod("setTac", Short.TYPE).invoke(obj, Short.valueOf(dataTable.get(i)[3]));
                        obj.getClass().getMethod("setFosTac", Byte.TYPE).invoke(obj, Byte.valueOf(dataTable.get(i)[4]));
                        obj.getClass().getMethod("setPH", Short.TYPE).invoke(obj, Short.valueOf(dataTable.get(i)[5]));
                        obj.getClass().getMethod("setKyselinaOctova", Short.TYPE).invoke(obj, Short.valueOf(dataTable.get(i)[6]));
                        obj.getClass().getMethod("setKyselinaPropionova", Byte.TYPE).invoke(obj, Byte.valueOf(dataTable.get(i)[7]));
                        obj.getClass().getMethod("setKyselinaMaselna", Byte.TYPE).invoke(obj, Byte.valueOf(dataTable.get(i)[8]));
                        obj.getClass().getMethod("setSumaNMK", Short.TYPE).invoke(obj, Short.valueOf(dataTable.get(i)[9]));
                        obj.getClass().getMethod("setSusina", Byte.TYPE).invoke(obj, Byte.valueOf(dataTable.get(i)[10]));

                        session.beginTransaction();
                        session.save(obj);
                        session.getTransaction().commit();

                        dataSave.add(obj);
                    }

                }

                session.close();

            }

            if (className.equals("app.DAO.Table4")){

                Session session = HibernateUtil.getSessionFactory().openSession();
                for (int i = 0; i < dataTable.size(); i++) {
                    if (dataTable.get(i).length > 1) {
                        Object obj = classTemp.newInstance();
                        int timestamp = 0;
                        timestamp = DataUtil.dateToTimestamp(dataTable.get(i)[5], "\\.");

                        double venkovniTeplota = Double.valueOf(dataTable.get(i)[0].replace(",", "."));
                        double teplotaFermentace = Double.valueOf(dataTable.get(i)[1].replace(",", "."));
                        int spotrebaPlynu = Integer.valueOf(dataTable.get(i)[2].replace(",", "."));
                        double plynojemMax = Double.valueOf(dataTable.get(i)[3].replace(",", "."));
                        double plynojemMin = Double.valueOf(dataTable.get(i)[4].replace(",", "."));
                        double plynojemPrumer = (plynojemMax + plynojemMin)/2;


                        obj.getClass().getMethod("setDatumBio", Integer.TYPE).invoke(obj, timestamp);
                        obj.getClass().getMethod("setVenkovniTeplota", Double.TYPE).invoke(obj, venkovniTeplota);
                        obj.getClass().getMethod("setTeplotaFermentace", Double.TYPE).invoke(obj, teplotaFermentace);
                        obj.getClass().getMethod("setSpotrebaPlynu", Integer.TYPE).invoke(obj, spotrebaPlynu);
                        obj.getClass().getMethod("setPlynojemMax", Double.TYPE).invoke(obj, plynojemMax);
                        obj.getClass().getMethod("setPlynojemMin", Double.TYPE).invoke(obj, plynojemMin);
                        obj.getClass().getMethod("setPlynojemPrumer", Double.TYPE).invoke(obj, plynojemPrumer);

                        session.beginTransaction();
                        session.save(obj);
                        session.getTransaction().commit();

                        dataSave.add(obj);
                    }

                }

                session.close();

            }

            if (className.equals("app.DAO.Table5")){

                Session session = HibernateUtil.getSessionFactory().openSession();
                for (int i = 0; i < dataTable.size(); i++) {
                    if (dataTable.get(i).length > 1) {
                        Object obj = classTemp.newInstance();
                        int timestamp = 0;
                        timestamp = DataUtil.dateToTimestamp(dataTable.get(i)[4], "\\.");

                        double metan = Double.valueOf(dataTable.get(i)[0].replace(",", "."));
                        double cO2 = Double.valueOf(dataTable.get(i)[1].replace(",", "."));
                        double kyslik = Double.valueOf(dataTable.get(i)[2].replace(",", "."));
                        int syra = Integer.valueOf(dataTable.get(i)[3].replace(",", "."));


                        obj.getClass().getMethod("setDatumPlyn", Integer.TYPE).invoke(obj, timestamp);
                        obj.getClass().getMethod("setMetan", Double.TYPE).invoke(obj, metan);
                        obj.getClass().getMethod("setcO2", Double.TYPE).invoke(obj, cO2);
                        obj.getClass().getMethod("setKyslik", Double.TYPE).invoke(obj, kyslik);
                        obj.getClass().getMethod("setSyra", Integer.TYPE).invoke(obj, syra);

                        session.beginTransaction();
                        session.save(obj);
                        session.getTransaction().commit();

                        dataSave.add(obj);
                    }

                }

                session.close();

            }

            if (className.equals("app.DAO.Table6")){

                Session session = HibernateUtil.getSessionFactory().openSession();
                for (int i = 0; i < dataTable.size(); i++) {
                    if (dataTable.get(i).length > 1) {
                        Object obj = classTemp.newInstance();

                        short a1 = Short.valueOf(dataTable.get(i)[0].replace(",", "."));
                        short a2 = Short.valueOf(dataTable.get(i)[1].replace(",", "."));
                        short a3 = Short.valueOf(dataTable.get(i)[2].replace(",", "."));
                        short a4 = Short.valueOf(dataTable.get(i)[3].replace(",", "."));
                        short a5 = Short.valueOf(dataTable.get(i)[4].replace(",", "."));
                        short a6 = Short.valueOf(dataTable.get(i)[5].replace(",", "."));
                        short b1 = Short.valueOf(dataTable.get(i)[6].replace(",", "."));
                        short b2 = Short.valueOf(dataTable.get(i)[7].replace(",", "."));
                        short b3 = Short.valueOf(dataTable.get(i)[8].replace(",", "."));
                        short b4 = Short.valueOf(dataTable.get(i)[9].replace(",", "."));
                        short b5 = Short.valueOf(dataTable.get(i)[10].replace(",", "."));
                        short b6 = Short.valueOf(dataTable.get(i)[11].replace(",", "."));
                        int aPrumer = (a1 + a2 + a3 + a4 + a5 + a6)/6;
                        int bPrumer = (b1 + b2 + b3 + b4 + b5 + b6)/6;
                        int prumer = (a1 + a2 + a3 + a4 + a5 + a6 + b1 + b2 + b3 + b4 + b5 + b6)/12;


                        obj.getClass().getMethod("setA1", Short.TYPE).invoke(obj, a1);
                        obj.getClass().getMethod("setA2", Short.TYPE).invoke(obj, a2);
                        obj.getClass().getMethod("setA3", Short.TYPE).invoke(obj, a3);
                        obj.getClass().getMethod("setA4", Short.TYPE).invoke(obj, a4);
                        obj.getClass().getMethod("setA5", Short.TYPE).invoke(obj, a5);
                        obj.getClass().getMethod("setA6", Short.TYPE).invoke(obj, a6);
                        obj.getClass().getMethod("setB1", Short.TYPE).invoke(obj, b1);
                        obj.getClass().getMethod("setB2", Short.TYPE).invoke(obj, b2);
                        obj.getClass().getMethod("setB3", Short.TYPE).invoke(obj, b3);
                        obj.getClass().getMethod("setB4", Short.TYPE).invoke(obj, b4);
                        obj.getClass().getMethod("setB5", Short.TYPE).invoke(obj, b5);
                        obj.getClass().getMethod("setB6", Short.TYPE).invoke(obj, b6);
                        obj.getClass().getMethod("setAPrumer", Integer.TYPE).invoke(obj, aPrumer);
                        obj.getClass().getMethod("setBPrumer", Integer.TYPE).invoke(obj, bPrumer);
                        obj.getClass().getMethod("setPrumer", Integer.TYPE).invoke(obj, prumer);

                        session.beginTransaction();
                        session.save(obj);
                        session.getTransaction().commit();

                        dataSave.add(obj);
                    }

                }

                session.close();

            }

        if (className.equals("app.DAO.Table7")){

            Session session = HibernateUtil.getSessionFactory().openSession();
            for (int i = 0; i < dataTable.size(); i++) {
                if (dataTable.get(i).length > 1) {
                    Object obj = classTemp.newInstance();
                    int timestamp = 0;
                    timestamp = DataUtil.dateToTimestamp(dataTable.get(i)[0], "\\.");

                    double teplotaOleje = Double.valueOf(dataTable.get(i)[1].replace(",", "."));
                    short klapkaSani = Short.valueOf(dataTable.get(i)[2].replace(",", "."));
                    double mazaciTlak = Double.valueOf(dataTable.get(i)[3].replace(",", "."));
                    short ventilHlavni = Short.valueOf(dataTable.get(i)[4].replace(",", "."));
                    short ventilNouzovy = Short.valueOf(dataTable.get(i)[5].replace(",", "."));
                    short teplotaSpalin = Short.valueOf(dataTable.get(i)[6].replace(",", "."));
                    double tlakKlikoveSkrine = Double.valueOf(dataTable.get(i)[7].replace(",", "."));

                    obj.getClass().getMethod("setDatumParam", Integer.TYPE).invoke(obj, timestamp);
                    obj.getClass().getMethod("setTeplotaOleje", Double.TYPE).invoke(obj, teplotaOleje);
                    obj.getClass().getMethod("setKlapkaSani", Short.TYPE).invoke(obj, klapkaSani);
                    obj.getClass().getMethod("setMazaciTlak", Double.TYPE).invoke(obj, mazaciTlak);
                    obj.getClass().getMethod("setVentilHlavni", Short.TYPE).invoke(obj, ventilHlavni);
                    obj.getClass().getMethod("setVentilNouzovy", Short.TYPE).invoke(obj, ventilNouzovy);
                    obj.getClass().getMethod("setTeplotaSpalin", Short.TYPE).invoke(obj, teplotaSpalin);
                    obj.getClass().getMethod("setTlakKlikoveSkrine", Double.TYPE).invoke(obj, tlakKlikoveSkrine);

                    session.beginTransaction();
                    session.save(obj);
                    session.getTransaction().commit();

                    dataSave.add(obj);
                }

            }

            session.close();

        }

            if (className.equals("app.DAO.Table8")){

                Session session = HibernateUtil.getSessionFactory().openSession();
                for (int i = 0; i < dataTable.size(); i++) {
                    if (dataTable.get(i).length > 1) {
                        Object obj = classTemp.newInstance();

                        short vykonSvorky = Short.valueOf(dataTable.get(i)[0].replace(",", "."));
                        byte spotrebaArealu = Byte.valueOf(dataTable.get(i)[1].replace(",", "."));
                        short fazeA1 = Short.valueOf(dataTable.get(i)[2].replace(",", "."));
                        short fazeA2 = Short.valueOf(dataTable.get(i)[3].replace(",", "."));
                        short fazeA3 = Short.valueOf(dataTable.get(i)[4].replace(",", "."));
                        short fazeV1 = Short.valueOf(dataTable.get(i)[5].replace(",", "."));
                        short fazeV2 = Short.valueOf(dataTable.get(i)[6].replace(",", "."));
                        short fazeV3 = Short.valueOf(dataTable.get(i)[7].replace(",", "."));

                        obj.getClass().getMethod("setVykonSvorky", Short.TYPE).invoke(obj, vykonSvorky);
                        obj.getClass().getMethod("setSpotrebaArealu", Byte.TYPE).invoke(obj, spotrebaArealu);
                        obj.getClass().getMethod("setFazeA1", Short.TYPE).invoke(obj, fazeA1);
                        obj.getClass().getMethod("setFazeA2", Short.TYPE).invoke(obj, fazeA2);
                        obj.getClass().getMethod("setFazeA3", Short.TYPE).invoke(obj, fazeA3);
                        obj.getClass().getMethod("setFazeV1", Short.TYPE).invoke(obj, fazeV1);
                        obj.getClass().getMethod("setFazeV2", Short.TYPE).invoke(obj, fazeV2);
                        obj.getClass().getMethod("setFazeV3", Short.TYPE).invoke(obj, fazeV3);

                        session.beginTransaction();
                        session.save(obj);
                        session.getTransaction().commit();

                        dataSave.add(obj);
                    }

                }

                session.close();

            }

            if (className.equals("app.DAO.Table9")){

                Session session = HibernateUtil.getSessionFactory().openSession();
                for (int i = 0; i < dataTable.size(); i++) {
                    if (dataTable.get(i).length > 1) {
                        Object obj = classTemp.newInstance();
                        int timestamp = 0;
                        timestamp = DataUtil.dateToTimestamp(dataTable.get(i)[0], "\\.");
                        int datumRozboru = (timestamp + 604800);

                        obj.getClass().getMethod("setDatumRozboru", Integer.TYPE).invoke(obj, datumRozboru);
                        obj.getClass().getMethod("setDatumOdberu", Integer.TYPE).invoke(obj, timestamp);
                        obj.getClass().getMethod("setDobaProvozu", Integer.TYPE).invoke(obj, Integer.valueOf(dataTable.get(i)[1].replace(",", ".")));
                        obj.getClass().getMethod("setDobaOleje", Short.TYPE).invoke(obj, Short.valueOf(dataTable.get(i)[2].replace(",", ".")));
                        obj.getClass().getMethod("setObsahVody", Double.TYPE).invoke(obj, Double.valueOf(dataTable.get(i)[3].replace(",", ".")));
                        obj.getClass().getMethod("setObsahGlykolu", Double.TYPE).invoke(obj, Double.valueOf(dataTable.get(i)[4].replace(",", ".")));
                        obj.getClass().getMethod("setNitrace", Byte.TYPE).invoke(obj, Byte.valueOf(dataTable.get(i)[5].replace(",", ".")));
                        obj.getClass().getMethod("setSulfatace", Byte.TYPE).invoke(obj, Byte.valueOf(dataTable.get(i)[6].replace(",", ".")));
                        obj.getClass().getMethod("setOxidace", Byte.TYPE).invoke(obj, Byte.valueOf(dataTable.get(i)[7].replace(",", ".")));
                        obj.getClass().getMethod("setTBN", Double.TYPE).invoke(obj, Double.valueOf(dataTable.get(i)[8].replace(",", ".")));
                        obj.getClass().getMethod("setAN", Double.TYPE).invoke(obj, Double.valueOf(dataTable.get(i)[9].replace(",", ".")));
                        obj.getClass().getMethod("setIPH", Double.TYPE).invoke(obj, Double.valueOf(dataTable.get(i)[10].replace(",", ".")));
                        obj.getClass().getMethod("setMed", Short.TYPE).invoke(obj, Short.valueOf(dataTable.get(i)[11].replace(",", ".")));
                        obj.getClass().getMethod("setZelezo", Short.TYPE).invoke(obj, Short.valueOf(dataTable.get(i)[12].replace(",", ".")));
                        obj.getClass().getMethod("setTypOleje", Integer.TYPE).invoke(obj, Integer.valueOf(dataTable.get(i)[14].replace(",", ".")));
                        obj.getClass().getMethod("setVymena", Integer.TYPE).invoke(obj, Integer.valueOf(dataTable.get(i)[14].replace(",", ".")));

                        session.beginTransaction();
                        session.save(obj);
                        session.getTransaction().commit();

                        dataSave.add(obj);
                    }

                }

                session.close();

            }

            if (className.equals("app.DAO.Table10")){

                Session session = HibernateUtil.getSessionFactory().openSession();
                for (int i = 0; i < dataTable.size(); i++) {
                    if (dataTable.get(i).length > 1) {
                        Object obj = classTemp.newInstance();
                        int timestamp = 0;
                        timestamp = DataUtil.dateToTimestamp(dataTable.get(i)[0], "\\.");
                        int dobaChodu = 0;
                        dobaChodu = DataUtil.timeToTimestamp(dataTable.get(i)[1], ":");

                        obj.getClass().getMethod("setDobaChodu", Integer.TYPE).invoke(obj, dobaChodu);
                        obj.getClass().getMethod("setDatum", Integer.TYPE).invoke(obj, timestamp);
                        obj.getClass().getMethod("setVykon", Integer.TYPE).invoke(obj, Integer.valueOf(dataTable.get(i)[2].replace(",", ".")));
                        obj.getClass().getMethod("setDoSite", Integer.TYPE).invoke(obj, Integer.valueOf(dataTable.get(i)[3].replace(",", ".")));
                        obj.getClass().getMethod("setZeSite", Integer.TYPE).invoke(obj, Integer.valueOf(dataTable.get(i)[4].replace(",", ".")));
                        obj.getClass().getMethod("setAreal", Integer.TYPE).invoke(obj, Integer.valueOf(dataTable.get(i)[5].replace(",", ".")));
                        obj.getClass().getMethod("setVlsSpot", Integer.TYPE).invoke(obj, Integer.valueOf(dataTable.get(i)[6].replace(",", ".")));

                        session.beginTransaction();
                        session.save(obj);
                        session.getTransaction().commit();

                        dataSave.add(obj);
                    }

                }

                session.close();

            }

            if (className.equals("app.DAO.DashboardPanelWidget")) {

                Session session = HibernateUtil.getSessionFactory().openSession();

                for (int i = 0; i < dataTable.size(); i++) {
                    if (dataTable.get(i).length > 1) {
                        Object obj = classTemp.newInstance();

                        obj.getClass().getMethod("setNameID", String.class).invoke(obj, String.valueOf(dataTable.get(i)[0]));
                        obj.getClass().getMethod("setRow", Integer.TYPE).invoke(obj, Integer.valueOf(dataTable.get(i)[1]));
                        obj.getClass().getMethod("setCol", Integer.TYPE).invoke(obj, Integer.valueOf(dataTable.get(i)[2]));

                        session.beginTransaction();
                        session.save(obj);
                        session.getTransaction().commit();

                        dataSave.add(obj);
                    }

                }

                session.close();

            }

            if (className.equals("app.DAO.OilT")) {

                Session session = HibernateUtil.getSessionFactory().openSession();

                for (int i = 0; i < dataTable.size(); i++) {
                    if (dataTable.get(i).length > 1) {
                        Object obj = classTemp.newInstance();

                        obj.getClass().getMethod("setNajeto", Integer.TYPE).invoke(obj, Integer.valueOf(dataTable.get(i)[0]));
                        obj.getClass().getMethod("setZbyva", Integer.TYPE).invoke(obj, Integer.valueOf(dataTable.get(i)[1]));
                        obj.getClass().getMethod("setCelkem", Integer.TYPE).invoke(obj, Integer.valueOf(dataTable.get(i)[2]));
                        obj.getClass().getMethod("setTeplota", Double.TYPE).invoke(obj, Double.valueOf(dataTable.get(i)[3].replace(",", ".")));

                        session.beginTransaction();
                        session.save(obj);
                        session.getTransaction().commit();

                        dataSave.add(obj);
                    }

                }

                session.close();

            }

            if (className.equals("app.DAO.ElektroT")) {

                Session session = HibernateUtil.getSessionFactory().openSession();

                for (int i = 0; i < dataTable.size(); i++) {
                    if (dataTable.get(i).length > 1) {
                        Object obj = classTemp.newInstance();

                        obj.getClass().getMethod("setSvorkOK", Integer.TYPE).invoke(obj, Integer.valueOf(dataTable.get(i)[0]));
                        obj.getClass().getMethod("setSvorkD", Integer.TYPE).invoke(obj, Integer.valueOf(dataTable.get(i)[1]));
                        obj.getClass().getMethod("setProdejD", Integer.TYPE).invoke(obj, Integer.valueOf(dataTable.get(i)[2]));
                        obj.getClass().getMethod("setSpotAD", Integer.TYPE).invoke(obj, Integer.valueOf(dataTable.get(i)[3]));
                        obj.getClass().getMethod("setSpotBPSD", Integer.TYPE).invoke(obj, Integer.valueOf(dataTable.get(i)[4]));
                        obj.getClass().getMethod("setBonusM", Integer.TYPE).invoke(obj, Integer.valueOf(dataTable.get(i)[5]));
                        obj.getClass().getMethod("setSvorkM", Integer.TYPE).invoke(obj, Integer.valueOf(dataTable.get(i)[6]));
                        obj.getClass().getMethod("setProdejM", Integer.TYPE).invoke(obj, Integer.valueOf(dataTable.get(i)[7]));

                        session.beginTransaction();
                        session.save(obj);
                        session.getTransaction().commit();

                        dataSave.add(obj);
                    }

                }

                session.close();

            }
            if (className.equals("app.DAO.ProcesStatusTable")) {

                Session session = HibernateUtil.getSessionFactory().openSession();

                for (int i = 0; i < dataTable.size(); i++) {
                    if (dataTable.get(i).length > 1) {
                        Object obj = classTemp.newInstance();

                        obj.getClass().getMethod("setUrovenVnoreni", String.class).invoke(obj, String.valueOf(dataTable.get(i)[0]));
                        obj.getClass().getMethod("setTipChyb", String.class).invoke(obj, String.valueOf(dataTable.get(i)[1]));
                        obj.getClass().getMethod("setPopis", String.class).invoke(obj, String.valueOf(dataTable.get(i)[2]));

                        session.beginTransaction();
                        session.save(obj);
                        session.getTransaction().commit();

                        dataSave.add(obj);
                    }

                }

                session.close();

            }

            if (className.equals("app.DAO.CalendarEvent")) {

                Session session = HibernateUtil.getSessionFactory().openSession();

                for (int i = 0; i < dataTable.size(); i++) {
                    if (dataTable.get(i).length > 1) {
                        Object obj = classTemp.newInstance();

                        obj.getClass().getMethod("setTimestamp", Integer.TYPE).invoke(obj, Integer.valueOf(dataTable.get(i)[0]));
                        obj.getClass().getMethod("setPopis", String.class).invoke(obj, String.valueOf(dataTable.get(i)[1]));
                        obj.getClass().getMethod("setVisible", Boolean.TYPE).invoke(obj, Boolean.valueOf(dataTable.get(i)[2]));

                        session.beginTransaction();
                        session.save(obj);
                        session.getTransaction().commit();

                        dataSave.add(obj);
                    }

                }

                session.close();

            }
            clearData();

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return dataSave;

    }

    @Override
    public void clearData() {
        dataTable.clear();
    }


}
