/// <summary> Add record to Changes Log and Workflow History </summary> /// <param name="context"></param> /// <param name="data"></param> /// <param name="changes"></param> /// <param name="formType"></param> /// <param name="actionType"></param> /// <param name="role"></param> /// <param name="actor"></param> /// <param name="comment"></param> private void LogsActivity(EMSDataModel context, PRODUCT_DEVELOPMENT data, Dictionary <string, string[]> changes, int formType, int actionType, int role, string actor, string comment = null) { try { foreach (var map in changes) { refService.AddChangeLog(context, formType, data.PD_ID.ToString(), map.Key, map.Value[0], map.Value[1], actor, DateTime.Now ); } refService.AddWorkflowHistory(context, formType, Convert.ToInt64(data.PD_ID), actionType, actor, DateTime.Now, role, comment ); context.SaveChanges(); } catch (Exception ex) { throw this.HandleException("Exception occured on Brand Registration Service. See Inner Exception property to see details", ex); } }
//public void CreateProduct(PRODUCT_DEVELOPMENT data, PRODUCT_DEVELOPMENT_DETAIL dataDetail, int formType, int actionType, int role, string user) public void CreateProduct(PRODUCT_DEVELOPMENT data, int formType, int actionType, int role, string user) { using (var context = new EMSDataModel()) { using (var transaction = context.Database.BeginTransaction()) { try { // Create Product Development context.PRODUCT_DEVELOPMENT.Add(data); context.SaveChanges(); // Create Product Development Detail //dataDetail.PD_ID = data.PD_ID; //context.PRODUCT_DEVELOPMENT_DETAIL.Add(dataDetail); //context.SaveChanges(); var changes = GetAllChanges(null, data); LogsActivity(context, data, changes, formType, actionType, role, user); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw this.HandleException("Exception occured on Product Development Service. See Inner Exception property to see details", ex); } } } }
public bool Edit(PRODUCT_DEVELOPMENT data, int formType, int actionType, int role, string user) { using (var context = new EMSDataModel()) { using (var transaction = context.Database.BeginTransaction()) { try { var old = context.PRODUCT_DEVELOPMENT.Find(data.PD_ID); Dictionary <string, string[]> changes = GetAllChanges(old, data); context.Entry(old).CurrentValues.SetValues(data); context.SaveChanges(); LogsActivity(context, data, changes, formType, actionType, role, user); transaction.Commit(); } catch (Exception ex) { transaction.Rollback(); throw this.HandleException("Exception occured on Product Development Service. See Inner Exception property to see details", ex); } } } return(true); }
/// <summary> Part of Changes Log Step which Mark All Available Changes </summary> /// <param name="old"></param> /// <param name="updated"></param> /// <returns></returns> private Dictionary <string, string[]> GetAllChanges(PRODUCT_DEVELOPMENT old, PRODUCT_DEVELOPMENT updated) { try { var changes = new Dictionary <string, string[]>(); var columns = new string[] { //"PROD_CODE", //"PRODUCT_TYPE", //"PRODUCT_ALIAS", //"CK4CEDITABLE", //"IS_DELETED", //"APPROVALSTATUS" }; var oldProps = new Dictionary <string, object>(); var props = new Dictionary <string, object>(); foreach (var prop in updated.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { props.Add(prop.Name, prop.GetValue(updated, null)); if (old != null) { oldProps.Add(prop.Name, prop.GetValue(old, null)); } else { oldProps.Add(prop.Name, null); } } foreach (var item in props) { var oldValue = (oldProps[item.Key] != null) ? oldProps[item.Key].ToString() : "N/A"; var newValue = (item.Value != null) ? item.ToString() : "N/A"; if (!columns.Contains(item.Key)) { continue; } if (item.Key == "APPROVALSTATUS") { if (item.Value != null) { newValue = ((SYS_REFFERENCES)item.Value).REFF_VALUE; } if (oldProps[item.Key] != null) { oldValue = ((SYS_REFFERENCES)oldProps[item.Key]).REFF_VALUE; } if (oldValue.Trim().ToUpper() != newValue.Trim().ToUpper()) { changes.Add(item.Key, new string[] { oldValue, newValue }); } continue; } if (item.Value != null) { if (item.Value is decimal) { newValue = ((decimal)item.Value).ToString("C2"); } else { newValue = item.Value.ToString(); } } if (oldProps[item.Key] != null) { if (oldProps[item.Key] is decimal) { oldValue = ((decimal)oldProps[item.Key]).ToString("C2"); } else { oldValue = oldProps[item.Key].ToString(); } } if (oldValue.Trim().ToUpper() != newValue.Trim().ToUpper()) { changes.Add(item.Key, new string[] { oldValue, newValue }); } } return(changes); } catch (Exception ex) { throw this.HandleException("Exception occured on Brand Registration Service. See Inner Exception property to see details", ex); } }