public static void MapFromDB(DBEntity input, ThisEntity output,
                                     bool includingParts)
        {
            output.Id          = input.Id;
            output.RowId       = input.RowId;
            output.Description = input.Description == null ? "" : input.Description;
            if (IsValidDate(input.StartDate))
            {
                output.StartDate = input.StartDate;
            }
            else
            {
                output.StartDate = null;
            }

            if (IsValidDate(input.EndDate))
            {
                output.EndDate = input.EndDate;
            }
            else
            {
                output.EndDate = null;
            }
            if (input.DataSourceCoverageType != null)
            {
                var list = input.DataSourceCoverageType.Split('|');
                foreach (var item in list)
                {
                    if (!string.IsNullOrWhiteSpace(item))
                    {
                        output.DataSourceCoverageTypeList.Add(item);
                    }
                }
            }
            else
            {
                output.DataSourceCoverageTypeList = new List <string>();
            }

            //
            if (IsValidDate(input.Created))
            {
                output.Created = ( DateTime )input.Created;
            }
            if (IsValidDate(input.LastUpdated))
            {
                output.LastUpdated = ( DateTime )input.LastUpdated;
            }

            //components
            if (includingParts)
            {
                //get DataAttributes
                output.DataAttributes = DataProfileManager.GetAll(output.Id);
            }
        }         //
        public static ThisEntity GetBasic(int id)
        {
            ThisEntity entity = new ThisEntity();

            using (var context = new EntityContext())
            {
                DBEntity item = context.DataSetTimeFrame
                                .SingleOrDefault(s => s.Id == id);

                if (item != null && item.Id > 0)
                {
                    MapFromDB(item, entity, false);
                }
            }

            return(entity);
        }
        //

        public static void MapToDB(ThisEntity input, DBEntity output)
        {
            //want output ensure fields input create are not wiped
            if (output.Id == 0)
            {
                output.DataSetProfileId = input.DataSetProfileId;
            }
            output.Id = input.Id;
            //output.EntityStateId = input.EntityStateId;
            output.Description = GetData(input.Description);
            output.Name        = GetData(input.Name);
            if (IsValidDate(input.StartDate))
            {
                output.StartDate = input.StartDate;
            }
            else
            {
                output.StartDate = null;
            }

            if (IsValidDate(input.EndDate))
            {
                output.EndDate = input.EndDate;
            }
            else
            {
                output.EndDate = null;
            }
            //
            if (input.DataSourceCoverageTypeList != null && input.DataSourceCoverageTypeList.Any())
            {
                output.DataSourceCoverageType = string.Join("|", input.DataSourceCoverageTypeList);
            }
            else
            {
                output.DataSourceCoverageType = null;
            }
        }
        /// <summary>
        /// add a DataSetTimeFrame
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        private int Add(ThisEntity entity, ref SaveStatus status)
        {
            DBEntity efEntity = new DBEntity();

            using (var context = new EntityContext())
            {
                try
                {
                    MapToDB(entity, efEntity);

                    if (IsValidGuid(entity.RowId))
                    {
                        efEntity.RowId = entity.RowId;
                    }
                    else
                    {
                        efEntity.RowId = Guid.NewGuid();
                    }

                    if (IsValidDate(status.EnvelopeCreatedDate))
                    {
                        efEntity.Created     = status.LocalCreatedDate;
                        efEntity.LastUpdated = status.LocalCreatedDate;
                    }
                    else
                    {
                        efEntity.Created     = System.DateTime.Now;
                        efEntity.LastUpdated = System.DateTime.Now;
                    }

                    context.DataSetTimeFrame.Add(efEntity);

                    // submit the change to database
                    int count = context.SaveChanges();
                    if (count > 0)
                    {
                        entity.Id    = efEntity.Id;
                        entity.RowId = efEntity.RowId;

                        if (UpdateParts(entity, ref status) == false)
                        {
                        }

                        return(efEntity.Id);
                    }
                    else
                    {
                        //?no info on error

                        string message = thisClassName + string.Format(". Add Failed", "Attempted to add a DataSetTimeFrame. The process appeared to not work, but was not an exception, so we have no message, or no clue. DataSetTimeFrame: {0}, DataSetProfileId: {1}", entity.Name ?? "no name", entity.DataSetProfileId);
                        status.AddError(thisClassName + ". Error - the add was not successful. " + message);
                        EmailManager.NotifyAdmin("DataSetTimeFrameManager. Add Failed", message);
                    }
                }
                catch (System.Data.Entity.Validation.DbEntityValidationException dbex)
                {
                    string message = HandleDBValidationError(dbex, thisClassName + ".Add() ", "DataSetTimeFrame");
                    status.AddError(thisClassName + ".Add(). Error - the save was not successful. " + message);

                    LoggingHelper.LogError(message, true);
                }
                catch (Exception ex)
                {
                    string message = FormatExceptions(ex);
                    LoggingHelper.LogError(ex, thisClassName + string.Format(".Add(), DataSetProfileId: {0}\r\n", efEntity.DataSetProfileId));
                    status.AddError(thisClassName + ".Add(). Error - the save was not successful. \r\n" + message);
                }
            }

            return(efEntity.Id);
        }
        public bool Save(ThisEntity entity, ref SaveStatus status)
        {
            bool isValid = true;
            int  count   = 0;

            try
            {
                using (var context = new EntityContext())
                {
                    if (ValidateProfile(entity, ref status) == false)
                    {
                        //return false;
                    }

                    if (entity.Id > 0)
                    {
                        //TODO - consider if necessary, or interferes with anything
                        context.Configuration.LazyLoadingEnabled = false;
                        DBEntity efEntity = context.DataSetTimeFrame
                                            .SingleOrDefault(s => s.Id == entity.Id);

                        if (efEntity != null && efEntity.Id > 0)
                        {
                            //fill in fields that may not be in entity
                            entity.RowId = efEntity.RowId;

                            MapToDB(entity, efEntity);
                            //these classes should probably use the dates from the parent
                            if (IsValidDate(status.EnvelopeCreatedDate) && status.LocalCreatedDate < efEntity.Created)
                            {
                                efEntity.Created = status.LocalCreatedDate;
                            }
                            //has changed?
                            if (HasStateChanged(context))
                            {
                                if (IsValidDate(status.EnvelopeUpdatedDate))
                                {
                                    efEntity.LastUpdated = status.LocalUpdatedDate;
                                }
                                else
                                {
                                    efEntity.LastUpdated = DateTime.Now;
                                }
                                //NOTE efEntity.EntityStateId is set to 0 in delete method )
                                count = context.SaveChanges();
                                //can be zero if no data changed
                                if (count >= 0)
                                {
                                    isValid = true;
                                }
                                else
                                {
                                    //?no info on error

                                    isValid = false;
                                    string message = string.Format(thisClassName + ".Save Failed", "Attempted to update a DataSetTimeFrame. The process appeared to not work, but was not an exception, so we have no message, or no clue. DataSetTimeFrame: {0}, Id: {1}", entity.Name, entity.Id);
                                    status.AddError("Error - the update was not successful. " + message);
                                    EmailManager.NotifyAdmin(thisClassName + ".Save Failed Failed", message);
                                }
                            }

                            if (isValid)
                            {
                                if (!UpdateParts(entity, ref status))
                                {
                                    isValid = false;
                                }
                            }
                        }
                        else
                        {
                            status.AddError("Error - update failed, as DataSetTimeFrame was not found.");
                        }
                    }
                    else
                    {
                        //add
                        int newId = Add(entity, ref status);
                        if (newId == 0 || status.HasErrors)
                        {
                            isValid = false;
                        }
                    }
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbex)
            {
                string message = HandleDBValidationError(dbex, thisClassName + string.Format(".Save. id: {0}, Name: {1}", entity.Id, entity.Name), "DataSetTimeFrame");
                status.AddError(thisClassName + ".Save(). Error - the save was not successful. " + message);
            }
            catch (Exception ex)
            {
                string message = FormatExceptions(ex);
                LoggingHelper.LogError(ex, thisClassName + string.Format(".Save. id: {0}, Name: {1}", entity.Id, entity.Name));
                status.AddError(thisClassName + ".Save(). Error - the save was not successful. " + message);
                isValid = false;
            }


            return(isValid);
        }