/// <summary> /// /// </summary> /// <param name="itemId"></param> /// <param name="models"></param> /// <param name="language"></param> /// <returns></returns> public List <ItemResultModel> UpdateItems(string itemId, List <UpdateListIds> models, string language) { var model = new List <ItemResultModel>(); var gcItems = new Dictionary <GCItem, string>(); foreach (var item in models) { GCItem gcItem = ItemsService.GetSingleItem(item.GCId).Data; gcItems.Add(gcItem, item.CMSId); } //var templates = MappingRepository.GetMappings(); var templatesDictionary = new Dictionary <int, GCTemplate>(); foreach (var item in gcItems) { GCItem gcItem = item.Key; //gc item string cmsId = item.Value; // corresponding cms id var itemResponseModel = new ItemResultModel { IsImportSuccessful = true, ImportMessage = "Update Successful" }; if (!string.IsNullOrEmpty(GcAccountSettings.GatherContentUrl)) { itemResponseModel.GcLink = string.Concat(GcAccountSettings.GatherContentUrl, "/item/", gcItem.Id); } itemResponseModel.GcItem = new GcItemModel { Id = gcItem.Id.ToString(), Title = gcItem.Name }; itemResponseModel.Status = new GcStatusModel { Color = gcItem.Status.Data.Color, Name = gcItem.Status.Data.Name, }; GCTemplate gcTemplate; int templateId = gcItem.TemplateId.Value; templatesDictionary.TryGetValue(templateId, out gcTemplate); if (gcTemplate == null) { gcTemplate = TemplatesService.GetSingleTemplate(templateId.ToString()).Data; templatesDictionary.Add(templateId, gcTemplate); } itemResponseModel.GcTemplate = new GcTemplateModel { Id = gcTemplate.Id.ToString(), Name = gcTemplate.Name }; string cmsLink = ItemsRepository.GetCmsItemLink(HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Host, cmsId); itemResponseModel.CmsLink = cmsLink; itemResponseModel.CmsId = cmsId; //MappingResultModel cmsItem; //TryMapItem(gcItem, gcTemplate, templates, out cmsItem); //result.Add(cmsItem); List <Element> gcFields = gcItem.Config.SelectMany(i => i.Elements).ToList(); //var templateMapping = templates.First(x => x.GcTemplate.GcTemplateId == gcItem.TemplateId.ToString()); var templateMapping = MappingRepository.GetMappingByItemId(cmsId, language); if (templateMapping != null) // template found, now map fields here { var gcContentIdField = templateMapping.FieldMappings.FirstOrDefault(fieldMapping => fieldMapping.CmsField.TemplateField.FieldName == "GC Content Id"); if (gcContentIdField != null) { templateMapping.FieldMappings.Remove(gcContentIdField); } var files = new List <File>(); if (gcItem.Config.SelectMany(config => config.Elements).Any(element => element.Type == "files")) { foreach (var file in ItemsService.GetItemFiles(gcItem.Id.ToString()).Data) { files.Add(new File { FileName = file.FileName, Url = file.Url, FieldId = file.Field, UpdatedDate = file.Updated, FileId = file.Id }); } } bool fieldError = CheckFieldError(templateMapping, gcFields, files, itemResponseModel); if (!fieldError) { var cmsContentIdField = new FieldMapping { CmsField = new CmsField { TemplateField = new CmsTemplateField { FieldName = "GC Content Id" }, Value = gcItem.Id.ToString() } }; templateMapping.FieldMappings.Add(cmsContentIdField); var cmsItem = new CmsItem { Template = templateMapping.CmsTemplate, Title = gcItem.Name, Fields = templateMapping.FieldMappings.Select(x => x.CmsField).ToList(), Language = language, Id = cmsId }; var fieldMappings = templateMapping.FieldMappings; // one CMS text field can be mapped to several GC fields // in this case we concatenate their texts and put into one CMS field foreach (IGrouping <string, FieldMapping> fields in fieldMappings.GroupBy(f => f.CmsField.TemplateField.FieldName)) { FieldMapping field = fields.First(); if (field.GcField != null) { switch (field.GcField.Type) { case "choice_radio": case "choice_checkbox": { ItemsRepository.MapChoice(cmsItem, field.CmsField); } break; case "files": { ItemsRepository.ResolveAttachmentMapping(cmsItem, field.CmsField); } break; default: { if (field.CmsField.TemplateField.FieldType == "Datetime" || field.CmsField.TemplateField.FieldType == "Date") { ItemsRepository.MapDateTime(cmsItem, field.CmsField); } else { if (fields.Count() > 1) { field.CmsField.Value = string.Join("\r\n", fields.Select(f => f.CmsField.Value.ToString())); } ItemsRepository.MapText(cmsItem, field.CmsField); } } break; } } } var cmsSyncDateField = new CmsField { TemplateField = new CmsTemplateField { FieldName = "Last Sync Date" }, Value = DateTime.UtcNow.ToString("yyyyMMddTHHmmss") }; ItemsRepository.MapText(cmsItem, cmsSyncDateField); } } else { //no template mapping, set error message itemResponseModel.ImportMessage = "Update failed: Template not mapped"; itemResponseModel.IsImportSuccessful = false; } model.Add(itemResponseModel); } return(model); }