public int ProjectCount()
 {
     using (SuiteCRMProjectUnitOfWork repository = new SuiteCRMProjectUnitOfWork(Helper.GetConnection(this.orchardServices, this.Logger)))
     {
         return(repository.Count());
     }
 }
        public IEnumerable <project> GetProjects(string[] ids)
        {
            List <project> projects = new List <project>();

            using (SuiteCRMProjectUnitOfWork repository = new SuiteCRMProjectUnitOfWork(Helper.GetConnection(this.orchardServices, this.Logger)))
            {
                projects = repository.GetProjects(ids).ToList();
            }

            return(projects);
        }
        public IEnumerable <project> GetProjects(int pageNumber, int pageSize)
        {
            if (pageSize == 0)
            {
                pageSize = 20;
            }

            var setting = this.orchardServices.WorkContext.CurrentSite.As <SuiteCRMSettingPart>();

            List <project> projects = new List <project>();

            using (SuiteCRMProjectUnitOfWork repository = new SuiteCRMProjectUnitOfWork(Helper.GetConnection(this.orchardServices, this.Logger)))
            {
                projects = repository.GetProjects(pageNumber, pageSize).ToList();
            }

            return(projects);
        }
        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);
        }