ワークフロー関連オブジェクトの操作
WorkflowDefinition オブジェクトの作成および取得、プロパティーと権限の設定および取得、Process Engine への転送、ワークフロー・サブスクリプションからの開始を行うことができます。
以下のコード例は、ワークフロー関連の操作を示しています。 Content Engine 上のワークフローの概要については、『ワークフロー』を参照してください。その他のサブスクリプション・コードの例については、サブスクリプションの操作を参照してください。
WorkflowDefinition オブジェクトの作成
次の Java™ および C# の例では、WorkflowDefinition オブジェクトを作成してオブジェクト・ストアに保持し、指定したフォルダーにファイリングする方法を示します。最初に、Factory クラスを使用して、新しい WorkflowDefinition オブジェクトを作成します。このオブジェクトに対しては、ドキュメント・タイトルとオブジェクトのコンテンツ・エレメントの 2 つのプロパティーが設定されます。これらのプロパティーは ContentTransfer クラスによって表されます。なお、ContentTransfer オブジェクトの設定には、WorkflowDefinition.pep ファイルのコンテンツとコンテンツ・タイプを使用します。WorkflowDefinition オブジェクトがチェックインされ、保存されると、Content Engine システムの Workflow Definitions フォルダーにこのオブジェクトが自動的に格納されます。
また、このコードは WorkflowDefinition オブジェクトを指定されたフォルダー Payroll に保存します。folder.file 呼び出しがドキュメント名に対して NULL 値を指定するため、WorkflowDefinition オブジェクトのデフォルト名 (DocumentTitle プロパティーに設定されている名前) が使用されます。
オプションで、WorkflowDefinition オブジェクトの作成時に権限を設定できます。コード例については、「アクセス権限の設定」を参照してください。
Java の例
public void createWorkflowDefinition(ObjectStore os) throws Exception
{
// WorkflowDefinition を作成し、プロパティーを設定。
WorkflowDefinition wdNew = Factory.WorkflowDefinition.createInstance(os,
(GuidConstants.Class_WorkflowDefinition).toString());
wdNew.getProperties().putValue("DocumentTitle", "Submit Timecards");
try
{
// 入力ストリームを作成し、それを ContentTransfer オブジェクト上で設定
FileInputStream inputStream = new FileInputStream("c:¥¥WorkflowDefinition.pep");
// non-Windows: FileInputStream inputStream = new FileInputStream("/tmp/WorkflowDefinition.pep");
ContentTransfer ctNew = Factory.ContentTransfer.createInstance();
ContentElementList contentList = Factory.ContentElement.createList();
ctNew.setCaptureSource(inputStream);
ctNew.set_ContentType("application/x-filenet-workflowdefinition");
contentList.add(ctNew);
// ワークフロー定義で ContentTransfer オブジェクトのリストを設定
wdNew.set_ContentElements(contentList);
}
catch (Exception e)
{
throw new Exception(e);
}
// ワークフロー定義をチェックイン
wdNew.checkin(AutoClassify.DO_NOT_AUTO_CLASSIFY, CheckinType.MAJOR_VERSION);
wdNew.save(RefreshMode.REFRESH);
// ワークフロー定義をフォルダーにファイリング
Folder folder=Factory.Folder.getInstance(os, ClassNames.FOLDER, "/Payroll");
DynamicReferentialContainmentRelationship drcr =
(DynamicReferentialContainmentRelationship)folder.file((IndependentlyPersistableObject)wdNew,
AutoUniqueName.AUTO_UNIQUE,
null,
DefineSecurityParentage.DO_NOT_DEFINE_SECURITY_PARENTAGE);
drcr.save(RefreshMode.NO_REFRESH);
}
C# の例
public void CreateWorkflowDefinition(IObjectStore os)
{
// WorkflowDefinition を作成し、プロパティーを設定。
IWorkflowDefinition wdNew = Factory.WorkflowDefinition.CreateInstance(os,
(GuidConstants.Class_WorkflowDefinition).ToString());
wdNew.Properties["DocumentTitle"] = "Submit Timecards";
// 入力ストリームを作成し、それを ContentTransfer オブジェクト上で設定
try
{
Stream fileStream = File.OpenRead(@"C:¥¥WorkflowDefinition.pep");
IContentTransfer ctNew = Factory.ContentTransfer.CreateInstance();
IContentElementList contentList = Factory.ContentElement.CreateList();
ctNew.SetCaptureSource(fileStream);
ctNew.ContentType="application/x-filenet-workflowdefinition";
contentList.Add(ctNew);
wdNew.ContentElements=contentList;
}
catch (Exception e)
{
throw new Exception("Error getting content from workflow", e);
}
// ワークフロー定義をチェックイン
wdNew.Checkin(AutoClassify.DO_NOT_AUTO_CLASSIFY, CheckinType.MAJOR_VERSION);
wdNew.Save(RefreshMode.REFRESH);
// ワークフロー定義をフォルダーにファイリング
IFolder folder=Factory.Folder.GetInstance(os, ClassNames.FOLDER, "/Payroll");
IDynamicReferentialContainmentRelationship drcr =
(IDynamicReferentialContainmentRelationship)folder.File((IIndependentlyPersistableObject)wdNew,
AutoUniqueName.AUTO_UNIQUE,
null,
DefineSecurityParentage.DO_NOT_DEFINE_SECURITY_PARENTAGE);
drcr.Save(RefreshMode.NO_REFRESH);
}
WorkflowDefinition オブジェクトの取得
次の Java および C# の例に、ワークフロー・サブスクリプションに使用するワークフロー定義を取得する方法を示します。ワークフロー・サブスクリプションは Process Engine 上に存在するワークフロー定義としか連携できないため、これらの例ではワークフロー定義の VWVersion プロパティーをテストします。このプロパティーは、転送されるワークフロー定義の識別子となります。Java の例では Process Java API の VWSession オブジェクトを使用しますが、C# の例では使用しません。これは、Process Engine に .NET API が組み込まれていないためです。
Java の例では、2 つのメソッドを示します。最初のメソッド getWorkFlowDefinitionDocument は、VWSession オブジェクトを使用してワークフロー定義の有効性をテストします。この最初のメソッドが 2 番目のメソッド getVWSession を呼び出し、2 番目のメソッドが Process Java API を使用して、VWSession オブジェクトを作成し、返します。VWSession オブジェクトを作成するには、Process Engine の接続ポイント識別子と Content Engine URI が必要です。
VWSession オブジェクトが返されると、その checkWorkflowIdentifier メソッドが呼び出されます。このメソッドは指定したワークフロー識別子に基づいて、ワークフローが Process Engine 上にあるかどうかを示します。この呼び出しでは余分のチェックを行います。これは、ワークフロー定義が以前に転送されたが、予期しないイベントが発生したために、Process Engine 上に存在しなくなった可能性があるためです。Process Engine 上にワークフローが存在しない場合は、transferWorkflowDefinition メソッドが呼び出されます。このメソッドは転送を実行し、ワークフロー定義の VWVersion プロパティーで設定された識別子を返します。
Java の例
public WorkflowDefinition getWorkFlowDefinitionDocument(ObjectStore os, String conn_point, String ceUri,
String workflowDefinitionId ) throws Exception
{
// Get the Workflow Definition document object.
PropertyFilter pf = new PropertyFilter();
pf.addIncludeProperty(new FilterElement(null, null, null, PropertyNames.VWVERSION, null));
WorkflowDefinition wfdDocument = Factory.WorkflowDefinition.fetchInstance( os, new Id(workflowDefinitionId), pf );
// 現在の VW バージョン番号を取得。
String vwCurrentVersion = wfdDocument.get_VWVersion();
// Calls method getVWSession to get the VWSession object, which represents the Process Engine session.
VWSession vwSession = getVWSession(conn_point, ceUri);
// 現在の VW バージョン番号の有効性を検査。無効な場合、
// transferWorkflowDefinition を呼び出す。
if ( vwCurrentVersion == null || !vwSession.checkWorkflowIdentifier(vwCurrentVersion) )
{
String vwNewVersion;
System.out.println("Workflow definition does not have a VW version number, or it is not up to date");
vwNewVersion = transferWorkflowDefinition(os, vwSession, wfdDocument);
wfdDocument.set_VWVersion(vwNewVersion);
wfdDocument.save(RefreshMode.REFRESH);
}
return wfdDocument;
}
//Returns the Process Engine session.
public VWSession getVWSession(String conn_point, String ceUri) throws Exception
{
VWSession vwSession=new VWSession();
vwSession.setBootstrapCEURI(ceUri);
try
{
vwSession.logon(conn_point);
}
catch (VWException vwe)
{
throw new Exception("Cannot connect to the connection point.");
}
return vwSession;
}
C# の例
public IWorkflowDefinition GetWorkFlowDefinitionDocument(IObjectStore os, String workflowDefinitionId )
{
// Get the Workflow Definition document object.
PropertyFilter pf = new PropertyFilter();
pf.AddIncludeProperty(new FilterElement(null, null, null, PropertyNames.VWVERSION, null));
IWorkflowDefinition wfdDocument = Factory.WorkflowDefinition.FetchInstance( os, new Id(workflowDefinitionId), pf );
// To determine if the workflow definition has previously been transferred to Process Engine,
// get and test the VW version number.
String vwCurrentVersion = wfdDocument.VWVersion;
if (vwCurrentVersion == null)
{
throw new Exception("Workflow definition has not been transferred to Process Engine.¥n" +
"Use Process Designer to transfer the workflow.");
}
// Return the Workflow Definition document.
return wfdDocument;
}
ワークフロー定義を Process Engine に転送
次の Java の例では、Content Engine Java API および Process Java API を使用して、ワークフロー定義をプログラムで転送する方法を示しています。Process Engine 用の .NET API はありません。Content Engine .NET API を使用する場合は、Process Designer アプリケーションを使用して、ワークフロー定義を手動で転送する必要があります。
例は 2 つのメソッドから成ります。最初の transferWorkflowDefinition は、指定した Content Engine ワークフロー定義を Process Engine に転送します。このメソッドが 2 番目のメソッド getContents を呼び出し、2 番目のメソッドがワークフロー定義のコンテンツを取得し、InputStream オブジェクトの形式で返します。
transferWorkflowDefinition メソッドは、Process Engine Java API から次のオブジェクトを使用します。
- VWWorkflowDefinition オブジェクトは、Process Engine に転送され、ワークフローとしてコンパイルされるワークフロー定義を表します。このオブジェクトは、Content Engine 上のワークフロー定義のコンテンツから作成されます。
- VWSession は、Process Engine セッションを表します。このオブジェクトは、VWWorkflowDefinition オブジェクトを Process Engine に転送するために使用します。
- VWTransferResult オブジェクトは vwSession.transfer 呼び出しによって返され、呼び出し結果を評価するために使用します。
Java の例
public String transferWorkflowDefinition(ObjectStore os, VWSession vwSession,
WorkflowDefinition workflowDocCE) throws Exception
{
String workflowVersion = null;
// VWSession オブジェクトに加え、次の Process Java API オブジェクトが使用される
VWWorkflowDefinition workflowDocPE = new VWWorkflowDefinition();
VWTransferResult transferResult = null;
// Get the definition content from the Content Engine object store to be transferred to Process Engine.
// getContents メソッドを呼び出す。
workflowDocPE = VWWorkflowDefinition.read( getContents(os.get_Name(), workflowDocCE) );
// 現在のバージョン・シリーズ ID を取得し、それを使用して転送ワークフローの固有名を作成
String verSerId = workflowDocCE.get_VersionSeries().get_Id().toString();
String canonicalName = "document" + ":" + os.get_Name() + ":" + verSerId + ":" + workflowDocCE.get_Id().toString();
// ワークフローを転送
try {
transferResult = vwSession.transfer(workflowDocPE, canonicalName, false, false);
}
catch (VWException e)
{
throw new Exception (e.getCauseDescription());
}
if ( ! transferResult.success() )
throw new Exception("Failed to transfer workflow definition");
else
workflowVersion = transferResult.getVersion();
// 転送されたワークフロー定義の識別子を返す
return workflowVersion;
}
// ワークフロー定義コンテンツを返す。
public InputStream getContents(String objectStoreName, WorkflowDefinition workflowDocCE) throws Exception
{
InputStream inStream = null;
ContentElementList contentList = Factory.ContentElement.createList();
ContentTransfer ctObject;
contentList = workflowDocCE.get_ContentElements();
try {
ctObject = (ContentTransfer) contentList.get(0);
}
catch (Exception e)
{
throw new Exception("Failed to retrieve document content.");
}
inStream = ctObject.accessContentStream();
return inStream;
}
ワークフロー・サブスクリプション・オブジェクトの作成
次の Java と C# の例では、ターゲット Document オブジェクトに対して InstanceWorkflowSubscription オブジェクトを、またターゲット DocumentClassDefinition クラスに対して ClassWorkflowSubscription を作成する方法を示しています。これら 2 つのタイプのワークフロー・サブスクリプションを作成するコードは大変よく似ています。例では、クラス・ワークフロー・サブスクリプションの作成に特有のコードはコメント化されています。
オプションで、ワークフロー・サブスクリプションの作成時に権限を設定できます。コード例については、「アクセス権限の設定」を参照してください。
Java の例
public void addWorkflowSubscription(ObjectStore os, Domain domain, String conn_point, String ceUri,
String workflowDefinitionId, String targetObjectId, String eventActionGuid) throws Exception
{
// インスタンスのワークフロー・サブスクリプションを作成
InstanceWorkflowSubscription wfSubscription =
Factory.InstanceWorkflowSubscription.createInstance(os, ClassNames.INSTANCE_WORKFLOW_SUBSCRIPTION);
// クラス・ワークフロー・サブスクリプションを作成。
/* ClassWorkflowSubscription wfSubscription =
Factory.ClassWorkflowSubscription.createInstance(os, ClassNames.CLASS_WORKFLOW_SUBSCRIPTION);*/
// ターゲット・ドキュメントを取得。
Document targetObject = Factory.Document.getInstance( os, "Document", new Id(targetObjectId) );
// ターゲット・クラスを取得。
// DocumentClassDefinition targetObject = Factory.DocumentClassDefinition.getInstance(os, new Id(targetObjectId) );
// ワークフロー定義ドキュメントを取得。メソッド getWorkFlowDefinitionDocument を呼び出す。
WorkflowDefinition workflowDefDocument = getWorkFlowDefinitionDocument(os, conn_point, uri, workflowDefinitionId);
// イベント・アクションを取得。
WorkflowEventAction eventAction = Factory.WorkflowEventAction.getInstance(os, ClassNames.WORKFLOW_EVENT_ACTION, new Id(eventActionGuid) );
// サブスクリプション付きイベントのリストを作成
Id subscribedEventId = GuidConstants.Class_CheckoutEvent;
EventClassDefinition evDef = Factory.EventClassDefinition.getInstance(
os, subscribedEventId);
SubscribedEvent subEvent = Factory.SubscribedEvent.createInstance();
subEvent.set_EventClass(evDef);
SubscribedEventList subEventList = Factory.SubscribedEvent.createList();
subEventList.add(subEvent);
// サブスクリプション・プロパティーを設定する
wfSubscription.set_SubscribedEvents(subEventList);
wfSubscription.set_SubscriptionTarget(targetObject);
wfSubscription.set_WorkflowDefinition(workflowDefDocument);
wfSubscription.set_EventAction(eventAction);
wfSubscription.set_DisplayName("InstanceWorkflowSubscriptionJava");
//wfSubscription.set_DisplayName("ClassWorkflowSubscriptionJava");
wfSubscription.set_VWVersion(workflowDefDocument.get_VWVersion());
PEConnectionPoint peConnPoint = Factory.PEConnectionPoint.fetchInstance(domain, conn_point, null);
wfSubscription.set_IsolatedRegionNumber(peConnPoint.get_IsolatedRegion().get_IsolatedRegionNumber());
// サブスクリプションを保存
wfSubscription.save(RefreshMode.REFRESH);
}
C# の例
public void AddInstanceWorkflowSubscription(IObjectStore os, IDomain domain, String connPoint, String ceUri,
String workflowDefinitionId, String targetObjectId, String eventActionGuid)
{
// インスタンスのワークフロー・サブスクリプションを作成
IInstanceWorkflowSubscription wfSubscription =
Factory.InstanceWorkflowSubscription.CreateInstance(os, ClassNames.INSTANCE_WORKFLOW_SUBSCRIPTION);
// クラス・ワークフロー・サブスクリプションを作成。
/* IClassWorkflowSubscription wfSubscription =
Factory.ClassWorkflowSubscription.CreateInstance(os, ClassNames.CLASS_WORKFLOW_SUBSCRIPTION);*/
// ターゲット・ドキュメントを取得。
IDocument targetDocument = Factory.Document.GetInstance(os, ClassNames.DOCUMENT, new Id(targetObjectId));
// ターゲット・クラスを取得。
// IDocumentClassDefinition targetObject = Factory.DocumentClassDefinition.GetInstance(os, new Id(targetObjectId));
// ワークフロー定義ドキュメントを取得。メソッド GetWorkFlowDefinitionDocument を呼び出す。
IWorkflowDefinition workflowDefDocument = GetWorkFlowDefinitionDocument(os, workflowDefinitionId);
// イベント・アクションを取得。
IWorkflowEventAction eventAction = Factory.WorkflowEventAction.GetInstance(os, ClassNames.WORKFLOW_EVENT_ACTION, new Id(eventActionGuid));
// サブスクリプション付きイベントのリストを作成
Id subscribedEventId = GuidConstants.Class_CheckoutEvent;
IEventClassDefinition evDef = Factory.EventClassDefinition.GetInstance(
os, subscribedEventId);
ISubscribedEvent subEvent = Factory.SubscribedEvent.CreateInstance();
subEvent.EventClass = evDef;
ISubscribedEventList subEventList = Factory.SubscribedEvent.CreateList();
subEventList.Add(subEvent);
// サブスクリプション・プロパティーを設定する
wfSubscription.SubscribedEvents = subEventList;
wfSubscription.SubscriptionTarget = targetObject;
wfSubscription.WorkflowDefinition = workflowDefDocument;
wfSubscription.EventAction = eventAction;
wfSubscription.DisplayName = "InstanceWorkflowSubscriptionCSharp";
// wfSubscription.DisplayName = "ClassWorkflowSubscriptionCSharp";
wfSubscription.VWVersion = workflowDefDocument.VWVersion;
IPEConnectionPoint peConnPoint = Factory.PEConnectionPoint.FetchInstance(domain, connPoint, null);
wfSubscription.IsolatedRegionNumber = peConnPoint.IsolatedRegion.IsolatedRegionNumber;
// サブスクリプションを保存
instanceWfSubscription.Save(RefreshMode.REFRESH);
}
ワークフロー・サブスクリプションの取得
Factory メソッドを使用して、1 つの InstanceWorkflowSubscription または ClassWorkflowSubscription オブジェクトを取得できます。また、次のようにして、サブスクリプション関連のコレクションから InstanceWorkflowSubscription および ClassWorkflowSubscription オブジェクトを取得することもできます。
SubscriptionSet オブジェクトから
「サブスクリプションの取得」の説明に従って、SubscriptionSet オブジェクトを取得します。Java および C# のコード例に示すように、SubscriptionSet オブジェクトを反復処理し、InstanceWorkflowSubscription および ClassWorkflowSubscription オブジェクトをフィルターすることができます。
Java の例
...
Iterator subscriptionsIter = subscriptions.iterator();
while (subscriptionsIter.hasNext())
{
Subscription subscription = (Subscription) subscriptionsIter.next();
String className = subscription.getClassName();
if ( className.equals(ClassNames.CLASS_WORKFLOW_SUBSCRIPTION) ) ||
( className.equals(ClassNames.INSTANCE_WORKFLOW_SUBSCRIPTION) )
{
System.out.println(className + " is " + subscription.get_DisplayName());
}
}
C# の例
...
foreach (ISubscription subscription in subscriptions)
{
String className = subscription.GetClassName();
if ( className.Equals(ClassNames.CLASS_WORKFLOW_SUBSCRIPTION) ||
className.Equals(ClassNames.INSTANCE_WORKFLOW_SUBSCRIPTION) )
{
System.Console.WriteLine(className + " is " + subscription.DisplayName);
}
}
InstanceWorkflowSubscriptionSet オブジェクトから
Subscribable サブオブジェクト (VersionSeries、Folder、Document、および CustomObject) から InstanceWorkflowSubscriptionSet オブジェクトを取得します。次の Java および C# のコード例では、InstanceWorkflowSubscriptionSet オブジェクトが Document オブジェクトの WorkflowSubscriptions プロパティーから返されます。この後、InstanceWorkflowSubscription オブジェクトのコレクションが反復処理されます。
Java の例
...
Document doc = Factory.Document.fetchInstance( os, new Id(targetDocId), null );
InstanceWorkflowSubscriptionSet instanceWfSubscriptions = doc.get_WorkflowSubscriptions();
subscriptionsIter = instanceWfSubscriptions.iterator();
while (subscriptionsIter.hasNext())
{
Subscription instanceWfSubscription = (Subscription) subscriptionsIter.next();
System.out.println(instanceWfSubscription.get_DisplayName());
}
C# の例
...
IDocument doc = Factory.Document.FetchInstance( os, new Id(targetDocId), null );
IInstanceWorkflowSubscriptionSet instanceWfSubscriptions = doc.WorkflowSubscriptions;
foreach (ISubscription subscription in instanceWfSubscriptions)
{
System.Console.WriteLine(subscription.DisplayName);
}
ClassWorkflowSubscriptionSet オブジェクトから
ClassDefinition サブオブジェクトから ClassWorkflowSubscriptionSet オブジェクトを取得します。次の Java および C# のコード例では、ClassWorkflowSubscriptionSet オブジェクトが SubscribableClassDefinition オブジェクトの WorkflowSubscriptions プロパティーから返されます。この後、ClassWorkflowSubscription オブジェクトのコレクションが反復処理されます。
Java の例
...
SubscribableClassDefinition docClass = Factory.SubscribableClassDefinition.fetchInstance(os, targetClassGuid, null);
ClassWorkflowSubscriptionSet classWfSubscriptions = docClass.get_WorkflowSubscriptions();
subscriptionsIter = classWfSubscriptions.iterator();
while (subscriptionsIter.hasNext())
{
Subscription classWfSubscription = (Subscription) subscriptionsIter.next();
System.out.println(classWfSubscription.get_DisplayName());
}
C# の例
...
ISubscribableClassDefinition docClass = Factory.SubscribableClassDefinition.FetchInstance(os, targetClassGuid, null );
IClassWorkflowSubscriptionSet classWfSubscriptions = docClass.WorkflowSubscriptions;
foreach (ISubscription subscription in classWfSubscriptions)
{
System.Console.WriteLine(subscription.DisplayName);
}