Exemple #1
0
 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;
 }
Exemple #2
0
        private static bool HasToDurations(DBEntity item)
        {
            bool result = false;

            if (item.ToYears.HasValue ||
                item.ToMonths.HasValue ||
                item.ToWeeks.HasValue ||
                item.ToDays.HasValue ||
                item.ToHours.HasValue ||
                item.ToMinutes.HasValue
                )
            {
                result = true;
            }

            return(result);
        }
Exemple #3
0
        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);
        }
Exemple #4
0
        public bool Delete(int recordId, ref string statusMessage)
        {
            bool isOK = true;

            using (var context = new EntityContext())
            {
                EM.Entity_DurationProfile p = context.Entity_DurationProfile.FirstOrDefault(s => s.Id == recordId);
                if (p != null && p.Id > 0)
                {
                    context.Entity_DurationProfile.Remove(p);
                    int count = context.SaveChanges();
                }
                else
                {
                    statusMessage = string.Format("DurationProflie record was not found: {0}", recordId);
                    isOK          = false;
                }
            }
            return(isOK);
        }
Exemple #5
0
        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);
        }
Exemple #6
0
        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;
            }
        }
Exemple #7
0
        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;
            }
        }