Пример #1
0
        /// <exclude />
        public static string GetBrokenReferencesReport(List <IData> brokenReferences)
        {
            var sb = new StringBuilder();

            const int maximumLinesToShow = 6;

            int toDisplay = brokenReferences.Count > maximumLinesToShow ? maximumLinesToShow - 1 : brokenReferences.Count;

            for (int i = 0; i < toDisplay; i++)
            {
                IData brokenReference = brokenReferences[i];
                Type  type            = brokenReference.DataSourceId.InterfaceType;


                string typeTitle = DynamicTypeReflectionFacade.GetTitle(type);
                if (string.IsNullOrEmpty(typeTitle))
                {
                    typeTitle = type.FullName;
                }

                string labelPropertyName = DynamicTypeReflectionFacade.GetLabelPropertyName(type);
                if (string.IsNullOrEmpty(labelPropertyName))
                {
                    labelPropertyName = "Id"; // This is a nasty fallback, but will work in most cases and all the time with generated types.
                }

                PropertyInfo labelPropertyInfo = brokenReference.GetType().GetProperty(labelPropertyName, BindingFlags.Instance | BindingFlags.Public);
                string       dataLabel;

                if (labelPropertyInfo != null)
                {
                    object propertyFieldValue = labelPropertyInfo.GetValue(brokenReference, null);
                    dataLabel = (propertyFieldValue ?? "NULL").ToString();
                }
                else
                {
                    dataLabel = "'Failed to resolve ({0}) field'".FormatWith(labelPropertyName);
                }


                sb.Append("{0}, {1}".FormatWith(typeTitle, dataLabel));
                sb.Append("\n\r");
            }

            if (brokenReferences.Count > maximumLinesToShow)
            {
                sb.Append("...");
            }

            return(sb.ToString());
        }
        private IData CloneData <T>(T data) where T : class, IData
        {
            IData newdata = DataFacade.BuildNew <T>();

            var dataProperties = typeof(T).GetPropertiesRecursively();

            foreach (var propertyInfo in dataProperties.Where(f => f.CanWrite))
            {
                if (typeof(T).GetPhysicalKeyProperties().Contains(propertyInfo) && propertyInfo.PropertyType == typeof(Guid))
                {
                    propertyInfo.SetValue(newdata, Guid.NewGuid());
                }
                else
                {
                    propertyInfo.SetValue(newdata, propertyInfo.GetValue(data));
                }
            }

            var page = data as IPage;

            if (page != null)
            {
                if (IsRootPage(page))
                {
                    SetNewValue <T>(page, newdata, dataProperties.First(p => p.Name == nameof(IPage.Title)));
                }
                else if (!SetNewValue <T>(page, newdata, dataProperties.First(p => p.Name == nameof(IPage.MenuTitle))))
                {
                    SetNewValue <T>(page, newdata, dataProperties.First(p => p.Name == nameof(IPage.Title)));
                }

                SetNewValue <T>(page, newdata, dataProperties.First(p => p.Name == nameof(IPage.UrlTitle)), isUrl: true);

                PageInsertPosition.After(page.Id).CreatePageStructure(newdata as IPage, page.GetParentId());
            }
            else
            {
                var labelProperty = typeof(T).GetProperty(
                    DynamicTypeReflectionFacade.GetLabelPropertyName(typeof(T)));

                if (labelProperty != null && labelProperty.PropertyType == typeof(string))
                {
                    SetNewValue <T>(data, newdata, labelProperty);
                }
            }

            if (newdata is IPublishControlled)
            {
                (newdata as IPublishControlled).PublicationStatus = GenericPublishProcessController.Draft;
            }
            if (newdata is ILocalizedControlled)
            {
                (newdata as ILocalizedControlled).SourceCultureName = UserSettings.ActiveLocaleCultureInfo.Name;
            }

            newdata = DataFacade.AddNew(newdata);

            if (data is IPage)
            {
                CopyPageData(data as IPage, newdata as IPage);
            }

            return(newdata);
        }