Exemplo n.º 1
0
        private bool TryProcessFile(JobItemDocument file, CancellationToken cancellationToken)
        {
            LogMessage($"Processing '{file.FilePath}'");

            var doc = file.Document;

            var closeDoc = false;
            var hideDoc  = false;

            if (!doc.IsCommitted)
            {
                closeDoc = true;
            }
            else
            {
                hideDoc = doc.State.HasFlag(DocumentState_e.Hidden);
            }

            try
            {
                file.ClearIssues();

                if (!doc.IsCommitted)
                {
                    var state = DocumentState_e.Silent;

                    if (!m_ActivateDocs)
                    {
                        state |= DocumentState_e.Hidden;
                    }

                    if (m_AllowReadOnly)
                    {
                        state |= DocumentState_e.ReadOnly;
                    }

                    if (m_AllowRapid)
                    {
                        state |= DocumentState_e.Rapid;
                    }

                    doc.State = state;
                    doc.Commit(cancellationToken);
                }

                if (m_ActivateDocs)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    m_App.Documents.Active = doc;
                }

                foreach (var macro in file.Macros)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    TryRunMacro(macro, doc);
                }

                if (m_AutoSaveDocs)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    doc.Save();
                }

                if (file.Macros.All(m => m.Status == JobItemStatus_e.Succeeded))
                {
                    file.Status = JobItemStatus_e.Succeeded;
                }
                else if (file.Macros.Any(m => m.Status == JobItemStatus_e.Succeeded))
                {
                    file.Status = JobItemStatus_e.Warning;
                }
                else
                {
                    file.Status = JobItemStatus_e.Failed;
                }
            }
            catch (Exception ex)
            {
                LogMessage($"Failed to process file '{file.FilePath}': {ex.ParseUserError()}");
                file.Status = JobItemStatus_e.Failed;
                file.ReportError(ex);
            }
            finally
            {
                try
                {
                    if (doc != null && doc.IsCommitted)
                    {
                        if (closeDoc)
                        {
                            if (doc.IsDirty)
                            {
                                if (file.Status == JobItemStatus_e.Succeeded)
                                {
                                    file.Status = JobItemStatus_e.Warning;
                                }

                                file.ReportIssue("The document has been modified and closed without saving changes. Use 'Auto Save' option or call Save API function from the macro directly if it is required to keep the changes");
                            }

                            m_Logger.Log($"Closing '{doc.Path}'", LoggerMessageSeverity_e.Debug);
                            doc.Close();
                        }
                        else if (hideDoc)
                        {
                            m_Logger.Log($"Hiding '{doc.Path}'", LoggerMessageSeverity_e.Debug);
                            var hiddenState = doc.State | DocumentState_e.Hidden;
                            doc.State = hiddenState;
                        }
                    }
                }
                catch (Exception ex)
                {
                    m_Logger.Log(ex);

                    if (file.Status == JobItemStatus_e.Succeeded)
                    {
                        file.Status = JobItemStatus_e.Warning;
                    }

                    file.ReportError(ex, "Failed to close document");
                }
            }

            return(file.Status == JobItemStatus_e.Succeeded || file.Status == JobItemStatus_e.Warning);
        }
        private bool TryProcessFile(JobItemDocument file, CancellationToken cancellationToken)
        {
            LogMessage($"Processing '{file.FilePath}'");

            var doc = file.Document;

            var closeDoc = !doc.IsCommitted || doc.State.HasFlag(DocumentState_e.Hidden);

            try
            {
                if (!doc.IsCommitted)
                {
                    var state = DocumentState_e.Silent;

                    if (!m_ActivateDocs)
                    {
                        state |= DocumentState_e.Hidden;
                    }

                    doc.State = state;
                    doc.Commit(cancellationToken);
                }

                if (m_ActivateDocs)
                {
                    m_App.Documents.Active = doc;
                }

                foreach (var macro in file.Macros)
                {
                    TryRunMacro(macro, doc);
                }

                if (file.Macros.All(m => m.Status == JobItemStatus_e.Succeeded))
                {
                    file.Status = JobItemStatus_e.Succeeded;
                }
                else if (file.Macros.Any(m => m.Status == JobItemStatus_e.Succeeded))
                {
                    file.Status = JobItemStatus_e.Warning;
                }
                else
                {
                    file.Status = JobItemStatus_e.Failed;
                }
            }
            catch (Exception ex)
            {
                LogMessage($"Failed to process file '{file.FilePath}': {ex.ParseUserError(out _)}");
                file.Status = JobItemStatus_e.Failed;
            }
            finally
            {
                if (closeDoc)
                {
                    try
                    {
                        if (doc != null && doc.IsCommitted)
                        {
                            doc.Close();
                        }
                    }
                    catch
                    {
                    }
                }
            }

            return(file.Status == JobItemStatus_e.Succeeded || file.Status == JobItemStatus_e.Warning);
        }