/*
p * 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.sikd5.sikd5service;

import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.naming.Context;

import sikd.sikd5.sikd5bean.fault.SIKDServiceException;
import sikd.sikd5.sikd5bean.tabel.mgr.TUserAccount;
import sikd.sikd5.sikd5bean.util.PropertiesLoader;
import sikd.sikd5.sikd5bean.util.ServerUtil;
import sikd.sikd5.sikd5bean.util.SikdSessionUtil;
import sikd.sikd5.sikd5bean.ws.simpatik.KontakPemda_WS;
import sikd.sikd5.sikd5bean.ws.simpatik.PerkiraanBelanja_WS;
import sikd.sikd5.sikd5ejb.session.KontakPemdaSessionBeanRemote;
import sikd.sikd5.sikd5ejb.session.PerkiraanBelanjaSessionBeanRemote;
import sikd.sikd5.sikd5ejb.session.UploadFileSessionBeanRemote;
import sikd.sikd5.sikd5ejb.session.login.LoginSessionBeanRemote;
import sikd.sikd5.sikd5service.util.XmlSikdUtil;

/**
 *
 * @author agus
 */
@WebService(serviceName = "KontakPemdaService")
@Stateless
public class KontakPemdaService {

	@WebMethod(operationName = "ambilKontakPemdaInfo")
//    @SuppressWarnings("null")
	public String ambilKontakPemdaInfo(@WebParam(name = "kodeSatker") String kodeSatker,
			@WebParam(name = "tahun") short tahun,
			@WebParam(name = "periode") short periode) throws SIKDServiceException {
		String result = "";
		try {
			Context ctxOffice = ServerUtil.getOfficeContextV3();
//           PajakServiceSessionBeanRemote apbdSession = (PajakServiceSessionBeanRemote)ctxOffice.lookup(SikdSessionUtil.PajakServiceSessionBean/remote");
			KontakPemdaSessionBeanRemote session = (KontakPemdaSessionBeanRemote) ctxOffice
					.lookup(SikdSessionUtil.KONTAK_PEMDA_SESSION);
			result = session.getInfoPengiriman(kodeSatker, tahun, periode);
		} catch (Exception exc) {
			Logger.getLogger(KontakPemdaService.class.getName()).log(Level.SEVERE, null, exc);
			result = exc.getMessage();
		}
		return result;
	}

	@WebMethod(operationName = "inputKontakPemda")
	@SuppressWarnings("null")
	public String inputKontakPemda(@WebParam(name = "kontakPemda") KontakPemda_WS kontakPemda)
			throws SIKDServiceException {
		String result = "Format salah";
		try {
			Context ctxOffice = ServerUtil.getOfficeContextV3();
			LoginSessionBeanRemote sessionLogin = (LoginSessionBeanRemote) ctxOffice
					.lookup(SikdSessionUtil.LOGIN_SESSION);
			TUserAccount tuserAccount = sessionLogin.getUserAccountbyName(kontakPemda.getKodeSatker());
			XmlSikdUtil.cekLoginService(kontakPemda.getKodeSatker(), kontakPemda.getKodePemda(),
					kontakPemda.getUserName(), kontakPemda.getPassword(), tuserAccount);
			KontakPemdaSessionBeanRemote session = (KontakPemdaSessionBeanRemote) ctxOffice
					.lookup(SikdSessionUtil.KONTAK_PEMDA_SESSION);
			String nmfile = "";
			long hasil = session.insert(kontakPemda, nmfile);
			if (hasil > 0) {
				result = "Berhasil input Kontak Pemda";
			} else {
				result = "Gagal input Kontak Pemda";

			}
			return result;
		} catch (Exception e) {
			result = e.getMessage();
			throw new SIKDServiceException("Gagal : " + e.getMessage());
		}

	}

