/// <summary>
 /// Deleta uma entidade
 /// </summary>
 /// <param name="entity">Entidade</param>
 public void delete(ResponseForm entity)
 {
     try
     {
         if (entity != null)
             delete(entity.Id);
     }
     catch (DbEntityValidationException ex)
     {
         //Adiciona na lista de erros os erros de DataAnnotation
         addErrorMessage(ex);
     }
     catch (Exception ex)
     {
         Log.ErrorLog.saveError("Lib.Repositories.ResponseFormRepository.delete", ex);
         throw new Exception("Lib.Repositories.ResponseFormRepository.delete - " + ex.Message, ex);
     }
 }
        public ResponseForm CreateTestResponseForm()
        {
            BaseForm bf = CreateTestBaseForm();
            List<decimal?> scores = new List<decimal?>();
            scores.Add(0);
            scores.Add(0.25m);
            scores.Add(0.5m);
            scores.Add(0.75m);
            scores.Add(1m);
            scores.Add(null);

            Random r = new Random();

            ResponseForm rf = new ResponseForm();
            rf.Answers = new List<Answer>();
            rf.BaseFormId = bf.Id;
            rf.CityId = 1;
            rf.UserId = 1;
            bf.BaseBlocks.ForEach(bb =>
                {
                    bb.BaseSubBlocks.ForEach(bsb =>
                        {
                            bsb.BaseQuestions.ForEach(bq =>
                                {
                                    Answer a = new Answer();
                                    a.BaseQuestionId = bq.Id;
                                    a.Observation = Guid.NewGuid().ToString();
                                    a.Score = scores[r.Next(0, 5)];

                                    rf.Answers.Add(a);
                                });
                        });
                });

            _createdResponseForm.Add(rf);
            ResponseFormRepository.save(rf);

            return rf;
        }
        public void submitForm(ResponseForm entity, string observations)
        {
            //ver o log hisotrico
            Lib.Entities.Submit submit = new Submit();
            submit.Date = DateTime.Now;
            submit.Observation = observations;
            submit.ResponseForm = entity;
            submit.Status = (int)Enumerations.SubmitStatus.NotApproved;

            context.Submits.Add(submit);
        }
        /// <summary>
        /// Salva uma entidade
        /// </summary>
        /// <param name="entity">Entidade</param>
        public void save(ResponseForm entity, bool submitForm = true)
        {
            List<Commons.LogHistory> logs = null;
            Lib.Repositories.LogHistoryRepository log = new LogHistoryRepository(this.ActiveUser);

            try
            {
                context = new FDTContext();

                if (entity.Id == 0)
                {
                    if (this.ActiveUser.UserTypeEnum == Enumerations.UserType.Entity)
                    {
                        //Cria uma submissão com status submitted
                        if (submitForm)
                        {
                            if (entity.Submits == null)
                                entity.Submits = new List<Submit>();
                            entity.Submits.Add(new Submit()
                            {
                                Date = DateTime.Now,
                                Status = (int)Lib.Enumerations.SubmitStatus.Submitted,
                                Observation = String.Empty
                            });
                        }
                    }

                    context.ResponseForms.Add(entity);
                }
                else
                {
                    var entityAux = context.ResponseForms.Include("Answers").Include("Submits").Where(e => e.Id == entity.Id).FirstOrDefault();
                    entityAux.CityId = entity.CityId;

                    if (entityAux != null)
                    {
                        //Atualiza apenas as notas e observações das perguntas\
                        entityAux.Answers.ForEach(ans =>
                        {
                            var answerUpdated = entity.Answers.Where(an => an.Id == ans.Id).FirstOrDefault();
                            if (answerUpdated != null)
                            {
                                ans.Observation = answerUpdated.Observation;
                                ans.Score = answerUpdated.Score;
                            }
                        });
                    }

                    if (this.ActiveUser.UserTypeEnum == Enumerations.UserType.Entity)
                    {
                        //Cria uma submissão com status submitted
                        if (submitForm)
                        {
                            if (entityAux.Submits == null)
                                entityAux.Submits = new List<Submit>();
                            entityAux.Submits.Add(new Submit()
                            {
                                Date = DateTime.Now,
                                Status = (int)Lib.Enumerations.SubmitStatus.Submitted,
                                Observation = String.Empty
                            });
                        }
                    }

                    //Adiciona as novas questões se houverem
                    entityAux.Answers.AddRange(entity.Answers.Where(a => a.Id == 0).ToList());
                }

                context.SaveChanges();

                //refaz os cálculos e salva novamente.
                var rf = getInstanceById(entity.Id);
                rf.calculateTotalScore();

                context.SaveChanges();

                if (logs != null)
                {
                    log.saveLogs(logs);
                }
            }
            catch (DbEntityValidationException ex)
            {
                //Adiciona na lista de erros os erros de DataAnnotation
                addErrorMessage(ex);
            }
            catch (Exception ex)
            {
                Log.ErrorLog.saveError("Lib.Repositories.ResponseFormRepository.save", ex);
                throw new Exception("Lib.Repositories.ResponseFormRepository.save - " + ex.Message, ex);
            }
        }