/*
 * 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 sikd.ikonsol.ejb.session;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.postgresql.copy.CopyManager;
import org.postgresql.core.BaseConnection;
import sikd.ikonsol.ejb.dao.HeaderDataKonsolidasiDao;
import sikd.ikonsol.model.HeaderDataKonsolidasi;
import sikd.ikonsol.model.RawData;
import sikd.ikonsol.model.RawDataLak;
import sikd.ikonsol.model.RawDataLpe;
import sikd.ikonsol.model.RawDataLpsal;
import sikd.sikd5.sikd5ejb.session.util.MyBatisConnectionFactory;

/**
 *
 * @author Imam Bhoim
 */
@Stateless
public class HeaderDataKonsolidasiEjbHomeImpl implements HeaderDataKonsolidasiEjbHome {

    private SqlSessionFactory sqlSessionFactory;
    private SqlSession session = null;

    private HeaderDataKonsolidasiDao dao;

    
    private SqlSessionFactory sqlSessionFactory2;
    private SqlSession session2 = null;
    
    @PostConstruct
    public void init() {
        try {
            System.out.println("-------- Load MyBatis Factory IKonsol ------- ");
            sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactoryIkonsol();
            dao = new HeaderDataKonsolidasiDao(sqlSessionFactory);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public List<HeaderDataKonsolidasi> findAll() {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        return dao.findAll();
    }

    @Override
    public HeaderDataKonsolidasi findByID(int id) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        return dao.findByID(id);
    }

    @Override
    public HeaderDataKonsolidasi createNew() {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        return new HeaderDataKonsolidasi();
    }

    @Override
    public HeaderDataKonsolidasi save(HeaderDataKonsolidasi obj) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        return dao.save(obj);
    }

