Demo entry 2581412

fsdfs

   

Submitted by fsdfsd on Aug 31, 2015 at 15:26
Language: C#. Code size: 4.8 kB.

  public class NameConverter
    {
        public const int LinePaddingLength = 30; 
        
        public static string GetTimePartHHMM(DateTime date)
        {
            return date.ToString("h:mm t");
        }

        public static void GenerateNamesForCodes(CSCalendarEvent evt, StringBuilder builder)
        {
            evt.StartDateTitle = FormatHelper.FormatDate(evt.StartDate?? default(DateTime)) + " " + GetTimePartHHMM(evt.StartDate ?? default(DateTime));
            evt.EndDateTitle = FormatHelper.FormatDate(evt.EndDate ?? default(DateTime)) + " " + GetTimePartHHMM(evt.EndDate ?? default(DateTime));
           
            if (evt.EventConfirmFlag == null)
                evt.EventStatus = Constants.EventType.None;
            else if (evt.EventConfirmFlag.Equals("S"))
                evt.EventStatus = Constants.EventType.Scheduled;
            else if (evt.EventConfirmFlag.Equals("W"))
                evt.EventStatus = Constants.EventType.Waitlisted;
            else if (evt.EventConfirmFlag.Equals("C"))
                evt.EventStatus = Constants.EventType.Confirmed;

            GenerateEventText(evt, builder);
        }

        private static void GenerateEventText(CSCalendarEvent evt, StringBuilder builder)
        {
            builder.Clear();
            const string separator = "-";
           
            switch ((Domain.Enums.Constants.CalendarTypes)evt.CalendarTypeId)
            {
                case Domain.Enums.Constants.CalendarTypes.SleepStudy:
                case Domain.Enums.Constants.CalendarTypes.DoctorSchedule:
                case Domain.Enums.Constants.CalendarTypes.NursePractioner:
                case Domain.Enums.Constants.CalendarTypes.DME:
                case Domain.Enums.Constants.CalendarTypes.Dentist:
                    {
                        builder.Append(evt.PatientName);
                        CustomAppend(builder, separator, evt.TreatmentDetailsName);
                        break;
                    }
                case Domain.Enums.Constants.CalendarTypes.RespiratoryTherapist:
                case Domain.Enums.Constants.CalendarTypes.RtCentralized:
                    {
						if (evt.ExposureType == (int)EventExposureType.CheckIn || evt.ExposureType == (int)EventExposureType.Departure)
	                    {
                            CustomAppend(builder, separator, evt.DrnprtName, evt.EventName);
	                    }
	                    else
	                    {
		                    builder.Append(evt.PatientName);
		                    CustomAppend(builder, separator, GetShortTreatmentName(evt.TreatmentDetailsName), evt.EquipmentGroupsText);
	                    }
	                    break;
                    }
                case Domain.Enums.Constants.CalendarTypes.Misc:
                    {
                        builder.Append(evt.EventName);
                        Trim(builder, LinePaddingLength);
                        break;
                    }
            }
            evt.Title =  builder.ToString();
        }

        private static string GetShortTreatmentName(string treatmentName)
        {
            if (treatmentName.Equals("INIT") || treatmentName.Equals("Initial"))
              return "I";
            if (treatmentName.Equals("REVIT"))
               return "R";
            if (treatmentName.Equals("CSSOFF"))
               return "D";
            return string.Empty;
        }

        private static void CustomAppend(StringBuilder builder, string separator, params string[] items)
        {
            if (!items.Any()) return;

            var notEmptyItems = items.Where(el => !string.IsNullOrWhiteSpace(el)).Select(el => el.Trim()).ToList();

            //calculate length
            var builderLength = builder.ToString().Length;
            var appendStrLength = notEmptyItems.Sum(el => el.Length) + separator.Length * notEmptyItems.Count();

            if (builderLength + appendStrLength > LinePaddingLength)
            {
                builderLength = LinePaddingLength - appendStrLength;
                builder.Length = builderLength > default(int) ? builderLength : default(int);
            }

            //append items
            if (builder.Length != default(int))
                builder.Append(separator);

            builder.Append(string.Join(separator, notEmptyItems));

            //trim if length more than allowed
            Trim(builder, LinePaddingLength);
        }

        private static void Trim(StringBuilder builder, int length)
        {
            if (length < 0) throw new ArgumentOutOfRangeException("length");
            if (builder.Length > length)
                builder.Length = length;
        }
    }

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).