} // public static List <ThisEntity> GetAllForList(Guid parentUid) { ThisEntity row = new ThisEntity(); DurationItem duration = new DurationItem(); List <ThisEntity> profiles = new List <ThisEntity>(); Entity parent = EntityManager.GetEntity(parentUid); using (var context = new EntityContext()) { List <DBEntity> results = context.Entity_CostProfile .Where(s => s.EntityId == parent.Id) .OrderBy(s => s.Id) .ToList(); if (results != null && results.Count > 0) { foreach (DBEntity item in results) { row = new ThisEntity(); MapFromDB(item, row, false); profiles.Add(row); } } return(profiles); } } //
//public bool DeleteAll( Entity parent, ref SaveStatus status, DateTime? lastUpdated = null ) //{ // bool isValid = true; // if ( parent == null || parent.Id == 0 ) // { // status.AddError( thisClassName + ". Error - the provided target parent entity was not provided." ); // return false; // } // int expectedDeleteCount = 0; // try // { // using ( var context = new EntityContext() ) // { // var results = context.Entity_CostProfile.Where( s => s.EntityId == parent.Id && ( lastUpdated == null || s.LastUpdated < lastUpdated ) ) // .ToList(); // if ( results == null || results.Count == 0 ) // return true; // expectedDeleteCount = results.Count; // foreach ( var item in results ) // { // context.Entity_CostProfile.Remove( item ); // var count = context.SaveChanges(); // if ( count > 0 ) // { // } // } // } // } // catch ( System.Data.Entity.Infrastructure.DbUpdateConcurrencyException dbcex ) // { // if ( dbcex.Message.IndexOf( "an unexpected number of rows (0)" ) > 0 ) // { // //don't know why this happens, quashing for now. // LoggingHelper.DoTrace( 1, string.Format( thisClassName + ".DeleteAll. Parent type: {0}, ParentId: {1}, expectedDeletes: {2}. Message: {3}", parent.EntityTypeId, parent.EntityBaseId, expectedDeleteCount, dbcex.Message ) ); // } // else // { // var msg = BaseFactory.FormatExceptions( dbcex ); // LoggingHelper.DoTrace( 1, string.Format( thisClassName + ".DeleteAll. ParentType: {0}, baseId: {1}, DbUpdateConcurrencyException: {2}", parent.EntityType, parent.EntityBaseId, msg ) ); // } // } // catch ( Exception ex ) // { // var msg = BaseFactory.FormatExceptions( ex ); // LoggingHelper.DoTrace( 1, string.Format( thisClassName + ".DeleteAll. ParentType: {0}, baseId: {1}, exception: {2}", parent.EntityType, parent.EntityBaseId, msg ) ); // } // return isValid; //} // private bool UpdateParts(ThisEntity entity, ref SaveStatus status) { bool isAllValid = true; // try { if (new Entity_ReferenceManager().Add(entity.Condition, entity.RowId, CodesManager.ENTITY_TYPE_COST_PROFILE, ref status, CodesManager.PROPERTY_CATEGORY_CONDITION_ITEM, false) == false) { isAllValid = false; } //JurisdictionProfile Entity_JurisdictionProfileManager jpm = new Entity_JurisdictionProfileManager(); jpm.SaveList(entity.Jurisdiction, entity.RowId, Entity_JurisdictionProfileManager.JURISDICTION_PURPOSE_SCOPE, ref status); } catch (Exception ex) { LoggingHelper.DoTrace(1, thisClassName + ".UpdateParts(). Exception while processing condition/jurisdiction. " + ex.Message); status.AddError(ex.Message); } if (entity.Items != null && entity.Items.Count > 0) { new CostProfileItemManager().SaveList(entity.Items, entity.Id, ref status); } return(isAllValid); }
} // public bool ValidateProfile(ThisEntity profile, ref SaveStatus status) { status.HasSectionErrors = false; if (!IsUrlValid(profile.CostDetails, ref commonStatusMessage)) { status.AddWarning("The Cost Details Url is invalid" + commonStatusMessage); } DateTime startDate = DateTime.Now; DateTime endDate = DateTime.Now; if (!string.IsNullOrWhiteSpace(profile.DateEffective)) { if (!IsValidDate(profile.DateEffective)) { status.AddWarning("Please enter a valid start date"); } else { DateTime.TryParse(profile.DateEffective, out startDate); } } if (!string.IsNullOrWhiteSpace(profile.ExpirationDate)) { if (!IsValidDate(profile.ExpirationDate)) { status.AddWarning("Please enter a valid end date"); } else { DateTime.TryParse(profile.ExpirationDate, out endDate); if (IsValidDate(profile.DateEffective) && startDate > endDate) { status.AddWarning("The end date must be greater than the start date."); } } } //currency? //if ( string.IsNullOrWhiteSpace( profile.Currency ) == false ) //{ // //length // if ( profile.Currency.Length != 3 || IsInteger( profile.Currency ) ) // { // status.AddError( "The currency code must be a three-letter alphabetic code " ); // isValid = false; // } //} return(!status.HasSectionErrors); }
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; } } }
static string SetCostProfileSummary(ThisEntity to) { string summary = "Cost Profile "; if (!string.IsNullOrWhiteSpace(to.ProfileName)) { summary = to.ProfileName; return(summary); } if (to.Id > 1) { summary += to.Id.ToString(); return(summary); } return(summary); }
// public static MicroProfile ConvertCostProfileToMicroProfile(MP.CostProfile item) { return(new MicroProfile() { Id = item.Id, RowId = item.RowId, Name = item.ProfileName, Heading2 = item.ProfileSummary, Description = item.Description, Selectors = new Dictionary <string, object>() { { "RowId", item.RowId }, { "Id", item.Id }, { "Name", item.ProfileName }, { "TypeName", item.GetType().Name } } }); }
} // 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); } //
private bool UpdateParts(ThisEntity entity, ref SaveStatus status) { bool isAllValid = true; if (new Entity_ReferenceManager().Add(entity.Condition, entity.RowId, CodesManager.ENTITY_TYPE_CONDITION_PROFILE, ref status, CodesManager.PROPERTY_CATEGORY_CONDITION_ITEM, false) == false) { isAllValid = false; } //JurisdictionProfile Entity_JurisdictionProfileManager jpm = new Entity_JurisdictionProfileManager(); jpm.SaveList(entity.Jurisdiction, entity.RowId, Entity_JurisdictionProfileManager.JURISDICTION_PURPOSE_SCOPE, ref status); if (entity.Items != null && entity.Items.Count > 0) { new CostProfileItemManager().SaveList(entity.Items, entity.Id, ref status); } return(isAllValid); }
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); }
public static List <CostProfile> ExpandCosts(List <CostProfileMerged> input) { var result = new List <CostProfile>(); //First expand each into its own CostProfile with one CostItem var holder = new List <CostProfile>(); foreach (var merged in input) { //Create cost profile var cost = new CostProfile() { ProfileName = merged.Name, Description = merged.Description, Jurisdiction = merged.Jurisdiction, StartTime = merged.StartTime, EndTime = merged.EndTime, StartDate = merged.StartDate, EndDate = merged.EndDate, CostDetails = merged.CostDetails, Currency = merged.Currency, CurrencySymbol = merged.CurrencySymbol, Condition = merged.Condition, Items = new List <CostProfileItem>() }; //If there's any data for a cost item, create one if (merged.Price > 0 || !string.IsNullOrWhiteSpace(merged.PaymentPattern) || merged.AudienceType.Items.Count() > 0 || merged.CostType.Items.Count() > 0 || merged.ResidencyType.Items.Count() > 0 ) { cost.Items.Add(new CostProfileItem() { ApplicableAudienceType = merged.AudienceType, DirectCostType = merged.CostType, PaymentPattern = merged.PaymentPattern, Price = merged.Price, ResidencyType = merged.ResidencyType }); } holder.Add(cost); } //Remove duplicates and hope that pass-by-reference issues don't cause trouble while (holder.Count() > 0) { //Take the first item in holder and set it aside var currentItem = holder.FirstOrDefault(); //Remove it from the holder list so it doesn't get included in the LINQ query results on the next line holder.Remove(currentItem); //Find any other items in the holder list that match the item we just took out var matches = holder.Where(m => m.ProfileName == currentItem.ProfileName && m.Description == currentItem.Description && m.CostDetails == currentItem.CostDetails && m.Currency == currentItem.Currency && m.CurrencySymbol == currentItem.CurrencySymbol ).ToList(); //For each matching item... foreach (var item in matches) { //Take its cost profile items (if it has any) and add them to the cost profile we set aside currentItem.Items = currentItem.Items.Concat(item.Items).ToList(); //Remove the item from the holder so it doesn't get detected again, and so that we eventually get out of this "while" loop holder.Remove(item); } //Now that currentItem has all of the cost profile items from all of its matches, add it to the result result.Add(currentItem); } return(result); }
/// <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); }