// Decompiled by DJ v3.0.0.63 Copyright 2002 Atanas Neshkov  Date: 29/09/2006 14:23:07

// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!

// Decompiler options: packimports(3) 

// Source File Name:   Q70ListasEnlazadasTag.java



package q02b.tag;



import es.ejie.frmk.infraestructura.tablas.Q70GestorTablasCodigos;

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

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

import es.ejie.frmk.presentacion.iu.taglib.html.Q70BaseHandlerTag;



import java.lang.reflect.Method;

import java.util.*;

import javax.servlet.jsp.JspException;

//import javax.servlet.jsp.PageContext;

//import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.beanutils.PropertyUtils;

import org.apache.struts.util.RequestUtils;



public class Q02bComboTablasCodigos extends Q70BaseHandlerTag

{

	 	protected static final String COMPONENTE = "TABLAS_CODIGOS_TAG";

	    private static Q70Traza tr = Q70Traza.getSingleton();

	    protected String localeKey;

	    private static Map scopes;

	    private String name;

	    private String type;

	    private String scope;

	    private String options;

	    private String extraOptions;

	    private String table;

	    private String labelColumn;

	    private String valueColumn;

	    private String whereClause;

	    private String activos;

	    private String codActual;

	    private String modo;	    

	    private String javascriptCallerFunc;

	    private String distinct;



	    static 

	    {

	        scopes = new HashMap();

	        scopes.put("page", new Integer(1));

	        scopes.put("request", new Integer(2));

	        scopes.put("session", new Integer(3));

	        scopes.put("application", new Integer(4));

	    }

	    

	    public String getLocale()

	    {

	        return localeKey;

	    }



	    public void setLocale(String s)

	    {

	        localeKey = s;

	    }



	    public String getName()

	    {

	        return name;

	    }



	    public void setName(String s)

	    {

	        name = s;

	    }



	    public String getScope()

	    {

	        return scope;

	    }



	    public void setScope(String s)

	    {

	        scope = s;

	    }



	    public String getOptions()

	    {

	        return options;

	    }



	    public void setOptions(String s)

	    {

	        options = s;

	    }



	    public String getExtraOptions()

	    {

	        return extraOptions;

	    }



	    public void setExtraOptions(String s)

	    {

	        extraOptions = s;

	    }



	    public String getTable()

	    {

	        return table;

	    }



	    public void setTable(String s)

	    {

	        table = s;

	    }



	    public String getLabelColumn()

	    {

	        return labelColumn;

	    }



	    public void setLabelColumn(String s)

	    {

	        labelColumn = s;

	    }



	    public String getValueColumn()

	    {

	        return valueColumn;

	    }



	    public void setValueColumn(String s)

	    {

	        valueColumn = s;

	    }



	    public String getWhereClause()

	    {

	        return whereClause;

	    }



	    public void setWhereClause(String s)

	    {

	        whereClause = s;

	    }

	    public String getActivos()

	    {

	        return activos;

	    }



	    public void setActivos(String s)

	    {

	    	activos = s;

	    }

	    

	    public String getType()

	    {

	        return type;

	    }



	    public void setType(String s)

	    {

	        type = s;

	    }

	    

	    public String getCodActual() {

			return codActual;

		}



		public void setCodActual(String codActual) {

			this.codActual = codActual;

		}

				

		

		

	    public String getModo() {

			return modo;

		}



		public void setModo(String modo) {

			this.modo = modo;

		}

		public String getJavascriptCallerFunc()

	    {

	        return javascriptCallerFunc;

	    }



		public void setJavascriptCallerFunc(String s)

		{

		      javascriptCallerFunc = s;

		}



		public String getDistinct()

	    {

	        return distinct;

	    }



		public void setDistinct(String s)

		{

			distinct = s;

		}

		

		public Q02bComboTablasCodigos()

	    {

	        localeKey = "org.apache.struts.action.LOCALE";

	        name = "es.ejie.frmk.presentacion.iu.taglib.html.TABLAS_BEAN";

	        type = null;

	        scope = null;

	        options = null;

	        extraOptions = null;

	        table = null;

	        labelColumn = null;

	        valueColumn = null;

	        whereClause = null;

	        activos = null;

	        modo = null;

	        javascriptCallerFunc = null;

	        distinct = null;

	    }



	    public int doStartTag()

	        throws JspException

	    {

	        if(tr.isTraceActive()){

	            tr.trace("CUSTOM_TAG", Q70TraceLevel.INFO, "Entrando en doStartTag de S99bTablasCodigosTag...");
            }
	        Object obj = getData();

	        if(null != obj){

	            storeData(obj);
            }
	        return 0;

	    }



	    public void release()

	    {

	        super.release();

	    }



	    private Object getData()

	        throws JspException

