package com.jasamedika.medifirst2000.dao.custom.impl;

import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;

import com.jasamedika.medifirst2000.dao.custom.DetailSpekAnggaranDaoCustom;
import com.jasamedika.medifirst2000.dao.custom.base.impl.CoreDaoImpl;
import com.jasamedika.medifirst2000.entities.DetailSpekAnggaran;
import com.jasamedika.medifirst2000.util.CommonUtil;
import com.jasamedika.medifirst2000.util.DateUtil;

/**
 * Repository Custom class
 * 
 * @author Adik
 * @param <T>
 */
@Repository
public class DetailSpekAnggaranDaoCustomImpl extends CoreDaoImpl<DetailSpekAnggaran>
		implements DetailSpekAnggaranDaoCustom {

	public DetailSpekAnggaranDaoCustomImpl() {
		super(DetailSpekAnggaran.class);
	}

	@Override
	public List<Map<String, Object>> findList(int rowStart, int rowEnd, String sort, String dir, String dateStart,
			String dateEnd, Integer tahun, List<Integer> pengendali, List<Integer> ruanganId, String gridFor,
			Integer pengendaliId) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("select new map( ");
		buffer.append("coalesce(spekChild.noRec,'-') as noRec, ");
		buffer.append("coalesce(satuanStandar.satuanStandar,'-') as namaSatuan, ");
		buffer.append("coalesce(spekChild.isVerifikasi,null) as isVerifikasi , ");
		buffer.append("coalesce(detail.noRec,'-') as noRecDetail, ");
		buffer.append("coalesce(kegiatanAnggaran.noRec,'-') as noRecKegiatan, ");
		buffer.append("coalesce(kegiatan.namaKegiatan,'-') as kegiatan, ");
		buffer.append("coalesce(kegiatanDetail.namaKegiatanDetail,'-') as kegiatanDetail, ");
		buffer.append("coalesce(head.tahun,0) as tahunAnggaran, ");
		buffer.append("coalesce(komponen.namaKomponen,'') as namaKomponen, ");
		buffer.append("coalesce(asalProduk.asalProduk,'') as sumberDana, ");
		buffer.append("coalesce(akun.namaMataAnggaran,'-') as namaAkun, ");
		buffer.append("coalesce(akun.kodeMataAnggaran,0) as kodeAkun, ");
		buffer.append("coalesce(detail.tanggalPengajuan,null) as tanggalPengajuan, ");
		buffer.append("coalesce(spekChild.volumeBarang,0) as volume, ");
		buffer.append("coalesce(spekChild.hargaSatuanBarang,0.0) as hargaSatuan, ");
		buffer.append("coalesce(spekParent.spesifikasi,'-') as spesifikasi, ");
		buffer.append("coalesce(produk.namaProduk,'-') as namaProduk, ");
		buffer.append("coalesce(spekChild.isVerifikasi,'-') as isVerifikasi ) ");
		buffer.append("from DetailSpekAnggaran spekChild ");
		buffer.append("join spekChild.detailAnggaran detail ");
		buffer.append(" join spekChild.detailSpekAnggaran spekParent ");
		buffer.append("join detail.kegiatanAnggaran kegiatanAnggaran ");
		buffer.append("join kegiatanAnggaran.kegiatan kegiatan ");
		buffer.append("left join kegiatanAnggaran.kegiatanDetail kegiatanDetail ");
		buffer.append("join kegiatanAnggaran.headAnggaran head ");
		buffer.append("left join detail.komponen komponen ");
		buffer.append("left join detail.asalProduk asalProduk ");
		buffer.append("left join detail.akun akun ");
		buffer.append("left join spekChild.produk produk ");
		buffer.append("left join produk.satuanStandar satuanStandar ");
		buffer.append("where spekChild.detailAnggaran is not null ");
		buffer.append("and spekChild.isActive=true ");

		// cek jika request dari ruangan
		if (CommonUtil.isNotNullOrEmpty(ruanganId) && (gridFor.equalsIgnoreCase("ruangan"))) {
			buffer.append(" and head.ruanganId in (:ruanganId) ");
		} else if (CommonUtil.isNotNullOrEmpty(pengendali) && (gridFor.equalsIgnoreCase("pengendali"))) {
			buffer.append("and spekChild.pengendali.id in (:pengendali) ");
		} else {
			buffer.append("and spekChild.pengendali.id is null ");
		}

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			buffer.append("and date(detail.tanggalPengajuan) between :dateStart and :dateEnd ");
		}
		if (CommonUtil.isNotNullOrEmpty(pengendaliId)) {
			buffer.append("and spekChild.pengendali.id in (:pengendaliId) ");
		}

		if (CommonUtil.isNotNullOrEmpty(tahun)) {
			buffer.append("and head.tahun =:tahun ");
		}

		if (sort != "") {
			buffer.append("order by spekChild." + sort + " " + dir);
		} else {
			buffer.append("order by spekChild.noRec desc ");
		}
		Query query = em.createQuery(buffer.toString());
		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			query.setParameter("dateStart", DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}

		if (CommonUtil.isNotNullOrEmpty(pengendali) && (gridFor.equalsIgnoreCase("pengendali"))) {
			query.setParameter("pengendali", pengendali);
		}

		if (CommonUtil.isNotNullOrEmpty(ruanganId) && (gridFor.equalsIgnoreCase("ruangan"))) {
			query.setParameter("ruanganId", ruanganId);
		}
		if (CommonUtil.isNotNullOrEmpty(pengendaliId)) {
			query.setParameter("pengendaliId", pengendaliId);
		}

		if (CommonUtil.isNotNullOrEmpty(tahun)) {
			query.setParameter("tahun", tahun);
		}
		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);
		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@Override
	public int findCount(String dateStart, String dateEnd, Integer tahun, List<Integer> pengendali,
			List<Integer> ruanganId, String gridFor, Integer pengendaliId) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("select count(spekChild.noRec) ");
		buffer.append("from DetailSpekAnggaran spekChild ");
		buffer.append("join spekChild.detailAnggaran detail ");
		buffer.append(" join spekChild.detailSpekAnggaran spekParent ");
		buffer.append("join detail.kegiatanAnggaran kegiatanAnggaran ");
		buffer.append("join kegiatanAnggaran.kegiatan kegiatan ");
		buffer.append("left join kegiatanAnggaran.kegiatanDetail kegiatanDetail ");
		buffer.append("join kegiatanAnggaran.headAnggaran head ");
		buffer.append("left join detail.komponen komponen ");
		buffer.append("left join detail.asalProduk asalProduk ");
		buffer.append("left join detail.akun akun ");
		buffer.append(
				"left join spekChild.produk produk where spekChild.detailAnggaran is not null and spekChild.isActive=true ");
		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			buffer.append("and date(detail.tanggalPengajuan) between :dateStart and :dateEnd ");
		}

		// cek jika request dari ruangan
		if (CommonUtil.isNotNullOrEmpty(ruanganId) && (gridFor.equalsIgnoreCase("ruangan"))) {
			buffer.append(" and head.ruanganId in (:ruanganId) ");
		} else if (CommonUtil.isNotNullOrEmpty(pengendali) && (gridFor.equalsIgnoreCase("pengendali"))) {
			buffer.append("and spekChild.pengendali.id in (:pengendali) ");
		} else {
			buffer.append("and spekChild.pengendali.id is null ");
		}

		if (CommonUtil.isNotNullOrEmpty(tahun)) {
			buffer.append("and head.tahun =:tahun ");
		}

		if (CommonUtil.isNotNullOrEmpty(pengendaliId)) {
			buffer.append("and spekChild.pengendali.id in (:pengendaliId) ");
		}

		Query query = em.createQuery(buffer.toString());
		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			query.setParameter("dateStart", DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}

		if (CommonUtil.isNotNullOrEmpty(pengendaliId)) {
			query.setParameter("pengendaliId", pengendaliId);
		}

		if (CommonUtil.isNotNullOrEmpty(pengendali) && (gridFor.equalsIgnoreCase("pengendali"))) {
			query.setParameter("pengendali", pengendali);
		}

		if (CommonUtil.isNotNullOrEmpty(tahun)) {
			query.setParameter("tahun", tahun);
		}

		if (CommonUtil.isNotNullOrEmpty(ruanganId) && (gridFor.equalsIgnoreCase("ruangan"))) {
			query.setParameter("ruanganId", ruanganId);
		}
		return ((Long) query.getSingleResult()).intValue();
	}

	@Override
	public List<Map<String, Object>> getJenisProduk() {

		StringBuffer buffer = new StringBuffer();
		buffer.append("select new map( ");
		buffer.append("coalesce(spekChild.noRec,'-') as noRec, ");
		buffer.append("coalesce(satuanStandar.satuanStandar,'-') as namaSatuan, ");
		buffer.append("coalesce(spekChild.isVerifikasi,null) as isVerifikasi , ");
		buffer.append("coalesce(detail.noRec,'-') as noRecDetail, ");
		buffer.append("coalesce(kegiatanAnggaran.noRec,'-') as noRecKegiatan, ");
		buffer.append("coalesce(kegiatan.namaKegiatan,'-') as kegiatan, ");
		buffer.append("coalesce(kegiatanDetail.namaKegiatanDetail,'-') as kegiatanDetail, ");
		buffer.append("coalesce(head.tahun,0) as tahunAnggaran, ");
		buffer.append("coalesce(komponen.namaKomponen,'') as namaKomponen, ");
		buffer.append("coalesce(asalProduk.asalProduk,'') as sumberDana, ");
		buffer.append("coalesce(akun.namaMataAnggaran,'-') as namaAkun, ");
		buffer.append("coalesce(akun.kodeMataAnggaran,0) as kodeAkun, ");
		buffer.append("coalesce(detail.tanggalPengajuan,null) as tanggalPengajuan, ");
		buffer.append("coalesce(spekChild.volumeBarang,0) as volume, ");
		buffer.append("coalesce(spekChild.hargaSatuanBarang,0.0) as hargaSatuan, ");
		buffer.append("coalesce(spekParent.spesifikasi,'-') as spesifikasi, ");
		buffer.append("coalesce(produk.namaProduk,'-') as namaProduk, ");
		buffer.append("coalesce(spekChild.isVerifikasi,'-') as isVerifikasi ) ");
		buffer.append("from DetailSpekAnggaran spekChild ");
		buffer.append("join spekChild.detailAnggaran detail ");
		buffer.append(" join spekChild.detailSpekAnggaran spekParent ");
		buffer.append("join detail.kegiatanAnggaran kegiatanAnggaran ");
		buffer.append("join kegiatanAnggaran.kegiatan kegiatan ");
		buffer.append("left join kegiatanAnggaran.kegiatanDetail kegiatanDetail ");
		buffer.append("join kegiatanAnggaran.headAnggaran head ");
		buffer.append("left join detail.komponen komponen ");
		buffer.append("left join detail.asalProduk asalProduk ");
		buffer.append("left join detail.akun akun ");
		buffer.append("left join spekChild.produk produk ");
		buffer.append("left join produk.satuanStandar satuanStandar ");
		buffer.append("where spekChild.detailAnggaran is not null ");
		buffer.append("and spekChild.isActive=true ");

		Query query = em.createQuery(buffer.toString());

		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	// add by Arya 2017-01-19
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> findPengajuanUsulanAnggaranByNoRec(String noRec) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(
				"select new map(dsa.noRec as noRec,ha.noRec as noRecHeadAnggaran,ka.noRec as noRecKegiatanAnggaran,da.noRec as noRecDetailAnggaran,spek.noRec as noRecSpek,"
						+ "ha.tahun as tahun,ha.kdHeadAnggaran as kdHeadAnggaran,rgn.id as ruanganId,rgn.namaRuangan as namaRuangan,"
						+ "kl.id as kemenLembId,kl.namaKemenLemb as namaKemenLemb,uk.id as unitKerjaId,uk.namaExternal as namaUnitKerja,"
						+ "uo.id as unitOrgId,uo.namaUnitOrg as namaUnitOrg,pgm.id as programId,pgm.namaProgram as namaProgram,"
						+ "kgt.id as kegiatanId,kgt.namaKegiatan as namaKegiatan,kd.id as kegiatanDetailId,"
						+ "kd.namaKegiatanDetail as namaKegiatanDetail,out.id as outputId,out.namaOutput as namaOutput,komp.id as komponenId,komp.namaKomponen as namaKomponen,"
						+ "jb.id as jenisBelanjaId,jb.namaJenisBelanja as namaJenisBelanja,jp.id as jenisPengadaanId,jp.namaJenisPengadaan as namaJenisPengadaan,"
						+ "akun.id as mataAnggaranId,akun.namaMataAnggaran as namaMataAnggaran,ap.id as asalProdukId,ap.asalProduk as asalProduk,"
						+ "dsa.pengendali.id as pengendaliId) from DetailSpekAnggaran dsa join dsa.detailAnggaran da join da.kegiatanAnggaran ka "
						+ "join ka.headAnggaran ha left join ka.kegiatan kgt left join ha.program pgm left join ha.unitKerja uk "
						+ "left join ha.kemenLemb kl left join ha.unitOrganisasi uo left join ha.ruangan rgn left join dsa.pengendali pgdl "
						+ "left join ka.kegiatanDetail kd left join ka.output out left join dsa.jenisBelanja jb left join dsa.jenisPengadaan jp "
						+ "left join da.akun akun left join da.asalProduk ap left join da.komponen komp left join dsa.detailSpekAnggaran spek "
						+ "left join dsa.produk pdk where ");

		if (CommonUtil.isNotNullOrEmpty(noRec)) {
			buffer.append("dsa.noRec=:noRec ");
		}

		Query query = em.createQuery(buffer.toString());

		if (CommonUtil.isNotNullOrEmpty(noRec)) {
			query.setParameter("noRec", noRec);
		}
		List<Map<String, Object>> list = query.getResultList();

		return list;
	}
	// end add by Arya 2017-01-19

	// add by Arya 2017-01-25
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> findSpesifikasiByNoRec(String noRec) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(
				"select new map(spek.noRec as noRecSpek,dsa.noRec as noRec,pdk.id as produkId,pdk.namaProduk as namaProduk,ss.id as satuanStandarId,"
						+ "coalesce(dsa.hargaSatuanBarang,0) as hargaSatuanBarang,"
						+ "coalesce(dsa.volumeBarang,0) as volumeBarang,coalesce(dsa.totalHargaBarang,0) as totalHargaBarang,"
						+ "dsa.tipe as tipe) from DetailSpekAnggaran dsa "
						+ "left join dsa.detailSpekAnggaran spek left join dsa.produk pdk left join dsa.produk.satuanStandar ss where ");

		if (CommonUtil.isNotNullOrEmpty(noRec)) {
			buffer.append("spek.noRec=:noRec ");
		}

		Query query = em.createQuery(buffer.toString());

		if (CommonUtil.isNotNullOrEmpty(noRec)) {
			query.setParameter("noRec", noRec);
		}
		List<Map<String, Object>> list = query.getResultList();

		return list;
	}
	// end add by Arya 2017-01-25

	// add by Arya 2017-01-26
	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> findNamaPaketUsulanByNoRec(String noRec) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("select new map(dsa.noRec as noRec,dsa.spesifikasi as namaPaketUsulan) "
				+ "from DetailSpekAnggaran dsa left join dsa.detailSpekAnggaran spek where ");

		if (CommonUtil.isNotNullOrEmpty(noRec)) {
			buffer.append("dsa.noRec=:noRec ");
		}

		Query query = em.createQuery(buffer.toString());

		if (CommonUtil.isNotNullOrEmpty(noRec)) {
			query.setParameter("noRec", noRec);
		}
		List<Map<String, Object>> list = query.getResultList();

		return list;
	}
	// end add by Arya 2017-01-26

	@Override
	public int findCountDetailAnggaran(String dateStart, String dateEnd, Integer tahun, List<Integer> pengendali,
			Integer pengendaliId) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("select count(spekChild.noRec) ");
		buffer.append("from DetailSpekAnggaran spekChild ");
		buffer.append("join spekChild.detailAnggaran detail ");
		buffer.append(" join spekChild.detailSpekAnggaran spekParent ");
		buffer.append("join detail.kegiatanAnggaran kegiatanAnggaran ");
		buffer.append("join kegiatanAnggaran.kegiatan kegiatan ");
		buffer.append("left join kegiatanAnggaran.kegiatanDetail kegiatanDetail ");
		buffer.append("join kegiatanAnggaran.headAnggaran head ");
		buffer.append("left join detail.komponen komponen ");
		buffer.append("left join detail.asalProduk asalProduk ");
		buffer.append("left join detail.akun akun ");
		buffer.append(
				"left join spekChild.produk produk where spekChild.detailAnggaran is not null and spekChild.isActive=true ");
		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			buffer.append("and date(detail.tanggalPengajuan) between :dateStart and :dateEnd ");
		}

		// cek jika request dari ruangan
		// if (CommonUtil.isNotNullOrEmpty(ruanganId) &&
		// (gridFor.equalsIgnoreCase("ruangan"))) {
		// buffer.append(" and head.ruanganId in (:ruanganId) ");
		// } else if (CommonUtil.isNotNullOrEmpty(pengendali) &&
		// (gridFor.equalsIgnoreCase("pengendali"))) {
		// buffer.append("and spekChild.pengendali.id in (:pengendali) ");
		// } else {
		// buffer.append("and spekChild.pengendali.id is null ");
		// }

		if (CommonUtil.isNotNullOrEmpty(tahun)) {
			buffer.append("and head.tahun =:tahun ");
		}

		if (CommonUtil.isNotNullOrEmpty(pengendaliId)) {
			buffer.append("and spekChild.pengendali.id in (:pengendaliId) ");
		}

		Query query = em.createQuery(buffer.toString());
		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			query.setParameter("dateStart", DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}

		if (CommonUtil.isNotNullOrEmpty(pengendaliId)) {
			query.setParameter("pengendaliId", pengendaliId);
		}

		// if (CommonUtil.isNotNullOrEmpty(pengendali) &&
		// (gridFor.equalsIgnoreCase("pengendali"))) {
		// query.setParameter("pengendali", pengendali);
		// }

		if (CommonUtil.isNotNullOrEmpty(pengendali)) {
			query.setParameter("pengendali", pengendali);
		}

		if (CommonUtil.isNotNullOrEmpty(tahun)) {
			query.setParameter("tahun", tahun);
		}

		// if (CommonUtil.isNotNullOrEmpty(ruanganId) &&
		// (gridFor.equalsIgnoreCase("ruangan"))) {
		// query.setParameter("ruanganId", ruanganId);
		// }
		return ((Long) query.getSingleResult()).intValue();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getDetailAnggaran(int rowStart, int rowEnd, String sort, String dir,
			String dateStart, String dateEnd, Integer tahun, List<Integer> pengendali, Integer pengendaliId) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("select new map( ");
		buffer.append("coalesce(spekChild.noRec,'-') as noRec, ");
		buffer.append("coalesce(spekParent.noRec,'-') as noRecSpekParent, ");
		buffer.append("coalesce(satuanStandar.satuanStandar,'-') as namaSatuan, ");
		buffer.append("coalesce(spekChild.isVerifikasi,null) as isVerifikasi , ");
		buffer.append("coalesce(detail.noRec,'-') as noRecDetail, ");
		buffer.append("coalesce(kegiatanAnggaran.noRec,'-') as noRecKegiatan, ");
		buffer.append("coalesce(kegiatan.namaKegiatan,'-') as kegiatan, ");
		buffer.append("coalesce(kegiatanDetail.namaKegiatanDetail,'-') as kegiatanDetail, ");
		buffer.append("coalesce(head.tahun,0) as tahunAnggaran, ");
		buffer.append("coalesce(komponen.namaKomponen,'') as namaKomponen, ");
		buffer.append("coalesce(asalProduk.asalProduk,'') as sumberDana, ");
		buffer.append("coalesce(akun.namaMataAnggaran,'-') as namaAkun, ");
		buffer.append("coalesce(akun.kodeMataAnggaran,0) as kodeAkun, ");
		buffer.append("coalesce(detail.tanggalPengajuan,null) as tanggalPengajuan, ");
		buffer.append("coalesce(spekChild.volumeBarang,0) as volume, ");
		buffer.append("coalesce(spekChild.hargaSatuanBarang,0.0) as hargaSatuan, ");
		buffer.append("coalesce(spekParent.spesifikasi,'-') as spesifikasi, ");
		buffer.append("coalesce(produk.namaProduk,'-') as namaProduk, ");
		buffer.append("coalesce(spekChild.isVerifikasi,'-') as isVerifikasi ) ");
		buffer.append("from DetailSpekAnggaran spekChild ");
		buffer.append("join spekChild.detailAnggaran detail ");
		buffer.append(" join spekChild.detailSpekAnggaran spekParent ");
		buffer.append("join detail.kegiatanAnggaran kegiatanAnggaran ");
		buffer.append("join kegiatanAnggaran.kegiatan kegiatan ");
		buffer.append("left join kegiatanAnggaran.kegiatanDetail kegiatanDetail ");
		buffer.append("join kegiatanAnggaran.headAnggaran head ");
		buffer.append("left join detail.komponen komponen ");
		buffer.append("left join detail.asalProduk asalProduk ");
		buffer.append("left join detail.akun akun ");
		buffer.append("left join spekChild.produk produk ");
		buffer.append("left join produk.satuanStandar satuanStandar ");
		buffer.append("where spekChild.detailAnggaran is not null ");
		buffer.append("and spekChild.isActive=true ");

		// cek jika request dari ruangan
		// if (CommonUtil.isNotNullOrEmpty(ruanganId) &&
		// (gridFor.equalsIgnoreCase("ruangan"))) {
		// buffer.append(" and head.ruanganId in (:ruanganId) ");
		// } else if (CommonUtil.isNotNullOrEmpty(pengendali) &&
		// (gridFor.equalsIgnoreCase("pengendali"))) {
		// buffer.append("and spekChild.pengendali.id in (:pengendali) ");
		// } else {
		// buffer.append("and spekChild.pengendali.id is null ");
		// }

		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			buffer.append("and date(detail.tanggalPengajuan) between :dateStart and :dateEnd ");
		}
		if (CommonUtil.isNotNullOrEmpty(pengendaliId)) {
			buffer.append("and spekChild.pengendali.id in (:pengendaliId) ");
		}

		if (CommonUtil.isNotNullOrEmpty(tahun)) {
			buffer.append("and head.tahun =:tahun ");
		}

		if (sort != "") {
			buffer.append("order by spekChild." + sort + " " + dir);
		} else {
			buffer.append("order by spekChild.noRec desc ");
		}
		Query query = em.createQuery(buffer.toString());
		if (CommonUtil.isNotNullOrEmpty(dateStart) && CommonUtil.isNotNullOrEmpty(dateEnd)) {
			query.setParameter("dateStart", DateUtil.toDate(dateStart));
			query.setParameter("dateEnd", DateUtil.toDate(dateEnd));
		}

		// if (CommonUtil.isNotNullOrEmpty(pengendali) &&
		// (gridFor.equalsIgnoreCase("pengendali"))) {
		// query.setParameter("pengendali", pengendali);
		// }

		if (CommonUtil.isNotNullOrEmpty(pengendali)) {
			query.setParameter("pengendali", pengendali);
		}

		// if (CommonUtil.isNotNullOrEmpty(ruanganId) &&
		// (gridFor.equalsIgnoreCase("ruangan"))) {
		// query.setParameter("ruanganId", ruanganId);
		// }
		if (CommonUtil.isNotNullOrEmpty(pengendaliId)) {
			query.setParameter("pengendaliId", pengendaliId);
		}

		if (CommonUtil.isNotNullOrEmpty(tahun)) {
			query.setParameter("tahun", tahun);
		}
		query.setFirstResult(rowStart);
		query.setMaxResults(rowEnd);
		List<Map<String, Object>> list = query.getResultList();

		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Map<String, Object>> getPengendali(String noRec) {

		StringBuffer buffer = new StringBuffer();
		buffer.append(
				"select distinct new map(spek.noRec as noRec,pgdl.id as pengendaliId,pgdl.namaPengendali as namaPengendali) "
						+ "from DetailSpekAnggaran dsa left join dsa.detailSpekAnggaran spek left join dsa.pengendali pgdl ");

		if (CommonUtil.isNotNullOrEmpty(noRec)) {
			buffer.append("where spek.noRec=:noRec ");
		}

		Query query = em.createQuery(buffer.toString());

		if (CommonUtil.isNotNullOrEmpty(noRec)) {
			query.setParameter("noRec", noRec);
		}
		List<Map<String, Object>> list = query.getResultList();

		return list;
	}
}
