public void AddPortlet(ContentItem projectDashboard, ContentItem portletTemplate, int position) { ContentItem newPortlet = null; if (portletTemplate.ContentType == ContentTypes.ProjectDashboardProjectionPortletTemplateContentType) { newPortlet = this.services.ContentManager.Create(ContentTypes.ProjectDashboardProjectionPortletContentType); ProjectionWithDynamicSortPart destinationProjectionPart = newPortlet.As <ProjectionWithDynamicSortPart>(); ProjectionWithDynamicSortPart sourceProjectionPart = portletTemplate.As <ProjectionWithDynamicSortPart>(); CRMHelper.Copy(layoutRepository, sourceProjectionPart.Record, destinationProjectionPart.Record); } else if (portletTemplate.ContentType == ContentTypes.ProjectDashboardReportViewerPortletTemplateContentType) { newPortlet = this.services.ContentManager.Create(ContentTypes.ProjectDashboardReportViewerPortletContentType); DataReportViewerPart destinationReportViewerPart = newPortlet.As <DataReportViewerPart>(); DataReportViewerPart sourceReportViewerPart = portletTemplate.As <DataReportViewerPart>(); CRMHelper.Copy(sourceReportViewerPart.Record, destinationReportViewerPart.Record); } else if (portletTemplate.ContentType == ContentTypes.ProjectLastActivityStreamTemplateContentType) { newPortlet = this.services.ContentManager.Create(ContentTypes.ProjectLastActivityStreamContentType); var sourceActivityStreamPart = portletTemplate.As <ActivityStreamPart>(); var destinationActivityStreamPart = newPortlet.As <ActivityStreamPart>(); destinationActivityStreamPart.QueryId = sourceActivityStreamPart.QueryId; } else { throw new ArgumentOutOfRangeException("The ContentType of the portletTemplate is not supported."); } // store templateId newPortlet.As <InfosetPart>().Store <int>(FieldNames.ProjectDashboardPortletTemplateId, portletTemplate.Id); // copy Title TitlePart titlePart = newPortlet.As <TitlePart>(); TitlePart templateTitlePart = portletTemplate.As <TitlePart>(); titlePart.Title = templateTitlePart.Title; ContainablePart containablePart = newPortlet.As <ContainablePart>(); containablePart.Position = position; var projectDetailContainerPart = projectDashboard.As <ContainerPart>(); var newPortletCommon = newPortlet.As <CommonPart>(); newPortletCommon.Container = projectDashboard; this.services.ContentManager.Publish(newPortlet); }
public SidebarDashboardHandler(IContentManager contentManager, IRepository <LayoutRecord> layoutRepository) { OnPublished <SidebarDashboardPart>((context, part) => { // portlet list var portletNames = string.IsNullOrEmpty(part.SidebarPortletList) ? new string[] { } : part.SidebarPortletList.Split(','); var portlets = contentManager.HqlQuery().ForType(Consts.SidebarProjectionPortletTemplateType, Consts.SidebarStaticPortletType).Where(c => c.ContentPartRecord <TitlePartRecord>(), c => c.In("Title", portletNames)).List().Select(c => c.As <TitlePart>()); // current portlets var currentPortlets = contentManager.Query().Where <CommonPartRecord>(c => c.Container.Id == part.ContentItem.Id).List().Select(c => c.As <TitlePart>()); // add new portlets int position = -1; foreach (var newPortlet in portletNames.Where(c => !currentPortlets.Any(d => d.Title == c))) { position++; var newPortletContentItemTemplate = portlets.FirstOrDefault(c => c.Title == newPortlet); if (newPortletContentItemTemplate == null) { continue; } ContentItem newPortletContentItem = null; if (newPortletContentItemTemplate.ContentItem.ContentType == Consts.SidebarProjectionPortletTemplateType) { newPortletContentItem = contentManager.Create(Consts.SidebarProjectionPortletType); CRMHelper.Copy(layoutRepository, newPortletContentItemTemplate.As <ProjectionWithDynamicSortPart>().Record, newPortletContentItem.As <ProjectionWithDynamicSortPart>().Record); } else { newPortletContentItem = contentManager.Create(Consts.SidebarStaticPortletType); newPortletContentItem.As <BodyPart>().Text = newPortletContentItemTemplate.As <BodyPart>().Text; } // Title newPortletContentItem.As <TitlePart>().Title = newPortletContentItemTemplate.Title; // Container var newPortletCommon = newPortletContentItem.As <CommonPart>(); if (newPortletCommon.Container != null && newPortletCommon.Container.Id != part.ContentItem.Id) { continue; } newPortletCommon.Container = part.ContentItem; ContainablePart containablePart = newPortletContentItem.As <ContainablePart>(); if (containablePart == null) { continue; } // Position containablePart.Position = position; // publish new portlet contentManager.Publish(newPortletContentItem); } // delete portlets foreach (var portlet in currentPortlets.Where(c => !portletNames.Any(d => d == c.Title))) { // Container contentManager.Remove(portlet.ContentItem); } // set position foreach (var portlet in currentPortlets.Where(c => portletNames.Any(d => d == c.Title))) { // Container int index = portletNames.ToList().IndexOf(portlet.Title); ContainablePart containablePart = portlet.As <ContainablePart>(); if (containablePart == null) { continue; } // Position containablePart.Position = index; // publish new portlet contentManager.Publish(portlet.ContentItem); } }); }
public GenericDashboardHandler( IContentManager contentManager, IContentDefinitionManager contentDefinitionManager, IRepository <LayoutRecord> layoutRepository) { OnPublished <GenericDashboardPart>((context, part) => { if (!part.CreatePortletsOnPublishing) { return; } // set default portlets for DashboardWidget if (part.ContentItem.ContentType == Consts.GenericCoverWidgetContentType && string.IsNullOrEmpty(part.PortletList)) { part.PortletList = string.Join(",", (new string[] { Consts.SmtpPortletContentType, Consts.IMAPTPortletContentType })); } var portletTypes = Helper.GetGenericDashboardAvailablePortletTypes(contentDefinitionManager); var portlets = contentManager.HqlQuery().ForType(portletTypes).List(); // portlet list var portletNames = string.IsNullOrEmpty(part.PortletList) ? new string[] { } : part.PortletList.Split(','); // current portlets var currentPortlets = contentManager.Query().Where <CommonPartRecord>(c => c.Container.Id == part.ContentItem.Id).List().Select(c => c.As <TitlePart>()); // add new portlets int position = -1; foreach (var newPortlet in portletNames.Where(c => !currentPortlets.Any(d => d.Title == c))) { position++; ContentTypeDefinition portletType = null; var newPortletContentItemTemplate = portlets.FirstOrDefault(c => (c.As <TitlePart>() != null && c.As <TitlePart>().Title == newPortlet)); if (newPortletContentItemTemplate != null) { portletType = newPortletContentItemTemplate.TypeDefinition; } else if (portletTypes.Any(c => c == newPortlet)) { portletType = contentDefinitionManager.GetTypeDefinition(newPortlet); } if (portletType == null) { continue; } ContentItem newPortletContentItem = null; if (portletType.Name == Consts.GenericProjectionPortletTemplateType) { newPortletContentItem = contentManager.Create(Consts.GenericProjectionPortletType); CRMHelper.Copy(layoutRepository, newPortletContentItemTemplate.As <ProjectionWithDynamicSortPart>().Record, newPortletContentItem.As <ProjectionWithDynamicSortPart>().Record); } else if (portletType.Name == Consts.GenericReportViewerPortletTemplateType) { newPortletContentItem = contentManager.Create(Consts.GenericReportViewerPortletType); CRMHelper.Copy(newPortletContentItemTemplate.As <DataReportViewerPart>().Record, newPortletContentItem.As <DataReportViewerPart>().Record); } else if (portletType.Name == Consts.GenericActivityStreamPortletTemplateType) { newPortletContentItem = contentManager.Create(Consts.GenericActivityStreamPortletType); var sourceActivityStreamPart = newPortletContentItemTemplate.As <ActivityStreamPart>(); var destinationActivityStreamPart = newPortletContentItem.As <ActivityStreamPart>(); destinationActivityStreamPart.QueryId = sourceActivityStreamPart.QueryId; } else { newPortletContentItem = contentManager.Create(portletType.Settings[Consts.TemplateInstanceType], VersionOptions.Draft); } ContainablePart containablePart = newPortletContentItem.As <ContainablePart>(); if (containablePart == null) { continue; } // Position containablePart.Position = position; // Container var newPortletCommon = newPortletContentItem.As <CommonPart>(); newPortletCommon.Container = part.ContentItem; contentManager.Publish(newPortletContentItem); } // delete portlets foreach (var portlet in currentPortlets.Where(c => !portletNames.Any(d => d == c.Title))) { // Container contentManager.Remove(portlet.ContentItem); } // set position foreach (var portlet in currentPortlets.Where(c => portletNames.Any(d => d == c.Title))) { // Container int index = portletNames.ToList().IndexOf(portlet.Title); ContainablePart containablePart = portlet.As <ContainablePart>(); if (containablePart == null) { continue; } // Position containablePart.Position = index; // publish new portlet contentManager.Publish(portlet.ContentItem); } }); }