/*
 * History			Who			What
 * 2008-05-09		wenyh		淇敼dostart,initDB閲囩敤寮傛鏂瑰紡,閬垮厤鍚姩鏃剁殑绛夊緟
 * 2009-06-04       wenyh       鍦╯imSearch涓姞鍏ヤ竴浜涙棩蹇楄皟璇曚俊鎭�.
 */

/**
 * Created:         2005-11-1 8:49:31
 * Last Modified:   2005-11-1/2005-11-1
 * Description:
 *      class CKMServerImpl
 */
package com.trs.components.ckm;

import java.io.File;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.trs.DreamFactory;
import com.trs.ckm.soap.ABSHold;
import com.trs.ckm.soap.ABSResult;
import com.trs.ckm.soap.CkmSoapException;
import com.trs.ckm.soap.CollErrorInfoDetail;
import com.trs.ckm.soap.RevDetail;
import com.trs.ckm.soap.RevHold;
import com.trs.ckm.soap.RuleCATField;
import com.trs.ckm.soap.TrsCkmSoapClient;
import com.trs.cms.auth.persistent.User;
import com.trs.components.wcm.content.persistent.Document;
import com.trs.components.wcm.content.persistent.Documents;
import com.trs.infra.BaseServer;
import com.trs.infra.I18NMessage;
import com.trs.infra.common.WCMDatainvalidException;
import com.trs.infra.common.WCMException;
import com.trs.infra.persistent.WCMFilter;
import com.trs.infra.persistent.db.DBManager;
import com.trs.infra.support.config.ConfigServer;
import com.trs.infra.util.CMyDateTime;
import com.trs.infra.util.CMyFile;
import com.trs.infra.util.CMyString;
import com.trs.infra.util.ExceptionNumber;
import com.trs.infra.util.Reports;

/**
 * Title: TRS 鍐呭鍗忎綔骞冲彴锛圱RS WCM锛� <BR>
 * Description: <BR>
 * 鏅鸿兘淇℃伅澶勭悊鏈嶅姟鍣� <BR>
 * Copyright: Copyright (c) 2004-2005 TRS淇℃伅鎶�鏈湁闄愬叕鍙� <BR>
 * Company: TRS淇℃伅鎶�鏈湁闄愬叕鍙�(www.trs.com.cn) <BR>
 * 
 * @author TRS淇℃伅鎶�鏈湁闄愬叕鍙�
 * @version 1.0
 */

public class TRSCKMServer extends BaseServer implements ICKMServer {
	private static org.apache.log4j.Logger logger = org.apache.log4j.Logger
			.getLogger(TRSCKMServer.class);

	public static final String KEY_CKM_SERVER = "CKM_SERVER";

	public static final String KEY_CKM_PORT = "CKM_PORT";

	public static final String KEY_CKM_USERNAME = "CKM_USERNAME";

	public static final String KEY_CKM_PASSWORD = "CKM_PASSWORD";

	public static final String KEY_CKM_ENABLE_SIMSEARCH = "CKM_ENABLE_SIMSEARCH";

	public static final String KEY_CKM_ENABLE_EXTRACT = "CKM_ENABLE_EXTRACT";

	public static final String KEY_CKM_ENABLE_AUTOCHECK = "CKM_ENABLE_AUTOCHECK";

	public static final String KEY_CKM_HAS_OPEN = "CKM_HAS_OPEN";

	private int m_nKeywordNum = 10;// 鍏抽敭璇嶄釜鏁�

	private int m_nAbsLenPercent = 50;// 鎽樿闀垮簲鐧惧垎姣�

