} // //public static List<CredentialAlignmentObjectItem> GetAllAsAlignmentObjects( Guid parentUid, string alignmentType ) //{ // //ThisEntity entity = new ThisEntity(); // List<CredentialAlignmentObjectItem> list = new List<CredentialAlignmentObjectItem>(); // CredentialAlignmentObjectItem entity = new CredentialAlignmentObjectItem(); // Entity parent = EntityManager.GetEntity( parentUid ); // if ( parent == null || parent.Id == 0 ) // { // return list; // } // try // { // using ( var context = new EntityContext() ) // { // List<DBEntity> results = context.Entity_Competency // .Where( s => s.EntityId == parent.Id // ) // .OrderBy( s => s.EducationFramework_Competency.EducationFramework.Name ) // .ThenBy( s => s.EducationFramework_Competency.Name ) // .ToList(); // //&& ( alignmentType == "" || s.AlignmentType == alignmentType ) // if ( results != null && results.Count > 0 ) // { // foreach ( DBEntity item in results ) // { // entity = new CredentialAlignmentObjectItem(); // ToMapAsAlignmentObjects( item, entity ); // list.Add( entity ); // } // } // } // } // catch ( Exception ex ) // { // LoggingHelper.LogError( ex, thisClassName + ".GetAllAsAlignmentObjects" ); // } // return list; //}// /// <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.Entity_Competency // .SingleOrDefault( s => s.Id == profileId ); // if ( item != null && item.Id > 0 ) // { // MapFromDB( item, entity ); // } // } // } // catch ( Exception ex ) // { // LoggingHelper.LogError( ex, thisClassName + ".Get" ); // } // return entity; //}// /// <summary> /// Get entity to determine if one exists for the entity and alignment type /// </summary> /// <param name="entityId"></param> /// <param name="targetNodeName"></param> /// <returns></returns> public static ThisEntity Get(int entityId, string targetNodeName) { ThisEntity entity = new ThisEntity(); if (string.IsNullOrWhiteSpace(targetNodeName)) { return(entity); } try { using (var context = new EntityContext()) { DBEntity item = context.Entity_Competency .FirstOrDefault(s => s.EntityId == entityId && s.TargetNodeName == targetNodeName); if (item != null && item.Id > 0) { MapFromDB(item, entity); } } } catch (Exception ex) { LoggingHelper.LogError(ex, thisClassName + ".Get"); } return(entity); } //
} // public static void MapFromDB(DBEntity from, ThisEntity to) { to.Id = from.Id; to.EntityId = from.EntityId; to.EducationFrameworkId = from.EducationFrameworkId ?? 0; to.TargetNodeName = from.TargetNodeName; to.TargetNodeDescription = from.TargetNodeDescription; if (to.TargetNodeDescription == to.TargetNodeName) { to.TargetNodeDescription = ""; } to.TargetNode = from.TargetNode; to.CodedNotation = from.CodedNotation; to.Weight = from.Weight ?? 0; //to.AlignmentTypeId = from.AlignmentTypeId ?? 0; //to.AlignmentType = from.AlignmentType; if (IsValidDate(from.Created)) { to.Created = ( DateTime )from.Created; } if (from.EducationFramework != null && from.EducationFramework.FrameworkName != null) { to.FrameworkName = from.EducationFramework.FrameworkName; } } //
/// <summary> /// Delete a competency /// </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.Entity_Competency.FirstOrDefault(s => s.Id == recordId); if (p != null && p.Id > 0) { context.Entity_Competency.Remove(p); int count = context.SaveChanges(); } else { statusMessage = string.Format("Entity Competency record was not found: {0}", recordId); isOK = false; } } return(isOK); }
} // public static void MapFromDB(DBEntity from, CredentialAlignmentObjectItem to) { to.Id = from.Id; to.ParentId = from.EntityId; to.TargetNode = from.TargetNode; to.TargetNodeDescription = from.TargetNodeDescription; to.TargetNodeName = from.TargetNodeName; if (to.TargetNodeDescription == to.TargetNodeName) { to.TargetNodeDescription = ""; } to.Weight = (from.Weight ?? 0M); to.CodedNotation = from.CodedNotation; if (IsValidDate(from.Created)) { to.Created = ( DateTime )from.Created; } }
/// <summary> /// Add a competency /// </summary> /// <param name="entity"></param> /// <param name="parentUid"></param> /// <param name="messages"></param> /// <returns></returns> public bool Save(ThisEntity entity, Entity parent, ref SaveStatus status) { bool isValid = true; int count = 0; DBEntity efEntity = new DBEntity(); using (var context = new EntityContext()) { if (ValidateProfile(entity, ref status) == false) { return(false); } if (entity.Id == 0) { //check if already exists //TODO - will need to add alignment type ThisEntity item = Get(parent.Id, entity.TargetNodeName); if (entity != null && entity.Id > 0) { status.AddWarning(thisClassName + string.Format("Save(). Error: the selected competency {0} already exists!", entity.TargetNodeName)); return(false); } //add efEntity = new DBEntity(); efEntity.EntityId = parent.Id; if (entity.EducationFrameworkId > 0) { efEntity.EducationFrameworkId = entity.EducationFrameworkId; } else { efEntity.EducationFrameworkId = null; } efEntity.FrameworkName = entity.FrameworkName; efEntity.FrameworkUrl = entity.FrameworkUrl; efEntity.TargetNodeName = entity.TargetNodeName; efEntity.TargetNodeDescription = entity.TargetNodeDescription; efEntity.TargetNode = entity.TargetNode; efEntity.CodedNotation = entity.CodedNotation; efEntity.Weight = entity.Weight; efEntity.Created = DateTime.Now; context.Entity_Competency.Add(efEntity); count = context.SaveChanges(); //update profile record so doesn't get deleted entity.Id = efEntity.Id; if (count == 0) { status.AddWarning(string.Format(" Unable to add Competency: {0} <br\\> ", string.IsNullOrWhiteSpace(entity.TargetNodeName) ? "no description" : entity.TargetNodeName)); } } else { //no update possible at this time entity.EntityId = parent.Id; efEntity = context.Entity_Competency.SingleOrDefault(s => s.Id == entity.Id); if (efEntity != null && efEntity.Id > 0) { //update if (entity.EducationFrameworkId > 0) { efEntity.EducationFrameworkId = entity.EducationFrameworkId; } else { efEntity.EducationFrameworkId = null; } efEntity.FrameworkName = entity.FrameworkName; efEntity.FrameworkUrl = entity.FrameworkUrl; efEntity.TargetNodeName = entity.TargetNodeName; efEntity.TargetNodeDescription = entity.TargetNodeDescription; efEntity.TargetNode = entity.TargetNode; efEntity.CodedNotation = entity.CodedNotation; efEntity.Weight = entity.Weight; //has changed? if (HasStateChanged(context)) { count = context.SaveChanges(); } } } } return(isValid); }