package com.blueland.bpm.engine.util.cache;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import org.activiti.engine.impl.persistence.deploy.DeploymentCache;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import com.blueland.bpm.common.util.SysConfig;


/**
 * @author lw
 */
public class MyProcessDefinitionCache implements
		DeploymentCache<ProcessDefinitionEntity> {

    protected static Logger logger = LoggerFactory.getLogger(MyProcessDefinitionCache.class);

    public final static String PROCESS_DEFINITION_KEY = "processDefinitionKey";


    private RedisTemplate<String, ProcessDefinitionEntity> redisTemplate;

    public MyProcessDefinitionCache() {
    }

	

	@Override
    public ProcessDefinitionEntity get(String id) {
        try {
            ProcessDefinitionEntity processDefinitionEntity =
                    (ProcessDefinitionEntity) redisTemplate.opsForHash().get(PROCESS_DEFINITION_KEY, id);
            if (logger.isDebugEnabled()) {
                logger.debug("从缓存中获取流程定义对象内容:{}", processDefinitionEntity);
            }
            return processDefinitionEntity;
        } catch (Exception e) {
            logger.error("从缓存中获取流程定义对象失败！id：{}", id, e);
            return null;
        }
    }

	@Override
    public void add(String id, ProcessDefinitionEntity processDefinitionEntity) {
        try {
            redisTemplate.opsForHash().put(PROCESS_DEFINITION_KEY, id, processDefinitionEntity);
            if (logger.isDebugEnabled()) {
                logger.debug("将流程定义对象添加至缓存容器中:{}", processDefinitionEntity);
            }
        } catch (Exception e) {
            logger.error("将流程定义对象添加至缓存容器中失败！", e);
        }
    }

	@Override
    public void remove(String id) {
        try {
            redisTemplate.opsForHash().delete(PROCESS_DEFINITION_KEY, id);
            if (logger.isDebugEnabled()) {
                logger.debug("从缓存中删除流程定义对象:{}", id);
            }
        } catch (Exception e) {
            logger.error("从缓存中删除流程定义对象失败！id：{}", id, e);
        }
    }

	@Override
    public void clear() {
        try {
            redisTemplate.delete(PROCESS_DEFINITION_KEY);
            if (logger.isDebugEnabled()) {
                logger.debug("清空缓存中所有流程定义对象，结果：{}");
            }
        } catch (Exception e) {
            logger.error("清空缓存中所有流程定义对象失败！", e);
        }
    }



	public RedisTemplate<String, ProcessDefinitionEntity> getRedisTemplate() {
		return redisTemplate;
	}



	public void setRedisTemplate(RedisTemplate<String, ProcessDefinitionEntity> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

}