Esempio n. 1
0
        public void ProcessComponents(Item newItem, Item importItem)
        {
            var components = new List <IComponentMapping>();

            components.AddRange(ComponentMappings);

            TemplateMapping tm = GetTemplateMapping(newItem);

            if (tm != null)
            {
                components.AddRange(tm.ComponentMappings);
            }

            foreach (var cm in components)
            {
                using (new LanguageSwitcher(ImportToLanguage))
                {
                    try
                    {
                        var dsName = cm.ToDatasourcePath.Contains("/")
                            ? cm.ToDatasourcePath.Substring(cm.ToDatasourcePath.LastIndexOf("/") + 1)
                            : cm.ToDatasourcePath;

                        var oldDeviceDef = PresentationService.FindDeviceDefinition(importItem, cm.FromDevice);
                        if (oldDeviceDef == null)
                        {
                            continue;
                        }

                        var oldRendering = PresentationService.FindRendering(oldDeviceDef, cm.FromPlaceholder, cm.FromComponent);
                        if (oldRendering == null)
                        {
                            Logger.Log($"There was no rendering matching device:{cm.FromDevice} - placeholder:{cm.FromPlaceholder} - component:{cm.FromComponent}", importItem.Paths.FullPath, LogType.MultilistToComponent, "device xml", oldDeviceDef.ToXml());
                            continue;
                        }

                        var oldDatasource = PresentationService.FindDatasource(this, importItem, oldRendering);
                        if (oldDatasource == null)
                        {
                            Logger.Log($"There was no datasource found matching name:{dsName}", importItem.Paths.FullPath, LogType.MultilistToComponent, "rendering xml", oldRendering.ToXml());
                            continue;
                        }

                        var newDeviceDef = PresentationService.FindDeviceDefinition(newItem, cm.ToDevice);
                        if (newDeviceDef == null)
                        {
                            continue;
                        }

                        if (!cm.OverwriteExisting)
                        {
                            var createDatasource = !string.IsNullOrWhiteSpace(cm.ToDatasourcePath);
                            var newDatasource    = createDatasource
                                ? PresentationService.CreateDatasource(this, newItem, newDeviceDef, dsName, cm.ToDatasourceFolder, cm.ToDatasourcePath, cm.ToComponent, cm.OverwriteExisting)
                                : null;
                            if (newDatasource == null && createDatasource)
                            {
                                Logger.Log($"There was no datasource created for device:{cm.ToDevice} - placeholder:{cm.ToPlaceholder} - component:{cm.ToComponent}", newItem.Paths.FullPath, LogType.MultilistToComponent, "device xml", newDeviceDef.ToXml());
                                continue;
                            }

                            SetDatasourceFields(oldDatasource, newDatasource, cm);
                            PresentationService.AddComponent(newItem, newDatasource, cm.ToPlaceholder, cm.ToComponent, cm.ToDevice, cm.ToParameters, cm.IsSXA);
                        }
                        else
                        {
                            var newRendering = PresentationService.FindRendering(newDeviceDef, cm.ToPlaceholder, cm.ToComponent);
                            if (newRendering == null)
                            {
                                Logger.Log($"There was no rendering matching device:{cm.ToDevice} - placeholder:{cm.ToPlaceholder} - component:{cm.ToComponent}", importItem.Paths.FullPath, LogType.MultilistToComponent, "device xml", newDeviceDef.ToXml());
                                continue;
                            }

                            var newDatasource = PresentationService.FindDatasource(this, newItem, newRendering);
                            if (newDatasource == null)
                            {
                                Logger.Log($"There was no datasource found matching name:{dsName}", newItem.Paths.FullPath, LogType.MultilistToComponent, "rendering xml", newRendering.ToXml());
                                continue;
                            }

                            SetDatasourceFields(oldDatasource, newDatasource, cm);
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Log("SitecoreDataMap.ProcessComponents", string.Format("failed to import component {0} on item {1}", cm.FromComponent, importItem.Paths.FullPath));
                    }
                }
            }
        }