public static bool HasDurationItems(WM.DurationItem item) { bool result = false; if (item == null) { return(false); } //if (!string.IsNullOrWhiteSpace(item.DurationISO8601) && item.DurationISO8601.Substring(0,1) == "P" ) //{ // //eventually need to handle expanding the ISO8601 duration // return true; //} if (item.Years > 0 || item.Months > 0 || item.Weeks > 0 || item.Days > 0 || item.Hours > 0 || item.Minutes > 0 ) { result = true; } return(result); }
} // /// <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 DurationProfile() { MinimumDuration = new DurationItem(); MaximumDuration = new DurationItem(); ExactDuration = new DurationItem(); ProfileName = ""; DurationSummary = ""; }
private static string DurationSummary(string conditions, WM.DurationItem entity, ref string durationOnly) { string duration = ""; string prefix = ""; string comma = ""; if (string.IsNullOrWhiteSpace(conditions)) { prefix = "Duration: "; } else { prefix = conditions + " "; } if (entity.Years > 0) { duration += SetLabel(entity.Years, "Year"); comma = ", "; } if (entity.Months > 0) { duration += comma + SetLabel(entity.Months, "Month"); comma = ", "; } if (entity.Weeks > 0) { duration += comma + SetLabel(entity.Weeks, "Week"); comma = ", "; } if (entity.Days > 0) { duration += comma + SetLabel(entity.Days, "Day"); comma = ", "; } if (entity.Hours > 0) { duration += comma + SetLabel(entity.Hours, "Hour"); comma = ", "; } if (entity.Minutes > 0) { duration += comma + SetLabel(entity.Minutes, "Minute"); comma = ", "; } //TODO could replace last comma with And int lastPos = duration.LastIndexOf(","); if (lastPos > 0) { duration = duration.Substring(0, lastPos) + " and " + duration.Substring(lastPos + 1); } durationOnly = duration; return(prefix + duration); }
private static void MapFromDB(DBEntity from, WM.DurationItem duration) { //WM.DurationItem duration = new WM.DurationItem(); //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; }
public static string AsSchemaDuration(WM.DurationItem entity, ref int totalMinutes) { string duration = "P"; totalMinutes = 0; if (entity.Years > 0) { duration += entity.Years.ToString() + "Y"; totalMinutes += entity.Years * 365 * 24 * 3600; } if (entity.Months > 0) { duration += entity.Months.ToString() + "M"; totalMinutes += entity.Months * 30 * 24 * 3600; } if (entity.Weeks > 0) { duration += entity.Weeks.ToString() + "W"; totalMinutes += entity.Weeks * 5 * 24 * 3600; } if (entity.Days > 0) { duration += entity.Days.ToString() + "D"; totalMinutes += entity.Days * 24 * 3600; } if (entity.Hours > 0 || entity.Minutes > 0) { duration += "T"; } if (entity.Hours > 0) { duration += entity.Hours.ToString() + "H"; totalMinutes += entity.Hours * 3600; } if (entity.Minutes > 0) { duration += entity.Minutes.ToString() + "M"; totalMinutes += entity.Minutes; } return(duration); }
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); }
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; } }