/// <summary>
        /// Clones the target page settings to source page.
        /// </summary>
        /// <param name="sourcePage">The source page.</param>
        /// <param name="targetPage">The target page.</param>
        /// <returns></returns>
        public static bool ClonePageSettings(Page sourcePage, Page targetPage)
        {
            var pageService = ServiceLocator.Current.GetInstance<IPageService>();
            var permissionCommonService = ServiceLocator.Current.GetInstance<IPermissionCommonService>();

            sourcePage.Widgets.AsParallel().ForAll(widget =>
            {
                var pageWidget = new PageWidget();
                pageWidget.InjectFrom<CloneEntityInjection>(widget);
                pageWidget.Page = targetPage;
                pageWidget.ParentWidgetId = widget.Id;

                //copy widget settings
                if (widget.Settings != null)
                {
                    pageWidget.Settings = (PageWidgetSettings)new PageWidgetSettings().InjectFrom<CloneEntityInjection>(widget.Settings);
                    pageWidget.Settings.LookAndFeelSettings = new LookAndFeelSettings().InjectFrom<CloneEntityInjection>(widget.Settings.LookAndFeelSettings) as LookAndFeelSettings;
                    pageWidget.Settings.Widget = pageWidget;
                }

                //clone page widget instance
                if (widget.InstanceId != null)
                {
                    pageWidget.InstanceId = CloneWidgetInstance(widget);
                }
                targetPage.AddWidget(pageWidget);
            });

            //copy page layout
            targetPage.PageLayout = (PageLayout)new PageLayout { Id = targetPage.PageLayout.Id }.InjectFrom<CloneEntityInjection>(sourcePage.PageLayout);
            targetPage.PageLayout.Page = targetPage;
            sourcePage.PageLayout.ColumnWidths.AsParallel().ForAll(column =>
                                                                       {
                                                                           var columnWidth = (PageLayoutColumnWidthValue)new PageLayoutColumnWidthValue().InjectFrom<CloneEntityInjection>(column);
                                                                           columnWidth.PageLayout = targetPage.PageLayout;
                                                                           targetPage.PageLayout.AddColumnWidth(columnWidth);
                                                                       });

            //copy page settings)
            if (sourcePage.Settings != null)
            {
                targetPage.Settings = (PageSettings)new PageSettings().InjectFrom<CloneEntityInjection>(sourcePage.Settings);
                targetPage.Settings.LookAndFeelSettings = (LookAndFeelSettings)new LookAndFeelSettings().InjectFrom<CloneEntityInjection>(sourcePage.Settings.LookAndFeelSettings);
                targetPage.Settings.Page = targetPage;
            }

            if (pageService.Save(targetPage))
            {
                //copy permissions and update page styles
                foreach (var item in targetPage.Widgets)
                {
                    if (item.ParentWidgetId == null) continue;
                    var systemWidget = MvcApplication.Widgets.FirstOrDefault(w => w.Identifier == item.Widget.Identifier);
                    var sourceWidget = sourcePage.Widgets.FirstOrDefault(w => w.Id == item.ParentWidgetId);
                    if (sourceWidget != null)
                    {
                        if (targetPage.Settings != null && !String.IsNullOrEmpty(targetPage.Settings.CustomCSS))
                        {
                            targetPage.Settings.CustomCSS = targetPage.Settings.CustomCSS.Replace(String.Format(PageWidgetTemplate, sourceWidget.Id), String.Format(PageWidgetTemplate, item.Id));
                        }
                        permissionCommonService.CloneObjectPermisions(systemWidget.GetType(), sourceWidget.Id, item.Id);
                    }
                }
                return pageService.Save(targetPage);
            }
            return false;
        }
        public static bool ClonePageSettingsFromTemplate(Page template, Page targetPage, long? widgetId)
        {
            var pageService = ServiceLocator.Current.GetInstance<IPageService>();
            var permissionCommonService = ServiceLocator.Current.GetInstance<IPermissionCommonService>();

            template.Widgets.Where(widget => widget.Widget.IsPlaceHolder).AsParallel().ForAll(widget =>
            {
                var pageWidget = new PageWidget();
                pageWidget.InjectFrom<CloneEntityInjection>(widget);
                pageWidget.Page = targetPage;
                pageWidget.ParentWidgetId = widget.Id;

                //copy widget settings
                if (widget.Settings != null)
                {
                    pageWidget.Settings = (PageWidgetSettings)new PageWidgetSettings().InjectFrom<CloneEntityInjection>(widget.Settings);
                    pageWidget.Settings.LookAndFeelSettings = new LookAndFeelSettings().InjectFrom<CloneEntityInjection>(widget.Settings.LookAndFeelSettings) as LookAndFeelSettings;
                    pageWidget.Settings.Widget = pageWidget;
                }
                if (widgetId.HasValue)
                {
                    var widgetService = ServiceLocator.Current.GetInstance<IWidgetService>();
                    pageWidget.Widget = widgetService.Find(widgetId.Value);
                }
                pageWidget.TemplateWidgetId = widget.Id;
                targetPage.AddWidget(pageWidget);
            });
            targetPage.Template = template;

            if (pageService.Save(targetPage))
            {
                //copy permissions and update page styles
                foreach (var item in targetPage.Widgets)
                {
                    if (item.ParentWidgetId == null) continue;
                    var systemWidget = MvcApplication.Widgets.FirstOrDefault(w => w.Identifier == item.Widget.Identifier);
                    var sourceWidget = template.Widgets.FirstOrDefault(w => w.Id == item.ParentWidgetId);
                    if (sourceWidget != null)
                    {
                        if (targetPage.Settings != null && !String.IsNullOrEmpty(targetPage.Settings.CustomCSS))
                        {
                            targetPage.Settings.CustomCSS = targetPage.Settings.CustomCSS.Replace(String.Format(PageWidgetTemplate, sourceWidget.Id), String.Format(PageWidgetTemplate, item.Id));
                        }
                        permissionCommonService.CloneObjectPermisions(systemWidget.GetType(), sourceWidget.Id, item.Id);
                    }
                }
                return pageService.Save(targetPage);
            }
            return false;
        }
        /// <summary>
        /// Unlinks the page.
        /// </summary>
        /// <param name="page">The page.</param>
        public static void UnlinkPage(Page page)
        {
            var pageService = ServiceLocator.Current.GetInstance<IPageService>();
            var permissionCommonService = ServiceLocator.Current.GetInstance<IPermissionCommonService>();
            var pageWidgetService = ServiceLocator.Current.GetInstance<IPageWidgetService>();
            if (page.Template != null)
            {
                page.Template.Widgets.AsEnumerable().Each(widget =>
                                                              {
                                                                  PageWidget pageWidget = null;
                                                                  if(widget.Widget.IsPlaceHolder)
                                                                  {
                                                                      pageWidget =
                                                                          page.Widgets.FirstOrDefault(
                                                                              currentPageWidget =>
                                                                              currentPageWidget.TemplateWidgetId ==
                                                                              widget.Id);
                                                                      pageWidget.ColumnNumber = widget.ColumnNumber;
                                                                      pageWidget.OrderNumber = widget.OrderNumber;
                                                                      pageWidget.TemplateWidgetId = null;
                                                                  }
                                                                  if (pageWidget == null)
                                                                  {
                                                                      pageWidget = new PageWidget();
                                                                      pageWidget.InjectFrom<CloneEntityInjection>(widget);
                                                                      pageWidget.Page = page;
                                                                      pageWidget.ParentWidgetId = widget.Id;
                                                                      //copy widget settings
                                                                      if (widget.Settings != null)
                                                                      {
                                                                          pageWidget.Settings =
                                                                              (PageWidgetSettings)
                                                                              new PageWidgetSettings().InjectFrom
                                                                                  <CloneEntityInjection>(widget.Settings);
                                                                          pageWidget.Settings.LookAndFeelSettings =
                                                                              new LookAndFeelSettings().InjectFrom
                                                                                  <CloneEntityInjection>(
                                                                                      widget.Settings.
                                                                                          LookAndFeelSettings) as
                                                                              LookAndFeelSettings;
                                                                          pageWidget.Settings.Widget = pageWidget;
                                                                      }

                                                                      //clone page widget instance
                                                                      if (widget.InstanceId != null)
                                                                      {
                                                                          pageWidget.InstanceId =
                                                                              CloneWidgetInstance(widget);
                                                                      }
                                                                      //TODO: save collection
                                                                      pageWidgetService.Save(pageWidget);
                                                                      page.AddWidget(pageWidget);
                                                                  }
                                                              });

                //copy page layout
                page.PageLayout =
                    (PageLayout)
                    new PageLayout().InjectFrom<CloneEntityInjection>(
                        page.Template.PageLayout);
                page.PageLayout.Page = page;
                page.Template.PageLayout.ColumnWidths.AsParallel().ForAll(column =>
                                                                           {
                                                                               var columnWidth =
                                                                                   (PageLayoutColumnWidthValue)
                                                                                   new PageLayoutColumnWidthValue().
                                                                                       InjectFrom<CloneEntityInjection>(
                                                                                           column);
                                                                               columnWidth.PageLayout =
                                                                                   page.PageLayout;
                                                                               page.PageLayout.AddColumnWidth(
                                                                                   columnWidth);
                                                                           });

                //copy page settings)
                if (page.Template.Settings != null)
                {
                    page.Settings =
                        (PageSettings)new PageSettings().InjectFrom<CloneEntityInjection>(page.Template.Settings);
                    page.Settings.LookAndFeelSettings =
                        (LookAndFeelSettings)
                        new LookAndFeelSettings().InjectFrom<CloneEntityInjection>(
                            page.Template.Settings.LookAndFeelSettings);
                    page.Settings.Page = page;
                }

                if (pageService.Save(page))
                {
                    //copy permissions and update page styles
                    foreach (var item in page.Widgets)
                    {
                        if (item.ParentWidgetId == null) continue;
                        var systemWidget =
                            MvcApplication.Widgets.FirstOrDefault(w => w.Identifier == item.Widget.Identifier);
                        var sourceWidget = page.Template.Widgets.FirstOrDefault(w => w.Id == item.ParentWidgetId);
                        if (sourceWidget != null)
                        {
                            if (page.Settings != null && !String.IsNullOrEmpty(page.Settings.CustomCSS))
                            {
                                page.Settings.CustomCSS =
                                    page.Settings.CustomCSS.Replace(
                                        String.Format(PageWidgetTemplate, sourceWidget.Id),
                                        String.Format(PageWidgetTemplate, item.Id));
                            }
                            permissionCommonService.CloneObjectPermisions(systemWidget.GetType(), sourceWidget.Id,
                                                                          item.Id);
                        }
                    }
                    page.Template = null;
                    pageService.Save(page);
                }
            }
        }