package zr.zrpower.web.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import zr.zrpower.web.bean.MessageBox;
import zr.zrpower.entity.sys.*;
import zr.zrpower.ejb.sys.SeManageService;
import zr.zrpower.common.util.*;
import zr.zrpower.common.web.Upload;
import zr.zrpower.common.util.SysPreperty;

public class ActionSysAttManage
    extends MessageBox {
    HttpSession MySession;
  //Initialize global variables
  public void init() throws ServletException {
  }


  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    request.setCharacterEncoding("GBK");
    response.setContentType("text/html; charset=GBK");
    out = response.getWriter();
    HttpSession session = request.getSession(true);
    SessionUser user = (SessionUser)session.getAttribute("userinfo");
     try {
    SeManageService SeMgr = new SeManageService();

    java.util.Date dt=new java.util.Date();
    long lg=dt.getTime();
    Long ld=new Long(lg);
    String mRecordID=ld.toString();

    String strPath=SysPreperty.getProperty().UploadFilePath+"/AttManage";

    String strFile_ID=user.getUserFileID();
    String strUser_ID=user.getUserID();

    if (strFile_ID=="" || strFile_ID==null || strFile_ID.length()==0) {
       strFile_ID = mRecordID;
       //创建目录
       SeMgr.CreateFileFoler(strPath);
       SeMgr.CreateFileFoler(strPath+"/"+strUser_ID);
       SeMgr.CreateFileFoler(strPath+"/"+strUser_ID+"/"+strFile_ID);
       //重设用户的文件ID
       user.setUserFileID(strFile_ID);
    }
    strPath = strPath +"/"+strUser_ID+ "/"+strFile_ID;
    //上传附件
    Upload upload = new Upload();
    upload.setSwitch(true);    //是否需要转换成新的文件名
    upload.setUpType(0);        //1文件流  0文件
    upload.setSuffix("*.*");    //可以上传的文件类型
    upload.setSize(1024000*1024000);   //上传的大小限制
    upload.setObjectPath(strPath);     //上传路径
    upload.setSourceFile(request);     //上传
    //
    Hashtable htFields = upload.getFields();
    String[] FileName = upload.getObjectFileName();
    //out.println("文件名："+FileName[0]);
    String strFLOWID = (String) htFields.get("FLOWID");
    String strDOCID = (String) htFields.get("DOCID");
    String strAID = (String) htFields.get("AID");
    String strTYPE = (String) htFields.get("TYPE");
    String strpTitle = (String) htFields.get("pTitle");

    int t1 = strpTitle.lastIndexOf("\\");
    if(t1 >= 0 &&  t1 < strpTitle.length()){
      strpTitle = strpTitle.substring(t1+1,strpTitle.length());
    }



    //插入附件记录
    SeMgr.SysAttFileAdd(strFLOWID,strDOCID,strAID,FileName[0],strUser_ID,strFile_ID,strpTitle);

    SeMgr = null;
    user = null;
    response.sendRedirect("zrsysmanage/att/AttAddFrm.jsp?FLOWID="+strFLOWID+"&DOCID="+strDOCID+"&AID="+strAID+"&TYPE="+strTYPE+"&parentload=1");
    }
     catch (Exception ex){
        out.println(ex.toString());
        //this.Msg = "连接中间件服务器失败，请稍后再试！";
     }
  }

  //Process the HTTP Post request
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    doGet(request, response);
  }

  //Clean up resources
  public void destroy() {
  }
}
