public List <MappingResultModel> MapItems(List <GCItem> items)
        {
            var templates = MappingRepository.GetMappings();
            List <MappingResultModel> result = TryMapItems(items, templates);

            return(result);
        }
        public List <MappingModel> GetMappingModel()
        {
            var mappings = MappingRepository.GetMappings();

            var model = new List <MappingModel>();

            foreach (var templateMapping in mappings)
            {
                var mappingModel = new MappingModel
                {
                    GcProject = new GcProjectModel
                    {
                        Name = templateMapping.GcProjectName
                    },
                    GcTemplate = new GcTemplateModel
                    {
                        Id   = templateMapping.GcTemplate.GcTemplateId,
                        Name = templateMapping.GcTemplate.GcTemplateName,
                    },
                    CmsTemplate = new CmsTemplateModel
                    {
                        Name = templateMapping.CmsTemplate.TemplateName,
                    },
                    MappingId          = templateMapping.MappingId,
                    MappingTitle       = templateMapping.MappingTitle,
                    LastMappedDateTime = templateMapping.LastMappedDateTime,
                    LastUpdatedDate    = templateMapping.LastUpdatedDate,
                };
                model.Add(mappingModel);
            }

            foreach (var mapping in model)
            {
                try
                {
                    var template = GetGcTemplateEntity(mapping.GcTemplate.Id);
                    if (template == null)
                    {
                        mapping.LastUpdatedDate = "Removed from GatherContent";
                    }
                    else
                    {
                        var gcUpdateDate = ConvertMsecToDate((double)template.Data.Updated);
                        var dateFormat   = AccountSettings.DateFormat;
                        if (string.IsNullOrEmpty(dateFormat))
                        {
                            dateFormat = Constants.DateFormat;
                        }
                        mapping.LastUpdatedDate = gcUpdateDate.ToString(dateFormat);
                    }
                }
                catch (Exception)
                {
                    mapping.LastUpdatedDate = "Removed from GatherContent";
                }
            }

            return(model);
        }
Example #3
0
        /// <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)
            {
                var gcItem            = item.Key;   //gc item
                var 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;
                var        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
                };
                var cmsLink = ItemsRepository.GetCmsItemLink(HttpContext.Current.Request.Url.Host, cmsId);
                itemResponseModel.CmsLink = cmsLink;

                //MappingResultModel cmsItem;
                //TryMapItem(gcItem, gcTemplate, templates, out cmsItem);
                //result.Add(cmsItem);
                List <Element> gcFields = gcItem.Config.SelectMany(i => i.Elements).ToList();

                var templateMapping = templates.FirstOrDefault(x => x.GcTemplate.GcTemplateId == gcItem.TemplateId.ToString());
                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)
                        .Select(element => element.Type)
                        .Contains("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
                            });
                        }
                    }

                    bool fieldError = false;

                    var groupedFields = templateMapping.FieldMappings.GroupBy(i => i.CmsField);

                    foreach (var grouping in groupedFields)
                    {
                        CmsField cmsField = grouping.Key;

                        var gcFieldIds    = grouping.Select(i => i.GcField.Id);
                        var gcFieldsToMap = grouping.Select(i => i.GcField);

                        IEnumerable <Element> gcFieldsForMapping =
                            gcFields.Where(i => gcFieldIds.Contains(i.Name)).ToList();

                        var gcField = gcFieldsForMapping.FirstOrDefault();

                        if (gcField != null)
                        {
                            var value   = GetValue(gcFieldsForMapping);
                            var options = GetOptions(gcFieldsForMapping);

                            cmsField.Files   = files.Where(x => x.FieldId == gcField.Name).ToList();
                            cmsField.Value   = value;
                            cmsField.Options = options;

                            //update GC fields' type
                            foreach (var field in gcFieldsToMap)
                            {
                                field.Type = gcField.Type;
                            }
                        }
                        else
                        {
                            //if field error, set error message
                            itemResponseModel.ImportMessage      = "Update failed: Template fields mismatch";
                            itemResponseModel.IsImportSuccessful = false;
                            fieldError = true;
                            break;
                        }
                    }

                    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 fields = templateMapping.FieldMappings;
                        try
                        {
                            foreach (var field in fields)
                            {
                                if (field.GcField != null)
                                {
                                    switch (field.GcField.Type)
                                    {
                                    case "choice_radio":
                                    case "choice_checkbox":
                                    {
                                        ItemsRepository.MapChoice(cmsItem, field.CmsField);
                                    }
                                    break;

                                    case "files":
                                    {
                                        ItemsRepository.MapFile(cmsItem, field.CmsField);
                                    }
                                    break;

                                    default:
                                    {
                                        ItemsRepository.MapText(cmsItem, field.CmsField);
                                    }
                                    break;
                                    }
                                }
                            }
                        }
                        catch (KeyNotFoundException ex)
                        {
                            itemResponseModel.ImportMessage      = "Update failed: Some fields has been deleted from CMS Template";
                            itemResponseModel.IsImportSuccessful = false;
                            model.Add(itemResponseModel);
                            break;
                        }

                        //ItemsRepository.UpdateItem(new CmsItem
                        //{


                        //});
                    }
                }
                else
                {
                    //no template mapping, set error message
                    itemResponseModel.ImportMessage      = "Update failed: Template not mapped";
                    itemResponseModel.IsImportSuccessful = false;
                }
                model.Add(itemResponseModel);
            }

            return(model);
        }