/*
 * 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.sikd5bean.ws.simpatik.PosisiKas_WS;
import sikd.sikd5.sikd5ejb.session.PerkiraanBelanjaSessionBeanRemote;
import sikd.sikd5.sikd5ejb.session.PosisiKasSessionBeanRemote;
import sikd.sikd5.sikd5ejb.session.login.LoginSessionBeanRemote;
import sikd.sikd5.sikd5service.util.XmlSikdUtil;

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

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

	@WebMethod(operationName = "inputPosisiKas")
	@SuppressWarnings("null")
	public String inputPosisiKas(@WebParam(name = "posisiKas") PosisiKas_WS posisiKas) throws SIKDServiceException {
		String result = "Format salah";
		try {
			Context ctxOffice = ServerUtil.getOfficeContextV3();
			LoginSessionBeanRemote sessionLogin = (LoginSessionBeanRemote) ctxOffice
					.lookup(SikdSessionUtil.LOGIN_SESSION);
			TUserAccount tuserAccount = sessionLogin.getUserAccountbyName(posisiKas.getUserName());
			XmlSikdUtil.cekLoginService(posisiKas.getKodeSatker(), posisiKas.getKodePemda(), posisiKas.getUserName(), posisiKas.getPassword(), tuserAccount);
			result = "Format Benar.. data belum masuk, untuk kirim data silahkan menggunakan method kirimPosisiKasXmlZip";
		} catch (Exception e) {
			result = e.getMessage();
			throw new SIKDServiceException(result);
		}
		return result;
	}

	@WebMethod(operationName = "inputPosisiKasPdf")
//    @SuppressWarnings("null")
	public String inputPosisiKasPdf(@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 = "berkasPosisiKas") byte[] berkasPosisiKas)
			throws SIKDServiceException {
	
		String result = "Gagal";
		try {
			Context ctxOffice = ServerUtil.getOfficeContextV3();
			LoginSessionBeanRemote sessionLogin = (LoginSessionBeanRemote) ctxOffice
					.lookup(SikdSessionUtil.LOGIN_SESSION);
			XmlSikdUtil.cekParamSimpatik(kodeSatker, kodePemda, tahun, bulan, userName, password);
			TUserAccount tuserAccount = sessionLogin.getUserAccountbyName(userName);
			XmlSikdUtil.cekLoginService(kodeSatker, kodePemda, userName, password, tuserAccount);
			String kdikd = "PKB";
			String uploadPath = ((Properties) PropertiesLoader.loadProperties("sikd.properties"))
					.getProperty("uploadPath");
			XmlSikdUtil.cekBerkasPdf(berkasPosisiKas);
			String filename = XmlSikdUtil.copyInsertPdf(uploadPath, kdikd, bulan, kodeSatker, tahun, berkasPosisiKas);
			
			result = "Berhasil";
			return result;
		} catch (Exception e) {
			result = e.getMessage();
			throw new SIKDServiceException(result);
		}
	}

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