/**
 * Created:         2005.08.02
 * Last Modified:   2006.01.19
 * Description:
 *      class TRSParserBaseTest
 * Update Log:
 *	2005.01.19	FuChengrui
 *		改用新的(只使用PublishPageContext)构造PublishTagContext对象
 *
 *	2005.12.27	FuChengrui
 *		改用新的(使用TagItem)构造PublishTagContext对象
 */
package com.trs;

import com.trs.components.common.publish.domain.publisher.PublishPageContext;
import com.trs.components.common.publish.domain.publisher.PublishTagContext;
import com.trs.components.common.publish.domain.publisher.PublishTaskContext;
import com.trs.components.common.publish.domain.tagparser.TagItem;
import com.trs.components.common.publish.domain.tagparser.TagParseHelper;
import com.trs.components.common.publish.persistent.element.IPublishContent;
import com.trs.components.common.publish.persistent.element.IPublishElement;
import com.trs.components.common.publish.persistent.element.IPublishFolder;
import com.trs.components.common.publish.persistent.element.PublishElementFactory;
import com.trs.infra.common.WCMException;
import com.trs.infra.util.CMyException;

/**
 * Title: TRS 内容协作平台（TRS WCM） <BR>
 * Description: <BR>
 * TODO <BR>
 * Copyright: Copyright (c) 2004-2005 TRS信息技术有限公司 <BR>
 * Company: TRS信息技术有限公司(www.trs.com.cn) <BR>
 * 
 * @author TRS信息技术有限公司
 * @version 1.0
 */

public abstract class TRSParserBaseTest extends TRSWCMBaseTest {
	private static org.apache.log4j.Logger logger = org.apache.log4j.Logger
			.getLogger(TRSParserBaseTest.class);

	/**
	 * 构造一个指定的Content
	 * 
	 * @param _contentContext
	 *            长度为4的数组：1：FolderType 2：FolderId 3：ContentType 4：ContentId
	 * @return
	 */
	protected IPublishContent makeContent(int[] _contentContext) {
		if (_contentContext == null) {
			logger.error("[makeContent]所需的测试数据不正确！需要传入长度为2或4的整型数组");
			fail("[makeContent]所需的测试数据不正确！需要传入长度为2或4的整型数组");
		}

		int nFolderType = 0;
		int nFolderId = 0;
		int nContentType = 0;
		int nContentId = 0;

		if (_contentContext.length == 2) {
			nContentType = _contentContext[0];
			nContentId = _contentContext[1];

		} else if (_contentContext.length >= 4) {
			nFolderType = _contentContext[0];
			nFolderId = _contentContext[1];
			nContentType = _contentContext[2];
			nContentId = _contentContext[3];
		} else {
			logger.error("[makeContent]所需的测试数据不正确！需要传入长度为2或4的整型数组");
			fail("[makeContent]所需的测试数据不正确！需要传入长度为2或4的整型数组");
		}

		try {
			IPublishContent content = (IPublishContent) PublishElementFactory
					.lookupElement(nContentType, nContentId);
			if (content == null) {
				logger.error("[makeContent]所需的Content数据不正确！ContentTypeConfig="
						+ nContentType + " ContentId=" + nContentId);
				fail("[makeContent]所需的Content数据不正确！ContentTypeConfig="
						+ nContentType + " ContentId=" + nContentId);
			}

			IPublishFolder folder = null;
			if (nFolderId != 0) {
				try {
					folder = (IPublishFolder) PublishElementFactory
							.lookupElement(nFolderType, nFolderId);
					if (folder == null) {
						logger
								.error("[makeContent]所需的Folder数据不正确！FolderTypeConfig="
										+ nFolderType
										+ " FolderId="
										+ nFolderId);
						fail("[makeContent]所需的Folder数据不正确！FolderTypeConfig="
								+ nFolderType + " FolderId=" + nFolderId);
					}
				} catch (Exception ex) {
					logger.error("测试[makeContent]-获取Folder出现异常！", ex);
					fail("测试[makeContent]-获取Folder出现异常！\n"
							+ CMyException.getStackTraceText(ex));
				}
			} else {
				folder = content.getOriginalHome();
			}

			content.setFolder(folder);
			return content;
		} catch (Exception ex) {
			logger.error("测试[makeContent]-makeContent出现异常！", ex);
			fail("测试[makeContent]-makeContent出现异常！\n"
					+ CMyException.getStackTraceText(ex));
		}

		return null;
	}

