Exemplo n.º 1
0
        public void Execute(IServiceProvider serviceProvider)
        {
            ITracingService             tracer  = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            IPluginExecutionContext     context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService        service = factory.CreateOrganizationService(context.UserId);

            try
            {
                EntityReference target = (EntityReference)context.InputParameters["Target"];

                tracer.Trace($"Target: {target.Id.ToString()}");

                JokeIdHelper jokeIdHelper = new JokeIdHelper();

                var webId = jokeIdHelper.GetWebIdByGuid(target.Id.ToString());

                if (string.IsNullOrEmpty(webId))
                {
                    throw new InvalidPluginExecutionException("Could not find joke webId");
                }

                tracer.Trace($"JokeWebId found: {webId}");

                var getJokeByIdTask = Task.Run(async() => await GetJokesById(tracer, webId));
                Task.WaitAll(getJokeByIdTask);

                var result = getJokeByIdTask.Result;

                tracer.Trace($"Joke found: {result.id}");

                Entity joke = JokeHelper.CreateJoke(tracer, result, jokeIdHelper);

                tracer.Trace($"Joke created: {joke.Id}");

                context.OutputParameters["BusinessEntity"] = joke;
            }
            catch (Exception e)
            {
                throw new InvalidPluginExecutionException(e.Message);
            }
        }
        public void Execute(IServiceProvider serviceProvider)
        {
            ITracingService             tracer  = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            IPluginExecutionContext     context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService        service = factory.CreateOrganizationService(context.UserId);

            //https://api.chucknorris.io/#!
            try
            {
                QueryExpression query = context.InputParameterOrDefault <QueryExpression>("Query");

                var visitor = new SearchVisitor();
                query.Accept(visitor);

                EntityCollection results = new EntityCollection();

                JokeIdHelper jokeIdHelper = new JokeIdHelper();

                if (string.IsNullOrEmpty(visitor.SearchKeyWord))
                {
                    tracer.Trace("Getting random joke");

                    var getRandomJokeTask = Task.Run(async() => await GetRandomJoke(tracer));
                    Task.WaitAll(getRandomJokeTask);

                    Result randomJoke = getRandomJokeTask.Result;

                    Entity joke = JokeHelper.CreateJoke(tracer, randomJoke, jokeIdHelper);
                    if (joke != null)
                    {
                        tracer.Trace($"Joke created: {joke.Id}");
                        results.Entities.Add(joke);
                    }
                }
                else
                {
                    tracer.Trace($"Searching jokes for: {visitor.SearchKeyWord}");

                    var getJokesByValueTask = Task.Run(async() => await GetJokesByValue(tracer, visitor.SearchKeyWord));
                    Task.WaitAll(getJokesByValueTask);

                    Jokes jokes = getJokesByValueTask.Result;

                    tracer.Trace($"Found {jokes.total} jokes");

                    foreach (Result result in jokes.result)
                    {
                        Entity joke = JokeHelper.CreateJoke(tracer, result, jokeIdHelper);
                        if (joke == null)
                        {
                            continue;
                        }

                        tracer.Trace($"Joke created: {joke.GetAttributeValue<Guid>("lat_chucknorrisjokeid")}");

                        results.Entities.Add(joke);
                    }
                }

                context.OutputParameters["BusinessEntityCollection"] = results;
            }
            catch (Exception e)
            {
                tracer.Trace($"{e.Message} {e.StackTrace}");
                if (e.InnerException != null)
                {
                    tracer.Trace($"{e.InnerException.Message} {e.InnerException.StackTrace}");
                }

                throw new InvalidPluginExecutionException(e.Message);
            }
        }