package com.nms.server.thread.change;

import java.util.Arrays;

import org.apache.log4j.Logger;

import com.nms.server.common.Common;
import com.nms.server.dao.CommonDao;
import com.nms.server.service.ChangeService;

/**
 *  监测类别信息变更更新操作类
 *  @date Dec 11, 2012 4:01:20 PM
 *	@author ZhangGang
 *
 */
public class ChangeForCheckType implements Runnable{
	private final Logger logger = Logger.getLogger(ChangeForCheckType.class);
	private String [] data = null;
	private Long recordId;

	/**
	 * 构造方法
	 * @param recordId	库表记录ID 可为空
	 * @param data	变更的监测类别ID
	 */
	public ChangeForCheckType(Long recordId,String [] data){
		this.recordId = recordId;
		this.data = data;
	}

	public void run(){
//		Thread.currentThread().setName("监测类别变更");
		Thread.currentThread().setName("Monitoring Class Change");
		CommonDao dao = null;
		
		try {
			dao = new CommonDao();
			ChangeService service = new ChangeService(dao);
			if(data == null || data.length==0){		//无效数据
				logger.warn("It is not effective to monitor class change information："+(data==null?null:Arrays.toString(data)));
				/*删除记录*/
				if(recordId!=null){
					service.deleteEventRecordByIds(recordId.toString());
				}
				return ;
			}
			String idstr = data[0];
			long checkTypeId = Long.parseLong(idstr);
			logger.info("更新监测类别ID "+checkTypeId);
			//只更新缓存，对于表结构的改变，需要动代码
			/* 查询 监测类别名称 */
			String obj = service.getCheckTypeNameById(checkTypeId);
			//重新初始化 checkType TableName Map
			service.updateCheckTableInfo(Common.getInsertTable(), obj);
			//重新初始化 Alarm Info Map 
			service.updateAlarmInfo(Common.getAlarmInfoMap(), checkTypeId);
			//重新初始化  Table Columns Map 
			service.updateTableColumnsMap(Common.getTableMap(), obj);
			
			/* 删除库表中的记录 */
			if(recordId!=null){
				service.deleteEventRecordByIds(recordId.toString());
			}
		} catch (Exception e) {
			logger.error("",e);
		}finally{
			if(dao!=null){
				dao.close();
				dao=null;
			}
		}
	}
}