	    {

	        if(tr.isTraceActive()){

	            tr.trace("CUSTOM_TAG", Q70TraceLevel.INFO, "Comenzando a obtener los datos llamando al Gestor de tablas...");
            }
	        Locale locale = RequestUtils.retrieveUserLocale(pageContext, getLocale());

	        Vector vector = new Vector(2);

	        vector.add(labelColumn);

	        if(null != valueColumn){

	            vector.add(valueColumn);
            }
	        if(tr.isTraceActive()){

	            tr.trace("CUSTOM_TAG", Q70TraceLevel.INFO, "Llamando al Gestor de tablas con los parametros:-locale:" + locale.getLanguage() + "-table:" + table + "-whereClause:" + whereClause + "-cols:" + vector);
            }
	        java.util.ArrayList arraylist = null;

	        try{

        	

	        	if (modo != null && !modo.trim().equals("")){

		        	if (modo.equals("A")){

		        		//MODO ALTA

		        		if (whereClause != null && !whereClause.trim().equals("")){	        			

		        			whereClause = whereClause + " AND S99GNT02_IND_INACTIVO = 0";

		        		}else{

		        			whereClause = " S99GNT02_IND_INACTIVO = 0";

		        		}	

		        	}

		        	if (modo.equals("M") || modo.equals("C")){

		        		if (whereClause != null && !whereClause.trim().equals("")){	        			

		        			whereClause = whereClause + " AND S99GNT02_IND_INACTIVO = 0";

		        		}else{

		        			whereClause = " S99GNT02_IND_INACTIVO = 0";

		        		}

		        		if (codActual != null && !codActual.trim().equals("")){

			        		whereClause = "( " + whereClause + ") OR ( S99GNT02_NI = " + codActual + ")";		        		

			        	}

		        	}

	        	}else{	        	

		        	if (activos != null && activos.equals("true")){

		        		if (whereClause != null && !whereClause.trim().equals("")){	        			

		        			whereClause = whereClause + " AND S99GNT02_IND_INACTIVO = 0";

		        		}else{

		        			whereClause = " S99GNT02_IND_INACTIVO = 0";

		        		}

		        		if (codActual != null && !codActual.trim().equals("")){

			        		whereClause = "( " + whereClause + ") OR ( S99GNT02_NI = " + codActual + ")";		        		

			        	}

		        	}

	        	}

	        		        		        		        		        		        		               	

	            arraylist = Q70GestorTablasCodigos.getSingleton().selectRecordsFromTable(table, whereClause, locale.getLanguage(), vector);

	            if (this.distinct != null && this.distinct.equals("true")){

		            ArrayList codigos = new ArrayList();

		            ArrayList resultados = new ArrayList();

		            for (int i=0; i < arraylist.size();i++){

		            	String codigo = (String) ((org.apache.commons.beanutils.BasicDynaBean)arraylist.get(i)).get("valueColumn");

		            	if (!codigos.contains((org.apache.commons.beanutils.BasicDynaBean)arraylist.get(i))){

		            		resultados.add(codigo);

		            		codigos.add(codigo);

		            	}

		            }

		            arraylist = resultados;

	            }

	            // Después de ejecutar la consulta se vacía la where 

	            // para que no salga en la siguiente select

	            whereClause = null;

	        }

	        catch(Exception exception)

	        {

	            if(tr.isTraceActive()){

	                tr.trace("CUSTOM_TAG", Q70TraceLevel.ERROR, "No ha sido posible obtener el resultado del Gestor de Tablas y Codigos.");
                }
	            throw new JspException("No ha sido posible obtener el resultado del Gestor de Tablas y Codigos.", exception);

	        }

	        Object obj = null;

	        if(null != arraylist){

	            obj = createAndFillDataBean(arraylist);
            }
	        return obj;

	    }



	    private void storeData(Object obj)

	        throws JspException

	    {

	        pageContext.setAttribute(name, obj, getScope(scope));

	    }



	    private int getScope(String s)

	        throws JspException

	    {

	        int i = 1;

	        if(null != s && s.length() > 0)

	        {

	            Integer integer = (Integer)scopes.get(s.toLowerCase());

	            if(null == integer){

	                throw new JspException();
                }
	            i = integer.intValue();

	        }

	        return i;

	    }



	    private Object createAndFillDataBean(Collection collection)

	        throws JspException

	    {

	        if(tr.isTraceActive()){

	            tr.trace("CUSTOM_TAG", Q70TraceLevel.INFO, "Comenzando a crear y llenar un Bean...");
            }
	        Object obj = null;

	        try

	        {

	            if(null == pageContext.getAttribute(name, getScope(scope))){//tablasCodigosBeanSexo

	                obj = RequestUtils.applicationInstance(type);
                }
	            else{

	                obj = pageContext.getAttribute(name, getScope(scope));
                }
	            if(tr.isTraceActive()){

	                tr.trace("CUSTOM_TAG", Q70TraceLevel.DEBUG, "El tipo del parametro '" + options + "' es= " + PropertyUtils.getPropertyType(obj, options).toString());
                }
	            Class aclass[] = {

	                PropertyUtils.getPropertyType(obj, options)

	            };

	            StringBuffer stringbuffer = new StringBuffer("set");

	            stringbuffer.append(options.substring(0, 1).toUpperCase());

	            stringbuffer.append(options.substring(1));

	            Method method = obj.getClass().getMethod(stringbuffer.toString(), aclass);

	            method.invoke(obj, new Object[] {

	                collection

	            });

	        }

	        catch(Exception exception)

	        {

	            if(tr.isTraceActive()){

	                tr.trace("CUSTOM_TAG", Q70TraceLevel.ERROR, "No ha sido posible crear y llenar el bean");
                }
	            throw new JspException("No ha sido posible crear y llenar el bean.", exception);

	        }

	        return obj;

	    }



		

	   

}