AttributeHandler.java 6.13 KB
package com.dili.titan.handler;

import com.dili.titan.domain.Attribute;
import com.dili.titan.domain.AttributeValue;
import com.dili.titan.domain.RedisKeyConstant;
import com.dili.titan.domain.log.LogHelper;
import com.dili.titan.domain.log.LogTypeEnum;
import com.dili.titan.mq.sender.TopicProducer;
import com.dili.titan.redis.JedisClient;
import com.dili.titan.service.impl.AttributeService;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

/**
 * <B>Description</B> TODO 属性监控<br />
 * <B>Copyright</B> Copyright (c) 2014 www.diligrp.com All rights reserved. <br />
 * 本软件源代码版权归地利集团,未经许可不得任意复制与传播.<br />
 * <B>Company</B> 地利集团
 * @createTime 2014年9月22日 下午2:49:22
 * @author yangjianjun
 */
@Component
public class AttributeHandler implements BaseHandler {

    private ObjectMapper mapper = new ObjectMapper();

    @Resource(name = "attributeService")
    private AttributeService attributeService;

    @Resource(name = "jedisClient")
    private JedisClient jedisClient;

    @Resource(name = "topicProducer")
    private TopicProducer topicProducer;

    @Override
    public void handle(String message) throws Exception {
        try {
            JsonNode node = mapper.readTree(message);
            long attrId = node.get("attrId").asLong();
            int operType = node.get("oper").asInt();

            switch (operType) {
            case 1:
                add(attrId);
                topicProducer.sendMQAttribute(attrId, operType);
                break;
            case 2:
                update(attrId);
                topicProducer.sendMQAttribute(attrId, operType);
                break;
            case 3:
                delete(attrId);
                topicProducer.sendMQAttribute(attrId, operType);
                break;
            default:
                LogHelper.error(LogTypeEnum.ATTRIBUTE,
                        "Attribute operType error");
                break;
            }
        } catch (Exception e) {
            LogHelper.error(LogTypeEnum.ATTRIBUTE, e, "handle error!",
                    e.getMessage());
            throw e;
        }
    }

    /**
     * 新增属性
     * 
     * @param attrId
     */
    private void add(long attrId) throws Exception {
        putAddUpdateAttr(attrId);
        putAddUpdateAttrValue(attrId);
    }

    /**
     * 修改属性
     * 
     */
    private void update(long attrId) throws Exception {
        putAddUpdateAttr(attrId);
        putAddUpdateAttrValue(attrId);
    }

    private void delete(long attrId) throws Exception {
        delAttr(attrId);
        delAttrValue(attrId);
    }

    private void putAddUpdateAttr(long attrId) throws Exception {
        try {
            Attribute attribute = attributeService.getAttributeById(attrId);
            String json = mapper.writeValueAsString(attribute);
            jedisClient.hput(RedisKeyConstant.ATTRIBUTE,
                    String.valueOf(attribute.getAttrId()), json,JedisClient.expireTime);

            List<Attribute> attributes = attributeService.getAllAttribute();
            for (Attribute a : attributes) {
                try {
                    String jsons = mapper.writeValueAsString(a);
                    jedisClient.hput(RedisKeyConstant.ATTRIBUTE,
                            String.valueOf(a.getAttrId()), jsons,JedisClient.expireTime);
                    putAddUpdateAttrValue(a.getAttrId());
                } catch (Exception e) {
                    LogHelper.error(LogTypeEnum.ATTRIBUTE, e,
                            "load attribute error!attrId={}", attrId);
                    throw e;
                }
            }

        } catch (Exception e) {
            LogHelper.error(LogTypeEnum.ATTRIBUTE, e,
                    "load addAttr error!attrId={}", attrId);
            throw e;
        }
    }

    private void putAddUpdateAttrValue(long attrId) throws Exception {
        try {
            List<AttributeValue> values = attributeService
                    .getAttributeValues(attrId);
            String json = mapper.writeValueAsString(values);
            jedisClient.hput(RedisKeyConstant.ATTR_VALUE, String.valueOf(attrId), json,JedisClient.expireTime);
        } catch (Exception e) {
            LogHelper.error(LogTypeEnum.ATTRIBUTE, e,
                    "load addAttrValue error!attrId={}", attrId);
            throw e;
        }
    }

    private void delAttr(long attrId) throws Exception {
        try {
            jedisClient.hdel(RedisKeyConstant.ATTRIBUTE, String.valueOf(attrId));
            putAddUpdateAttr(attrId);
        } catch (Exception e) {
            LogHelper.error(LogTypeEnum.ATTRIBUTE, e,
                    "load delAttr error!attrId={}", attrId);
            throw e;
        }
    }

    private void delAttrValue(long attrId) throws Exception {
        try {
            jedisClient.hdel(RedisKeyConstant.ATTR_VALUE, String.valueOf(attrId));
            putAddUpdateAttrValue(attrId);
        } catch (Exception e) {
            LogHelper.error(LogTypeEnum.ATTRIBUTE, e,
                    "load delAttrValue error!attrId={}", attrId);
            throw e;
        }
    }

    /**
     * 应用启动时自动执行load方法,加载所有的属性信息
     * 
     */
    @PostConstruct
    public void load() {
        LogHelper.info(LogTypeEnum.ATTRIBUTE, "begin load attribute info");
        List<Attribute> attributes = attributeService.getAllAttribute();
        for (Attribute a : attributes) {
            try {
                String json = mapper.writeValueAsString(a);
                jedisClient.hput(RedisKeyConstant.ATTRIBUTE, String.valueOf(a.getAttrId()),
                        json,JedisClient.expireTime);
                putAddUpdateAttrValue(a.getAttrId());
            } catch (Exception e) {
                LogHelper.error(LogTypeEnum.ATTRIBUTE, e,
                        "load attribute error!attrId={}", a.getAttrId());
            }
        }
        LogHelper.info(LogTypeEnum.ATTRIBUTE, "load attribute info done");
	}
}