/// <summary>
        /// This command shows how to remove the reference of a Batch Job from the project.
        /// In this example you can right click a Batch Job in the Reviewer Batch Jobs gallery and remove it from the current project.
        /// </summary>
        /// <returns></returns>
        internal static async Task RemoveBatchJob_Advanced()
        {
            try
            {
                //Step 1: Get the Batch Job that is right clicked
                var galleryItem = Utilities.GetContext <GalleryItem>().FirstOrDefault();
                if (null == galleryItem)
                {
                    return;
                }
                else
                {
                    //Step 2: Remove the Batch Job from the Gallery
                    GalleryItemsChangedEvent.Publish(galleryItem, false);
                }

                //Step 3: Get all Reviewer Batch Job project items from the current project
                IEnumerable <ReviewerBatchJobProjectItem> projectItems = Project.Current.GetItems <ReviewerBatchJobProjectItem>();

                await QueuedTask.Run(() =>
                {
                    //Step 4: Find the Batch Job (in the current project) that is right clicked in the gallery
                    IProjectItem projectItemToRemove = projectItems.Where(p => (p as ReviewerBatchJobProjectItem).Path == galleryItem.Path).FirstOrDefault() as IProjectItem;
                    if (null != projectItemToRemove)
                    {
                        //Step 5: Remove the Batch Job from Project
                        Project.Current.RemoveItem(projectItemToRemove);
                    }
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
            }
        }
        /// <summary>
        /// This command shows how to add existing Batch Jobs to the current project.
        /// In this example you can browse and select multiple Reviewer Batch Jobs to add those to the current project.
        /// If the selected batch jobs contain any invalid batch jobs, those batch jobs will be reported to the user.
        /// </summary>
        /// <returns></returns>
        internal static async Task AddBatchJobs_Advanced()
        {
            try
            {
                //Step 1: Browse to select one or multiple batch jobs and get their path
                IEnumerable <string> rbjPaths = OpenBrowseBatchJobFileDialog();
                if (null == rbjPaths || rbjPaths.Count() < 1)
                {
                    return;
                }

                string strInvalidBatchJobs = "";
                int    invalidRbjCount     = 0;
                await QueuedTask.Run(() =>
                {
                    foreach (string rbjPath in rbjPaths)
                    {
                        try
                        {
                            //Step 2:  Create Reviewer BatchJobItems
                            //This is done inside a try-catch so that you can Skip invalid batch jobs and continue with valid batch jobs
                            IProjectItem rbjItem = DataReviewerModule.CreateBatchJobProjectItem(rbjPath);
                            if (null != rbjItem)
                            {
                                //Step 3: Add BatchJobItems to the current project
                                Project.Current.AddItem(rbjItem as IProjectItem);
                                ReviewerBatchJobProjectItem rbjProjectItem = rbjItem as ReviewerBatchJobProjectItem;

                                //Step 3: Add BatchJobItems to the gallery
                                if (null != rbjProjectItem)
                                {
                                    GalleryItemsChangedEvent.Publish(new GalleryItem(rbjProjectItem.TypeID, rbjProjectItem.Path, rbjProjectItem.Name), true);
                                }
                            }
                        }
                        catch (Exception)
                        {
                            //Exception for invalid batch job
                            //Create a string to report invalid Batch Jobs
                            invalidRbjCount++;
                            strInvalidBatchJobs = strInvalidBatchJobs + "{0}" + invalidRbjCount.ToString() + ". " + rbjPath;
                        }
                    }
                });

                //Display error message for invalid Batch Jobs
                if (invalidRbjCount > 0)
                {
                    MessageBox.Show(string.Format("Selected Batch Jobs contain following invalid Batch Jobs :" + strInvalidBatchJobs, Environment.NewLine) + Environment.NewLine + "These Batch Jobs are not added to the project.",
                                    "Error", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
            }
        }
        /// <summary>
        /// This command shows how to remove the reference of a Reviewer Session from the current project.
        /// In this example you can right click a session in the Reviewer Session gallery and remove it from the current project.
        /// </summary>
        /// <returns></returns>
        internal static async Task RemoveSessions_Advanced()
        {
            try
            {
                //Step 1: Get the session that is right clicked
                var galleryItem = Utilities.GetContext <GalleryItem>().FirstOrDefault();
                if (null == galleryItem)
                {
                    return;
                }
                else
                {
                    //Step 2: Remove the Session from Gallery
                    GalleryItemsChangedEvent.Publish(galleryItem, false);
                }

                await QueuedTask.Run(async() =>
                {
                    //Step 3: Get Reviewer Results project item
                    //A project can contain only one Reviewer Results project item
                    IEnumerable <ReviewerResultsProjectItem> projectItems = Project.Current.GetItems <ReviewerResultsProjectItem>();
                    ReviewerResultsProjectItem resultsProjectItem         = null;
                    if (projectItems.Count() > 0)
                    {
                        resultsProjectItem = projectItems.FirstOrDefault() as ReviewerResultsProjectItem;
                    }

                    if (null != resultsProjectItem)
                    {
                        //Step 4: Get all session items that are currently referenced in the project
                        IEnumerable <Item> sessionItems = resultsProjectItem.GetItems();
                        if (null != sessionItems)
                        {
                            //Step 5: Find the session item (in the project) that is right clicked in the gallery
                            Item itemToRemove = sessionItems.Where(p => (p as Item).Path == galleryItem.Path).FirstOrDefault() as Item;
                            if (null != itemToRemove)
                            {
                                //Step 6: Remove this Session from the current project
                                await resultsProjectItem.RemoveSessionItemAsync(itemToRemove);
                            }
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
            }
        }
        /// <summary>
        /// This command shows how to add all sessions in a Reviewer Workspace to the current project.
        /// These sessions are also added in the Reviewer Sessions Gallery for quick access
        /// </summary>
        /// <returns></returns>
        internal static async Task AddSessions_Advanced()
        {
            try
            {
                //Step 1: Get Reviewer Results project item
                //A project can contain only one Reviewer Results project item
                IEnumerable <ReviewerResultsProjectItem> projectItems = Project.Current.GetItems <ReviewerResultsProjectItem>();
                ReviewerResultsProjectItem resultsProjectItem         = null;
                if (projectItems.Count() > 0)
                {
                    resultsProjectItem = projectItems.FirstOrDefault() as ReviewerResultsProjectItem;
                }
                else
                {
                    MessageBox.Show(string.Format("Current project does not have a connection to the Reviewer Results.{0}Please add a connection to the Reviewer Results", Environment.NewLine), "Warning", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Warning);
                    return;
                }

                if (null != resultsProjectItem)
                {
                    //Step 2: Get the Reviewer Dataset associated with the Reviewer Results project item
                    ReviewerDataset reviewerDataset = resultsProjectItem.ReviewerDataset;
                    if (null != reviewerDataset)
                    {
                        IEnumerable <Session> reviewerSessions = null;
                        await QueuedTask.Run(() =>
                        {
                            //Step 3: Get all Reviewer session that are in the Reviewer dataset
                            reviewerSessions = reviewerDataset.GetSessions();
                            foreach (Session session in reviewerSessions)
                            {
                                //Step 4: Add each Reviewer session to the current project
                                Item sessionItem = resultsProjectItem.CreateSessionItem(session);
                                resultsProjectItem.AddSessionItemAsync(sessionItem);

                                //Step 5: Raise GalleryItemsChangedEvent to add the Reviewer session to the Gallery
                                GalleryItemsChangedEvent.Publish(new GalleryItem(sessionItem.TypeID, sessionItem.Path, sessionItem.Name), true);
                            }
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
            }
        }
 /// <summary>
 /// Unsubscribe gallery item events
 /// </summary>
 protected override void Uninitialize()
 {
     base.Uninitialize();
     GalleryItemsChangedEvent.Unsubscribe(OnChanged);
 }
 /// <summary>
 /// subscribe to gallery item events
 /// </summary>
 public GalleryItemsViewModel()
 {
     GalleryItemsChangedEvent.Subscribe(OnChanged);
 }
 static public void Publish(GalleryItem galleryItem, bool adding)
 {
     GalleryItemsChangedEvent.Publish(new Args(galleryItem, adding));
 }