using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Yb.CustomTemplating;
using Yb.DbSchemaReader.DataSchema;
using Yb.Plugin.Base;
using Yb.Utility;
namespace Yb.Plugin.CastleDevExpress.Wpf
{
public abstract class CastleDxWpfPluginRepositoryBase : PluginRepositoryBase
{
private int _totalCount =
0;
private int _currentIndex =
0;
protected virtual int GetShouldBuildCodeNum(
int listCount)
{
var templateCount = GetTemplateInfos().Count();
return listCount * (templateCount);
}
/// <summary> /// 生成代码 /// </summary> /// <param name="dataSource"></param> /// <param name="templateInfos"></param> /// <returns></returns> public override IEnumerable<TemplateInfo> BuildCodes(
object dataSource, IEnumerable<TemplateInfo> templateInfos,
object dataSourceInfo)
{
// 参数类型转换 var tableInfos = BaseInfoUtility.ObjectToDatabaseTableList(dataSource).Where(c => c.Checked).ToList();
// 生成结果 var templateList =
new List<TemplateInfo>();
_totalCount = GetShouldBuildCodeNum(tableInfos.Count);
_currentIndex =
0;
foreach (
var templateInfo
in templateInfos)
{
if (!templateInfo.BuildEnable)
continue;
// 读模板信息 templateInfo.TemplateContent = FileUtility.ReadFile(templateInfo.TemplateRelativePath);
// 判断模板类型,是否每张表都需生成一个模板 if ((templateInfo.Tag & StaticResources.IsTableInfoListOfTemplateArg) ==
0)
{
foreach (
var tableInfo
in tableInfos)
{
// 复制模板,防止生成后下一个循环被覆盖 var currentTemplateInfo = (TemplateInfo)templateInfo.Clone();
// 生成代码 BuildTableCode(
" CurrentTable ", templateList, tableInfo, currentTemplateInfo);
}
}
else {
// 生成 App.Config 文件 if (templateInfo.ExportFileNameFormatString.Equals(
" App.config ", StringComparison.OrdinalIgnoreCase))
{
var sourceInfo = dataSourceInfo
as DataSourceInfo;
// 创建Web.Config代码 if (sourceInfo !=
null)
BuildAppConfigCode(templateList, templateInfo, tableInfos, sourceInfo);
}
else {
// 创建如项目文件等需要传入“表对象集合”为参数的模板代码 BuildTablesCode(templateList, templateInfo, tableInfos);
}
}
}
return templateList;
}
/// <summary> /// 创建和表有关的代码 /// </summary> /// <param name="templateArgName"></param> /// <param name="templateList"></param> /// <param name="tableInfo"></param> /// <param name="templateInfo"></param> private void BuildTableCode(
string templateArgName, List<TemplateInfo> templateList, DatabaseTable tableInfo, TemplateInfo templateInfo)
{
try {
// 代码生成关键代码,根据模板生成代码 templateInfo.ExportContent = Template.Transform(templateInfo.TemplateContent, templateArgName,
tableInfo);
// 更新模板标题,标题也是要生成文件的名称 templateInfo.Title = BaseInfoUtility.ReplaceString(templateInfo.ExportFileNameFormatString,
tableInfo.Name);
templateInfo.ExportRelativePath = BaseInfoUtility.ReplaceString(templateInfo.ExportRelativePathFormatString,
tableInfo.Name);
AddCodeList(templateList, templateInfo);
}
catch (Exception er)
{
NotifyException(templateInfo, er);
}
}
/// <summary> /// 创建和数据库有关的代码 /// </summary> /// <param name="templateList"></param> /// <param name="templateInfo"></param> /// <param name="tableInfos"></param> private void BuildTablesCode(List<TemplateInfo> templateList, TemplateInfo templateInfo, List<DatabaseTable> tableInfos)
{
try {
templateInfo.ExportContent = Template.Transform(templateInfo.TemplateContent,
" TableInfos ",
tableInfos);
templateInfo.Title = templateInfo.ExportFileNameFormatString;
templateInfo.ExportRelativePath = templateInfo.ExportRelativePathFormatString;
AddCodeList(templateList, templateInfo);
}
catch (Exception er)
{
NotifyException(templateInfo, er);
}
}
/// <summary> /// 创建Web.Config 代码 /// </summary> /// <param name="templateList"></param> /// <param name="templateInfo"></param> /// <param name="tableInfos"></param> private void BuildAppConfigCode(List<TemplateInfo> templateList, TemplateInfo templateInfo, List<DatabaseTable> tableInfos, DataSourceInfo dataSourceInfo)
{
try {
templateInfo.ExportContent = Template.Transform(templateInfo.TemplateContent,
" DataSourceInfo ",
dataSourceInfo);
templateInfo.Title = templateInfo.ExportFileNameFormatString;
templateInfo.ExportRelativePath = templateInfo.ExportRelativePathFormatString;
AddCodeList(templateList, templateInfo);
}
catch (Exception er)
{
NotifyException(templateInfo, er);
}
}
/// <summary> /// 代码创建后进行事件通知 /// </summary> /// <param name="templateList"></param> /// <param name="templateInfo"></param> private void AddCodeList(List<TemplateInfo> templateList, TemplateInfo templateInfo)
{
templateList.Add(templateInfo);
_currentIndex++;
OnNotifyChanged(
new NotifyChangedEventArgs(NotifyType.Seccessful,
string.Format(
" 代码 {0} 生成成功 ", templateInfo.Title)
, _currentIndex, _totalCount));
}
/// <summary> /// 通知代码创建失败 /// </summary> /// <param name="templateInfo"></param> /// <param name="er"></param> private void NotifyException(TemplateInfo templateInfo, Exception er)
{
_currentIndex++;
OnNotifyChanged(
new NotifyChangedEventArgs(NotifyType.Error,
string.Format(
" 代码 {0} 生成失败。{1} ", templateInfo.Title, er.Message),
_currentIndex, _totalCount));
}
public override IEnumerable<TemplateInfo> GetTemplateInfos()
{
return StaticResources.GetTemplateInfos();
}
}
}