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.Created))
            {
                output.Created = ( DateTime )input.Created;
            }
            if (IsValidDate(input.LastUpdated))
            {
                output.LastUpdated = ( DateTime )input.LastUpdated;
            }

            //components
            if (includingParts)
            {
                //get DataAttributes
                //
                output.DataProfileAttributeSummaryJson = input.DataProfileAttributeSummaryJson;
                output.DataProfileAttributesJson       = input.DataProfileAttributesJson;

                output.DataProfileAttributeSummary = JsonConvert.DeserializeObject <DataProfileJson>(input.DataProfileAttributeSummaryJson);
                output.DataProfileAttributes       = JsonConvert.DeserializeObject <DataProfileAttributes>(input.DataProfileAttributesJson);
            }
        }         //
        //
        public static void MapToDB(ThisEntity input, DBEntity output)
        {
            //want output ensure fields input create are not wiped
            if (output.Id == 0)
            {
                output.DataSetTimeFrameId = input.DataSetTimeFrameId;
            }
            output.Id = input.Id;
            //output.EntityStateId = input.EntityStateId;
            output.Description = GetData(input.Description);

            //
            output.DataProfileAttributeSummaryJson = JsonConvert.SerializeObject(input.DataProfileAttributeSummary, JsonHelper.GetJsonSettings());
            //
            output.DataProfileAttributesJson = JsonConvert.SerializeObject(input.DataProfileAttributes, JsonHelper.GetJsonSettings());
        }
        //
        public static ThisEntity GetBasic(int id)
        {
            ThisEntity entity = new ThisEntity();

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

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

            return(entity);
        }
        /// <summary>
        /// add a DataProfile
        /// </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.DataProfile.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
                    {
                        string message = thisClassName + string.Format(". Add Failed", "Attempted to add a DataProfile. The process appeared to not work, but was not an exception, so we have no message, or no clue. DataProfile: {0}, DataSetProfileId: {1}", entity.DataSetTimeFrameId, entity.DataSetTimeFrameId);
                        status.AddError(thisClassName + ". Error - the add was not successful. " + message);
                        EmailManager.NotifyAdmin("DataProfileManager. Add Failed", message);
                    }
                }
                catch (System.Data.Entity.Validation.DbEntityValidationException dbex)
                {
                    string message = HandleDBValidationError(dbex, thisClassName + ".Add() ", "DataProfile");
                    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(), DataSetTimeFrameId: {0}\r\n", efEntity.DataSetTimeFrameId));
                    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)
                    {
                        context.Configuration.LazyLoadingEnabled = false;
                        DBEntity efEntity = context.DataProfile
                                            .SingleOrDefault(s => s.Id == entity.Id);

                        if (efEntity != null && efEntity.Id > 0)
                        {
                            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 DataProfile. The process appeared to not work, but was not an exception, so we have no message, or no clue. DataSetTimeFrameId: {0}, Id: {1}", entity.DataSetTimeFrameId, 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 DataProfile was not found.");
                        }
                    }
                    else
                    {
                        //add
                        int newId = Add(entity, ref status);
                        if (newId == 0 || status.HasErrors)
                        {
                            isValid = false;
                        }
                        //status.Messages = new List<StatusMessage>();
                        //status.HasErrors = false;
                    }
                }
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbex)
            {
                string message = HandleDBValidationError(dbex, thisClassName + string.Format(".Save. id: {0}, DataSetTimeFrameId: {1}", entity.Id, entity.DataSetTimeFrameId), "DataProfile");
                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}, DataSetTimeFrameId: {1}", entity.Id, entity.DataSetTimeFrameId));
                status.AddError(thisClassName + ".Save(). Error - the save was not successful. " + message);
                isValid = false;
            }


            return(isValid);
        }