	/**
	 * 构造指定的Folder
	 * 
	 * @param _folderContext
	 *            长度为2的数组：1：Folder Type 2：Folder Id
	 * @return
	 */
	protected IPublishFolder makeFolder(int[] _folderContext) {
		if (_folderContext == null || _folderContext.length < 2) {
			logger.error("[makeContent]所需的测试数据不正确！需要传入长度为4的整型数组");
			fail("[makeContent]所需的测试数据不正确！需要传入长度为4的整型数组");
		}

		int nFolderType = _folderContext[0];
		int nFolderId = _folderContext[1];

		IPublishFolder folder = null;
		try {
			folder = (IPublishFolder) PublishElementFactory.lookupElement(
					nFolderType, nFolderId);
			if (folder == null) {
				logger.error("[makeFolder]所需的Folder数据不正确！FolderTypeConfig="
						+ nFolderType + " FolderId=" + nFolderId);
				fail("[makeFolder]所需的Folder数据不正确！FolderTypeConfig="
						+ nFolderType + " FolderId=" + nFolderId);
			}
		} catch (Exception ex) {
			logger.error("测试[makeFolder]-获取Folder出现异常！", ex);
			fail("测试[makeFolder]-获取Folder出现异常！\n"
					+ CMyException.getStackTraceText(ex));
		}
		return folder;
	}

	/**
	 * @param data
	 * @throws WCMException
	 * @throws Exception
	 */
	protected void parse(String[] data, IPublishElement _pageElement,
			IPublishElement _upperHost) throws WCMException, Exception {
		// 3.构造发布的相关上下文参数
		PublishTaskContext taskContent = new PublishTaskContext(null,
				_pageElement.getFolder());
		PublishPageContext pageContext = new PublishPageContext(_pageElement,
				taskContent);
		PublishTagContext tagContext = new PublishTagContext(pageContext);
		// 4.遍历测试
		TagItem tagItem;
		String[] results = null;
		for (int i = 0; i < data.length; i++) {
			tagItem = TagParseHelper.strToTagItem(data[i]);
			System.out.println(data[i]);
			System.out.println("[" + i + "]" + tagItem.toString());

			long lStart = System.currentTimeMillis();
			try {
				results = TagParseHelper.parseItem(tagItem, tagContext);
				if (pageContext.getWarnings() != null)
					System.out.println("Warning:" + pageContext.getWarnings());
			} catch (Exception ex) {
				ex.printStackTrace(System.out);
				fail();
			} finally {
				pageContext.clear();
			}

			long lEnd = System.currentTimeMillis();
			System.out.println("time used: " + (lEnd - lStart) + "ms");
			if (results != null) {
				for (int j = 0; j < results.length; j++) {
					System.out.println("result[" + j + "]: " + results[j]);
				}
			}
		}
	}

	/**
	 * @param data
	 * @param arFolder
	 * @param arContent
	 * @throws WCMException
	 * @throws Exception
	 */
	protected void parse(String[] data, int[][] arFolder, int[][] arContent)
			throws WCMException, Exception {
		for (int i = 0; i < arContent.length; i++) {
			IPublishFolder folder = makeFolder(arFolder[i]);
			IPublishContent content = makeContent(arContent[i]);

			if (arContent[i].length > 4 && arContent[i][4] == 1) {// 在细览页面中解析
				parse(data, content, content);
			} else {// 在概览页面中解析
				parse(data, folder, content);
			}
		}
		// endfor
	}

	/**
	 * 在指定的站点或栏目下发布指定的模板片段
	 * 
	 * @param data
	 *            需要发布的模板片段内容，参数类型为字符串数组，可以指定多个需要发布的模板片段
	 * @param arFolder
	 *            当前模板片段所依赖的站点或栏目的信息，因为同一个模板在不同站点或栏目对象下发布出来的内容是不一样的；
	 *            指定站点或栏目对象的FolderType和FolderId，同时可以指定多个，如：{{101, 2}, {103, 1}}
	 * @throws WCMException
	 * @throws Exception
	 */
	protected void parseFolders(String[] data, int[][] arFolder)
			throws WCMException, Exception {
		for (int i = 0; i < arFolder.length; i++) {
			IPublishFolder folder = makeFolder(arFolder[i]);
			parse(data, folder, null);
		}
	}

	/**
	 * 在指定的文档对象下发布指定的模板片段
	 * 
	 * @param data
	 *            需要发布的模板片段内容，参数类型为字符串数组，可以指定多个需要发布的模板片段
	 * @param arFolder
	 *            当前模板片段所依赖的文档的信息，因为同一个模板在不同文档下发布出来的内容是不一样的；
	 *            指定文档对象的FolderType、FolderId
	 *            （可选，默认为当前文档自身所在的栏目）及ContentType和ContentId ，同时可以指定多个，如：{{101,
	 *            2, 605, 32}, {605, 2}}
	 * @throws WCMException
	 * @throws Exception
	 */
	protected void parseContents(String[] data, int[][] arContent)
			throws WCMException, Exception {
		for (int i = 0; i < arContent.length; i++) {
			IPublishContent content = makeContent(arContent[i]);
			parse(data, content, null);
		}
	}
}