/* * NewsServlet.java * * Created on 20 de Maio de 2005, 17:48 */ package com.evolute.siprp.pagina; import java.io.IOException; import java.io.StringWriter; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Properties; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import com.evolute.utils.arrays.Virtual2DArray; import com.evolute.utils.db.DBManager; import com.evolute.utils.db.JDBCManager; import com.evolute.utils.db.SQLExecuter; import com.evolute.utils.sql.Field; import com.evolute.utils.sql.SQLQuery; import com.evolute.utils.sql.Select; import com.evolute.utils.strings.StringConverter; /** * * @author lflores */ public class NewsServlet extends HttpServlet implements GlobalConstants { private static final long serialVersionUID = 1L; 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 ) { } } private String getNews() { try { if ( executer == null ) { executer = ( SQLExecuter ) DBM.getSharedExecuter( this ); } Virtual2DArray array = executer.executeQuery( SELECT ); Object o[][] = array.getObjects(); if ( o != null && o.length > 0 ) { return StringConverter.unicodeToHTML( o[ 0 ][ 1 ].toString() ); } } catch ( Exception ex ) { ex.printStackTrace(); close(); } return null; } public void doGet( HttpServletRequest req, HttpServletResponse res ) throws IOException { init(); ServletOutputStream out = res.getOutputStream(); res.setContentType( "text/html" ); String news = getNews(); Map< String, Object > parameters = new HashMap< String, Object >(); parameters.put( "noticias", news == null ? "" : news ); out.println( showPage( "noticias/mostrar_noticias.html", parameters ) ); } private String showPage( String page, Map< String, Object > parameters ) { VelocityContext context = new VelocityContext(); StringWriter output = new StringWriter(); try { if ( parameters != null ) { Iterator< String > it = parameters.keySet().iterator(); while ( it.hasNext() ) { String key = it.next(); context.put( key, parameters.get( key ) ); } } Velocity.mergeTemplate( page, Velocity.ENCODING_DEFAULT, context, output ); return output.toString(); } catch ( Exception e ) { e.printStackTrace(); } return null; } }