📄 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 org.redsoft.forum.util.ForumUtils;
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;
/**
* 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.1.1.1 2003/07/08 08:25:17 cinc 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 = "";
PersistentThread persist;
if( args.length > 3 ){
for( int index = 3; index < args.length; index++ ){
persist = dao.findByUID( Long.parseLong( args[ index ] ) );
threads.add( new Thread( persist.getID(),
persist.getTitle(),
persist.getContent(),
persist.getAuthor(),
persist.getTimeStamp(),
persist.getParentID(),
persist.getCategory(),
persist.getLastUpdated(),
persist.getReply(),
persist.getClick(),
persist.getRepliedThreadID() ) );
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;
for( int index = 0; index < ForumUtils.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) );
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.getLastUpdated() + ")</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();
}
}//EOC
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -