/// <summary> /// Map a DataTable's Rows to a List of DictionaryItem Entity. /// </summary> /// <returns></returns> public static IList <DictionaryItem> Row2Entity(System.Data.DataTable dt) { IList <DictionaryItem> list = null; if (dt != null && dt.Rows.Count > 0) { list = new List <DictionaryItem>(dt.Rows.Count); foreach (System.Data.DataRow row in dt.Rows) { DictionaryItem entity = Row2Entity(row); if (entity != null) { list.Add(entity); } } } return(list); }
/// <summary> /// Map a DataRow to a DictionaryItem Entity. /// </summary> /// <returns></returns> public static DictionaryItem Row2Entity(System.Data.DataRow row) { if (row == null) { return(null); } DictionaryItem entity = new DictionaryItem(); entity._itemCode = Cast.String(row["DIC_ITEM_CODE"]); entity._groupCode = Cast.String(row["DIC_GROUP"]); entity._name = Cast.String(row["DIC_NAME"]); entity._itemType = Cast.Enum <DictionaryItemType>(row["DIC_ITEM_TYPE"]); entity._boolValue = Cast.Bool(row["BOOL_VALUE"]); entity._numberValue = Cast.Decimal(row["NUMBER_VALUE"]); entity._stringValue = Cast.String(row["STRING_VALUE"]); entity._note = Cast.String(row["DIC_NOTE"]); return(entity); }
/// <summary> /// 获取单个字典项 /// </summary> /// <param name="itemCode"></param> /// <returns></returns> public static DictionaryItem GetDictionaryItemByCode(string itemCode) { if (_dicItems.ContainsKey(itemCode)) { return(_dicItems[itemCode]); } else { DictionaryItem item = null; using (ISession session = new Session()) { item = DictionaryItem.Retrieve(session, itemCode); if (item != null) { _dicItems.Add(itemCode, item); } else { throw new ApplicationException(string.Format("没有设置 项代码为:{0}的数据字典项,请确认初始化数据中包含此代码的初始化数据", itemCode)); } return(item); } } }
/// <summary> /// 获取一组字典项 /// </summary> /// <param name="session"></param> /// <param name="groupCode"></param> /// <returns></returns> public static IList <DictionaryItem> GetDictionaryItemsByGroup(string groupCode) { if (_groupItems.ContainsKey(groupCode)) { return(_groupItems[groupCode]); } else { IList <DictionaryItem> items = null; using (ISession session = new Session()) { items = DictionaryItem.GetDictionaryItemsByGroup(session, groupCode); } if (items == null || items.Count <= 0) { throw new ApplicationException(string.Format("没有设置 组代码为:{0}的数据字典子项,请确认初始化数据中包含此代码的初始化数据", groupCode)); } else { _groupItems.Add(groupCode, items); } return(items); } }
/// <summary> /// Map a DataRow to a DictionaryItem Entity. /// </summary> /// <returns></returns> public static DictionaryItem Row2Entity(System.Data.DataRow row) { if (row == null) return null; DictionaryItem entity = new DictionaryItem(); entity._itemCode = Cast.String(row["DIC_ITEM_CODE"]); entity._groupCode = Cast.String(row["DIC_GROUP"]); entity._name = Cast.String(row["DIC_NAME"]); entity._itemType = Cast.Enum<DictionaryItemType>(row["DIC_ITEM_TYPE"]); entity._boolValue = Cast.Bool(row["BOOL_VALUE"]); entity._numberValue = Cast.Decimal(row["NUMBER_VALUE"]); entity._stringValue = Cast.String(row["STRING_VALUE"]); entity._note = Cast.String(row["DIC_NOTE"]); return entity; }
//Save Data private void SaveData() { DictionaryItem dictionaryItem = new DictionaryItem(); bool flag = true; try { if (_actionMode == Mode.AddChild) { dictionaryItem.GroupCode = txtGroupCode.Text.Trim(); } else if (_actionMode == Mode.New) { dictionaryItem.GroupCode = DictionaryItem.Root.ItemCode; } dictionaryItem.ItemCode = txtCode.Text.Trim(); dictionaryItem.Name = txtName.Text.Trim(); dictionaryItem.ItemType = Cast.Enum<DictionaryItemType>(rdlItemType.SelectedValue); switch (dictionaryItem.ItemType) { case DictionaryItemType.Boolean: dictionaryItem.BoolValue = Cast.Bool(this.ddlBoolValue.SelectedValue); break; case DictionaryItemType.Numric: dictionaryItem.NumberValue = Cast.Decimal(this.txtNumberValue.Text); break; case DictionaryItemType.String: if (this.txtStringValue.Value.Trim().Length > 1000) { dictionaryItem.StringValue = this.txtStringValue.Value.Trim().Substring(0, 1000); } else dictionaryItem.StringValue = this.txtStringValue.Value; break; default: break; } dictionaryItem.Note = txtNote.Value.Trim(); using (_session = new Session()) { if (IsAddNew()) { if (DictionaryItem.Exists(_session, new string[] { "ItemCode" }, new object[] { dictionaryItem.ItemCode })) { throw new ApplicationException(string.Format("代码:{0}已经存在;如果是子项,建议将组代码作为前缀", dictionaryItem.ItemCode)); } flag = dictionaryItem.Create(_session); } else { dictionaryItem.ItemCode = (this.hidItemCode.Value); flag = dictionaryItem.Update(_session,"ItemCode","Name","BoolValue","NumberValue","StringValue"); } } this.hidItemCode.Value = dictionaryItem.ItemCode.ToString(); if(flag) WebUtil.ShowMsg(this,"操作成功","提示"); else WebUtil.ShowMsg(this,"操作失败","提示"); } catch(UnauthorizedException ex) { WebUtil.ShowMsg(this,ex.Message,"警告"); } catch(ApplicationException ex) { WebUtil.ShowMsg(this,ex.Message,"提示"); } catch(Exception ex) { logger.Info("保存DictionaryItem", ex); WebUtil.ShowMsg(this, "发生未处理的异常,请刷新页面重新操作,或者联系系统管理员"); } }