} // public static void MapFromDB(DBEntity from, ThisEntity to) { to.Id = from.Id; to.RowId = from.RowId; to.EntityStateId = from.EntityStateId; to.FrameworkName = from.FrameworkName; to.CTID = from.CTID; to.OrganizationCTID = from.OrganizationCTID ?? ""; to.SourceUrl = from.SourceUrl; to.FrameworkUri = from.FrameworkUri; to.CredentialRegistryId = from.CredentialRegistryId ?? ""; //this should be replace by presence of CredentialRegistryId 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 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.EducationFramework .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.EducationFramework .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 bool ValidateProfile(ThisEntity profile, ref SaveStatus status) { status.HasSectionErrors = false; if (string.IsNullOrWhiteSpace(profile.FrameworkName)) { status.AddWarning("An educational framework name must be entered"); } //if we don't require url, we can't resolve potentially duplicate framework names return(!status.HasSectionErrors); }
} // public static void MapToDB(ThisEntity from, DBEntity to) { //want to ensure fields from create are not wiped //to.Id = from.Id; to.FrameworkName = from.FrameworkName; 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(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; } } //
} // public int Lookup_OR_Add(string frameworkUri, string frameworkName) { int frameworkId = 0; if (string.IsNullOrWhiteSpace(frameworkUri)) { return(0); } //*** no data for frameworkURL, just frameworkUri or sourceUrl ThisEntity entity = GetByUrl(frameworkUri); if (entity != null && entity.Id > 0) { return(entity.Id); } //skip if no name if (string.IsNullOrWhiteSpace(frameworkName)) { return(0); } SaveStatus status = new SaveStatus(); entity.FrameworkName = frameworkName; //this could an external Url, or a registry Uri if (frameworkUri.ToLower().IndexOf("credentialengineregistry.org/resources/") > -1 || frameworkUri.ToLower().IndexOf("credentialengineregistry.org/graph/") > -1) { entity.FrameworkUri = frameworkUri; } else { entity.SourceUrl = frameworkUri; } Save(entity, ref status); if (entity.Id > 0) { return(entity.Id); } return(frameworkId); } //
/// <summary> /// Check if the provided framework has already been sync'd. /// If not, it will be added. /// </summary> /// <param name="request"></param> /// <param name="userId"></param> /// <param name="messages"></param> /// <param name="frameworkId"></param> /// <returns></returns> //public bool HandleFrameworkRequest( CassFramework request, // int userId, // ref SaveStatus status, // ref int frameworkId ) //{ // bool isValid = true; // if ( request == null || string.IsNullOrWhiteSpace(request._IdAndVersion) ) // { // status.AddWarning( "The Cass Request doesn't contain a valid Cass Framework class." ); // return false; // } // ThisEntity item = Get( request._IdAndVersion ); // if (item != null && item.Id > 0) // { // //TODO - do we want to attempt an update - if changed // // - if we plan to implement a batch refresh of sync'd content, then not necessary // frameworkId = item.Id; // return true; // } // //add the framework... // ThisEntity entity = new ThisEntity(); // entity.Name = request.Name; // entity.Description = request.Description; // entity.FrameworkUrl = request.Url; // entity.RepositoryUri = request._IdAndVersion; // //TDO - need owning org - BUT, first person to reference a framework is not necessarily the owner!!!!! // //actually, we may not care here. Eventually get a ctid from CASS // //entity.OwningOrganizationId = 0; // isValid = Save( entity, userId, ref status ); // frameworkId = entity.Id; // return isValid; //} //actually not likely to have a separate list of frameworks //public bool SaveList( List<ThisEntity> list, ref SaveStatus status ) //{ // if ( list == null || list.Count == 0 ) // return true; // bool isAllValid = true; // foreach ( ThisEntity item in list ) // { // Save( item, ref status ); // } // return isAllValid; //} /// <summary> /// Add/Update a EducationFramework /// </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); efEntity.Created = efEntity.LastUpdated = DateTime.Now; if (IsValidGuid(entity.RowId)) { efEntity.RowId = entity.RowId; } else { efEntity.RowId = Guid.NewGuid(); } context.EducationFramework.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.FrameworkName) ? "no description" : entity.FrameworkName)); } 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.FrameworkName, entity.FrameworkUri), ActivityObjectId = entity.Id }; new ActivityManager().SiteActivityAdd(sa); } } } else { efEntity = context.EducationFramework.FirstOrDefault(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 = 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.FrameworkName, entity.FrameworkUri), ActivityObjectId = entity.Id }; new ActivityManager().SiteActivityAdd(sa); } } } } } }catch (Exception ex) { LoggingHelper.LogError(ex, "EducationFrameworkManager.Save()"); } return(isValid); }