/// <summary> /// add a ConditionProfile /// </summary> /// <param name="entity"></param> /// <param name="statusMessage"></param> /// <returns></returns> private int Add(ThisEntity entity, DateTime updateDate, ref SaveStatus status) { DBEntity efEntity = new DBEntity(); using (var context = new EntityContext()) { try { MapToDB(entity, efEntity); efEntity.EntityId = entity.EntityId; if (IsValidGuid(entity.RowId)) { efEntity.RowId = entity.RowId; } else { efEntity.RowId = Guid.NewGuid(); } efEntity.Created = efEntity.LastUpdated = updateDate; context.Entity_AggregateDataProfile.Add(efEntity); // submit the change to database int count = context.SaveChanges(); if (count > 0) { entity.Id = efEntity.Id; entity.RowId = efEntity.RowId; UpdateParts(entity, updateDate, ref status); return(efEntity.Id); } else { //?no info on error status.AddWarning("Error - the profile was not saved. "); string message = string.Format("{0}.Add() Failed", "Attempted to add a AggregateDataProfile. The process appeared to not work, but was not an exception, so we have no message, or no clue. AggregateDataProfile. EntityId: {1}", thisClassName, entity.EntityId); EmailManager.NotifyAdmin(thisClassName + ".Add() Failed", message); } } catch (System.Data.Entity.Validation.DbEntityValidationException dbex) { string message = HandleDBValidationError(dbex, "AggregateDataProfileManager.Add()", string.Format("EntityId: 0 ", entity.EntityId)); status.AddWarning(message); } catch (Exception ex) { LoggingHelper.LogError(ex, thisClassName + string.Format(".Add(), EntityId: {0}", entity.EntityId)); } } return(efEntity.Id); }
private bool Save(ThisEntity item, Entity parent, DateTime updateDate, ref SaveStatus status) { bool isValid = true; item.EntityId = parent.Id; using (var context = new EntityContext()) { if (!ValidateProfile(item, ref status)) { return(false); } //should always be add if always resetting the entity if (item.Id > 0) { DBEntity p = context.Entity_AggregateDataProfile .FirstOrDefault(s => s.Id == item.Id); if (p != null && p.Id > 0) { item.RowId = p.RowId; item.EntityId = p.EntityId; MapToDB(item, p); if (HasStateChanged(context)) { p.LastUpdated = System.DateTime.Now; context.SaveChanges(); } //regardless, check parts isValid = UpdateParts(item, updateDate, ref status); } else { //error should have been found isValid = false; status.AddWarning(string.Format("Error: the requested record was not found: recordId: {0}", item.Id)); } } else { int newId = Add(item, updateDate, ref status); if (newId == 0 || status.HasErrors) { isValid = false; } } } return(isValid); }
} // private static void MapToDB(ThisEntity input, DBEntity output) { //want output ensure fields input create are not wiped if (output.Id < 1) { //output.EntityId = input.; } else { } output.Id = input.Id; output.Name = GetData(input.Name); output.Description = GetData(input.Description); output.DemographicInformation = GetData(input.DemographicInformation); // output.NumberAwarded = input.NumberAwarded; output.LowEarnings = input.LowEarnings; output.MedianEarnings = input.MedianEarnings; output.HighEarnings = input.HighEarnings; output.PostReceiptMonths = input.PostReceiptMonths; output.Source = input.Source; if (IsValidDate(input.DateEffective)) { output.DateEffective = DateTime.Parse(input.DateEffective); } else { output.DateEffective = null; } if (input.JobsObtained != null && input.JobsObtained.Any()) { output.JobsObtainedJson = JsonConvert.SerializeObject(input.JobsObtained); } else { output.JobsObtainedJson = null; } // output.Currency = input.Currency; }
public static void MapFromDB(DBEntity input, ThisEntity output) { output.Id = input.Id; output.RowId = input.RowId; output.Name = input.Name == null ? "" : input.Name; output.Description = input.Description == null ? "" : input.Description; output.DemographicInformation = input.DemographicInformation == null ? "" : input.DemographicInformation; if (IsValidDate(input.DateEffective)) { output.DateEffective = (( DateTime )input.DateEffective).ToString("yyyy-MM-dd"); } else { output.DateEffective = ""; } // output.NumberAwarded = (input.NumberAwarded ?? 0); output.LowEarnings = (input.LowEarnings ?? 0); output.MedianEarnings = (input.MedianEarnings ?? 0); output.HighEarnings = (input.HighEarnings ?? 0); output.PostReceiptMonths = (input.PostReceiptMonths ?? 0); output.Source = GetUrlData(input.Source); output.Currency = input.Currency; Views.Codes_Currency code = CodesManager.GetCurrencyItem(output.Currency); if (code != null && code.NumericCode > 0) { output.Currency = code.Currency; output.CurrencySymbol = code.HtmlCodes; } if (IsValidDate(input.Created)) { output.Created = ( DateTime )input.Created; } if (IsValidDate(input.LastUpdated)) { output.LastUpdated = ( DateTime )input.LastUpdated; } if (!string.IsNullOrEmpty(input.JobsObtainedJson)) { var jobsObtained = JsonConvert.DeserializeObject <List <QuantitativeValue> >(input.JobsObtainedJson); if (jobsObtained != null) { output.JobsObtained = jobsObtained; } } //===== //var relatedEntity = EntityManager.GetEntity( output.RowId, false ); //if ( relatedEntity != null && relatedEntity.Id > 0 ) // output.EntityLastUpdated = relatedEntity.LastUpdated; //components // output.Jurisdiction = Entity_JurisdictionProfileManager.Jurisdiction_GetAll(output.RowId); //get datasetprofiles output.RelevantDataSet = DataSetProfileManager.GetAll(output.RowId, true); //========== } //