📄 latestthreads.java
字号:
/* * XP Forum * * Copyright (c) 2002-2003 RedSoft Group. All rights reserved. * */package org.redsoft.forum.web;import java.sql.SQLException;import java.util.ArrayList;import org.redsoft.forum.dao.mysql.MysqlDAOFactory;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import org.redsoft.forum.fixture.MysqlFixture;import org.redsoft.forum.dao.PersistentThread;import java.io.OutputStreamWriter;import java.io.FileOutputStream;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import org.redsoft.forum.dao.ThreadDAO;import org.redsoft.forum.dao.DAOFactory;import org.redsoft.forum.dao.ForumDAO;import org.redsoft.forum.dao.Thread;/** * Create a latestThreads.jsp for the index.jsp to inlcude.It gets * the most popular threads in the past 24 hours. * * @author Charles Huang * @version $Id: LatestThreads.java,v 1.4 2004/02/26 03:05:37 mustang Exp $ */public class LatestThreads{ public static final String front = "<%@ page contentType=\"text/html; charset=gb2312\" %>\r" + "<TABLE cellSpacing=\"0\" cellPadding=\"2\" width=\"100%\" border=\"0\" class=\"headstyle11\" align=\"left\">\r" + "<TBODY>\r" + "<TR>\r" + "<TD height=\"23\" bgcolor=\"#ffffff\">\r" + "<b>HOT TOPICS</b>\r" + "</TD>\r" + "</TR>\r" + "<TR bgcolor=\"#3366cc\">\r" + "<TD height=\"1\">\r" + "</TD>\r" + "</TR>\r" + "<TR bgcolor=\"#ffffff\">\r" + "<TD height=\"2\">\r" + "</TD>\r" + "</TR>\r" + "<TR class=\"Subject\">\r" + "<TD vAlign=\"top\">\r"; public static final String tail = "</TD>\r" + "</TR>\r" + "</TBODY></TABLE>"; // Holds the latest and most popular threads in the past 24 hours private ArrayList threads = new ArrayList(); private String filePath; // Holds the numbre of latest threads that are displayed in this jsp private int numberOfThreads = 5; private static final int maxlength = 205; private static final int maxLengthTitle = 64; // Holds the tiem period, default is past 12 hours private int period = 12; /** * Constructor * * @param filePath - The file to which the class writes * @param numberOfThreads - Number of the lastest threads displayed in the output jsp */ public LatestThreads( final String filePath, final int numberOfThreads, final int period ){ this.filePath = filePath; this.numberOfThreads = numberOfThreads; this.period = period; } /** * Gets the latest and the most popular threads in the past 24 hours */ public void getThreads( final String[] args ){ try{ final MysqlFixture mysqlFixture = new MysqlFixture(); mysqlFixture.setUp(); final Connection conn = MysqlDAOFactory.getConnection(); // Get the desinated threads from input final ThreadDAO dao = DAOFactory.getInstance().getThreadDAO(); String condition = ""; org.redsoft.forum.dao.Thread persist; if( args.length > 3 ){ for( int index = 3; index < args.length; index++ ){ persist = dao.findByUID( Long.parseLong( args[ index ] ) ); threads.add( new org.redsoft.forum.dao.Thread( persist.getId(), persist.getTitle(), persist.getContent(), persist.getAuthor(), persist.getTimeStamp(), persist.getParent_id(), persist.getCategory(), persist.getLast_update(), persist.getReply(), persist.getClick(), persist.getReplied_thread(), persist.isNotify() ) ); condition = condition + " and id != " + persist.getId(); } } // Get the latest and most popular threads in the past 12 hours except those // have been desinated from input final long TwentyFourHoursAgo = System.currentTimeMillis() - ( period * 60 * 60 * 1000 ); final PreparedStatement stat = conn.prepareStatement( "select * from " + PersistentThread.TABLE_PERSISTENCE + " where " + PersistentThread.PROPERTY_CATEGORY + "= ? and " + PersistentThread.PROPERTY_PARENT_ID + "=-1" + " and timestamp>=" + TwentyFourHoursAgo + condition + " order by click DESC" ); ResultSet resultSet; Thread thread; ForumDAO forumDAO = DAOFactory.getInstance().getForumDAO(); for( int index = 0; index < forumDAO.getForumCategory().length; index++ ){ stat.setInt( 1, index+ 1 ); resultSet = stat.executeQuery(); while( resultSet.next() && (resultSet.getRow() < numberOfThreads ) ){ thread = new Thread( resultSet.getLong( PersistentThread.PROPERTY_ID ), resultSet.getString( PersistentThread.PROPERTY_TITLE ), resultSet.getString( PersistentThread.PROPERTY_CONTENT ), resultSet.getString( PersistentThread.PROPERTY_AUTHOR ), resultSet.getLong( PersistentThread.PROPERTY_TIMESTAMP ), resultSet.getLong( PersistentThread.PROPERTY_PARENT_ID ), resultSet.getInt( PersistentThread.PROPERTY_CATEGORY ), resultSet.getLong( PersistentThread.PROPERTY_LAST_UPDATED), resultSet.getInt( PersistentThread.PROPERTY_REPLY), resultSet.getInt(PersistentThread.PROPERTY_CLICK), resultSet.getLong(PersistentThread.PROPERTY_REPLIED_THREAD), resultSet.getBoolean(PersistentThread.PROPERTY_NOTIFY )); threads.add( thread ); } } stat.close(); conn.close(); }catch( final SQLException sqlException ){ sqlException.printStackTrace(); }catch( final Exception exception ){ exception.printStackTrace(); } } /** * Output to the jsp * */ public void output(){ try{ final File file = new File( filePath ); file.delete(); file.createNewFile(); final OutputStreamWriter writer = new OutputStreamWriter( new FileOutputStream( file ),"gb2312" ); writer.write( front, 0, front.length() ); Thread thread; String content; for( int index = 0; index < threads.size() ; index++ ){ thread = (Thread)threads.get( index ); writer.write("<A class=\"BoldSubject\" href=\"viewThread.go?parentId=" + thread.getId() + "&forum=" + thread.getCategory() + "\"><IMG alt=\"Read more\" hspace=\"4\" src=\"images/participate.gif\" align=\"baseline\" border=\"0\">" + thread.getTitle() + "</A>\r" ); writer.write("<br>\r"); writer.write("<FONT color=\"#000000\">\r"); content = thread.getContent(); int indexOfInvalidChar; if( thread.getContent().length() > maxlength ){ content = content.substring(0, maxlength ); if( (indexOfInvalidChar = content.lastIndexOf("&")) > -1 && maxlength-indexOfInvalidChar <= 5 ){ content = content.substring(0, indexOfInvalidChar ); }else if( (indexOfInvalidChar = content.lastIndexOf("<b")) > -1 ){ content = content.substring(0, indexOfInvalidChar ); }else if( (indexOfInvalidChar = content.lastIndexOf("<")) > -1 ){ content = content.substring(0, indexOfInvalidChar ); }else if( (indexOfInvalidChar = content.lastIndexOf("<br>")) >-1 && maxlength-indexOfInvalidChar <= 20 ){ content = content.substring(0, indexOfInvalidChar ); } } while( (indexOfInvalidChar = content.lastIndexOf("<br>")) >-1 && content.length()-indexOfInvalidChar <= 20 ){ content = content.substring(0, indexOfInvalidChar ); } writer.write( content ); writer.write( "<br>"); writer.write( "<I>(" + thread.getReply() + " replies, Last Updated " + thread.getLast_update() + ")</I>"); writer.write("</FONT>\r<br><br>"); } writer.write( tail, 0, tail.length() ); writer.flush(); }catch( final FileNotFoundException fileNotFound ){ fileNotFound.printStackTrace(); }catch( final IOException ioEcxeption ){ ioEcxeption.printStackTrace(); } } public static void main( final String[] args ){ final LatestThreads latestThreads = new LatestThreads( args[0] , Integer.parseInt( args[1] ), Integer.parseInt( args[2] ) ); latestThreads.getThreads( args ); latestThreads.output(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -