/*
 * Generated by the Jasper component of Apache Tomcat
 * Version: Apache Tomcat/7.0.34
 * Generated at: 2013-01-21 02:31:26 UTC
 * Note: The last modified time of this file was set to
 *       the last modified time of the source file after
 *       generation to assist with modification tracking.
 */
package org.apache.jsp.admin;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*;
import java.sql.*;
import tot.search.*;
import tot.exception.*;
import tot.util.*;
import tot.bean.*;
import tot.db.*;
import tot.dao.DaoFactory;
import java.sql.*;
import java.net.*;
import java.io.*;
import tot.global.Sysconfig;
import tot.global.Syslog;
import org.apache.lucene.store.Directory;
import org.apache.lucene.index.IndexWriter;

public final class index_002dupdate_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {

  private static final javax.servlet.jsp.JspFactory _jspxFactory =
          javax.servlet.jsp.JspFactory.getDefaultFactory();

  private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;

  static {
    _jspx_dependants = new java.util.HashMap<java.lang.String,java.lang.Long>(1);
    _jspx_dependants.put("/admin/session.jsp", Long.valueOf(1309954872765L));
  }

  private javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.tomcat.InstanceManager _jsp_instancemanager;

  public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
    return _jspx_dependants;
  }

  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
  }

  public void _jspDestroy() {
  }

  public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
        throws java.io.IOException, javax.servlet.ServletException {

    final javax.servlet.jsp.PageContext pageContext;
    javax.servlet.http.HttpSession session = null;
    final javax.servlet.ServletContext application;
    final javax.servlet.ServletConfig config;
    javax.servlet.jsp.JspWriter out = null;
    final java.lang.Object page = this;
    javax.servlet.jsp.JspWriter _jspx_out = null;
    javax.servlet.jsp.PageContext _jspx_page_context = null;


    try {
      response.setContentType("text/html; charset=UTF-8");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			"", true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write('\r');
      out.write('\n');

String totsearch_admin=null;

if(session.getAttribute("totsearch_admin")!=null){
	totsearch_admin=(String)session.getAttribute("totsearch_admin");
}

if(totsearch_admin==null || totsearch_admin.equals("")){
	response.sendRedirect("login.jsp");
}

      out.write('\r');
      out.write('\n');

String act=RequestUtil.getString(request,"act");
String key=RequestUtil.getString(request,"key");
Connection conn = null;
Statement stmt=null;
ResultSet rs = null;
String tblname="v9_news";

      out.write("\r\n");
      out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n");
      out.write("<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n");
      out.write("<head>\r\n");
      out.write("<meta HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=utf-8\">\r\n");
      out.write("<title>add article index</title>\r\n");
      out.write("<link href=\"style/global.css\" rel=\"stylesheet\" type=\"text/css\" />\r\n");
      out.write("</head>\r\n");
      out.write("<body>\r\n");

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;	
	//tblname=tblname+k;	
	
	//String sql="select "+fields+" from "+tblname+" where id>=1300000 and id<1500000";
	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 fields="id,catid,title,description,updatetime,url,content";
						String sql="select a.id,a.catid,a.title,a.description,a.inputtime,a.url,b.id,b.author from "+tblname+" a,"+tblname+"_data b where a.id=b.id and a.id>"+lastindexid;
						//out.print(sql);
						if(last_id==0){
							sql="select a.id,a.catid,a.title,a.description,a.inputtime,a.url,b.id,b.author from "+tblname+" a,"+tblname+"_data b where a.id=b.id and a.id>"+(k-1)*indexper+" and a.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(7);
								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",rs.getString(6),0);
								df.setField("author",rs.getString(8),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.back();\">&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 "+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);
	}

      out.write("\r\n");
      out.write("<p></p><p></p>\r\n");
      out.write("<div align=\"center\">\r\n");
      out.write("<table width=\"98%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"1\" bgcolor=\"#999999\">\r\n");
      out.write("  <tr>\r\n");
      out.write("    <td height=\"25\" bgcolor=\"#DFFFEF\"><div align=\"center\">\r\n");
      out.write("      <p>&nbsp;</p>\r\n");
      out.write("      <p>&nbsp;</p>\r\n");
      out.write("      <p><a href=\"?act=do&currentid=");
      out.print(currentid);
      out.write("&tblname=");
      out.print(tblname);
      out.write("\" style=\"color:#F00; font-size:14px;\"><strong>[点击更新索引]=&gt;</strong>&gt;</a></p>\r\n");
      out.write("      <p>&nbsp;</p>\r\n");
      out.write("      <p>(当前ID：");
      out.print(currentid);
      out.write(",上次索引至ID：");
      out.print(lastindexid);
      out.write(")\r\n");
      out.write("      </p>\r\n");
      out.write("    </div></td>\r\n");
      out.write("  </tr>\r\n");
      out.write("</table>\r\n");
      out.write("</div>\r\n");
      out.write("<p align=\"center\">\r\n");
      out.write("<p align=\"center\">\r\n");
      out.write("  <input type=\"button\" value=\"返回\" onclick=\"javascript:history.back();\" />\r\n");
      out.write("  ");

}

      out.write("\r\n");
      out.write("</body>\r\n");
      out.write("</html>");
    } catch (java.lang.Throwable t) {
      if (!(t instanceof javax.servlet.jsp.SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try { out.clearBuffer(); } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
        else throw new ServletException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}
