} // public static ThisEntity GetByCtid(string ctid) { ThisEntity entity = new ThisEntity(); if (string.IsNullOrWhiteSpace(ctid)) { return(entity); } try { using (var context = new EntityContext()) { //lookup by frameworkUri, or SourceUrl DBEntity item = context.CompetencyFramework .FirstOrDefault(s => s.CTID.ToLower() == ctid.ToLower()); if (item != null && item.Id > 0) { MapFromDB(item, entity); } } } catch (Exception ex) { LoggingHelper.LogError(ex, thisClassName + ".GetByUrl"); } return(entity); } //
/// <summary> /// Get a competency record /// </summary> /// <param name="profileId"></param> /// <returns></returns> public static ThisEntity Get(int profileId) { ThisEntity entity = new ThisEntity(); if (profileId == 0) { return(entity); } try { using (var context = new EntityContext()) { DBEntity item = context.CompetencyFramework .SingleOrDefault(s => s.Id == profileId); if (item != null && item.Id > 0) { MapFromDB(item, entity); } } } catch (Exception ex) { LoggingHelper.LogError(ex, thisClassName + ".Get"); } return(entity); } //
public int AddPendingRecord(Guid entityUid, string ctid, string registryAtId, ref string status) { DBEntity efEntity = new DBEntity(); try { //var pathwayCTIDTemp = "ce-abcb5fe0-8fde-4f06-9d70-860cd5bdc763"; using (var context = new EntityContext()) { if (!IsValidGuid(entityUid)) { status = thisClassName + " - A valid GUID must be provided to create a pending entity"; return(0); } //quick check to ensure not existing ThisEntity entity = GetByCtid(ctid); if (entity != null && entity.Id > 0) { return(entity.Id); } //only add DB required properties //NOTE - an entity will be created via trigger efEntity.Name = "Placeholder until full document is downloaded"; efEntity.Description = "Placeholder until full document is downloaded"; //realitically the component should be added in the same workflow efEntity.EntityStateId = 1; efEntity.RowId = entityUid; //watch that Ctid can be updated if not provided now!! efEntity.CTID = ctid; efEntity.FrameworkUri = registryAtId; efEntity.Created = System.DateTime.Now; efEntity.LastUpdated = System.DateTime.Now; context.CompetencyFramework.Add(efEntity); int count = context.SaveChanges(); if (count > 0) { return(efEntity.Id); } status = thisClassName + " Error - the save was not successful, but no message provided. "; } } catch (Exception ex) { string message = FormatExceptions(ex); LoggingHelper.LogError(ex, thisClassName + string.Format(".AddPendingRecord. entityUid: {0}, ctid: {1}", entityUid, ctid)); status = thisClassName + " Error - the save was not successful. " + message; } return(0); }
/// <summary> /// Delete a framework - only if no remaining references!! /// MAY NOT expose initially /// </summary> /// <param name="recordId"></param> /// <param name="statusMessage"></param> /// <returns></returns> public bool Delete(int recordId, ref string statusMessage) { bool isOK = true; using (var context = new EntityContext()) { DBEntity p = context.CompetencyFramework.FirstOrDefault(s => s.Id == recordId); if (p != null && p.Id > 0) { context.CompetencyFramework.Remove(p); int count = context.SaveChanges(); } else { statusMessage = string.Format("The record was not found: {0}", recordId); isOK = false; } } return(isOK); }
} // public static void MapFromDB(DBEntity from, ThisEntity to) { to.Id = from.Id; to.RowId = from.RowId; to.EntityStateId = from.EntityStateId; to.Name = from.Name; to.Description = from.Description; to.CTID = from.CTID; to.OrganizationCTID = from.OrganizationCTID ?? ""; to.SourceUrl = from.SourceUrl; to.FrameworkUri = from.FrameworkUri; to.CredentialRegistryId = from.CredentialRegistryId ?? ""; to.TotalCompetencies = from.TotalCompetencies; to.CompentenciesStore = from.CompetenciesStore; to.CompetencyFrameworkGraph = from.CompetencyFrameworkGraph; to.APIFramework = from.CompetencyFrameworkHierarchy; if (!string.IsNullOrEmpty(to.APIFramework)) { //ApiFramework to.ApiFramework = JsonConvert.DeserializeObject <ApiFramework>(to.APIFramework); } //this should be replace by presence of CredentialRegistryId if (from.ExistsInRegistry != null) { to.ExistsInRegistry = ( bool )from.ExistsInRegistry; } if (from.Created != null) { to.Created = ( DateTime )from.Created; } if (from.LastUpdated != null) { to.LastUpdated = ( DateTime )from.LastUpdated; } //soon to be obsolete //to.FrameworkUrl = from.FrameworkUrl; }
} // public static void MapToDB(ThisEntity from, DBEntity to) { //want to ensure fields from create are not wiped //to.Id = from.Id; to.Name = from.Name; to.Description = from.Description; to.SourceUrl = from.SourceUrl ?? ""; to.FrameworkUri = from.FrameworkUri ?? ""; to.CredentialRegistryId = from.CredentialRegistryId ?? ""; //will want to extract from FrameworkUri (for now) if (!string.IsNullOrWhiteSpace(from.CTID) && from.CTID.Length == 39) { to.CTID = from.CTID; } else { if (to.FrameworkUri.ToLower().IndexOf("credentialengineregistry.org/resources/ce-") > -1 || to.FrameworkUri.ToLower().IndexOf("credentialengineregistry.org/graph/ce-") > -1) { to.CTID = from.FrameworkUri.Substring(from.FrameworkUri.IndexOf("/ce-") + 1); } //else if ( from.FrameworkUri.ToLower().IndexOf("credentialengineregistry.org/resources/ce-") > -1 ) //{ // to.CTID = from.FrameworkUri.Substring(from.FrameworkUri.IndexOf("/ce-") + 1); //} } if (!string.IsNullOrWhiteSpace(from.OrganizationCTID) && from.OrganizationCTID.Length == 39) { to.OrganizationCTID = from.OrganizationCTID; } //TODO - have to be consistent in having this data //this may done separately. At very least setting false will be done separately //actually the presence of a ctid should only be for registry denizen if (from.ExistsInRegistry || (!string.IsNullOrWhiteSpace(from.CTID) && from.CTID.Length == 39) || (!string.IsNullOrWhiteSpace(to.CredentialRegistryId) && to.CredentialRegistryId.Length == 36) ) { to.EntityStateId = 3; to.ExistsInRegistry = from.ExistsInRegistry; } else { //dont think there is a case to set to 1 to.EntityStateId = 2; to.ExistsInRegistry = false; } to.TotalCompetencies = from.TotalCompetencies; if (!string.IsNullOrWhiteSpace(from.CompentenciesStore)) { to.CompetenciesStore = from.CompentenciesStore; } else { //ensure we don't reset the store to.CompetenciesStore = null; } if (!string.IsNullOrWhiteSpace(from.APIFramework)) { to.CompetencyFrameworkHierarchy = from.APIFramework; } else { //ensure we don't reset the property to.CompetencyFrameworkHierarchy = null; } //20-07-02 mp - just store the (index ready) competencies json, not the whole graph // - may stop saving this for now? if (!string.IsNullOrWhiteSpace(from.CompetencyFrameworkGraph)) { to.CompetencyFrameworkGraph = from.CompetencyFrameworkGraph; } else { //ensure we don't reset the graph } } //
/// <summary> /// Add/Update a CompetencyFramework /// </summary> /// <param name="entity"></param> /// <param name="messages"></param> /// <returns></returns> public bool Save(ThisEntity entity, ref SaveStatus status, bool addingActivity = false) { bool isValid = true; int count = 0; DBEntity efEntity = new DBEntity(); try { using (var context = new EntityContext()) { if (ValidateProfile(entity, ref status) == false) { return(false); } if (entity.Id == 0) { //add efEntity = new DBEntity(); MapToDB(entity, efEntity); if (IsValidDate(status.EnvelopeCreatedDate)) { efEntity.Created = status.LocalCreatedDate; } else { efEntity.Created = DateTime.Now; } // if (IsValidDate(status.EnvelopeUpdatedDate)) { efEntity.LastUpdated = status.LocalUpdatedDate; } else { efEntity.LastUpdated = DateTime.Now; } if (IsValidGuid(entity.RowId)) { efEntity.RowId = entity.RowId; } else { efEntity.RowId = Guid.NewGuid(); } context.CompetencyFramework.Add(efEntity); count = context.SaveChanges(); entity.Id = efEntity.Id; entity.RowId = efEntity.RowId; if (count == 0) { status.AddWarning(string.Format(" Unable to add Profile: {0} <br\\> ", string.IsNullOrWhiteSpace(entity.Name) ? "no description" : entity.Name)); } else { if (addingActivity) { //add log entry SiteActivity sa = new SiteActivity() { ActivityType = "CompetencyFramework", Activity = "Import", Event = "Add", Comment = string.Format("New Competency Framework was found by the import. Name: {0}, URI: {1}", entity.Name, entity.FrameworkUri), ActivityObjectId = entity.Id }; new ActivityManager().SiteActivityAdd(sa); } } } else { efEntity = context.CompetencyFramework.FirstOrDefault(s => s.Id == entity.Id); if (efEntity != null && efEntity.Id > 0) { entity.RowId = efEntity.RowId; //update MapToDB(entity, efEntity); //need to do the date check here, or may not be updated 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; } count = context.SaveChanges(); if (addingActivity) { //add log entry SiteActivity sa = new SiteActivity() { ActivityType = "CompetencyFramework", Activity = "Import", Event = "Update", Comment = string.Format("Updated Competency Framework found by the import. Name: {0}, URI: {1}", entity.Name, entity.FrameworkUri), ActivityObjectId = entity.Id }; new ActivityManager().SiteActivityAdd(sa); } } } } } } catch (Exception ex) { LoggingHelper.LogError(ex, "CompetencyFrameworkManager.Save()"); } return(isValid); }