public static void MapToDB(ThisEntity from, DBEntity to) { to.Id = from.Id; if (to.Id == 0) { //make sure EntityId is not wiped out. Also can't actually chg if ((to.EntityId) == 0) { to.EntityId = from.EntityId; } } to.ProfileName = from.ProfileName; to.Description = from.Description; if (IsValidDate(from.EndDate)) { to.ExpirationDate = DateTime.Parse(from.EndDate); } else { to.ExpirationDate = null; } if (IsValidDate(from.StartDate)) { to.DateEffective = DateTime.Parse(from.StartDate); } else { to.DateEffective = null; } to.DetailsUrl = from.CostDetails; to.CurrencyTypeId = null; if (from.CurrencyTypeId > 0) { to.CurrencyTypeId = from.CurrencyTypeId; } else if (!string.IsNullOrWhiteSpace(from.Currency)) { Views.Codes_Currency currency = CodesManager.GetCurrencyItem(from.Currency); if (currency != null && currency.NumericCode > 0) { to.CurrencyTypeId = currency.NumericCode; } } }
public bool Delete(int recordId, ref string statusMessage) { bool isOK = true; using (var context = new EntityContext()) { DBEntity p = context.Entity_CostProfile.FirstOrDefault(s => s.Id == recordId); if (p != null && p.Id > 0) { context.Entity_CostProfile.Remove(p); int count = context.SaveChanges(); } else { statusMessage = string.Format("Cost Profile record was not found: {0}", recordId); isOK = false; } } return(isOK); }
} // public static ThisEntity GetBasicProfile(Guid profileUid) { ThisEntity entity = new ThisEntity(); using (var context = new EntityContext()) { context.Configuration.LazyLoadingEnabled = false; DBEntity item = context.Entity_CostProfile .SingleOrDefault(s => s.RowId == profileUid); if (item != null && item.Id > 0) { entity.Id = item.Id; entity.RowId = item.RowId; entity.EntityId = item.EntityId; entity.ProfileName = item.ProfileName; entity.Description = item.Description; } } return(entity); } //
public static void MapFromDB(DBEntity from, ThisEntity to, bool includingItems) { to.Id = from.Id; to.RowId = from.RowId; to.EntityId = from.EntityId; to.ProfileName = from.ProfileName; to.Description = from.Description; if (IsValidDate(from.ExpirationDate)) { to.EndDate = (( DateTime )from.ExpirationDate).ToString("yyyy-MM-dd"); } else { to.EndDate = ""; } if (IsValidDate(from.DateEffective)) { to.StartDate = (( DateTime )from.DateEffective).ToString("yyyy-MM-dd"); } else { to.StartDate = ""; } to.CostDetails = from.DetailsUrl; to.CurrencyTypeId = (int)(from.CurrencyTypeId ?? 0); Views.Codes_Currency code = CodesManager.GetCurrencyItem(to.CurrencyTypeId); if (code != null && code.NumericCode > 0) { to.Currency = code.Currency; to.CurrencySymbol = code.HtmlCodes; } to.ProfileSummary = SetCostProfileSummary(to); if (IsValidDate(from.Created)) { to.Created = ( DateTime )from.Created; } if (IsValidDate(from.LastUpdated)) { to.LastUpdated = ( DateTime )from.LastUpdated; } to.Condition = Entity_ReferenceManager.GetAll(to.RowId, CodesManager.PROPERTY_CATEGORY_CONDITION_ITEM); if (includingItems) { //TODO - the items should be part of the EF record if (from.Entity_CostProfileItem != null && from.Entity_CostProfileItem.Count > 0) { CostProfileItem row = new CostProfileItem(); foreach (EM.Entity_CostProfileItem item in from.Entity_CostProfileItem) { row = new CostProfileItem(); //TODO CostProfileItemManager.MapFromDB(item, row, true); to.Items.Add(row); } } to.Jurisdiction = Entity_JurisdictionProfileManager.Jurisdiction_GetAll(to.RowId); to.Region = Entity_JurisdictionProfileManager.Jurisdiction_GetAll(to.RowId, Entity_JurisdictionProfileManager.JURISDICTION_PURPOSE_RESIDENT); } }
/// <summary> /// Persist Cost Profile /// </summary> /// <param name="entity"></param> /// <param name="parentUid"></param> /// <param name="userId"></param> /// <param name="messages"></param> /// <returns></returns> public bool Save(ThisEntity entity, Entity parent, ref SaveStatus status) { bool isValid = true; //if ( !IsValidGuid( parentUid ) ) //{ // status.AddError( thisClassName + " - Error: the parent identifier was not provided." ); // return false; //} ////get parent entity //Entity parent = EntityManager.GetEntity( parentUid ); if (parent == null || parent.Id == 0) { status.AddError(thisClassName + " - Error - the parent entity was not found."); return(false); } int count = 0; DBEntity efEntity = new DBEntity(); if (ValidateProfile(entity, ref status) == false) { //can't really scrub from here - too late? //at least add some identifer //return false; } try { bool doingUpdateParts = true; using (var context = new EntityContext()) { if (entity.Id == 0) { efEntity = new DBEntity(); //check for current match - only do if not deleting //not unexpected that the same cost details url could be used for more than one profile //var exists = context.Entity_CostProfile // .Where( s => s.EntityId == parent.Id // && s.Description == entity.Description // && s.DetailsUrl == entity.CostDetails // ) // .OrderBy( s => s.Created ).ThenBy( s => s.LastUpdated ) // .ToList(); //just in case entity.EntityId = parent.Id; //add efEntity = new DBEntity(); MapToDB(entity, efEntity); efEntity.Created = efEntity.LastUpdated = DateTime.Now; if (IsValidGuid(entity.RowId)) { efEntity.RowId = entity.RowId; } else { efEntity.RowId = Guid.NewGuid(); } context.Entity_CostProfile.Add(efEntity); count = context.SaveChanges(); //update profile record so doesn't get deleted entity.Id = efEntity.Id; entity.RowId = efEntity.RowId; if (count == 0) { status.AddError(thisClassName + " - Unable to add Cost Profile"); doingUpdateParts = false; } else { //if ( !UpdateParts( entity, ref status ) ) // isValid = false; } } else { //context.Configuration.LazyLoadingEnabled = false; efEntity = context.Entity_CostProfile.SingleOrDefault(s => s.Id == entity.Id); if (efEntity != null && efEntity.Id > 0) { entity.RowId = efEntity.RowId; //update MapToDB(entity, efEntity); //has changed? if (HasStateChanged(context)) { efEntity.LastUpdated = System.DateTime.Now; count = context.SaveChanges(); } //always check parts //if ( !UpdateParts( entity, ref status ) ) // isValid = false; } } } //21-04-21 mparsons - end the current context before doing parts if (doingUpdateParts) { //always check parts if (!UpdateParts(entity, ref status)) { isValid = false; } } } catch (System.Data.Entity.Validation.DbEntityValidationException dbex) { string message = HandleDBValidationError(dbex, thisClassName + ".Save()", entity.ProfileName); status.AddWarning(thisClassName + " - Error - the save was not successful. " + message); LoggingHelper.LogError(dbex, thisClassName + string.Format(".Save()-DbEntityValidationException, Parent: {0} (type: {1}, Id: {2})", parent.EntityBaseName, parent.EntityTypeId, parent.EntityBaseId)); isValid = false; } catch (Exception ex) { string message = FormatExceptions(ex); status.AddError(thisClassName + " - Error - the save was not successful. " + message); LoggingHelper.LogError(ex, thisClassName + string.Format(".Save(), Parent: {0} (type: {1}, Id: {2})", parent.EntityBaseName, parent.EntityTypeId, parent.EntityBaseId)); isValid = false; } return(isValid); }
/// <summary> /// Persist Cost Profile /// </summary> /// <param name="entity"></param> /// <param name="parentUid"></param> /// <param name="userId"></param> /// <param name="messages"></param> /// <returns></returns> public bool Save(ThisEntity entity, Guid parentUid, ref SaveStatus status) { bool isValid = true; if (!IsValidGuid(parentUid)) { status.AddError(thisClassName + " - Error: the parent identifier was not provided."); return(false); } //get parent entity Entity parent = EntityManager.GetEntity(parentUid); if (parent == null || parent.Id == 0) { status.AddError(thisClassName + " - Error - the parent entity was not found."); return(false); } int count = 0; DBEntity efEntity = new DBEntity(); using (var context = new EntityContext()) { if (ValidateProfile(entity, ref status) == false) { //can't really scrub from here - too late? //at least add some identifer return(false); } try { if (entity.Id == 0) { //just in case entity.EntityId = parent.Id; //add efEntity = new DBEntity(); MapToDB(entity, efEntity); efEntity.Created = efEntity.LastUpdated = DateTime.Now; if (IsValidGuid(entity.RowId)) { efEntity.RowId = entity.RowId; } else { efEntity.RowId = Guid.NewGuid(); } context.Entity_CostProfile.Add(efEntity); count = context.SaveChanges(); //update profile record so doesn't get deleted entity.Id = efEntity.Id; entity.RowId = efEntity.RowId; if (count == 0) { status.AddError(thisClassName + " - Unable to add Cost Profile"); } else { if (!UpdateParts(entity, ref status)) { isValid = false; } } } else { context.Configuration.LazyLoadingEnabled = false; efEntity = context.Entity_CostProfile.SingleOrDefault(s => s.Id == entity.Id); if (efEntity != null && efEntity.Id > 0) { entity.RowId = efEntity.RowId; //update MapToDB(entity, efEntity); //has changed? if (HasStateChanged(context)) { efEntity.LastUpdated = System.DateTime.Now; count = context.SaveChanges(); } //always check parts if (!UpdateParts(entity, ref status)) { isValid = false; } } } } catch (System.Data.Entity.Validation.DbEntityValidationException dbex) { string message = HandleDBValidationError(dbex, thisClassName + ".Save()", entity.ProfileName); status.AddWarning(thisClassName + " - Error - the save was not successful. " + message); LoggingHelper.LogError(dbex, thisClassName + string.Format(".Save(), Parent: {0} ({1})", parent.EntityBaseName, parent.EntityBaseId)); isValid = false; } catch (Exception ex) { string message = FormatExceptions(ex); status.AddError(thisClassName + " - Error - the save was not successful. " + message); LoggingHelper.LogError(ex, thisClassName + string.Format(".Save(), Parent: {0} ({1}), UserId: {2}", parent.EntityBaseName, parent.EntityBaseId)); isValid = false; } } return(isValid); }