package jp.agentec.sinaburocast.action.user;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.ParseException;

import javax.servlet.http.HttpServletResponse;

import jp.agentec.sinaburocast.action.AbstractAction;
import jp.agentec.sinaburocast.common.annotation.NoLogin;
import jp.agentec.sinaburocast.entity.Postalcode;
import jp.agentec.sinaburocast.form.user.WebServiceForm;
import jp.agentec.sinaburocast.service.PostalcodeService;
import net.arnx.jsonic.JSON;

import org.seasar.struts.annotation.ActionForm;
import org.seasar.struts.annotation.Execute;
import org.seasar.struts.util.ResponseUtil;

/**
 * WEBサービス
 *
 * @author arakawa
 *
 */
@NoLogin
public class WebServiceAction extends AbstractAction {

	@ActionForm
	public WebServiceForm webServiceForm;

	public PostalcodeService postalcodeService;


	/**
	 * 住所検索
	 *
	 * @return JSONで住所情報を返却する
	 * @throws ParseException
	 */
	@Execute(validator = false)
	public String addrSearch() throws IOException{

		Postalcode postalcode = postalcodeService.findAddressByZipCode(webServiceForm.zipCode);

		String text = JSON.encode(postalcode);

		HttpServletResponse httpServletResponse = ResponseUtil.getResponse();
		httpServletResponse.setContentType("application/json; charset=UTF-8");

		PrintWriter sendAddress = new PrintWriter
				(new OutputStreamWriter(httpServletResponse.getOutputStream(), "UTF8"), true);

		sendAddress.println(text);

		sendAddress.flush();
		sendAddress.close();

		return null;
	}
}