// run once for every configured column
        private bool IsEveryColumnValid(Configuration configuration, ApiHttpClient apiClient)
        {
            var result = true;

            // ReSharper disable once LoopCanBeConvertedToQuery
            foreach (var c in configuration.Columns)
            {
                var column = ColumnFactory.GetColumn(c);
                result = column.IsColumnConfigurationValid(apiClient) && result;
            }
            return(result);
        }
        // run once for every type of column
        private bool IsEntireConfigurationValid(Configuration configuration)
        {
            var result = true;

            // ReSharper disable once LoopCanBeConvertedToQuery
            foreach (ColumnType c in Enum.GetValues(typeof(ColumnType)))
            {
                var column = ColumnFactory.GetColumn(new Configuration.Column {
                    Header = "Some_Header", Type = c
                });
                result = column.IsEntireConfigurationValid(configuration) && result;
            }
            return(result);
        }
        private bool AreValuesValid(Csv csv)
        {
            var result = true;

            foreach (var row in csv.Values)
            {
                foreach (var c in _configuration.Columns)
                {
                    var column = ColumnFactory.GetColumn(c);
                    result =
                        column.IsValueValid(row.ContainsKey(c.Header) ? row[c.Header] as string : null, _apiClient) &&
                        result;
                }
            }
            return(result);
        }
        public void RunImport(out bool hasErrors)
        {
            hasErrors = false;

            var apiClient = ApiClientFactory.GetClient(_configuration);

            var count = 0;
            var total = _csv.Values.Count();

            foreach (var row in _csv.Values)
            {
                ApplicantParameter applicantParameter;
                ApplicantResponse  applicantResponse;

                var idColumn            = _configuration.Columns.FirstOrDefault(x => x.Type == ColumnType.Id);
                int?existingApplicantId = null;
                if (idColumn != null && row.ContainsKey(idColumn.Header) && row[idColumn.Header].ToString().IsInt())
                {
                    existingApplicantId = row[idColumn.Header].ToString().GetInt();
                    Program.Write($"Updating applicant #{existingApplicantId} {++count}/{total} ...");

                    applicantResponse  = new ApplicantGetRequest(existingApplicantId.Value).LoadResult(apiClient);
                    applicantParameter = new ApplicantParameter(applicantResponse);
                }
                else
                {
                    Program.Write($"Creating applicant {++count}/{total} ...");
                    applicantParameter = new ApplicantParameter
                    {
                        FirstName = "First-Name",
                        LastName  = "Last-Name",
                        IsActive  = true
                    };
                }

                try
                {
                    foreach (var c in _configuration.Columns)
                    {
                        var column = ColumnFactory.GetColumn(c);
                        column.SetValueBeforeCreate(row.ContainsKey(c.Header) ? row[c.Header] as string : null,
                                                    applicantParameter, apiClient);
                    }

                    if (existingApplicantId.HasValue)
                    {
                        applicantResponse =
                            new ApplicantPostRequest(existingApplicantId.Value, applicantParameter).LoadResult(apiClient);
                        Program.Write(
                            $"Applicant '#{applicantResponse.Id} {applicantResponse.FirstName} {applicantResponse.LastName}' updated. Setting additional attributes ...");
                    }
                    else
                    {
                        applicantResponse =
                            new ApplicantPutRequest(applicantParameter, false, new Uri("http://does_not_matter"))
                            .LoadResult(apiClient);
                        Program.Write(
                            $"Applicant '#{applicantResponse.Id} {applicantResponse.FirstName} {applicantResponse.LastName}' created. Setting additional attributes ...");
                    }

                    foreach (var c in _configuration.Columns)
                    {
                        var column = ColumnFactory.GetColumn(c);
                        column.SetValueAfterCreate(row.ContainsKey(c.Header) ? row[c.Header] as string : null,
                                                   applicantResponse, apiClient);
                    }

                    Program.Write("");
                }
                catch (Exception ex)
                {
                    Program.WriteError("Unable to import applicant: " + ex.Message);
                    hasErrors = true;
                    return;
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="query"></param>
        /// <returns>Sorted list of ArticleItemProxies based on query</returns>
        public List <ArticleItemWrapper> RunSearch(VwbQuery query)
        {
            // var param = new ArticleSearchParam();
            //param.IncludeSidebarArticles();
            List <ArticleItemWrapper> articles;

            using (new Sitecore.SecurityModel.SecurityDisabler())
            {
                string searchPageId = Constants.VWBSearchPageId;
                string hostName     = Factory.GetSiteInfo("website")?.HostName ?? WebUtil.GetHostName();
                string url          = string.Format("{0}://{1}/api/informasearch?pId={2}&sortBy=plannedpublishdate&sortOrder=desc", HttpContext.Current.Request.Url.Scheme, hostName, searchPageId);

                if (query.InProgressValue)
                {
                    url += "&inprogress=1";
                }

                DateTime startDate = (query.StartDate != null)
                                                                                ? query.StartDate.Value
                                                                                : DateTime.Now.AddDays(-1).Date;//From the begining of the day  before

                DateTime endDate = (query.EndDate != null)
                                                ? query.EndDate.Value
                                                : DateTime.Now.Date.AddDays(31).AddSeconds(-1);//till the end of 30 days ahead

                url += "&plannedpublishdate=" + startDate.ToString("MM/dd/yyyy");
                url += ";" + endDate.ToString("MM/dd/yyyy");
                if (string.IsNullOrEmpty(query.PublicationCodes) == false)
                {
                    url += "&SearchPublicationTitle=" + query.PublicationCodes;
                }

                var client  = new WebClient();
                var content = client.DownloadString(url);

                var results = JsonConvert.DeserializeObject <SearchResults>(content);

                var resultItems = new List <ArticleItem>();

                Database masterDb = Factory.GetDatabase("master");

                foreach (var searchResult in results.results)
                {
                    var theItem = (ArticleItem)masterDb.GetItem(searchResult.ItemId);

                    if (theItem == null)
                    {
                        continue;
                    }

                    //Manually filtering for time
                    IArticle article = theItem.InnerItem.GlassCast <IArticle>(inferType: true);

                    if (article.Planned_Publish_Date.Ticks >= startDate.Ticks &&
                        article.Planned_Publish_Date.Ticks <= endDate.Ticks)
                    {
                        resultItems.Add(theItem);
                    }
                }

                articles = ArticleItemWrapper.GetArticleItemProxies(resultItems).ToList();
            }

            if (query.SortColumnKey != null && ColumnFactory.GetColumn(query.SortColumnKey) != null)
            {
                articles.Sort(ColumnFactory.GetColumn(query.SortColumnKey));
            }

            if (query.SortColumnKey == ColumnFactory.GetArticleNumberColumn().Key())
            {
                articles.Sort(ColumnFactory.GetArticleNumberColumn());
            }

            if (query.SortColumnKey == ColumnFactory.GetTitleColumn().Key())
            {
                articles.Sort(ColumnFactory.GetTitleColumn());
            }

            if (query.Descending)
            {
                articles.Reverse();
            }

            if (query.NumResultsValue != null)
            {
                return(articles.GetRange(0, Math.Min(articles.Count, (int)query.NumResultsValue)));
            }

            return(articles);
        }