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

import java.io.*;
import java.util.*;

import javax.servlet.http.*;

import jp.co.sint.config.SIConfig;
import jp.co.sint.basic.SICmdtyDeleteImg;
import jp.co.sint.tools.SICustomError;
import jp.co.sint.tools.SICustomErrors;
import jp.co.sint.tools.SIURLParameter;
import jp.co.sint.tools.SIUtil;

/**
 * @version $Id : UICmdtyDeleteImgList.java,v 1.0 Exp $
 * @author      : Chihiro Imai
 * <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>
 * Chihiro Imai   2004/09/28  Original
 */

public class UICmdtyDeleteImgList extends SICmdtyDeleteImg{

  /**
   * UICmdtyListCond
   * コンストラクタ
   * @param なし
   * @return なし
   * @throws なし
   */
  public UICmdtyDeleteImgList(){}
  
  //ファイル情報
  private String fileName="";
  private String fileSize="";
  private String fileLastUpdateData="";
  private String filedir="";
  private String[] FileChk=new String[0];
  
  //ディレクトリ情報
  private String shopCode = "";
  private String destPath = "";
  
  /**
   * <b>init</b>
   * @param lUrlParam
   * @return なし
   * @throws なし
   */
  public void init(SIURLParameter lUrlParam){
    this.setShopCode((String)lUrlParam.getParam("subDir"));
    this.setDestPath((String)lUrlParam.getParam("destPath"));
    setPageSizeSel((String)lUrlParam.getParam("pageSizeSel"));
  }
  
  /**
   * <b>initDelete</b>
   * 削除対象のファイルを設定します。
   * @param lRequest
   * @return なし
   * @throws なし
   */
  public void initDelete(HttpServletRequest lRequest){
    setFileChk(lRequest.getParameterValues("cmdtyFileNameChk"));
  }
  
  //setter of 画像種類
  public void setDestPath(String lDestPath) {
    if (SIUtil.isNull(lDestPath)) lDestPath = "";
    this.destPath = SIUtil.changeTo(lDestPath.trim(), getEncode());
  }
  
  //setter of ショップコード
  public void setShopCode(String lShopCode) {
    if (SIUtil.isNull(lShopCode)) lShopCode = "";
    this.shopCode = SIUtil.changeTo(lShopCode.trim(), getEncode());
  }
  
  //getter of 画像種類
  public String getDestPath() {
    return destPath;
  }
  
  //getter of ショップコード
  public String getShopCode() {
    return shopCode;
  }
  
  /**getCollection
   *指定されたディレクトリのファイル一覧を取得します
   *@param ldir
   *@return imageListColl
  */
  public Collection getCollection(){
    File file = new File(getFileDir());
    String[] fileList = file.list();
    Collection imageListColl=new ArrayList();
    String dir = this.getFileDir();
    
    //ファイル一覧を取得
    if (file.exists()){
      for (int ii=0;ii<fileList.length;ii++){
        if(!fileList[ii].equalsIgnoreCase("Thumbs.db") && !fileList[ii].equalsIgnoreCase("index.jsp")){//Thumbs.db(キャッシュファイル)とindex.jsp以外のとき //7.2.1 ST2001 修正
          SICmdtyDeleteImg cmdtyDeleteImg=new SICmdtyDeleteImg();
          cmdtyDeleteImg.setEncode(this.getEncode());
          File f = new File(SIUtil.getOsFileName(dir + "/" + fileList[ii]));
          cmdtyDeleteImg.setFileName(fileList[ii]); //ファイル名
          cmdtyDeleteImg.setFileSize(f.length()); //ファイルサイズ
          cmdtyDeleteImg.setFileLastUpdateData(f.lastModified()); //最終更新日時
          imageListColl.add(cmdtyDeleteImg);
        } 
      }
    }
    return imageListColl;
  }
  
  /**
   * validate
   * 入力したデータをチェックします。
   * 不正なデータがある場合、エラーをオブジェクトに格納します。
   * そのオブジェクトは、エラーメッセージとして画面に表示されます。
   * @param HttpServletRequest ，Connection , SIURLParameter
   * @return true:エラーがない false:エラーが１つ以上ある
   * @throws なし
   */
  public boolean validate(HttpServletRequest lRequest){
    lRequest.removeAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY);
    SICustomErrors errors = new SICustomErrors();
    
    if(!this.getDestPath().equalsIgnoreCase(SIConfig.SISITELOGO_FOLDER_NAME) && this.getShopCode().equals("")){
      errors.addError(new SICustomError("input.data.require","ショップコード"));
    }
    
    if(this.getDestPath().equals("")){
      errors.addError(new SICustomError("input.data.require","画像種類"));
    }
    
    if (!errors.isEmpty()) lRequest.setAttribute(SIConfig.SIERROR_ATTRIBUTE_MESSAGE_KEY, errors);
    
    return errors.isEmpty();
  }
  
  /**initRequest
   *メニューから表示された場合に古いセッション情報を消します。
   *@param request
   *@param lSessionName
   *@param lUrlParam
   *@return
  */
  public static void initRequest(HttpServletRequest request,String lSessionName,SIURLParameter lUrlParam){
    String action = (String)lUrlParam.getParam("actionNameTxt");
    HttpSession lSession=null;
    if(SIUtil.isNotNull(action)&&action.equals(SIConfig.SIACTION_CLEAR)){
      lSession=request.getSession(true);
      lSession.removeAttribute(lSessionName);
    }
  }
  
  //7.2.1 ST2019 修正ここから
  /**deleteDirecty
   *ディレクトリの削除を行ないます
   *@param lFile
   *@return
  */
  public void deleteDirecty(File lFile) {
    //ディレクトリのパスを取得
    String dirpath = lFile.getPath();
    //ディレクトリのファイルオブジェクト作成
    File tempFile = new File(dirpath);
    //ファイルリストの取得
    String [] tempfileList = tempFile.list();
    int i =0;
    if (tempfileList != null) i=tempfileList.length; 
    if (i > 0) {
      for (int ii=0;ii<tempfileList.length;ii++){
        String deleteFileName = tempfileList[ii];
        String deleteFile = dirpath + SIUtil.getOsFileName("/") + deleteFileName;
        File delfile = new File(deleteFile);
        if (delfile.exists()){
          if (delfile.isFile()) {
            delfile.delete();
          } else {
            //パスがディレクトリの場合には再帰呼出
            deleteDirecty(delfile);
          }
        }
      }
    }
    lFile.delete();
  }
  //7.2.1 ST2019 修正ここまで
}