	@WebMethod(operationName = "inputKontakPemdaPdf")
	@SuppressWarnings("null")
	public String inputKontakPemdaPdf(@WebParam(name = "kodeSatker") String kodeSatker,
			@WebParam(name = "kodePemda") String kodePemda,
			@WebParam(name = "tahun") short tahun,
			@WebParam(name = "bulan") short bulan,
			@WebParam(name = "userName") String userName,
			@WebParam(name = "password") String password,
			@WebParam(name = "berkasKontakPemda") byte[] berkasKontakPemda) throws SIKDServiceException {
		String result = "Gagal";
		try {
			XmlSikdUtil.cekParamSimpatik(kodeSatker, kodePemda, tahun, bulan, userName, password);
			Context ctxOffice = ServerUtil.getOfficeContextV3();
			LoginSessionBeanRemote sessionLogin = (LoginSessionBeanRemote) ctxOffice
					.lookup(SikdSessionUtil.LOGIN_SESSION);
			TUserAccount tuserAccount = sessionLogin.getUserAccountbyName(userName);
			XmlSikdUtil.cekLoginService(kodeSatker, kodePemda, userName, password, tuserAccount);
			String kdikd = "KON";
			String uploadPath = ((Properties) PropertiesLoader.loadProperties("sikd.properties"))
					.getProperty("uploadPath");
			XmlSikdUtil.cekBerkasPdf(berkasKontakPemda);
			String filename = XmlSikdUtil.copyInsertPdf(uploadPath, kdikd, bulan, kodeSatker, tahun, berkasKontakPemda);

			result = "Berhasil";
			return result;
		} catch (Exception e) {
			result = e.getMessage();
			throw new SIKDServiceException(result);
		}

	}

	@WebMethod(operationName = "kirimKontakPemdaXmlZip")
	@SuppressWarnings("null")
	public String kirimKontakPemdaXmlZip(@WebParam(name = "kodeSatker") String kodeSatker,
			@WebParam(name = "kodePemda") String kodePemda,
			@WebParam(name = "tahun") short tahun,
			@WebParam(name = "bulan") short bulan,
			@WebParam(name = "userName") String userName,
			@WebParam(name = "password") String password,
			@WebParam(name = "berkasKontakPemda") byte[] berkasKontakPemda) throws SIKDServiceException {
		String hasil = "Gagal";
		String fileNamex1 = "";
		String str = "";
		String kdikd = "KON";
		try {
			XmlSikdUtil.cekParamSimpatik(kodeSatker, kodePemda, tahun, bulan, userName, password);
			Context ctxOffice = ServerUtil.getOfficeContextV3();
			LoginSessionBeanRemote sessionLogin = (LoginSessionBeanRemote) ctxOffice
					.lookup(SikdSessionUtil.LOGIN_SESSION);
			TUserAccount tuserAccount = sessionLogin.getUserAccountbyName(userName);
			XmlSikdUtil.cekLoginService(kodeSatker, kodePemda, userName, password, tuserAccount);
			XmlSikdUtil.cekBerkasZip(berkasKontakPemda);
			String uploadPath = ((Properties) PropertiesLoader.loadProperties("sikd.properties"))
					.getProperty("uploadPath");
			String[] filenya = XmlSikdUtil.simpanExtract(uploadPath, kdikd, bulan, kodeSatker, tahun, berkasKontakPemda,
					password);
			Object obj = XmlSikdUtil.parsingXML(filenya[1], kdikd);
			KontakPemda_WS kontakPemda = (KontakPemda_WS) obj;
			KontakPemdaSessionBeanRemote session = (KontakPemdaSessionBeanRemote) ctxOffice
					.lookup(SikdSessionUtil.KONTAK_PEMDA_SESSION);
			session.insert(kontakPemda, filenya[0]);
			hasil = "Berhasil";
		} catch (Exception ex) {
			ex.printStackTrace();
			hasil = ex.getMessage();
			throw new SIKDServiceException(ex.getMessage());
		}
		return hasil;
	}

}
