public override void OnException(MethodExecutionArgs args)
 {
     var methodInfo = args.Method as MethodInfo;
     var erroDescription = new ErrorDescription(Constants.ERROR_PROCESSING_REQUEST, args.Exception.Message);
     var result = Activator.CreateInstance(methodInfo.ReturnType, erroDescription);
     args.ReturnValue = result;
     args.FlowBehavior = FlowBehavior.Return;
 }
        public override void OnEntry(MethodExecutionArgs args)
        {
            bool valid = true;
            StringBuilder errorMessage = new StringBuilder();
            foreach (var arg in args.Arguments)
            {
                valid = ((BaseRequest)arg).IsValid();
                if (!valid)
                    errorMessage.Append(((BaseRequest)arg).GetErrorMessages());
            }

            if (!valid)
            {
                var methodInfo = args.Method as MethodInfo;
                var erroDescription = new ErrorDescription(Constants.ERROR_VALIDATION_REQUEST, errorMessage.ToString());
                var result = Activator.CreateInstance(methodInfo.ReturnType, erroDescription);
                args.ReturnValue = result;
                args.FlowBehavior = FlowBehavior.Return;
            }
        }
        public Response.ServiceResult<ClientDTO> SaveClient(Request.Client.SaveClientRequest request)
        {
            Client client = null;

            using (var business = UnityManager.Container.Resolve<IClientBusiness>())
            {
                client = business.Save(Mapper.Map<ClientDTO, Client>(request.Client));
            }

            if (client != null)
            {
                var response = new Response.ServiceResult<ClientDTO>(Mapper.Map<Client, ClientDTO>(client));
                return response;
            }
            else
            {
                ErrorDescription error = new ErrorDescription(Constants.ERROR_PROCESSING_REQUEST, string.Format("Sorry, it was not possible to save the Client {0}.", request.Client.Name));
                var response = new Response.ServiceResult<ClientDTO>(error);
                return response;
            }
        }
        public Response.ServiceResult<ICollection<ScheduleDTO>> SearchSchedule(Request.Schedule.SearchScheduleRequest request)
        {
            IEnumerable<Schedule> schedules = null;

            using (var business = UnityManager.Container.Resolve<IScheduleBusiness>())
            {
                if (request.GetAll)
                    schedules = business.GetAll();
                else
                    schedules = business.GetByExample(Mapper.Map<ScheduleDTO, Schedule>(request.Schedule));
            }

            if (schedules == null)
            {
                ErrorDescription error = new ErrorDescription(Constants.ERROR_PROCESSING_REQUEST, "Sorry, it was not possible to search for Schedules.");
                return new Response.ServiceResult<ICollection<ScheduleDTO>>(error);
            }
            else
            {
                IList<ScheduleDTO> schedulesVO = new List<ScheduleDTO>();
                foreach (var s in schedules)
                {
                    schedulesVO.Add(Mapper.Map<Schedule, ScheduleDTO>(s));
                }
                return new Response.ServiceResult<ICollection<ScheduleDTO>>(schedulesVO);
            }
        }
        public Response.ServiceResult<ICollection<ExecutionLogDTO>> SearchExecutionLog(Request.ExecutionLog.SearchExecutionLogRequest request)
        {
            IEnumerable<ExecutionLog> logs = null;

            using (var business = UnityManager.Container.Resolve<IExecutionLogBusiness>())
            {
                logs = business.GetByIdAndPeriod(request.CommandId, request.StartDateTime, request.EndDateTime);
            }

            if (logs == null)
            {
                ErrorDescription error = new ErrorDescription(Constants.ERROR_PROCESSING_REQUEST, "Sorry, it was not possible to search for Execution Logs.");
                return new Response.ServiceResult<ICollection<ExecutionLogDTO>>(error);
            }
            else
            {
                IList<ExecutionLogDTO> logsVO = new List<ExecutionLogDTO>();
                foreach (var l in logs)
                {
                    logsVO.Add(Mapper.Map<ExecutionLog, ExecutionLogDTO>(l));
                }
                return new Response.ServiceResult<ICollection<ExecutionLogDTO>>(logsVO);
            }
        }
        public Response.ServiceResult<ICollection<CommandDTO>> SearchCommand(Request.Command.SearchCommandRequest request)
        {
            IEnumerable<Command> commands = null;

            using (var business = UnityManager.Container.Resolve<ICommandBusiness>())
            {
                if (request.GetAll)
                    commands = business.GetAll();
                else
                    commands = business.GetByExample(Mapper.Map<CommandDTO, Command>(request.Command));
            }

            if (commands == null)
            {
                ErrorDescription error = new ErrorDescription(Constants.ERROR_PROCESSING_REQUEST, "Sorry, it was not possible to search for Commands.");
                return new Response.ServiceResult<ICollection<CommandDTO>>(error);
            }
            else
            {
                IList<CommandDTO> commandsVO = new List<CommandDTO>();
                foreach (var c in commands)
                {
                    commandsVO.Add(Mapper.Map<Command, CommandDTO>(c));
                }
                return new Response.ServiceResult<ICollection<CommandDTO>>(commandsVO);
            }
        }
        public Response.ServiceResult<ScheduleDTO> SaveSchedule(Request.Schedule.SaveScheduleRequest request)
        {
            Schedule schedule = null;

            using (var business = UnityManager.Container.Resolve<IScheduleBusiness>())
            {
                schedule = business.Save(Mapper.Map<ScheduleDTO, Schedule>(request.Schedule));
            }

            if (schedule != null)
            {
                using (var business = UnityManager.Container.Resolve<ICommandBusiness>())
                {
                    schedule.Command = business.GetById(schedule.CommandId);
                    Engine.Schedule(schedule);
                }

                var response = new Response.ServiceResult<ScheduleDTO>(Mapper.Map<Schedule, ScheduleDTO>(schedule));
                return response;
            }
            else
            {
                ErrorDescription error = new ErrorDescription(Constants.ERROR_PROCESSING_REQUEST, "Sorry, it was not possible to save the Schedule.");
                var response = new Response.ServiceResult<ScheduleDTO>(error);
                return response;
            }
        }
        public Response.ServiceResult<CommandDTO> SaveCommand(Request.Command.SaveCommandRequest request)
        {
            Command command = null;

            using (var business = UnityManager.Container.Resolve<ICommandBusiness>())
            {
                command = business.Save(Mapper.Map<CommandDTO, Command>(request.Command));
            }

            if (command != null)
            {
                var response = new Response.ServiceResult<CommandDTO>(Mapper.Map<Command, CommandDTO>(command));
                return response;
            }
            else
            {
                ErrorDescription error = new ErrorDescription(Constants.ERROR_PROCESSING_REQUEST, string.Format("Sorry, it was not possible to save the Comman {0}.", request.Command.Name));
                var response = new Response.ServiceResult<CommandDTO>(error);
                return response;
            }
        }