package q02b.cuestionariosSMAnu.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.cuestionariosSMAnu.valueobjects.Q02bCuestionarioSM;

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 Q02bActionSeleccionarCuestionarioSM 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 listaCuestionariosSMAnu = (ArrayList)contexto.get("listaCuestionariosSMAnu");

		String niCuestionarioSMAnuSeleccionado = (String)contexto.get("niCuesSMAnu");

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

		

		

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

		Q02bCuestionarioSM cuestionarioSMAnuBD = new Q02bCuestionarioSM();

		HashMap cuestionarioSMAnu = 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 (listaCuestionariosSMAnu != null) {		

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

					cuestionarioSMAnuBD = (Q02bCuestionarioSM)listaCuestionariosSMAnu.get(i);

					if ( (cuestionarioSMAnuBD.getNiCuesSMAnu().toString()).equals(((String)niCuestionarioSMAnuSeleccionado)) ){

						found = true;

					}else{

						++i;

					}

				}

				//Deposita al contacto encontrado en el contexto de OP

				if (found){					

					try

					{						

						cuestionarioSMAnu =  Q02bVoUtils.getHashMap(cuestionarioSMAnuBD);	

						contexto.set("cuestionarioSMAnu", cuestionarioSMAnu);	

					}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("cuestionarioSMAnu", cuestionarioSMAnu);	

			

		}

	}



	/**

	 * @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("irAltaCuestionariosSMAnu");

		}else{

			salida = mapping.findForward("irCuestionarioSMAnu");

		}

		return salida;

	}



}

