/*
 * 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.PerkiraanBelanja_WS;
import sikd.sikd5.sikd5ejb.session.PerkiraanBelanjaSessionBeanRemote;
import sikd.sikd5.sikd5ejb.session.login.LoginSessionBeanRemote;
import sikd.sikd5.sikd5service.util.XmlSikdUtil;

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

	@WebMethod(operationName = "ambilPerkiraanBelanjaInfo")
	@SuppressWarnings("null")
	public String ambilPerkiraanBelanjaInfo(@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");
			PerkiraanBelanjaSessionBeanRemote session = (PerkiraanBelanjaSessionBeanRemote) ctxOffice
					.lookup(SikdSessionUtil.PERKIRAAN_BELANJA_SESSION);
			result = session.getInfoPengiriman(kodeSatker, tahun, periode);
		} catch (Exception exc) {
			Logger.getLogger(PerkiraanBelanjaService.class.getName()).log(Level.SEVERE, null, exc);
			result = exc.getMessage();
		}
		return result;
	}

	@WebMethod(operationName = "inputPerkiraanBelanja")
	@SuppressWarnings("null")
	public String inputPerkiraanBelanja(@WebParam(name = "perkiraanBelanja") PerkiraanBelanja_WS perkiraanBelanja)
			throws SIKDServiceException {
		String result = "Format salah";
		try {
			Context ctxOffice = ServerUtil.getOfficeContextV3();
			PerkiraanBelanjaSessionBeanRemote session = (PerkiraanBelanjaSessionBeanRemote) ctxOffice
					.lookup(SikdSessionUtil.PERKIRAAN_BELANJA_SESSION);
			result = "Format Benar.. data belum masuk, untuk kirim data silahkan menggunakan method kirimPerkiraanBelanjaXmlZip";
//            session.insert(perkiraanBelanja, nmfile);
		} catch (Exception e) {
			result = e.getMessage();
		}
		return result;
	}

	@WebMethod(operationName = "inputPerkiraanBelanjaPdf")
	@SuppressWarnings("null")
	public String inputPerkiraanBelanjaPdf(
			@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 = "berkasPerkiraanBelanja") byte[] berkasPerkiraanBelanja) 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 = "PBB";
			String uploadPath = ((Properties) PropertiesLoader.loadProperties("sikd.properties"))
					.getProperty("uploadPath");
			XmlSikdUtil.cekBerkasPdf(berkasPerkiraanBelanja);
			String filename = XmlSikdUtil.copyInsertPdf(uploadPath, kdikd, bulan, kodeSatker, tahun, berkasPerkiraanBelanja);
			
			result = "Berhasil";
			return result;
		} catch (Exception e) {
//			e.printStackTrace();
			result = e.getMessage();
			throw new SIKDServiceException(result);
		}
		
	}

	@WebMethod(operationName = "kirimPerkiraanBelanjaXmlZip")
	@SuppressWarnings("null")
	public String kirimPerkiraanBelanjaXmlZip(@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 = "berkasPerkiraanBelanja") byte[] berkasPerkiraanBelanja) throws SIKDServiceException {
		String hasil = "Gagal";
		try {
			XmlSikdUtil.cekParamSimpatik(kodeSatker, kodePemda, tahun, bulan, userName, password);
			XmlSikdUtil.cekBerkasZip(berkasPerkiraanBelanja);
			String kdikd = "PBB";
			String uploadPath = ((Properties) PropertiesLoader.loadProperties("sikd.properties"))
					.getProperty("uploadPath");
			String[] filenya = XmlSikdUtil.simpanExtract(uploadPath, kdikd,bulan, kodeSatker, tahun, berkasPerkiraanBelanja,
					password);
			Object obj = XmlSikdUtil.parsingXML(filenya[1], kdikd);
			PerkiraanBelanja_WS perkiraanBelanja = (PerkiraanBelanja_WS) obj;
			Context ctxOffice = ServerUtil.getOfficeContextV3();
			PerkiraanBelanjaSessionBeanRemote session = (PerkiraanBelanjaSessionBeanRemote) ctxOffice
					.lookup(SikdSessionUtil.PERKIRAAN_BELANJA_SESSION);
			LoginSessionBeanRemote sessionLogin = (LoginSessionBeanRemote) ctxOffice
					.lookup(SikdSessionUtil.LOGIN_SESSION);
			TUserAccount tuserAccount = sessionLogin.getUserAccountbyName(userName);
			XmlSikdUtil.cekLoginService(kodeSatker, kodePemda, userName, password, tuserAccount);
			session.insert(perkiraanBelanja, filenya[0]);
			hasil = "Berhasil";
			return hasil;
		} catch (Exception ex) {
			ex.printStackTrace();
			hasil = ex.getMessage();
			throw new SIKDServiceException(ex.getMessage());
		}
		
	}

}