    @Override
    public HeaderDataKonsolidasi findByKode(String kode) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public HeaderDataKonsolidasi update(HeaderDataKonsolidasi obj) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        return dao.update(obj);
    }

    @Override
    public void delete(int id) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        dao.deleteByID(id);
    }

    @Override
    public RawData saveImportRawdata(RawData obj) {
        return dao.saveImportRawdata(obj);

    }

    @Override
    public List<HeaderDataKonsolidasi> findByPemdaCount(int header) {
        // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        return dao.findAllByPemdaCount(header);
    }

    @Override
    public RawDataLak saveImportRawdataLak(RawDataLak obj) {
        return dao.saveImportRawdataLak(obj);
    }

    @Override
    public RawDataLpe saveImportRawdataLpe(RawDataLpe obj) {
        return dao.saveImportRawdataLpe(obj);
    }

    @Override
    public RawDataLpsal saveImportRawdataLpsal(RawDataLpsal obj) {
        return dao.saveImportRawdataLpsal(obj);
    }

    @Override
    public List<HeaderDataKonsolidasi> findByPemdaCountLak(int header) {
        return dao.findAllByPemdaCountLak(header);
    }

    @Override
    public List<HeaderDataKonsolidasi> findByPemdaCountLpe(int header) {
        return dao.findAllByPemdaCountLpe(header);
    }

    @Override
    public List<HeaderDataKonsolidasi> findByPemdaCountLpsal(int header) {
        return dao.findAllByPemdaCountLpsal(header);
    }

    public HeaderDataKonsolidasi copyData(HeaderDataKonsolidasi obj) {
        try {
            long a = new CopyManager((BaseConnection) sqlSessionFactory.openSession().getConnection()).copyIn(
                    "COPY imported_reports FROM STDIN (FORMAT csv, HEADER)",
                    new BufferedReader(new FileReader("G:/AAA.csv"))
            );
        } catch (SQLException ex) {
            Logger.getLogger(HeaderDataKonsolidasiEjbHomeImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(HeaderDataKonsolidasiEjbHomeImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return obj;

    }

//     public static void readInputData(String inputDataFile)
//            throws FileNotFoundException, IOException, SQLException {
//        Connection connection = DriverManager.getConnection(
//                "jdbc:postgresql://localhost:5433/ikonsol", "postgres", "haromain182");
//        String sql = "COPY imported_reports FROM stdin CSV HEADER DELIMITER ','";
//        BaseConnection pgcon = (BaseConnection) SqlSessionFactory;
//        CopyManager mgr = new CopyManager(pgcon);
//        try {
//            Reader in = new BufferedReader(new FileReader(new File(inputDataFile)));
//            long rowsaffected = mgr.copyIn(sql, in);
//        } catch (SQLException ex) {
//            System.err.println(ex.getClass().getName() + ": " + ex.getMessage());
//            System.exit(0);
//        }
//    }
    @Override
    public RawData saveImportRawdataUpdate(RawData obj) {
        return dao.saveImportRawdataUpdate(obj);
    }

    @Override
    public List<HeaderDataKonsolidasi> findKdpemda() {
        return dao.findKdpemda();
    }

    @Override
    public void deleteData(String kdpemda, int id) {
        dao.deleteData(kdpemda, id);
    }

    @Override
    public void deleteRawTemp() {
        dao.deleteRawTemp();
    }

    @Override
    public RawDataLak saveImportRawdataLakUpdate(RawDataLak obj) {
        return dao.saveImportRawdataLakUpdate(obj);
    }

    @Override
    public RawDataLpe saveImportRawdataLpeUpdate(RawDataLpe obj) {
        return dao.saveImportRawdataLpeUpdate(obj);
    }

    @Override
    public RawDataLpsal saveImportRawdataLpsalUpdate(RawDataLpsal obj) {
        return dao.saveImportRawdataLpsalUpdate(obj);
    }

    @Override
    public List<HeaderDataKonsolidasi> findKdpemdaLpe() {
        return dao.findKdpemdaLpe();
    }

    @Override
    public void deleteDataLpe(String kdpemda, int id) {
        dao.deleteDataLpe(kdpemda, id);
    }

    @Override
    public void deleteRawLpeTemp() {
        dao.deleteRawLpeTemp();
    }

    @Override
    public List<HeaderDataKonsolidasi> findKdpemdaLak() {
        return dao.findKdpemdaLak();
    }

    @Override
    public void deleteDataLak(String kdpemda, int id) {
        dao.deleteDataLak(kdpemda, id);
    }

    @Override
    public void deleteRawLakTemp() {
        dao.deleteRawLakTemp();
    }

    @Override
    public List<HeaderDataKonsolidasi> findKdpemdaLpsal() {
        return dao.findKdpemdaLpsal();
    }

    @Override
    public void deleteDataLpsal(String kdpemda, int id) {
        dao.deleteDataLpsal(kdpemda, id);
    }

    @Override
    public void deleteRawLpsalTemp() {
        dao.deleteRawLpsalTemp();
    }

    @Override
    public void deleteRawDataLRANeracaLO(int id) {
        dao.deleteByIDRawDataLRANeracaLO(id);
    }

    @Override
    public void deleteRawDataLak(int id) {
        dao.deleteByIDRawDataLak(id);
    }

    @Override
    public void deleteRawDataLpe(int id) {
        dao.deleteByIDRawDataLpe(id);
    }

    @Override
    public void deleteRawDataLpsal(int id) {
        dao.deleteByIDRawDataLpsal(id);
    }

    @Override
    public HeaderDataKonsolidasi findCountRawData(int id) {
   return dao.findCountData(id);
    }
    
    
    @Override
    public HeaderDataKonsolidasi cekCon(HeaderDataKonsolidasi obj) {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        HeaderDataKonsolidasi obj2 = null;
        try {
            System.out.println("--------Loadmuluuu------- ");
            MyBatisConnectionFactory.setSqlSessionFactoryIkonsolDw2(obj.getServerDb(), obj.getNamaDb(), obj.getPortDb(), obj.getVendorDb(), obj.getUserName(), obj.getPassDb());
            sqlSessionFactory2 = MyBatisConnectionFactory.getSqlSessionFactoryIkonsolDw2();
            dao = new HeaderDataKonsolidasiDao(sqlSessionFactory2);

            obj2 = dao.findAllByPemdaCount();
            System.out.println("XXXX=======" + obj2.getTahun());
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return obj2;

    }

    public HeaderDataKonsolidasi updateDW(HeaderDataKonsolidasi obj) {
        // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        return dao.updateDW(obj);
    }

    @Override
    public HeaderDataKonsolidasi findCountRawDataLak(int id) {
   return dao.findCountDataLak(id);
 }

    @Override
    public HeaderDataKonsolidasi findCountRawDataLpe(int id) {
     return dao.findCountDataLpe(id);
}

    @Override
    public HeaderDataKonsolidasi findCountRawDataLpsal(int id) {
    return dao.findCountDataLpsal(id);
}

}
