diff --git a/trunk/WEB-INF/classes/siprp/pagina/NewsServlet.java b/trunk/WEB-INF/classes/siprp/pagina/NewsServlet.java new file mode 100644 index 00000000..a73194d3 --- /dev/null +++ b/trunk/WEB-INF/classes/siprp/pagina/NewsServlet.java @@ -0,0 +1,144 @@ +/* + * 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.arrays.*; +import com.evolute.utils.db.*; +import com.evolute.utils.sql.*; + +/** + * + * @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; + + public void init() + { + if( DBM != null ) + { + return; + } + try + { + DBM = new JDBCManager( bdUrl + "?prepareThreshold=1", + bdUsername, bdPassword , 10, 8, 8, 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() + " - " + 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", "SIPRP" ); + } + 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; + } +}