/*
 * 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.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
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;

/**
 *
 * @author riyan
 */
public class DataWarehouseDao implements IDAO<RawData>,Serializable{

    
    private SqlSessionFactory sqlSessionFactory = null;
    private SqlSession session = null;

    public DataWarehouseDao(SqlSessionFactory sqlSessionFactory) {
        this.sqlSessionFactory = sqlSessionFactory;
    }
    
    @Override
    public RawData createNew() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<RawData> findAll() {
     //   throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        List<RawData> list = new ArrayList<RawData>();

        session = sqlSessionFactory.openSession();
        try {
            list = session.selectList("DwMapper.selectAll");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.close();
        }

        return list;
    }

    @Override
    public List<RawData> findByCriteria(String sql) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

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

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

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

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

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

    @Override
    public void deleteByPK(String pk) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    
public List<RawData> findAllByParam(int tahun,String periode,int jenis,String namaTbl,String prov) {
        Map<String, Object> map = new HashMap<>();
        map.put("tahun", tahun);
        map.put("periode", periode);
        map.put("namaTbl",namaTbl.toString().trim());
        map.put("prpov",prov);
        List<RawData> list = null;
        session = sqlSessionFactory.openSession();
        try {

            if (jenis == 1) {

                list = session.selectList("DwMapper.selectAllVwLra", map);
            } else if (jenis == 2) {

                list = session.selectList("DwMapper.selectAllVwNeraca", map);
            } else if (jenis == 3) {

                list = session.selectList("DwMapper.selectAllVwLo", map);
            } else if (jenis == 4) {

                list = session.selectList("DwMapper.selectAllVwLak", map);
            } else if (jenis == 5) {

                list = session.selectList("DwMapper.selectAllVwLpe", map);
            } else if (jenis == 6) {

                list = session.selectList("DwMapper.selectAllVwLpsal", map);
            }
            

        } finally {
            session.close();
        }
        return list;
    }

public List<RawDataLpe> findAllByParamLpe(int tahun,String periode,int jenis,String namaTbl,String prov) {
        Map<String, Object> map = new HashMap<>();
        map.put("tahun", tahun);
        map.put("periode", periode);
        map.put("namaTbl",namaTbl);
        map.put("prpov",prov);
        List<RawDataLpe> list = null;
        session = sqlSessionFactory.openSession();
        try {

            if(jenis == 1){
            
            list = session.selectList("DwMapper.selectAllVwLra", map);
            }else  if(jenis == 2){
            
            list = session.selectList("DwMapper.selectAllVwNeraca", map);
            }else  if(jenis == 3){
            
            list = session.selectList("DwMapper.selectAllVwLo", map);
            }
            else  if(jenis == 4){
            
            list = session.selectList("DwMapper.selectAllVwLak", map);
            }
            else  if(jenis == 5){
            
            list = session.selectList("DwMapper.selectAllVwLpe", map);
            }
            else  if(jenis == 6){
            
            list = session.selectList("DwMapper.selectAllVwLpsal", map);
            }
            

        } finally {
            session.close();
        }
        return list;
    }

public List<RawDataLak> findAllByParamLak(int tahun,String periode,int jenis,String namaTbl,String prov) {
        Map<String, Object> map = new HashMap<>();
        map.put("tahun", tahun);
        map.put("periode", periode);
        map.put("namaTbl",namaTbl);
        map.put("prpov",prov);
        List<RawDataLak> list = null;
        session = sqlSessionFactory.openSession();
        try {

            if(jenis == 1){
            
            list = session.selectList("DwMapper.selectAllVwLra", map);
            }else  if(jenis == 2){
            
            list = session.selectList("DwMapper.selectAllVwNeraca", map);
            }else  if(jenis == 3){
            
            list = session.selectList("DwMapper.selectAllVwLo", map);
            }
            else  if(jenis == 4){
            
            list = session.selectList("DwMapper.selectAllVwLak", map);
            }
            else  if(jenis == 5){
            
            list = session.selectList("DwMapper.selectAllVwLpe", map);
            }
            else  if(jenis == 6){
            
            list = session.selectList("DwMapper.selectAllVwLpsal", map);
            }
            

        } finally {
            session.close();
        }
        return list;
    }

public List<RawDataLpsal> findAllByParamLpsal(int tahun,String periode,int jenis,String namaTbl,String prov) {
        Map<String, Object> map = new HashMap<>();
        map.put("tahun", tahun);
        map.put("periode", periode);
        map.put("namaTbl",namaTbl);
        map.put("prpov",prov);
        List<RawDataLpsal> list = null;
        session = sqlSessionFactory.openSession();
        try {

            if(jenis == 1){
            
            list = session.selectList("DwMapper.selectAllVwLra", map);
            }
            else  if(jenis == 6){
            
            list = session.selectList("DwMapper.selectAllVwLpsal", map);
            }
            

        } finally {
            session.close();
        }
        return list;
    }

public RawData findCountByParam(int tahun,String periode,int jenis,String namaTbl,String prov) {
        Map<String, Object> map = new HashMap<>();
        map.put("tahun", tahun);
        map.put("periode", periode);
        map.put("namaTbl",namaTbl.toString().trim());
        map.put("prpov",prov);
       RawData list = null;
        session = sqlSessionFactory.openSession();
        try {

            if(jenis == 1){
            
            list = session.selectOne("DwMapper.selectCountVwLra", map);
            }else  if(jenis == 2){
            
            list = session.selectOne("DwMapper.selectCountVwNeraca", map);
            }else  if(jenis == 3){
            
            list = session.selectOne("DwMapper.selectCountVwLo", map);
            }
            else  if(jenis == 4){
            
            list = session.selectOne("DwMapper.selectCountVwLak", map);
            }
            else  if(jenis == 5){
            
            list = session.selectOne("DwMapper.selectCountVwLpe", map);
            }
            else  if(jenis == 6){
            
            list = session.selectOne("DwMapper.selectCountVwLpsal", map);
            }
            

        } finally {
            session.close();
        }
        return list;
    }
        
