package com.roadway.edmail.fboperate;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import com.roadway.edmail.fboperate.model.OperateModel;
import com.roadway.edmail.util.StrUtil;
import com.roadway.edmail.util.SysParameters;
public class OperateModelCacheHelper
{
    /** 日志记录 */
    private static Logger logger = Logger
            .getLogger(OperateModelCacheHelper.class.getName());
    
    /** 未处理的操作模型的文件的路径 */
    private static String operatedModelPath = SysParameters.basicPath
            + "operatingModel.txt";;
    
    /** 已经处理的操作模型的文件的路径 */
    private static String operatingModelPath = SysParameters.basicPath
            + "operatedModel.txt";
    
    /**
     * 本方法用于把一个操作模型存入到临时文件
     * 
     * @param filePath
     *            文件的路径
     * @param model
     *            对像模型
     */
    public static synchronized void model2File(String filePath,
            OperateModel model, boolean isAppend)
    {
        /** 如果对象为NULL */
        if (model == null)
        {
            return;
        }
        File operatingFile = new File(filePath);
        /** 如果文件不存在,退出程序 */
        if (!operatingFile.exists())
        {
            return;
        }
        FileOutputStream fos = null;
        try
        {
            fos = new FileOutputStream(operatingFile, isAppend);
            fos.write(model.toString().getBytes());
        }
        catch (Exception ex)
        {
            /** 把异常信息加入到日志 */
            OperateModelCacheHelper.logger.error(StrUtil.getExceptionInfo(ex,
                    "edm_h"));
        }
        finally
        {
            try
            {
                /** 关闭输入输出流 */
                fos.close();
            }
            catch (Exception e)
            {   
            }
        }
    }
    
    /**
     * @param filePath
     * @return
     */
    public static synchronized List<OperateModel> file2Models(String filePath)
    {
        /** 装models的集合 */
        List<OperateModel> models = new ArrayList<OperateModel>();
        File operatingFile = new File(filePath);
        /** 如果文件不存在,退出程序 */
        if (!operatingFile.exists())
        {
            return models;
        }
        StringBuffer content = new StringBuffer();
        FileInputStream fis = null;
        try
        {
            fis = new FileInputStream(operatingFile);
            byte[] buffer = new byte[1024];
            int byteRead = 0;
            while ((byteRead = fis.read(buffer, 0, buffer.length)) != -1)
            {
                content.append(new String(buffer, 0, byteRead));
            }
        }
        catch (Exception ex)
        {
            /** 把异常信息加入到日志 */
            OperateModelCacheHelper.logger.error(StrUtil.getExceptionInfo(ex,
                    "edm_h"));
        }
        finally
        {
            try
            {
                fis.close();
            }
            catch (Exception ex)
            {}
        }
        String contentStr = content.toString();
        /** 记录日志 */
        OperateModelCacheHelper.logger.debug("file2Model的内容为:" + contentStr);
        String[] strModels = contentStr.split("\n\r");
        
        OperateModel model = null;
        for (int len = 0; len < strModels.length; len++)
        {
            String[] infos = strModels[len].split("[,]");
            if (infos.length == 3)
            {
                model = new OperateModel(infos[0], infos[1], infos[3]);
                models.add(model);
            }
        }
        return models;
    }
}