public IEnumerable <SuiteCRMProjectDetailViewModel> GetProjects(int pageNumber, int pageSize, bool basedOnSuiteCRMList)
        {
            IEnumerable <project>             suiteCRMProjects             = null;
            IEnumerable <SuiteCRMProjectPart> orchardCollaborationProjects = null;

            if (basedOnSuiteCRMList)
            {
                suiteCRMProjects = this.suiteCRMProjectService.GetProjects(pageNumber, pageSize);

                // TODO: it gets the list of all projects. Find a better way
                orchardCollaborationProjects = this.projectService.GetProjects(null).Where(c => c.Is <SuiteCRMProjectPart>()).Select(c => c.As <SuiteCRMProjectPart>()).ToList();
                orchardCollaborationProjects = orchardCollaborationProjects.Where(c => suiteCRMProjects.Any(d => !string.IsNullOrEmpty(c.ExternalId) && d.id.ToLower() == c.ExternalId.ToLower())).ToList();
            }
            else
            {
                var pager = new Pager(this.services.WorkContext.CurrentSite, pageNumber + 1, pageSize);
                orchardCollaborationProjects = this.projectService.GetProjects(pager).Where(c => c.Is <SuiteCRMProjectPart>()).Select(c => c.As <SuiteCRMProjectPart>()).ToList();

                var suiteCRMIds = orchardCollaborationProjects.Select(c => c.ExternalId).ToArray();
                suiteCRMProjects = this.suiteCRMProjectService.GetProjects(suiteCRMIds);
            }

            List <SuiteCRMProjectDetailViewModel> returnValue = new List <SuiteCRMProjectDetailViewModel>();

            foreach (var project in orchardCollaborationProjects.OrderByDescending(c => c.As <CommonPart>().ModifiedUtc))
            {
                SuiteCRMProjectDetailViewModel projectModel = new SuiteCRMProjectDetailViewModel();
                projectModel.OrchardCollaborationProject = project.ContentItem;
                projectModel.LastSyncTime = project.LastSyncTime;

                ProjectPart projectPart = project.As <ProjectPart>();
                if (projectPart != null)
                {
                    projectModel.OrchardCollaborationTitle = projectPart.Title;
                }

                if (!string.IsNullOrEmpty(project.ExternalId))
                {
                    var suiteCRMPRoject = suiteCRMProjects.FirstOrDefault(c => c.id.ToLower() == project.ExternalId.ToLower());

                    if (suiteCRMPRoject != null)
                    {
                        projectModel.IsSync          = true;
                        projectModel.SuiteCRMProject = this.Convert(suiteCRMPRoject);
                    }
                }

                returnValue.Add(projectModel);
            }

            foreach (var suiteCRMProject in suiteCRMProjects.Where(c => !returnValue.Any(d => d.SuiteCRMProject != null && d.SuiteCRMProject.Id == c.id)))
            {
                SuiteCRMProjectDetailViewModel projectModel = new SuiteCRMProjectDetailViewModel();
                projectModel.SuiteCRMProject = this.Convert(suiteCRMProject);
                returnValue.Add(projectModel);
            }

            return(returnValue);
        }
        public IEnumerable <SuiteCRMProjectDetailViewModel> CopyOrchardProjectsToSuite(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))
                        using (var suiteCRMTransaction = projectRepository.BeginTransaction())
                        {
                            try
                            {
                                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 => c.OrchardCollaborationProjectId.HasValue))
                                {
                                    var suiteCRMProjectPart = orchardProjects.FirstOrDefault(c => c.Id == item.OrchardCollaborationProjectId.Value);

                                    if (suiteCRMProjectPart == null)
                                    {
                                        continue;
                                    }

                                    var orchardProject = suiteCRMProjectPart.As <ProjectPart>();

                                    project suiteCRMProject = null;
                                    if (!string.IsNullOrEmpty(item.SuiteCRMId))
                                    {
                                        suiteCRMProject = suiteProjects.FirstOrDefault(c => c.id == item.SuiteCRMId);

                                        if (suiteCRMProject == null)
                                        {
                                            suiteCRMProject = new project();
                                            projectRepository.Add(suiteCRMProject);
                                        }
                                    }
                                    else
                                    {
                                        suiteCRMProject = new project();
                                        projectRepository.Add(suiteCRMProject);
                                    }

                                    // the values will be overridde in case user doesn't care about update time (item.DoNotOverrideNewerValues == false) or
                                    // the target modified date is smaller than source modified date
                                    CommonPart commonPart = orchardProject.As <CommonPart>();
                                    DateTime?  lastOrchardTicketChangeDate = commonPart.ModifiedUtc ?? commonPart.CreatedUtc;
                                    if (!item.DoNotOverrideNewerValues ||
                                        suiteCRMProject.date_modified == null ||
                                        (lastOrchardTicketChangeDate.HasValue &&
                                         suiteCRMProject.date_modified.Value <= lastOrchardTicketChangeDate.Value))
                                    {
                                        this.Copy(orchardProject, suiteCRMProject);
                                        projectRepository.Save();
                                    }

                                    suiteCRMProjectPart.LastSyncTime = DateTime.UtcNow;
                                    suiteCRMProjectPart.ExternalId   = suiteCRMProject.id;
                                    item.SuiteCRMId = suiteCRMProject.id;

                                    if (item.SyncTasks)
                                    {
                                        TicketContext context = new TicketContext();
                                        context.ProjectTaskUnitOfWork = projectTasksRepository;
                                        context.Priorities            = this.priorityRepository.Table.ToList();
                                        context.StatusList            = this.statusRepository.Table.ToList();
                                        this.CopyOrchardTicketsToSuiteCRM(context, item);
                                    }

                                    this.services.ContentManager.Publish(suiteCRMProjectPart.ContentItem);

                                    SuiteCRMProjectDetailViewModel projectModel = new SuiteCRMProjectDetailViewModel();
                                    projectModel.OrchardCollaborationProject = suiteCRMProjectPart.ContentItem;
                                    projectModel.IsSync                    = true;
                                    projectModel.LastSyncTime              = suiteCRMProjectPart.LastSyncTime;
                                    projectModel.SuiteCRMProject           = this.Convert(suiteCRMProject);
                                    projectModel.OrchardCollaborationTitle = orchardProject.Record.Title;
                                    returnValue.Add(projectModel);
                                }

                                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);
        }