天天看點

EventAction的代碼編寫及相關作用說明

在com.filenet.api.engine包中提供了一些與EventActions和運作在CE上非FileNet的代碼相關的類和接口。此包中的六個類和六個接口中隻有三個接口是可以被implement,其他都是CE内部所用。這三個接口是:DocumentClassifier、DocumentLifecycleActionHandler和EventActionHandler。對于上文所講的subscription的配置過程,我們可以通過EventActionHandler來建立自定義的EventAction,來完成觸發某事件時要執行的動作。

接口EventActionHandler提供了一個方法:void onEvent(ObjectChangeEvent event, Id subscriptionId),下面代碼展示了一個自定義EventAction

import com.filenet.api.engine.EventActionHandler;
import com.filenet.api.events.*;
import com.filenet.api.property.*;
import com.filenet.api.security.*;
import com.filenet.api.util.Id;
import com.filenet.api.exception.*;
import com.filenet.api.core.*;
import com.filenet.api.collection.GroupSet;
import com.filenet.api.constants.*;
import java.util.Iterator;

public class FilterNewDocumentsEventHandler implements EventActionHandler
{
   public void onEvent(ObjectChangeEvent event, Id subId)
   {
      try
      {
         // As a best practice, fetch the persisted source object of the event, 
         // filtered on the two required properties, Owner and Name.
         ObjectStore os = event.getObjectStore();
         Id id = event.get_SourceObjectId();
         FilterElement fe = new FilterElement(null, null, null, "Owner Name", null);	
         PropertyFilter pf = new PropertyFilter();
         pf.addIncludeProperty(fe);
         Document doc = (Document)Factory.Document.fetchInstance(os, id, pf);
         User user = Factory.User.fetchInstance(os.getConnection(), doc.get_Owner(), null);
         GroupSet groups = user.get_MemberOfGroups();
         Iterator groupsIter = groups.iterator();
         while (groupsIter.hasNext())
         {
            Group group = (Group) groupsIter.next();
            if ( group.get_ShortName().equals("NOL") )
            {
               Folder folder=Factory.Folder.fetchInstance(os, "/Special Processing", null);
               // File form and save.
               DynamicReferentialContainmentRelationship drcr =
                   (DynamicReferentialContainmentRelationship)folder.file((IndependentlyPersistableObject)doc,
                  AutoUniqueName.AUTO_UNIQUE,
                  doc.getProperties().getStringValue("Name"),
                  DefineSecurityParentage.DO_NOT_DEFINE_SECURITY_PARENTAGE);
               drcr.save(RefreshMode.NO_REFRESH);
               break;
            }
         }
      }
      catch (Exception e)
      {
         ErrorRecord er[] = {new ErrorRecord (e)};
         throw new EngineRuntimeException(e, ExceptionCode.EVENT_HANDLER_THREW, er);
      }
   }
}
           

通過上述代碼可以看出,有了event這個參數可以擷取觸發事件發生的類,及操作FileNeT相關API必要資訊,如ObjectStore等等,進而可以完成事件處理過程。有了EventAction類的編寫執行個體,加之上文所談及的如何配置subscription,我們大體上對這塊知識有了初步認識,下面在簡單陳述下subscription所涉及的三個核心概念:event action, target object, and triggerevent。

Event action 是指一個實作了 Content Engine API's EventActionHandler 接口的 Java™ 類,即描述了當某一個 trigger event 觸發 target object 定義的 subscription 時,Content Engine 需要執行的操作。

Event action 有着廣泛的用途,如:

  • 通知 / 通訊:你可以實作一個 event action 用來自動根據特定的事件和源對象給合适的人發送相關的 email 等資訊。
  • 記錄日志 / 計數:通過跟蹤 CE 中各種不同對象和事件的使用頻率來自動生成關于事件的詳細記錄,或者自動增加相關計數器,并将這些資訊寫入資料庫或者檔案中。
  • 過濾:對于像文檔生命周期(document life cycle)或者工作流流程(workflow process)等複雜的流程,您可能需要一個進階的過濾器用來在系統接收某個 document 之前對于這個 document 的内容或者 document 本身進行過濾。您也可以編寫一個 event handler 來拒絕某些特定的 document 的進入并且中止或者復原事務(transaction)。

Target object 是指被 subscription 作用的對象,它可以是以下幾種類型:

  • Class definition:在這種情況下,subscription 可以影響到它的所有 instance 。
  • Single instance:在這種情況下,subscription 隻會影響到該對象的某一個 version 。
  • Version series:在這種情況下,subscription 可以影響到一個 versionable object 的所有 version,而每個 version 都是一個獨立的 instance 。

《ventAction和subscription的配置過程》的例子就屬于 Class definition 的情況。

雖然您可以将 subscription 指派給獨立的 instance,但是如果将 subscription 指派給 class 會讓它變得更加有效,因為将 subscription 指派給 class 會保證它的對象會得到一緻的管理。而且,在 class 上建立 subscription 會減少同時運作的 subscription 的總量,進而提升系統性能。

Trigger event 是指希望被用來觸發某個 subscription 的事件,而 target object 的類型決定了哪些 trigger event 類型對于其上的 subscription 是可用的。

本文講述了EventAction的自定義開發方式和配置subscription的相關步驟,也簡要說明了下subscription在FileNet中的作用,在今後的業務其必将發揮巨大作用。