 public RawDataLpe findCountByParamLpe(int tahun,String periode,int jenis,String namaTbl,String prov) {
        Map<String, Object> map = new HashMap<>();
        map.put("tahun", tahun);
        map.put("periode", periode);
        map.put("namaTbl",namaTbl);
        map.put("prpov",prov);
       RawDataLpe list = null;
        session = sqlSessionFactory.openSession();
        try {

            if(jenis == 1){
            
            list = session.selectOne("DwMapper.selectCountVwLra", map);
            }else  if(jenis == 2){
            
            list = session.selectOne("DwMapper.selectCountVwNeraca", map);
            }else  if(jenis == 3){
            
            list = session.selectOne("DwMapper.selectCountVwLo", map);
            }
            else  if(jenis == 4){
            
            list = session.selectOne("DwMapper.selectCountVwLak", map);
            }
            else  if(jenis == 5){
            
            list = session.selectOne("DwMapper.selectCountVwLpe", map);
            }
            else  if(jenis == 6){
            
            list = session.selectOne("DwMapper.selectCountVwLpsal", map);
            }
            

        } finally {
            session.close();
        }
        return list;
    }

 public RawDataLak findCountByParamLak(int tahun,String periode,int jenis,String namaTbl,String prov) {
        Map<String, Object> map = new HashMap<>();
        map.put("tahun", tahun);
        map.put("periode", periode);
        map.put("namaTbl",namaTbl);
        map.put("prpov",prov);
       RawDataLak list = null;
        session = sqlSessionFactory.openSession();
        try {

            if(jenis == 1){
            
            list = session.selectOne("DwMapper.selectCountVwLra", map);
            }else  if(jenis == 2){
            
            list = session.selectOne("DwMapper.selectCountVwNeraca", map);
            }else  if(jenis == 3){
            
            list = session.selectOne("DwMapper.selectCountVwLo", map);
            }
            else  if(jenis == 4){
            
            list = session.selectOne("DwMapper.selectCountVwLak", map);
            }
            else  if(jenis == 5){
            
            list = session.selectOne("DwMapper.selectCountVwLpe", map);
            }
            else  if(jenis == 6){
            
            list = session.selectOne("DwMapper.selectCountVwLpsal", map);
            }
            

        } finally {
            session.close();
        }
        return list;
    }
 
 
 public RawDataLpsal findCountByParamLpsal(int tahun,String periode,int jenis,String namaTbl,String prov) {
        Map<String, Object> map = new HashMap<>();
        map.put("tahun", tahun);
        map.put("periode", periode);
       map.put("namaTbl",namaTbl);
        map.put("prpov",prov);
        RawDataLpsal list = null;
        session = sqlSessionFactory.openSession();
        try {

            if(jenis == 1){
            
            list = session.selectOne("DwMapper.selectCountVwLra", map);
            }else  if(jenis == 2){
            
            list = session.selectOne("DwMapper.selectCountVwNeraca", map);
            }else  if(jenis == 3){
            
            list = session.selectOne("DwMapper.selectCountVwLo", map);
            }
            else  if(jenis == 4){
            
            list = session.selectOne("DwMapper.selectCountVwLak", map);
            }
            else  if(jenis == 5){
            
            list = session.selectOne("DwMapper.selectCountVwLpe", map);
            }
            else  if(jenis == 6){
            
            list = session.selectOne("DwMapper.selectCountVwLpsal", map);
            }
            

        } finally {
            session.close();
        }
        return list;
    }
 
 
 
