package com.dianping.cat.system.config;

import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import org.unidal.dal.jdbc.DalNotFoundException;
import org.unidal.helper.Files;

import com.dianping.cat.Cat;
import com.dianping.cat.core.config.Config;
import com.dianping.cat.core.config.ConfigEntity;
import com.dianping.cat.home.rule.entity.MonitorRules;
import com.dianping.cat.home.rule.transform.DefaultSaxParser;

public class BusinessRuleConfigManager extends BaseRuleConfigManager  implements Initializable {

	private static final String CONFIG_NAME = "businessRulesConfig";

	@Override
   protected String getConfigName() {
	   return CONFIG_NAME;
   }

	@Override
	public void initialize() throws InitializationException {
		try {
			Config config = m_configDao.findByName(CONFIG_NAME, ConfigEntity.READSET_FULL);
			String content = config.getContent();

			m_config = DefaultSaxParser.parse(content);
			m_configId = config.getId();
		} catch (DalNotFoundException e) {
			try {
				String content = Files.forIO().readFrom(
				      this.getClass().getResourceAsStream("/config/default-business-metric-rule-config.xml"), "utf-8");
				Config config = m_configDao.createLocal();

				config.setName(CONFIG_NAME);
				config.setContent(content);
				m_configDao.insert(config);

				m_config = DefaultSaxParser.parse(content);
				m_configId = config.getId();
			} catch (Exception ex) {
				Cat.logError(ex);
			}
		} catch (Exception e) {
			Cat.logError(e);
		}
		if (m_config == null) {
			m_config = new MonitorRules();
		}
	}

}

@@ -34,16 +34,40 @@
 onfig;%0A%0A
+import java.util.List;%0A%0A
 import o
@@ -427,16 +427,68 @@
 Entity;%0A
+import com.dianping.cat.home.rule.entity.Condition;%0A
 import c
@@ -534,16 +534,71 @@
 rRules;%0A
+import com.dianping.cat.home.rule.entity.Subcondition;%0A
 import c
@@ -650,16 +650,70 @@
 xParser;
+%0Aimport com.dianping.cat.report.task.alert.MetricType;
 %0A%0Apublic
@@ -775,17 +775,16 @@
 Manager 
- 
 implemen
@@ -881,19 +881,17 @@
 verride%0A
-   
+%09
 protecte
@@ -918,19 +918,17 @@
 me() %7B%0A%09
-   
+%09
 return C
@@ -939,19 +939,760 @@
 G_NAME;%0A
-   
+%09%7D%0A%0A%09public List%3Ccom.dianping.cat.home.rule.entity.Config%3E queryConfigs(String metricKey, MetricType type) %7B%0A%09%09List%3Ccom.dianping.cat.home.rule.entity.Config%3E configs = super.queryConfigs(metricKey, type);%0A%0A%09%09if (configs.size() == 0) %7B%0A%09%09%09com.dianping.cat.home.rule.entity.Config config = new com.dianping.cat.home.rule.entity.Config();%0A%09%09%09Condition condition = new Condition();%0A%09%09%09Subcondition descPerSubcon = new Subcondition();%0A%09%09%09Subcondition descValSubcon = new Subcondition();%0A%0A%09%09%09descPerSubcon.setType(%22DescPer%22).setText(%2250%22);%0A%09%09%09descValSubcon.setType(%22DescVal%22).setText(%22100%22);%0A%09%09%09condition.addSubcondition(descPerSubcon).addSubcondition(descValSubcon);%0A%09%09%09config.addCondition(condition);%0A%09%09%09configs.add(config);%0A%09%09%7D%0A%09%09return configs;%0A%09
 %7D%0A%0A%09@Ove

modify the config