Skip to content
Rain Hu's Workspace
Go back

[DXP] 在 spotfire 中創建自定義任務

Rain Hu

簡介

Spotfire 用語

名詞解釋
TaskTask 是用於創建複雜工作的基礎模塊。自動化服務附帶了許多預定義的 Tasks,例如應用書籤或發送電子郵件或乃至其它可擴充的 Tasks。
Job排程好待執行的 Tasks 組合成一個 Job,Job 又被儲存於 Job File 中。
Job FileJob File 是一個可讀的 XML,用於定義 Job。
Job BuilderJob Builder 是一個在 Spotfire 專業版中運行的應用程式,提供了一個易於使用的界面,用於創建和編輯工作。
Job SenderClientJobSender 是一個工具,可將作業發送到 Spotfire Server 以進行執行。使用 ClientJobSender 設置定期或事件驅動的作業執行。

架構

pic1

Job File 格式

<as:Job xmlns:as="urn:tibco:spotfire.dxp.automation">
  <as:Tasks>
    <OpenAnalysisFromLibrary xmlns="urn:tibco:spotfire.dxp.automation.tasks">
      <as:Title>Open Analysis from Library</as:Title>
      <AnalysisPath>/Users/Username/Baseball stats</AnalysisPath>
    </OpenAnalysisFromLibrary>
    <ExportImage xmlns="urn:tibco:spotfire.dxp.automation.tasks">
      <as:Title>Export Image</as:Title>
      <VisualizationId>292a90c6-0e03-47fe-961e-a528a14e9735</VisualizationId>
      <DestinationPath>C:inetpubwwwrootMyWebServerImage.png</DestinationPath>
      <Width>640</Width>
      <Height>480</Height>
    </ExportImage>
  </as:Tasks>
</as:Job>

自動化 Task 範例

// 自定義任務的命名空間
[XmlRoot("urn:spotfiredeveloper.automationservicesexample")]
public sealed class ApplyBookmark : Task
{
    // 建立一個無參建構式,並呼叫 base 建構式,同時傳入標題與描述
    public ApplyBookmark()
        : base(Properties.Resources.ApplyBookmarkTitle, Properties.Resources.ApplyBookmarkDescription)
    {
    }

    // 一些 public 的 properties 可供 UI 存取
    [Description("The GUID of the bookmark to apply")]
    public Guid BookmarkId { get; set; }
}
protected override TaskExecutionStatus ExecuteCore(TaskExecutionContext context)
{
    // 沒有文件被讀取
    if (context.Application.Document == null)
    {
        return new TaskExecutionStatus(false, Properties.Resources.NoAnalysisLoaded);
    }

    // 檢查書籤是否存在於新書籤中
    var manager = context.Application.GetService<BookmarkManager>();
    if (manager != null)
    {
        Bookmark bookmark;
        if (manager.TryGetBookmark(this.BookmarkId, out bookmark))
        {
            // 套用書籤
            manager.Apply(bookmark);
            return new TaskExecutionStatus(true);
        }
    }

    // 檢查書籤是否存在於舊書籤中
    foreach (var bookmark in context.Application.Document.Bookmarks)
    {
        if (bookmark.Id == this.BookmarkId)
        {
            // 套用書籤
            bookmark.Apply();
            return new TaskExecutionStatus(true);
        }
    }

    // 找不到書籤,報錯
    Console.WriteLine("Error in execution of job {0}. The bookmark {1} was not found.", context.Id, this.BookmarkId);
    return new TaskExecutionStatus(false, Common.Format(Properties.Resources.BookmarkNotAvaliable, this.BookmarkId));
}

註冊

public sealed class TasksAddIn : RegisterTasksAddIn
{
    public override void RegisterTasks(TaskRegistrar registrar)
    {
        registrar.Register(new ApplyBookmark());
    }
}

Share this post on:

Previous
[DXP] 在 spotfire 中創建自定義工具
Next
[DXP] 使用 Visual Studio 對 Spotfire 開發進行環境設置