public override List <HistoricalEvaluationDto> ExecuteWithResult()
        {
            IQueryable <EmployeeEvaluationHistory_Search.Projection> query = RavenSession
                                                                             .Query <EmployeeEvaluationHistory_Search.Projection, EmployeeEvaluationHistory_Search>()
                                                                             .Where(e => e.UserName == _userName);

            var employeesProjection = query.ToList()
                                      .Where(x => x.Period != null && 0 >= string.Compare(x.Period, _lastPeriod))
                                      .OrderByDescending(x => x.Period)
                                      .ToList();
            var mapper = new EmployeeEvaluationHelper(RavenSession, _userName);

            return(employeesProjection.Select(e =>
                                              new HistoricalEvaluationDto()
            {
                AverageCalification = e.Califications,
                ResponsibleId = e.ResponsibleId,
                FullName = e.FullName,
                UserName = e.UserName,
                Period = e.Period,
                Evaluators = e.Evaluators != null ? e.Evaluators.ToList() : new List <string>(),
                State = EvaluationStateHelper.GetEvaluationState(e.AutoEvaluationDone, e.ResponsibleEvaluationDone, e.CompanyEvaluationDone, e.OpenToDevolution, e.Finished),
                Id = e.Id,
            }).ToList());
        }
        public override CalificationsDto ExecuteWithResult()
        {
            var evId             = EmployeeEvaluation.GenerateEvaluationId(_period, _evaluatedUser);
            var evaluation       = RavenSession.Load <EmployeeEvaluation>(evId);
            var evaluationHelper = new EmployeeEvaluationHelper(RavenSession, _loggedUser);

            if (evaluation == null)
            {
                throw new ApplicationException(string.Format("Error: Evaluación inexistente: {0}.", evId));
            }

            var isVisitor = (evaluation.SharedLinks == null ? false : evaluation.SharedLinks.Any(x => x.SharedCode == _sharedCode && x.ExpirationDate > DateTime.UtcNow)) || //has SharedCode
                            (string.Compare(evaluationHelper.GetLastPeriodForResponisble(_evaluatedUser), _period) > 0); //is newer Responsible

            Employee_Search.Projection employee = RavenSession
                                                  .Query <Employee_Search.Projection, Employee_Search>()
                                                  .Where(x => x.IsActive && x.UserName == evaluation.UserName).FirstOrDefault();

            var califications = RavenSession.Advanced.LoadStartingWith <EvaluationCalification>(evId + "/").ToList();

            var evaluators                = califications.Where(c => c.Owner == CalificationType.Evaluator).Select(c => c.EvaluatorEmployee).ToList();
            var autoEvaluationDone        = califications.Any(c => c.Owner == CalificationType.Auto && c.Finished);
            var responsibleEvaluationDone = califications.Any(c => c.Owner == CalificationType.Responsible && c.Finished);
            var companyEvaluationDone     = califications.Any(c => c.Owner == CalificationType.Company && c.Finished);
            var state = EvaluationStateHelper.GetEvaluationState(autoEvaluationDone, responsibleEvaluationDone, companyEvaluationDone, evaluation.ReadyForDevolution, evaluation.Finished);

            var evaluationDto = CalificationsEvaluationDto.Create(evaluation, evaluators, evaluation.CurrentPosition ?? employee.CurrentPosition, evaluation.Seniority ?? employee.Seniority, companyEvaluationDone, state);

            if (!CanViewEvaluation(evaluationDto, califications, isVisitor))
            {
                throw new ApplicationException(string.Format("Error: Usuario {0} no tiene permiso para ver la evaluación {1}", _loggedUser, evId));
            }

            // If evaluation is finished
            // - Show ALWAYS company & auto

            if (evaluation.Finished)
            {
                return(GetFinishedEvaluation(evaluationDto, califications));
            }

            // If loggedUser is the evaluated
            // - Show auto-eval if not RFD
            // - Show auto-eval & company if RFD

            if (_loggedUser == _evaluatedUser)
            {
                return(GetAutoEvaluation(evaluationDto, califications));
            }

            // If loggedUser is responsible
            // - Show all

            if (_loggedUser == evaluationDto.ResponsibleId)
            {
                return(GetFullEvaluation(evaluationDto, califications));
            }

            // If loggedUser is evaluator
            // - Show evluator eval

            return(GetEvaluatorEvaluation(evaluationDto, califications));
        }