public static void MapToDB(ThisEntity from, DBEntity to) { to.Id = from.Id; if (to.Id < 1) { } else { } //don't map rowId, ctid, or dates as not on form //to.RowId = from.RowId; to.Name = from.Name; to.CTID = from.CTID; if (!string.IsNullOrWhiteSpace(from.CredentialRegistryId)) { //this may not exist if added as pending? to.CredentialRegistryId = from.CredentialRegistryId ?? ""; } to.EntityStateId = from.EntityStateId > 0 ? from.EntityStateId : 3; to.Description = from.Description; to.SubjectWebpage = from.SubjectWebpage; if (from.OwningAgentUid != null) { to.OwningAgentUid = ( Guid )from.OwningAgentUid; } }
public static void MapFromDB(DBEntity input, ThisEntity output, bool includingPathways = false) { output.Id = input.Id; output.RowId = input.RowId; output.CTID = input.CTID; output.Name = input.Name; output.Description = input.Description; output.SubjectWebpage = input.SubjectWebpage; output.EntityStateId = (int)(input.EntityStateId ?? 2); //TODO - get pathways if (includingPathways) { output.Pathways = Entity_PathwayManager.GetAll(output.RowId, true); output.HasPathway = output.Pathways.Select(m => m.CTID).ToList(); } else { //really only need the pathway ids for publishing - also ctid //might just get the lite versions of a pathway? output.Pathways = Entity_PathwayManager.GetAll(output.RowId, false); //always collect for now output.HasPathway = output.Pathways.Select(m => m.CTID).ToList(); } if (IsGuidValid(input.OwningAgentUid)) { output.OwningAgentUid = ( Guid )input.OwningAgentUid; output.OwningOrganization = OrganizationManager.GetForSummary(output.OwningAgentUid); //get roles OrganizationRoleProfile orp = Entity_AgentRelationshipManager.AgentEntityRole_GetAsEnumerationFromCSV(output.RowId, output.OwningAgentUid); output.OwnerRoles = orp.AgentRole; } // output.OrganizationRole = Entity_AgentRelationshipManager.AgentEntityRole_GetAll_ToEnumeration(output.RowId, true); //confustion over OrganizationRole and OwnerRoles (enum)!!! //to.OrganizationRole = Entity_AgentRelationshipManager.AgentEntityRole_GetAll_ToEnumeration( to.RowId, true ); output.CredentialRegistryId = input.CredentialRegistryId; if (IsValidDate(input.Created)) { output.Created = ( DateTime )input.Created; } if (IsValidDate(input.LastUpdated)) { output.LastUpdated = ( DateTime )input.LastUpdated; } var relatedEntity = EntityManager.GetEntity(output.RowId, false); if (relatedEntity != null && relatedEntity.Id > 0) { output.EntityLastUpdated = relatedEntity.LastUpdated; } }
//currently public bool DoesEntityExist(string ctid, ref ThisEntity entity) { bool exists = false; entity = EntityServices.PathwaySetGetByCtid(ctid); if (entity != null && entity.Id > 0) { return(true); } return(exists); }
/// <summary> /// Get a pathway set /// if includingPathways is false, only a list of pathway ids is returned /// </summary> /// <param name="id"></param> /// <param name="includingPathways"></param> /// <returns></returns> public static ThisEntity Get(int id, bool includingPathways = true) { ThisEntity entity = new ThisEntity(); using (var context = new EntityContext()) { DBEntity item = context.PathwaySet .SingleOrDefault(s => s.Id == id); if (item != null && item.Id > 0) { MapFromDB(item, entity, includingPathways); } } return(entity); }
public bool UpdateParts(ThisEntity entity, ref SaveStatus status) { bool isAllValid = true; Entity_AgentRelationshipManager mgr = new Entity_AgentRelationshipManager(); Entity relatedEntity = EntityManager.GetEntity(entity.RowId); if (relatedEntity == null || relatedEntity.Id == 0) { status.AddError(thisClassName + " - Error - the parent entity was not found."); return(false); } mgr.DeleteAll(relatedEntity, ref status); mgr.SaveList(relatedEntity.Id, Entity_AgentRelationshipManager.ROLE_TYPE_OFFERED_BY, entity.OfferedBy, ref status); mgr.SaveList(relatedEntity.Id, Entity_AgentRelationshipManager.ROLE_TYPE_OWNER, entity.OwnedBy, ref status); var epmgr = new Entity_PathwayManager(); //handle pathways - using replace //actually just use typical pattern of delete all and then add //could be extreme //epmgr.DeleteAll( parent.EntityUid, ref status ); var list = new List <int>(); //check if we should get the list of ids easier foreach (var item in entity.HasPathwayList) { var p = PathwayManager.GetBasic(item); if (p != null && p.Id > 0) { list.Add(p.Id); } else { //?? status.AddError(thisClassName + string.Format(" - Error - the pathway using Guid: {0} entity was not found.", item.ToString())); } } if (!new Entity_PathwayManager().Replace(entity.RowId, 1, list, ref status)) { isAllValid = false; } return(isAllValid); }
/// <summary> /// add a PathwaySet /// </summary> /// <param name="entity"></param> /// <param name="statusMessage"></param> /// <returns></returns> public bool Save(ThisEntity entity, ref SaveStatus status) { bool isValid = true; var efEntity = new DBEntity(); using (var context = new EntityContext()) { try { //messages = new List<string>(); if (ValidateProfile(entity, ref status) == false) { return(false); } if (entity.Id == 0) { //entity.StatusId = 1; MapToDB(entity, efEntity); if (entity.RowId == null || entity.RowId == Guid.Empty) { efEntity.RowId = entity.RowId = Guid.NewGuid(); } else { efEntity.RowId = entity.RowId; } if (IsValidDate(status.EnvelopeCreatedDate)) { efEntity.Created = status.LocalCreatedDate; efEntity.LastUpdated = status.LocalCreatedDate; } else { efEntity.Created = System.DateTime.Now; efEntity.LastUpdated = System.DateTime.Now; } context.PathwaySet.Add(efEntity); // submit the change to database int count = context.SaveChanges(); if (count > 0) { entity.Id = efEntity.Id; //add log entry SiteActivity sa = new SiteActivity() { ActivityType = "PathwaySet", Activity = "Import", Event = "Add", Comment = string.Format("Full PathwaySet was added by the import. Name: {0}, SWP: {1}", entity.Name, entity.SubjectWebpage), ActivityObjectId = entity.Id }; new ActivityManager().SiteActivityAdd(sa); UpdateParts(entity, ref status); return(true); } else { //?no info on error status.AddError("Error - the profile was not saved. "); string message = string.Format("PathwayManager.Add Failed. Attempted to add a PathwaySet. The process appeared to not work, but was not an exception, so we have no message, or no clue.PathwaySet. PathwaySet: {0}, createdById: {1}", entity.Name, entity.CreatedById); EmailManager.NotifyAdmin(thisClassName + ".Add Failed", message); } } else { efEntity = context.PathwaySet .SingleOrDefault(s => s.Id == entity.Id); if (efEntity != null && efEntity.Id > 0) { //for updates, chances are some fields will not be in interface, don't map these (ie created stuff) MapToDB(entity, efEntity); if (IsValidDate(status.EnvelopeCreatedDate) && status.LocalCreatedDate < efEntity.Created) { efEntity.Created = status.LocalCreatedDate; } if (IsValidDate(status.EnvelopeUpdatedDate) && status.LocalUpdatedDate != efEntity.LastUpdated) { efEntity.LastUpdated = status.LocalUpdatedDate; } //has changed? if (HasStateChanged(context)) { if (IsValidDate(status.EnvelopeUpdatedDate)) { efEntity.LastUpdated = status.LocalUpdatedDate; } else { efEntity.LastUpdated = DateTime.Now; } int count = context.SaveChanges(); //can be zero if no data changed if (count >= 0) { isValid = true; } else { //?no info on error status.AddError("Error - the update was not successful. "); string message = string.Format(thisClassName + ".Save Failed", "Attempted to update a PathwaySet. The process appeared to not work, but was not an exception, so we have no message, or no clue. PathwayId: {0}, Id: {1}, updatedById: {2}", entity.Id, entity.Id, entity.LastUpdatedById); EmailManager.NotifyAdmin(thisClassName + ". Pathway_Update Failed", message); } } //continue with parts regardless UpdateParts(entity, ref status); } else { status.AddError("Error - update failed, as record was not found."); } } } //catch ( System.Data.Entity.Validation.DBEntityValidationException dbex ) //{ // //LoggingHelper.LogError( dbex, thisClassName + string.Format( ".ContentAdd() DBEntityValidationException, Type:{0}", entity.TypeId ) ); // string message = thisClassName + string.Format( ".Pathway_Add() DBEntityValidationException, PathwayId: {0}", PathwaySet.Id ); // foreach ( var eve in dbex.EntityValidationErrors ) // { // message += string.Format( "\rEntity of type \"{0}\" in state \"{1}\" has the following validation errors:", // eve.Entry.Entity.GetType().Name, eve.Entry.State ); // foreach ( var ve in eve.ValidationErrors ) // { // message += string.Format( "- Property: \"{0}\", Error: \"{1}\"", // ve.PropertyName, ve.ErrorMessage ); // } // LoggingHelper.LogError( message, true ); // } //} catch (Exception ex) { LoggingHelper.LogError(ex, thisClassName + string.Format(".Save(), PathwaySet: '{0}'", entity.Name)); status.AddError(string.Format("PathwayManager.Save Failed. PathwaySet: {0}, createdById: {1}, Error: {2}", entity.Name, entity.CreatedById, ex.Message)); isValid = false; } } return(isValid); }