import java.util.Vector;

import lotus.domino.AgentBase;
import lotus.domino.AgentContext;
import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.NotesException;
import lotus.domino.Session;
import lotus.domino.View;
import lotus.domino.ViewEntry;
import lotus.domino.ViewEntryCollection;

// 表单显示代理1.0
// 作者:wnight88
// 2009年2月27日
public class showfileNew extends AgentBase
{
  String htmlString = "";
  String path = "";
  String viewName = "";

   public String getHeadHtml(Vector headList)
  {
    String tempString = "";
    tempString = tempString
        + "<table class=tbbg align=center cellpadding=2 cellspacing=1>";
    tempString = tempString + "<tr class=toptrbg>";
     for ( int i = 0; i < headList.size(); i++)
    {
      tempString = tempString + "<td align=center nowrap>"
          + headList.get(i) + "</td>";
    }
    tempString = tempString + "<td align=center nowrap>选择</td>";
    tempString = tempString + "</tr>";
     return tempString;
  }

   public String getBodyHtml(Vector fieldsList, ViewEntry entry,
      String fieldNameOfLink) throws NotesException
  {
    String tempString = "<tr class=trbg>";
    Document doc = entry.getDocument();
     for ( int i = 0; i < fieldsList.size(); i++)
    {
      tempString = tempString + "<td>";
       if (((String) fieldsList.get(i)).equals(fieldNameOfLink))
      {
        tempString = tempString + "<a href='" + path + "/" + viewName
            + "/" + doc.getUniversalID() + "?opendocument'>";
        tempString = tempString
            + doc.getItemValueString((String) fieldsList.get(i));
        tempString = tempString + "</a>";

      }
       else
      {
        tempString = tempString
            + doc.getItemValueString((String) fieldsList.get(i));
      }
      tempString = tempString + "</td>";
    }
    tempString = tempString + "<td align=center nowrap>"
        + "<input class='hinput' type='checkbox' name='SelDoc' value="
        + doc.getUniversalID() + "></td>";
    tempString = tempString + "</tr>";
     return tempString;
  }

   public void NotesMain()
  {

     try
    {
      Session session = getSession();
      AgentContext agentContext = session.getAgentContext();
      Database currentDB = agentContext.getCurrentDatabase();

       // 当前文档
      Document currentDoc = agentContext.getDocumentContext();

       // 获取要列出记录的视图名称
      viewName = currentDoc.getItemValueString( "viewName");
       // 获取每页显示行数
       int lineNum = currentDoc.getItemValueInteger( "lineNum");
       // 当前表单地址
      String url = currentDoc.getItemValueString( "curl");
       // 当前页
       int currentPage = currentDoc.getItemValueInteger( "curpage");
      path = currentDoc.getItemValueString( "path");
      View view = currentDB.getView(viewName);
      ViewEntryCollection vec = view.getAllEntries();

       // 计算总文档数和总页数
       int totalDoc, totalPage;
      totalDoc = vec.getCount();
       if (totalDoc % lineNum == 0)
      {
        totalPage = totalDoc / lineNum;
      }
       else
      {
        totalPage = (totalDoc / lineNum) + 1;
      }

       // 计算每页开始的文档号、结束的文档号
       int beginDocNo, endDocNo;
       if (currentPage == 1)
      {
         if (currentPage != totalPage)
        {
          beginDocNo = 1;
          endDocNo = lineNum;
        }
         else
        {
          beginDocNo = 1;
          endDocNo = totalDoc;
        }
      }
       else if (totalDoc < lineNum)
      {
        beginDocNo = 1;
        endDocNo = totalDoc;
      }
       else
      {
        beginDocNo = currentPage * lineNum - lineNum;
         if (currentPage != totalPage)
        {
          endDocNo = currentPage * lineNum;
        }
         else
        {
          endDocNo = totalDoc;
        }
      }

       // 显示头信息
      Vector headList = currentDoc.getItemValue( "headSet");
      htmlString = htmlString + this.getHeadHtml(headList);

       // 显示记录内容
      Vector fieldsOfBody = currentDoc.getItemValue( "fieldsOfBody");
      String fieldNameOfLink = currentDoc
          .getItemValueString( "filedNameOfLink");
       for ( int i = beginDocNo; i <= endDocNo; i++)
      {
        htmlString = htmlString
            + this.getBodyHtml(fieldsOfBody, vec.getNthEntry(i),
                fieldNameOfLink);
      }
      htmlString = htmlString + "</tr>";
      htmlString = htmlString + "</table>";
      currentDoc.replaceItemValue( "htmlStr", htmlString);

       // 显示分页信息
       int prePageNo, nextPageNo, firstPageNo, lastPageNo;
       if (currentPage <= 1)
      {
        prePageNo = 1;
        nextPageNo = 2;
      }
       else if (currentPage >= totalPage)
      {
        prePageNo = currentPage - 1;
        nextPageNo = currentPage;
      }
       else
      {
        prePageNo = currentPage - 1;
        nextPageNo = currentPage + 1;
      }
      firstPageNo = 1;
      lastPageNo = totalPage;
      String firstPageURL, lastPageURL, prePageURL, nextPageURL;
      firstPageURL = "/" + url + "&page=" + firstPageNo;
      lastPageURL = "/" + url + "&page=" + lastPageNo;
      prePageURL = "/" + url + "&page=" + prePageNo;
      nextPageURL = "/" + url + "&page=" + nextPageNo;
      htmlString = "";
      htmlString = htmlString
          + "<table border=0 align=center cellpadding=2 cellspacing=1 >"
          + "<tr bgcolor=ffffff><td>";
      htmlString = htmlString + "共有" + totalDoc + "个文档 合计" + totalPage
          + "页 ";
      htmlString = htmlString + "<a href='" + firstPageURL
          + "'>[首页]</a> ";
      htmlString = htmlString + "<a href='" + prePageURL + "'>[上一页]</a> ";
      htmlString = htmlString + "<a href='" + nextPageURL
          + "'>[下一页]</a> ";
      htmlString = htmlString + "<a href='" + lastPageURL + "'>[尾页]</a> ";
      htmlString = htmlString + "</td>";
      htmlString = htmlString + "</tr>";
      htmlString = htmlString + "</table>";

      currentDoc.replaceItemValue( "pageStr", htmlString);
       // (Your code goes here)

    }
     catch (Exception e)
    {
      e.printStackTrace();
    }
  }
}