/**
 * Copyright (c) 2003-2006 System Integrator Corporation.
 *                 All Rights Reserved.
 */
package jp.co.sint.docs;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import jp.co.sint.config.SIConfig;
import jp.co.sint.config.SIExcelConf;
import jp.co.sint.tools.SIUtil;

import org.apache.log4j.Category;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

/**
 * @version $Id: SIExcelRead.java,v 1.0 2006/05/10 Exp $
 * @author  帳票取込を処理します。
 * <br>Description:
 * <p>History</p>
 * <p>Author&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reason</p>
 *  ============&nbsp;&nbsp;&nbsp;==========&nbsp;&nbsp;===========================<br>
 * Hong.M.J        2006/05/10 12:20:42  Original
 */
public class SIExcelRead {
    
//  ログ用のインスタンスの生成
    private static Category log=Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);
    
    private HSSFRow row = null;
    
    private HSSFCell cell=null;
    
    private HSSFWorkbook workBook = null;
    
    private HSSFSheet workSheet = null;   

    private POIFSFileSystem fs=null;    
    
    private int fileKind = 0;//帳票種類
    
    private int startRowIndex = 0;//明細開始ローインデックス
    
    private int nowRowIndex = 0;//現在のローインデックス
    
    public SIExcelRead(){}
    
    public SIExcelRead(int fileKind,String filePath){
        try {
            fs = new POIFSFileSystem(new FileInputStream(filePath));
            workBook = new HSSFWorkbook(fs);
            this.fileKind = fileKind;
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void makeSheet(int sheetNum){
        workSheet = workBook.getSheetAt(sheetNum);
        //変数初期化
        nowRowIndex =0;
        startRowIndex = Integer.parseInt(SIExcelConf.getStartRowIndex(fileKind));
    }
    
    public void readLine(int rowindex){
        nowRowIndex = rowindex+startRowIndex;        
        row = workSheet.getRow(nowRowIndex);
        if(row==null)row = workSheet.createRow(nowRowIndex);
    }
    /**
     * <b>itemList</b>
     * データリストを取得します。
     * @param なし
     * @return データリスト
     * @throws なし
     */
    public String readDetailItem(int cellindex){
        String result= "";
        cell = row.getCell((short)cellindex);
        
        //CELL_TYPE_STRING, CELL_TYPE_NUMERIC, CELL_TYPE_FORMULA, CELL_TYPE_BOOLEAN, CELL_TYPE_ERROR
        //CELL_TYPE_STRING, CELL_TYPE_NUMERICのみ対応
        if(cell!=null){
	        switch (cell.getCellType()){
	        	case HSSFCell.CELL_TYPE_STRING:
	        	    result = cell.getStringCellValue();
	        	    break;
	        	case HSSFCell.CELL_TYPE_NUMERIC:
	        	    double numericresult = cell.getNumericCellValue();
	        		result = String.valueOf((int)numericresult);//小数点切り
	        	    break;
	        }
        }        
        return result;        
    }
    
    public String readHeaderItem(int rowindex,int cellindex){
        row = workSheet.getRow(rowindex);
        String result= "";
        cell = row.getCell((short)cellindex);
        
        //CELL_TYPE_STRING, CELL_TYPE_NUMERIC, CELL_TYPE_FORMULA, CELL_TYPE_BOOLEAN, CELL_TYPE_ERROR
        //CELL_TYPE_STRING, CELL_TYPE_NUMERICのみ対応
        if(cell!=null){
	        switch (cell.getCellType()){
	        	case HSSFCell.CELL_TYPE_STRING:
	        	    result = cell.getStringCellValue();
	        	    break;
	        	case HSSFCell.CELL_TYPE_NUMERIC:
	        	    double numericresult = cell.getNumericCellValue();
	        		result = String.valueOf((int)numericresult);//小数点切り
	        	    break;
	        }
        }        
        return result;        
    }
    
    public boolean isEnd(String endString){
        if(SIUtil.isNull(endString))return true;
        for(int i =0;i<SIExcelConf.FOOTER_TITLE.length;i++){
            if(endString.trim().equals(SIExcelConf.FOOTER_TITLE[i])){
                return true;
            }
        }
        return false;        
    }
    public int size(){
        if (fs==null||workSheet==null){log.error("not set input file .");return 0;}
        return workSheet.getPhysicalNumberOfRows();
    }   
  
}
