public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { writer.WriteStartObject(); SuiteCRMProjectPart contentPart = value as SuiteCRMProjectPart; Action <string, string, JsonWriter, JsonSerializer> propertyWriter = Orchard.CRM.Core.Providers.Serialization.Utility.WriteProperty; // ExternalId propertyWriter("ExternalId", contentPart.ExternalId, writer, serializer); // LastSyncTime propertyWriter("LastSyncTime", contentPart.LastSyncTime.ToString(), writer, serializer); this.WriteCommonFields(writer, contentPart, serializer); writer.WriteEnd(); }
private bool IsSyncingTicketValid(CopyOrchardTasksToSuiteViewModel.TaskIdentifiers item, SuiteCRMTaskPart ticket, out SuiteCRMProjectPart projectPart) { projectPart = null; if (ticket == null) { return(false); } var suiteCRMTaskPart = ticket.As <SuiteCRMTaskPart>(); if (suiteCRMTaskPart == null) { return(false); } TicketPart ticketPart = ticket.As <TicketPart>(); if (ticketPart == null) { return(false); } if (!item.SyncSubTasks && ticketPart.Record.Parent != null) { return(false); } AttachToProjectPart attachToProjectPart = ticket.As <AttachToProjectPart>(); if (item.IsProjectTask && (attachToProjectPart == null || attachToProjectPart.Record.Project == null)) { return(false); } if (item.IsProjectTask) { var project = this.services.ContentManager.Get(attachToProjectPart.Record.Project.Id); if (project == null) { return(false); } projectPart = project.As <SuiteCRMProjectPart>(); if (string.IsNullOrEmpty(projectPart.ExternalId)) { return(false); } } return(true); }
public IEnumerable <SuiteCRMTaskDetailViewModel> CopyOrchardTicketsToSuite(CopyOrchardTasksToSuiteViewModel model) { List <SuiteCRMTaskDetailViewModel> returnValue = new List <SuiteCRMTaskDetailViewModel>(); using (var connection = Helper.GetConnection(this.services, this.Logger)) using (SuiteCRMTaskUnitOfWork taskRepository = new SuiteCRMTaskUnitOfWork(connection)) using (SuiteCRMEmailAddressBeanUnitOfWork suiteCRMEmailAddressBeanUnitOfWork = new SuiteCRMEmailAddressBeanUnitOfWork(taskRepository)) using (SuiteCRMEmailAddressUnitOfWork suiteCRMEmailAddressUnitOfWork = new SuiteCRMEmailAddressUnitOfWork(taskRepository)) using (SuiteCRMProjectTaskUnitOfWork projectTasksRepository = new SuiteCRMProjectTaskUnitOfWork(taskRepository)) using (var suiteCRMTransaction = taskRepository.BeginTransaction()) { TicketContext context = new TicketContext(); context.ProjectTaskUnitOfWork = projectTasksRepository; context.Priorities = this.priorityRepository.Table.ToList(); context.StatusList = this.statusRepository.Table.ToList(); try { var taskIds = model.Tasks.Where(c => !string.IsNullOrEmpty(c.SuiteCRMId)).Select(c => c.SuiteCRMId).ToArray(); var suiteTasks = taskRepository.GetTasks(taskIds); var suiteProjectTasks = projectTasksRepository.GetTasks(taskIds); var orchardTickets = this.services .ContentManager .GetMany <SuiteCRMTaskPart>( model.Tasks.Where(c => c.OrchardCollaborationTicketId.HasValue).Select(c => c.OrchardCollaborationTicketId.Value), VersionOptions.Published, new QueryHints().ExpandParts <TicketPart>()); foreach (var item in model.Tasks) { if (item.OrchardCollaborationTicketId == null) { continue; } var ticket = orchardTickets.FirstOrDefault(c => c.Id == item.OrchardCollaborationTicketId.Value); var suiteCRMTaskPart = ticket.As <SuiteCRMTaskPart>(); TicketPart ticketPart = ticket.As <TicketPart>(); ContentItemPermissionPart permissionPart = ticket.As <ContentItemPermissionPart>(); AttachToProjectPart attachToProjectPart = ticket.As <AttachToProjectPart>(); SuiteCRMProjectPart projectPart = null; if (!this.IsSyncingTicketValid(item, ticket, out projectPart)) { continue; } project_task suiteCRMProjectTask = null; task suiteCRMTask = null; if (!string.IsNullOrEmpty(suiteCRMTaskPart.ExternalId) && item.IsProjectTask) { suiteCRMProjectTask = suiteProjectTasks.FirstOrDefault(c => c.id == suiteCRMTaskPart.ExternalId); } if (!string.IsNullOrEmpty(suiteCRMTaskPart.ExternalId) && !item.IsProjectTask) { suiteCRMTask = suiteTasks.FirstOrDefault(c => c.id == suiteCRMTaskPart.ExternalId); } if (suiteCRMProjectTask == null && item.IsProjectTask) { suiteCRMProjectTask = new project_task(); suiteCRMProjectTask.project_id = item.SuiteCRMId; projectTasksRepository.Add(suiteCRMProjectTask); } if (suiteCRMTask == null && !item.IsProjectTask) { suiteCRMTask = new task(); taskRepository.Add(suiteCRMTask); } CommonPart commonPart = ticketPart.As <CommonPart>(); DateTime? lastOrchardTicketChangeDate = commonPart.ModifiedUtc ?? commonPart.CreatedUtc; if (suiteCRMProjectTask != null && ( !item.DoNotOverrideNewerValues || suiteCRMProjectTask.date_modified == null || (lastOrchardTicketChangeDate.HasValue && suiteCRMProjectTask.date_modified.Value <= lastOrchardTicketChangeDate.Value) )) { this.Copy(ticketPart, permissionPart, suiteCRMProjectTask, context); suiteCRMProjectTask.project_id = projectPart.ExternalId; projectTasksRepository.Save(); } if (suiteCRMTask != null && ( !item.DoNotOverrideNewerValues || suiteCRMTask.date_modified == null || (lastOrchardTicketChangeDate.HasValue && suiteCRMTask.date_modified.Value <= lastOrchardTicketChangeDate.Value) )) { this.Copy(ticketPart, permissionPart, suiteCRMTask, context); // set contact if (string.IsNullOrEmpty(suiteCRMTask.created_by) && commonPart.Owner != null) { var emailAddress = suiteCRMEmailAddressUnitOfWork.GetByEmail(commonPart.Owner.Email); if (emailAddress != null) { var contact = suiteCRMEmailAddressBeanUnitOfWork.GetBeanIdOfEmailAddress(Helper.ContactsModuleName, new[] { emailAddress.id }).FirstOrDefault(); if (contact != null) { suiteCRMTask.parent_id = contact.id; suiteCRMTask.parent_type = Helper.ContactsModuleName; } } } projectTasksRepository.Save(); } suiteCRMTaskPart.ExternalId = item.IsProjectTask ? suiteCRMProjectTask.id : suiteCRMTask.id; suiteCRMTaskPart.LastSyncTime = DateTime.UtcNow; suiteCRMTaskPart.TaskType = item.IsProjectTask ? SuiteCRMTaskPart.SuiteCRMProjectTaskTypeValue : SuiteCRMTaskPart.SuiteCRMTaskTypeValue; this.services.ContentManager.Publish(ticket.ContentItem); SuiteCRMTaskDetailViewModel returnValueItem = new SuiteCRMTaskDetailViewModel(); returnValueItem.OrchardCollaborationTicket = ticket.ContentItem; returnValueItem.IsSync = true; returnValueItem.IsProjectTask = item.IsProjectTask; returnValueItem.LastSyncTime = suiteCRMTaskPart.LastSyncTime; returnValueItem.SuiteCRMTaskId = suiteCRMTaskPart.ExternalId; returnValue.Add(returnValueItem); } suiteCRMTransaction.Commit(); } catch (Exception ex) { suiteCRMTransaction.Rollback(); throw ex; } } return(returnValue); }
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); }