    public RawData findAllByPemdaCount() { 
     //   throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
     session = sqlSessionFactory.openSession();
        RawData obj = null;
        try {
            obj = session.selectOne("DwMapper.selectCountByPemdaCount");

        } finally {
            session.close();
        }
        return obj;
    
    }

    public HeaderDataKonsolidasi updateDW(HeaderDataKonsolidasi obj) {
    
        session = sqlSessionFactory.openSession();
        session.update("HeaderDataKonsolidasiMapper.updateOneDw", obj);
        session.commit();
        session.close();
        return obj;
    }

    public List<RawData> findCountPemdaByParam(int tahun, String periode, int jenis,String namaTbl,String prov) {
     //   throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
         Map<String, Object> map = new HashMap<>();
        map.put("tahun", tahun);
        map.put("periode", periode);        
        map.put("namaTbl",namaTbl.toString().trim());
        map.put("prpov",prov);
        
       List<RawData> list = null;
        session = sqlSessionFactory.openSession();
        try {

            if(jenis == 1){
            
            list = session.selectList("DwMapper.selectCountVwLraPemda", map);
            }else  if(jenis == 2){
            
            list = session.selectList("DwMapper.selectCountVwNeracaPemda", map);
            }else  if(jenis == 3){
            
            list = session.selectList("DwMapper.selectCountVwLoPemda", map);
            }            

        } finally {
            session.close();
        }
        return list;
    
    
    }

     public List<RawDataLpe> findCountPemdaByParamLpe(int tahun, String periode, int jenis,String namaTbl,String prov) {
     //   throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
         Map<String, Object> map = new HashMap<>();
        map.put("tahun", tahun);
        map.put("periode", periode);        
        map.put("namaTbl",namaTbl.toString().trim());
        map.put("prpov",prov);
        
       List<RawDataLpe> list = null;
        session = sqlSessionFactory.openSession();
        try {

            list = session.selectList("DwMapper.selectCountVwLpePemda", map);
           

        } finally {
            session.close();
        }
        return list;
    
    
    }


 public List<RawDataLpsal> findCountPemdaByParamLpsal(int tahun, String periode, int jenis,String namaTbl,String prov) {
     //   throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
         Map<String, Object> map = new HashMap<>();
        map.put("tahun", tahun);
        map.put("periode", periode);        
        map.put("namaTbl",namaTbl.toString().trim());
        map.put("prpov",prov);
        
       List<RawDataLpsal> list = null;
        session = sqlSessionFactory.openSession();
        try {

            list = session.selectList("DwMapper.selectCountVwLpsalPemda", map);
           

        } finally {
            session.close();
        }
        return list;
    
    
    }

     
    public List<RawDataLak> findCountPemdaByParamLak(int tahun, String periode, int jenisLap, String namaTbl, String prov) {
    
                Map<String, Object> map = new HashMap<>();
        map.put("tahun", tahun);
        map.put("periode", periode);        
        map.put("namaTbl",namaTbl.toString().trim());
        map.put("prpov",prov);
        
       List<RawDataLak> list = null;
        session = sqlSessionFactory.openSession();
        try {

            list = session.selectList("DwMapper.selectCountVwLakPemda", map);
           

        } finally {
            session.close();
        }
        return list;
     
    }

}

