public IEnumerable <SuiteCRMProjectDetailViewModel> CopySuiteCRMProjectsToOrchard(CopyOrchardProjectToSuiteViewModel model) { List <SuiteCRMProjectDetailViewModel> returnValue = new List <SuiteCRMProjectDetailViewModel>(); using (var connection = Helper.GetConnection(this.services, this.Logger)) using (SuiteCRMProjectUnitOfWork projectRepository = new SuiteCRMProjectUnitOfWork(connection)) using (SuiteCRMProjectTaskUnitOfWork projectTasksRepository = new SuiteCRMProjectTaskUnitOfWork(projectRepository)) { var suiteProjects = projectRepository.GetProjects(model.Projects.Where(c => !string.IsNullOrEmpty(c.SuiteCRMId)).Select(c => c.SuiteCRMId).ToArray()); var orchardProjects = this.services .ContentManager .GetMany <SuiteCRMProjectPart>( model.Projects.Where(c => c.OrchardCollaborationProjectId.HasValue).Select(c => c.OrchardCollaborationProjectId.Value), VersionOptions.Published, new QueryHints().ExpandParts <ProjectPart>()); foreach (var item in model.Projects.Where(c => !string.IsNullOrEmpty(c.SuiteCRMId))) { var suiteCRMProject = suiteProjects.FirstOrDefault(c => c.id.ToLower() == item.SuiteCRMId.ToLower()); if (suiteCRMProject == null) { continue; } ContentItem orchardProject = null; dynamic projectSnapshot = null; bool isNew = false; if (item.OrchardCollaborationProjectId.HasValue) { var part = orchardProjects.FirstOrDefault(c => c.Id == item.OrchardCollaborationProjectId.Value); if (part != null) { orchardProject = part.ContentItem; projectSnapshot = this.streamService.TakeSnapshot(orchardProject); } else { isNew = true; orchardProject = this.services.ContentManager.New("ProjectItem"); } } else { isNew = true; orchardProject = this.services.ContentManager.New("ProjectItem"); } if (isNew) { ProjectDashboardEditorPart editorPart = orchardProject.As <ProjectDashboardEditorPart>(); var portletTemplates = this.projectService.GetPortletsTemplates(); editorPart.PortletList = this.projectService.GetDefaultPortletIds(portletTemplates).ToArray(); this.services.ContentManager.Create(orchardProject); } // by building editor, we can be sure that the default values will be set this.services.ContentManager.BuildEditor(orchardProject); SuiteCRMProjectPart suiteCRMProjectPart = orchardProject.As <SuiteCRMProjectPart>(); ProjectPart projectPart = orchardProject.As <ProjectPart>(); suiteCRMProjectPart.ExternalId = suiteCRMProject.id; suiteCRMProjectPart.LastSyncTime = DateTime.UtcNow; // the values will be overridde in case user doesn't care about update time (item.DoNotOverrideNewerValues == false) or // the target modified date is less than source modified date DateTime? lastSuiteCRMChangeDate = suiteCRMProject.date_modified ?? suiteCRMProject.date_entered; CommonPart commonPart = orchardProject.As <CommonPart>(); if (!item.DoNotOverrideNewerValues || isNew || (lastSuiteCRMChangeDate.HasValue && commonPart.ModifiedUtc <= lastSuiteCRMChangeDate.Value)) { this.Copy(suiteCRMProject, projectPart); this.services.ContentManager.Publish(orchardProject); item.OrchardCollaborationProjectId = orchardProject.Id; this.streamService.WriteChangesToStreamActivity(orchardProject, projectSnapshot, null); } if (item.SyncTasks) { TicketContext context = new TicketContext(); context.ProjectTaskUnitOfWork = projectTasksRepository; context.Priorities = this.priorityRepository.Table.ToList(); context.StatusList = this.statusRepository.Table.ToList(); this.CopySuiteCRMTasksToOrchardTickets(context, item); } SuiteCRMProjectDetailViewModel projectModel = new SuiteCRMProjectDetailViewModel(); projectModel.SuiteCRMProject = this.Convert(suiteCRMProject); projectModel.LastSyncTime = suiteCRMProjectPart.LastSyncTime; projectModel.OrchardCollaborationProject = orchardProject; projectModel.OrchardCollaborationTitle = projectPart.Title; returnValue.Add(projectModel); } } return(returnValue); }
public ProjectHandler( ISearchTicketService searchTicketService, IContentManager contentManager, IMenuService menuService, IRepository <ProjectPartRecord> repository, IRepository <AttachToProjectPartRecord> attachToProjectRepository, IExtendedProjectService projectService) { OnCreated <ProjectPart>((context, projectPart) => { projectService.CreateProjectDependencies(projectPart); }); OnRemoved <ProjectPart>((context, projectPart) => { // Delete menu and menu widget var menu = menuService.GetMenu(projectPart.MenuId); if (menu != null) { contentManager.Remove(menu.ContentItem); var projectMenuWidget = projectService.GetProjectMenuWidget(projectPart.Id); if (projectMenuWidget != null) { contentManager.Remove(projectMenuWidget); } } // update all tickets who are attached to the project var attachToProjects = contentManager.HqlQuery <AttachToProjectPart>().Where(c => c.ContentPartRecord <AttachToProjectPartRecord>(), c => c.Eq("Project.Id", projectPart.Record.Id)).List(); foreach (var attachToProject in attachToProjects) { contentManager.Remove(attachToProject.ContentItem); } }); OnLoaded <ProjectPart>((context, projectPart) => { if (projectPart.RelatedItemsHaveBeenInitialized == false) { projectService.CreateMilestoneAndBacklogForProject(projectPart); projectPart.RelatedItemsHaveBeenInitialized = true; } // if the project doesn't have a menu, create a new one for it. if (projectPart.MenuId == default(int)) { projectService.CreateProjectMenu(projectPart); } }); OnPublished <ProjectPart>((context, projectPart) => { ProjectDashboardEditorPart dashboardPart = projectPart.As <ProjectDashboardEditorPart>(); int[] portletIds = dashboardPart.PortletList ?? new int[] { }; ContentItem projectDetail = contentManager .Query() .ForType(ContentTypes.ProjectDetailContentType) .Where <AttachToProjectPartRecord>(c => c.Project.Id == projectPart.Id) .Slice(1) .FirstOrDefault(); if (projectDetail == null) { return; } // portlets var currentPortlets = contentManager.Query().Where <CommonPartRecord>(c => c.Container.Id == projectDetail.Id).List(); var portletTemplates = contentManager.GetMany <ContentItem>(portletIds, VersionOptions.Published, new QueryHints()); // add new portlets int position = -1; foreach (var portletId in portletIds) { position++; var currentPortlet = currentPortlets.FirstOrDefault(c => c.As <InfosetPart>().Retrieve <int>(FieldNames.ProjectDashboardPortletTemplateId) == portletId); if (currentPortlet != null) { ContainablePart containablePart = currentPortlet.As <ContainablePart>(); containablePart.Position = position; continue; } var portletTemplate = portletTemplates.FirstOrDefault(c => c.Id == portletId); if (portletTemplate == null) { continue; } projectService.AddPortlet(projectDetail, portletTemplate, position); } // delete existing portlets that are not exist in the portletIds foreach (var portlet in currentPortlets) { var templateId = portlet.As <InfosetPart>().Retrieve <int>(FieldNames.ProjectDashboardPortletTemplateId); if (!portletIds.Contains(templateId)) { contentManager.Remove(portlet); } } }); }