private GCTemplate GetTemplate(int templateId, Dictionary <int, GCTemplate> templatesDictionary)
        {
            GCTemplate gcTemplate;

            templatesDictionary.TryGetValue(templateId, out gcTemplate);
            if (gcTemplate == null)
            {
                gcTemplate = TemplatesService.GetSingleTemplate(templateId.ToString()).Data;
                templatesDictionary.Add(templateId, gcTemplate);
            }

            return(gcTemplate);
        }
        /// <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);
        }
        private List <ItemResultModel> Import(string itemId, List <ImportItemModel> items, string projectId, string statusId, string language)
        {
            var model = new List <ItemResultModel>();

            //get all paths
            var fullGcPaths          = GetItemsMap(projectId, items.Select(x => x.Id));
            var pathItemsToBeRemoved = new List <int>();

            Dictionary <string, List <ItemEntity> > shortPaths = new Dictionary <string, List <ItemEntity> >();

            if (fullGcPaths.Count() > 1)
            {
                var firstPath = fullGcPaths.First();
                foreach (var path in firstPath.Value)
                {
                    //if all paths start with same item and this item is not selected
                    if (fullGcPaths.Select(x => x.Value).All(x => x.First().Data.Id == path.Data.Id) && !items.Select(x => x.Id).Contains(path.Data.Id.ToString()))
                    {
                        pathItemsToBeRemoved.Add(path.Data.Id);
                    }
                }
            }

            foreach (var item in fullGcPaths)
            {
                List <ItemEntity> itemsToAdd = new List <ItemEntity>();

                foreach (var gcPathItem in item.Value)
                {
                    if (!pathItemsToBeRemoved.Contains(gcPathItem.Data.Id))
                    {
                        itemsToAdd.Add(gcPathItem);
                    }
                }
                shortPaths.Add(item.Key, itemsToAdd);
            }

            var sorted = shortPaths.OrderBy(x => x.Value.Count).ThenBy(x => x.Value.First().Data.Name);            //sort to start from shortest and alphabetically asc

            foreach (var path in sorted)
            {
                var itemResponseModel = new ItemResultModel
                {
                    IsImportSuccessful = true,
                    ImportMessage      = "Import Successful"
                };

                var gcItem = path.Value.Last();                                            //this is the item we selected to import
                var item   = items.FirstOrDefault(x => x.Id == gcItem.Data.Id.ToString()); //item coming from UI; selected mapping and other info

                if (gcItem != null && gcItem.Data != null && gcItem.Data.TemplateId != null)
                {
                    if (!string.IsNullOrEmpty(GcAccountSettings.GatherContentUrl))
                    {
                        itemResponseModel.GcLink = string.Concat(GcAccountSettings.GatherContentUrl, "/item/", gcItem.Data.Id);
                    }
                    itemResponseModel.GcItem = new GcItemModel
                    {
                        Id    = gcItem.Data.Id.ToString(),
                        Title = gcItem.Data.Name
                    };

                    itemResponseModel.Status = new GcStatusModel
                    {
                        Color = gcItem.Data.Status.Data.Color,
                        Name  = gcItem.Data.Status.Data.Name,
                    };

                    var gcTemplate = TemplatesService.GetSingleTemplate(gcItem.Data.TemplateId.ToString());
                    itemResponseModel.GcTemplate = new GcTemplateModel
                    {
                        Id   = gcTemplate.Data.Id.ToString(),
                        Name = gcTemplate.Data.Name
                    };

                    //element that corresponds to item in CMS that holds mappings
                    TemplateMapping templateMapping = MappingRepository.GetMappingById(item.SelectedMappingId);

                    List <Element> gcFields = gcItem.Data.Config.SelectMany(i => i.Elements).ToList();

                    if (templateMapping != null)                     // template found, now map fields here
                    {
                        var files = new List <File>();
                        if (gcItem.Data.Config.SelectMany(config => config.Elements).Select(element => element.Type).Contains("files"))
                        {
                            foreach (var file in ItemsService.GetItemFiles(gcItem.Data.Id.ToString()).Data)
                            {
                                files.Add(new File
                                {
                                    FileName    = file.FileName,
                                    Url         = file.Url,
                                    FieldId     = file.Field,
                                    UpdatedDate = file.Updated
                                });
                            }
                        }

                        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.Data.Id.ToString()
                                }
                            };
                            templateMapping.FieldMappings.Add(cmsContentIdField);

                            var cmsItem = new CmsItem
                            {
                                Template = templateMapping.CmsTemplate,
                                Title    = gcItem.Data.Name,
                                Fields   = templateMapping.FieldMappings.Select(x => x.CmsField).ToList(),
                                Language = language
                            };

                            var gcPath = string.Join("/", path.Value.Select(x => x.Data.Name));

                            var parentId = itemId;

                            bool alreadyMappedItemInPath = false;
                            //for each mapping which is fact GC Item => Sitecore/Umbraco item - get GC Path and run through its each item
                            for (int i = 0; i < path.Value.Count; i++)
                            {
                                //for each path item check if it exists already in CMS and if yes - skip; otherwise - add not mapped item
                                if (i == path.Value.Count - 1)
                                {
                                    //if we at the last item in the path - import mapped item
                                    if (ItemsRepository.IfMappedItemExists(parentId, cmsItem, templateMapping.MappingId, gcPath))
                                    {
                                        cmsItem.Id = ItemsRepository.AddNewVersion(parentId, cmsItem, templateMapping.MappingId, gcPath);
                                    }
                                    else
                                    {
                                        cmsItem.Id = ItemsRepository.CreateMappedItem(parentId, cmsItem, templateMapping.MappingId, gcPath);
                                    }
                                    parentId = cmsItem.Id;

                                    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))
                                    {
                                        var fieldMappingsAdditionals = fields
                                                                       .Where(f => f.CmsFieldAdditionalMapping != null &&
                                                                              f.CmsFieldAdditionalMapping.Any()).ToList()
                                                                       .SelectMany(f => f.CmsFieldAdditionalMapping).ToList();

                                        FieldMapping field = fields.First();
                                        if (field.GcField != null)
                                        {
                                            foreach (var fieldMappingsAdditional in fieldMappingsAdditionals)
                                            {
                                                fieldMappingsAdditional.Value   = field.CmsField.Value;
                                                fieldMappingsAdditional.Files   = field.CmsField.Files;
                                                fieldMappingsAdditional.Options = field.CmsField.Options;
                                            }

                                            switch (field.GcField.Type)
                                            {
                                            case "choice_radio":
                                            case "choice_checkbox":
                                            {
                                                ItemsRepository.MapChoice(cmsItem, field.CmsField);
                                                foreach (var fieldMappingsAdditional in fieldMappingsAdditionals)
                                                {
                                                    ItemsRepository.MapChoice(cmsItem, fieldMappingsAdditional);
                                                }
                                            }
                                            break;

                                            case "files":
                                            {
                                                ItemsRepository.ResolveAttachmentMapping(cmsItem, field.CmsField);
                                                foreach (var fieldMappingsAdditional in fieldMappingsAdditionals)
                                                {
                                                    ItemsRepository.ResolveAttachmentMapping(cmsItem, fieldMappingsAdditional);
                                                }
                                            }
                                            break;

                                            default:
                                            {
                                                if (field.CmsField.TemplateField.FieldType == "Datetime" || field.CmsField.TemplateField.FieldType == "Date")
                                                {
                                                    ItemsRepository.MapDateTime(cmsItem, field.CmsField);
                                                    foreach (var fieldMappingsAdditional in fieldMappingsAdditionals)
                                                    {
                                                        ItemsRepository.MapDateTime(cmsItem, fieldMappingsAdditional);
                                                    }
                                                }
                                                else
                                                {
                                                    if (fields.Count() > 1)
                                                    {
                                                        var newValue = string.Join("\r\n", fields.Select(f => f.CmsField.Value.ToString()));
                                                        field.CmsField.Value = newValue;
                                                        fieldMappingsAdditionals.ForEach(f => f.Value = newValue);
                                                    }

                                                    ItemsRepository.MapText(cmsItem, field.CmsField);
                                                    foreach (var fieldMappingsAdditional in fieldMappingsAdditionals)
                                                    {
                                                        ItemsRepository.MapText(cmsItem, fieldMappingsAdditional);
                                                    }
                                                }
                                            }
                                            break;
                                            }
                                        }
                                    }

                                    //Publish Parent Image Folder with SubItems?

                                    string parentMediaFolder = SimpleMediaRepository.publishParentMediaFolder;
                                    ItemsRepository.PublishItems(parentMediaFolder, isMediaFolder: true);
                                    ItemsRepository.PublishItems(cmsItem.Id, isContentItem: true);


                                    //set CMS link after we got out CMS Id
                                    var cmsLink = ItemsRepository.GetCmsItemLink(HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Host, cmsItem.Id);
                                    itemResponseModel.CmsLink = cmsLink;
                                    itemResponseModel.CmsId   = cmsItem.Id;

                                    if (!string.IsNullOrEmpty(statusId))
                                    {
                                        var status = PostNewItemStatus(gcItem.Data.Id.ToString(), statusId, projectId);
                                        itemResponseModel.Status.Color = status.Color;
                                        itemResponseModel.Status.Name  = status.Name;
                                    }
                                }
                                else
                                {
                                    var currentCmsItem = new CmsItem
                                    {
                                        Title    = path.Value[i].Data.Name,
                                        Language = language
                                    };
                                    //if we are not at the selected item, somewhere in the middle
                                    //1. если замапленный айтем существует (такое же название и такой же gc path?), то тогда выставляем alreadyMappedItemInPath = тру
                                    //и пропускаем всякое создание, сеттим только парент id
                                    //2. иначе если есть незамапленный айтем:
                                    // - alreadyMappedItemInPath == true = - скипуем создание, выставляем его как парент айди
                                    // - alreadyMappedItemInPath == false, - скипуем всё, парент айди не меняем
                                    //3. айтема никакого нет
                                    // - alreadyMappedItemInPath == true = - создаём незамапленный айтем, выставляем его как парент айди
                                    // - alreadyMappedItemInPath == false, - скипуем всё, парент айди не меняем
                                    if (ItemsRepository.IfMappedItemExists(parentId, currentCmsItem))
                                    {
                                        //cmsItem.Id = ItemsRepository.CreateNotMappedItem(parentId, notMappedCmsItem);
                                        //parentId = cmsItem.Id;
                                        alreadyMappedItemInPath = true;
                                        parentId = ItemsRepository.GetItemId(parentId, currentCmsItem);
                                    }
                                    else if (ItemsRepository.IfNotMappedItemExists(parentId, currentCmsItem))
                                    {
                                        if (alreadyMappedItemInPath)
                                        {
                                            parentId = ItemsRepository.GetItemId(parentId, currentCmsItem);
                                        }
                                    }
                                    else
                                    {
                                        if (alreadyMappedItemInPath)
                                        {
                                            parentId = ItemsRepository.CreateNotMappedItem(parentId, currentCmsItem);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        //no template mapping, set error message
                        itemResponseModel.ImportMessage      = "Import failed: Template not mapped";
                        itemResponseModel.IsImportSuccessful = false;
                    }
                }
                model.Add(itemResponseModel);
            }

            return(model);
        }
Example #4
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);
        }