/**
 * The contents of this file are subject to the license and copyright
 * detailed in the LICENSE and NOTICE files at the root of the source
 * tree and available online at
 *
 * http://www.dspace.org/license/
 */
package org.dspace.app.webui.jsptag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.jstl.fmt.LocaleSupport;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.ArrayUtils;
import org.dspace.content.Collection;

/**
 * Tag for display a list of collections
 * 
 * @author Robert Tansley
 * @version $Revision$
 */
public class CollectionListTag extends TagSupport
{
    /** Collections to display */
    private transient Collection[] collections;

    private static final long serialVersionUID = -9040013543196580904L;

    public CollectionListTag()
    {
        super();
    }

    public int doStartTag() throws JspException
    {
        JspWriter out = pageContext.getOut();

        try
        {
        	  //Cambio para GV: estilo de la tabla
            out.println("<table cellpadding=\"0\" cellspacing=\"0\" align=\"center\" class=\"miscTable\" title=\"Collection List\">");

            // Write column headings
            out.print("<tr><th id=\"t4\" class=\"oddRowOddCol\">"
                            + LocaleSupport.getLocalizedMessage(pageContext,
                                    "org.dspace.app.webui.jsptag.CollectionListTag.collectionName")
                    + "</th></tr>");

            // Row: toggles between Odd and Even
            String row = "even";

            for (int i = 0; i < collections.length; i++)
            {
                // name
                String name = collections[i].getMetadata("name");

                // first and only column is 'name'
                out.print("<tr><td headers=\"t4\" class=\"" + row + "RowEvenCol\">");
                out.print("<a href=\"");

                HttpServletRequest hrq = (HttpServletRequest) pageContext
                        .getRequest();
                out.print(hrq.getContextPath() + "/handle/");
                out.print(collections[i].getHandle());
                out.print("\">");
                
                //Cambio para GV: traduccion del nombre de la comunidad
                String nombre_coleccion = name;
			    	    if ((nombre_coleccion != null) && (!nombre_coleccion.equals(""))) {
			      	    char[] nombre_coleccion_sep = nombre_coleccion.toCharArray();
			      	    if (nombre_coleccion_sep[4] == '@') {
			        	    nombre_coleccion =  LocaleSupport.getLocalizedMessage(pageContext, nombre_coleccion.substring(4));
			        		}
				    		}  
				    		
                out.print(nombre_coleccion);
                out.print("</a>");

                out.println("</td></tr>");

                row = (row.equals("odd") ? "even" : "odd");
            }

            out.println("</table>");
        }
        catch (IOException ie)
        {
            throw new JspException(ie);
        }

        return SKIP_BODY;
    }

    /**
     * Get the collections to list
     * 
     * @return the collections
     */
    public Collection[] getCollections()
    {
        return (Collection[]) ArrayUtils.clone(collections);
    }

    /**
     * Set the collections to list
     * 
     * @param collectionsIn
     *            the collections
     */
    public void setCollections(Collection[] collectionsIn)
    {
        collections = (Collection[]) ArrayUtils.clone(collectionsIn);
    }

    public void release()
    {
        collections = null;
    }
}
