Esempio n. 1
0
        public static ThisEntity Get(int parentId, int assessmentId)
        {
            ThisEntity entity = new ThisEntity();

            if (parentId < 1 || assessmentId < 1)
            {
                return(entity);
            }
            try
            {
                using (var context = new EntityContext())
                {
                    EM.Entity_Assessment from = context.Entity_Assessment
                                                .SingleOrDefault(s => s.AssessmentId == assessmentId && s.EntityId == parentId);

                    if (from != null && from.Id > 0)
                    {
                        entity.Id                 = from.Id;
                        entity.AssessmentId       = from.AssessmentId;
                        entity.EntityId           = from.EntityId;
                        entity.RelationshipTypeId = from.RelationshipTypeId;
                        entity.ProfileSummary     = from.Assessment.Name;
                        //to.Credential = from.Credential;
                        entity.Assessment = new AssessmentProfile();
                        AssessmentManager.MapFromDB_Basic(from.Assessment, entity.Assessment,
                                                          false       //includeCosts - propose to use for credential editor
                                                          );

                        if (IsValidDate(from.Created))
                        {
                            entity.Created = ( DateTime )from.Created;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LoggingHelper.LogError(ex, thisClassName + ".Get");
            }
            return(entity);
        }        //
Esempio n. 2
0
        /// <summary>
        /// Add an Entity assessment
        /// </summary>
        /// <param name="parentUid"></param>
        /// <param name="assessmentId"></param>
        /// <param name="relationshipTypeId"></param>
        /// <param name="allowMultiples">If false, check if an assessment exists. If found, do an update</param>
        /// <param name="messages"></param>
        /// <returns></returns>
        public int Add(Guid parentUid,
                       int assessmentId, int relationshipTypeId,
                       bool allowMultiples,
                       ref SaveStatus status)
        {
            int id    = 0;
            int count = 0;

            if (assessmentId == 0)
            {
                status.AddError(string.Format("A valid Assessment identifier was not provided to the {0}.EntityAssessment_Add method.", thisClassName));
                return(0);
            }
            if (relationshipTypeId == 0)
            {
                relationshipTypeId = 1;
            }

            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
                {
                    //first check for duplicates
                    efEntity = context.Entity_Assessment
                               .FirstOrDefault(s => s.EntityId == parent.Id && s.AssessmentId == assessmentId && s.RelationshipTypeId == relationshipTypeId);

                    if (efEntity != null && efEntity.Id > 0)
                    {
                        //status.AddError( string.Format( "Error - this Assessment has already been added to this profile.", thisClassName ) );
                        return(0);
                    }

                    if (allowMultiples == false)
                    {
                        //check if one exists, and replace if found
                        //efEntity = context.Entity_Assessment
                        //	.FirstOrDefault( s => s.EntityId == parent.Id  );
                        //if ( efEntity != null && efEntity.Id > 0 )
                        //{
                        //	efEntity.AssessmentId = assessmentId;

                        //	count = context.SaveChanges();
                        //	return efEntity.Id;
                        //}
                    }
                    efEntity                    = new DBEntity();
                    efEntity.EntityId           = parent.Id;
                    efEntity.AssessmentId       = assessmentId;
                    efEntity.RelationshipTypeId = relationshipTypeId > 0 ? relationshipTypeId : 1;
                    efEntity.Created            = System.DateTime.Now;

                    context.Entity_Assessment.Add(efEntity);

                    // submit the change to database
                    count = context.SaveChanges();
                    if (count > 0)
                    {
                        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 a Assessment for a profile. The process appeared to not work, but there was no exception, so we have no message, or no clue. Parent Profile: {0}, Type: {1}, assessmentId: {2}", parentUid, parent.EntityType, assessmentId);
                        EmailManager.NotifyAdmin(thisClassName + ".Add Failed", message);
                    }
                }
                catch (System.Data.Entity.Validation.DbEntityValidationException dbex)
                {
                    string message = HandleDBValidationError(dbex, thisClassName + ".Add() ", "Entity_Assessment");
                    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);
        }