/// <summary>
        /// Gets a value indicating whether the submit buttons are enabled or not
        /// </summary>
        /// <returns>
        /// true if enabled, false if not
        /// </returns>
        private bool IsSubmitOrSynchronizeEnabled()
        {
            if (this.Session == null)
            {
                return(false);
            }

            if (this.Iterations.Count == 0)
            {
                return(false);
            }

            var activeWorkbook = this.ExcelQuery.QueryActiveWorkbook(this.officeApplicationWrapper.Excel);

            if (activeWorkbook == null)
            {
                return(false);
            }

            var workbookSessionDal = new WorkbookSessionDal(activeWorkbook);
            var workbookSession    = workbookSessionDal.Read();

            if (workbookSession == null)
            {
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// Queries the content of the Rebuild control label
        /// </summary>
        /// <returns>
        /// the label content
        /// </returns>
        private string QueryRebuildLabel()
        {
            if (this.Session == null)
            {
                return("Rebuild");
            }

            var activeWorkbook = this.ExcelQuery.QueryActiveWorkbook(this.officeApplicationWrapper.Excel);

            if (activeWorkbook == null)
            {
                return("Rebuild");
            }

            var workbookSessionDal = new WorkbookSessionDal(activeWorkbook);
            var workbookSession    = workbookSessionDal.Read();

            if (workbookSession != null)
            {
                var sb = new StringBuilder();
                sb.AppendFormat("{0} : {1}", workbookSession.EngineeringModelSetup.ShortName, workbookSession.IterationSetup.IterationNumber);
                sb.AppendLine();
                sb.Append(workbookSession.DomainOfExpertise.ShortName);

                return(sb.ToString());
            }

            return("Rebuild");
        }
        public void VerifyThatTheSessionDataIsWrittenToAWorkbookandCanBeRetrieved()
        {
            NetOffice.ExcelApi.Application application = null;
            Workbook workbook = null;

            try
            {
                application = new Application();
                workbook    = application.Workbooks.Open(this.testfilepath, false, false);

                var workbookSessionDal = new WorkbookSessionDal(workbook);
                workbookSessionDal.Write(this.workbookSession);
                workbook.Save();

                var retrievedWbSession = workbookSessionDal.Read();
                Assert.NotNull(retrievedWbSession);

                Assert.AreEqual(this.workbookSession.RebuildDateTime, retrievedWbSession.RebuildDateTime);
                Assert.AreEqual(this.workbookSession.Person.Iid, retrievedWbSession.Person.Iid);
                Assert.AreEqual(this.workbookSession.DomainOfExpertise.Iid, retrievedWbSession.DomainOfExpertise.Iid);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                throw ex;
            }
            finally
            {
                if (workbook != null)
                {
                    Console.WriteLine("Closing workbook {0}", this.testfilepath);
                    workbook.Close();
                    workbook.Dispose();
                }

                if (application != null)
                {
                    Console.WriteLine("Closing Excel Application");
                    application.Quit();
                    application.Dispose();
                }
            }
        }
        /// <summary>
        /// Gets the workbook that corresponds to the specified <see cref="Iteration"/>
        /// </summary>
        /// <param name="application">
        /// The Excel application
        /// </param>
        /// <param name="iteration">
        /// The <see cref="Iteration"/> for which the workbook is queried
        /// </param>
        /// <returns>
        /// The <see cref="Workbook"/> that corresponds to the queried Iteration, null if the workbook cannot be found.
        /// </returns>
        private Workbook QueryIterationWorkbook(Application application, Iteration iteration)
        {
            foreach (var workbook in application.Workbooks)
            {
                var workbookSessionDal = new WorkbookSessionDal(workbook);
                var workbookSession    = workbookSessionDal.Read();

                if (workbookSession == null)
                {
                    continue;
                }

                if (workbookSession.IterationSetup.IterationIid == iteration.Iid)
                {
                    return(workbook);
                }
            }

            return(null);
        }
        /// <summary>
        /// Submit all values sets that have changed on the parameter sheet of the active workbook
        /// </summary>
        private async Task SubmitAll()
        {
            var application = this.officeApplicationWrapper.Excel;

            var activeWorkbook = this.ExcelQuery.QueryActiveWorkbook(application);

            if (activeWorkbook == null)
            {
                return;
            }

            var workbookSessionDal = new WorkbookSessionDal(activeWorkbook);
            var workbookSession    = workbookSessionDal.Read();

            if (workbookSession == null)
            {
                return;
            }

            var iteration = this.Iterations.SingleOrDefault(x => x.Iid == workbookSession.IterationSetup.IterationIid);

            if (iteration == null)
            {
                logger.Debug("The values cannot be submitted: iteration {0} cannot be found", workbookSession.IterationSetup.IterationIid);
                return;
            }

            try
            {
                var workbookOperator = new WorkbookOperator(application, activeWorkbook, this.DialogNavigationService);
                await workbookOperator.SubmitAll(this.Session, iteration);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
        }
        public void VerifyThatIfCustomXmlPartDoesNotExistsTheWorkbookSessionIsNull()
        {
            NetOffice.ExcelApi.Application application = null;
            Workbook workbook = null;

            try
            {
                application = new Application();
                workbook    = application.Workbooks.Open(this.testfilepath, false, false);

                var workbookSessionDal = new WorkbookSessionDal(workbook);
                var retrievedWbSession = workbookSessionDal.Read();
                Assert.IsNull(retrievedWbSession);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                throw ex;
            }
            finally
            {
                if (workbook != null)
                {
                    Console.WriteLine("Closing workbook {0}", this.testfilepath);
                    workbook.Close();
                    workbook.Dispose();
                }

                if (application != null)
                {
                    Console.WriteLine("Closing Excel Application");
                    application.Quit();
                    application.Dispose();
                }
            }
        }