<%@ page contentType="text/html; charset=UTF-8" language="java" errorPage="" %>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="tot.search.*"%>
<%@ page import="tot.exception.*"%>
<%@ page import="tot.util.*" %>
<%@ page import="tot.bean.*" %>
<%@ page import="tot.db.*" %>
<%@ page import="tot.dao.DaoFactory" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.net.*" %>
<%@ page import="java.io.*" %>
<%@ page import="tot.global.Sysconfig" %>
<%@ page import="tot.global.Syslog" %>
<%@ page import="org.apache.lucene.store.Directory" %>
<%@ page import="org.apache.lucene.index.IndexWriter" %>
<%@ include file="session.jsp" %>
<%
String act=RequestUtil.getString(request,"act");
String key=RequestUtil.getString(request,"key");
Connection conn = null;
Statement stmt=null;
ResultSet rs = null;
String tbl_pre="phome_ecms_";
String tblname=RequestUtil.getString(request,"tblname");
String siteurl=Sysconfig.getSiteUrl();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
<title>add article index</title>
<link href="style/global.css" rel="stylesheet" type="text/css" />
</head>
<body>
<%
String lastidname=tblname+"_id.txt";
String separator=FileUtil.getSeparator();	
String idfile=request.getRealPath("/id")+separator+lastidname;
String lastindexid="0";
long id_next=0l;
if(act!=null && act.equals("do")){
	long currentid=RequestUtil.getLong(request,"currentid");	
	if(FileUtil.exitsFile(idfile)){
		lastindexid=FileUtil.readFile(idfile,"UTF-8");
	}
	long last_id=Long.parseLong(lastindexid);
	long loop_n=1l;
	long indexper=100;
	if(last_id==0) loop_n=10000;	
	int Articles_PER_FETCH = 200;
	int MERGE_FACTOR = 10;	
	String fields="id,classid,title,smalltext,newstime,titleurl";
	if(tblname.equals("download")){
		fields=StringUtils.replaceString(fields,"smalltext","softsay");
	}else if(tblname.equals("photo")){
		fields=StringUtils.replaceString(fields,"smalltext","picsay");
	}else if(tblname.equals("flash")){
		fields=StringUtils.replaceString(fields,"smalltext","flashsay");
	}else if(tblname.equals("movie")){
		fields=StringUtils.replaceString(fields,"smalltext","moviesay");
	}else if(tblname.equals("shop")){
		fields=StringUtils.replaceString(fields,"smalltext","intro");
	}else{
		fields=StringUtils.replaceString(fields,"smalltext","title");
	}
	boolean isRebuilding = false;
	long start = System.currentTimeMillis();
	Directory directory = null;
	IndexWriter writer = null;
	int indexNum=0;
	String content=null;
	int i=0;
	try {
			directory = SearchService.getSearchIndexDir();
			writer = ArticleIndex.getIndexWriter(directory, false);
			//
			Statement stmtm=null;
			try{
					conn = DBUtils.getConnection();
					for(int k=1;k<=loop_n;k++){
						out.print("<h2>索引表："+tblname+k+"</h2>");
						out.flush();						
						String sql="select "+fields+" from "+tbl_pre+tblname+" where id>"+lastindexid;
						//out.print(sql);
						if(last_id==0){
							sql="select "+fields+" from "+tbl_pre+tblname+" where id>"+(k-1)*indexper+" and id<="+((k-1)*indexper+indexper);
						}
						stmt = conn.createStatement();						
						rs=stmt.executeQuery(sql);
						DataField df;
						String[] splitStr=null;
						splitStr=fields.split(",");
						i=0;
						while(rs.next()) {
								df=new DataField();
								id_next=rs.getLong(1);
								df.setField("id", rs.getString(1),0);
								df.setField("classid", rs.getString(2),0);
								df.setField("title", rs.getString(3),0);
								content=rs.getString(4);
								if(content==null || content.equals("")) content=".";
								df.setField("content", content,0);
								df.setField("demons",rs.getString(4),0);
								long newstime=rs.getLong(5)*1000l;
								df.setField("newstime", new Timestamp(newstime).toString(),0);															
								df.setField("linkurl",siteurl+rs.getString(6),0);
								ArticleIndex.doIndex(df, writer,tblname);
								indexNum++;						
								i++;								
						}				
						out.print("索引:"+i+"条<br>");
						out.flush();
						DBUtils.closeResultSet(rs);            
						DBUtils.closeStatement(stmt);
						if(id_next>=currentid){
							FileUtil.createFile(currentid+"",idfile,"UTF-8");
							break;
						}
					}            
			} catch(Exception e){
					out.print(e.toString());
			} finally{            
					DBUtils.closeConnection(conn);
			}
			out.print("共索引个数： " + indexNum + " 条.<br>");
	} catch (Exception e) {
			out.print("Error while rebuilding index"+e);
	} finally {
			try {
					if (writer != null) {
							writer.close();
					}
					if (directory != null) {
							directory.close();						
					}
			} catch (IOException e) {
					out.print("Close directory case exception:"+ e);
			}
	}
	//out.print(System.currentTimeMillis());
	long endt=System.currentTimeMillis();
	long timeuse=endt-start;
	if(timeuse<1000l){		
		out.print("用时：" + timeuse+"ms");
	}else{
		timeuse=timeuse/1000;
		out.print("用时：" + DateUtil.DateFormats((int)timeuse));
	}	
	out.print("<br><a href=\"javascript:history.go(-2);\">&gt;&gt;&gt;&gt;返回</a>");
}//if
else{
	if(FileUtil.exitsFile(idfile)){
		lastindexid=FileUtil.readFile(idfile,"UTF-8");
	}
	long currentid=0l;
	try{
		conn = DBUtils.getConnection();
		stmt = conn.createStatement();						
		rs=stmt.executeQuery("select id from "+tbl_pre+tblname+" where 1=1 order by id desc limit 0,1");
		if(rs.next()) {
			currentid=rs.getLong(1);
		}
	} catch(Exception e){
			out.print(e.toString());
	} finally{
			DBUtils.closeResultSet(rs);
			//DBUtils.resetStatement(stmt);
			DBUtils.closeStatement(stmt);
			DBUtils.closeConnection(conn);
	}
%>
<p></p><p></p>
<div align="center">
<table width="98%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#999999">
  <tr>
    <td height="25" bgcolor="#DFFFEF"><div align="center">
      <p>&nbsp;</p>
      <p>索引表：<%=tbl_pre+tblname%></p>
      <p><a href="?act=do&currentid=<%=currentid%>&tblname=<%=tblname%>" style="color:#F00; font-size:14px;"><strong>[点击更新索引]=&gt;</strong>&gt;</a></p>
      <p>&nbsp;</p>
      <p>(当前ID：<%=currentid%>,上次索引至ID：<%=lastindexid%>)
      </p>
    </div></td>
  </tr>
</table>
</div>
<p align="center">
<p align="center">
  <input type="button" value="返回" onclick="javascript:history.back();" />
  <%
}
%>
</body>
</html>