/// <summary> /// Update Entity properies /// </summary> /// <param name="entity"></param> /// <param name="parentUid"></param> /// <param name="parentTypeId">NOt used directly, useful for messages</param> /// <param name="categoryId">This could be part of the entity, just need to confirm</param> /// <param name="statusMessage"></param> /// <returns></returns> public bool AddProperties(Enumeration entity, Guid parentUid, int parentTypeId, int categoryId, bool isRequired, ref SaveStatus status) { bool isAllValid = true; int updatedCount = 0; int count = 0; if (!IsGuidValid(parentUid)) { status.AddError("A valid identifier was not provided to the Update method."); return(false); } if (entity == null) { entity = new Enumeration(); return(true); } //get parent entity Entity parent = EntityManager.GetEntity(parentUid); if (parent == null || parent.Id == 0) { status.AddError("Error - the parent entity was not found."); return(false); } string schemaName = ""; using (var context = new EntityContext()) { DBEntity op = new DBEntity(); foreach (var item in entity.Items) { schemaName = ""; if (!string.IsNullOrWhiteSpace(item.SchemaName)) { schemaName = item.SchemaName; } else if (!string.IsNullOrWhiteSpace(item.Name)) { schemaName = item.Name; } if (!string.IsNullOrWhiteSpace(schemaName)) { CodeItem code = CodesManager.Codes_PropertyValue_GetBySchema(categoryId, schemaName); if (code == null || code.Id == 0 && categoryId == CodesManager.PROPERTY_CATEGORY_ASMT_DELIVERY_TYPE && schemaName == "deliveryType:BlendedDelivery") { schemaName = schemaName.Replace("deliveryType", "assessmentDeliveryType"); code = CodesManager.Codes_PropertyValue_GetBySchema(categoryId, schemaName); } if (code != null && code.Id > 0) { op = new DBEntity(); op.EntityId = parent.Id; op.PropertyValueId = code.Id; op.Created = System.DateTime.Now; //do a quick duplicates check var property = context.Entity_Property.FirstOrDefault(s => s.EntityId == parent.Id && s.PropertyValueId == code.Id); if (property == null || property.Id == 0) { context.Entity_Property.Add(op); count = context.SaveChanges(); if (count == 0) { status.AddWarning(string.Format(thisClassName + ".AddProperties(). Unable to add property value Id of: {0} for categoryId: {1}, parentTypeId: {2} ", code.Id, categoryId, parentTypeId)); isAllValid = false; } else { updatedCount++; } } else { //not sure how can happen status.AddWarning(string.Format(thisClassName + ".AddProperties(). Duplicate property encountered for categoryId: {0}, propertyValueId: {1} parentTypeId: {2}, parent.Id: {3}. IGNORED ", categoryId, code.Id, parentTypeId, parent.Id)); } } else { //document invalid schema status.AddWarning(string.Format(thisClassName + ".AddProperties(). Invalid schema name encountered of: '{0}' for categoryId: {1}, parentTypeId: {2}. IGNORED ", schemaName, categoryId, parentTypeId)); //isAllValid = false; } } else { //document invalid schema status.AddWarning(string.Format(thisClassName + ".AddProperties(). Invalid schema name encountered of: '{0}' for categoryId: {1}, parentTypeId: {2} ", schemaName, categoryId, parentTypeId)); isAllValid = false; } } } if (updatedCount == 0 && isRequired) { //document invalid schema status.AddError(string.Format(thisClassName + ".AddProperties(). Error a property is required for categoryId: {0} ", categoryId)); isAllValid = false; } return(isAllValid); }
/// <summary> /// Persist Cost Profile /// </summary> /// <param name="entity"></param> /// <param name="parentUid"></param> /// <param name="userId"></param> /// <param name="messages"></param> /// <returns></returns> public bool Save(ThisEntity entity, Guid parentUid, ref SaveStatus status) { bool isValid = true; if (!IsValidGuid(parentUid)) { status.AddError(thisClassName + " - Error: the parent identifier was not provided."); return(false); } //get parent entity Entity parent = EntityManager.GetEntity(parentUid); if (parent == null || parent.Id == 0) { status.AddError(thisClassName + " - Error - the parent entity was not found."); return(false); } int count = 0; DBEntity efEntity = new DBEntity(); using (var context = new EntityContext()) { if (ValidateProfile(entity, ref status) == false) { //can't really scrub from here - too late? //at least add some identifer return(false); } try { if (entity.Id == 0) { //just in case entity.EntityId = parent.Id; //add efEntity = new DBEntity(); MapToDB(entity, efEntity); efEntity.Created = efEntity.LastUpdated = DateTime.Now; if (IsValidGuid(entity.RowId)) { efEntity.RowId = entity.RowId; } else { efEntity.RowId = Guid.NewGuid(); } context.Entity_CostProfile.Add(efEntity); count = context.SaveChanges(); //update profile record so doesn't get deleted entity.Id = efEntity.Id; entity.RowId = efEntity.RowId; if (count == 0) { status.AddError(thisClassName + " - Unable to add Cost Profile"); } else { if (!UpdateParts(entity, ref status)) { isValid = false; } } } else { context.Configuration.LazyLoadingEnabled = false; efEntity = context.Entity_CostProfile.SingleOrDefault(s => s.Id == entity.Id); if (efEntity != null && efEntity.Id > 0) { entity.RowId = efEntity.RowId; //update MapToDB(entity, efEntity); //has changed? if (HasStateChanged(context)) { efEntity.LastUpdated = System.DateTime.Now; count = context.SaveChanges(); } //always check parts if (!UpdateParts(entity, ref status)) { isValid = false; } } } } catch (System.Data.Entity.Validation.DbEntityValidationException dbex) { string message = HandleDBValidationError(dbex, thisClassName + ".Save()", entity.ProfileName); status.AddWarning(thisClassName + " - Error - the save was not successful. " + message); LoggingHelper.LogError(dbex, thisClassName + string.Format(".Save(), Parent: {0} ({1})", parent.EntityBaseName, parent.EntityBaseId)); isValid = false; } catch (Exception ex) { string message = FormatExceptions(ex); status.AddError(thisClassName + " - Error - the save was not successful. " + message); LoggingHelper.LogError(ex, thisClassName + string.Format(".Save(), Parent: {0} ({1}), UserId: {2}", parent.EntityBaseName, parent.EntityBaseId)); isValid = false; } } return(isValid); }
/// <summary> /// Format a summary of the EmploymentOutcomeProfile for use in search and gray boxes /// </summary> /// <param name="parentUid"></param> /// <returns></returns> public static string GetSummary(Guid parentUid) { var list = new List <EmploymentOutcomeProfile>(); var entity = new EmploymentOutcomeProfile(); Entity parent = EntityManager.GetEntity(parentUid); LoggingHelper.DoTrace(7, string.Format(thisClassName + ".GetAll: parentUid:{0} entityId:{1}, e.EntityTypeId:{2}", parentUid, parent.Id, parent.EntityTypeId)); var summary = ""; var lineBreak = ""; try { using (var context = new EntityContext()) { List <DBEntity> results = context.Entity_EmploymentOutcomeProfile .Where(s => s.EntityId == parent.Id) .OrderBy(s => s.Created) .ToList(); if (results != null && results.Count > 0) { foreach (DBEntity item in results) { entity = new EmploymentOutcomeProfile(); if (item.EmploymentOutcomeProfile != null && item.EmploymentOutcomeProfile.EntityStateId > 1) { if (!string.IsNullOrWhiteSpace(item.EmploymentOutcomeProfile.Name)) { summary = item.EmploymentOutcomeProfile.Name + lineBreak; } else if (!string.IsNullOrWhiteSpace(item.EmploymentOutcomeProfile.Description)) { summary = item.EmploymentOutcomeProfile.Description.Length < 200 ? item.EmploymentOutcomeProfile.Description : item.EmploymentOutcomeProfile.Description.Substring(0, 200) + " ... " + lineBreak; } else { } if (item.EmploymentOutcomeProfile.JobsObtainedJson != null) { var jp = JsonConvert.DeserializeObject <EmploymentOutcomeProfileProperties>(item.EmploymentOutcomeProfile.JobsObtainedJson); if (jp != null) { //unpack JobsObtainedList var jobsObtained = jp.JobsObtainedList; var joSummary = jobsObtained[0].Summary(); //need a helper for display if (!string.IsNullOrWhiteSpace(joSummary)) { summary += string.Format(" Jobs Obtained: {0}", joSummary); } } } } lineBreak = "<\br>"; } } return(summary); } } catch (Exception ex) { LoggingHelper.LogError(ex, thisClassName + ".GetSummary"); } return(summary); }
/// <summary> /// Persist ProcessProfile /// </summary> /// <param name="entity"></param> /// <param name="parentUid"></param> /// <param name="userId"></param> /// <param name="messages"></param> /// <returns></returns> public bool Save(ThisEntity entity, Guid parentUid, ref SaveStatus status) { status.HasSectionErrors = false; if (!IsValidGuid(parentUid)) { status.AddError("Error: the parent identifier was not provided."); return(false); } int count = 0; DBEntity efEntity = new DBEntity(); Entity parent = EntityManager.GetEntity(parentUid); if (parent == null || parent.Id == 0) { status.AddError("Error - the parent entity was not found."); return(false); } //determine type int profileTypeId = 0; if (entity.ProcessTypeId > 0) { profileTypeId = entity.ProcessTypeId; } else { // switch (entity.ProcessProfileType) { case "AppealProcess": entity.ProcessTypeId = APPEAL_PROCESS_TYPE; break; case "ComplaintProcess": entity.ProcessTypeId = COMPLAINT_PROCESS_TYPE; break; //case "CriteriaProcess": // entity.ProcessTypeId = CRITERIA_PROCESS_TYPE; // break; case "ReviewProcess": entity.ProcessTypeId = REVIEW_PROCESS_TYPE; break; case "RevocationProcess": entity.ProcessTypeId = REVOKE_PROCESS_TYPE; break; case "ProcessProfile": entity.ProcessTypeId = DEFAULT_PROCESS_TYPE; break; case "CredentialProcess": entity.ProcessTypeId = DEFAULT_PROCESS_TYPE; break; case "MaintenanceProcess": entity.ProcessTypeId = MTCE_PROCESS_TYPE; break; case "AdministrationProcess": entity.ProcessTypeId = ADMIN_PROCESS_TYPE; break; case "DevelopmentProcess": entity.ProcessTypeId = DEV_PROCESS_TYPE; break; // default: entity.ProcessTypeId = 1; status.AddError(string.Format("Error: Unexpected profile type of {0} was encountered.", entity.ProcessProfileType)); return(false); } } using (var context = new EntityContext()) { if (ValidateProfile(entity, ref status) == false) { status.AddError("Process Profile was invalid. " + SetEntitySummary(entity)); return(false); } if (entity.Id == 0) { //add efEntity = new DBEntity(); MapToDB(entity, efEntity); efEntity.EntityId = parent.Id; efEntity.Created = efEntity.LastUpdated = DateTime.Now; if (IsValidGuid(entity.RowId)) { efEntity.RowId = entity.RowId; } else { efEntity.RowId = Guid.NewGuid(); } context.Entity_ProcessProfile.Add(efEntity); count = context.SaveChanges(); //update profile record so doesn't get deleted entity.Id = efEntity.Id; entity.ParentId = parent.Id; entity.RowId = efEntity.RowId; if (count == 0) { status.AddError(string.Format(" Unable to add Profile: {0} <br\\> ", string.IsNullOrWhiteSpace(entity.ProfileName) ? "no description" : entity.ProfileName)); } else { //other entity components use a trigger to create the entity Object. If a trigger is not created, then child adds will fail (as typically use entity_summary to get the parent. As the latter is easy, make the direct call? UpdateParts(entity, ref status); } } else { entity.ParentId = parent.Id; efEntity = context.Entity_ProcessProfile.SingleOrDefault(s => s.Id == entity.Id); if (efEntity != null && efEntity.Id > 0) { entity.RowId = efEntity.RowId; //update MapToDB(entity, efEntity); //has changed? if (HasStateChanged(context)) { efEntity.LastUpdated = System.DateTime.Now; count = context.SaveChanges(); } //always check parts UpdateParts(entity, ref status); } } } return(!status.HasSectionErrors); }
/// <summary> /// Add an Entity_EmploymentOutcomeProfile /// </summary> /// <param name="parentUid"></param> /// <param name="EmploymentOutcomeProfileId"></param> /// <param name="messages"></param> /// <returns></returns> public int Add(Guid parentUid, int employmentOutcomeProfileId, ref SaveStatus status) { int id = 0; int count = 0; if (employmentOutcomeProfileId == 0) { status.AddError(string.Format("A valid EmploymentOutcomeProfile identifier was not provided to the {0}.EntityEarnings_Add method.", thisClassName)); return(0); } Entity parent = EntityManager.GetEntity(parentUid); if (parent == null || parent.Id == 0) { status.AddError("Error - the parent entity was not found."); return(0); } using (var context = new EntityContext()) { DBEntity efEntity = new DBEntity(); try { efEntity = context.Entity_EmploymentOutcomeProfile .FirstOrDefault(s => s.EntityId == parent.Id && s.EmploymentOutcomeProfileId == employmentOutcomeProfileId); if (efEntity != null && efEntity.Id > 0) { return(0); } efEntity = new DBEntity { EntityId = parent.Id, EmploymentOutcomeProfileId = employmentOutcomeProfileId, Created = System.DateTime.Now }; context.Entity_EmploymentOutcomeProfile.Add(efEntity); // submit the change to database count = context.SaveChanges(); if (count > 0) { if (efEntity.Id == 0) { List <string> messages = new List <string>(); Delete(efEntity.Id, ref messages); context.Entity_EmploymentOutcomeProfile.Add(efEntity); count = context.SaveChanges(); id = efEntity.Id; } else { id = efEntity.Id; } return(efEntity.Id); } else { //?no info on error status.AddError(thisClassName + "Error - the add was not successful."); string message = thisClassName + string.Format(".Add Failed", "Attempted to add an Entity_EmploymentOutcomeProfile. The process appeared to not work, but there was no exception, so we have no message, or no clue. Parent Profile: {0}, Type: {1}, EarningsId: {2}", parentUid, parent.EntityType, employmentOutcomeProfileId); EmailManager.NotifyAdmin(thisClassName + ".Add Failed", message); } } catch (System.Data.Entity.Validation.DbEntityValidationException dbex) { string message = HandleDBValidationError(dbex, thisClassName + ".Add() ", "Entity_Earnings"); status.AddError("Error - the save was not successful. " + message); LoggingHelper.LogError(dbex, thisClassName + string.Format(".Save(), Parent: {0} ({1})", parent.EntityBaseName, parent.EntityBaseId)); } catch (Exception ex) { string message = FormatExceptions(ex); status.AddError("Error - the save was not successful. " + message); LoggingHelper.LogError(ex, thisClassName + string.Format(".Save(), Parent: {0} ({1})", parent.EntityBaseName, parent.EntityBaseId)); } } return(id); }