我們來欣賞程式的美
發表於 : 週五 10月 21, 2011 4:42 pm
代碼: 選擇全部
public class TwCardContacts
{
private readonly IExtendedAttrDefService _extendedAttrDefService = RheaFactory.ServiceFactory.GetService<IExtendedAttrDefService>();
private readonly IContactMechTypeService _contactMechTypeService = RheaFactory.ServiceFactory.GetService<IContactMechTypeService>();
private IList<IContactMechType> _contactMechTypes;
private IList<IExtendedAttrDef> _personAccountExtendedAttrDefs;
private string[] _otherCustomerContactsExtendedAttrDefs;
private IList<T> GetContactMechTypeBySubType<T>(Func<T, bool> predicate)
{
return _contactMechTypes.OfType<T>().Where(predicate).ToList();
}
private IList<T> GetExtendedAttrDef<T>(Func<T, bool> predicate)
{
return _personAccountExtendedAttrDefs.OfType<T>().Where(predicate).ToList();
}
private IList<T> GetOtherExtendedAttrDef<T>(Func<T, bool> predicate)
{
return _otherCustomerContactsExtendedAttrDefs.OfType<T>().Where(predicate).ToList();
}
private ITextBox InitTextboxLayout()
{
ITextBox textbox = _extendedAttrDefService.MakeAttrLayout<ITextBox>();
textbox.IsMultiLines = false;
textbox.MaxLength = 255;
return textbox;
}
private void AddExtendedAttrDef(string extendedAttrDefName)
{
IExtendedAttrDef extendedAttrDef = _extendedAttrDefService.MakeExtendedAttrDef(InitTextboxLayout());
extendedAttrDef.DefaultValue = "";
extendedAttrDef.IsRequired = false;
extendedAttrDef.Name = extendedAttrDefName;
extendedAttrDef.Order = 255;
extendedAttrDef.Domain = EnumDomain.PersonAccount;
_extendedAttrDefService.CreateOrUpdate(extendedAttrDef);
}
public bool GenMetadata(string param)
{
_contactMechTypes = _contactMechTypeService.LoadAll().OfType<IContactMechType>().ToList();
_personAccountExtendedAttrDefs = _extendedAttrDefService.GetByDomain((EnumDomain)Enum.Parse(typeof(EnumDomain), "PersonAccount")).Cast<IExtendedAttrDef>().ToList();
//// [固定欄位 - 不處理欄位]
//// 全名 CurrentName / 名字 FirstName / 姓氏 LastName
//// 暱稱 NickName
//// 生日 Birthday
//// 性別 Gender
//// 備註 Notes
//// 類別 Categories
//// [被取代欄位] 公司 Company / 職稱 JobTitle / 部門 Department
string[] otherExtendedAttrDefs = new[] { "Company".ToI18n(GetType().Name), "JobTitle".ToI18n(GetType().Name), "Department".ToI18n(GetType().Name)};
_otherCustomerContactsExtendedAttrDefs = new[] { "Company".ToI18n(typeof(CustomerContacts).Name), "JobTitle".ToI18n(typeof(CustomerContacts).Name), "Department".ToI18n(typeof(CustomerContacts).Name) };
//// [擴充欄位]
//// 其他 Other / 星座 Constellation / 血型 BloodType / 學歷 Education / 配偶 Spouse / 統一編號 UniformNumber
string[] extendedAttrDefs = new[] { "Other".ToI18n(GetType().Name), "Constellation".ToI18n(GetType().Name), "BloodType".ToI18n(GetType().Name), "Education".ToI18n(GetType().Name), "Spouse".ToI18n(GetType().Name), "UniformNumber".ToI18n(GetType().Name) };
//////// 商務電話 BusinessPhone / 住家電話 HomePhone / 公司總機 CompanySwitchboard
string[] fixedPhones = new[] { "BusinessPhone".ToI18n(GetType().Name), "HomePhone".ToI18n(GetType().Name), "CompanySwitchboard".ToI18n(GetType().Name) };
//////// 專線電話 Hotline / 手機電話 MobilePhone
string[] mobilePhones = new[] { "Hotline".ToI18n(GetType().Name), "MobilePhone".ToI18n(GetType().Name) };
//////// 商務傳真 BusinessFax / 住家傳真 HomeFax
string[] faxs = new[] { "BusinessFax".ToI18n(GetType().Name), "HomeFax".ToI18n(GetType().Name) };
//////// 商務地址 CompanyAddress
string[] addrs = new[] { "CompanyAddress".ToI18n(GetType().Name) };
//////// Messenger Messenger / Skype Skype
string[] ims = new[] { "Messenger".ToI18n(GetType().Name), "Skype".ToI18n(GetType().Name) };
//////// 商務電子郵件 BusinessAddress
string[] emails = new[] { "BusinessAddress".ToI18n(GetType().Name) };
//////// 商務網址 BusinessWebsite
string[] urls = new[] { "BusinessWebsite".ToI18n(GetType().Name) };
string[] fields = param.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var field in fields)
{
string newField = field.ToI18n(GetType().Name);
CheckType<IMobilePhoneType>(mobilePhones, newField, AddType<IMobilePhone>, w => w.Name == newField, GetContactMechTypeBySubType);
CheckType<IFixedPhoneType>(fixedPhones, newField, AddType<IFixedPhone>, w => w.Name == newField, GetContactMechTypeBySubType);
CheckType<IFaxType>(faxs, newField, AddType<IFax>, w => w.Name == newField, GetContactMechTypeBySubType);
CheckType<IPostalAddressType>(addrs, newField, AddType<IPostalAddress>, w => w.Name == newField, GetContactMechTypeBySubType);
CheckType<IEmailType>(emails, newField, AddType<IEmail>, w => w.Name == newField, GetContactMechTypeBySubType);
CheckType<IUrlType>(urls, newField, AddType<IUrl>, w => w.Name == newField, GetContactMechTypeBySubType);
CheckType<IIMType>(ims, newField, AddType<IIM>, w => w.Name == newField, GetContactMechTypeBySubType);
CheckType<IExtendedAttrDef>(extendedAttrDefs, newField, AddExtendedAttrDef, w => w.Name == newField, GetExtendedAttrDef);
CheckType<string>(otherExtendedAttrDefs, newField, AddExtendedAttrDef, w => Condation(w, newField), GetOtherExtendedAttrDef);
}
return true;
}
private bool Condation(string sourceName, string newField)
{
IList<IExtendedAttrDef> o = _personAccountExtendedAttrDefs.OfType<IExtendedAttrDef>().Where(w => w.Name == newField).ToList();
if (o.Count == 0)
{
return sourceName.Replace(",", string.Empty).Equals(newField);
}
return true;
}
private static void CheckType<T>(IEnumerable<string> acceptField, string field, Action<string> addAny, Func<T, bool> where, Func<Func<T, bool>, IList<T>> source)
{
if (acceptField.Contains(field))
{
IList<T> items = source.Invoke(where);
if (items == null || items.Count == 0)
{
addAny.Invoke(field);
}
}
}
private void AddType<T>(string field)
{
_contactMechTypeService.CreateOrGetContactMechType<T>(field, true);
}
}