package com.aimluck.eip.modules.screens;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStreamWriter;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import jp.co.imtt.businesssystem.common.IMXlsWriter;

import net.sf.json.JSONArray;

import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
import org.apache.jetspeed.services.logging.JetspeedLogger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.turbine.util.RunData;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.context.Context;

import com.aimluck.eip.genka.GenkaXlsWriter;
import com.aimluck.eip.genka.ProfitXlsWriter;
import com.aimluck.eip.genka.WipXlsWriter;
import com.aimluck.eip.genka.util.GenkaUtils;
import com.aimluck.eip.common.ALDBErrorException;
import com.aimluck.eip.common.ALEipConstants;
import com.aimluck.eip.common.ALPageNotFoundException;
import com.aimluck.eip.common.ALPermissionException;
import com.aimluck.eip.orm.DatabaseOrmService;
import com.aimluck.eip.util.ALEipUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;

public class GenkaXlsExportScreen extends ALXlsScreen {
  
  /** logger */
  private static final JetspeedLogger logger = JetspeedLogFactoryService
      .getLogger(GenkaXlsExportScreen.class.getName());

  private static final String FILE_NAME = "book.xls";
  
  /** 一時フォルダへのフォルダ */
  private File rootFolder;
  
  @Override
  protected boolean createHSSFWorkbook(RunData rundata, Context context,
      HSSFWorkbook wb) {
    try {
      
    } catch (Exception e) {
      logger.error("GenkaXlsExportScreen", e);
      return false;
    }
    return true;
  }

  @Override
  protected String getFileName() {
    return FILE_NAME;
  }

  @Override
  protected String getFolderPath() {
    return rootFolder.getAbsolutePath();
  }
  
  /**
   * 初期化処理を行います。
   * 
   * @param action
   * @param rundata
   * @param context
   */
  public void init(RunData rundata, Context context)
      throws ALPageNotFoundException, ALDBErrorException {
    super.init(rundata, context);
    
    rootFolder = GenkaUtils.getGenkaRootFolder(DatabaseOrmService
        .getInstance().getOrgId(rundata), ALEipUtils.getUserId(rundata));
    
  }
  
  protected void doOutput(RunData rundata) throws Exception {
    VelocityContext context = new VelocityContext();
//    ServletOutputStream out = null;

    ArrayList msgList = new ArrayList();
    
    try {
      init(rundata, context);

      doCheckAclPermission(rundata, context, getDefineAclType());
      
      // 帳票の作成
      int genka_id = rundata.getParameters().getInt("genka");
      IMXlsWriter writer;
      switch (genka_id) {
      case 1:
        writer = new GenkaXlsWriter(rundata);
        break;
      case 2:
        writer = new ProfitXlsWriter(rundata);
        break;
      case 3:
        writer = new WipXlsWriter(rundata);
        break;
      default:
        return;
      } 
      //　ファイル内容を直接ストリームへ出力
      writer.outputXls(rundata, context);
      
    } catch (ALPermissionException e) {
      ALEipUtils.redirectPermissionError(rundata);
    } catch (Exception e) {
      logger.error("[ERROR]", e);
    }
  }
  


}
