/*
#=======================================================================================================
#
#                   Copyright (c) 2007 Origin Enterprise Solution LTD.
#
#=======================================================================================================
# File description:  ExportSearchResult.java
#=======================================================================================================
#	 Date	       time   	   Name	 Action	   Description of Change
#	2011-1-17      下午03:43:41    tyl  	Ini	            初始化文件
#=======================================================================================================
*/
package cust.com.rac.workflow;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import com.teamcenter.rac.util.MessageBox;

import cust.com.rac.workflow.SearchResultDialog.SearchResults;

public class ExportSearchResult {
	public ExportSearchResult(List<SearchResults> tableInfos, PackExcelInfo excelInfo) {
		createExcel(tableInfos, excelInfo);
	}

	protected void createExcel(List<SearchResults> tableInfos, PackExcelInfo excelInfo) {
		Workbook workbook = null;
		WritableSheet sourceSheet;
		WritableWorkbook writeWorkbook;
		File file = new File(excelInfo.getTemplateFileName());
		int listsize = tableInfos.size();
		int pageSize = excelInfo.getPageSize();
		List<Object> listTemp = new ArrayList<Object>();
		try {
			workbook = Workbook.getWorkbook(file);
			writeWorkbook = Workbook.createWorkbook(new File(excelInfo.getTargetFileName()), workbook);
			sourceSheet = writeWorkbook.getSheet(0);
			WritableSheet sheet = writeWorkbook.getSheet(0);
			
			String sourceSheetName = sourceSheet.getName();
			int page = listsize / pageSize;
			if (((listsize % pageSize) > 0)) {
				page = page + 1;
			}
			for (int i = 1; i <= page; i++) {
				sheet = null;
				listTemp = new ArrayList<Object>();
				writeWorkbook.copySheet(sourceSheetName, "第" + i + "页", i);
				sheet = writeWorkbook.getSheet("第" + i + "页");
				for (int j = (i - 1) * pageSize; (j < pageSize * (i)) && ((j < listsize)); j++) {
					listTemp.add(tableInfos.get(j));
				}
				addDataToExcel(sourceSheet, sheet, listTemp, i, excelInfo);
			}
			writeWorkbook.removeSheet(0);
			writeWorkbook.write();
			writeWorkbook.close();
		} catch (BiffException e1) {
			e1.printStackTrace();
		} catch (WriteException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			MessageBox.post("请检查:文件是否正在使用,请关闭", "信息提示", MessageBox.INFORMATION);
			e1.printStackTrace();
		}
	}

	private void addDataToExcel(WritableSheet sourceSheet, WritableSheet sheet, List<Object> listTemp, int currentpage, PackExcelInfo excelInfo) {
		int row = excelInfo.getInitRow();
		try {
			for (row = 0; row < 34; row++) {
				for (int col = 0; col < 15; col++) {
					if (row >= 4) {
						SearchResults result = (SearchResults) listTemp.get(row - 4);
						if (result != null) {
							if (col == 0) {
								insertCell(sourceSheet, sheet, col, row, result.getIndex());
							} else if (col == 1) {
								insertCell(sourceSheet, sheet, col, row, result.getTask_name());
							} else if (col == 2) {
								insertCell(sourceSheet, sheet, col, row, result.getTask_executor());
							} else if (col == 3) {
								insertCell(sourceSheet, sheet, col, row, result.getTask_createDate());
							} else if (col == 4) {
								insertCell(sourceSheet, sheet, col, row, result.getTask_finishDate());
							} else if (col == 5) {
								insertCell(sourceSheet, sheet, col, row, result.getTask_status());
							} else if (col == 6) {
								insertCell(sourceSheet, sheet, col, row, result.getTask_note());
							} else if (col == 7) {
								insertCell(sourceSheet, sheet, col, row, result.getTask_type());
							} else if (col == 8) {
								insertCell(sourceSheet, sheet, col, row, result.getProcess_executor());
							} else if (col == 9) {
								insertCell(sourceSheet, sheet, col, row, result.getProcess_name());
							} else if (col == 10) {
								insertCell(sourceSheet, sheet, col, row, result.getProcess_createDate());
							} else if (col == 11) {
								insertCell(sourceSheet, sheet, col, row, result.getProcess_finishDate());
							} else if (col == 12) {
								insertCell(sourceSheet, sheet, col, row, result.getProcess_lastModeUser());
							} else if (col == 13) {
								insertCell(sourceSheet, sheet, col, row, result.getProcess_status());
							} else if (col == 14) {
								insertCell(sourceSheet, sheet, col, row, result.getProcess_templateName());
							}
						}
					} else {
						if ((row == 0) && (col == 0)) {
							insertCell(sourceSheet, sheet, col, row, excelInfo.getTitle());
						} else
							insertCell(sourceSheet, sheet, col, row, "");
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void insertCell(WritableSheet sourceSheet, WritableSheet sheet, int col, int row, String value) {
		try {
			if ((value.trim().length() == 0) || (value == null)) {
				Label lab = new Label(col, row, sourceSheet.getCell(col, row).getContents());
				if (lab != null) {
					if (sourceSheet.getCell(col, row) != null) {
						if (sourceSheet.getCell(col, row).getCellFormat() != null) {
							lab.setCellFormat(sourceSheet.getCell(col, row).getCellFormat());
						}
					}
					sheet.addCell(lab);
				}
			} else {
				Label lab = new Label(col, row, value);
				if (lab != null) {
					if (sourceSheet.getCell(col, row) != null) {
						if (sourceSheet.getCell(col, row).getCellFormat() != null) {
							lab.setCellFormat(sourceSheet.getCell(col, row).getCellFormat());
						}
					}
					sheet.addCell(lab);
				}
			}
		} catch (RowsExceededException e) {
			e.printStackTrace();
		} catch (WriteException e) {
			e.printStackTrace();
		}	}
	}