/*
 * NewsServlet.java
 *
 * Created on 20 de Maio de 2005, 17:48
 */

package siprp.pagina;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import org.apache.velocity.*;
import org.apache.velocity.app.*;

import com.evolute.utils.*;
import com.evolute.utils.arrays.*;
import com.evolute.utils.db.*;
import com.evolute.utils.sql.*;
import com.evolute.utils.strings.*;

/**
 *
 * @author  lflores
 */
public class NewsServlet extends HttpServlet
	implements GlobalConstants
{
	private static DBManager DBM = null;

	private static final Select SELECT = new Select( new String[]{ "not_noticias" }, 
			new String[]{"data", "noticia"}, new Field( "id" ).in( 
					new Field( "( SELECT MAX( id ) FROM not_noticias )" ) ) );
	
	private SQLExecuter executer = null;
	
	private static boolean velocityInit = false;
	
	public void init()
	{
		if( !velocityInit )
		{
			try
			{
				String TEMPLATE_DIR = this.getServletContext().getRealPath( "/" ) + "html/";

				Properties props = new Properties();
				props.setProperty( "file.resource.loader.path", TEMPLATE_DIR );
				Velocity.init( props );
			}
			catch( Exception ex )
			{
				ex.printStackTrace();
			}
			velocityInit = true;
		}
		
		if( DBM != null )
		{
			return;
		}
		
		try
		{
			DBM = new JDBCManager( bdUrl + "?prepareThreshold=1", 
					bdUsername, bdPassword , 8, 8, 0, new SQLQuery[] {} );
		}
		catch( Exception e )
		{
			e.printStackTrace();
		}
	}
	
	private void close()
	{
		try
		{
			DBM.close();
			DBM = null;
		}
		catch( Exception ex )
		{
			// we come here after an error
			// so we discard this exception
		}
	}
	
	private String getNews()
	{
//System.err.println( "NEWS: BEGIN" );
		try
		{
			if( executer == null )
			{
				executer = ( SQLExecuter )DBM.getSharedExecuter();
			}
			Virtual2DArray array = executer.executeQuery( SELECT );
			Object o[][] = array.getObjects();
			if( o != null && o.length > 0 )
			{
//System.err.println( "NEWS: " + o[ 0 ][ 0 ].toString() );
				return 
					//o[ 0 ][ 0 ].toString() +
					StringConverter.unicodeToHTML( o[ 0 ][ 1 ].toString() );
			}
		}
		catch( Exception ex )
		{
//System.err.println( "NEWS: EX" );
			ex.printStackTrace();
			close();
		}
//System.err.println( "NEWS: END" );
		return null;
	}
	
	public void doGet( HttpServletRequest req, HttpServletResponse res )
		throws IOException
	{
//System.err.println( "NEWS: GET BEGIN" );
		init();
//System.err.println( "NEWS: AF INIT" );
		ServletOutputStream out = res.getOutputStream();
//		String queryString = req.getQueryString();
		res.setContentType( "text/html" );
		String news = getNews();
		Hashtable parameters = new Hashtable();
		if( news == null )
		{
			parameters.put( "noticias", "" );
		}
		else
		{
			parameters.put( "noticias", news );
		}
//System.err.println( "NEWS: BF SHOW" );
		out.println( showPage( "noticias/mostrar_noticias.html", parameters ) );
	}
	
	private String showPage( String page, Hashtable parameters )
	{
		VelocityContext context = new VelocityContext();
		StringWriter output = new StringWriter();

		try
		{
			if( parameters != null )
			{
				String key;
				for( Enumeration e = parameters.keys(); e.hasMoreElements(); )
				{
					key = ( String ) e.nextElement();
					context.put( key, parameters.get( key ) );
				}
			}

			Velocity.mergeTemplate( page, Velocity.ENCODING_DEFAULT, context, output );

			return output.toString();
		}
		catch( Exception e )
		{
			e.printStackTrace();
		}
		return null;
	}
}