	private ConfigServer m_oConfigServer = ConfigServer.createInstance();

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.trs.components.ckm.ICKMServer#validateCKMServer(java.lang.String,
	 * java.lang.String, java.lang.String, java.lang.String)
	 */
	public Reports validateCKMServer(String _sServerIP, String _sServerPort,
			String _sUserName, String _sPassword) {
		Reports result = new Reports(I18NMessage.get(TRSCKMServer.class,
				"TRSCKMServer.label1", "鏍￠獙CKM鏈嶅姟鍣ㄦ槸鍚︽甯�"));
		if (_sServerIP == null || _sServerPort == null || _sUserName == null
				|| _sPassword == null) {
			result.addFailedReport(I18NMessage.get(TRSCKMServer.class,
					"TRSCKMServer.label2", "鍙傛暟鏃犳晥锛�"), null);
			return result;
		}

		TrsCkmSoapClient oCKMSoapClient = new TrsCkmSoapClient();
		oCKMSoapClient.SetKeepAlive(true);
		oCKMSoapClient.SetServer("http://" + _sServerIP + ":" + _sServerPort,
				_sUserName, _sPassword);

		ABSHold absParam = new ABSHold(300, 50);
		try {
			oCKMSoapClient.ABSext("test", absParam);
			result.addSucessedReport(I18NMessage.get(TRSCKMServer.class,
					"TRSCKMServer.label3", "鏍￠獙鎴愬姛"));
		} catch (Exception e) {
			logger.error("validateCKMServer failed:", e);
			result.addFailedReport(I18NMessage.get(TRSCKMServer.class,
					"TRSCKMServer.label4", "CKM鏈嶅姟鍣╗http://") + _sServerIP + ":"
					+ _sServerPort + I18NMessage.get(TRSCKMServer.class,
							"TRSCKMServer.label5", "]鏍￠獙澶辫触锛�"),
					e);
		}

		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.trs.components.ckm.ICKMServer#setCKMServer(java.lang.String,
	 * java.lang.String, java.lang.String, java.lang.String)
	 */
	public void setCKMServer(String _sServerIP, String _sServerPort,
			String _sUserName, String _sPassword) {
		try {
			m_oConfigServer.updateConfigValue(KEY_CKM_SERVER, _sServerIP);
			m_oConfigServer.updateConfigValue(KEY_CKM_PORT, _sServerPort);
			m_oConfigServer.updateConfigValue(KEY_CKM_USERNAME, _sUserName);
			m_oConfigServer.updateConfigValue(KEY_CKM_PASSWORD, _sPassword);
		} catch (Exception e) {
			logger.error(I18NMessage.get(TRSCKMServer.class,
					"TRSCKMServer.label6", "鏇存柊CKMServer鐨勯厤缃俊鎭け璐ワ紒"), e);
		}

		if (!this.isStarted()) {
			this.startup();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.trs.components.ckm.ICKMServer#enableCKMService(boolean, boolean,
	 * boolean)
	 */
	public void enableCKMService(boolean _bSimSearch, boolean _bAutorExtract,
			boolean _bAutoCheck) {
		try {
			m_oConfigServer.updateConfigValue(KEY_CKM_ENABLE_SIMSEARCH,
					_bSimSearch ? "true" : "false");
			m_oConfigServer.updateConfigValue(KEY_CKM_ENABLE_EXTRACT,
					_bAutorExtract ? "true" : "false");
			m_oConfigServer.updateConfigValue(KEY_CKM_ENABLE_AUTOCHECK,
					_bAutoCheck ? "true" : "false");
		} catch (Exception e) {
			logger.error(I18NMessage.get(TRSCKMServer.class,
					"TRSCKMServer.label6", "鏇存柊CKMServer鐨勯厤缃俊鎭け璐ワ紒"), e);
		}

		try {
			String sWCMPath = m_oConfigServer.getInitProperty("WCM_PATH");
			if (sWCMPath == null) {
				logger.error(I18NMessage.get(TRSCKMServer.class,
						"TRSCKMServer.label7", "WCM_PATH璁剧疆閿欒锛�"));
				return;
			}
			sWCMPath = CMyString.setStrEndWith(sWCMPath, File.separatorChar);

			String sConfigFile = sWCMPath + "html_edit/js/trsckm_config.js";
			String sFileContent = "config.AutoExtract 	= "
					+ (_bAutorExtract ? "true" : "false") + "\n";
			sFileContent += "config.AutoCheck 	= "
					+ (_bAutoCheck ? "true" : "false") + "\n";
			CMyFile.writeFile(sConfigFile, sFileContent);

			String sSimSearchConfigFile = sWCMPath
					+ "js/validator/CWCMCKMDocumentSimValidator_config.js";
			String sSimSearchFileContent = "var WCMCKMSimSearchConfig = new Object();\n";
			sSimSearchFileContent += "WCMCKMSimSearchConfig.enable 	= "
					+ (_bSimSearch ? "true" : "false") + "\n";
			CMyFile.writeFile(sSimSearchConfigFile, sSimSearchFileContent);
		} catch (Exception e) {
			logger.error(I18NMessage.get(TRSCKMServer.class,
					"TRSCKMServer.label8", "鍚屾鏇存柊鐩稿叧閰嶇疆鏂囦欢澶辫触锛�"), e);
		}
	}

	// TODO 鍙厤缃�
	private final String SIM_MODEL_WCM52 = "TRSWCM52";

	private TrsCkmSoapClient m_oCKMSoapClient = null;

	private static ICKMServer s_oCKMServer = null;// new TRSCKMServer();

	public static ICKMServer getInstance() {
		if (s_oCKMServer == null) {
			s_oCKMServer = (ICKMServer) DreamFactory
					.createObjectById("ICKMServer");
		}

		try {
			if (s_oCKMServer.isNeedStartup())
				s_oCKMServer.startup();
		} catch (Exception e) {
			logger.error(I18NMessage.get(TRSCKMServer.class,
					"TRSCKMServer.label9", "鍚姩CKM Server澶辫触锛�"), e);
		}

		return s_oCKMServer;
	}

	public String[] extractAbstractAndKeywords(String _sContent)
			throws WCMException {
		if (m_oCKMSoapClient == null)
			throw new WCMDatainvalidException(
					I18NMessage.get(TRSCKMServer.class, "TRSCKMServer.label10",
							"TRS鐭ヨ瘑绠＄悊鏈嶅姟鍣ㄦ病鏈夊惎鍔ㄦ垨鑰呭嚭鐜颁簡鍏跺畠寮傚父锛�"));

		try {
			ABSHold absParam = new ABSHold(m_nKeywordNum, m_nAbsLenPercent);

			ABSResult result = m_oCKMSoapClient.ABSext(_sContent, absParam);
			if (result == null) {
				logger.error(I18NMessage.get(TRSCKMServer.class,
						"TRSCKMServer.label11", "CKM 鏈嶅姟鍙戠敓寮傚父锛�"));
				return null;
			}

			return new String[] { result.getabs(), result.getwordlist() };
		} catch (CkmSoapException e) {
			throw new WCMException(
					I18NMessage.get(TRSCKMServer.class, "TRSCKMServer.label10",
							"TRS鐭ヨ瘑绠＄悊鏈嶅姟鍣ㄦ病鏈夊惎鍔ㄦ垨鑰呭嚭鐜颁簡鍏跺畠寮傚父锛�"),
					e);
		}
	}

	public Documents simSearch(int _nDocId, String _sContent) {
		User system = User.getSystem();

		if (CMyString.isEmpty(_sContent)) {
			return new Documents(system);
		}
		long start = 0;
		try {
			// TODO 闇�瑕侀厤缃�
			RevHold simParams = new RevHold(0, 0.8f);

			if (logger.isDebugEnabled()) {
				start = System.currentTimeMillis();
				logger.debug("simsearch begin...");
			}
			RevDetail[] pRevDetail = m_oCKMSoapClient
					.SimRetrieveByText(SIM_MODEL_WCM52, simParams, _sContent);

			if (pRevDetail == null || pRevDetail.length == 0) {
				return new Documents(system);
			}
			if (logger.isDebugEnabled()) {
				logger.debug("simsearch end...,time used(ms):"
						+ (System.currentTimeMillis() - start));
			}

			Set idSet = new HashSet(pRevDetail.length);
			for (int i = 0, len = pRevDetail.length; i < len; i++) {
				idSet.add(pRevDetail[i].getid());
			}
			idSet.remove(String.valueOf(_nDocId));
			if (idSet.isEmpty()) {
				return new Documents(system);
			}
			StringBuffer buff = new StringBuffer(128);
			Iterator idItr = idSet.iterator();
			while (idItr.hasNext()) {
				buff.append(',').append(idItr.next());
			}
			idSet.clear();

			return Documents.findByIds(system, buff.toString());
		} catch (Throwable e) {
			logger.error(I18NMessage.get(TRSCKMServer.class,
					"TRSCKMServer.label12", "鐩镐技鎬ф绱㈠け璐ワ紒"), e);
			return Documents.createNewInstance(system);
		} finally {
			if (logger.isDebugEnabled()) {
				long end = System.currentTimeMillis();
				long total = end - start;
				if (total > 10000) {// 10s
					logger.debug("sim search total time(s):" + (total / 1000));
				}
			}
		}
	}

	public CollErrorInfoDetail[] collateText(String _sContent)
			throws WCMException {
		if (m_oCKMSoapClient == null)
			throw new WCMDatainvalidException(
					I18NMessage.get(TRSCKMServer.class, "TRSCKMServer.label10",
							"TRS鐭ヨ瘑绠＄悊鏈嶅姟鍣ㄦ病鏈夊惎鍔ㄦ垨鑰呭嚭鐜颁簡鍏跺畠寮傚父锛�"));

		try {
			CollErrorInfoDetail[] result = m_oCKMSoapClient
					.CollationText(_sContent);
			return result;

			// String[][] pResult = new String[result.length][2];
			// for (int i = 0; i < result.length; i++) {
			// CollErrorInfoDetail detail = result[i];
			// pResult[i][0] = detail.geterrinfo();
			// pResult[i][1] = detail.getwordlist()[0].getword();
			// }
			//
			// return pResult;
		} catch (CkmSoapException e) {
			logger.error(I18NMessage.get(TRSCKMServer.class,
					"TRSCKMServer.label11", "CKM 鏈嶅姟鍙戠敓寮傚父锛�"), e);
			throw new WCMException(
					I18NMessage.get(TRSCKMServer.class, "TRSCKMServer.label10",
							"TRS鐭ヨ瘑绠＄悊鏈嶅姟鍣ㄦ病鏈夊惎鍔ㄦ垨鑰呭嚭鐜颁簡鍏跺畠寮傚父锛�"),
					e);
		}
	}

	public void updateSimIndex(Document _oDocument) {
		if (!isOpenCKMServer()) {
			return;
		}

		try {
			String content = _oDocument.getContent();
			if (CMyString.isEmpty(content)) {
				return;
			}
			m_oCKMSoapClient.SimUpdateIndex(SIM_MODEL_WCM52,
					_oDocument.getKey().toString(), _oDocument.getContent());
		} catch (Exception e) {
			logger.error("update " + _oDocument + " sim index error!", e);
		}
	}

	public void updateSimIndex(int _nId, String _sContent) {
		if (!isOpenCKMServer()) {
			return;
		}
		
		if (CMyString.isEmpty(_sContent)) {
			return;
		}
		try {
			m_oCKMSoapClient.SimUpdateIndex(SIM_MODEL_WCM52,
					String.valueOf(_nId), _sContent);
		} catch (Exception e) {
			logger.error("update [id=" + _nId + "] sim index error!", e);
		}
	}

	public void deleteSimIndex(Document _oDocument) {
		try {
			m_oCKMSoapClient.SimDeleteIndexByID(SIM_MODEL_WCM52,
					_oDocument.getKey().toString());
		} catch (Exception e) {
			logger.error("update " + _oDocument + " sim index error!", e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.trs.infra.BaseServer#doShutdown()
	 */
	protected void doShutdown() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.trs.infra.BaseServer#doStart()
	 */
	protected boolean doStart() {
		if (!isOpenCKMServer()) {
			logger.info("CKMServer is not open by AdminUser!");
			return false;

		}
		if (this.validateCKMServer().getSucessedReporterAt(0) == null) {
			logger.error("Validate CKM Error!");
			return false;
		}

		m_oCKMSoapClient = new TrsCkmSoapClient();
		m_oCKMSoapClient.SetKeepAlive(true);
		m_oCKMSoapClient.SetServer(
				"http://" + getCKMServerIP() + ":" + getCKMServerPort(),
				getCKMServerUserName(), getCKMServerPassword());

		if (isEnableSimSearch()) {
			initSimDB();
		}

		try {
			String temp = m_oConfigServer.getSysConfigValue("CKM_KEYWORDNUM",
					"10");
			m_nKeywordNum = Integer.parseInt(temp);
			temp = m_oConfigServer.getSysConfigValue("CKM_ABSLENPERCENT", "50");
			m_nAbsLenPercent = Integer.parseInt(temp);
			if (m_nAbsLenPercent > 100) {
				m_nAbsLenPercent = 50;
			}
			// temp = m_oConfigServer.getSysConfigValue("CKM_ABSWORDNUM",
			// "200");
			// m_nAbsWordNum = Integer.parseInt(temp);
		} catch (Exception ex) {
		}
		return true;
	}

	/**
	 * 鑾峰彇CKMSoap 鎺ュ彛
	 * 
	 * @return
	 * @throws WCMException
	 */
	public TrsCkmSoapClient getSoapClient() throws WCMException {
		if (!isOpenCKMServer()) {
			logger.debug("娌℃湁鍚姩鏅鸿兘鐭ヨ瘑鏈嶅姟锛佸缓璁噰璐互鎻愬崌鐭ヨ瘑搴撶殑浠峰�硷紒");
			return null;
		}

		if (m_oCKMSoapClient == null)
			throw new WCMException("鑾峰彇CKM鎺ュ彛澶辫触锛孋KMSoapClient娌℃湁鍚姩");

		return m_oCKMSoapClient;
	}

	private void initSimDB() {
		final TrsCkmSoapClient client = m_oCKMSoapClient;
		new Thread() {
			public void run() {
				// Create Model
				try {
					client.SimDropModel(SIM_MODEL_WCM52);
				} catch (Exception e) {
					// e.printStackTrace();
				}

				try {
					client.SimCreateModel(SIM_MODEL_WCM52);
				} catch (Exception e) {
					// e.printStackTrace();
				}

				// Add Sim Content
				try {
					User system = User.getSystem();
					CMyDateTime dtStart = CMyDateTime.now()
							.dateAdd(CMyDateTime.MONTH, -3);
					String sWhere = "DocStatus>0 and DocChannel>=0 and DocType in("
							+ Document.TYPE_HTML + "," + Document.TYPE_NORMAL
							+ ") and CRTIME>="
							+ DBManager.getDBManager().getDBType().sqlDateTime(
									dtStart.toString(),
									CMyDateTime.DEF_DATETIME_FORMAT_DB);
					String sSelectField = "DocId,DocChannel,DocTitle,DocContent";
					WCMFilter filter = new WCMFilter("", sWhere, "",
							sSelectField);

					Documents documents = Documents.openWCMObjs(system, filter);
					for (int i = 0, nSize = documents.size(); i < nSize; i++) {
						Document document = (Document) documents.getAt(i);
						if (document == null)
							continue;

						client.SimUpdateIndex(SIM_MODEL_WCM52,
								String.valueOf(document.getId()),
								document.getContent());
					}
				} catch (Exception e) {
					// logger.error("鍒濆鍖栫浉浼兼�ф绱㈠尯澶辫触锛�", e);
					// ignore.
				}

			}
		}.start();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.trs.components.ckm.ICKMServer#validateCKMServer()
	 */
	public Reports validateCKMServer() {
		try {
			String sServer = m_oConfigServer.getSysConfigValue(KEY_CKM_SERVER,
					"localhost");
			String sPort = m_oConfigServer.getSysConfigValue(KEY_CKM_PORT,
					"8000");
			String sUserName = m_oConfigServer
					.getSysConfigValue(KEY_CKM_USERNAME, "admin");
			String sPassword = m_oConfigServer
					.getSysConfigValue(KEY_CKM_PASSWORD, "admin");

			return validateCKMServer(sServer, sPort, sUserName, sPassword);
		} catch (Exception e) {
			logger.error(I18NMessage.get(TRSCKMServer.class,
					"TRSCKMServer.label13", "鏍￠獙CKM璁剧疆澶辫触"), e);

			Reports reports = new Reports(I18NMessage.get(TRSCKMServer.class,
					"TRSCKMServer.label14", "鏍￠獙CKM璁剧疆澶辫触!"));
			reports.addFailedReport(I18NMessage.get(TRSCKMServer.class,
					"TRSCKMServer.label13", "鏍￠獙CKM璁剧疆澶辫触"), e);
			return reports;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.trs.components.ckm.ICKMServer#getCKMServerIP()
	 */
	public String getCKMServerIP() {
		try {
			return m_oConfigServer.getSysConfigValue(KEY_CKM_SERVER,
					"localhost");
		} catch (Exception e) {
			logger.error(I18NMessage.get(TRSCKMServer.class,
					"TRSCKMServer.label15", "鑾峰彇CKMServer鐨処P澶辫触"), e);
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.trs.components.ckm.ICKMServer#getCKMServerPort()
	 */
	public String getCKMServerPort() {
		try {
			return m_oConfigServer.getSysConfigValue(KEY_CKM_PORT, "8000");
		} catch (Exception e) {
			logger.error(I18NMessage.get(TRSCKMServer.class,
					"TRSCKMServer.label16", "鑾峰彇CKMServer鐨凱ort澶辫触"), e);
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.trs.components.ckm.ICKMServer#getCKMServerUserName()
	 */
	public String getCKMServerUserName() {
		try {
			return m_oConfigServer.getSysConfigValue(KEY_CKM_USERNAME, "admin");
		} catch (Exception e) {
			logger.error(I18NMessage.get(TRSCKMServer.class,
					"TRSCKMServer.label17", "鑾峰彇CKMServer鐨刄ser澶辫触"), e);
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.trs.components.ckm.ICKMServer#getCKMServerPassword()
	 */
	public String getCKMServerPassword() {
		try {
			return m_oConfigServer.getSysConfigValue(KEY_CKM_PASSWORD, "admin");
		} catch (Exception e) {
			logger.error(I18NMessage.get(TRSCKMServer.class,
					"TRSCKMServer.label18", "鑾峰彇CKMServer鐨凱assword澶辫触"), e);
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.trs.components.ckm.ICKMServer#isEnableSimSearch()
	 */
	public boolean isEnableSimSearch() {
		try {
			return "TRUE".equalsIgnoreCase(m_oConfigServer
					.getSysConfigValue(KEY_CKM_ENABLE_SIMSEARCH, "false"));
		} catch (Exception e) {
			logger.error(I18NMessage.get(TRSCKMServer.class,
					"TRSCKMServer.label18", "鑾峰彇CKMServer鐨凱assword澶辫触"), e);
			return false;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.trs.components.ckm.ICKMServer#isEnableAutoExtract()
	 */
	public boolean isEnableAutoExtract() {
		try {
			return "TRUE".equalsIgnoreCase(m_oConfigServer
					.getSysConfigValue(KEY_CKM_ENABLE_EXTRACT, "false"));
		} catch (Exception e) {
			logger.error(I18NMessage.get(TRSCKMServer.class,
					"TRSCKMServer.label18", "鑾峰彇CKMServer鐨凱assword澶辫触"), e);
			return false;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.trs.components.ckm.ICKMServer#isEnableAutoCheck()
	 */
	public boolean isEnableAutoCheck() {
		try {
			return "TRUE".equalsIgnoreCase(m_oConfigServer
					.getSysConfigValue(KEY_CKM_ENABLE_AUTOCHECK, "false"));
		} catch (Exception e) {
			logger.error(I18NMessage.get(TRSCKMServer.class,
					"TRSCKMServer.label18", "鑾峰彇CKMServer鐨凱assword澶辫触"), e);
			return false;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.trs.components.ckm.ICKMServer#isNeedStartup()
	 */
	public boolean isNeedStartup() {
		return !this.isStarted() && (this.isEnableAutoCheck()
				|| this.isEnableAutoExtract() || this.isEnableSimSearch());
	}

	/**
	 * @author can
	 * @add at 2007.12.26 11:05 @鍔熻兘锛� 瀵规槸鍚﹀紑鍚簡ckmserver杩涜鍒ゆ柇
	 * @return
	 */
	public boolean isOpenCKMServer() {
		// add by caohui@2017骞�7鏈�12鏃� 涓婂崍11:48:13
		// 鏈湴璋冭瘯锛屼笉寮�鍚紝涔熶笉鎻愪氦浠ｇ爜
		// return (isEnableSimSearch() || isEnableAutoCheck() ||
		// isEnableAutoExtract());
		return false;
	}

	// 浠ヤ笅鏂规硶涓� EIP鎵╁睍鐨勬柟娉�====================================================
	/**
	 * 鑾峰彇ckm閰嶇疆涓厤缃殑妫�绱㈢殑Tag闇�瑕佹绱㈢殑Model鍚嶇О
	 */
	private String getCKMModelName() {
		try {
			return m_oConfigServer.getSysConfigValue("TRS_CKM_MODELNAME",
					"demo");
		} catch (Exception e) {
			logger.error(
					I18NMessage.get(TRSCKMServer.class, "TRSCKMServer.label18",
							"鑾峰彇CKMServer鐨凾RS_CKM_MODELNAME澶辫触"),
					e);
			return null;
		}
	}

	/**
	 * 鎷奸煶妫�绱�
	 * 
	 * @param _sCurrWord
	 * @return
	 * @throws WCMException
	 */
	public String[] pySearch(String _sCurrWord) throws WCMException {
		TrsCkmSoapClient aSoapClient = getSoapClient();
		if (aSoapClient != null) {
			try {
				return aSoapClient.PySearch(_sCurrWord, getCKMModelName(), 0);
			} catch (CkmSoapException e) {
				throw new WCMException(ExceptionNumber.ERR_UNKNOWN,
						"鎷奸煶妫�绱㈠け璐ワ紝澶辫触鍘熷洜[" + e.getMessage() + "]");
			}
		}
		return null;
	}

	/**
	 * 鐭妫�绱�
	 * 
	 * @param _sCurrWord
	 * @return
	 * @throws WCMException
	 */
	public String[] dySearch(String _sCurrWord) throws WCMException {
		TrsCkmSoapClient aSoapClient = getSoapClient();
		if (aSoapClient != null) {
			try {
				return aSoapClient.DySearch(_sCurrWord, getCKMModelName(), 0);
			} catch (CkmSoapException e) {
				throw new WCMException(ExceptionNumber.ERR_UNKNOWN,
						"鐭妫�绱㈠け璐ワ紝澶辫触鍘熷洜[" + e.getMessage() + "]");
			}
		}
		return null;
	}

	/**
	 * 涓婁紶瑙勫垯鍒嗙被妯℃澘(浠ョ洰褰曟柟寮�,浜岃繘鍒朵笂浼�)
	 * 
	 * @param _sDirPath
	 *            鏈湴妯℃澘瀛樻斁璺緞
	 * @param _iOption
	 *            閫夐」(榛樿0)
	 * @param _sCATModelName
	 *            妯℃澘鍚�
	 * @return
	 * @throws WCMException
	 */
	public int doRuleCATUploadModel(String _sCATModelName, int _iOption,
			String _sDirPath) throws WCMException {
		TrsCkmSoapClient aSoapClient = getSoapClient();
		if (aSoapClient != null) {
			try {
				return aSoapClient.RuleCATUploadModel(_sCATModelName, _iOption,
						_sDirPath);
			} catch (CkmSoapException e) {
				throw new WCMException(ExceptionNumber.ERR_UNKNOWN,
						"涓婁紶瑙勫垯鍒嗙被妯℃澘澶辫触锛屽け璐ュ師鍥燵" + e.getMessage() + "]");
			}
		}
		return 0;
	}

	/**
	 * 鎵цCKM瑙勫垯鍒嗙被
	 * 
	 * @param _sCATModelName
	 *            瑙勫垯鍒嗙被鐢ㄧ殑妯℃澘鍚�
	 * @param sContents
	 *            瑙勫垯鍒嗙被鐨勫唴瀹规暟缁�
	 * @param sContentNames
	 *            瑙勫垯鍒嗙被鐨勫唴瀹瑰搴斿瓧娈垫暟缁�
	 * @return 瑙勫垯鍒嗙被鐨勭粨鏋�
	 * @throws WCMException
	 */
	public String[] doRuleCATClassify(String _sCATModelName, String[] sContents,
			String[] sContentNames) throws WCMException {
		TrsCkmSoapClient aSoapClient = getSoapClient();

		if (sContents == null || sContentNames == null)
			return null;
		if (sContents.length != sContentNames.length)
			return null;

		if (aSoapClient != null) {
			RuleCATField[] aRuleCATFields = new RuleCATField[sContents.length];
			for (int i = 0; i < sContents.length; i++) {
				RuleCATField aRuleCATField = new RuleCATField(sContents[i],
						sContentNames[i]);
				aRuleCATFields[i] = aRuleCATField;
			}
			try {
				if (logger.isDebugEnabled()) {
					System.out.println(
							aRuleCATFields.length + ":" + _sCATModelName + ":"
									+ sContents[0] + ":" + sContentNames[0]);
				}
				String sClassifyResult = aSoapClient
						.RuleCATClassifyText(_sCATModelName, aRuleCATFields);
				if (logger.isDebugEnabled()) {
					System.out.println("sClassifyResult:" + sClassifyResult);
				}
				if (sClassifyResult != null) {
					return CMyString.split(sClassifyResult, ";");
				}
				return null;
			} catch (CkmSoapException e) {
				throw new WCMException(ExceptionNumber.ERR_UNKNOWN,
						"璋冪敤CKM瑙勫垯鍒嗙被澶辫触锛屽け璐ュ師鍥燵" + e.getMessage() + "]");
			}
		}
		return null;
	}

}