} // /// <summary> /// Get a single DurationProfile by integer Id /// </summary> /// <param name="id"></param> /// <returns></returns> /// public static WM.DurationItem GetRenewalDuration(Guid parentUid) { WM.DurationItem duration = new WM.DurationItem(); ThisEntity profile = new ThisEntity(); Entity parent = EntityManager.GetEntity(parentUid); if (parent == null || parent.Id == 0) { return(duration); } using (var context = new EntityContext()) { List <EM.Entity_DurationProfile> results = context.Entity_DurationProfile .Where(s => s.EntityId == parent.Id && (s.TypeId == 3)) .ToList(); if (results != null && results.Count > 0) { foreach (EM.Entity_DurationProfile item in results) { MapFromDB(item, duration); break; } } return(duration); } }
/// <summary> /// Retrieve and fill duration profiles for parent entity /// </summary> /// <param name="parentUid"></param> public static List <ThisEntity> GetAll(Guid parentUid, int typeId = 0) { ThisEntity row = new ThisEntity(); WM.DurationItem duration = new WM.DurationItem(); List <ThisEntity> profiles = new List <ThisEntity>(); Entity parent = EntityManager.GetEntity(parentUid); if (parent == null || parent.Id == 0) { return(profiles); } using (var context = new EntityContext()) { List <EM.Entity_DurationProfile> results = context.Entity_DurationProfile .Where(s => s.EntityId == parent.Id && ((typeId == 0 && s.TypeId < 3) || s.TypeId == typeId)) .OrderBy(s => s.Id) .ToList(); if (results != null && results.Count > 0) { foreach (EM.Entity_DurationProfile item in results) { row = new ThisEntity(); MapFromDB(item, row); profiles.Add(row); } } return(profiles); } } //
public void AssignDurationSummary( ) { ThisEntity row = new ThisEntity(); using (var context = new EntityContext()) { List <EM.Entity_DurationProfile> results = context.Entity_DurationProfile .OrderBy(s => s.Id) .ToList(); if (results != null && results.Count > 0) { foreach (EM.Entity_DurationProfile item in results) { row = new ThisEntity(); MapFromDB(item, row); if (row.ExactDuration.HasValue || row.IsRange) { item.DurationSummary = row.DurationSummary; if (HasStateChanged(context)) { //note: testing - the latter may be true if the child has changed - but shouldn't as the mapping only updates the parent //item.LastUpdated = System.DateTime.Now; context.SaveChanges(); } } } } } }
public bool ValidateDurationProfile(ThisEntity profile, ref bool isEmpty, ref SaveStatus status) { status.HasSectionErrors = false; bool hasConditions = false; isEmpty = false; //string message = ""; if (string.IsNullOrWhiteSpace(profile.Conditions) == false) { hasConditions = true; } bool hasExactDuration = false; bool hasRangeDuration = false; if (HasDurationItems(profile.ExactDuration)) { hasExactDuration = true; } if (HasDurationItems(profile.MinimumDuration) || HasDurationItems(profile.MaximumDuration)) { hasRangeDuration = true; } //validations should be done before here if (hasExactDuration) { if (hasRangeDuration) { //inconsistent, take exact for now status.AddWarning("Error - you must either enter an Exact duration or a Minimum/Maximum range duration but not both. For now, the exact duration was used"); } } else if (hasRangeDuration == false && hasConditions == false) { //nothing, status.AddWarning("Error - you must enter either an Exact duration or a Minimum/Maximum range duration (but not both). <br/>"); isEmpty = true; } //if ( !string.IsNullOrWhiteSpace( profile.ProfileName ) && profile.ProfileName.Length > 200 ) //{ // //nothing, // status.AddError( "Error - the profile name is too long, the maximum length is 200 characters.<br/>" ); // isValid = false; // isEmpty = false; //} if (!string.IsNullOrWhiteSpace(profile.Conditions) && profile.Conditions.Length > 999) { //nothing, status.AddWarning("Error - the description is too long, the maximum length is 1000 characters.<br/>"); isEmpty = false; } return(!status.HasSectionErrors); }
public static ThisEntity Get(int id) { ThisEntity entity = new ThisEntity(); using (var context = new EntityContext()) { EM.Entity_DurationProfile item = context.Entity_DurationProfile .SingleOrDefault(s => s.Id == id); if (item != null && item.Id > 0) { MapFromDB(item, entity); } } return(entity); }
public bool SaveRenewalFrequency(WM.DurationItem entity, Guid parentUid, ref SaveStatus status) { bool isValid = true; Entity parent = EntityManager.GetEntity(parentUid); if (parent == null || parent.Id == 0) { status.AddError(thisClassName + ". Error - the parent entity was not found."); return(false); } WM.DurationProfile item = new ThisEntity(); if (entity != null && entity.HasValue) { item.ExactDuration = entity; item.DurationProfileTypeId = 3; return(Save(item, parent, ref status)); } return(isValid); }
public bool Save(WM.DurationProfile profile, Entity parent, ref SaveStatus status) { bool isValid = true; int count = 0; //Entity parent = EntityManager.GetEntity( parentUid ); //if ( parent == null || parent.Id == 0 ) //{ // status.AddError( thisClassName + "Error - the parent entity was not found " + parentUid ); // return false; //} profile.EntityId = parent.Id; EM.Entity_DurationProfile efEntity = new EM.Entity_DurationProfile(); //entityId will be in the passed entity //will it????? using (var context = new EntityContext()) { //check add/updates first bool isEmpty = false; if (ValidateDurationProfile(profile, ref isEmpty, ref status) == false) { return(false); } if (isEmpty) { //status.AddWarning( thisClassName + "Error - no data was entered." ); return(false); } if (profile.Id == 0) { //add efEntity = new EM.Entity_DurationProfile(); MapToDB(profile, efEntity); efEntity.EntityId = profile.EntityId; efEntity.Created = efEntity.LastUpdated = DateTime.Now; context.Entity_DurationProfile.Add(efEntity); count = context.SaveChanges(); //update profile record so doesn't get deleted profile.Id = efEntity.Id; if (count == 0) { status.AddError(" Unable to add Duration Profile"); isValid = false; } } else { efEntity = context.Entity_DurationProfile.SingleOrDefault(s => s.Id == profile.Id); if (efEntity != null && efEntity.Id > 0) { //update MapToDB(profile, efEntity); //has changed? if (HasStateChanged(context)) { //note: testing - the latter may be true if the child has changed - but shouldn't as the mapping only updates the parent efEntity.LastUpdated = System.DateTime.Now; count = context.SaveChanges(); } } else { //??? shouldn't happen unless deleted somehow status.AddError(" Unable to update Duration Profile - the profile was not found."); } } } return(isValid); }
private static void MapFromDB(DBEntity from, ThisEntity to) { WM.DurationItem duration = new WM.DurationItem(); int totalMinutes = 0; string durationOnly = ""; to.Id = from.Id; to.EntityId = from.EntityId ?? 0; to.Conditions = from.DurationComment; to.Created = from.Created != null ? ( DateTime )from.Created : DateTime.Now; to.Created = from.LastUpdated != null ? ( DateTime )from.LastUpdated : DateTime.Now; duration = new WM.DurationItem(); duration.Years = from.FromYears == null ? 0 : ( int )from.FromYears; duration.Months = from.FromMonths == null ? 0 : ( int )from.FromMonths; duration.Weeks = from.FromWeeks == null ? 0 : ( int )from.FromWeeks; duration.Days = from.FromDays == null ? 0 : ( int )from.FromDays; duration.Hours = from.FromHours == null ? 0 : ( int )from.FromHours; duration.Minutes = from.FromMinutes == null ? 0 : ( int )from.FromMinutes; if (HasToDurations(from)) { //format as from and to to.MinimumDuration = duration; to.MinimumDurationISO8601 = AsSchemaDuration(duration, ref totalMinutes); to.ProfileSummary = DurationSummary(to.Conditions, duration, ref durationOnly); if (!string.IsNullOrWhiteSpace(durationOnly)) { to.DurationSummary = durationOnly; } duration = new WM.DurationItem(); duration.Years = from.ToYears == null ? 0 : ( int )from.ToYears; duration.Months = from.ToMonths == null ? 0 : ( int )from.ToMonths; duration.Weeks = from.ToWeeks == null ? 0 : ( int )from.ToWeeks; duration.Days = from.ToDays == null ? 0 : ( int )from.ToDays; duration.Hours = from.ToHours == null ? 0 : ( int )from.ToHours; duration.Minutes = from.ToMinutes == null ? 0 : ( int )from.ToMinutes; to.MaximumDuration = duration; to.MaximumDurationISO8601 = AsSchemaDuration(duration, ref totalMinutes); to.ProfileSummary += DurationSummary(" to ", duration, ref durationOnly); if (!string.IsNullOrWhiteSpace(durationOnly)) { to.DurationSummary += " to " + durationOnly; } } else { to.ExactDuration = duration; to.ExactDurationISO8601 = AsSchemaDuration(duration, ref totalMinutes); to.ProfileSummary = DurationSummary(to.Conditions, duration, ref durationOnly); if (!string.IsNullOrWhiteSpace(durationOnly)) { to.DurationSummary = durationOnly; } } if (string.IsNullOrWhiteSpace(to.ProfileName)) { to.ProfileName = to.ProfileSummary; } }
private void MapToDB(ThisEntity from, DBEntity to) { int totalMinutes = 0; to.Id = from.Id; //make sure EntityId is not wiped out. Also can't actually chg //if ( (to.EntityId ?? 0) == 0) // to.EntityId = from.EntityId; if (to.Id == 0) { //to.ParentUid = from.ParentUid; //to.ParentTypeId = from.ParentTypeId; } to.DurationComment = from.Description; bool hasExactDuration = false; bool hasRangeDuration = false; if (HasDurationItems(from.ExactDuration)) { hasExactDuration = true; } if (HasDurationItems(from.MinimumDuration) || HasDurationItems(from.MaximumDuration)) { hasRangeDuration = true; } string durationSummary = ""; string durationOnly = ""; to.TypeId = 0; //validations should be done before here if (hasExactDuration) { //if ( hasRangeDuration ) //{ // //inconsistent, take exact for now // ConsoleMessageHelper.SetConsoleErrorMessage( "Error - you must either enter just an exact duration or a 'from - to' duration, not both. For now, the exact duration was used", "", false ); //} to.FromYears = from.ExactDuration.Years; to.FromMonths = from.ExactDuration.Months; to.FromWeeks = from.ExactDuration.Weeks; to.FromDays = from.ExactDuration.Days; to.FromHours = from.ExactDuration.Hours; to.FromMinutes = from.ExactDuration.Minutes; to.FromDuration = AsSchemaDuration(from.ExactDuration, ref totalMinutes); to.AverageMinutes = totalMinutes; to.TypeId = from.DurationProfileTypeId == 3 ? from.DurationProfileTypeId : 1; //reset any to max duration values to.ToYears = null; to.ToMonths = null; to.ToWeeks = null; to.ToDays = null; to.ToHours = null; to.ToMinutes = null; to.ToDuration = ""; DurationSummary("", from.ExactDuration, ref durationOnly); to.DurationSummary = durationOnly; } else if (hasRangeDuration) { to.FromYears = from.MinimumDuration.Years; to.FromMonths = from.MinimumDuration.Months; to.FromWeeks = from.MinimumDuration.Weeks; to.FromDays = from.MinimumDuration.Days; to.FromHours = from.MinimumDuration.Hours; to.FromMinutes = from.MinimumDuration.Minutes; to.FromDuration = AsSchemaDuration(from.MinimumDuration, ref totalMinutes); int fromMin = totalMinutes; DurationSummary("", from.MinimumDuration, ref durationOnly); to.DurationSummary = durationOnly; to.ToYears = from.MaximumDuration.Years; to.ToMonths = from.MaximumDuration.Months; to.ToWeeks = from.MaximumDuration.Weeks; to.ToDays = from.MaximumDuration.Days; to.ToHours = from.MaximumDuration.Hours; to.ToMinutes = from.MaximumDuration.Minutes; to.ToDuration = AsSchemaDuration(from.MaximumDuration, ref totalMinutes); to.AverageMinutes = (fromMin + totalMinutes) / 2; to.TypeId = 2; DurationSummary("", from.MaximumDuration, ref durationOnly); durationOnly += " to " + durationOnly; } }