
/*
 * Created on Dec 16, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package gov.va.med.esr.ui.ee.action;

import java.util.List;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import gov.va.med.fw.util.StringUtils;

import gov.va.med.esr.common.model.lookup.POWCampLocation;

import gov.va.med.esr.ui.common.service.LookupCacheService;
import gov.va.med.esr.ui.common.action.AbstractAction;


/**
 * @author DNS   CHENB
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class PowCampCodeLookupAction extends AbstractAction {
    public static final String CAMP_CODE 		= "campCode";
    public static final String ROOT_ELEMENT_START 	= "<message>";
    public static final String ROOT_ELEMENT_END   	= "</message>";
    public static final String CODE_LOOKUP_START 	= "<codeLookup>";
    public static final String CODE_LOOKUP_END	 	= "</codeLookup>";
    public static final String CODE_START 			= "<code>";
    public static final String CODE_END 			= "</code>";
    public static final String INDEX_START 			= "<index>";
    public static final String INDEX_END 			= "</index>";
   
    public static final String NAME_START 			= "<name>";
    public static final String NAME_END 			= "</name>";
    public static final String CITY_START 			= "<city>";
    public static final String CITY_END 			= "</city>";



	/**
	 * 
	 */
	public PowCampCodeLookupAction() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	public ActionForward lookup(ActionMapping mapping, 
			ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response)
	throws Exception 
	{
        String param = request.getParameter(CAMP_CODE);
        int div = param.indexOf(":");
        String campCode = param.substring(0, div);
        String index = param.substring(div+1);
        
        StringBuffer xml = new StringBuffer();
        xml.append(ROOT_ELEMENT_START);

        if(StringUtils.isNotEmpty(campCode))
        {
        	POWCampLocation camp = (POWCampLocation)getLookupCacheService().getByCodeFromCache(
        			POWCampLocation.class, campCode); 
        	if (camp != null)
        	{
	            xml.append(CODE_LOOKUP_START);
	            xml.append(INDEX_START);
	            xml.append(index);
	            xml.append(INDEX_END);
	            xml.append(CODE_START);
	            xml.append(camp.getCode());
	            xml.append(CODE_END);
	            xml.append(NAME_START);
	            xml.append(camp.getName());
	            xml.append(NAME_END);
	            xml.append(CITY_START);
	            xml.append(camp.getCity());
	            xml.append(CITY_END);
	            xml.append(CODE_LOOKUP_END);
        	}
        }
		//Write the XML to response
        xml.append(ROOT_ELEMENT_END);

        response.setContentType("text/xml");
		response.setHeader("Cache-Control","no-cache");
		PrintWriter out = response.getWriter();
		out.println(xml.toString());
		out.flush();
		
		return null;
	}

	/* (non-Javadoc)
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	public void afterPropertiesSet() throws Exception {
		// TODO Auto-generated method stub

	}

}
