package q02b.cuestionariosVTMAnu.action;



import java.util.ArrayList;

import java.util.HashMap;



import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;



import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;



import q02b.exe.cuestionariosVTMAnu.valueobjects.Q02bCuestionarioVTMAnu;

import q02b.exe.utils.Q02bVoUtils;

import es.ejie.frmk.infraestructura.traza.Q70TraceLevel;

import es.ejie.frmk.infraestructura.traza.Q70Traza;

import es.ejie.frmk.presentacion.operacion.paso.Q70ActionPasoOP;

import es.ejie.frmk.presentacion.sesiondatos.Q70ContextoEJIE;





/**

 * @version 1.0

 * Fecha: 08-mar-04

 */

public class Q02bActionSeleccionarCuestionarioVTMAnu extends Q70ActionPasoOP {

	private static Q70Traza tr = Q70Traza.getSingleton();

	private static final String COMPONENTE = "ACTION PASOOP";



	/**

	 * @see es.ejie.frmk.presentacion.operacion.paso.ActionPasoOP#processOP(es.ejie.frmk.presentacion.sesiondatos.ContextoEJIE, org.apache.struts.action.ActionErrors, org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

	 */

	protected void processOP(

   

		Q70ContextoEJIE contexto,

		ActionErrors errores,

		ActionMapping mapping,

		ActionForm form,

		HttpServletRequest request,

		HttpServletResponse response)

		throws Exception {



		//Recogida de atributos del contexto de OP

		ArrayList listaCuestionariosVTMAnu = (ArrayList)contexto.get("listaCuestionariosVTMAnu");

		String niCuestionarioVTMAnuSeleccionado = (String)contexto.get("niCuesVTMAnu");

		String accion = (String)contexto.get("accion");

		

		

		//Búsqueda de cargo en la Lista de contactos

		Q02bCuestionarioVTMAnu cuestionarioVTMAnuBD = new Q02bCuestionarioVTMAnu();

		HashMap cuestionarioVTMAnu = new HashMap();

		int i = 0;

		boolean found = false; 

		if (accion != null && !accion.equals("KEY_ALTA")){ //baja o modificación

			//si ya existe no lo buscamos

			if (listaCuestionariosVTMAnu != null) {		

				while (!found && i<listaCuestionariosVTMAnu.size()){

					cuestionarioVTMAnuBD = (Q02bCuestionarioVTMAnu)listaCuestionariosVTMAnu.get(i);

					if ( (cuestionarioVTMAnuBD.getNiCuesVTMAnu().toString()).equals(((String)niCuestionarioVTMAnuSeleccionado)) ){

						found = true;

					}else{

						++i;

					}

				}

				//Deposita al contacto encontrado en el contexto de OP

				if (found){					

					try

					{						

						cuestionarioVTMAnu =  Q02bVoUtils.getHashMap(cuestionarioVTMAnuBD);	

						contexto.set("cuestionarioVTMAnu", cuestionarioVTMAnu);	

					}catch(Exception ex){

			            java.io.StringWriter sw = new java.io.StringWriter();

			            java.io.PrintWriter pw = new java.io.PrintWriter(sw);

			            ex.printStackTrace(pw);

			            if (tr.isTraceActive()){

							tr.trace(COMPONENTE, Q70TraceLevel.ERROR, this.getClass().getName() + ":Q02bActionSeleccionarCargo: " + sw.getBuffer ().toString());

						}			          

			            throw ex;

					}													

				}

			}

		}else{

			//ALTA DE cargo

			contexto.set("cuestionarioVTMAnu", cuestionarioVTMAnu);	

			

		}

	}



	/**

	 * @see es.ejie.frmk.presentacion.operacion.paso.ActionPasoOP#processForward(es.ejie.frmk.presentacion.sesiondatos.ContextoEJIE, org.apache.struts.action.ActionErrors, org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

	 */

	protected ActionForward processForward(

		Q70ContextoEJIE contexto,

		ActionErrors errores,

		ActionMapping mapping,

		ActionForm form,

		HttpServletRequest request,

		HttpServletResponse response)

		throws Exception {

		ActionForward salida = new ActionForward();

		String accion = (String)contexto.get("accion");

		if (accion != null && accion.equals("KEY_ALTA")){ //baja o modificación

			salida = mapping.findForward("irAltaCuestionariosVTMAnu");

		}else{

			salida = mapping.findForward("irCuestionarioVTMAnu");

		}

		return salida;

	}



}

